org_xemacs.el 810 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981
  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 <dominik at science dot uva dot nl>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://www.astro.uva.nl/~dominik/Tools/org/
  8. ;; Version: 4.78
  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 2, 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://staff.science.uva.nl/~dominik/Tools/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://www.astro.uva.nl/~dominik/Tools/org/Changes
  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 "4.78"
  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. ;;; The custom variables
  92. (defgroup org nil
  93. "Outline-based notes management and organizer."
  94. :tag "Org"
  95. :group 'outlines
  96. :group 'hypermedia
  97. :group 'calendar)
  98. (defgroup org-startup nil
  99. "Options concerning startup of Org-mode."
  100. :tag "Org Startup"
  101. :group 'org)
  102. (defcustom org-startup-folded t
  103. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  104. This can also be configured on a per-file basis by adding one of
  105. the following lines anywhere in the buffer:
  106. #+STARTUP: fold
  107. #+STARTUP: nofold
  108. #+STARTUP: content"
  109. :group 'org-startup
  110. :type '(choice
  111. (const :tag "nofold: show all" nil)
  112. (const :tag "fold: overview" t)
  113. (const :tag "content: all headlines" content)))
  114. (defcustom org-startup-truncated t
  115. "Non-nil means, entering Org-mode will set `truncate-lines'.
  116. This is useful since some lines containing links can be very long and
  117. uninteresting. Also tables look terrible when wrapped."
  118. :group 'org-startup
  119. :type 'boolean)
  120. (defcustom org-startup-align-all-tables nil
  121. "Non-nil means, align all tables when visiting a file.
  122. This is useful when the column width in tables is forced with <N> cookies
  123. in table fields. Such tables will look correct only after the first re-align.
  124. This can also be configured on a per-file basis by adding one of
  125. the following lines anywhere in the buffer:
  126. #+STARTUP: align
  127. #+STARTUP: noalign"
  128. :group 'org-startup
  129. :type 'boolean)
  130. (defcustom org-insert-mode-line-in-empty-file nil
  131. "Non-nil means insert the first line setting Org-mode in empty files.
  132. When the function `org-mode' is called interactively in an empty file, this
  133. normally means that the file name does not automatically trigger Org-mode.
  134. To ensure that the file will always be in Org-mode in the future, a
  135. line enforcing Org-mode will be inserted into the buffer, if this option
  136. has been set."
  137. :group 'org-startup
  138. :type 'boolean)
  139. (defcustom org-replace-disputed-keys nil
  140. "Non-nil means use alternative key bindings for some keys.
  141. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  142. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  143. If you want to use Org-mode together with one of these other modes,
  144. or more generally if you would like to move some Org-mode commands to
  145. other keys, set this variable and configure the keys with the variable
  146. `org-disputed-keys'.
  147. This option is only relevant at load-time of Org-mode, and must be set
  148. *before* org.el is loaded. Changing it requires a restart of Emacs to
  149. become effective."
  150. :group 'org-startup
  151. :type 'boolean)
  152. (if (fboundp 'defvaralias)
  153. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  154. (defcustom org-disputed-keys
  155. '(([(shift up)] . [(meta p)])
  156. ([(shift down)] . [(meta n)])
  157. ([(shift left)] . [(meta -)])
  158. ([(shift right)] . [(meta +)])
  159. ([(control shift right)] . [(meta shift +)])
  160. ([(control shift left)] . [(meta shift -)]))
  161. "Keys for which Org-mode and other modes compete.
  162. This is an alist, cars are the default keys, second element specifies
  163. the alternative to use when `org-replace-disputed-keys' is t.
  164. Keys can be specified in any syntax supported by `define-key'.
  165. The value of this option takes effect only at Org-mode's startup,
  166. therefore you'll have to restart Emacs to apply it after changing."
  167. :group 'org-startup
  168. :type 'alist)
  169. (defun org-key (key)
  170. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  171. Or return the original if not disputed."
  172. (if org-replace-disputed-keys
  173. (let* ((nkey (key-description key))
  174. (x (org-find-if (lambda (x)
  175. (equal (key-description (car x)) nkey))
  176. org-disputed-keys)))
  177. (if x (cdr x) key))
  178. key))
  179. (defun org-find-if (predicate seq)
  180. (catch 'exit
  181. (while seq
  182. (if (funcall predicate (car seq))
  183. (throw 'exit (car seq))
  184. (pop seq)))))
  185. (defun org-defkey (keymap key def)
  186. "Define a key, possibly translated, as returned by `org-key'."
  187. (define-key keymap (org-key key) def))
  188. (defcustom org-ellipsis nil
  189. "The ellipsis to use in the Org-mode outline.
  190. When nil, just use the standard three dots. When a string, use that instead,
  191. and just in Org-mode (which will then use its own display table).
  192. Changing this requires executing `M-x org-mode' in a buffer to become
  193. effective."
  194. :group 'org-startup
  195. :type '(choice (const :tag "Default" nil)
  196. (string :tag "String" :value "...#")))
  197. (defvar org-display-table nil
  198. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  199. (defgroup org-keywords nil
  200. "Keywords in Org-mode."
  201. :tag "Org Keywords"
  202. :group 'org)
  203. (defcustom org-deadline-string "DEADLINE:"
  204. "String to mark deadline entries.
  205. A deadline is this string, followed by a time stamp. Should be a word,
  206. terminated by a colon. You can insert a schedule keyword and
  207. a timestamp with \\[org-deadline].
  208. Changes become only effective after restarting Emacs."
  209. :group 'org-keywords
  210. :type 'string)
  211. (defcustom org-scheduled-string "SCHEDULED:"
  212. "String to mark scheduled TODO entries.
  213. A schedule is this string, followed by a time stamp. Should be a word,
  214. terminated by a colon. You can insert a schedule keyword and
  215. a timestamp with \\[org-schedule].
  216. Changes become only effective after restarting Emacs."
  217. :group 'org-keywords
  218. :type 'string)
  219. (defcustom org-closed-string "CLOSED:"
  220. "String used as the prefix for timestamps logging closing a TODO entry."
  221. :group 'org-keywords
  222. :type 'string)
  223. (defcustom org-clock-string "CLOCK:"
  224. "String used as prefix for timestamps clocking work hours on an item."
  225. :group 'org-keywords
  226. :type 'string)
  227. (defcustom org-comment-string "COMMENT"
  228. "Entries starting with this keyword will never be exported.
  229. An entry can be toggled between COMMENT and normal with
  230. \\[org-toggle-comment].
  231. Changes become only effective after restarting Emacs."
  232. :group 'org-keywords
  233. :type 'string)
  234. (defcustom org-quote-string "QUOTE"
  235. "Entries starting with this keyword will be exported in fixed-width font.
  236. Quoting applies only to the text in the entry following the headline, and does
  237. not extend beyond the next headline, even if that is lower level.
  238. An entry can be toggled between QUOTE and normal with
  239. \\[org-toggle-fixed-width-section]."
  240. :group 'org-keywords
  241. :type 'string)
  242. (defconst org-repeat-re
  243. (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  244. " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  245. "Regular expression for specifying repeated events.
  246. After a match, group 1 contains the repeat expression.")
  247. (defgroup org-structure nil
  248. "Options concerning the general structure of Org-mode files."
  249. :tag "Org Structure"
  250. :group 'org)
  251. (defgroup org-reveal-location nil
  252. "Options about how to make context of a location visible."
  253. :tag "Org Reveal Location"
  254. :group 'org-structure)
  255. (defcustom org-show-hierarchy-above '((default . t))
  256. "Non-nil means, show full hierarchy when revealing a location.
  257. Org-mode often shows locations in an org-mode file which might have
  258. been invisible before. When this is set, the hierarchy of headings
  259. above the exposed location is shown.
  260. Turning this off for example for sparse trees makes them very compact.
  261. Instead of t, this can also be an alist specifying this option for different
  262. contexts. Valid contexts are
  263. agenda when exposing an entry from the agenda
  264. org-goto when using the command `org-goto' on key C-c C-j
  265. occur-tree when using the command `org-occur' on key C-c /
  266. tags-tree when constructing a sparse tree based on tags matches
  267. link-search when exposing search matches associated with a link
  268. mark-goto when exposing the jump goal of a mark
  269. bookmark-jump when exposing a bookmark location
  270. isearch when exiting from an incremental search
  271. default default for all contexts not set explicitly"
  272. :group 'org-reveal-location
  273. :type '(choice
  274. (const :tag "Always" t)
  275. (const :tag "Never" nil)
  276. (repeat :greedy t :tag "Individual contexts"
  277. (cons
  278. (choice :tag "Context"
  279. (const agenda)
  280. (const org-goto)
  281. (const occur-tree)
  282. (const tags-tree)
  283. (const link-search)
  284. (const mark-goto)
  285. (const bookmark-jump)
  286. (const isearch)
  287. (const default))
  288. (boolean)))))
  289. (defcustom org-show-following-heading '((default . nil))
  290. "Non-nil means, show following heading when revealing a location.
  291. Org-mode often shows locations in an org-mode file which might have
  292. been invisible before. When this is set, the heading following the
  293. match is shown.
  294. Turning this off for example for sparse trees makes them very compact,
  295. but makes it harder to edit the location of the match. In such a case,
  296. use the command \\[org-reveal] to show more context.
  297. Instead of t, this can also be an alist specifying this option for different
  298. contexts. See `org-show-hierarchy-above' for valid contexts."
  299. :group 'org-reveal-location
  300. :type '(choice
  301. (const :tag "Always" t)
  302. (const :tag "Never" nil)
  303. (repeat :greedy t :tag "Individual contexts"
  304. (cons
  305. (choice :tag "Context"
  306. (const agenda)
  307. (const org-goto)
  308. (const occur-tree)
  309. (const tags-tree)
  310. (const link-search)
  311. (const mark-goto)
  312. (const bookmark-jump)
  313. (const isearch)
  314. (const default))
  315. (boolean)))))
  316. (defcustom org-show-siblings '((default . nil) (isearch t))
  317. "Non-nil means, show all sibling heading when revealing a location.
  318. Org-mode often shows locations in an org-mode file which might have
  319. been invisible before. When this is set, the sibling of the current entry
  320. heading are all made visible. If `org-show-hierarchy-above' is t,
  321. the same happens on each level of the hierarchy above the current entry.
  322. By default this is on for the isearch context, off for all other contexts.
  323. Turning this off for example for sparse trees makes them very compact,
  324. but makes it harder to edit the location of the match. In such a case,
  325. use the command \\[org-reveal] to show more context.
  326. Instead of t, this can also be an alist specifying this option for different
  327. contexts. See `org-show-hierarchy-above' for valid contexts."
  328. :group 'org-reveal-location
  329. :type '(choice
  330. (const :tag "Always" t)
  331. (const :tag "Never" nil)
  332. (repeat :greedy t :tag "Individual contexts"
  333. (cons
  334. (choice :tag "Context"
  335. (const agenda)
  336. (const org-goto)
  337. (const occur-tree)
  338. (const tags-tree)
  339. (const link-search)
  340. (const mark-goto)
  341. (const bookmark-jump)
  342. (const isearch)
  343. (const default))
  344. (boolean)))))
  345. (defgroup org-cycle nil
  346. "Options concerning visibility cycling in Org-mode."
  347. :tag "Org Cycle"
  348. :group 'org-structure)
  349. (defcustom org-cycle-global-at-bob t
  350. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  351. This makes it possible to do global cycling without having to use S-TAB or
  352. C-u TAB. For this special case to work, the first line of the buffer
  353. must not be a headline - it may be empty ot some other text. When used in
  354. this way, `org-cycle-hook' is disables temporarily, to make sure the
  355. cursor stays at the beginning of the buffer.
  356. When this option is nil, don't do anything special at the beginning
  357. of the buffer."
  358. :group 'org-cycle
  359. :type 'boolean)
  360. (defcustom org-cycle-emulate-tab t
  361. "Where should `org-cycle' emulate TAB.
  362. nil Never
  363. white Only in completely white lines
  364. whitestart Only at the beginning of lines, before the first non-white char.
  365. t Everywhere except in headlines
  366. exc-hl-bol Everywhere except at the start of a headline
  367. If TAB is used in a place where it does not emulate TAB, the current subtree
  368. visibility is cycled."
  369. :group 'org-cycle
  370. :type '(choice (const :tag "Never" nil)
  371. (const :tag "Only in completely white lines" white)
  372. (const :tag "Before first char in a line" whitestart)
  373. (const :tag "Everywhere except in headlines" t)
  374. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  375. ))
  376. (defcustom org-cycle-separator-lines 2
  377. "Number of empty lines needed to keep an empty line between collapsed trees.
  378. If you leave an empty line between the end of a subtree and the following
  379. headline, this empty line is hidden when the subtree is folded.
  380. Org-mode will leave (exactly) one empty line visible if the number of
  381. empty lines is equal or larger to the number given in this variable.
  382. So the default 2 means, at least 2 empty lines after the end of a subtree
  383. are needed to produce free space between a collapsed subtree and the
  384. following headline.
  385. Special case: when 0, never leave empty lines in collapsed view."
  386. :group 'org-cycle
  387. :type 'integer)
  388. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  389. org-cycle-show-empty-lines
  390. org-optimize-window-after-visibility-change)
  391. "Hook that is run after `org-cycle' has changed the buffer visibility.
  392. The function(s) in this hook must accept a single argument which indicates
  393. the new state that was set by the most recent `org-cycle' command. The
  394. argument is a symbol. After a global state change, it can have the values
  395. `overview', `content', or `all'. After a local state change, it can have
  396. the values `folded', `children', or `subtree'."
  397. :group 'org-cycle
  398. :type 'hook)
  399. (defgroup org-edit-structure nil
  400. "Options concerning structure editing in Org-mode."
  401. :tag "Org Edit Structure"
  402. :group 'org-structure)
  403. (defcustom org-special-ctrl-a nil
  404. "Non-nil means `C-a' behaves specially in headlines.
  405. When set, `C-a' will bring back the cursor to the beginning of the
  406. headline text, i.e. after the stars and after a possible TODO keyword.
  407. When the cursor is already at that position, another `C-a' will bring
  408. it to the beginning of the line."
  409. :group 'org-edit-structure
  410. :type 'boolean)
  411. (defcustom org-odd-levels-only nil
  412. "Non-nil means, skip even levels and only use odd levels for the outline.
  413. This has the effect that two stars are being added/taken away in
  414. promotion/demotion commands. It also influences how levels are
  415. handled by the exporters.
  416. Changing it requires restart of `font-lock-mode' to become effective
  417. for fontification also in regions already fontified.
  418. You may also set this on a per-file basis by adding one of the following
  419. lines to the buffer:
  420. #+STARTUP: odd
  421. #+STARTUP: oddeven"
  422. :group 'org-edit-structure
  423. :group 'org-font-lock
  424. :type 'boolean)
  425. (defcustom org-adapt-indentation t
  426. "Non-nil means, adapt indentation when promoting and demoting.
  427. When this is set and the *entire* text in an entry is indented, the
  428. indentation is increased by one space in a demotion command, and
  429. decreased by one in a promotion command. If any line in the entry
  430. body starts at column 0, indentation is not changed at all."
  431. :group 'org-edit-structure
  432. :type 'boolean)
  433. (defcustom org-blank-before-new-entry '((heading . nil)
  434. (plain-list-item . nil))
  435. "Should `org-insert-heading' leave a blank line before new heading/item?
  436. The value is an alist, with `heading' and `plain-list-item' as car,
  437. and a boolean flag as cdr."
  438. :group 'org-edit-structure
  439. :type '(list
  440. (cons (const heading) (boolean))
  441. (cons (const plain-list-item) (boolean))))
  442. (defcustom org-insert-heading-hook nil
  443. "Hook being run after inserting a new heading."
  444. :group 'org-edit-structure
  445. :type 'boolean)
  446. (defcustom org-enable-fixed-width-editor t
  447. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  448. This currently only means, they are never auto-wrapped.
  449. When nil, such lines will be treated like ordinary lines.
  450. See also the QUOTE keyword."
  451. :group 'org-edit-structure
  452. :type 'boolean)
  453. (defgroup org-sparse-trees nil
  454. "Options concerning sparse trees in Org-mode."
  455. :tag "Org Sparse Trees"
  456. :group 'org-structure)
  457. (defcustom org-highlight-sparse-tree-matches t
  458. "Non-nil means, highlight all matches that define a sparse tree.
  459. The highlights will automatically disappear the next time the buffer is
  460. changed by an edit command."
  461. :group 'org-sparse-trees
  462. :type 'boolean)
  463. (defcustom org-remove-highlights-with-change t
  464. "Non-nil means, any change to the buffer will remove temporary highlights.
  465. Such highlights are created by `org-occur' and `org-clock-display'.
  466. When nil, `C-c C-c needs to be used to get rid of the highlights.
  467. The highlights created by `org-preview-latex-fragment' always need
  468. `C-c C-c' to be removed."
  469. :group 'org-sparse-trees
  470. :group 'org-time
  471. :type 'boolean)
  472. (defcustom org-occur-hook '(org-first-headline-recenter)
  473. "Hook that is run after `org-occur' has constructed a sparse tree.
  474. This can be used to recenter the window to show as much of the structure
  475. as possible."
  476. :group 'org-sparse-trees
  477. :type 'hook)
  478. (defgroup org-plain-lists nil
  479. "Options concerning plain lists in Org-mode."
  480. :tag "Org Plain lists"
  481. :group 'org-structure)
  482. (defcustom org-cycle-include-plain-lists nil
  483. "Non-nil means, include plain lists into visibility cycling.
  484. This means that during cycling, plain list items will *temporarily* be
  485. interpreted as outline headlines with a level given by 1000+i where i is the
  486. indentation of the bullet. In all other operations, plain list items are
  487. not seen as headlines. For example, you cannot assign a TODO keyword to
  488. such an item."
  489. :group 'org-plain-lists
  490. :type 'boolean)
  491. (defcustom org-plain-list-ordered-item-terminator t
  492. "The character that makes a line with leading number an ordered list item.
  493. Valid values are ?. and ?\). To get both terminators, use t. While
  494. ?. may look nicer, it creates the danger that a line with leading
  495. number may be incorrectly interpreted as an item. ?\) therefore is
  496. the safe choice."
  497. :group 'org-plain-lists
  498. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  499. (const :tag "paren like in \"2)\"" ?\))
  500. (const :tab "both" t)))
  501. (defcustom org-auto-renumber-ordered-lists t
  502. "Non-nil means, automatically renumber ordered plain lists.
  503. Renumbering happens when the sequence have been changed with
  504. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  505. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  506. :group 'org-plain-lists
  507. :type 'boolean)
  508. (defcustom org-provide-checkbox-statistics t
  509. "Non-nil means, update checkbox statistics after insert and toggle.
  510. When this is set, checkbox statistics is updated each time you either insert
  511. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  512. with \\[org-ctrl-c-ctrl-c\\]."
  513. :group 'org-plain-lists
  514. :type 'boolean)
  515. (defgroup org-archive nil
  516. "Options concerning archiving in Org-mode."
  517. :tag "Org Archive"
  518. :group 'org-structure)
  519. (defcustom org-archive-tag "ARCHIVE"
  520. "The tag that marks a subtree as archived.
  521. An archived subtree does not open during visibility cycling, and does
  522. not contribute to the agenda listings."
  523. :group 'org-archive
  524. :group 'org-keywords
  525. :type 'string)
  526. (defcustom org-agenda-skip-archived-trees t
  527. "Non-nil means, the agenda will skip any items located in archived trees.
  528. An archived tree is a tree marked with the tag ARCHIVE."
  529. :group 'org-archive
  530. :group 'org-agenda-skip
  531. :type 'boolean)
  532. (defcustom org-cycle-open-archived-trees nil
  533. "Non-nil means, `org-cycle' will open archived trees.
  534. An archived tree is a tree marked with the tag ARCHIVE.
  535. When nil, archived trees will stay folded. You can still open them with
  536. normal outline commands like `show-all', but not with the cycling commands."
  537. :group 'org-archive
  538. :group 'org-cycle
  539. :type 'boolean)
  540. (defcustom org-sparse-tree-open-archived-trees nil
  541. "Non-nil means sparse tree construction shows matches in archived trees.
  542. When nil, matches in these trees are highlighted, but the trees are kept in
  543. collapsed state."
  544. :group 'org-archive
  545. :group 'org-sparse-trees
  546. :type 'boolean)
  547. (defcustom org-archive-location "%s_archive::"
  548. "The location where subtrees should be archived.
  549. This string consists of two parts, separated by a double-colon.
  550. The first part is a file name - when omitted, archiving happens in the same
  551. file. %s will be replaced by the current file name (without directory part).
  552. Archiving to a different file is useful to keep archived entries from
  553. contributing to the Org-mode Agenda.
  554. The part after the double colon is a headline. The archived entries will be
  555. filed under that headline. When omitted, the subtrees are simply filed away
  556. at the end of the file, as top-level entries.
  557. Here are a few examples:
  558. \"%s_archive::\"
  559. If the current file is Projects.org, archive in file
  560. Projects.org_archive, as top-level trees. This is the default.
  561. \"::* Archived Tasks\"
  562. Archive in the current file, under the top-level headline
  563. \"* Archived Tasks\".
  564. \"~/org/archive.org::\"
  565. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  566. \"basement::** Finished Tasks\"
  567. Archive in file ./basement (relative path), as level 3 trees
  568. below the level 2 heading \"** Finished Tasks\".
  569. You may set this option on a per-file basis by adding to the buffer a
  570. line like
  571. #+ARCHIVE: basement::** Finished Tasks"
  572. :group 'org-archive
  573. :type 'string)
  574. (defcustom org-archive-mark-done t
  575. "Non-nil means, mark entries as DONE when they are moved to the archive file."
  576. :group 'org-archive
  577. :type 'boolean)
  578. (defcustom org-archive-stamp-time t
  579. "Non-nil means, add a time stamp to entries moved to an archive file.
  580. The time stamp will be added directly after the TODO state keyword in the
  581. first line, so it is probably best to use this in combinations with
  582. `org-archive-mark-done'."
  583. :group 'org-archive
  584. :type 'boolean)
  585. (defgroup org-table nil
  586. "Options concerning tables in Org-mode."
  587. :tag "Org Table"
  588. :group 'org)
  589. (defcustom org-enable-table-editor 'optimized
  590. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  591. When nil, such lines will be treated like ordinary lines.
  592. When equal to the symbol `optimized', the table editor will be optimized to
  593. do the following:
  594. - Automatic overwrite mode in front of whitespace in table fields.
  595. This makes the structure of the table stay in tact as long as the edited
  596. field does not exceed the column width.
  597. - Minimize the number of realigns. Normally, the table is aligned each time
  598. TAB or RET are pressed to move to another field. With optimization this
  599. happens only if changes to a field might have changed the column width.
  600. Optimization requires replacing the functions `self-insert-command',
  601. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  602. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  603. very good at guessing when a re-align will be necessary, but you can always
  604. force one with \\[org-ctrl-c-ctrl-c].
  605. If you would like to use the optimized version in Org-mode, but the
  606. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  607. This variable can be used to turn on and off the table editor during a session,
  608. but in order to toggle optimization, a restart is required.
  609. See also the variable `org-table-auto-blank-field'."
  610. :group 'org-table
  611. :type '(choice
  612. (const :tag "off" nil)
  613. (const :tag "on" t)
  614. (const :tag "on, optimized" optimized)))
  615. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  616. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  617. In the optimized version, the table editor takes over all simple keys that
  618. normally just insert a character. In tables, the characters are inserted
  619. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  620. for empty fields). Outside tables, the correct binding of the keys is
  621. restored.
  622. The default for this option is t if the optimized version is also used in
  623. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  624. this variable requires a restart of Emacs to become effective."
  625. :group 'org-table
  626. :type 'boolean)
  627. (defcustom orgtbl-radio-table-templates
  628. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  629. % END RECEIVE ORGTBL %n
  630. \\begin{comment}
  631. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  632. | | |
  633. \\end{comment}\n")
  634. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  635. @c END RECEIVE ORGTBL %n
  636. @ignore
  637. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  638. | | |
  639. @end ignore\n")
  640. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  641. <!-- END RECEIVE ORGTBL %n -->
  642. <!--
  643. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  644. | | |
  645. -->\n"))
  646. "Templates for radio tables in different major modes.
  647. All occurrences of %n in a template will be replaced with the name of the
  648. table, obtained by prompting the user."
  649. :group 'org-table
  650. :type '(repeat
  651. (list (symbol :tag "Major mode")
  652. (string :tag "Format"))))
  653. (defgroup org-table-settings nil
  654. "Settings for tables in Org-mode."
  655. :tag "Org Table Settings"
  656. :group 'org-table)
  657. (defcustom org-table-default-size "5x2"
  658. "The default size for newly created tables, Columns x Rows."
  659. :group 'org-table-settings
  660. :type 'string)
  661. (defcustom org-table-number-regexp
  662. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\)$"
  663. "Regular expression for recognizing numbers in table columns.
  664. If a table column contains mostly numbers, it will be aligned to the
  665. right. If not, it will be aligned to the left.
  666. The default value of this option is a regular expression which allows
  667. anything which looks remotely like a number as used in scientific
  668. context. For example, all of the following will be considered a
  669. number:
  670. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  671. Other options offered by the customize interface are more restrictive."
  672. :group 'org-table-settings
  673. :type '(choice
  674. (const :tag "Positive Integers"
  675. "^[0-9]+$")
  676. (const :tag "Integers"
  677. "^[-+]?[0-9]+$")
  678. (const :tag "Floating Point Numbers"
  679. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  680. (const :tag "Floating Point Number or Integer"
  681. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  682. (const :tag "Exponential, Floating point, Integer"
  683. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  684. (const :tag "Very General Number-Like, including hex"
  685. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\)$")
  686. (string :tag "Regexp:")))
  687. (defcustom org-table-number-fraction 0.5
  688. "Fraction of numbers in a column required to make the column align right.
  689. In a column all non-white fields are considered. If at least this
  690. fraction of fields is matched by `org-table-number-fraction',
  691. alignment to the right border applies."
  692. :group 'org-table-settings
  693. :type 'number)
  694. (defgroup org-table-editing nil
  695. "Bahavior of tables during editing in Org-mode."
  696. :tag "Org Table Editing"
  697. :group 'org-table)
  698. (defcustom org-table-automatic-realign t
  699. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  700. When nil, aligning is only done with \\[org-table-align], or after column
  701. removal/insertion."
  702. :group 'org-table-editing
  703. :type 'boolean)
  704. (defcustom org-table-auto-blank-field t
  705. "Non-nil means, automatically blank table field when starting to type into it.
  706. This only happens when typing immediately after a field motion
  707. command (TAB, S-TAB or RET).
  708. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  709. :group 'org-table-editing
  710. :type 'boolean)
  711. (defcustom org-table-tab-jumps-over-hlines t
  712. "Non-nil means, tab in the last column of a table with jump over a hline.
  713. If a horizontal separator line is following the current line,
  714. `org-table-next-field' can either create a new row before that line, or jump
  715. over the line. When this option is nil, a new line will be created before
  716. this line."
  717. :group 'org-table-editing
  718. :type 'boolean)
  719. (defcustom org-table-tab-recognizes-table.el t
  720. "Non-nil means, TAB will automatically notice a table.el table.
  721. When it sees such a table, it moves point into it and - if necessary -
  722. calls `table-recognize-table'."
  723. :group 'org-table-editing
  724. :type 'boolean)
  725. (defgroup org-table-calculation nil
  726. "Options concerning tables in Org-mode."
  727. :tag "Org Table Calculation"
  728. :group 'org-table)
  729. (defcustom org-table-use-standard-references t
  730. "Should org-mode work with table refrences like B3 instead of @3$2?
  731. Possible values are:
  732. nil never use them
  733. from accept as input, do not present for editing
  734. t: accept as input and present for editing"
  735. :group 'org-table-calculation
  736. :type '(choice
  737. (const :tag "Never, don't even check unser input for them" nil)
  738. (const :tag "Always, both as user input, and when editing" t)
  739. (const :tag "Convert user input, don't offer during editing" 'from)))
  740. (defcustom org-table-copy-increment t
  741. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  742. :group 'org-table-calculation
  743. :type 'boolean)
  744. (defcustom org-calc-default-modes
  745. '(calc-internal-prec 12
  746. calc-float-format (float 5)
  747. calc-angle-mode deg
  748. calc-prefer-frac nil
  749. calc-symbolic-mode nil
  750. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  751. calc-display-working-message t
  752. )
  753. "List with Calc mode settings for use in calc-eval for table formulas.
  754. The list must contain alternating symbols (Calc modes variables and values).
  755. Don't remove any of the default settings, just change the values. Org-mode
  756. relies on the variables to be present in the list."
  757. :group 'org-table-calculation
  758. :type 'plist)
  759. (defcustom org-table-formula-evaluate-inline t
  760. "Non-nil means, TAB and RET evaluate a formula in current table field.
  761. If the current field starts with an equal sign, it is assumed to be a formula
  762. which should be evaluated as described in the manual and in the documentation
  763. string of the command `org-table-eval-formula'. This feature requires the
  764. Emacs calc package.
  765. When this variable is nil, formula calculation is only available through
  766. the command \\[org-table-eval-formula]."
  767. :group 'org-table-calculation
  768. :type 'boolean)
  769. (defcustom org-table-formula-use-constants t
  770. "Non-nil means, interpret constants in formulas in tables.
  771. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  772. by the value given in `org-table-formula-constants', or by a value obtained
  773. from the `constants.el' package."
  774. :group 'org-table-calculation
  775. :type 'boolean)
  776. ;; FIXME this is also a variable that makes Org-mode files non-portable
  777. ;; Maybe I should have a #+ options for constants?
  778. (defcustom org-table-formula-constants nil
  779. "Alist with constant names and values, for use in table formulas.
  780. The car of each element is a name of a constant, without the `$' before it.
  781. The cdr is the value as a string. For example, if you'd like to use the
  782. speed of light in a formula, you would configure
  783. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  784. and then use it in an equation like `$1*$c'."
  785. :group 'org-table-calculation
  786. :type '(repeat
  787. (cons (string :tag "name")
  788. (string :tag "value"))))
  789. (defcustom org-table-allow-automatic-line-recalculation t
  790. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  791. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  792. :group 'org-table-calculation
  793. :type 'boolean)
  794. (defgroup org-link nil
  795. "Options concerning links in Org-mode."
  796. :tag "Org Link"
  797. :group 'org)
  798. (defvar org-link-abbrev-alist-local nil
  799. "Buffer-local version of `org-link-abbrev-alist', which see.
  800. The value of this is taken from the #+LINK lines.")
  801. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  802. (defcustom org-link-abbrev-alist nil
  803. "Alist of link abbreviations.
  804. The car of each element is a string, to be replaced at the start of a link.
  805. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  806. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  807. [[linkkey:tag][description]]
  808. If REPLACE is a string, the tag will simply be appended to create the link.
  809. If the string contains \"%s\", the tag will be inserted there. REPLACE may
  810. also be a function that will be called with the tag as the only argument to
  811. create the link. See the manual for examples."
  812. :group 'org-link
  813. :type 'alist)
  814. (defcustom org-descriptive-links t
  815. "Non-nil means, hide link part and only show description of bracket links.
  816. Bracket links are like [[link][descritpion]]. This variable sets the initial
  817. state in new org-mode buffers. The setting can then be toggled on a
  818. per-buffer basis from the Org->Hyperlinks menu."
  819. :group 'org-link
  820. :type 'boolean)
  821. (defcustom org-link-file-path-type 'adaptive
  822. "How the path name in file links should be stored.
  823. Valid values are:
  824. relative relative to the current directory, i.e. the directory of the file
  825. into which the link is being inserted.
  826. absolute absolute path, if possible with ~ for home directory.
  827. noabbrev absolute path, no abbreviation of home directory.
  828. adaptive Use relative path for files in the current directory and sub-
  829. directories of it. For other files, use an absolute path."
  830. :group 'org-link
  831. :type '(choice
  832. (const relative)
  833. (const absolute)
  834. (const noabbrev)
  835. (const adaptive)))
  836. (defcustom org-activate-links '(bracket angle plain radio tag date)
  837. "Types of links that should be activated in Org-mode files.
  838. This is a list of symbols, each leading to the activation of a certain link
  839. type. In principle, it does not hurt to turn on most link types - there may
  840. be a small gain when turning off unused link types. The types are:
  841. bracket The recommended [[link][description]] or [[link]] links with hiding.
  842. angular Links in angular brackes that may contain whitespace like
  843. <bbdb:Carsten Dominik>.
  844. plain Plain links in normal text, no whitespace, like http://google.com.
  845. radio Text that is matched by a radio target, see manual for details.
  846. tag Tag settings in a headline (link to tag search).
  847. date Time stamps (link to calendar).
  848. Changing this variable requires a restart of Emacs to become effective."
  849. :group 'org-link
  850. :type '(set (const :tag "Double bracket links (new style)" bracket)
  851. (const :tag "Angular bracket links (old style)" angular)
  852. (const :tag "plain text links" plain)
  853. (const :tag "Radio target matches" radio)
  854. (const :tag "Tags" tag)
  855. (const :tag "Tags" target)
  856. (const :tag "Timestamps" date)))
  857. (defgroup org-link-store nil
  858. "Options concerning storing links in Org-mode"
  859. :tag "Org Store Link"
  860. :group 'org-link)
  861. (defcustom org-email-link-description-format "Email %c: %.30s"
  862. "Format of the description part of a link to an email or usenet message.
  863. The following %-excapes will be replaced by corresponding information:
  864. %F full \"From\" field
  865. %f name, taken from \"From\" field, address if no name
  866. %T full \"To\" field
  867. %t first name in \"To\" field, address if no name
  868. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  869. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  870. %s subject
  871. %m message-id.
  872. You may use normal field width specification between the % and the letter.
  873. This is for example useful to limit the length of the subject.
  874. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  875. :group 'org-link-store
  876. :type 'string)
  877. (defcustom org-from-is-user-regexp
  878. (let (r1 r2)
  879. (when (and user-mail-address (not (string= user-mail-address "")))
  880. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  881. (when (and user-full-name (not (string= user-full-name "")))
  882. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  883. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  884. "Regexp mached against the \"From:\" header of an email or usenet message.
  885. It should match if the message is from the user him/herself."
  886. :group 'org-link-store
  887. :type 'regexp)
  888. (defcustom org-context-in-file-links t
  889. "Non-nil means, file links from `org-store-link' contain context.
  890. A search string will be added to the file name with :: as separator and
  891. used to find the context when the link is activated by the command
  892. `org-open-at-point'.
  893. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  894. negates this setting for the duration of the command."
  895. :group 'org-link-store
  896. :type 'boolean)
  897. (defcustom org-keep-stored-link-after-insertion nil
  898. "Non-nil means, keep link in list for entire session.
  899. The command `org-store-link' adds a link pointing to the current
  900. location to an internal list. These links accumulate during a session.
  901. The command `org-insert-link' can be used to insert links into any
  902. Org-mode file (offering completion for all stored links). When this
  903. option is nil, every link which has been inserted once using \\[org-insert-link]
  904. will be removed from the list, to make completing the unused links
  905. more efficient."
  906. :group 'org-link-store
  907. :type 'boolean)
  908. (defcustom org-usenet-links-prefer-google nil
  909. "Non-nil means, `org-store-link' will create web links to Google groups.
  910. When nil, Gnus will be used for such links.
  911. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  912. negates this setting for the duration of the command."
  913. :group 'org-link-store
  914. :type 'boolean)
  915. (defgroup org-link-follow nil
  916. "Options concerning following links in Org-mode"
  917. :tag "Org Follow Link"
  918. :group 'org-link)
  919. (defcustom org-tab-follows-link nil
  920. "Non-nil means, on links TAB will follow the link.
  921. Needs to be set before org.el is loaded."
  922. :group 'org-link-follow
  923. :type 'boolean)
  924. (defcustom org-return-follows-link nil
  925. "Non-nil means, on links RET will follow the link.
  926. Needs to be set before org.el is loaded."
  927. :group 'org-link-follow
  928. :type 'boolean)
  929. (defcustom org-mouse-1-follows-link t
  930. "Non-nil means, mouse-1 on a link will follow the link.
  931. A longer mouse click will still set point. Does not wortk on XEmacs.
  932. Needs to be set before org.el is loaded."
  933. :group 'org-link-follow
  934. :type 'boolean)
  935. (defcustom org-mark-ring-length 4
  936. "Number of different positions to be recorded in the ring
  937. Changing this requires a restart of Emacs to work correctly."
  938. :group 'org-link-follow
  939. :type 'interger)
  940. (defcustom org-link-frame-setup
  941. '((vm . vm-visit-folder-other-frame)
  942. (gnus . gnus-other-frame)
  943. (file . find-file-other-window))
  944. "Setup the frame configuration for following links.
  945. When following a link with Emacs, it may often be useful to display
  946. this link in another window or frame. This variable can be used to
  947. set this up for the different types of links.
  948. For VM, use any of
  949. `vm-visit-folder'
  950. `vm-visit-folder-other-frame'
  951. For Gnus, use any of
  952. `gnus'
  953. `gnus-other-frame'
  954. For FILE, use any of
  955. `find-file'
  956. `find-file-other-window'
  957. `find-file-other-frame'
  958. For the calendar, use the variable `calendar-setup'.
  959. For BBDB, it is currently only possible to display the matches in
  960. another window."
  961. :group 'org-link-follow
  962. :type '(list
  963. (cons (const vm)
  964. (choice
  965. (const vm-visit-folder)
  966. (const vm-visit-folder-other-window)
  967. (const vm-visit-folder-other-frame)))
  968. (cons (const gnus)
  969. (choice
  970. (const gnus)
  971. (const gnus-other-frame)))
  972. (cons (const file)
  973. (choice
  974. (const find-file)
  975. (const find-file-other-window)
  976. (const find-file-other-frame)))))
  977. (defcustom org-display-internal-link-with-indirect-buffer nil
  978. "Non-nil means, use indirect buffer to display infile links.
  979. Activating internal links (from one location in a file to another location
  980. in the same file) normally just jumps to the location. When the link is
  981. activated with a C-u prefix (or with mouse-3), the link is displayed in
  982. another window. When this option is set, the other window actually displays
  983. an indirect buffer clone of the current buffer, to avoid any visibility
  984. changes to the current buffer."
  985. :group 'org-link-follow
  986. :type 'boolean)
  987. (defcustom org-open-non-existing-files nil
  988. "Non-nil means, `org-open-file' will open non-existing files.
  989. When nil, an error will be generated."
  990. :group 'org-link-follow
  991. :type 'boolean)
  992. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  993. "Function and arguments to call for following mailto links.
  994. This is a list with the first element being a lisp function, and the
  995. remaining elements being arguments to the function. In string arguments,
  996. %a will be replaced by the address, and %s will be replaced by the subject
  997. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  998. :group 'org-link-follow
  999. :type '(choice
  1000. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1001. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1002. (const :tag "message-mail" (message-mail "%a" "%s"))
  1003. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1004. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1005. "Non-nil means, ask for confirmation before executing shell links.
  1006. Shell links can be dangerous, just thing about a link
  1007. [[shell:rm -rf ~/*][Google Search]]
  1008. This link would show up in your Org-mode document as \"Google Search\"
  1009. but really it would remove your entire home directory.
  1010. Therefore I *definitely* advise against setting this variable to nil.
  1011. Just change it to `y-or-n-p' of you want to confirm with a single key press
  1012. rather than having to type \"yes\"."
  1013. :group 'org-link-follow
  1014. :type '(choice
  1015. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1016. (const :tag "with y-or-n (faster)" y-or-n-p)
  1017. (const :tag "no confirmation (dangerous)" nil)))
  1018. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1019. "Non-nil means, ask for confirmation before executing elisp links.
  1020. Elisp links can be dangerous, just think about a link
  1021. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1022. This link would show up in your Org-mode document as \"Google Search\"
  1023. but really it would remove your entire home directory.
  1024. Therefore I *definitely* advise against setting this variable to nil.
  1025. Just change it to `y-or-n-p' of you want to confirm with a single key press
  1026. rather than having to type \"yes\"."
  1027. :group 'org-link-follow
  1028. :type '(choice
  1029. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1030. (const :tag "with y-or-n (faster)" y-or-n-p)
  1031. (const :tag "no confirmation (dangerous)" nil)))
  1032. (defconst org-file-apps-defaults-gnu
  1033. '((remote . emacs)
  1034. (t . mailcap))
  1035. "Default file applications on a UNIX or GNU/Linux system.
  1036. See `org-file-apps'.")
  1037. (defconst org-file-apps-defaults-macosx
  1038. '((remote . emacs)
  1039. (t . "open %s")
  1040. ("ps" . "gv %s")
  1041. ("ps.gz" . "gv %s")
  1042. ("eps" . "gv %s")
  1043. ("eps.gz" . "gv %s")
  1044. ("dvi" . "xdvi %s")
  1045. ("fig" . "xfig %s"))
  1046. "Default file applications on a MacOS X system.
  1047. The system \"open\" is known as a default, but we use X11 applications
  1048. for some files for which the OS does not have a good default.
  1049. See `org-file-apps'.")
  1050. (defconst org-file-apps-defaults-windowsnt
  1051. (list
  1052. '(remote . emacs)
  1053. (cons t
  1054. (list (if (featurep 'xemacs)
  1055. 'mswindows-shell-execute
  1056. 'w32-shell-execute)
  1057. "open" 'file)))
  1058. "Default file applications on a Windows NT system.
  1059. The system \"open\" is used for most files.
  1060. See `org-file-apps'.")
  1061. (defcustom org-file-apps
  1062. '(
  1063. ("txt" . emacs)
  1064. ("tex" . emacs)
  1065. ("ltx" . emacs)
  1066. ("org" . emacs)
  1067. ("el" . emacs)
  1068. ("bib" . emacs)
  1069. )
  1070. "External applications for opening `file:path' items in a document.
  1071. Org-mode uses system defaults for different file types, but
  1072. you can use this variable to set the application for a given file
  1073. extension. The entries in this list are cons cells where the car identifies
  1074. files and the cdr the corresponding command. Possible values for the
  1075. file identifier are
  1076. \"ext\" A string identifying an extension
  1077. `directory' Matches a directory
  1078. `remote' Matches a remote file, accessible through tramp or efs.
  1079. Remote files most likely should be visited through Emacs
  1080. because external applications cannot handle such paths.
  1081. t Default for all remaining files
  1082. Possible values for the command are:
  1083. `emacs' The file will be visited by the current Emacs process.
  1084. `default' Use the default application for this file type.
  1085. string A command to be executed by a shell; %s will be replaced
  1086. by the path to the file.
  1087. sexp A Lisp form which will be evaluated. The file path will
  1088. be available in the Lisp variable `file'.
  1089. For more examples, see the system specific constants
  1090. `org-file-apps-defaults-macosx'
  1091. `org-file-apps-defaults-windowsnt'
  1092. `org-file-apps-defaults-gnu'."
  1093. :group 'org-link-follow
  1094. :type '(repeat
  1095. (cons (choice :value ""
  1096. (string :tag "Extension")
  1097. (const :tag "Default for unrecognized files" t)
  1098. (const :tag "Remote file" remote)
  1099. (const :tag "Links to a directory" directory))
  1100. (choice :value ""
  1101. (const :tag "Visit with Emacs" emacs)
  1102. (const :tag "Use system default" default)
  1103. (string :tag "Command")
  1104. (sexp :tag "Lisp form")))))
  1105. (defcustom org-mhe-search-all-folders nil
  1106. "Non-nil means, that the search for the mh-message will be extended to
  1107. all folders if the message cannot be found in the folder given in the link.
  1108. Searching all folders is very efficient with one of the search engines
  1109. supported by MH-E, but will be slow with pick."
  1110. :group 'org-link-follow
  1111. :type 'boolean)
  1112. (defgroup org-remember nil
  1113. "Options concerning interaction with remember.el."
  1114. :tag "Org Remember"
  1115. :group 'org)
  1116. (defcustom org-directory "~/org"
  1117. "Directory with org files.
  1118. This directory will be used as default to prompt for org files.
  1119. Used by the hooks for remember.el."
  1120. :group 'org-remember
  1121. :type 'directory)
  1122. (defcustom org-default-notes-file "~/.notes"
  1123. "Default target for storing notes.
  1124. Used by the hooks for remember.el. This can be a string, or nil to mean
  1125. the value of `remember-data-file'.
  1126. You can set this on a per-template basis with the variable
  1127. `org-remember-templates'."
  1128. :group 'org-remember
  1129. :type '(choice
  1130. (const :tag "Default from remember-data-file" nil)
  1131. file))
  1132. (defcustom org-remember-default-headline ""
  1133. "The headline that should be the default location in the notes file.
  1134. When filing remember notes, the cursor will start at that position.
  1135. You can set this on a per-template basis with the variable
  1136. `org-remember-templates'."
  1137. :group 'org-remember
  1138. :type 'string)
  1139. (defcustom org-remember-templates nil
  1140. "Templates for the creation of remember buffers.
  1141. When nil, just let remember make the buffer.
  1142. When not nil, this is a list of 4-element lists. In each entry, the first
  1143. element is a character, a unique key to select this template.
  1144. The second element is the template. The third element is optional and can
  1145. specify a destination file for remember items created with this template.
  1146. The default file is given by `org-default-notes-file'. An optional third
  1147. element can specify the headline in that file that should be offered
  1148. first when the user is asked to file the entry. The default headline is
  1149. given in the variable `org-remember-default-headline'.
  1150. The template specifies the structure of the remember buffer. It should have
  1151. a first line starting with a star, to act as the org-mode headline.
  1152. Furthermore, the following %-escapes will be replaced with content:
  1153. %^{prompt} prompt the user for a string and replace this sequence with it.
  1154. %t time stamp, date only
  1155. %T time stamp with date and time
  1156. %u, %U like the above, but inactive time stamps
  1157. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1158. You may define a prompt like %^{Please specify birthday}t
  1159. %n user name (taken from `user-full-name')
  1160. %a annotation, normally the link created with org-store-link
  1161. %i initial content, the region when remember is called with C-u.
  1162. If %i is indented, the entire inserted text will be indented
  1163. as well.
  1164. %? After completing the template, position cursor here.
  1165. Apart from these general escapes, you can access information specific to the
  1166. link type that is created. For example, calling `remember' in emails or gnus
  1167. will record the author and the subject of the message, which you can access
  1168. with %:author and %:subject, respectively. Here is a complete list of what
  1169. is recorded for each link type.
  1170. Link type | Available information
  1171. -------------------+------------------------------------------------------
  1172. bbdb | %:type %:name %:company
  1173. vm, wl, mh, rmail | %:type %:subject %:message-id
  1174. | %:from %:fromname %:fromaddress
  1175. | %:to %:toname %:toaddress
  1176. | %:fromto (either \"to NAME\" or \"from NAME\")
  1177. gnus | %:group, for messages also all email fields
  1178. w3, w3m | %:type %:url
  1179. info | %:type %:file %:node
  1180. calendar | %:type %:date"
  1181. :group 'org-remember
  1182. :get (lambda (var) ; Make sure all entries have 4 elements
  1183. (mapcar (lambda (x)
  1184. (cond ((= (length x) 3) (append x '("")))
  1185. ((= (length x) 2) (append x '("" "")))
  1186. (t x)))
  1187. (default-value var)))
  1188. :type '(repeat
  1189. :tag "enabled"
  1190. (list :value (?a "\n" nil nil)
  1191. (character :tag "Selection Key")
  1192. (string :tag "Template")
  1193. (file :tag "Destination file (optional)")
  1194. (string :tag "Destination headline (optional)"))))
  1195. (defcustom org-reverse-note-order nil
  1196. "Non-nil means, store new notes at the beginning of a file or entry.
  1197. When nil, new notes will be filed to the end of a file or entry."
  1198. :group 'org-remember
  1199. :type '(choice
  1200. (const :tag "Reverse always" t)
  1201. (const :tag "Reverse never" nil)
  1202. (repeat :tag "By file name regexp"
  1203. (cons regexp boolean))))
  1204. (defgroup org-todo nil
  1205. "Options concerning TODO items in Org-mode."
  1206. :tag "Org TODO"
  1207. :group 'org)
  1208. (defgroup org-progress nil
  1209. "Options concerning Progress logging in Org-mode."
  1210. :tag "Org Progress"
  1211. :group 'org-time)
  1212. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1213. "List of TODO entry keyword sequences and their interpretation.
  1214. \\<org-mode-map>This is a list of sequences.
  1215. Each sequence starts with a symbol, either `sequence' or `type',
  1216. indicating if the keywords should be interpreted as a sequence of
  1217. action steps, or as different types of TODO items. The first
  1218. keywords are states requiring action - these states will select a headline
  1219. for inclusion into the global TODO list Org-mode produces. If one of
  1220. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1221. signify that no further action is necessary. If \"|\" is not found,
  1222. the last keyword is treated as the only DONE state of the sequence.
  1223. The command \\[org-todo] cycles an entry through these states, and one
  1224. additional state where no keyword is present. For details about this
  1225. cycling, see the manual.
  1226. TODO keywords and interpretation can also be set on a per-file basis with
  1227. the special #+SEQ_TODO and #+TYP_TODO lines.
  1228. For backward compatibility, this variable may also be just a list
  1229. of keywords - in this case the interptetation (sequence or type) will be
  1230. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1231. :group 'org-todo
  1232. :group 'org-keywords
  1233. :type '(choice
  1234. (repeat :tag "Old syntax, just keywords"
  1235. (string :tag "Keyword"))
  1236. (repeat :tag "New syntax"
  1237. (cons
  1238. (choice
  1239. :tag "Interpretation"
  1240. (const :tag "Sequence (cycling hits every state)" sequence)
  1241. (const :tag "Type (cycling directly to DONE)" type))
  1242. (repeat
  1243. (string :tag "Keyword"))))))
  1244. (defvar org-todo-keywords-1 nil)
  1245. (make-variable-buffer-local 'org-todo-keywords-1)
  1246. (defvar org-todo-keywords-for-agenda nil)
  1247. (defvar org-done-keywords-for-agenda nil)
  1248. (defvar org-not-done-keywords nil)
  1249. (make-variable-buffer-local 'org-not-done-keywords)
  1250. (defvar org-done-keywords nil)
  1251. (make-variable-buffer-local 'org-done-keywords)
  1252. (defvar org-todo-heads nil)
  1253. (make-variable-buffer-local 'org-todo-heads)
  1254. (defvar org-todo-sets nil)
  1255. (make-variable-buffer-local 'org-todo-sets)
  1256. (defvar org-todo-kwd-alist nil)
  1257. (make-variable-buffer-local 'org-todo-kwd-alist)
  1258. (defcustom org-todo-interpretation 'sequence
  1259. "Controls how TODO keywords are interpreted.
  1260. This variable is in principle obsolete and is only used for
  1261. backward compatibility, if the interpretation of todo keywords is
  1262. not given already in `org-todo-keywords'. See that variable for
  1263. more information."
  1264. :group 'org-todo
  1265. :group 'org-keywords
  1266. :type '(choice (const sequence)
  1267. (const type)))
  1268. (defcustom org-after-todo-state-change-hook nil
  1269. "Hook which is run after the state of a TODO item was changed.
  1270. The new state (a string with a TODO keyword, or nil) is available in the
  1271. Lisp variable `state'."
  1272. :group 'org-todo
  1273. :type 'hook)
  1274. (defcustom org-log-done nil
  1275. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1276. When the state of an entry is changed from nothing to TODO, remove a previous
  1277. closing date.
  1278. This can also be a list of symbols indicating under which conditions
  1279. the time stamp recording the action should be annotated with a short note.
  1280. Valid members of this list are
  1281. done Offer to record a note when marking entries done
  1282. state Offer to record a note whenever changing the TODO state
  1283. of an item. This is only relevant if TODO keywords are
  1284. interpreted as sequence, see variable `org-todo-interpretation'.
  1285. When `state' is set, this includes tracking `done'.
  1286. clock-out Offer to record a note when clocking out of an item.
  1287. A separate window will then pop up and allow you to type a note.
  1288. After finishing with C-c C-c, the note will be added directly after the
  1289. timestamp, as a plain list item. See also the variable
  1290. `org-log-note-headings'.
  1291. Logging can also be configured on a per-file basis by adding one of
  1292. the following lines anywhere in the buffer:
  1293. #+STARTUP: logdone
  1294. #+STARTUP: nologging
  1295. #+STARTUP: lognotedone
  1296. #+STARTUP: lognotestate
  1297. #+STARTUP: lognoteclock-out"
  1298. :group 'org-todo
  1299. :group 'org-progress
  1300. :type '(choice
  1301. (const :tag "off" nil)
  1302. (const :tag "on" t)
  1303. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1304. (const :tag "when item is marked DONE" done)
  1305. (const :tag "when TODO state changes" state)
  1306. (const :tag "when clocking out" clock-out))))
  1307. (defcustom org-log-done-with-time t
  1308. "Non-nil means, the CLOSED time stamp will contain date and time.
  1309. When nil, only the date will be recorded."
  1310. :group 'org-progress
  1311. :type 'boolean)
  1312. (defcustom org-log-note-headings
  1313. '((done . "CLOSING NOTE %t")
  1314. (state . "State %-12s %t")
  1315. (clock-out . ""))
  1316. "Headings for notes added when clocking out or closing TODO items.
  1317. The value is an alist, with the car being a sympol indicating the note
  1318. context, and the cdr is the heading to be used. The heading may also be the
  1319. empty string.
  1320. %t in the heading will be replaced by a time stamp.
  1321. %s will be replaced by the new TODO state, in double quotes.
  1322. %u will be replaced by the user name.
  1323. %U will be replaced by the full user name."
  1324. :group 'org-todo
  1325. :group 'org-progress
  1326. :type '(list :greedy t
  1327. (cons (const :tag "Heading when closing an item" done) string)
  1328. (cons (const :tag
  1329. "Heading when changing todo state (todo sequence only)"
  1330. state) string)
  1331. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1332. (defcustom org-log-repeat t
  1333. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1334. When nil, no note will be taken."
  1335. :group 'org-todo
  1336. :group 'org-progress
  1337. :type 'boolean)
  1338. (defgroup org-priorities nil
  1339. "Priorities in Org-mode."
  1340. :tag "Org Priorities"
  1341. :group 'org-todo)
  1342. (defcustom org-highest-priority ?A
  1343. "The highest priority of TODO items. A character like ?A, ?B etc.
  1344. Must have a smaller ASCII number than `org-lowest-priority'."
  1345. :group 'org-priorities
  1346. :type 'character)
  1347. (defcustom org-lowest-priority ?C
  1348. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1349. Must have a larger ASCII number than `org-highest-priority'."
  1350. :group 'org-priorities
  1351. :type 'character)
  1352. (defcustom org-default-priority ?B
  1353. "The default priority of TODO items.
  1354. This is the priority an item get if no explicit priority is given."
  1355. :group 'org-priorities
  1356. :type 'character)
  1357. (defgroup org-time nil
  1358. "Options concerning time stamps and deadlines in Org-mode."
  1359. :tag "Org Time"
  1360. :group 'org)
  1361. (defcustom org-insert-labeled-timestamps-at-point nil
  1362. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1363. When nil, these labeled time stamps are forces into the second line of an
  1364. entry, just after the headline. When scheduling from the global TODO list,
  1365. the time stamp will always be forced into the second line."
  1366. :group 'org-time
  1367. :type 'boolean)
  1368. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1369. "Formats for `format-time-string' which are used for time stamps.
  1370. It is not recommended to change this constant.")
  1371. (defcustom org-time-stamp-rounding-minutes 0
  1372. "Number of minutes to round time stamps to upon insertion.
  1373. When zero, insert the time unmodified. Useful rounding numbers
  1374. should be factors of 60, so for example 5, 10, 15.
  1375. When this is not zero, you can still force an exact time-stamp by using
  1376. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1377. :group 'org-time
  1378. :type 'integer)
  1379. (defcustom org-display-custom-times nil
  1380. "Non-nil means, overlay custom formats over all time stamps.
  1381. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1382. To turn this on on a per-file basis, insert anywhere in the file:
  1383. #+STARTUP: customtime"
  1384. :group 'org-time
  1385. :set 'set-default
  1386. :type 'sexp)
  1387. (make-variable-buffer-local 'org-display-custom-times)
  1388. (defcustom org-time-stamp-custom-formats
  1389. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1390. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1391. These are overlayed over the default ISO format if the variable
  1392. `org-display-custom-times' is set. Time like %H:%M should be at the
  1393. end of the second format."
  1394. :group 'org-time
  1395. :type 'sexp)
  1396. (defun org-time-stamp-format (&optional long inactive)
  1397. "Get the right format for a time string."
  1398. (let ((f (if long (cdr org-time-stamp-formats)
  1399. (car org-time-stamp-formats))))
  1400. (if inactive
  1401. (concat "[" (substring f 1 -1) "]")
  1402. f)))
  1403. (defcustom org-deadline-warning-days 30
  1404. "No. of days before expiration during which a deadline becomes active.
  1405. This variable governs the display in sparse trees and in the agenda."
  1406. :group 'org-time
  1407. :type 'number)
  1408. (defcustom org-popup-calendar-for-date-prompt t
  1409. "Non-nil means, pop up a calendar when prompting for a date.
  1410. In the calendar, the date can be selected with mouse-1. However, the
  1411. minibuffer will also be active, and you can simply enter the date as well.
  1412. When nil, only the minibuffer will be available."
  1413. :group 'org-time
  1414. :type 'boolean)
  1415. (defcustom org-calendar-follow-timestamp-change t
  1416. "Non-nil means, make the calendar window follow timestamp changes.
  1417. When a timestamp is modified and the calendar window is visible, it will be
  1418. moved to the new date."
  1419. :group 'org-time
  1420. :type 'boolean)
  1421. (defgroup org-tags nil
  1422. "Options concerning tags in Org-mode."
  1423. :tag "Org Tags"
  1424. :group 'org)
  1425. (defcustom org-tag-alist nil
  1426. "List of tags allowed in Org-mode files.
  1427. When this list is nil, Org-mode will base TAG input on what is already in the
  1428. buffer.
  1429. The value of this variable is an alist, the car may be (and should) be a
  1430. character that is used to select that tag through the fast-tag-selection
  1431. interface. See the manual for details."
  1432. :group 'org-tags
  1433. :type '(repeat
  1434. (choice
  1435. (cons (string :tag "Tag name")
  1436. (character :tag "Access char"))
  1437. (const :tag "Start radio group" (:startgroup))
  1438. (const :tag "End radio group" (:endgroup)))))
  1439. (defcustom org-use-fast-tag-selection 'auto
  1440. "Non-nil means, use fast tag selection scheme.
  1441. This is a special interface to select and deselect tags with single keys.
  1442. When nil, fast selection is never used.
  1443. When the symbol `auto', fast selection is used if and only if selection
  1444. characters for tags have been configured, either through the variable
  1445. `org-tag-alist' or through a #+TAGS line in the buffer.
  1446. When t, fast selection is always used and selection keys are assigned
  1447. automatically if necessary."
  1448. :group 'org-tags
  1449. :type '(choice
  1450. (const :tag "Always" t)
  1451. (const :tag "Never" nil)
  1452. (const :tag "When selection characters are configured" 'auto)))
  1453. (defcustom org-fast-tag-selection-single-key nil
  1454. "Non-nil means, fast tag selection exits after first change.
  1455. When nil, you have to press RET to exit it.
  1456. During fast tag selection, you can toggle this flag with `C-c'.
  1457. This variable can also have the value `expert'. In this case, the window
  1458. displaying the tags menu is not even shown, until you press C-c again."
  1459. :group 'org-tags
  1460. :type '(choice
  1461. (const :tag "No" nil)
  1462. (const :tag "Yes" t)
  1463. (const :tag "Expert" expert)))
  1464. (defcustom org-tags-column 48
  1465. "The column to which tags should be indented in a headline.
  1466. If this number is positive, it specifies the column. If it is negative,
  1467. it means that the tags should be flushright to that column. For example,
  1468. -79 works well for a normal 80 character screen."
  1469. :group 'org-tags
  1470. :type 'integer)
  1471. (defcustom org-auto-align-tags t
  1472. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1473. These operations change the length of a headline and therefore shift
  1474. the tags around. With this options turned on, after each such operation
  1475. the tags are again aligned to `org-tags-column'."
  1476. :group 'org-tags
  1477. :type 'boolean)
  1478. (defcustom org-use-tag-inheritance t
  1479. "Non-nil means, tags in levels apply also for sublevels.
  1480. When nil, only the tags directly given in a specific line apply there.
  1481. If you turn off this option, you very likely want to turn on the
  1482. companion option `org-tags-match-list-sublevels'."
  1483. :group 'org-tags
  1484. :type 'boolean)
  1485. (defcustom org-tags-match-list-sublevels nil
  1486. "Non-nil means list also sublevels of headlines matching tag search.
  1487. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1488. the sublevels of a headline matching a tag search often also match
  1489. the same search. Listing all of them can create very long lists.
  1490. Setting this variable to nil causes subtrees of a match to be skipped.
  1491. This option is off by default, because inheritance in on. If you turn
  1492. inheritance off, you very likely want to turn this option on.
  1493. As a special case, if the tag search is restricted to TODO items, the
  1494. value of this variable is ignored and sublevels are always checked, to
  1495. make sure all corresponding TODO items find their way into the list."
  1496. :group 'org-tags
  1497. :type 'boolean)
  1498. (defvar org-tags-history nil
  1499. "History of minibuffer reads for tags.")
  1500. (defvar org-last-tags-completion-table nil
  1501. "The last used completion table for tags.")
  1502. (defgroup org-agenda nil
  1503. "Options concerning agenda views in Org-mode."
  1504. :tag "Org Agenda"
  1505. :group 'org)
  1506. (defvar org-category nil
  1507. "Variable used by org files to set a category for agenda display.
  1508. Such files should use a file variable to set it, for example
  1509. # -*- mode: org; org-category: \"ELisp\"
  1510. or contain a special line
  1511. #+CATEGORY: ELisp
  1512. If the file does not specify a category, then file's base name
  1513. is used instead.")
  1514. (make-variable-buffer-local 'org-category)
  1515. (defcustom org-agenda-files nil
  1516. "The files to be used for agenda display.
  1517. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1518. \\[org-remove-file]. You can also use customize to edit the list.
  1519. If the value of the variable is not a list but a single file name, then
  1520. the list of agenda files is actually stored and maintained in that file, one
  1521. agenda file per line."
  1522. :group 'org-agenda
  1523. :type '(choice
  1524. (repeat :tag "List of files" file)
  1525. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1526. (defcustom org-agenda-confirm-kill 1
  1527. "When set, remote killing from the agenda buffer needs confirmation.
  1528. When t, a confirmation is always needed. When a number N, confirmation is
  1529. only needed when the text to be killed contains more than N non-white lines."
  1530. :group 'org-agenda
  1531. :type '(choice
  1532. (const :tag "Never" nil)
  1533. (const :tag "Always" t)
  1534. (number :tag "When more than N lines")))
  1535. (defcustom org-calendar-to-agenda-key [?c]
  1536. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1537. The command `org-calendar-goto-agenda' will be bound to this key. The
  1538. default is the character `c' because then `c' can be used to switch back and
  1539. forth between agenda and calendar."
  1540. :group 'org-agenda
  1541. :type 'sexp)
  1542. (defgroup org-agenda-export nil
  1543. "Options concerning exporting agenda views in Org-mode."
  1544. :tag "Org Agenda Export"
  1545. :group 'org-agenda)
  1546. (defcustom org-agenda-with-colors t
  1547. "Non-nil means, use colors in agenda views."
  1548. :group 'org-agenda-export
  1549. :type 'boolean)
  1550. (defcustom org-agenda-exporter-settings nil
  1551. "Alist of variable/value pairs that should be active during agenda export.
  1552. This is a good place to set uptions for ps-print and for htmlize."
  1553. :group 'org-agenda-export
  1554. :type '(repeat
  1555. (list
  1556. (variable)
  1557. (sexp :tag "Value"))))
  1558. (defcustom org-agenda-export-html-style ""
  1559. "The style specification for exported HTML Agenda files.
  1560. If this variable contains a string, it will replace the default <style>
  1561. section as produced by `htmlize'.
  1562. Since there are different ways of setting style information, this variable
  1563. needs to contain the full HTML structure to provide a style, including the
  1564. surrounding HTML tags. The style specifications should include definitions
  1565. the fonts used by the agenda, here is an example:
  1566. <style type=\"text/css\">
  1567. p { font-weight: normal; color: gray; }
  1568. .org-agenda-structure {
  1569. font-size: 110%;
  1570. color: #003399;
  1571. font-weight: 600;
  1572. }
  1573. .org-todo {
  1574. color: #cc6666;Week-agenda:
  1575. font-weight: bold;
  1576. }
  1577. .org-done {
  1578. color: #339933;
  1579. }
  1580. .title { text-align: center; }
  1581. .todo, .deadline { color: red; }
  1582. .done { color: green; }
  1583. </style>
  1584. or, if you want to keep the style in a file,
  1585. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1586. As the value of this option simply gets inserted into the HTML <head> header,
  1587. you can \"misuse\" it to also add other text to the header. However,
  1588. <style>...</style> is required, if not present the variable will be ignored."
  1589. :group 'org-agenda-export
  1590. :group 'org-export-html
  1591. :type 'string)
  1592. (defgroup org-agenda-custom-commands nil
  1593. "Options concerning agenda views in Org-mode."
  1594. :tag "Org Agenda Custom Commands"
  1595. :group 'org-agenda)
  1596. (defcustom org-agenda-custom-commands nil
  1597. "Custom commands for the agenda.
  1598. These commands will be offered on the splash screen displayed by the
  1599. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1600. (key type match options files)
  1601. key The key (a single char as a string) to be associated with the command.
  1602. type The command type, any of the following symbols:
  1603. todo Entries with a specific TODO keyword, in all agenda files.
  1604. tags Tags match in all agenda files.
  1605. tags-todo Tags match in all agenda files, TODO entries only.
  1606. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1607. tags-tree Sparse tree with all tags matches in *current* file.
  1608. occur-tree Occur sparse tree for *current* file.
  1609. match What to search for:
  1610. - a single keyword for TODO keyword searches
  1611. - a tags match expression for tags searches
  1612. - a regular expression for occur searches
  1613. options A list of option setttings, similar to that in a let form, so like
  1614. this: ((opt1 val1) (opt2 val2) ...)
  1615. files A list of files file to write the produced agenda buffer to
  1616. with the command `org-store-agenda-views'.
  1617. If a file name ends in \".html\", an HTML version of the buffer
  1618. is written out. If it ends in \".ps\", a postscript version is
  1619. produced. Otherwide, only the plain text is written to the file.
  1620. You can also define a set of commands, to create a composite agenda buffer.
  1621. In this case, an entry looks like this:
  1622. (key desc (cmd1 cmd2 ...) general-options file)
  1623. where
  1624. desc A description string to be displayed in the dispatcher menu.
  1625. cmd An agenda command, similar to the above. However, tree commands
  1626. are no allowed, but instead you can get agenda and global todo list.
  1627. So valid commands for a set are:
  1628. (agenda)
  1629. (alltodo)
  1630. (stuck)
  1631. (todo \"match\" options files)
  1632. (tags \"match\" options files)
  1633. (tags-todo \"match\" options files)
  1634. Each command can carry a list of options, and another set of options can be
  1635. given for the whole set of commands. Individual command options take
  1636. precedence over the general options."
  1637. :group 'org-agenda-custom-commands
  1638. :type '(repeat
  1639. (choice :value ("a" tags "" nil)
  1640. (list :tag "Single command"
  1641. (string :tag "Key")
  1642. (choice
  1643. (const :tag "Agenda" agenda)
  1644. (const :tag "TODO list" alltodo)
  1645. (const :tag "Stuck projects" stuck)
  1646. (const :tag "Tags search (all agenda files)" tags)
  1647. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1648. (const :tag "TODO keyword search (all agenda files)" todo)
  1649. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1650. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  1651. (const :tag "Occur tree (current buffer)" occur-tree)
  1652. (symbol :tag "Other, user-defined function"))
  1653. (string :tag "Match")
  1654. (repeat :tag "Local options"
  1655. (list (variable :tag "Option") (sexp :tag "Value")))
  1656. (option (repeat :tag "Export" (file :tag "Export to"))))
  1657. (list :tag "Command series, all agenda files"
  1658. (string :tag "Key")
  1659. (string :tag "Description")
  1660. (repeat
  1661. (choice
  1662. (const :tag "Agenda" (agenda))
  1663. (const :tag "TODO list" (alltodo))
  1664. (const :tag "Stuck projects" (stuck))
  1665. (list :tag "Tags search"
  1666. (const :format "" tags)
  1667. (string :tag "Match")
  1668. (repeat :tag "Local options"
  1669. (list (variable :tag "Option")
  1670. (sexp :tag "Value"))))
  1671. (list :tag "Tags search, TODO entries only"
  1672. (const :format "" tags-todo)
  1673. (string :tag "Match")
  1674. (repeat :tag "Local options"
  1675. (list (variable :tag "Option")
  1676. (sexp :tag "Value"))))
  1677. (list :tag "TODO keyword search"
  1678. (const :format "" todo)
  1679. (string :tag "Match")
  1680. (repeat :tag "Local options"
  1681. (list (variable :tag "Option")
  1682. (sexp :tag "Value"))))
  1683. (list :tag "Other, user-defined function"
  1684. (symbol :tag "function")
  1685. (string :tag "Match")
  1686. (repeat :tag "Local options"
  1687. (list (variable :tag "Option")
  1688. (sexp :tag "Value"))))))
  1689. (repeat :tag "General options"
  1690. (list (variable :tag "Option")
  1691. (sexp :tag "Value")))
  1692. (option (repeat :tag "Export" (file :tag "Export to")))))))
  1693. (defcustom org-stuck-projects
  1694. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  1695. "How to identify stuck projects.
  1696. This is a list of four items:
  1697. 1. A tags/todo matcher string that is used to identify a project.
  1698. The entire tree below a headline matched by this is considered one project.
  1699. 2. A list of TODO keywords identifying non-stuck projects.
  1700. If the project subtree contains any headline with one of these todo
  1701. keywords, the project is considered to be not stuck. If you specify
  1702. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  1703. 3. A list of tags identifying non-stuck projects.
  1704. If the project subtree contains any headline with one of these tags,
  1705. the project is considered to be not stuck. If you specify \"*\" as
  1706. a tag, any tag will mark the project unstuck.
  1707. 4. An arbitrary regular expression matching non-stuck projects.
  1708. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  1709. or `C-c a #' to produce the list."
  1710. :group 'org-agenda-custom-commands
  1711. :type '(list
  1712. (string :tag "Tags/TODO match to identify a project")
  1713. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  1714. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  1715. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  1716. (defgroup org-agenda-skip nil
  1717. "Options concerning skipping parts of agenda files."
  1718. :tag "Org Agenda Skip"
  1719. :group 'org-agenda)
  1720. (defcustom org-agenda-todo-list-sublevels t
  1721. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1722. When nil, the sublevels of a TODO entry are not checked, resulting in
  1723. potentially much shorter TODO lists."
  1724. :group 'org-agenda-skip
  1725. :group 'org-todo
  1726. :type 'boolean)
  1727. (defcustom org-agenda-todo-ignore-scheduled nil
  1728. "Non-nil means, don't show scheduled entries in the global todo list.
  1729. The idea behind this is that by scheduling it, you have already taken care
  1730. of this item."
  1731. :group 'org-agenda-skip
  1732. :group 'org-todo
  1733. :type 'boolean)
  1734. (defcustom org-agenda-todo-ignore-deadlines nil
  1735. "Non-nil means, don't show near deadline entries in the global todo list.
  1736. Near means closer than `org-deadline-warning-days' days.
  1737. The idea behind this is that such items will appear in the agenda anyway."
  1738. :group 'org-agenda-skip
  1739. :group 'org-todo
  1740. :type 'boolean)
  1741. (defcustom org-agenda-skip-scheduled-if-done nil
  1742. "Non-nil means don't show scheduled items in agenda when they are done.
  1743. This is relevant for the daily/weekly agenda, not for the TODO list."
  1744. :group 'org-agenda-skip
  1745. :type 'boolean)
  1746. (defcustom org-agenda-skip-deadline-if-done nil
  1747. "Non-nil means don't show deadines when the corresponding item is done.
  1748. When nil, the deadline is still shown and should give you a happy feeling.
  1749. This is relevant for the daily/weekly agenda."
  1750. :group 'org-agenda-skip
  1751. :type 'boolean)
  1752. (defcustom org-timeline-show-empty-dates 3
  1753. "Non-nil means, `org-timeline' also shows dates without an entry.
  1754. When nil, only the days which actually have entries are shown.
  1755. When t, all days between the first and the last date are shown.
  1756. When an integer, show also empty dates, but if there is a gap of more than
  1757. N days, just insert a special line indicating the size of the gap."
  1758. :group 'org-agenda-skip
  1759. :type '(choice
  1760. (const :tag "None" nil)
  1761. (const :tag "All" t)
  1762. (number :tag "at most")))
  1763. (defgroup org-agenda-startup nil
  1764. "Options concerning initial settings in the Agenda in Org Mode."
  1765. :tag "Org Agenda Startup"
  1766. :group 'org-agenda)
  1767. (defcustom org-finalize-agenda-hook nil
  1768. "Hook run just before displaying an agenda buffer."
  1769. :group 'org-agenda-startup
  1770. :type 'hook)
  1771. (defcustom org-agenda-mouse-1-follows-link nil
  1772. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1773. A longer mouse click will still set point. Does not wortk on XEmacs.
  1774. Needs to be set before org.el is loaded."
  1775. :group 'org-agenda-startup
  1776. :type 'boolean)
  1777. (defcustom org-agenda-start-with-follow-mode nil
  1778. "The initial value of follow-mode in a newly created agenda window."
  1779. :group 'org-agenda-startup
  1780. :type 'boolean)
  1781. (defgroup org-agenda-windows nil
  1782. "Options concerning the windows used by the Agenda in Org Mode."
  1783. :tag "Org Agenda Windows"
  1784. :group 'org-agenda)
  1785. (defcustom org-agenda-window-setup 'reorganize-frame
  1786. "How the agenda buffer should be displayed.
  1787. Possible values for this option are:
  1788. current-window Show agenda in the current window, keeping all other windows.
  1789. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  1790. other-window Use `switch-to-buffer-other-window' to display agenda.
  1791. reorganize-frame Show only two windows on the current frame, the current
  1792. window and the agenda.
  1793. See also the variable `org-agenda-restore-windows-after-quit'."
  1794. :group 'org-agenda-windows
  1795. :type '(choice
  1796. (const current-window)
  1797. (const other-frame)
  1798. (const other-window)
  1799. (const reorganize-frame)))
  1800. (defcustom org-agenda-restore-windows-after-quit nil
  1801. "Non-nil means, restore window configuration open exiting agenda.
  1802. Before the window configuration is changed for displaying the agenda,
  1803. the current status is recorded. When the agenda is exited with
  1804. `q' or `x' and this option is set, the old state is restored. If
  1805. `org-agenda-window-setup' is `other-frame', the value of this
  1806. option will be ignored.."
  1807. :group 'org-agenda-windows
  1808. :type 'boolean)
  1809. (defcustom org-indirect-buffer-display 'other-window
  1810. "How should indirect tree buffers be displayed?
  1811. This applies to indirect buffers created with the commands
  1812. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1813. Valid values are:
  1814. current-window Display in the current window
  1815. other-window Just display in another window.
  1816. dedicated-frame Create one new frame, and re-use it each time.
  1817. new-frame Make a new frame each time."
  1818. :group 'org-structure
  1819. :group 'org-agenda-windows
  1820. :type '(choice
  1821. (const :tag "In current window" current-window)
  1822. (const :tag "In current frame, other window" other-window)
  1823. (const :tag "Each time a new frame" new-frame)
  1824. (const :tag "One dedicated frame" dedicated-frame)))
  1825. (defgroup org-agenda-daily/weekly nil
  1826. "Options concerning the daily/weekly agenda."
  1827. :tag "Org Agenda Daily/Weekly"
  1828. :group 'org-agenda)
  1829. (defcustom org-agenda-ndays 7
  1830. "Number of days to include in overview display.
  1831. Should be 1 or 7."
  1832. :group 'org-agenda-daily/weekly
  1833. :type 'number)
  1834. (defcustom org-agenda-start-on-weekday 1
  1835. "Non-nil means, start the overview always on the specified weekday.
  1836. 0 denotes Sunday, 1 denotes Monday etc.
  1837. When nil, always start on the current day."
  1838. :group 'org-agenda-daily/weekly
  1839. :type '(choice (const :tag "Today" nil)
  1840. (number :tag "Weekday No.")))
  1841. (defcustom org-agenda-show-all-dates t
  1842. "Non-nil means, `org-agenda' shows every day in the selected range.
  1843. When nil, only the days which actually have entries are shown."
  1844. :group 'org-agenda-daily/weekly
  1845. :type 'boolean)
  1846. (defcustom org-agenda-date-format "%A %d %B %Y"
  1847. "Format string for displaying dates in the agenda.
  1848. Used by the daily/weekly agenda and by the timeline. This should be
  1849. a format string understood by `format-time-string'.
  1850. FIXME: Not used currently, because of timezone problem."
  1851. :group 'org-agenda-daily/weekly
  1852. :type 'string)
  1853. (defcustom org-agenda-include-diary nil
  1854. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1855. :group 'org-agenda-daily/weekly
  1856. :type 'boolean)
  1857. (defcustom org-agenda-include-all-todo nil
  1858. "Set means weekly/daily agenda will always contain all TODO entries.
  1859. The TODO entries will be listed at the top of the agenda, before
  1860. the entries for specific days."
  1861. :group 'org-agenda-daily/weekly
  1862. :type 'boolean)
  1863. (defgroup org-agenda-time-grid nil
  1864. "Options concerning the time grid in the Org-mode Agenda."
  1865. :tag "Org Agenda Time Grid"
  1866. :group 'org-agenda)
  1867. (defcustom org-agenda-use-time-grid t
  1868. "Non-nil means, show a time grid in the agenda schedule.
  1869. A time grid is a set of lines for specific times (like every two hours between
  1870. 8:00 and 20:00). The items scheduled for a day at specific times are
  1871. sorted in between these lines.
  1872. For details about when the grid will be shown, and what it will look like, see
  1873. the variable `org-agenda-time-grid'."
  1874. :group 'org-agenda-time-grid
  1875. :type 'boolean)
  1876. (defcustom org-agenda-time-grid
  1877. '((daily today require-timed)
  1878. "----------------"
  1879. (800 1000 1200 1400 1600 1800 2000))
  1880. "The settings for time grid for agenda display.
  1881. This is a list of three items. The first item is again a list. It contains
  1882. symbols specifying conditions when the grid should be displayed:
  1883. daily if the agenda shows a single day
  1884. weekly if the agenda shows an entire week
  1885. today show grid on current date, independent of daily/weekly display
  1886. require-timed show grid only if at least one item has a time specification
  1887. The second item is a string which will be places behing the grid time.
  1888. The third item is a list of integers, indicating the times that should have
  1889. a grid line."
  1890. :group 'org-agenda-time-grid
  1891. :type
  1892. '(list
  1893. (set :greedy t :tag "Grid Display Options"
  1894. (const :tag "Show grid in single day agenda display" daily)
  1895. (const :tag "Show grid in weekly agenda display" weekly)
  1896. (const :tag "Always show grid for today" today)
  1897. (const :tag "Show grid only if any timed entries are present"
  1898. require-timed)
  1899. (const :tag "Skip grid times already present in an entry"
  1900. remove-match))
  1901. (string :tag "Grid String")
  1902. (repeat :tag "Grid Times" (integer :tag "Time"))))
  1903. (defgroup org-agenda-sorting nil
  1904. "Options concerning sorting in the Org-mode Agenda."
  1905. :tag "Org Agenda Sorting"
  1906. :group 'org-agenda)
  1907. (let ((sorting-choice
  1908. '(choice
  1909. (const time-up) (const time-down)
  1910. (const category-keep) (const category-up) (const category-down)
  1911. (const tag-down) (const tag-up)
  1912. (const priority-up) (const priority-down))))
  1913. (defcustom org-agenda-sorting-strategy
  1914. '((agenda time-up category-keep priority-down)
  1915. (todo category-keep priority-down)
  1916. (tags category-keep priority-down))
  1917. "Sorting structure for the agenda items of a single day.
  1918. This is a list of symbols which will be used in sequence to determine
  1919. if an entry should be listed before another entry. The following
  1920. symbols are recognized:
  1921. time-up Put entries with time-of-day indications first, early first
  1922. time-down Put entries with time-of-day indications first, late first
  1923. category-keep Keep the default order of categories, corresponding to the
  1924. sequence in `org-agenda-files'.
  1925. category-up Sort alphabetically by category, A-Z.
  1926. category-down Sort alphabetically by category, Z-A.
  1927. tag-up Sort alphabetically by last tag, A-Z.
  1928. tag-down Sort alphabetically by last tag, Z-A.
  1929. priority-up Sort numerically by priority, high priority last.
  1930. priority-down Sort numerically by priority, high priority first.
  1931. The different possibilities will be tried in sequence, and testing stops
  1932. if one comparison returns a \"not-equal\". For example, the default
  1933. '(time-up category-keep priority-down)
  1934. means: Pull out all entries having a specified time of day and sort them,
  1935. in order to make a time schedule for the current day the first thing in the
  1936. agenda listing for the day. Of the entries without a time indication, keep
  1937. the grouped in categories, don't sort the categories, but keep them in
  1938. the sequence given in `org-agenda-files'. Within each category sort by
  1939. priority.
  1940. Leaving out `category-keep' would mean that items will be sorted across
  1941. categories by priority."
  1942. :group 'org-agenda-sorting
  1943. :type `(choice
  1944. (repeat :tag "General" ,sorting-choice)
  1945. (list :tag "Individually"
  1946. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  1947. (repeat ,sorting-choice))
  1948. (cons (const :tag "Strategy for TODO lists" todo)
  1949. (repeat ,sorting-choice))
  1950. (cons (const :tag "Strategy for Tags matches" tags)
  1951. (repeat ,sorting-choice))))))
  1952. (defcustom org-sort-agenda-notime-is-late t
  1953. "Non-nil means, items without time are considered late.
  1954. This is only relevant for sorting. When t, items which have no explicit
  1955. time like 15:30 will be considered as 99:01, i.e. later than any items which
  1956. do have a time. When nil, the default time is before 0:00. You can use this
  1957. option to decide if the schedule for today should come before or after timeless
  1958. agenda entries."
  1959. :group 'org-agenda-sorting
  1960. :type 'boolean)
  1961. (defgroup org-agenda-prefix nil
  1962. "Options concerning the entry prefix in the Org-mode agenda display."
  1963. :tag "Org Agenda Prefix"
  1964. :group 'org-agenda)
  1965. (defcustom org-agenda-prefix-format
  1966. '((agenda . " %-12:c%?-12t% s")
  1967. (timeline . " % s")
  1968. (todo . " %-12:c")
  1969. (tags . " %-12:c"))
  1970. "Format specifications for the prefix of items in the agenda views.
  1971. An alist with four entries, for the different agenda types. The keys to the
  1972. sublists are `agenda', `timeline', `todo', and `tags'. The values
  1973. are format strings.
  1974. This format works similar to a printf format, with the following meaning:
  1975. %c the category of the item, \"Diary\" for entries from the diary, or
  1976. as given by the CATEGORY keyword or derived from the file name.
  1977. %T the *last* tag of the item. Last because inherited tags come
  1978. first in the list.
  1979. %t the time-of-day specification if one applies to the entry, in the
  1980. format HH:MM
  1981. %s Scheduling/Deadline information, a short string
  1982. All specifiers work basically like the standard `%s' of printf, but may
  1983. contain two additional characters: A question mark just after the `%' and
  1984. a whitespace/punctuation character just before the final letter.
  1985. If the first character after `%' is a question mark, the entire field
  1986. will only be included if the corresponding value applies to the
  1987. current entry. This is useful for fields which should have fixed
  1988. width when present, but zero width when absent. For example,
  1989. \"%?-12t\" will result in a 12 character time field if a time of the
  1990. day is specified, but will completely disappear in entries which do
  1991. not contain a time.
  1992. If there is punctuation or whitespace character just before the final
  1993. format letter, this character will be appended to the field value if
  1994. the value is not empty. For example, the format \"%-12:c\" leads to
  1995. \"Diary: \" if the category is \"Diary\". If the category were be
  1996. empty, no additional colon would be interted.
  1997. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  1998. - Indent the line with two space characters
  1999. - Give the category in a 12 chars wide field, padded with whitespace on
  2000. the right (because of `-'). Append a colon if there is a category
  2001. (because of `:').
  2002. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2003. time, don't put in an empty field, just skip it (because of '?').
  2004. - Finally, put the scheduling information and append a whitespace.
  2005. As another example, if you don't want the time-of-day of entries in
  2006. the prefix, you could use:
  2007. (setq org-agenda-prefix-format \" %-11:c% s\")
  2008. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2009. `org-agenda-remove-tags'."
  2010. :type '(choice
  2011. (string :tag "General format")
  2012. (list :greedy t :tag "View dependent"
  2013. (cons (const agenda) (string :tag "Format"))
  2014. (cons (const timeline) (string :tag "Format"))
  2015. (cons (const todo) (string :tag "Format"))
  2016. (cons (const tags) (string :tag "Format"))))
  2017. :group 'org-agenda-prefix)
  2018. (defvar org-prefix-format-compiled nil
  2019. "The compiled version of the most recently used prefix format.
  2020. See the variable `org-agenda-prefix-format'.")
  2021. (defcustom org-agenda-remove-times-when-in-prefix t
  2022. "Non-nil means, remove duplicate time specifications in agenda items.
  2023. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2024. time-of-day specification in a headline or diary entry is extracted and
  2025. placed into the prefix. If this option is non-nil, the original specification
  2026. \(a timestamp or -range, or just a plain time(range) specification like
  2027. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2028. cluttered.
  2029. The option can be t or nil. It may also be the symbol `beg', indicating
  2030. that the time should only be removed what it is located at the beginning of
  2031. the headline/diary entry."
  2032. :group 'org-agenda-prefix
  2033. :type '(choice
  2034. (const :tag "Always" t)
  2035. (const :tag "Never" nil)
  2036. (const :tag "When at beginning of entry" beg)))
  2037. (defcustom org-agenda-remove-tags nil
  2038. "Non-nil means, remove the tags from the headline copy in the agenda.
  2039. When this is the symbol `prefix', only remove tags when
  2040. `org-agenda-prefix-format' contains a `%T' specifier."
  2041. :group 'org-agenda-prefix
  2042. :type '(choice
  2043. (const :tag "Always" t)
  2044. (const :tag "Never" nil)
  2045. (const :tag "When prefix format contains %T" prefix)))
  2046. (if (fboundp 'defvaralias)
  2047. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2048. 'org-agenda-remove-tags))
  2049. (defcustom org-agenda-align-tags-to-column 65
  2050. "Shift tags in agenda items to this column."
  2051. :group 'org-agenda-prefix
  2052. :type 'integer)
  2053. (defgroup org-latex nil
  2054. "Options for embedding LaTeX code into Org-mode"
  2055. :tag "Org LaTeX"
  2056. :group 'org)
  2057. (defcustom org-format-latex-options
  2058. '(:foreground default :background default :scale 1.0
  2059. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2060. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2061. "Options for creating images from LaTeX fragments.
  2062. This is a property list with the following properties:
  2063. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2064. `default' means use the forground of the default face.
  2065. :background the background color, or \"Transparent\".
  2066. `default' means use the background of the default face.
  2067. :scale a scaling factor for the size of the images
  2068. :html-foreground, :html-background, :html-scale
  2069. The same numbers for HTML export.
  2070. :matchers a list indicating which matchers should be used to
  2071. find LaTeX fragments. Valid members of this list are:
  2072. \"begin\" find environments
  2073. \"$\" find math expressions surrounded by $...$
  2074. \"$$\" find math expressions surrounded by $$....$$
  2075. \"\\(\" find math expressions surrounded by \\(...\\)
  2076. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2077. :group 'org-latex
  2078. :type 'plist)
  2079. (defcustom org-format-latex-header "\\documentclass{article}
  2080. \\usepackage{fullpage} % do not remove
  2081. \\usepackage{amssymb}
  2082. \\usepackage[usenames]{color}
  2083. \\usepackage{amsmath}
  2084. \\usepackage{latexsym}
  2085. \\usepackage[mathscr]{eucal}
  2086. \\pagestyle{empty} % do not remove"
  2087. "The document header used for processing LaTeX fragments."
  2088. :group 'org-latex
  2089. :type 'string)
  2090. (defgroup org-export nil
  2091. "Options for exporting org-listings."
  2092. :tag "Org Export"
  2093. :group 'org)
  2094. (defgroup org-export-general nil
  2095. "General options for exporting Org-mode files."
  2096. :tag "Org Export General"
  2097. :group 'org-export)
  2098. (defcustom org-export-publishing-directory "."
  2099. "Path to the location where exported files should be located.
  2100. This path may be relative to the directory where the Org-mode file lives.
  2101. The default is to put them into the same directory as the Org-mode file.
  2102. The variable may also be an alist with export types `:html', `:ascii',
  2103. `:ical', or `:xoxo' and the corresponding directories. If a direcoty path
  2104. is relative, it is interpreted relative to the directory where the exported
  2105. Org-mode files lives."
  2106. :group 'org-export-general
  2107. :type '(choice
  2108. (directory)
  2109. (repeat
  2110. (cons
  2111. (choice :tag "Type"
  2112. (const :html) (const :ascii) (const :ical) (const :xoxo))
  2113. (directory)))))
  2114. (defcustom org-export-language-setup
  2115. '(("en" "Author" "Date" "Table of Contents")
  2116. ("cs" "Autor" "Datum" "Obsah")
  2117. ("da" "Ophavsmand" "Dato" "Indhold")
  2118. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2119. ("es" "Autor" "Fecha" "\xccndice")
  2120. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  2121. ("it" "Autore" "Data" "Indice")
  2122. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2123. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2124. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2125. "Terms used in export text, translated to different languages.
  2126. Use the variable `org-export-default-language' to set the language,
  2127. or use the +OPTION lines for a per-file setting."
  2128. :group 'org-export-general
  2129. :type '(repeat
  2130. (list
  2131. (string :tag "HTML language tag")
  2132. (string :tag "Author")
  2133. (string :tag "Date")
  2134. (string :tag "Table of Contents"))))
  2135. (defcustom org-export-default-language "en"
  2136. "The default language of HTML export, as a string.
  2137. This should have an association in `org-export-language-setup'."
  2138. :group 'org-export-general
  2139. :type 'string)
  2140. (defcustom org-export-skip-text-before-1st-heading t
  2141. "Non-nil means, skip all text before the first headline when exporting.
  2142. When nil, that text is exported as well."
  2143. :group 'org-export-general
  2144. :type 'boolean)
  2145. (defcustom org-export-headline-levels 3
  2146. "The last level which is still exported as a headline.
  2147. Inferior levels will produce itemize lists when exported.
  2148. Note that a numeric prefix argument to an exporter function overrides
  2149. this setting.
  2150. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2151. :group 'org-export-general
  2152. :type 'number)
  2153. (defcustom org-export-with-section-numbers t
  2154. "Non-nil means, add section numbers to headlines when exporting.
  2155. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2156. :group 'org-export-general
  2157. :type 'boolean)
  2158. (defcustom org-export-with-toc t
  2159. "Non-nil means, create a table of contents in exported files.
  2160. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2161. When an integer, include levels up to N in the toc, this may then be
  2162. different from `org-export-headline-levels', but it will not be allowed
  2163. to be larger than the number of headline levels.
  2164. When nil, no table of contents is made.
  2165. Headlines which contain any TODO items will be marked with \"(*)\" in
  2166. ASCII export, and with red color in HTML output, if the option
  2167. `org-export-mark-todo-in-toc' is set.
  2168. In HTML output, the TOC will be clickable.
  2169. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2170. or \"toc:3\"."
  2171. :group 'org-export-general
  2172. :type '(choice
  2173. (const :tag "No Table of Contents" nil)
  2174. (const :tag "Full Table of Contents" t)
  2175. (integer :tag "TOC to level")))
  2176. (defcustom org-export-mark-todo-in-toc nil
  2177. "Non-nil means, mark TOC lines that contain any open TODO items."
  2178. :group 'org-export-general
  2179. :type 'boolean)
  2180. (defcustom org-export-preserve-breaks nil
  2181. "Non-nil means, preserve all line breaks when exporting.
  2182. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2183. export, line breaks will always be preserved, regardless of this variable.
  2184. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2185. :group 'org-export-general
  2186. :type 'boolean)
  2187. (defcustom org-export-with-archived-trees 'headline
  2188. "Whether subtrees with the ARCHIVE tag should be exported.
  2189. This can have three different values
  2190. nil Do not export, pretend this tree is not present
  2191. t Do export the entire tree
  2192. headline Only export the headline, but skip the tree below it."
  2193. :group 'org-export-general
  2194. :group 'org-archive
  2195. :type '(choice
  2196. (const :tag "not at all" nil)
  2197. (const :tag "headline only" 'headline)
  2198. (const :tag "entirely" t)))
  2199. (defcustom org-export-with-timestamps t
  2200. "If nil, do not export time stamps and associated keywords."
  2201. :group 'org-export-general
  2202. :type 'boolean)
  2203. (defcustom org-export-remove-timestamps-from-toc t
  2204. "If nil, remove timestamps from the table of contents entries."
  2205. :group 'org-export-general
  2206. :type 'boolean)
  2207. (defcustom org-export-with-tags 'not-in-toc
  2208. "If nil, do not export tags, just remove them from headlines.
  2209. If this is the symbol `not-in-toc', tags will be removed from table of
  2210. contents entries, but still be shown in the headlines of the document."
  2211. :group 'org-export-general
  2212. :type '(choice
  2213. (const :tag "Off" nil)
  2214. (const :tag "Not in TOC" not-in-toc)
  2215. (const :tag "On" t)))
  2216. (defgroup org-export-translation nil
  2217. "Options for translating special ascii sequences for the export backends."
  2218. :tag "Org Export Translation"
  2219. :group 'org-export)
  2220. (defcustom org-export-with-emphasize t
  2221. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2222. If the export target supports emphasizing text, the word will be
  2223. typeset in bold, italic, or underlined, respectively. Works only for
  2224. single words, but you can say: I *really* *mean* *this*.
  2225. Not all export backends support this.
  2226. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2227. :group 'org-export-translation
  2228. :type 'boolean)
  2229. (defcustom org-export-with-sub-superscripts t
  2230. "Non-nil means, interpret \"_\" and \"^\" for export.
  2231. When this option is turned on, you can use TeX-like syntax for sub- and
  2232. superscripts. Several characters after \"_\" or \"^\" will be
  2233. considered as a single item - so grouping with {} is normally not
  2234. needed. For example, the following things will be parsed as single
  2235. sub- or superscripts.
  2236. 10^24 or 10^tau several digits will be considered 1 item.
  2237. 10^-12 or 10^-tau a leading sign with digits or a word
  2238. x^2-y^3 will be read as x^2 - y^3, because items are
  2239. terminated by almost any nonword/nondigit char.
  2240. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2241. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2242. sub/superscript. If you set this variable to the symbol `{}',
  2243. the braces are *required* in order to trigger interpretations as
  2244. sub/superscript. This can be helpful in documents that need \"_\"
  2245. frequently in plain text.
  2246. Not all export backends support this, but HTML does.
  2247. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2248. :group 'org-export-translation
  2249. :type '(choice
  2250. (const :tag "Always interpret" t)
  2251. (const :tag "Only with braces" {})
  2252. (const :tag "Never interpret" nil)))
  2253. (defcustom org-export-with-TeX-macros t
  2254. "Non-nil means, interpret simple TeX-like macros when exporting.
  2255. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2256. No only real TeX macros will work here, but the standard HTML entities
  2257. for math can be used as macro names as well. For a list of supported
  2258. names in HTML export, see the constant `org-html-entities'.
  2259. Not all export backends support this.
  2260. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2261. :group 'org-export-translation
  2262. :group 'org-latex
  2263. :type 'boolean)
  2264. (defcustom org-export-with-LaTeX-fragments nil
  2265. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2266. When set, the exporter will find LaTeX environments if the \\begin line is
  2267. the first non-white thing on a line. It will also find the math delimiters
  2268. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2269. display math.
  2270. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2271. :group 'org-export-translation
  2272. :group 'org-latex
  2273. :type 'boolean)
  2274. (defcustom org-export-with-fixed-width t
  2275. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2276. This can be used to have pre-formatted text, fragments of code etc. For
  2277. example:
  2278. : ;; Some Lisp examples
  2279. : (while (defc cnt)
  2280. : (ding))
  2281. will be looking just like this in also HTML. See also the QUOTE keyword.
  2282. Not all export backends support this.
  2283. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2284. :group 'org-export-translation
  2285. :type 'boolean)
  2286. (defcustom org-match-sexp-depth 3
  2287. "Number of stacked braces for sub/superscript matching.
  2288. This has to be set before loading org.el to be effective."
  2289. :group 'org-export-translation
  2290. :type 'integer)
  2291. (defgroup org-export-tables nil
  2292. "Options for exporting tables in Org-mode."
  2293. :tag "Org Export Tables"
  2294. :group 'org-export)
  2295. (defcustom org-export-with-tables t
  2296. "If non-nil, lines starting with \"|\" define a table.
  2297. For example:
  2298. | Name | Address | Birthday |
  2299. |-------------+----------+-----------|
  2300. | Arthur Dent | England | 29.2.2100 |
  2301. Not all export backends support this.
  2302. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2303. :group 'org-export-tables
  2304. :type 'boolean)
  2305. (defcustom org-export-highlight-first-table-line t
  2306. "Non-nil means, highlight the first table line.
  2307. In HTML export, this means use <th> instead of <td>.
  2308. In tables created with table.el, this applies to the first table line.
  2309. In Org-mode tables, all lines before the first horizontal separator
  2310. line will be formatted with <th> tags."
  2311. :group 'org-export-tables
  2312. :type 'boolean)
  2313. (defcustom org-export-table-remove-special-lines t
  2314. "Remove special lines and marking characters in calculating tables.
  2315. This removes the special marking character column from tables that are set
  2316. up for spreadsheet calculations. It also removes the entire lines
  2317. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2318. the values of constants may be useful to have."
  2319. :group 'org-export-tables
  2320. :type 'boolean)
  2321. (defcustom org-export-prefer-native-exporter-for-tables nil
  2322. "Non-nil means, always export tables created with table.el natively.
  2323. Natively means, use the HTML code generator in table.el.
  2324. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2325. the table does not use row- or column-spanning). This has the
  2326. advantage, that the automatic HTML conversions for math symbols and
  2327. sub/superscripts can be applied. Org-mode's HTML generator is also
  2328. much faster."
  2329. :group 'org-export-tables
  2330. :type 'boolean)
  2331. (defgroup org-export-ascii nil
  2332. "Options specific for ASCII export of Org-mode files."
  2333. :tag "Org Export ASCII"
  2334. :group 'org-export)
  2335. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2336. "Characters for underlining headings in ASCII export.
  2337. In the given sequence, these characters will be used for level 1, 2, ..."
  2338. :group 'org-export-ascii
  2339. :type '(repeat character))
  2340. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2341. "Bullet characters for headlines converted to lists in ASCII export.
  2342. The first character is is used for the first lest level generated in this
  2343. way, and so on. If there are more levels than characters given here,
  2344. the list will be repeated.
  2345. Note that plain lists will keep the same bullets as the have in the
  2346. Org-mode file."
  2347. :group 'org-export-ascii
  2348. :type '(repeat character))
  2349. (defgroup org-export-xml nil
  2350. "Options specific for XML export of Org-mode files."
  2351. :tag "Org Export XML"
  2352. :group 'org-export)
  2353. (defgroup org-export-html nil
  2354. "Options specific for HTML export of Org-mode files."
  2355. :tag "Org Export HTML"
  2356. :group 'org-export)
  2357. (defcustom org-export-html-coding-system nil
  2358. ""
  2359. :group 'org-export-html
  2360. :type 'coding-system)
  2361. (defcustom org-export-html-style
  2362. "<style type=\"text/css\">
  2363. html {
  2364. font-family: Times, serif;
  2365. font-size: 12pt;
  2366. }
  2367. .title { text-align: center; }
  2368. .todo { color: red; }
  2369. .done { color: green; }
  2370. .timestamp { color: grey }
  2371. .timestamp-kwd { color: CadetBlue }
  2372. .tag { background-color:lightblue; font-weight:normal }
  2373. .target { background-color: lavender; }
  2374. pre {
  2375. border: 1pt solid #AEBDCC;
  2376. background-color: #F3F5F7;
  2377. padding: 5pt;
  2378. font-family: courier, monospace;
  2379. }
  2380. table { border-collapse: collapse; }
  2381. td, th {
  2382. vertical-align: top;
  2383. <!--border: 1pt solid #ADB9CC;-->
  2384. }
  2385. </style>"
  2386. "The default style specification for exported HTML files.
  2387. Since there are different ways of setting style information, this variable
  2388. needs to contain the full HTML structure to provide a style, including the
  2389. surrounding HTML tags. The style specifications should include definitions
  2390. for new classes todo, done, title, and deadline. For example, legal values
  2391. would be:
  2392. <style type=\"text/css\">
  2393. p { font-weight: normal; color: gray; }
  2394. h1 { color: black; }
  2395. .title { text-align: center; }
  2396. .todo, .deadline { color: red; }
  2397. .done { color: green; }
  2398. </style>
  2399. or, if you want to keep the style in a file,
  2400. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2401. As the value of this option simply gets inserted into the HTML <head> header,
  2402. you can \"misuse\" it to add arbitrary text to the header."
  2403. :group 'org-export-html
  2404. :type 'string)
  2405. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2406. "Format for typesetting the document title in HTML export."
  2407. :group 'org-export-html
  2408. :type 'string)
  2409. (defcustom org-export-html-toplevel-hlevel 2
  2410. "The <H> level for level 1 headings in HTML export."
  2411. :group 'org-export-html
  2412. :type 'string)
  2413. (defcustom org-export-html-link-org-files-as-html t
  2414. "Non-nil means, make file links to `file.org' point to `file.html'.
  2415. When org-mode is exporting an org-mode file to HTML, links to
  2416. non-html files are directly put into a href tag in HTML.
  2417. However, links to other Org-mode files (recognized by the
  2418. extension `.org.) should become links to the corresponding html
  2419. file, assuming that the linked org-mode file will also be
  2420. converted to HTML.
  2421. When nil, the links still point to the plain `.org' file."
  2422. :group 'org-export-html
  2423. :type 'boolean)
  2424. (defcustom org-export-html-inline-images 'maybe
  2425. "Non-nil means, inline images into exported HTML pages.
  2426. This is done using an <img> tag. When nil, an anchor with href is used to
  2427. link to the image. If this option is `maybe', then images in links with
  2428. an empty description will be inlined, while images with a description will
  2429. be linked only."
  2430. :group 'org-export-html
  2431. :type '(choice (const :tag "Never" nil)
  2432. (const :tag "Always" t)
  2433. (const :tag "When there is no description" maybe)))
  2434. ;; FIXME: rename
  2435. (defcustom org-export-html-expand t
  2436. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2437. When nil, these tags will be exported as plain text and therefore
  2438. not be interpreted by a browser.
  2439. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2440. :group 'org-export-html
  2441. :type 'boolean)
  2442. (defcustom org-export-html-table-tag
  2443. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  2444. "The HTML tag that is used to start a table.
  2445. This must be a <table> tag, but you may change the options like
  2446. borders and spacing."
  2447. :group 'org-export-html
  2448. :type 'string)
  2449. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  2450. "The opening tag for table header fields.
  2451. This is customizable so that alignment options can be specified."
  2452. :group 'org-export-tables
  2453. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2454. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  2455. "The opening tag for table data fields.
  2456. This is customizable so that alignment options can be specified."
  2457. :group 'org-export-tables
  2458. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2459. (defcustom org-export-html-with-timestamp nil
  2460. "If non-nil, write `org-export-html-html-helper-timestamp'
  2461. into the exported HTML text. Otherwise, the buffer will just be saved
  2462. to a file."
  2463. :group 'org-export-html
  2464. :type 'boolean)
  2465. (defcustom org-export-html-html-helper-timestamp
  2466. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2467. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2468. :group 'org-export-html
  2469. :type 'string)
  2470. (defgroup org-export-icalendar nil
  2471. "Options specific for iCalendar export of Org-mode files."
  2472. :tag "Org Export iCalendar"
  2473. :group 'org-export)
  2474. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2475. "The file name for the iCalendar file covering all agenda files.
  2476. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2477. The file name should be absolute."
  2478. :group 'org-export-icalendar
  2479. :type 'file)
  2480. (defcustom org-icalendar-include-todo nil
  2481. "Non-nil means, export to iCalendar files should also cover TODO items."
  2482. :group 'org-export-icalendar
  2483. :type '(choice
  2484. (const :tag "None" nil)
  2485. (const :tag "Unfinished" t)
  2486. (const :tag "All" all)))
  2487. (defcustom org-icalendar-include-sexps t
  2488. "Non-nil means, export to iCalendar files should also cover sexp entries.
  2489. These are entries like in the diary, but directly in an Org-mode file."
  2490. :group 'org-export-icalendar
  2491. :type 'boolean)
  2492. (defcustom org-icalendar-combined-name "OrgMode"
  2493. "Calendar name for the combined iCalendar representing all agenda files."
  2494. :group 'org-export-icalendar
  2495. :type 'string)
  2496. (defgroup org-font-lock nil
  2497. "Font-lock settings for highlighting in Org-mode."
  2498. :tag "Org Font Lock"
  2499. :group 'org)
  2500. (defcustom org-level-color-stars-only nil
  2501. "Non-nil means fontify only the stars in each headline.
  2502. When nil, the entire headline is fontified.
  2503. Changing it requires restart of `font-lock-mode' to become effective
  2504. also in regions already fontified."
  2505. :group 'org-font-lock
  2506. :type 'boolean)
  2507. (defcustom org-hide-leading-stars nil
  2508. "Non-nil means, hide the first N-1 stars in a headline.
  2509. This works by using the face `org-hide' for these stars. This
  2510. face is white for a light background, and black for a dark
  2511. background. You may have to customize the face `org-hide' to
  2512. make this work.
  2513. Changing it requires restart of `font-lock-mode' to become effective
  2514. also in regions already fontified.
  2515. You may also set this on a per-file basis by adding one of the following
  2516. lines to the buffer:
  2517. #+STARTUP: hidestars
  2518. #+STARTUP: showstars"
  2519. :group 'org-font-lock
  2520. :type 'boolean)
  2521. (defcustom org-fontify-done-headline nil
  2522. "Non-nil means, change the face of a headline if it is marked DONE.
  2523. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2524. When this is non-nil, the headline after the keyword is set to the
  2525. `org-headline-done' as an additional indication."
  2526. :group 'org-font-lock
  2527. :type 'boolean)
  2528. (defcustom org-fontify-emphasized-text t
  2529. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2530. Changing this variable requires a restart of Emacs to take effect."
  2531. :group 'org-font-lock
  2532. :type 'boolean)
  2533. (defvar org-emph-re nil
  2534. "Regular expression for matching emphasis.")
  2535. (defvar org-emphasis-regexp-components) ; defined just below
  2536. (defvar org-emphasis-alist) ; defined just below
  2537. (defun org-set-emph-re (var val)
  2538. "Set variable and compute the emphasis regular expression."
  2539. (set var val)
  2540. (when (and (boundp 'org-emphasis-alist)
  2541. (boundp 'org-emphasis-regexp-components)
  2542. org-emphasis-alist org-emphasis-regexp-components)
  2543. (let* ((e org-emphasis-regexp-components)
  2544. (pre (car e))
  2545. (post (nth 1 e))
  2546. (border (nth 2 e))
  2547. (body (nth 3 e))
  2548. (nl (nth 4 e))
  2549. (stacked (nth 5 e))
  2550. (body1 (concat body "*?"))
  2551. (markers (mapconcat 'car org-emphasis-alist "")))
  2552. ;; make sure special characters appear at the right position in the class
  2553. (if (string-match "\\^" markers)
  2554. (setq markers (concat (replace-match "" t t markers) "^")))
  2555. (if (string-match "-" markers)
  2556. (setq markers (concat (replace-match "" t t markers) "-")))
  2557. (if (> nl 0)
  2558. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2559. (int-to-string nl) "\\}")))
  2560. ;; Make the regexp
  2561. (setq org-emph-re
  2562. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  2563. "\\("
  2564. "\\([" markers "]\\)"
  2565. "\\("
  2566. "[^" border (if (and nil stacked) markers) "]"
  2567. body1
  2568. "[^" border (if (and nil stacked) markers) "]"
  2569. "\\)"
  2570. "\\3\\)"
  2571. "\\([" post (if stacked markers) "]\\|$\\)")))))
  2572. (defcustom org-emphasis-regexp-components
  2573. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1 nil)
  2574. "Components used to build the reqular expression for emphasis.
  2575. This is a list with 6 entries. Terminology: In an emphasis string
  2576. like \" *strong word* \", we call the initial space PREMATCH, the final
  2577. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2578. and \"trong wor\" is the body. The different components in this variable
  2579. specify what is allowed/forbidden in each part:
  2580. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2581. post Chars allowed as postmatch. End of line will be allowed too.
  2582. border The chars *forbidden* as border characters.
  2583. body-regexp A regexp like \".\" to match a body character. Don't use
  2584. non-shy groups here, and don't allow newline here.
  2585. newline The maximum number of newlines allowed in an emphasis exp.
  2586. stacked Non-nil means, allow stacked styles. This works only in HTML
  2587. export. When this is set, all marker characters (as given in
  2588. `org-emphasis-alist') will be allowed as pre/post, aiding
  2589. inside-out matching.
  2590. Use customize to modify this, or restart Emacs after changing it."
  2591. :group 'org-font-lock
  2592. :set 'org-set-emph-re
  2593. :type '(list
  2594. (sexp :tag "Allowed chars in pre ")
  2595. (sexp :tag "Allowed chars in post ")
  2596. (sexp :tag "Forbidden chars in border ")
  2597. (sexp :tag "Regexp for body ")
  2598. (integer :tag "number of newlines allowed")
  2599. (boolean :tag "Stacking allowed ")))
  2600. (defcustom org-emphasis-alist
  2601. '(("*" bold "<b>" "</b>")
  2602. ("/" italic "<i>" "</i>")
  2603. ("_" underline "<u>" "</u>")
  2604. ("=" shadow "<code>" "</code>")
  2605. ("+" (:strike-through t) "<del>" "</del>")
  2606. )
  2607. "Special syntax for emphasized text.
  2608. Text starting and ending with a special character will be emphasized, for
  2609. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2610. characters, the face to be used by font-lock for highlighting in Org-mode
  2611. Emacs buffers, and the HTML tags to be used for this.
  2612. Use customize to modify this, or restart Emacs after changing it."
  2613. :group 'org-font-lock
  2614. :set 'org-set-emph-re
  2615. :type '(repeat
  2616. (list
  2617. (string :tag "Marker character")
  2618. (choice
  2619. (face :tag "Font-lock-face")
  2620. (plist :tag "Face property list"))
  2621. (string :tag "HTML start tag")
  2622. (string :tag "HTML end tag"))))
  2623. ;;; The faces
  2624. (defgroup org-faces nil
  2625. "Faces in Org-mode."
  2626. :tag "Org Faces"
  2627. :group 'org-font-lock)
  2628. (defun org-compatible-face (specs)
  2629. "Make a compatible face specification.
  2630. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  2631. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  2632. to the top of the list. The `min-colors' attribute will be removed from
  2633. any other entries, and any resulting duplicates will be removed entirely."
  2634. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  2635. (let (r e a)
  2636. (while (setq e (pop specs))
  2637. (cond
  2638. ((memq (car e) '(t default)) (push e r))
  2639. ((setq a (member '(min-colors 8) (car e)))
  2640. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  2641. (cdr e)))))
  2642. ((setq a (assq 'min-colors (car e)))
  2643. (setq e (cons (delq a (car e)) (cdr e)))
  2644. (or (assoc (car e) r) (push e r)))
  2645. (t (or (assoc (car e) r) (push e r)))))
  2646. (nreverse r))
  2647. specs))
  2648. (defface org-hide
  2649. '((((background light)) (:foreground "white"))
  2650. (((background dark)) (:foreground "black")))
  2651. "Face used to hide leading stars in headlines.
  2652. The forground color of this face should be equal to the background
  2653. color of the frame."
  2654. :group 'org-faces)
  2655. (defface org-level-1 ;; font-lock-function-name-face
  2656. (org-compatible-face
  2657. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2658. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2659. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2660. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2661. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2662. (t (:bold t))))
  2663. "Face used for level 1 headlines."
  2664. :group 'org-faces)
  2665. (defface org-level-2 ;; font-lock-variable-name-face
  2666. (org-compatible-face
  2667. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2668. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2669. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2670. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2671. (t (:bold t))))
  2672. "Face used for level 2 headlines."
  2673. :group 'org-faces)
  2674. (defface org-level-3 ;; font-lock-keyword-face
  2675. (org-compatible-face
  2676. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2677. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2678. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2679. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2680. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2681. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2682. (t (:bold t))))
  2683. "Face used for level 3 headlines."
  2684. :group 'org-faces)
  2685. (defface org-level-4 ;; font-lock-comment-face
  2686. (org-compatible-face
  2687. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2688. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2689. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2690. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2691. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2692. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2693. (t (:bold t))))
  2694. "Face used for level 4 headlines."
  2695. :group 'org-faces)
  2696. (defface org-level-5 ;; font-lock-type-face
  2697. (org-compatible-face
  2698. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2699. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2700. (((class color) (min-colors 8)) (:foreground "green"))))
  2701. "Face used for level 5 headlines."
  2702. :group 'org-faces)
  2703. (defface org-level-6 ;; font-lock-constant-face
  2704. (org-compatible-face
  2705. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2706. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2707. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2708. "Face used for level 6 headlines."
  2709. :group 'org-faces)
  2710. (defface org-level-7 ;; font-lock-builtin-face
  2711. (org-compatible-face
  2712. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2713. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2714. (((class color) (min-colors 8)) (:foreground "blue"))))
  2715. "Face used for level 7 headlines."
  2716. :group 'org-faces)
  2717. (defface org-level-8 ;; font-lock-string-face
  2718. (org-compatible-face
  2719. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2720. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2721. (((class color) (min-colors 8)) (:foreground "green"))))
  2722. "Face used for level 8 headlines."
  2723. :group 'org-faces)
  2724. (defface org-special-keyword ;; font-lock-string-face
  2725. (org-compatible-face
  2726. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2727. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2728. (t (:italic t))))
  2729. "Face used for special keywords."
  2730. :group 'org-faces)
  2731. (defface org-warning ;; font-lock-warning-face
  2732. (org-compatible-face
  2733. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2734. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2735. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2736. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2737. (t (:bold t))))
  2738. "Face for deadlines and TODO keywords."
  2739. :group 'org-faces)
  2740. (defface org-archived ; similar to shadow
  2741. (org-compatible-face
  2742. '((((class color grayscale) (min-colors 88) (background light))
  2743. (:foreground "grey50"))
  2744. (((class color grayscale) (min-colors 88) (background dark))
  2745. (:foreground "grey70"))
  2746. (((class color) (min-colors 8) (background light))
  2747. (:foreground "green"))
  2748. (((class color) (min-colors 8) (background dark))
  2749. (:foreground "yellow"))))
  2750. "Face for headline with the ARCHIVE tag."
  2751. :group 'org-faces)
  2752. (defface org-link
  2753. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2754. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2755. (t (:underline t)))
  2756. "Face for links."
  2757. :group 'org-faces)
  2758. (defface org-target
  2759. '((((class color) (background light)) (:underline t))
  2760. (((class color) (background dark)) (:underline t))
  2761. (t (:underline t)))
  2762. "Face for links."
  2763. :group 'org-faces)
  2764. (defface org-date
  2765. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2766. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2767. (t (:underline t)))
  2768. "Face for links."
  2769. :group 'org-faces)
  2770. (defface org-sexp-date
  2771. '((((class color) (background light)) (:foreground "Purple"))
  2772. (((class color) (background dark)) (:foreground "Cyan"))
  2773. (t (:underline t)))
  2774. "Face for links."
  2775. :group 'org-faces)
  2776. (defface org-tag
  2777. '((t (:bold t)))
  2778. "Face for tags."
  2779. :group 'org-faces)
  2780. (defface org-todo ;; font-lock-warning-face
  2781. (org-compatible-face
  2782. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2783. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2784. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2785. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2786. (t (:inverse-video t :bold t))))
  2787. "Face for TODO keywords."
  2788. :group 'org-faces)
  2789. (defface org-done ;; font-lock-type-face
  2790. (org-compatible-face
  2791. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2792. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2793. (((class color) (min-colors 8)) (:foreground "green"))
  2794. (t (:bold t))))
  2795. "Face used for todo keywords that indicate DONE items."
  2796. :group 'org-faces)
  2797. (defface org-headline-done ;; font-lock-string-face
  2798. (org-compatible-face
  2799. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2800. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2801. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2802. "Face used to indicate that a headline is DONE.
  2803. This face is only used if `org-fontify-done-headline' is set. If applies
  2804. to the part of the headline after the DONE keyword."
  2805. :group 'org-faces)
  2806. (defface org-table ;; font-lock-function-name-face
  2807. (org-compatible-face
  2808. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2809. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2810. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2811. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2812. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2813. (((class color) (min-colors 8) (background dark)))))
  2814. "Face used for tables."
  2815. :group 'org-faces)
  2816. (defface org-formula
  2817. (org-compatible-face
  2818. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2819. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2820. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2821. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2822. (t (:bold t :italic t))))
  2823. "Face for formulas."
  2824. :group 'org-faces)
  2825. (defface org-agenda-structure ;; font-lock-function-name-face
  2826. (org-compatible-face
  2827. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2828. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2829. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2830. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2831. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2832. (t (:bold t))))
  2833. "Face used in agenda for captions and dates."
  2834. :group 'org-faces)
  2835. (defface org-scheduled-today
  2836. (org-compatible-face
  2837. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2838. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2839. (((class color) (min-colors 8)) (:foreground "green"))
  2840. (t (:bold t :italic t))))
  2841. "Face for items scheduled for a certain day."
  2842. :group 'org-faces)
  2843. (defface org-scheduled-previously
  2844. (org-compatible-face
  2845. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2846. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2847. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2848. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2849. (t (:bold t))))
  2850. "Face for items scheduled previously, and not yet done."
  2851. :group 'org-faces)
  2852. (defface org-upcoming-deadline
  2853. (org-compatible-face
  2854. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2855. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2856. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2857. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2858. (t (:bold t))))
  2859. "Face for items scheduled previously, and not yet done."
  2860. :group 'org-faces)
  2861. (defface org-time-grid ;; font-lock-variable-name-face
  2862. (org-compatible-face
  2863. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2864. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2865. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2866. "Face used for time grids."
  2867. :group 'org-faces)
  2868. (defconst org-level-faces
  2869. '(org-level-1 org-level-2 org-level-3 org-level-4
  2870. org-level-5 org-level-6 org-level-7 org-level-8
  2871. ))
  2872. (defconst org-n-levels (length org-level-faces))
  2873. ;;; Variables for pre-computed regular expressions, all buffer local
  2874. (defvar org-todo-regexp nil
  2875. "Matches any of the TODO state keywords.")
  2876. (make-variable-buffer-local 'org-todo-regexp)
  2877. (defvar org-not-done-regexp nil
  2878. "Matches any of the TODO state keywords except the last one.")
  2879. (make-variable-buffer-local 'org-not-done-regexp)
  2880. (defvar org-todo-line-regexp nil
  2881. "Matches a headline and puts TODO state into group 2 if present.")
  2882. (make-variable-buffer-local 'org-todo-line-regexp)
  2883. (defvar org-todo-line-tags-regexp nil
  2884. "Matches a headline and puts TODO state into group 2 if present.
  2885. Also put tags into group 4 if tags are present.")
  2886. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2887. (defvar org-nl-done-regexp nil
  2888. "Matches newline followed by a headline with the DONE keyword.")
  2889. (make-variable-buffer-local 'org-nl-done-regexp)
  2890. (defvar org-looking-at-done-regexp nil
  2891. "Matches the DONE keyword a point.")
  2892. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2893. (defvar org-ds-keyword-length 12
  2894. "Maximum length of the Deadline and SCHEDULED keywords.")
  2895. (make-variable-buffer-local 'org-ds-keyword-length)
  2896. (defvar org-deadline-regexp nil
  2897. "Matches the DEADLINE keyword.")
  2898. (make-variable-buffer-local 'org-deadline-regexp)
  2899. (defvar org-deadline-time-regexp nil
  2900. "Matches the DEADLINE keyword together with a time stamp.")
  2901. (make-variable-buffer-local 'org-deadline-time-regexp)
  2902. (defvar org-deadline-line-regexp nil
  2903. "Matches the DEADLINE keyword and the rest of the line.")
  2904. (make-variable-buffer-local 'org-deadline-line-regexp)
  2905. (defvar org-scheduled-regexp nil
  2906. "Matches the SCHEDULED keyword.")
  2907. (make-variable-buffer-local 'org-scheduled-regexp)
  2908. (defvar org-scheduled-time-regexp nil
  2909. "Matches the SCHEDULED keyword together with a time stamp.")
  2910. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2911. (defvar org-closed-time-regexp nil
  2912. "Matches the CLOSED keyword together with a time stamp.")
  2913. (make-variable-buffer-local 'org-closed-time-regexp)
  2914. (defvar org-keyword-time-regexp nil
  2915. "Matches any of the 4 keywords, together with the time stamp.")
  2916. (make-variable-buffer-local 'org-keyword-time-regexp)
  2917. (defvar org-keyword-time-not-clock-regexp nil
  2918. "Matches any of the 3 keywords, together with the time stamp.")
  2919. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2920. (defvar org-maybe-keyword-time-regexp nil
  2921. "Matches a timestamp, possibly preceeded by a keyword.")
  2922. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2923. (defvar org-planning-or-clock-line-re nil
  2924. "Matches a line with planning or clock info.")
  2925. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2926. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  2927. rear-nonsticky t mouse-map t fontified t)
  2928. "Properties to remove when a string without properties is wanted.")
  2929. (defsubst org-match-string-no-properties (num &optional string)
  2930. (if (featurep 'xemacs)
  2931. (let ((s (match-string num string)))
  2932. (remove-text-properties 0 (length s) org-rm-props s)
  2933. s)
  2934. (match-string-no-properties num string)))
  2935. (defsubst org-no-properties (s)
  2936. (remove-text-properties 0 (length s) org-rm-props s)
  2937. s)
  2938. (defsubst org-get-alist-option (option key)
  2939. (cond ((eq key t) t)
  2940. ((eq option t) t)
  2941. ((assoc key option) (cdr (assoc key option)))
  2942. (t (cdr (assq 'default option)))))
  2943. (defsubst org-inhibit-invisibility ()
  2944. "Modified `buffer-invisibility-spec' for Emacs 21.
  2945. Some ops with invisible text do not work correctly on Emacs 21. For these
  2946. we turn off invisibility temporarily. Use this in a `let' form."
  2947. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  2948. (defsubst org-set-local (var value)
  2949. "Make VAR local in current buffer and set it to VALUE."
  2950. (set (make-variable-buffer-local var) value))
  2951. (defsubst org-mode-p ()
  2952. "Check if the current buffer is in Org-mode."
  2953. (eq major-mode 'org-mode))
  2954. (defsubst org-last (list)
  2955. "Return the last element of LIST."
  2956. (car (last list)))
  2957. (defun org-let (list &rest body)
  2958. (eval (cons 'let (cons list body))))
  2959. (put 'org-let 'lisp-indent-function 1)
  2960. (defun org-let2 (list1 list2 &rest body)
  2961. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  2962. (put 'org-let2 'lisp-indent-function 2)
  2963. (defconst org-startup-options
  2964. '(("fold" org-startup-folded t)
  2965. ("overview" org-startup-folded t)
  2966. ("nofold" org-startup-folded nil)
  2967. ("showall" org-startup-folded nil)
  2968. ("content" org-startup-folded content)
  2969. ("hidestars" org-hide-leading-stars t)
  2970. ("showstars" org-hide-leading-stars nil)
  2971. ("odd" org-odd-levels-only t)
  2972. ("oddeven" org-odd-levels-only nil)
  2973. ("align" org-startup-align-all-tables t)
  2974. ("noalign" org-startup-align-all-tables nil)
  2975. ("customtime" org-display-custom-times t)
  2976. ("logging" org-log-done t)
  2977. ("logdone" org-log-done t)
  2978. ("nologging" org-log-done nil)
  2979. ("lognotedone" org-log-done done push)
  2980. ("lognotestate" org-log-done state push)
  2981. ("lognoteclock-out" org-log-done clock-out push)
  2982. ("logrepeat" org-log-repeat t)
  2983. ("nologrepeat" org-log-repeat nil)
  2984. ("constcgs" constants-unit-system cgs)
  2985. ("constSI" constants-unit-system SI))
  2986. "Variable associated with STARTUP options for org-mode.
  2987. Each element is a list of three items: The startup options as written
  2988. in the #+STARTUP line, the corresponding variable, and the value to
  2989. set this variable to if the option is found. An optional forth element PUSH
  2990. means to push this value onto the list in the variable.")
  2991. (defun org-set-regexps-and-options ()
  2992. "Precompute regular expressions for current buffer."
  2993. (when (org-mode-p)
  2994. (org-set-local 'org-todo-kwd-alist nil)
  2995. (org-set-local 'org-todo-keywords-1 nil)
  2996. (org-set-local 'org-done-keywords nil)
  2997. (org-set-local 'org-todo-heads nil)
  2998. (org-set-local 'org-todo-sets nil)
  2999. (let ((re (org-make-options-regexp
  3000. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  3001. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES")))
  3002. (splitre "[ \t]+")
  3003. kwds key value cat arch tags links hw dws tail sep kws1 prio)
  3004. (save-excursion
  3005. (save-restriction
  3006. (widen)
  3007. (goto-char (point-min))
  3008. (while (re-search-forward re nil t)
  3009. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3010. (cond
  3011. ((equal key "CATEGORY")
  3012. (if (string-match "[ \t]+$" value)
  3013. (setq value (replace-match "" t t value)))
  3014. (setq cat (intern value)))
  3015. ((equal key "SEQ_TODO")
  3016. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3017. ((equal key "TYP_TODO")
  3018. (push (cons 'type (org-split-string value splitre)) kwds))
  3019. ((equal key "TAGS")
  3020. (setq tags (append tags (org-split-string value splitre))))
  3021. ((equal key "LINK")
  3022. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3023. (push (cons (match-string 1 value)
  3024. (org-trim (match-string 2 value)))
  3025. links)))
  3026. ((equal key "PRIORITIES")
  3027. (setq prio (org-split-string value " +")))
  3028. ((equal key "STARTUP")
  3029. (let ((opts (org-split-string value splitre))
  3030. l var val)
  3031. (while (setq l (pop opts))
  3032. (when (setq l (assoc l org-startup-options))
  3033. (setq var (nth 1 l) val (nth 2 l))
  3034. (if (not (nth 3 l))
  3035. (set (make-local-variable var) val)
  3036. (if (not (listp (symbol-value var)))
  3037. (set (make-local-variable var) nil))
  3038. (set (make-local-variable var) (symbol-value var))
  3039. (add-to-list var val))))))
  3040. ((equal key "ARCHIVE")
  3041. (string-match " *$" value)
  3042. (setq arch (replace-match "" t t value))
  3043. (remove-text-properties 0 (length arch)
  3044. '(face t fontified t) arch)))
  3045. )))
  3046. (and cat (org-set-local 'org-category cat))
  3047. (when prio
  3048. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3049. (setq prio (mapcar 'string-to-char prio))
  3050. (org-set-local 'org-highest-priority (nth 0 prio))
  3051. (org-set-local 'org-lowest-priority (nth 1 prio))
  3052. (org-set-local 'org-default-priority (nth 2 prio)))
  3053. (and arch (org-set-local 'org-archive-location arch))
  3054. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3055. ;; Process the TODO keywords
  3056. (unless kwds
  3057. ;; Use the global values as if they had been given locally.
  3058. (setq kwds (default-value 'org-todo-keywords))
  3059. (if (stringp (car kwds))
  3060. (setq kwds (list (cons org-todo-interpretation
  3061. (default-value 'org-todo-keywords)))))
  3062. (setq kwds (reverse kwds)))
  3063. (setq kwds (nreverse kwds))
  3064. (let (inter kws)
  3065. (while (setq kws (pop kwds))
  3066. (setq inter (pop kws) sep (member "|" kws)
  3067. kws1 (delete "|" (copy-sequence kws))
  3068. hw (car kws1)
  3069. dws (if sep (cdr sep) (last kws1))
  3070. tail (list inter hw (car dws) (org-last dws)))
  3071. (add-to-list 'org-todo-heads hw 'append)
  3072. (push kws1 org-todo-sets)
  3073. (setq org-done-keywords (append org-done-keywords dws nil))
  3074. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3075. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3076. (setq org-todo-sets (nreverse org-todo-sets)
  3077. org-todo-kwd-alist (nreverse org-todo-kwd-alist)))
  3078. ;; Process the tags.
  3079. (when tags
  3080. (let (e tgs)
  3081. (while (setq e (pop tags))
  3082. (cond
  3083. ((equal e "{") (push '(:startgroup) tgs))
  3084. ((equal e "}") (push '(:endgroup) tgs))
  3085. ((string-match "^\\([a-zA-Z0-9_@]+\\)(\\(.\\))$" e)
  3086. (push (cons (match-string 1 e)
  3087. (string-to-char (match-string 2 e)))
  3088. tgs))
  3089. (t (push (list e) tgs))))
  3090. (org-set-local 'org-tag-alist nil)
  3091. (while (setq e (pop tgs))
  3092. (or (and (stringp (car e))
  3093. (assoc (car e) org-tag-alist))
  3094. (push e org-tag-alist))))))
  3095. ;; Compute the regular expressions and other local variables
  3096. (if (not org-done-keywords)
  3097. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3098. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3099. (length org-scheduled-string)))
  3100. org-not-done-keywords
  3101. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3102. org-todo-regexp
  3103. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3104. "\\|") "\\)\\>")
  3105. org-not-done-regexp
  3106. (concat "\\<\\("
  3107. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3108. "\\)\\>")
  3109. org-todo-line-regexp
  3110. (concat "^\\(\\*+\\)[ \t]*\\(?:\\("
  3111. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3112. "\\)\\>\\)? *\\(.*\\)")
  3113. org-nl-done-regexp
  3114. (concat "[\r\n]\\*+[ \t]+"
  3115. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3116. "\\)" "\\>")
  3117. org-todo-line-tags-regexp
  3118. (concat "^\\(\\*+\\)[ \t]*\\(?:\\("
  3119. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3120. "\\)\\>\\)? *\\(.*?\\([ \t]:[a-zA-Z0-9:_@]+:[ \t]*\\)?$\\)")
  3121. org-looking-at-done-regexp
  3122. (concat "^" "\\(?:"
  3123. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3124. "\\>")
  3125. org-deadline-regexp (concat "\\<" org-deadline-string)
  3126. org-deadline-time-regexp
  3127. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3128. org-deadline-line-regexp
  3129. (concat "\\<\\(" org-deadline-string "\\).*")
  3130. org-scheduled-regexp
  3131. (concat "\\<" org-scheduled-string)
  3132. org-scheduled-time-regexp
  3133. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3134. org-closed-time-regexp
  3135. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3136. org-keyword-time-regexp
  3137. (concat "\\<\\(" org-scheduled-string
  3138. "\\|" org-deadline-string
  3139. "\\|" org-closed-string
  3140. "\\|" org-clock-string "\\)"
  3141. " *[[<]\\([^]>]+\\)[]>]")
  3142. org-keyword-time-not-clock-regexp
  3143. (concat "\\<\\(" org-scheduled-string
  3144. "\\|" org-deadline-string
  3145. "\\|" org-closed-string "\\)"
  3146. " *[[<]\\([^]>]+\\)[]>]")
  3147. org-maybe-keyword-time-regexp
  3148. (concat "\\(\\<\\(" org-scheduled-string
  3149. "\\|" org-deadline-string
  3150. "\\|" org-closed-string
  3151. "\\|" org-clock-string "\\)\\)?"
  3152. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3153. org-planning-or-clock-line-re
  3154. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3155. "\\|" org-deadline-string
  3156. "\\|" org-closed-string "\\|" org-clock-string "\\)\\>\\)")
  3157. )
  3158. (org-set-font-lock-defaults)))
  3159. ;;; Some variables ujsed in various places
  3160. (defvar org-window-configuration nil
  3161. "Used in various places to store a window configuration.")
  3162. (defvar org-finish-function nil
  3163. "Function to be called when `C-c C-c' is used.
  3164. This is for getting out of special buffers like remember.")
  3165. ;;; Foreign variables, to inform the compiler
  3166. ;; XEmacs only
  3167. (defvar outline-mode-menu-heading)
  3168. (defvar outline-mode-menu-show)
  3169. (defvar outline-mode-menu-hide)
  3170. (defvar zmacs-regions) ; XEmacs regions
  3171. ;; Emacs only
  3172. (defvar mark-active)
  3173. ;; Packages that org-mode interacts with
  3174. (defvar calc-embedded-close-formula)
  3175. (defvar calc-embedded-open-formula)
  3176. (defvar font-lock-unfontify-region-function)
  3177. (defvar org-goto-start-pos)
  3178. (defvar vm-message-pointer)
  3179. (defvar vm-folder-directory)
  3180. (defvar wl-summary-buffer-elmo-folder)
  3181. (defvar wl-summary-buffer-folder-name)
  3182. (defvar gnus-other-frame-object)
  3183. (defvar gnus-group-name)
  3184. (defvar gnus-article-current)
  3185. (defvar w3m-current-url)
  3186. (defvar w3m-current-title)
  3187. (defvar mh-progs)
  3188. (defvar mh-current-folder)
  3189. (defvar mh-show-folder-buffer)
  3190. (defvar mh-index-folder)
  3191. (defvar mh-searcher)
  3192. (defvar calendar-mode-map)
  3193. (defvar Info-current-file)
  3194. (defvar Info-current-node)
  3195. (defvar texmathp-why)
  3196. (defvar remember-save-after-remembering)
  3197. (defvar remember-data-file)
  3198. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  3199. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  3200. (defvar org-latex-regexps)
  3201. (defvar constants-unit-system)
  3202. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3203. ;; FIXME: Occasionally check by commenting these, to make sure
  3204. ;; no other functions uses these, forgetting to let-bind them.
  3205. (defvar entry)
  3206. (defvar state)
  3207. (defvar last-state)
  3208. (defvar date)
  3209. (defvar description)
  3210. ;; Defined somewhere in this file, but used before definition.
  3211. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  3212. (defvar org-agenda-undo-list)
  3213. (defvar org-agenda-pending-undo-list)
  3214. (defvar org-agenda-overriding-header)
  3215. (defvar orgtbl-mode)
  3216. (defvar org-html-entities)
  3217. (defvar org-struct-menu)
  3218. (defvar org-org-menu)
  3219. (defvar org-tbl-menu)
  3220. (defvar org-agenda-keymap)
  3221. (defvar org-category-table)
  3222. ;;;; Emacs/XEmacs compatibility
  3223. ;; Overlay compatibility functions
  3224. (defun org-make-overlay (beg end &optional buffer)
  3225. (if (featurep 'xemacs)
  3226. (make-extent beg end buffer)
  3227. (make-overlay beg end buffer)))
  3228. (defun org-delete-overlay (ovl)
  3229. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  3230. (defun org-detach-overlay (ovl)
  3231. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  3232. (defun org-move-overlay (ovl beg end &optional buffer)
  3233. (if (featurep 'xemacs)
  3234. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  3235. (move-overlay ovl beg end buffer)))
  3236. (defun org-overlay-put (ovl prop value)
  3237. (if (featurep 'xemacs)
  3238. (set-extent-property ovl prop value)
  3239. (overlay-put ovl prop value)))
  3240. (defun org-overlay-display (ovl text &optional face evap)
  3241. "Make overlay OVL display TEXT with face FACE."
  3242. (if (featurep 'xemacs)
  3243. (let ((gl (make-glyph text)))
  3244. (and face (set-glyph-face gl face))
  3245. (set-extent-property ovl 'invisible t)
  3246. (set-extent-property ovl 'end-glyph gl))
  3247. (overlay-put ovl 'display text)
  3248. (if face (overlay-put ovl 'face face))
  3249. (if evap (overlay-put ovl 'evaporate t))))
  3250. (defun org-overlay-before-string (ovl text &optional face evap)
  3251. "Make overlay OVL display TEXT with face FACE."
  3252. (if (featurep 'xemacs)
  3253. (let ((gl (make-glyph text)))
  3254. (and face (set-glyph-face gl face))
  3255. (set-extent-property ovl 'begin-glyph gl))
  3256. (if face (org-add-props text nil 'face face))
  3257. (overlay-put ovl 'before-string text)
  3258. (if evap (overlay-put ovl 'evaporate t))))
  3259. (defun org-overlay-get (ovl prop)
  3260. (if (featurep 'xemacs)
  3261. (extent-property ovl prop)
  3262. (overlay-get ovl prop)))
  3263. (defun org-overlays-at (pos)
  3264. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  3265. ;; FIXME: this is currently not used
  3266. (defun org-overlays-in (&optional start end)
  3267. (if (featurep 'xemacs)
  3268. (extent-list nil start end)
  3269. (overlays-in start end)))
  3270. (defun org-overlay-start (o)
  3271. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  3272. (defun org-overlay-end (o)
  3273. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  3274. ;; FIXME: this is currently not used
  3275. (defun org-find-overlays (prop &optional pos delete)
  3276. "Find all overlays specifying PROP at POS or point.
  3277. If DELETE is non-nil, delete all those overlays."
  3278. (let ((overlays (org-overlays-at (or pos (point))))
  3279. ov found)
  3280. (while (setq ov (pop overlays))
  3281. (if (org-overlay-get ov prop)
  3282. (if delete (org-delete-overlay ov) (push ov found))))
  3283. found))
  3284. ;; Region compatibility
  3285. (defun org-add-hook (hook function &optional append local)
  3286. "Add-hook, compatible with both Emacsen."
  3287. (if (and local (featurep 'xemacs))
  3288. (add-local-hook hook function append)
  3289. (add-hook hook function append local)))
  3290. (defvar org-ignore-region nil
  3291. "To temporarily disable the active region.")
  3292. (defun org-region-active-p ()
  3293. "Is `transient-mark-mode' on and the region active?
  3294. Works on both Emacs and XEmacs."
  3295. (if org-ignore-region
  3296. nil
  3297. (if (featurep 'xemacs)
  3298. (and zmacs-regions (region-active-p))
  3299. (and transient-mark-mode mark-active))))
  3300. ;; Invisibility compatibility
  3301. (defun org-add-to-invisibility-spec (arg)
  3302. "Add elements to `buffer-invisibility-spec'.
  3303. See documentation for `buffer-invisibility-spec' for the kind of elements
  3304. that can be added."
  3305. (cond
  3306. ((fboundp 'add-to-invisibility-spec)
  3307. (add-to-invisibility-spec arg))
  3308. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  3309. (setq buffer-invisibility-spec (list arg)))
  3310. (t
  3311. (setq buffer-invisibility-spec
  3312. (cons arg buffer-invisibility-spec)))))
  3313. (defun org-remove-from-invisibility-spec (arg)
  3314. "Remove elements from `buffer-invisibility-spec'."
  3315. (if (fboundp 'remove-from-invisibility-spec)
  3316. (remove-from-invisibility-spec arg)
  3317. (if (consp buffer-invisibility-spec)
  3318. (setq buffer-invisibility-spec
  3319. (delete arg buffer-invisibility-spec)))))
  3320. ;; FIXME: this is currently not used
  3321. (defun org-in-invisibility-spec-p (arg)
  3322. "Is ARG a member of `buffer-invisibility-spec'?"
  3323. (if (consp buffer-invisibility-spec)
  3324. (member arg buffer-invisibility-spec)
  3325. nil))
  3326. ;;;; Define the Org-mode
  3327. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3328. (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."))
  3329. ;; We use a before-change function to check if a table might need
  3330. ;; an update.
  3331. (defvar org-table-may-need-update t
  3332. "Indicates that a table might need an update.
  3333. This variable is set by `org-before-change-function'.
  3334. `org-table-align' sets it back to nil.")
  3335. (defvar org-mode-map)
  3336. (defvar org-mode-hook nil)
  3337. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3338. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3339. (defvar org-table-buffer-is-an nil)
  3340. ;;;###autoload
  3341. (define-derived-mode org-mode outline-mode "Org"
  3342. "Outline-based notes management and organizer, alias
  3343. \"Carsten's outline-mode for keeping track of everything.\"
  3344. Org-mode develops organizational tasks around a NOTES file which
  3345. contains information about projects as plain text. Org-mode is
  3346. implemented on top of outline-mode, which is ideal to keep the content
  3347. of large files well structured. It supports ToDo items, deadlines and
  3348. time stamps, which magically appear in the diary listing of the Emacs
  3349. calendar. Tables are easily created with a built-in table editor.
  3350. Plain text URL-like links connect to websites, emails (VM), Usenet
  3351. messages (Gnus), BBDB entries, and any files related to the project.
  3352. For printing and sharing of notes, an Org-mode file (or a part of it)
  3353. can be exported as a structured ASCII or HTML file.
  3354. The following commands are available:
  3355. \\{org-mode-map}"
  3356. ;; Get rid of Outline menus, they are not needed
  3357. ;; Need to do this here because define-derived-mode sets up
  3358. ;; the keymap so late. Still, it is a waste to call this each time
  3359. ;; we switch another buffer into org-mode.
  3360. (if (featurep 'xemacs)
  3361. (when (boundp 'outline-mode-menu-heading)
  3362. ;; Assume this is Greg's port, it used easymenu
  3363. (easy-menu-remove outline-mode-menu-heading)
  3364. (easy-menu-remove outline-mode-menu-show)
  3365. (easy-menu-remove outline-mode-menu-hide))
  3366. (define-key org-mode-map [menu-bar headings] 'undefined)
  3367. (define-key org-mode-map [menu-bar hide] 'undefined)
  3368. (define-key org-mode-map [menu-bar show] 'undefined))
  3369. (easy-menu-add org-org-menu)
  3370. (easy-menu-add org-tbl-menu)
  3371. (org-install-agenda-files-menu)
  3372. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3373. (org-add-to-invisibility-spec '(org-cwidth))
  3374. (when (featurep 'xemacs)
  3375. (org-set-local 'line-move-ignore-invisible t))
  3376. (setq outline-regexp "\\*+")
  3377. (setq outline-level 'org-outline-level)
  3378. (when (and org-ellipsis (stringp org-ellipsis)
  3379. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table))
  3380. (unless org-display-table
  3381. (setq org-display-table (make-display-table)))
  3382. (set-display-table-slot org-display-table
  3383. 4 (string-to-vector org-ellipsis))
  3384. (setq buffer-display-table org-display-table))
  3385. (org-set-regexps-and-options)
  3386. ;; Calc embedded
  3387. (org-set-local 'calc-embedded-open-mode "# ")
  3388. (modify-syntax-entry ?# "<")
  3389. (modify-syntax-entry ?@ "w")
  3390. (if org-startup-truncated (setq truncate-lines t))
  3391. (org-set-local 'font-lock-unfontify-region-function
  3392. 'org-unfontify-region)
  3393. ;; Activate before-change-function
  3394. (org-set-local 'org-table-may-need-update t)
  3395. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3396. 'local)
  3397. ;; Check for running clock before killing a buffer
  3398. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3399. ;; Paragraphs and auto-filling
  3400. (org-set-autofill-regexps)
  3401. (setq indent-line-function 'org-indent-line-function)
  3402. (org-update-radio-target-regexp)
  3403. ;; Comment characters
  3404. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3405. (org-set-local 'comment-padding " ")
  3406. ;; Make isearch reveal context
  3407. (if (or (featurep 'xemacs)
  3408. (not (boundp 'outline-isearch-open-invisible-function)))
  3409. ;; Emacs 21 and XEmacs make use of the hook
  3410. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3411. ;; Emacs 22 deals with this through a special variable
  3412. (org-set-local 'outline-isearch-open-invisible-function
  3413. (lambda (&rest ignore) (org-show-context 'isearch))))
  3414. ;; If empty file that did not turn on org-mode automatically, make it to.
  3415. (if (and org-insert-mode-line-in-empty-file
  3416. (interactive-p)
  3417. (= (point-min) (point-max)))
  3418. (insert "# -*- mode: org -*-\n\n"))
  3419. (unless org-inhibit-startup
  3420. (when org-startup-align-all-tables
  3421. (let ((bmp (buffer-modified-p)))
  3422. (org-table-map-tables 'org-table-align)
  3423. (set-buffer-modified-p bmp)))
  3424. (cond
  3425. ((eq org-startup-folded t)
  3426. (org-cycle '(4)))
  3427. ((eq org-startup-folded 'content)
  3428. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3429. (org-cycle '(4)) (org-cycle '(4)))))))
  3430. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3431. (defsubst org-call-with-arg (command arg)
  3432. "Call COMMAND interactively, but pretend prefix are was ARG."
  3433. (let ((current-prefix-arg arg)) (call-interactively command)))
  3434. (defsubst org-current-line (&optional pos)
  3435. (save-excursion
  3436. (and pos (goto-char pos))
  3437. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  3438. (defun org-current-time ()
  3439. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3440. (if (> org-time-stamp-rounding-minutes 0)
  3441. (let ((r org-time-stamp-rounding-minutes)
  3442. (time (decode-time)))
  3443. (apply 'encode-time
  3444. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3445. (nthcdr 2 time))))
  3446. (current-time)))
  3447. (defun org-add-props (string plist &rest props)
  3448. "Add text properties to entire string, from beginning to end.
  3449. PLIST may be a list of properties, PROPS are individual properties and values
  3450. that will be added to PLIST. Returns the string that was modified."
  3451. (add-text-properties
  3452. 0 (length string) (if props (append plist props) plist) string)
  3453. string)
  3454. (put 'org-add-props 'lisp-indent-function 2)
  3455. ;;;; Font-Lock stuff, including the activators
  3456. (defvar org-mouse-map (make-sparse-keymap))
  3457. (org-defkey org-mouse-map
  3458. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3459. (org-defkey org-mouse-map
  3460. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3461. (when org-mouse-1-follows-link
  3462. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3463. (when org-tab-follows-link
  3464. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3465. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3466. (when org-return-follows-link
  3467. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3468. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3469. (require 'font-lock)
  3470. (defconst org-non-link-chars "]\t\n\r<>")
  3471. (defconst org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  3472. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  3473. (defconst org-link-re-with-space
  3474. (concat
  3475. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3476. "\\([^" org-non-link-chars " ]"
  3477. "[^" org-non-link-chars "]*"
  3478. "[^" org-non-link-chars " ]\\)>?")
  3479. "Matches a link with spaces, optional angular brackets around it.")
  3480. (defconst org-link-re-with-space2
  3481. (concat
  3482. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3483. "\\([^" org-non-link-chars " ]"
  3484. "[^]\t\n\r]*"
  3485. "[^" org-non-link-chars " ]\\)>?")
  3486. "Matches a link with spaces, optional angular brackets around it.")
  3487. (defconst org-angle-link-re
  3488. (concat
  3489. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3490. "\\([^" org-non-link-chars " ]"
  3491. "[^" org-non-link-chars "]*"
  3492. "\\)>")
  3493. "Matches link with angular brackets, spaces are allowed.")
  3494. (defconst org-plain-link-re
  3495. (concat
  3496. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3497. "\\([^]\t\n\r<>,;() ]+\\)")
  3498. "Matches plain link, without spaces.")
  3499. (defconst org-bracket-link-regexp
  3500. "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
  3501. "Matches a link in double brackets.")
  3502. (defconst org-bracket-link-analytic-regexp
  3503. (concat
  3504. "\\[\\["
  3505. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3506. "\\([^]]+\\)"
  3507. "\\]"
  3508. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3509. "\\]"))
  3510. ; 1: http:
  3511. ; 2: http
  3512. ; 3: path
  3513. ; 4: [desc]
  3514. ; 5: desc
  3515. (defconst org-any-link-re
  3516. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3517. org-angle-link-re "\\)\\|\\("
  3518. org-plain-link-re "\\)")
  3519. "Regular expression matching any link.")
  3520. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3521. "Regular expression for fast time stamp matching.")
  3522. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3523. "Regular expression for fast time stamp matching.")
  3524. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3525. "Regular expression matching time strings for analysis.
  3526. This one does not require the space after the date.")
  3527. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3528. "Regular expression matching time strings for analysis.")
  3529. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,11\\}>")
  3530. "Regular expression matching time stamps, with groups.")
  3531. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,11\\}[]>]")
  3532. "Regular expression matching time stamps (also [..]), with groups.")
  3533. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3534. "Regular expression matching a time stamp range.")
  3535. (defconst org-tr-regexp-both
  3536. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3537. "Regular expression matching a time stamp range.")
  3538. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3539. org-ts-regexp "\\)?")
  3540. "Regular expression matching a time stamp or time stamp range.")
  3541. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3542. org-ts-regexp-both "\\)?")
  3543. "Regular expression matching a time stamp or time stamp range.
  3544. The time stamps may be either active or inactive.")
  3545. (defvar org-emph-face nil)
  3546. (defun org-do-emphasis-faces (limit)
  3547. "Run through the buffer and add overlays to links."
  3548. (let (rtn)
  3549. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3550. (if (not (= (char-after (match-beginning 3))
  3551. (char-after (match-beginning 4))))
  3552. (progn
  3553. (setq rtn t)
  3554. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3555. 'face
  3556. (nth 1 (assoc (match-string 3)
  3557. org-emphasis-alist)))
  3558. (add-text-properties (match-beginning 2) (match-end 2)
  3559. '(font-lock-multiline t))
  3560. (backward-char 1))))
  3561. rtn))
  3562. (defun org-emphasize (&optional char)
  3563. "Insert or change an emphasis, i.e. a font like bold or italic.
  3564. If there is an active region, change that region to a new emphasis.
  3565. If there is no region, just insert the marker characters and position
  3566. the cursor between them.
  3567. CHAR should be either the marker character, or the first character of the
  3568. HTML tag associated with that emphasis. If CHAR is a space, the means
  3569. to remove the emphasis of the selected region.
  3570. If char is not given (for example in an interactive call) it
  3571. will be prompted for."
  3572. (interactive)
  3573. (let ((eal org-emphasis-alist) e det
  3574. (erc org-emphasis-regexp-components)
  3575. (prompt "")
  3576. (string "") beg end move tag c s)
  3577. (if (org-region-active-p)
  3578. (setq beg (region-beginning) end (region-end)
  3579. string (buffer-substring beg end))
  3580. (setq move t))
  3581. (while (setq e (pop eal))
  3582. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3583. c (aref tag 0))
  3584. (push (cons c (string-to-char (car e))) det)
  3585. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3586. (substring tag 1)))))
  3587. (unless char
  3588. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3589. (setq char (read-char-exclusive)))
  3590. (setq char (or (cdr (assoc char det)) char))
  3591. (if (equal char ?\ )
  3592. (setq s "" move nil)
  3593. (unless (assoc (char-to-string char) org-emphasis-alist)
  3594. (error "No such emphasis marker: \"%c\"" char))
  3595. (setq s (char-to-string char)))
  3596. (while (and (> (length string) 1)
  3597. (equal (substring string 0 1) (substring string -1))
  3598. (assoc (substring string 0 1) org-emphasis-alist))
  3599. (setq string (substring string 1 -1)))
  3600. (setq string (concat s string s))
  3601. (if beg (delete-region beg end))
  3602. (unless (or (bolp)
  3603. (string-match (concat "[" (nth 0 erc) "\n]")
  3604. (char-to-string (char-before (point)))))
  3605. (insert " "))
  3606. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3607. (char-to-string (char-after (point))))
  3608. (insert " ") (backward-char 1))
  3609. (insert string)
  3610. (and move (backward-char 1))))
  3611. (defun org-activate-plain-links (limit)
  3612. "Run through the buffer and add overlays to links."
  3613. (catch 'exit
  3614. (let (f)
  3615. (while (re-search-forward org-plain-link-re limit t)
  3616. (setq f (get-text-property (match-beginning 0) 'face))
  3617. (if (or (eq f 'org-tag)
  3618. (and (listp f) (memq 'org-tag f)))
  3619. nil
  3620. (add-text-properties (match-beginning 0) (match-end 0)
  3621. (list 'mouse-face 'highlight
  3622. 'rear-nonsticky t
  3623. 'keymap org-mouse-map
  3624. ))
  3625. (throw 'exit t))))))
  3626. (defun org-activate-angle-links (limit)
  3627. "Run through the buffer and add overlays to links."
  3628. (if (re-search-forward org-angle-link-re limit t)
  3629. (progn
  3630. (add-text-properties (match-beginning 0) (match-end 0)
  3631. (list 'mouse-face 'highlight
  3632. 'rear-nonsticky t
  3633. 'keymap org-mouse-map
  3634. ))
  3635. t)))
  3636. (defmacro org-maybe-intangible (props)
  3637. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  3638. In emacs 21, invisible text is not avoided by the command loop, so the
  3639. intangible property is needed to make sure point skips this text.
  3640. In Emacs 22, this is not necessary. The intangible text property has
  3641. led to problems with flyspell. These problems are fixed in flyspell.el,
  3642. but we still avoid setting the property in Emacs 22 and later.
  3643. We use a macro so that the test can happen at compilation time."
  3644. (if (< emacs-major-version 22)
  3645. `(append '(intangible t) ,props)
  3646. props))
  3647. (defun org-activate-bracket-links (limit)
  3648. "Run through the buffer and add overlays to bracketed links."
  3649. (if (re-search-forward org-bracket-link-regexp limit t)
  3650. (let* ((help (concat "LINK: "
  3651. (org-match-string-no-properties 1)))
  3652. ;; FIXME: above we should remove the escapes.
  3653. ;; but that requires another match, protecting match data,
  3654. ;; a lot of overhead for font-lock.
  3655. (ip (org-maybe-intangible
  3656. (list 'invisible 'org-link 'rear-nonsticky t
  3657. 'keymap org-mouse-map 'mouse-face 'highlight
  3658. 'help-echo help)))
  3659. (vp (list 'rear-nonsticky t
  3660. 'keymap org-mouse-map 'mouse-face 'highlight
  3661. 'help-echo help)))
  3662. ;; We need to remove the invisible property here. Table narrowing
  3663. ;; may have made some of this invisible.
  3664. (remove-text-properties (match-beginning 0) (match-end 0)
  3665. '(invisible nil))
  3666. (if (match-end 3)
  3667. (progn
  3668. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3669. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3670. (add-text-properties (match-end 3) (match-end 0) ip))
  3671. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3672. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3673. (add-text-properties (match-end 1) (match-end 0) ip))
  3674. t)))
  3675. (defun org-activate-dates (limit)
  3676. "Run through the buffer and add overlays to dates."
  3677. (if (re-search-forward org-tsr-regexp-both limit t)
  3678. (progn
  3679. (add-text-properties (match-beginning 0) (match-end 0)
  3680. (list 'mouse-face 'highlight
  3681. 'rear-nonsticky t
  3682. 'keymap org-mouse-map))
  3683. (when org-display-custom-times
  3684. (if (match-end 3)
  3685. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3686. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3687. t)))
  3688. (defvar org-target-link-regexp nil
  3689. "Regular expression matching radio targets in plain text.")
  3690. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3691. "Regular expression matching a link target.")
  3692. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3693. "Regular expression matching a radio target.")
  3694. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3695. "Regular expression matching any target.")
  3696. (defun org-activate-target-links (limit)
  3697. "Run through the buffer and add overlays to target matches."
  3698. (when org-target-link-regexp
  3699. (let ((case-fold-search t))
  3700. (if (re-search-forward org-target-link-regexp limit t)
  3701. (progn
  3702. (add-text-properties (match-beginning 0) (match-end 0)
  3703. (list 'mouse-face 'highlight
  3704. 'rear-nonsticky t
  3705. 'keymap org-mouse-map
  3706. 'help-echo "Radio target link"
  3707. 'org-linked-text t))
  3708. t)))))
  3709. (defun org-update-radio-target-regexp ()
  3710. "Find all radio targets in this file and update the regular expression."
  3711. (interactive)
  3712. (when (memq 'radio org-activate-links)
  3713. (setq org-target-link-regexp
  3714. (org-make-target-link-regexp (org-all-targets 'radio)))
  3715. (org-restart-font-lock)))
  3716. (defun org-hide-wide-columns (limit)
  3717. (let (s e)
  3718. (setq s (text-property-any (point) (or limit (point-max))
  3719. 'org-cwidth t))
  3720. (when s
  3721. (setq e (next-single-property-change s 'org-cwidth))
  3722. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3723. (goto-char e)
  3724. t)))
  3725. (defun org-restart-font-lock ()
  3726. "Restart font-lock-mode, to force refontification."
  3727. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3728. (font-lock-mode -1)
  3729. (font-lock-mode 1)))
  3730. (defun org-all-targets (&optional radio)
  3731. "Return a list of all targets in this file.
  3732. With optional argument RADIO, only find radio targets."
  3733. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3734. rtn)
  3735. (save-excursion
  3736. (goto-char (point-min))
  3737. (while (re-search-forward re nil t)
  3738. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3739. rtn)))
  3740. (defun org-make-target-link-regexp (targets)
  3741. "Make regular expression matching all strings in TARGETS.
  3742. The regular expression finds the targets also if there is a line break
  3743. between words."
  3744. (and targets
  3745. (concat
  3746. "\\<\\("
  3747. (mapconcat
  3748. (lambda (x)
  3749. (while (string-match " +" x)
  3750. (setq x (replace-match "\\s-+" t t x)))
  3751. x)
  3752. targets
  3753. "\\|")
  3754. "\\)\\>")))
  3755. (defun org-activate-tags (limit)
  3756. (if (re-search-forward "[ \t]\\(:[a-zA-Z0-9_@:]+:\\)[ \r\n]" limit t)
  3757. (progn
  3758. (add-text-properties (match-beginning 1) (match-end 1)
  3759. (list 'mouse-face 'highlight
  3760. 'rear-nonsticky t
  3761. 'keymap org-mouse-map))
  3762. t)))
  3763. (defun org-outline-level ()
  3764. (save-excursion
  3765. (looking-at outline-regexp)
  3766. (if (match-beginning 1)
  3767. (+ (org-get-string-indentation (match-string 1)) 1000)
  3768. (- (match-end 0) (match-beginning 0)))))
  3769. (defvar org-font-lock-keywords nil)
  3770. (defun org-set-font-lock-defaults ()
  3771. (let* ((em org-fontify-emphasized-text)
  3772. (lk org-activate-links)
  3773. (org-font-lock-extra-keywords
  3774. ;; Headlines
  3775. (list
  3776. '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
  3777. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3778. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3779. (1 'org-table))
  3780. ;; Links
  3781. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3782. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3783. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3784. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3785. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3786. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3787. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3788. '(org-hide-wide-columns (0 nil append))
  3789. ;; TODO lines
  3790. (list (concat "^\\*+[ \t]*" org-not-done-regexp)
  3791. '(1 'org-todo t))
  3792. ;; Priorities
  3793. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3794. ;; Special keywords
  3795. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3796. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3797. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3798. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3799. ;; Emphasis
  3800. (if em
  3801. (if (featurep 'xemacs)
  3802. '(org-do-emphasis-faces (0 nil append))
  3803. '(org-do-emphasis-faces)))
  3804. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  3805. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
  3806. 2 'bold prepend)
  3807. (if org-provide-checkbox-statistics
  3808. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3809. (0 (org-get-checkbox-statistics-face) t)))
  3810. ;; COMMENT
  3811. (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
  3812. "\\|" org-quote-string "\\)\\>")
  3813. '(1 'org-special-keyword t))
  3814. '("^#.*" (0 'font-lock-comment-face t))
  3815. ;; DONE
  3816. (if org-fontify-done-headline
  3817. (list (concat "^[*]+ +\\<\\("
  3818. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3819. "\\)\\(.*\\)")
  3820. '(1 'org-done t) '(2 'org-headline-done t))
  3821. (list (concat "^[*]+ +\\<\\("
  3822. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3823. "\\)\\>")
  3824. '(1 'org-done t)))
  3825. ;; Table stuff
  3826. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  3827. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3828. '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
  3829. (if org-format-transports-properties-p
  3830. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3831. '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
  3832. )))
  3833. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3834. ;; Now set the full font-lock-keywords
  3835. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3836. (org-set-local 'font-lock-defaults
  3837. '(org-font-lock-keywords t nil nil backward-paragraph))
  3838. (kill-local-variable 'font-lock-keywords) nil))
  3839. (defvar org-m nil)
  3840. (defvar org-l nil)
  3841. (defvar org-f nil)
  3842. (defun org-get-level-face (n)
  3843. "Get the right face for match N in font-lock matching of healdines."
  3844. (setq org-l (- (match-end 2) (match-beginning 1)))
  3845. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3846. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  3847. (cond
  3848. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3849. ((eq n 2) org-f)
  3850. (t (if org-level-color-stars-only nil org-f))))
  3851. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3852. "Remove fontification and activation overlays from links."
  3853. (font-lock-default-unfontify-region beg end)
  3854. (let* ((buffer-undo-list t)
  3855. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3856. (inhibit-modification-hooks t)
  3857. deactivate-mark buffer-file-name buffer-file-truename)
  3858. (remove-text-properties beg end
  3859. '(mouse-face t keymap t org-linked-text t
  3860. rear-nonsticky t
  3861. invisible t intangible t))))
  3862. ;;;; Visibility cycling, including org-goto and indirect buffer
  3863. ;;; Cycling
  3864. (defvar org-cycle-global-status nil)
  3865. (make-variable-buffer-local 'org-cycle-global-status)
  3866. (defvar org-cycle-subtree-status nil)
  3867. (make-variable-buffer-local 'org-cycle-subtree-status)
  3868. ;;;###autoload
  3869. (defun org-cycle (&optional arg)
  3870. "Visibility cycling for Org-mode.
  3871. - When this function is called with a prefix argument, rotate the entire
  3872. buffer through 3 states (global cycling)
  3873. 1. OVERVIEW: Show only top-level headlines.
  3874. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3875. 3. SHOW ALL: Show everything.
  3876. - When point is at the beginning of a headline, rotate the subtree started
  3877. by this line through 3 different states (local cycling)
  3878. 1. FOLDED: Only the main headline is shown.
  3879. 2. CHILDREN: The main headline and the direct children are shown.
  3880. From this state, you can move to one of the children
  3881. and zoom in further.
  3882. 3. SUBTREE: Show the entire subtree, including body text.
  3883. - When there is a numeric prefix, go up to a heading with level ARG, do
  3884. a `show-subtree' and return to the previous cursor position. If ARG
  3885. is negative, go up that many levels.
  3886. - When point is not at the beginning of a headline, execute
  3887. `indent-relative', like TAB normally does. See the option
  3888. `org-cycle-emulate-tab' for details.
  3889. - Special case: if point is at the beginning of the buffer and there is
  3890. no headline in line 1, this function will act as if called with prefix arg."
  3891. (interactive "P")
  3892. (let* ((outline-regexp
  3893. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3894. "\\(?:\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3895. outline-regexp))
  3896. (bob-special (and org-cycle-global-at-bob (bobp)
  3897. (not (looking-at outline-regexp))))
  3898. (org-cycle-hook
  3899. (if bob-special
  3900. (delq 'org-optimize-window-after-visibility-change
  3901. (copy-sequence org-cycle-hook))
  3902. org-cycle-hook))
  3903. (pos (point)))
  3904. (if (or bob-special (equal arg '(4)))
  3905. ;; special case: use global cycling
  3906. (setq arg t))
  3907. (cond
  3908. ((org-at-table-p 'any)
  3909. ;; Enter the table or move to the next field in the table
  3910. (or (org-table-recognize-table.el)
  3911. (progn
  3912. (if arg (org-table-edit-field t)
  3913. (org-table-justify-field-maybe)
  3914. (call-interactively 'org-table-next-field)))))
  3915. ((eq arg t) ;; Global cycling
  3916. (cond
  3917. ((and (eq last-command this-command)
  3918. (eq org-cycle-global-status 'overview))
  3919. ;; We just created the overview - now do table of contents
  3920. ;; This can be slow in very large buffers, so indicate action
  3921. (message "CONTENTS...")
  3922. (org-content)
  3923. (message "CONTENTS...done")
  3924. (setq org-cycle-global-status 'contents)
  3925. (run-hook-with-args 'org-cycle-hook 'contents))
  3926. ((and (eq last-command this-command)
  3927. (eq org-cycle-global-status 'contents))
  3928. ;; We just showed the table of contents - now show everything
  3929. (show-all)
  3930. (message "SHOW ALL")
  3931. (setq org-cycle-global-status 'all)
  3932. (run-hook-with-args 'org-cycle-hook 'all))
  3933. (t
  3934. ;; Default action: go to overview
  3935. (org-overview)
  3936. (message "OVERVIEW")
  3937. (setq org-cycle-global-status 'overview)
  3938. (run-hook-with-args 'org-cycle-hook 'overview))))
  3939. ((integerp arg)
  3940. ;; Show-subtree, ARG levels up from here.
  3941. (save-excursion
  3942. (org-back-to-heading)
  3943. (outline-up-heading (if (< arg 0) (- arg)
  3944. (- (funcall outline-level) arg)))
  3945. (org-show-subtree)))
  3946. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3947. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3948. ;; At a heading: rotate between three different views
  3949. (org-back-to-heading)
  3950. (let ((goal-column 0) eoh eol eos)
  3951. ;; First, some boundaries
  3952. (save-excursion
  3953. (org-back-to-heading)
  3954. (save-excursion
  3955. (beginning-of-line 2)
  3956. (while (and (not (eobp)) ;; this is like `next-line'
  3957. (get-char-property (1- (point)) 'invisible))
  3958. (beginning-of-line 2)) (setq eol (point)))
  3959. (outline-end-of-heading) (setq eoh (point))
  3960. (org-end-of-subtree t)
  3961. (unless (eobp)
  3962. (skip-chars-forward " \t\n")
  3963. (beginning-of-line 1) ; in case this is an item
  3964. )
  3965. (setq eos (1- (point))))
  3966. ;; Find out what to do next and set `this-command'
  3967. (cond
  3968. ((= eos eoh)
  3969. ;; Nothing is hidden behind this heading
  3970. (message "EMPTY ENTRY")
  3971. (setq org-cycle-subtree-status nil)
  3972. (save-excursion
  3973. (goto-char eos)
  3974. (outline-next-heading)
  3975. (if (org-invisible-p) (org-flag-heading nil))))
  3976. ((>= eol eos)
  3977. ;; Entire subtree is hidden in one line: open it
  3978. (org-show-entry)
  3979. (show-children)
  3980. (message "CHILDREN")
  3981. (save-excursion
  3982. (goto-char eos)
  3983. (outline-next-heading)
  3984. (if (org-invisible-p) (org-flag-heading nil)))
  3985. (setq org-cycle-subtree-status 'children)
  3986. (run-hook-with-args 'org-cycle-hook 'children))
  3987. ((and (eq last-command this-command)
  3988. (eq org-cycle-subtree-status 'children))
  3989. ;; We just showed the children, now show everything.
  3990. (org-show-subtree)
  3991. (message "SUBTREE")
  3992. (setq org-cycle-subtree-status 'subtree)
  3993. (run-hook-with-args 'org-cycle-hook 'subtree))
  3994. (t
  3995. ;; Default action: hide the subtree.
  3996. (hide-subtree)
  3997. (message "FOLDED")
  3998. (setq org-cycle-subtree-status 'folded)
  3999. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4000. ;; TAB emulation
  4001. (buffer-read-only (org-back-to-heading))
  4002. ((org-try-cdlatex-tab))
  4003. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4004. (or (not (bolp))
  4005. (not (looking-at outline-regexp))))
  4006. (call-interactively (global-key-binding "\t")))
  4007. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4008. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4009. (or (and (eq org-cycle-emulate-tab 'white)
  4010. (= (match-end 0) (point-at-eol)))
  4011. (and (eq org-cycle-emulate-tab 'whitestart)
  4012. (>= (match-end 0) pos))))
  4013. t
  4014. (eq org-cycle-emulate-tab t))
  4015. (if (and (looking-at "[ \n\r\t]")
  4016. (string-match "^[ \t]*$" (buffer-substring
  4017. (point-at-bol) (point))))
  4018. (progn
  4019. (beginning-of-line 1)
  4020. (and (looking-at "[ \t]+") (replace-match ""))))
  4021. (call-interactively (global-key-binding "\t")))
  4022. (t (save-excursion
  4023. (org-back-to-heading)
  4024. (org-cycle))))))
  4025. ;;;###autoload
  4026. (defun org-global-cycle (&optional arg)
  4027. "Cycle the global visibility. For details see `org-cycle'."
  4028. (interactive "P")
  4029. (let ((org-cycle-include-plain-lists
  4030. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4031. (if (integerp arg)
  4032. (progn
  4033. (show-all)
  4034. (hide-sublevels arg)
  4035. (setq org-cycle-global-status 'contents))
  4036. (org-cycle '(4)))))
  4037. (defun org-overview ()
  4038. "Switch to overview mode, shoing only top-level headlines.
  4039. Really, this shows all headlines with level equal or greater than the level
  4040. of the first headline in the buffer. This is important, because if the
  4041. first headline is not level one, then (hide-sublevels 1) gives confusing
  4042. results."
  4043. (interactive)
  4044. (let ((level (save-excursion
  4045. (goto-char (point-min))
  4046. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4047. (progn
  4048. (goto-char (match-beginning 0))
  4049. (funcall outline-level))))))
  4050. (and level (hide-sublevels level))))
  4051. (defun org-content (&optional arg)
  4052. "Show all headlines in the buffer, like a table of contents.
  4053. With numerical argument N, show content up to level N."
  4054. (interactive "P")
  4055. (save-excursion
  4056. ;; Visit all headings and show their offspring
  4057. (and (integerp arg) (org-overview))
  4058. (goto-char (point-max))
  4059. (catch 'exit
  4060. (while (and (progn (condition-case nil
  4061. (outline-previous-visible-heading 1)
  4062. (error (goto-char (point-min))))
  4063. t)
  4064. (looking-at outline-regexp))
  4065. (if (integerp arg)
  4066. (show-children (1- arg))
  4067. (show-branches))
  4068. (if (bobp) (throw 'exit nil))))))
  4069. (defun org-optimize-window-after-visibility-change (state)
  4070. "Adjust the window after a change in outline visibility.
  4071. This function is the default value of the hook `org-cycle-hook'."
  4072. (when (get-buffer-window (current-buffer))
  4073. (cond
  4074. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4075. ; ((eq state 'overview) (org-beginning-of-line))
  4076. ((eq state 'content) nil)
  4077. ((eq state 'all) nil)
  4078. ((eq state 'folded) nil)
  4079. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4080. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4081. (defun org-cycle-show-empty-lines (state)
  4082. "Show empty lines above all visible headlines.
  4083. The region to be covered depends on STATE when called through
  4084. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4085. entire buffer covered. Note that an empty line is only shown if there
  4086. are at least `org-cycle-separator-lines' empty lines before the headeline."
  4087. (when (> org-cycle-separator-lines 0)
  4088. (save-excursion
  4089. (let* ((n org-cycle-separator-lines)
  4090. (re (cond
  4091. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4092. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4093. (t (let ((ns (number-to-string (- n 2))))
  4094. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4095. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4096. beg end)
  4097. (cond
  4098. ((memq state '(overview contents t))
  4099. (setq beg (point-min) end (point-max)))
  4100. ((memq state '(children folded))
  4101. (setq beg (point) end (progn (org-end-of-subtree t t)
  4102. (beginning-of-line 2)
  4103. (point)))))
  4104. (when beg
  4105. (goto-char beg)
  4106. (while (re-search-forward re end t)
  4107. (if (not (get-char-property (match-end 1) 'invisible))
  4108. (outline-flag-region
  4109. (match-beginning 1) (match-end 1) nil)))))))
  4110. ;; Never hide empty lines at the end of the file.
  4111. (save-excursion
  4112. (goto-char (point-max))
  4113. (outline-previous-heading)
  4114. (outline-end-of-heading)
  4115. (if (and (looking-at "[ \t\n]+")
  4116. (= (match-end 0) (point-max)))
  4117. (outline-flag-region (point) (match-end 0) nil))))
  4118. (defun org-subtree-end-visible-p ()
  4119. "Is the end of the current subtree visible?"
  4120. (pos-visible-in-window-p
  4121. (save-excursion (org-end-of-subtree t) (point))))
  4122. (defun org-first-headline-recenter (&optional N)
  4123. "Move cursor to the first headline and recenter the headline.
  4124. Optional argument N means, put the headline into the Nth line of the window."
  4125. (goto-char (point-min))
  4126. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4127. (beginning-of-line)
  4128. (recenter (prefix-numeric-value N))))
  4129. ;;; Org-goto
  4130. (defvar org-goto-window-configuration nil)
  4131. (defvar org-goto-marker nil)
  4132. (defvar org-goto-map (make-sparse-keymap))
  4133. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  4134. (while (setq cmd (pop cmds))
  4135. (substitute-key-definition cmd cmd org-goto-map global-map)))
  4136. (org-defkey org-goto-map "\C-m" 'org-goto-ret)
  4137. (org-defkey org-goto-map [(left)] 'org-goto-left)
  4138. (org-defkey org-goto-map [(right)] 'org-goto-right)
  4139. (org-defkey org-goto-map [(?q)] 'org-goto-quit)
  4140. (org-defkey org-goto-map [(control ?g)] 'org-goto-quit)
  4141. (org-defkey org-goto-map "\C-i" 'org-cycle)
  4142. (org-defkey org-goto-map [(tab)] 'org-cycle)
  4143. (org-defkey org-goto-map [(down)] 'outline-next-visible-heading)
  4144. (org-defkey org-goto-map [(up)] 'outline-previous-visible-heading)
  4145. (org-defkey org-goto-map "n" 'outline-next-visible-heading)
  4146. (org-defkey org-goto-map "p" 'outline-previous-visible-heading)
  4147. (org-defkey org-goto-map "f" 'outline-forward-same-level)
  4148. (org-defkey org-goto-map "b" 'outline-backward-same-level)
  4149. (org-defkey org-goto-map "u" 'outline-up-heading)
  4150. (org-defkey org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  4151. (org-defkey org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  4152. (org-defkey org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  4153. (org-defkey org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  4154. (org-defkey org-goto-map "\C-c\C-u" 'outline-up-heading)
  4155. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  4156. (while l (org-defkey org-goto-map (int-to-string (pop l)) 'digit-argument)))
  4157. (defconst org-goto-help
  4158. "Select a location to jump to, press RET
  4159. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  4160. (defun org-goto ()
  4161. "Go to a different location of the document, keeping current visibility.
  4162. When you want to go to a different location in a document, the fastest way
  4163. is often to fold the entire buffer and then dive into the tree. This
  4164. method has the disadvantage, that the previous location will be folded,
  4165. which may not be what you want.
  4166. This command works around this by showing a copy of the current buffer in
  4167. overview mode. You can dive into the tree in that copy, to find the
  4168. location you want to reach. When pressing RET, the command returns to the
  4169. original buffer in which the visibility is still unchanged. It then jumps
  4170. to the new location, making it and the headline hierarchy above it visible."
  4171. (interactive)
  4172. (let* ((org-goto-start-pos (point))
  4173. (selected-point
  4174. (org-get-location (current-buffer) org-goto-help)))
  4175. (if selected-point
  4176. (progn
  4177. (org-mark-ring-push org-goto-start-pos)
  4178. (goto-char selected-point)
  4179. (if (or (org-invisible-p) (org-invisible-p2))
  4180. (org-show-context 'org-goto)))
  4181. (error "Quit"))))
  4182. (defvar org-selected-point nil) ; dynamically scoped parameter
  4183. (defun org-get-location (buf help)
  4184. "Let the user select a location in the Org-mode buffer BUF.
  4185. This function uses a recursive edit. It returns the selected position
  4186. or nil."
  4187. (let (org-selected-point)
  4188. (save-excursion
  4189. (save-window-excursion
  4190. (delete-other-windows)
  4191. (switch-to-buffer (get-buffer-create "*org-goto*"))
  4192. (with-output-to-temp-buffer "*Help*"
  4193. (princ help))
  4194. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  4195. (setq buffer-read-only nil)
  4196. (erase-buffer)
  4197. (insert-buffer-substring buf)
  4198. (let ((org-startup-truncated t)
  4199. (org-startup-folded t)
  4200. (org-startup-align-all-tables nil))
  4201. (org-mode))
  4202. (setq buffer-read-only t)
  4203. (if (and (boundp 'org-goto-start-pos)
  4204. (integer-or-marker-p org-goto-start-pos))
  4205. (let ((org-show-hierarchy-above t)
  4206. (org-show-siblings t)
  4207. (org-show-following-heading t))
  4208. (goto-char org-goto-start-pos)
  4209. (and (org-invisible-p) (org-show-context)))
  4210. (goto-char (point-min)))
  4211. (org-beginning-of-line)
  4212. (message "Select location and press RET")
  4213. ;; now we make sure that during selection, ony very few keys work
  4214. ;; and that it is impossible to switch to another window.
  4215. (let ((gm (current-global-map))
  4216. (overriding-local-map org-goto-map))
  4217. (unwind-protect
  4218. (progn
  4219. (use-global-map org-goto-map)
  4220. (recursive-edit))
  4221. (use-global-map gm)))))
  4222. (kill-buffer "*org-goto*")
  4223. org-selected-point))
  4224. (defun org-goto-ret (&optional arg)
  4225. "Finish `org-goto' by going to the new location."
  4226. (interactive "P")
  4227. (setq org-selected-point (point)
  4228. current-prefix-arg arg)
  4229. (throw 'exit nil))
  4230. (defun org-goto-left ()
  4231. "Finish `org-goto' by going to the new location."
  4232. (interactive)
  4233. (if (org-on-heading-p)
  4234. (progn
  4235. (beginning-of-line 1)
  4236. (setq org-selected-point (point)
  4237. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  4238. (throw 'exit nil))
  4239. (error "Not on a heading")))
  4240. (defun org-goto-right ()
  4241. "Finish `org-goto' by going to the new location."
  4242. (interactive)
  4243. (if (org-on-heading-p)
  4244. (progn
  4245. (outline-end-of-subtree)
  4246. (or (eobp) (forward-char 1))
  4247. (setq org-selected-point (point)
  4248. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  4249. (throw 'exit nil))
  4250. (error "Not on a heading")))
  4251. (defun org-goto-quit ()
  4252. "Finish `org-goto' without cursor motion."
  4253. (interactive)
  4254. (setq org-selected-point nil)
  4255. (throw 'exit nil))
  4256. ;;; Indirect buffer display of subtrees
  4257. (defvar org-indirect-dedicated-frame nil
  4258. "This is the frame being used for indirect tree display.")
  4259. (defvar org-last-indirect-buffer nil)
  4260. (defun org-tree-to-indirect-buffer (&optional arg)
  4261. "Create indirect buffer and narrow it to current subtree.
  4262. With numerical prefix ARG, go up to this level and then take that tree.
  4263. If ARG is negative, go up that many levels.
  4264. Normally this command removes the indirect buffer previously made
  4265. with this command. However, when called with a C-u prefix, the last buffer
  4266. is kept so that you can work with several indirect buffers at the same time.
  4267. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4268. requests that a new frame be made for the new buffer, so that the dedicated
  4269. frame is not changed."
  4270. (interactive "P")
  4271. (let ((cbuf (current-buffer))
  4272. (cwin (selected-window))
  4273. (pos (point))
  4274. beg end level heading ibuf)
  4275. (save-excursion
  4276. (org-back-to-heading t)
  4277. (when (numberp arg)
  4278. (setq level (org-outline-level))
  4279. (if (< arg 0) (setq arg (+ level arg)))
  4280. (while (> (setq level (org-outline-level)) arg)
  4281. (outline-up-heading 1 t)))
  4282. (setq beg (point)
  4283. heading (org-get-heading))
  4284. (org-end-of-subtree t) (setq end (point)))
  4285. (if (and (not arg)
  4286. (buffer-live-p org-last-indirect-buffer))
  4287. (kill-buffer org-last-indirect-buffer))
  4288. (setq ibuf (org-get-indirect-buffer cbuf)
  4289. org-last-indirect-buffer ibuf)
  4290. (cond
  4291. ((or (eq org-indirect-buffer-display 'new-frame)
  4292. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4293. (select-frame (make-frame))
  4294. (delete-other-windows)
  4295. (switch-to-buffer ibuf)
  4296. (org-set-frame-title heading))
  4297. ((eq org-indirect-buffer-display 'dedicated-frame)
  4298. (raise-frame
  4299. (select-frame (or (and org-indirect-dedicated-frame
  4300. (frame-live-p org-indirect-dedicated-frame)
  4301. org-indirect-dedicated-frame)
  4302. (setq org-indirect-dedicated-frame (make-frame)))))
  4303. (delete-other-windows)
  4304. (switch-to-buffer ibuf)
  4305. (org-set-frame-title (concat "Indirect: " heading)))
  4306. ((eq org-indirect-buffer-display 'current-window)
  4307. (switch-to-buffer ibuf))
  4308. ((eq org-indirect-buffer-display 'other-window)
  4309. (pop-to-buffer ibuf))
  4310. (t (error "Invalid value.")))
  4311. (if (featurep 'xemacs)
  4312. (save-excursion (org-mode) (turn-on-font-lock)))
  4313. (narrow-to-region beg end)
  4314. (show-all)
  4315. (goto-char pos)
  4316. (and (window-live-p cwin) (select-window cwin))))
  4317. (defun org-get-indirect-buffer (&optional buffer)
  4318. (setq buffer (or buffer (current-buffer)))
  4319. (let ((n 1) (base (buffer-name buffer)) bname)
  4320. (while (buffer-live-p
  4321. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4322. (setq n (1+ n)))
  4323. (condition-case nil
  4324. (make-indirect-buffer buffer bname 'clone)
  4325. (error (make-indirect-buffer buffer bname)))))
  4326. (defun org-set-frame-title (title)
  4327. "Set the title of the current frame to the string TITLE."
  4328. ;; FIXME: how to name a single frame in XEmacs???
  4329. (unless (featurep 'xemacs)
  4330. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4331. ;;;; Structure editing
  4332. ;;; Inserting headlines
  4333. (defun org-insert-heading (&optional force-heading)
  4334. "Insert a new heading or item with same depth at point.
  4335. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4336. If point is at the beginning of a headline, insert a sibling before the
  4337. current headline. If point is in the middle of a headline, split the headline
  4338. at that position and make the rest of the headline part of the sibling below
  4339. the current headline."
  4340. (interactive "P")
  4341. (if (= (buffer-size) 0)
  4342. (insert "\n* ")
  4343. (when (or force-heading (not (org-insert-item)))
  4344. (let* ((head (save-excursion
  4345. (condition-case nil
  4346. (progn
  4347. (org-back-to-heading)
  4348. (match-string 0))
  4349. (error "*"))))
  4350. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4351. pos)
  4352. (cond
  4353. ((and (org-on-heading-p) (bolp)
  4354. (or (bobp)
  4355. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4356. (open-line (if blank 2 1)))
  4357. ((and (bolp)
  4358. (or (bobp)
  4359. (save-excursion
  4360. (backward-char 1) (not (org-invisible-p)))))
  4361. nil)
  4362. (t (newline (if blank 2 1))))
  4363. (insert head) (just-one-space)
  4364. (setq pos (point))
  4365. (end-of-line 1)
  4366. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4367. (run-hooks 'org-insert-heading-hook)))))
  4368. (defun org-insert-todo-heading (arg)
  4369. "Insert a new heading with the same level and TODO state as current heading.
  4370. If the heading has no TODO state, or if the state is DONE, use the first
  4371. state (TODO by default). Also with prefix arg, force first state."
  4372. (interactive "P")
  4373. (when (not (org-insert-item 'checkbox))
  4374. (org-insert-heading)
  4375. (save-excursion
  4376. (org-back-to-heading)
  4377. (outline-previous-heading)
  4378. (looking-at org-todo-line-regexp))
  4379. (if (or arg
  4380. (not (match-beginning 2))
  4381. (member (match-string 2) org-done-keywords))
  4382. (insert (car org-todo-keywords-1) " ")
  4383. (insert (match-string 2) " "))))
  4384. ;;; Promotion and Demotion
  4385. (defun org-promote-subtree ()
  4386. "Promote the entire subtree.
  4387. See also `org-promote'."
  4388. (interactive)
  4389. (save-excursion
  4390. (org-map-tree 'org-promote))
  4391. (org-fix-position-after-promote))
  4392. (defun org-demote-subtree ()
  4393. "Demote the entire subtree. See `org-demote'.
  4394. See also `org-promote'."
  4395. (interactive)
  4396. (save-excursion
  4397. (org-map-tree 'org-demote))
  4398. (org-fix-position-after-promote))
  4399. (defun org-do-promote ()
  4400. "Promote the current heading higher up the tree.
  4401. If the region is active in `transient-mark-mode', promote all headings
  4402. in the region."
  4403. (interactive)
  4404. (save-excursion
  4405. (if (org-region-active-p)
  4406. (org-map-region 'org-promote (region-beginning) (region-end))
  4407. (org-promote)))
  4408. (org-fix-position-after-promote))
  4409. (defun org-do-demote ()
  4410. "Demote the current heading lower down the tree.
  4411. If the region is active in `transient-mark-mode', demote all headings
  4412. in the region."
  4413. (interactive)
  4414. (save-excursion
  4415. (if (org-region-active-p)
  4416. (org-map-region 'org-demote (region-beginning) (region-end))
  4417. (org-demote)))
  4418. (org-fix-position-after-promote))
  4419. (defun org-fix-position-after-promote ()
  4420. "Make sure that after pro/demotion cursor position is right."
  4421. (let ((pos (point)))
  4422. (when (save-excursion
  4423. (beginning-of-line 1)
  4424. (looking-at org-todo-line-regexp)
  4425. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4426. (cond ((eobp) (insert " "))
  4427. ((eolp) (insert " "))
  4428. ((equal (char-after) ?\ ) (forward-char 1))))))
  4429. (defun org-get-legal-level (level &optional change)
  4430. "Rectify a level change under the influence of `org-odd-levels-only'
  4431. LEVEL is a current level, CHANGE is by how much the level should be
  4432. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4433. even level numbers will become the next higher odd number."
  4434. (if org-odd-levels-only
  4435. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4436. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4437. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4438. (max 1 (+ level change))))
  4439. (defun org-promote ()
  4440. "Promote the current heading higher up the tree.
  4441. If the region is active in `transient-mark-mode', promote all headings
  4442. in the region."
  4443. (org-back-to-heading t)
  4444. (let* ((level (save-match-data (funcall outline-level)))
  4445. (up-head (make-string (org-get-legal-level level -1) ?*))
  4446. (diff (abs (- level (length up-head)))))
  4447. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4448. (replace-match up-head nil t)
  4449. ;; Fixup tag positioning
  4450. (and org-auto-align-tags (org-set-tags nil t))
  4451. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4452. (defun org-demote ()
  4453. "Demote the current heading lower down the tree.
  4454. If the region is active in `transient-mark-mode', demote all headings
  4455. in the region."
  4456. (org-back-to-heading t)
  4457. (let* ((level (save-match-data (funcall outline-level)))
  4458. (down-head (make-string (org-get-legal-level level 1) ?*))
  4459. (diff (abs (- level (length down-head)))))
  4460. (replace-match down-head nil t)
  4461. ;; Fixup tag positioning
  4462. (and org-auto-align-tags (org-set-tags nil t))
  4463. (if org-adapt-indentation (org-fixup-indentation diff))))
  4464. (defun org-map-tree (fun)
  4465. "Call FUN for every heading underneath the current one."
  4466. (org-back-to-heading)
  4467. (let ((level (funcall outline-level)))
  4468. (save-excursion
  4469. (funcall fun)
  4470. (while (and (progn
  4471. (outline-next-heading)
  4472. (> (funcall outline-level) level))
  4473. (not (eobp)))
  4474. (funcall fun)))))
  4475. (defun org-map-region (fun beg end)
  4476. "Call FUN for every heading between BEG and END."
  4477. (let ((org-ignore-region t))
  4478. (save-excursion
  4479. (setq end (copy-marker end))
  4480. (goto-char beg)
  4481. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4482. (< (point) end))
  4483. (funcall fun))
  4484. (while (and (progn
  4485. (outline-next-heading)
  4486. (< (point) end))
  4487. (not (eobp)))
  4488. (funcall fun)))))
  4489. (defun org-fixup-indentation (diff)
  4490. "Change the indentation in the current entry by DIFF
  4491. However, if any line in the current entry has no indentation, or if it
  4492. would end up with no indentation after the change, nothing at all is done."
  4493. (save-excursion
  4494. (let ((end (save-excursion (outline-next-heading)
  4495. (point-marker)))
  4496. (prohibit (if (> diff 0)
  4497. "^\\S-"
  4498. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4499. col)
  4500. (unless (save-excursion (re-search-forward prohibit end t))
  4501. (while (re-search-forward "^[ \t]+" end t)
  4502. (goto-char (match-end 0))
  4503. (setq col (current-column))
  4504. (if (< diff 0) (replace-match ""))
  4505. (indent-to (+ diff col))))
  4506. (move-marker end nil))))
  4507. (defun org-convert-to-odd-levels ()
  4508. "Convert an org-mode file with all levels allowed to one with odd levels.
  4509. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4510. level 5 etc."
  4511. (interactive)
  4512. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4513. (let ((org-odd-levels-only nil) n)
  4514. (save-excursion
  4515. (goto-char (point-min))
  4516. (while (re-search-forward "^\\*\\*+" nil t)
  4517. (setq n (1- (length (match-string 0))))
  4518. (while (>= (setq n (1- n)) 0)
  4519. (org-demote))
  4520. (end-of-line 1))))))
  4521. (defun org-convert-to-oddeven-levels ()
  4522. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4523. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4524. section with an even level, conversion would destroy the structure of the file. An error
  4525. is signaled in this case."
  4526. (interactive)
  4527. (goto-char (point-min))
  4528. ;; First check if there are no even levels
  4529. (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
  4530. (org-show-context t)
  4531. (error "Not all levels are odd in this file. Conversion not possible."))
  4532. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4533. (let ((org-odd-levels-only nil) n)
  4534. (save-excursion
  4535. (goto-char (point-min))
  4536. (while (re-search-forward "^\\*\\*+" nil t)
  4537. (setq n (/ (length (match-string 0)) 2))
  4538. (while (>= (setq n (1- n)) 0)
  4539. (org-promote))
  4540. (end-of-line 1))))))
  4541. (defun org-tr-level (n)
  4542. "Make N odd if required."
  4543. (if org-odd-levels-only (1+ (/ n 2)) n))
  4544. ;;; Vertical tree motion, cutting and pasting of subtrees
  4545. (defun org-move-subtree-up (&optional arg)
  4546. "Move the current subtree up past ARG headlines of the same level."
  4547. (interactive "p")
  4548. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4549. (defun org-move-subtree-down (&optional arg)
  4550. "Move the current subtree down past ARG headlines of the same level."
  4551. (interactive "p")
  4552. (setq arg (prefix-numeric-value arg))
  4553. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4554. 'outline-get-last-sibling))
  4555. (ins-point (make-marker))
  4556. (cnt (abs arg))
  4557. beg end txt folded)
  4558. ;; Select the tree
  4559. (org-back-to-heading)
  4560. (setq beg (point))
  4561. (save-match-data
  4562. (save-excursion (outline-end-of-heading)
  4563. (setq folded (org-invisible-p)))
  4564. (outline-end-of-subtree))
  4565. (outline-next-heading)
  4566. (setq end (point))
  4567. ;; Find insertion point, with error handling
  4568. (goto-char beg)
  4569. (while (> cnt 0)
  4570. (or (and (funcall movfunc) (looking-at outline-regexp))
  4571. (progn (goto-char beg)
  4572. (error "Cannot move past superior level or buffer limit")))
  4573. (setq cnt (1- cnt)))
  4574. (if (> arg 0)
  4575. ;; Moving forward - still need to move over subtree
  4576. (progn (outline-end-of-subtree)
  4577. (outline-next-heading)
  4578. (if (not (or (looking-at (concat "^" outline-regexp))
  4579. (bolp)))
  4580. (newline))))
  4581. (move-marker ins-point (point))
  4582. (setq txt (buffer-substring beg end))
  4583. (delete-region beg end)
  4584. (insert txt)
  4585. (or (bolp) (insert "\n"))
  4586. (goto-char ins-point)
  4587. (if folded (hide-subtree))
  4588. (move-marker ins-point nil)))
  4589. (defvar org-subtree-clip ""
  4590. "Clipboard for cut and paste of subtrees.
  4591. This is actually only a copy of the kill, because we use the normal kill
  4592. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4593. (defvar org-subtree-clip-folded nil
  4594. "Was the last copied subtree folded?
  4595. This is used to fold the tree back after pasting.")
  4596. (defun org-cut-subtree ()
  4597. "Cut the current subtree into the clipboard.
  4598. This is a short-hand for marking the subtree and then cutting it."
  4599. (interactive)
  4600. (org-copy-subtree 'cut))
  4601. (defun org-copy-subtree (&optional cut)
  4602. "Cut the current subtree into the clipboard.
  4603. This is a short-hand for marking the subtree and then copying it.
  4604. If CUT is non-nil, actually cut the subtree."
  4605. (interactive)
  4606. (let (beg end folded)
  4607. (if (interactive-p)
  4608. (org-back-to-heading nil) ; take what looks like a subtree
  4609. (org-back-to-heading t)) ; take what is really there
  4610. (setq beg (point))
  4611. (save-match-data
  4612. (save-excursion (outline-end-of-heading)
  4613. (setq folded (org-invisible-p)))
  4614. (outline-end-of-subtree))
  4615. (if (equal (char-after) ?\n) (forward-char 1))
  4616. (setq end (point))
  4617. (goto-char beg)
  4618. (when (> end beg)
  4619. (setq org-subtree-clip-folded folded)
  4620. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4621. (setq org-subtree-clip (current-kill 0))
  4622. (message "%s: Subtree with %d characters"
  4623. (if cut "Cut" "Copied")
  4624. (length org-subtree-clip)))))
  4625. (defun org-paste-subtree (&optional level tree)
  4626. "Paste the clipboard as a subtree, with modification of headline level.
  4627. The entire subtree is promoted or demoted in order to match a new headline
  4628. level. By default, the new level is derived from the visible headings
  4629. before and after the insertion point, and taken to be the inferior headline
  4630. level of the two. So if the previous visible heading is level 3 and the
  4631. next is level 4 (or vice versa), level 4 will be used for insertion.
  4632. This makes sure that the subtree remains an independent subtree and does
  4633. not swallow low level entries.
  4634. You can also force a different level, either by using a numeric prefix
  4635. argument, or by inserting the heading marker by hand. For example, if the
  4636. cursor is after \"*****\", then the tree will be shifted to level 5.
  4637. If you want to insert the tree as is, just use \\[yank].
  4638. If optional TREE is given, use this text instead of the kill ring."
  4639. (interactive "P")
  4640. (unless (org-kill-is-subtree-p tree)
  4641. (error
  4642. (substitute-command-keys
  4643. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4644. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4645. (^re (concat "^\\(" outline-regexp "\\)"))
  4646. (re (concat "\\(" outline-regexp "\\)"))
  4647. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  4648. (old-level (if (string-match ^re txt)
  4649. (- (match-end 0) (match-beginning 0))
  4650. -1))
  4651. (force-level (cond (level (prefix-numeric-value level))
  4652. ((string-match
  4653. ^re_ (buffer-substring (point-at-bol) (point)))
  4654. (- (match-end 0) (match-beginning 0)))
  4655. (t nil)))
  4656. (previous-level (save-excursion
  4657. (condition-case nil
  4658. (progn
  4659. (outline-previous-visible-heading 1)
  4660. (if (looking-at re)
  4661. (- (match-end 0) (match-beginning 0))
  4662. 1))
  4663. (error 1))))
  4664. (next-level (save-excursion
  4665. (condition-case nil
  4666. (progn
  4667. (outline-next-visible-heading 1)
  4668. (if (looking-at re)
  4669. (- (match-end 0) (match-beginning 0))
  4670. 1))
  4671. (error 1))))
  4672. (new-level (or force-level (max previous-level next-level)))
  4673. (shift (if (or (= old-level -1)
  4674. (= new-level -1)
  4675. (= old-level new-level))
  4676. 0
  4677. (- new-level old-level)))
  4678. (shift1 shift)
  4679. (delta (if (> shift 0) -1 1))
  4680. (func (if (> shift 0) 'org-demote 'org-promote))
  4681. (org-odd-levels-only nil)
  4682. beg end)
  4683. ;; Remove the forces level indicator
  4684. (if force-level
  4685. (delete-region (point-at-bol) (point)))
  4686. ;; Make sure we start at the beginning of an empty line
  4687. (if (not (bolp)) (insert "\n"))
  4688. (if (not (looking-at "[ \t]*$"))
  4689. (progn (insert "\n") (backward-char 1)))
  4690. ;; Paste
  4691. (setq beg (point))
  4692. (if (string-match "[ \t\r\n]+\\'" txt)
  4693. (setq txt (replace-match "\n" t t txt)))
  4694. (insert txt)
  4695. (setq end (point))
  4696. (if (looking-at "[ \t\r\n]+")
  4697. (replace-match "\n"))
  4698. (goto-char beg)
  4699. ;; Shift if necessary
  4700. (if (= shift 0)
  4701. (message "Pasted at level %d, without shift" new-level)
  4702. (save-restriction
  4703. (narrow-to-region beg end)
  4704. (while (not (= shift 0))
  4705. (org-map-region func (point-min) (point-max))
  4706. (setq shift (+ delta shift)))
  4707. (goto-char (point-min))
  4708. (message "Pasted at level %d, with shift by %d levels"
  4709. new-level shift1)))
  4710. (if (and kill-ring
  4711. (eq org-subtree-clip (current-kill 0))
  4712. org-subtree-clip-folded)
  4713. ;; The tree was folded before it was killed/copied
  4714. (hide-subtree))))
  4715. (defun org-kill-is-subtree-p (&optional txt)
  4716. "Check if the current kill is an outline subtree, or a set of trees.
  4717. Returns nil if kill does not start with a headline, or if the first
  4718. headline level is not the largest headline level in the tree.
  4719. So this will actually accept several entries of equal levels as well,
  4720. which is OK for `org-paste-subtree'.
  4721. If optional TXT is given, check this string instead of the current kill."
  4722. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4723. (start-level (and kill
  4724. (string-match (concat "\\`" outline-regexp) kill)
  4725. (- (match-end 0) (match-beginning 0))))
  4726. (re (concat "^" outline-regexp))
  4727. (start 1))
  4728. (if (not start-level)
  4729. nil ;; does not even start with a heading
  4730. (catch 'exit
  4731. (while (setq start (string-match re kill (1+ start)))
  4732. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  4733. (throw 'exit nil)))
  4734. t))))
  4735. (defun org-narrow-to-subtree ()
  4736. "Narrow buffer to the current subtree."
  4737. (interactive)
  4738. (save-excursion
  4739. (narrow-to-region
  4740. (progn (org-back-to-heading) (point))
  4741. (progn (org-end-of-subtree t t) (point)))))
  4742. ;;; Outline Sorting
  4743. (defun org-sort (with-case)
  4744. "Call `org-sort-entries' or `org-table-sort-lines', depending on context."
  4745. (interactive "P")
  4746. (if (org-at-table-p)
  4747. (org-call-with-arg 'org-table-sort-lines with-case)
  4748. (org-call-with-arg 'org-sort-entries with-case)))
  4749. (defun org-sort-entries (&optional with-case sorting-type)
  4750. "Sort entries on a certain level of an outline tree.
  4751. If there is an active region, the entries in the region are sorted.
  4752. Else, if the cursor is before the first entry, sort the top-level items.
  4753. Else, the children of the entry at point are sorted.
  4754. Sorting can be alphabetically, numerically, and by date/time as given by
  4755. the first time stamp in the entry. The command prompts for the sorting
  4756. type unless it has been given to the function through the SORTING-TYPE
  4757. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T).
  4758. Comparing entries ignores case by default. However, with an optional argument
  4759. WITH-CASE, the sorting considers case as well. With two prefix arguments
  4760. `C-u C-u', sorting is case-sensitive and duplicate entries will be removed."
  4761. (interactive "P")
  4762. (let ((unique (equal with-case '(16)))
  4763. start beg end entries stars re re2 p nentries (nremoved 0)
  4764. last txt what)
  4765. ;; Find beginning and end of region to sort
  4766. (cond
  4767. ((org-region-active-p)
  4768. ;; we will sort the region
  4769. (setq end (region-end)
  4770. what "region")
  4771. (goto-char (region-beginning))
  4772. (if (not (org-on-heading-p)) (outline-next-heading))
  4773. (setq start (point)))
  4774. ((or (org-on-heading-p)
  4775. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4776. ;; we will sort the children of the current headline
  4777. (org-back-to-heading)
  4778. (setq start (point) end (org-end-of-subtree) what "children")
  4779. (goto-char start)
  4780. (show-subtree)
  4781. (outline-next-heading))
  4782. (t
  4783. ;; we will sort the top-level entries in this file
  4784. (goto-char (point-min))
  4785. (or (org-on-heading-p) (outline-next-heading))
  4786. (setq start (point) end (point-max) what "top-level")
  4787. (goto-char start)
  4788. (show-all)))
  4789. (setq beg (point))
  4790. (if (>= (point) end) (error "Nothing to sort"))
  4791. (looking-at "\\(\\*+\\)")
  4792. (setq stars (match-string 1)
  4793. re (concat "^" (regexp-quote stars) " +")
  4794. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4795. txt (buffer-substring beg end))
  4796. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4797. (if (and (not (equal stars "*")) (string-match re2 txt))
  4798. (error "Region to sort contains a level above the first entry"))
  4799. ;; Make a list that can be sorted.
  4800. ;; The car is the string for comparison, the cdr is the subtree
  4801. (message "Sorting entries...")
  4802. (setq entries
  4803. (mapcar
  4804. (lambda (x)
  4805. (string-match "^.*\\(\n.*\\)?" x) ; take two lines
  4806. (cons (match-string 0 x) x))
  4807. (org-split-string txt re)))
  4808. ;; Sort the list
  4809. (save-excursion
  4810. (goto-char start)
  4811. (setq entries (org-do-sort entries what with-case sorting-type)))
  4812. ;; Delete the old stuff
  4813. (goto-char beg)
  4814. (kill-region beg end)
  4815. (setq nentries (length entries))
  4816. ;; Insert the sorted entries, and remove duplicates if this is required
  4817. (while (setq p (pop entries))
  4818. (if (and unique (equal last (setq last (org-trim (cdr p)))))
  4819. (setq nremoved (1+ nremoved)) ; same entry as before, skip it
  4820. (insert stars " " (cdr p))))
  4821. (goto-char start)
  4822. (message "Sorting entries...done (%d entries%s)"
  4823. nentries
  4824. (if unique (format ", %d duplicates removed" nremoved) ""))))
  4825. (defun org-do-sort (table what &optional with-case sorting-type)
  4826. "Sort TABLE of WHAT according to SORTING-TYPE.
  4827. The user will be prompted for the SORTING-TYPE if the call to this
  4828. function does not specify it. WHAT is only for the prompt, to indicate
  4829. what is being sorted. The sorting key will be extracted from
  4830. the car of the elements of the table.
  4831. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4832. (unless sorting-type
  4833. (message
  4834. "Sort %s: [a]lphabetically [n]umerically [t]ime. A/N/T means reversed:"
  4835. what)
  4836. (setq sorting-type (read-char-exclusive)))
  4837. (let ((dcst (downcase sorting-type))
  4838. extractfun comparefun)
  4839. ;; Define the appropriate functions
  4840. (cond
  4841. ((= dcst ?n)
  4842. (setq extractfun 'string-to-number
  4843. comparefun (if (= dcst sorting-type) '< '>)))
  4844. ((= dcst ?a)
  4845. (setq extractfun (if with-case 'identity 'downcase)
  4846. comparefun (if (= dcst sorting-type)
  4847. 'string<
  4848. (lambda (a b) (and (not (string< a b))
  4849. (not (string= a b)))))))
  4850. ((= dcst ?t)
  4851. (setq extractfun
  4852. (lambda (x)
  4853. (if (string-match org-ts-regexp x)
  4854. (time-to-seconds
  4855. (org-time-string-to-time (match-string 0 x)))
  4856. 0))
  4857. comparefun (if (= dcst sorting-type) '< '>)))
  4858. (t (error "Invalid sorting type `%c'" sorting-type)))
  4859. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4860. table)
  4861. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4862. ;;;; Plain list items, including checkboxes
  4863. ;;; Plain list items
  4864. (defun org-at-item-p ()
  4865. "Is point in a line starting a hand-formatted item?"
  4866. (let ((llt org-plain-list-ordered-item-terminator))
  4867. (save-excursion
  4868. (goto-char (point-at-bol))
  4869. (looking-at
  4870. (cond
  4871. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4872. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4873. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4874. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4875. (defun org-in-item-p ()
  4876. "It the cursor inside a plain list item.
  4877. Does not have to be the first line."
  4878. (save-excursion
  4879. (condition-case nil
  4880. (progn
  4881. (org-beginning-of-item)
  4882. (org-at-item-p)
  4883. t)
  4884. (error nil))))
  4885. (defun org-insert-item (&optional checkbox)
  4886. "Insert a new item at the current level.
  4887. Return t when things worked, nil when we are not in an item."
  4888. (when (save-excursion
  4889. (condition-case nil
  4890. (progn
  4891. (org-beginning-of-item)
  4892. (org-at-item-p)
  4893. (if (org-invisible-p) (error "Invisible item"))
  4894. t)
  4895. (error nil)))
  4896. (let* ((bul (match-string 0))
  4897. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4898. (match-end 0)))
  4899. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4900. pos)
  4901. (cond
  4902. ((and (org-at-item-p) (<= (point) eow))
  4903. ;; before the bullet
  4904. (beginning-of-line 1)
  4905. (open-line (if blank 2 1)))
  4906. ((<= (point) eow)
  4907. (beginning-of-line 1))
  4908. (t (newline (if blank 2 1))))
  4909. (insert bul (if checkbox "[ ]" ""))
  4910. (just-one-space)
  4911. (setq pos (point))
  4912. (end-of-line 1)
  4913. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4914. (org-maybe-renumber-ordered-list)
  4915. (and checkbox (org-update-checkbox-count-maybe))
  4916. t))
  4917. ;;; Checkboxes
  4918. (defun org-at-item-checkbox-p ()
  4919. "Is point at a line starting a plain-list item with a checklet?"
  4920. (and (org-at-item-p)
  4921. (save-excursion
  4922. (goto-char (match-end 0))
  4923. (skip-chars-forward " \t")
  4924. (looking-at "\\[[ X]\\]"))))
  4925. (defun org-toggle-checkbox (&optional arg)
  4926. "Toggle the checkbox in the current line."
  4927. (interactive "P")
  4928. (catch 'exit
  4929. (let (beg end status (firstnew 'unknown))
  4930. (cond
  4931. ((org-region-active-p)
  4932. (setq beg (region-beginning) end (region-end)))
  4933. ((org-on-heading-p)
  4934. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4935. ((org-at-item-checkbox-p)
  4936. (save-excursion
  4937. (replace-match (if (equal (match-string 0) "[ ]") "[X]" "[ ]") t t))
  4938. (throw 'exit t))
  4939. (t (error "Not at a checkbox or heading, and no active region")))
  4940. (save-excursion
  4941. (goto-char beg)
  4942. (while (< (point) end)
  4943. (when (org-at-item-checkbox-p)
  4944. (setq status (equal (match-string 0) "[X]"))
  4945. (when (eq firstnew 'unknown)
  4946. (setq firstnew (not status)))
  4947. (replace-match
  4948. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4949. (beginning-of-line 2)))))
  4950. (org-update-checkbox-count-maybe))
  4951. (defun org-update-checkbox-count-maybe ()
  4952. "Update checkbox statistics unless turned off by user."
  4953. (when org-provide-checkbox-statistics
  4954. (org-update-checkbox-count)))
  4955. (defun org-update-checkbox-count (&optional all)
  4956. "Update the checkbox statistics in the current section.
  4957. This will find all statistic cookies like [57%] and [6/12] and update them
  4958. with the current numbers. With optional prefix argument ALL, do this for
  4959. the whole buffer."
  4960. (interactive "P")
  4961. (save-excursion
  4962. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4963. (beg (progn (outline-back-to-heading) (point)))
  4964. (end (move-marker (make-marker)
  4965. (progn (outline-next-heading) (point))))
  4966. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  4967. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)")
  4968. b1 e1 f1 c-on c-off lim (cstat 0))
  4969. (when all
  4970. (goto-char (point-min))
  4971. (outline-next-heading)
  4972. (setq beg (point) end (point-max)))
  4973. (goto-char beg)
  4974. (while (re-search-forward re end t)
  4975. (setq cstat (1+ cstat)
  4976. b1 (match-beginning 0)
  4977. e1 (match-end 0)
  4978. f1 (match-beginning 1)
  4979. lim (cond
  4980. ((org-on-heading-p) (outline-next-heading) (point))
  4981. ((org-at-item-p) (org-end-of-item) (point))
  4982. (t nil))
  4983. c-on 0 c-off 0)
  4984. (goto-char e1)
  4985. (when lim
  4986. (while (re-search-forward re-box lim t)
  4987. (if (equal (match-string 2) "[ ]")
  4988. (setq c-off (1+ c-off))
  4989. (setq c-on (1+ c-on))))
  4990. (delete-region b1 e1)
  4991. (goto-char b1)
  4992. (insert (if f1
  4993. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4994. (format "[%d/%d]" c-on (+ c-on c-off))))))
  4995. (when (interactive-p)
  4996. (message "Checkbox satistics updated %s (%d places)"
  4997. (if all "in entire file" "in current outline entry") cstat)))))
  4998. (defun org-get-checkbox-statistics-face ()
  4999. "Select the face for checkbox statistics.
  5000. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5001. it will be `org-todo'."
  5002. (if (match-end 1)
  5003. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5004. (if (and (> (match-end 2) (match-beginning 2))
  5005. (equal (match-string 2) (match-string 3)))
  5006. 'org-done
  5007. 'org-todo)))
  5008. (defun org-get-indentation (&optional line)
  5009. "Get the indentation of the current line, interpreting tabs.
  5010. When LINE is given, assume it represents a line and compute its indentation."
  5011. (if line
  5012. (if (string-match "^ *" (org-remove-tabs line))
  5013. (match-end 0))
  5014. (save-excursion
  5015. (beginning-of-line 1)
  5016. (skip-chars-forward " \t")
  5017. (current-column))))
  5018. (defun org-remove-tabs (s &optional width)
  5019. "Replace tabulators in S with spaces.
  5020. Assumes that s is a single line, starting in column 0."
  5021. (setq width (or width tab-width))
  5022. (while (string-match "\t" s)
  5023. (setq s (replace-match
  5024. (make-string
  5025. (- (* width (/ (+ (match-beginning 0) width) width))
  5026. (match-beginning 0)) ?\ )
  5027. t t s)))
  5028. s)
  5029. (defun org-fix-indentation (line ind)
  5030. "Fix indentation in LINE.
  5031. IND is a cons cell with target and minimum indentation.
  5032. If the current indenation in LINE is smaller than the minimum,
  5033. leave it alone. If it is larger than ind, set it to the target."
  5034. (let* ((l (org-remove-tabs line))
  5035. (i (org-get-indentation l))
  5036. (i1 (car ind)) (i2 (cdr ind)))
  5037. (if (>= i i2) (setq l (substring line i2)))
  5038. (if (> i1 0)
  5039. (concat (make-string i1 ?\ ) l)
  5040. l)))
  5041. (defcustom org-empty-line-terminates-plain-lists nil
  5042. "Non-nil means, an empty line ends all plain list levels.
  5043. When nil, empty lines are part of the preceeding item."
  5044. :group 'org-plain-lists
  5045. :type 'boolean)
  5046. (defun org-beginning-of-item ()
  5047. "Go to the beginning of the current hand-formatted item.
  5048. If the cursor is not in an item, throw an error."
  5049. (interactive)
  5050. (let ((pos (point))
  5051. (limit (save-excursion
  5052. (condition-case nil
  5053. (progn
  5054. (org-back-to-heading)
  5055. (beginning-of-line 2) (point))
  5056. (error (point-min)))))
  5057. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5058. ind ind1)
  5059. (if (org-at-item-p)
  5060. (beginning-of-line 1)
  5061. (beginning-of-line 1)
  5062. (skip-chars-forward " \t")
  5063. (setq ind (current-column))
  5064. (if (catch 'exit
  5065. (while t
  5066. (beginning-of-line 0)
  5067. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5068. (if (looking-at "[ \t]*$")
  5069. (setq ind1 ind-empty)
  5070. (skip-chars-forward " \t")
  5071. (setq ind1 (current-column)))
  5072. (if (< ind1 ind)
  5073. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5074. nil
  5075. (goto-char pos)
  5076. (error "Not in an item")))))
  5077. (defun org-end-of-item ()
  5078. "Go to the end of the current hand-formatted item.
  5079. If the cursor is not in an item, throw an error."
  5080. (interactive)
  5081. (let* ((pos (point))
  5082. ind1
  5083. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5084. (limit (save-excursion (outline-next-heading) (point)))
  5085. (ind (save-excursion
  5086. (org-beginning-of-item)
  5087. (skip-chars-forward " \t")
  5088. (current-column)))
  5089. (end (catch 'exit
  5090. (while t
  5091. (beginning-of-line 2)
  5092. (if (eobp) (throw 'exit (point)))
  5093. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5094. (if (looking-at "[ \t]*$")
  5095. (setq ind1 ind-empty)
  5096. (skip-chars-forward " \t")
  5097. (setq ind1 (current-column)))
  5098. (if (<= ind1 ind)
  5099. (throw 'exit (point-at-bol)))))))
  5100. (if end
  5101. (goto-char end)
  5102. (goto-char pos)
  5103. (error "Not in an item"))))
  5104. (defun org-next-item ()
  5105. "Move to the beginning of the next item in the current plain list.
  5106. Error if not at a plain list, or if this is the last item in the list."
  5107. (interactive)
  5108. (let (ind ind1 (pos (point)))
  5109. (org-beginning-of-item)
  5110. (setq ind (org-get-indentation))
  5111. (org-end-of-item)
  5112. (setq ind1 (org-get-indentation))
  5113. (unless (and (org-at-item-p) (= ind ind1))
  5114. (goto-char pos)
  5115. (error "On last item"))))
  5116. (defun org-previous-item ()
  5117. "Move to the beginning of the previous item in the current plain list.
  5118. Error if not at a plain list, or if this is the first item in the list."
  5119. (interactive)
  5120. (let (beg ind ind1 (pos (point)))
  5121. (org-beginning-of-item)
  5122. (setq beg (point))
  5123. (setq ind (org-get-indentation))
  5124. (goto-char beg)
  5125. (catch 'exit
  5126. (while t
  5127. (beginning-of-line 0)
  5128. (if (looking-at "[ \t]*$")
  5129. nil
  5130. (if (<= (setq ind1 (org-get-indentation)) ind)
  5131. (throw 'exit t)))))
  5132. (condition-case nil
  5133. (if (or (not (org-at-item-p))
  5134. (< ind1 (1- ind)))
  5135. (error "")
  5136. (org-beginning-of-item))
  5137. (error (goto-char pos)
  5138. (error "On first item")))))
  5139. (defun org-move-item-down ()
  5140. "Move the plain list item at point down, i.e. swap with following item.
  5141. Subitems (items with larger indentation) are considered part of the item,
  5142. so this really moves item trees."
  5143. (interactive)
  5144. (let (beg end ind ind1 (pos (point)) txt)
  5145. (org-beginning-of-item)
  5146. (setq beg (point))
  5147. (setq ind (org-get-indentation))
  5148. (org-end-of-item)
  5149. (setq end (point))
  5150. (setq ind1 (org-get-indentation))
  5151. (if (and (org-at-item-p) (= ind ind1))
  5152. (progn
  5153. (org-end-of-item)
  5154. (setq txt (buffer-substring beg end))
  5155. (save-excursion
  5156. (delete-region beg end))
  5157. (setq pos (point))
  5158. (insert txt)
  5159. (goto-char pos)
  5160. (org-maybe-renumber-ordered-list))
  5161. (goto-char pos)
  5162. (error "Cannot move this item further down"))))
  5163. (defun org-move-item-up (arg)
  5164. "Move the plain list item at point up, i.e. swap with previous item.
  5165. Subitems (items with larger indentation) are considered part of the item,
  5166. so this really moves item trees."
  5167. (interactive "p")
  5168. (let (beg end ind ind1 (pos (point)) txt)
  5169. (org-beginning-of-item)
  5170. (setq beg (point))
  5171. (setq ind (org-get-indentation))
  5172. (org-end-of-item)
  5173. (setq end (point))
  5174. (goto-char beg)
  5175. (catch 'exit
  5176. (while t
  5177. (beginning-of-line 0)
  5178. (if (looking-at "[ \t]*$")
  5179. (if org-empty-line-terminates-plain-lists
  5180. (progn
  5181. (goto-char pos)
  5182. (error "Cannot move this item further up"))
  5183. nil)
  5184. (if (<= (setq ind1 (org-get-indentation)) ind)
  5185. (throw 'exit t)))))
  5186. (condition-case nil
  5187. (org-beginning-of-item)
  5188. (error (goto-char beg)
  5189. (error "Cannot move this item further up")))
  5190. (setq ind1 (org-get-indentation))
  5191. (if (and (org-at-item-p) (= ind ind1))
  5192. (progn
  5193. (setq txt (buffer-substring beg end))
  5194. (save-excursion
  5195. (delete-region beg end))
  5196. (setq pos (point))
  5197. (insert txt)
  5198. (goto-char pos)
  5199. (org-maybe-renumber-ordered-list))
  5200. (goto-char pos)
  5201. (error "Cannot move this item further up"))))
  5202. (defun org-maybe-renumber-ordered-list ()
  5203. "Renumber the ordered list at point if setup allows it.
  5204. This tests the user option `org-auto-renumber-ordered-lists' before
  5205. doing the renumbering."
  5206. (interactive)
  5207. (when (and org-auto-renumber-ordered-lists
  5208. (org-at-item-p))
  5209. (if (match-beginning 3)
  5210. (org-renumber-ordered-list 1)
  5211. (org-fix-bullet-type 1))))
  5212. (defun org-maybe-renumber-ordered-list-safe ()
  5213. (condition-case nil
  5214. (save-excursion
  5215. (org-maybe-renumber-ordered-list))
  5216. (error nil)))
  5217. (defun org-get-string-indentation (s)
  5218. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5219. (let ((n -1) (i 0) (w tab-width) c)
  5220. (catch 'exit
  5221. (while (< (setq n (1+ n)) (length s))
  5222. (setq c (aref s n))
  5223. (cond ((= c ?\ ) (setq i (1+ i)))
  5224. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5225. (t (throw 'exit t)))))
  5226. i))
  5227. (defun org-renumber-ordered-list (arg)
  5228. "Renumber an ordered plain list.
  5229. Cursor needs to be in the first line of an item, the line that starts
  5230. with something like \"1.\" or \"2)\"."
  5231. (interactive "p")
  5232. (unless (and (org-at-item-p)
  5233. (match-beginning 3))
  5234. (error "This is not an ordered list"))
  5235. (let ((line (org-current-line))
  5236. (col (current-column))
  5237. (ind (org-get-string-indentation
  5238. (buffer-substring (point-at-bol) (match-beginning 3))))
  5239. ;; (term (substring (match-string 3) -1))
  5240. ind1 (n (1- arg))
  5241. fmt)
  5242. ;; find where this list begins
  5243. (org-beginning-of-item-list)
  5244. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5245. (setq fmt (concat "%d" (match-string 1)))
  5246. (beginning-of-line 0)
  5247. ;; walk forward and replace these numbers
  5248. (catch 'exit
  5249. (while t
  5250. (catch 'next
  5251. (beginning-of-line 2)
  5252. (if (eobp) (throw 'exit nil))
  5253. (if (looking-at "[ \t]*$") (throw 'next nil))
  5254. (skip-chars-forward " \t") (setq ind1 (current-column))
  5255. (if (> ind1 ind) (throw 'next t))
  5256. (if (< ind1 ind) (throw 'exit t))
  5257. (if (not (org-at-item-p)) (throw 'exit nil))
  5258. (delete-region (match-beginning 2) (match-end 2))
  5259. (goto-char (match-beginning 2))
  5260. (insert (format fmt (setq n (1+ n)))))))
  5261. (goto-line line)
  5262. (move-to-column col)))
  5263. (defun org-fix-bullet-type (arg)
  5264. "Renumber an ordered plain list.
  5265. Cursor needs to be in the first line of an item, the line that starts
  5266. with something like \"1.\" or \"2)\"."
  5267. (interactive "p")
  5268. (unless (org-at-item-p) (error "This is not a list"))
  5269. (let ((line (org-current-line))
  5270. (col (current-column))
  5271. (ind (current-indentation))
  5272. ind1 (n (1- arg))
  5273. bullet)
  5274. ;; find where this list begins
  5275. (org-beginning-of-item-list)
  5276. (beginning-of-line 1)
  5277. ;; find out what the bullet type is
  5278. (looking-at "[ \t]*\\(\\S-+\\)")
  5279. (setq bullet (match-string 1))
  5280. ;; walk forward and replace these numbers
  5281. (beginning-of-line 0)
  5282. (catch 'exit
  5283. (while t
  5284. (catch 'next
  5285. (beginning-of-line 2)
  5286. (if (eobp) (throw 'exit nil))
  5287. (if (looking-at "[ \t]*$") (throw 'next nil))
  5288. (skip-chars-forward " \t") (setq ind1 (current-column))
  5289. (if (> ind1 ind) (throw 'next t))
  5290. (if (< ind1 ind) (throw 'exit t))
  5291. (if (not (org-at-item-p)) (throw 'exit nil))
  5292. (skip-chars-forward " \t")
  5293. (looking-at "\\S-+")
  5294. (replace-match bullet))))
  5295. (goto-line line)
  5296. (move-to-column col)
  5297. (if (string-match "[0-9]" bullet)
  5298. (org-renumber-ordered-list 1))))
  5299. (defun org-beginning-of-item-list ()
  5300. "Go to the beginning of the current item list.
  5301. I.e. to the first item in this list."
  5302. (interactive)
  5303. (org-beginning-of-item)
  5304. (let ((pos (point-at-bol))
  5305. (ind (org-get-indentation))
  5306. ind1)
  5307. ;; find where this list begins
  5308. (catch 'exit
  5309. (while t
  5310. (catch 'next
  5311. (beginning-of-line 0)
  5312. (if (looking-at "[ \t]*$") (throw 'next t))
  5313. (skip-chars-forward " \t") (setq ind1 (current-column))
  5314. (if (or (< ind1 ind)
  5315. (and (= ind1 ind)
  5316. (not (org-at-item-p))))
  5317. (throw 'exit t)
  5318. (setq pos (point-at-bol))))))
  5319. (goto-char pos)))
  5320. (defvar org-last-indent-begin-marker (make-marker))
  5321. (defvar org-last-indent-end-marker (make-marker))
  5322. (defun org-outdent-item (arg)
  5323. "Outdent a local list item."
  5324. (interactive "p")
  5325. (org-indent-item (- arg)))
  5326. (defun org-indent-item (arg)
  5327. "Indent a local list item."
  5328. (interactive "p")
  5329. (unless (org-at-item-p)
  5330. (error "Not on an item"))
  5331. (save-excursion
  5332. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5333. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5334. (setq beg org-last-indent-begin-marker
  5335. end org-last-indent-end-marker)
  5336. (org-beginning-of-item)
  5337. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5338. (org-end-of-item)
  5339. (setq end (move-marker org-last-indent-end-marker (point))))
  5340. (goto-char beg)
  5341. (setq tmp (org-item-indent-positions)
  5342. ind (car tmp)
  5343. ind-down (nth 2 tmp)
  5344. ind-up (nth 1 tmp)
  5345. delta (if (> arg 0)
  5346. (if ind-down (- ind-down ind) (+ 2 ind))
  5347. (if ind-up (- ind-up ind) (- ind 2))))
  5348. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5349. (while (< (point) end)
  5350. (beginning-of-line 1)
  5351. (skip-chars-forward " \t") (setq ind1 (current-column))
  5352. (delete-region (point-at-bol) (point))
  5353. (or (eolp) (indent-to-column (+ ind1 delta)))
  5354. (beginning-of-line 2))))
  5355. (org-maybe-renumber-ordered-list-safe)
  5356. (save-excursion
  5357. (beginning-of-line 0)
  5358. (condition-case nil (org-beginning-of-item) (error nil))
  5359. (org-maybe-renumber-ordered-list-safe)))
  5360. (defun org-item-indent-positions ()
  5361. "Assumes cursor in item line. FIXME"
  5362. (let* ((bolpos (point-at-bol))
  5363. (ind (org-get-indentation))
  5364. ind-down ind-up pos)
  5365. (save-excursion
  5366. (org-beginning-of-item-list)
  5367. (skip-chars-backward "\n\r \t")
  5368. (when (org-in-item-p)
  5369. (org-beginning-of-item)
  5370. (setq ind-up (org-get-indentation))))
  5371. (setq pos (point))
  5372. (save-excursion
  5373. (cond
  5374. ((and (condition-case nil (progn (org-previous-item) t)
  5375. (error nil))
  5376. (or (forward-char 1) t)
  5377. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5378. (setq ind-down (org-get-indentation)))
  5379. ((and (goto-char pos)
  5380. (org-at-item-p))
  5381. (goto-char (match-end 0))
  5382. (skip-chars-forward " \t")
  5383. (setq ind-down (current-column)))))
  5384. (list ind ind-up ind-down)))
  5385. ;;;; Archiving
  5386. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  5387. (defun org-archive-subtree (&optional find-done)
  5388. "Move the current subtree to the archive.
  5389. The archive can be a certain top-level heading in the current file, or in
  5390. a different file. The tree will be moved to that location, the subtree
  5391. heading be marked DONE, and the current time will be added.
  5392. When called with prefix argument FIND-DONE, find whole trees without any
  5393. open TODO items and archive them (after getting confirmation from the user).
  5394. If the cursor is not at a headline when this comand is called, try all level
  5395. 1 trees. If the cursor is on a headline, only try the direct children of
  5396. this heading."
  5397. (interactive "P")
  5398. (if find-done
  5399. (org-archive-all-done)
  5400. ;; Save all relevant TODO keyword-relatex variables
  5401. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  5402. (tr-org-todo-keywords-1 org-todo-keywords-1)
  5403. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  5404. (tr-org-done-keywords org-done-keywords)
  5405. (tr-org-todo-regexp org-todo-regexp)
  5406. (tr-org-todo-line-regexp org-todo-line-regexp)
  5407. (tr-org-odd-levels-only org-odd-levels-only)
  5408. (this-buffer (current-buffer))
  5409. (org-archive-location org-archive-location)
  5410. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  5411. file heading buffer level newfile-p)
  5412. ;; Try to find a local archive location
  5413. (save-excursion
  5414. (save-restriction
  5415. (widen)
  5416. (if (or (re-search-backward re nil t) (re-search-forward re nil t))
  5417. (setq org-archive-location (match-string 1)))))
  5418. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  5419. (progn
  5420. (setq file (format (match-string 1 org-archive-location)
  5421. (file-name-nondirectory buffer-file-name))
  5422. heading (match-string 2 org-archive-location)))
  5423. (error "Invalid `org-archive-location'"))
  5424. (if (> (length file) 0)
  5425. (setq newfile-p (not (file-exists-p file))
  5426. buffer (find-file-noselect file))
  5427. (setq buffer (current-buffer)))
  5428. (unless buffer
  5429. (error "Cannot access file \"%s\"" file))
  5430. (if (and (> (length heading) 0)
  5431. (string-match "^\\*+" heading))
  5432. (setq level (match-end 0))
  5433. (setq heading nil level 0))
  5434. (save-excursion
  5435. ;; We first only copy, in case something goes wrong
  5436. ;; we need to protect this-command, to avoid kill-region sets it,
  5437. ;; which would lead to duplication of subtrees
  5438. (let (this-command) (org-copy-subtree))
  5439. (set-buffer buffer)
  5440. ;; Enforce org-mode for the archive buffer
  5441. (if (not (org-mode-p))
  5442. ;; Force the mode for future visits.
  5443. (let ((org-insert-mode-line-in-empty-file t)
  5444. (org-inhibit-startup t))
  5445. (call-interactively 'org-mode)))
  5446. (when newfile-p
  5447. (goto-char (point-max))
  5448. (insert (format "\nArchived entries from file %s\n\n"
  5449. (buffer-file-name this-buffer))))
  5450. ;; Force the TODO keywords of the original buffer
  5451. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  5452. (org-todo-keywords-1 tr-org-todo-keywords-1)
  5453. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  5454. (org-done-keywords tr-org-done-keywords)
  5455. (org-todo-regexp tr-org-todo-regexp)
  5456. (org-todo-line-regexp tr-org-todo-line-regexp)
  5457. (org-odd-levels-only
  5458. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  5459. org-odd-levels-only
  5460. tr-org-odd-levels-only)))
  5461. (goto-char (point-min))
  5462. (if heading
  5463. (progn
  5464. (if (re-search-forward
  5465. (concat "\\(^\\|\r\\)"
  5466. (regexp-quote heading) "[ \t]*\\(:[a-zA-Z0-9_@:]+:\\)?[ \t]*\\($\\|\r\\)")
  5467. nil t)
  5468. (goto-char (match-end 0))
  5469. ;; Heading not found, just insert it at the end
  5470. (goto-char (point-max))
  5471. (or (bolp) (insert "\n"))
  5472. (insert "\n" heading "\n")
  5473. (end-of-line 0))
  5474. ;; Make the subtree visible
  5475. (show-subtree)
  5476. (org-end-of-subtree t)
  5477. (skip-chars-backward " \t\r\n")
  5478. (and (looking-at "[ \t\r\n]*")
  5479. (replace-match "\n\n")))
  5480. ;; No specific heading, just go to end of file.
  5481. (goto-char (point-max)) (insert "\n"))
  5482. ;; Paste
  5483. (org-paste-subtree (org-get-legal-level level 1))
  5484. ;; Mark the entry as done, i.e. set to last word in org-todo-keywords-1 FIXME: not right anymore!!!!!!!
  5485. (if org-archive-mark-done
  5486. (let (org-log-done)
  5487. (org-todo (length org-todo-keywords-1))))
  5488. ;; Move cursor to right after the TODO keyword
  5489. (when org-archive-stamp-time
  5490. (beginning-of-line 1)
  5491. (looking-at org-todo-line-regexp)
  5492. (goto-char (or (match-end 2) (match-beginning 3)))
  5493. (org-insert-time-stamp (org-current-time) t t "(" ")"))
  5494. ;; Save the buffer, if it is not the same buffer.
  5495. (if (not (eq this-buffer buffer)) (save-buffer))))
  5496. ;; Here we are back in the original buffer. Everything seems to have
  5497. ;; worked. So now cut the tree and finish up.
  5498. (let (this-command) (org-cut-subtree))
  5499. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  5500. (message "Subtree archived %s"
  5501. (if (eq this-buffer buffer)
  5502. (concat "under heading: " heading)
  5503. (concat "in file: " (abbreviate-file-name file)))))))
  5504. (defun org-archive-all-done (&optional tag)
  5505. "Archive sublevels of the current tree without open TODO items.
  5506. If the cursor is not on a headline, try all level 1 trees. If
  5507. it is on a headline, try all direct children.
  5508. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  5509. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  5510. (rea (concat ".*:" org-archive-tag ":"))
  5511. (begm (make-marker))
  5512. (endm (make-marker))
  5513. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  5514. "Move subtree to archive (no open TODO items)? "))
  5515. beg end (cntarch 0))
  5516. (if (org-on-heading-p)
  5517. (progn
  5518. (setq re1 (concat "^" (regexp-quote
  5519. (make-string
  5520. (1+ (- (match-end 0) (match-beginning 0)))
  5521. ?*))
  5522. " "))
  5523. (move-marker begm (point))
  5524. (move-marker endm (org-end-of-subtree t)))
  5525. (setq re1 "^* ")
  5526. (move-marker begm (point-min))
  5527. (move-marker endm (point-max)))
  5528. (save-excursion
  5529. (goto-char begm)
  5530. (while (re-search-forward re1 endm t)
  5531. (setq beg (match-beginning 0)
  5532. end (save-excursion (org-end-of-subtree t) (point)))
  5533. (goto-char beg)
  5534. (if (re-search-forward re end t)
  5535. (goto-char end)
  5536. (goto-char beg)
  5537. (if (and (or (not tag) (not (looking-at rea)))
  5538. (y-or-n-p question))
  5539. (progn
  5540. (if tag
  5541. (org-toggle-tag org-archive-tag 'on)
  5542. (org-archive-subtree))
  5543. (setq cntarch (1+ cntarch)))
  5544. (goto-char end)))))
  5545. (message "%d trees archived" cntarch)))
  5546. (defun org-cycle-hide-archived-subtrees (state)
  5547. "Re-hide all archived subtrees after a visibility state change."
  5548. (when (and (not org-cycle-open-archived-trees)
  5549. (not (memq state '(overview folded))))
  5550. (save-excursion
  5551. (let* ((globalp (memq state '(contents all)))
  5552. (beg (if globalp (point-min) (point)))
  5553. (end (if globalp (point-max) (org-end-of-subtree t))))
  5554. (org-hide-archived-subtrees beg end)
  5555. (goto-char beg)
  5556. (if (looking-at (concat ".*:" org-archive-tag ":"))
  5557. (message (substitute-command-keys
  5558. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  5559. (defun org-force-cycle-archived ()
  5560. "Cycle subtree even if it is archived."
  5561. (interactive)
  5562. (setq this-command 'org-cycle)
  5563. (let ((org-cycle-open-archived-trees t))
  5564. (call-interactively 'org-cycle)))
  5565. (defun org-hide-archived-subtrees (beg end)
  5566. "Re-hide all archived subtrees after a visibility state change."
  5567. (save-excursion
  5568. (let* ((re (concat ":" org-archive-tag ":")))
  5569. (goto-char beg)
  5570. (while (re-search-forward re end t)
  5571. (and (org-on-heading-p) (hide-subtree))
  5572. (org-end-of-subtree t)))))
  5573. (defun org-toggle-tag (tag &optional onoff)
  5574. "Toggle the tag TAG for the current line.
  5575. If ONOFF is `on' or `off', don't toggle but set to this state."
  5576. (unless (org-on-heading-p t) (error "Not on headling"))
  5577. (let (res current)
  5578. (save-excursion
  5579. (beginning-of-line)
  5580. (if (re-search-forward "[ \t]:\\([a-zA-Z0-9_@:]+\\):[ \t]*$"
  5581. (point-at-eol) t)
  5582. (progn
  5583. (setq current (match-string 1))
  5584. (replace-match ""))
  5585. (setq current ""))
  5586. (setq current (nreverse (org-split-string current ":")))
  5587. (cond
  5588. ((eq onoff 'on)
  5589. (setq res t)
  5590. (or (member tag current) (push tag current)))
  5591. ((eq onoff 'off)
  5592. (or (not (member tag current)) (setq current (delete tag current))))
  5593. (t (if (member tag current)
  5594. (setq current (delete tag current))
  5595. (setq res t)
  5596. (push tag current))))
  5597. (end-of-line 1)
  5598. (when current
  5599. (insert " :" (mapconcat 'identity (nreverse current) ":") ":"))
  5600. (org-set-tags nil t))
  5601. res))
  5602. (defun org-toggle-archive-tag (&optional arg)
  5603. "Toggle the archive tag for the current headline.
  5604. With prefix ARG, check all children of current headline and offer tagging
  5605. the children that do not contain any open TODO items."
  5606. (interactive "P")
  5607. (if arg
  5608. (org-archive-all-done 'tag)
  5609. (let (set)
  5610. (save-excursion
  5611. (org-back-to-heading t)
  5612. (setq set (org-toggle-tag org-archive-tag))
  5613. (when set (hide-subtree)))
  5614. (and set (beginning-of-line 1))
  5615. (message "Subtree %s" (if set "archived" "unarchived")))))
  5616. ;;;; Tables
  5617. ;;; The table editor
  5618. ;; Watch out: Here we are talking about two different kind of tables.
  5619. ;; Most of the code is for the tables created with the Org-mode table editor.
  5620. ;; Sometimes, we talk about tables created and edited with the table.el
  5621. ;; Emacs package. We call the former org-type tables, and the latter
  5622. ;; table.el-type tables.
  5623. (defun org-before-change-function (beg end)
  5624. "Every change indicates that a table might need an update."
  5625. (setq org-table-may-need-update t))
  5626. (defconst org-table-line-regexp "^[ \t]*|"
  5627. "Detects an org-type table line.")
  5628. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  5629. "Detects an org-type table line.")
  5630. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  5631. "Detects a table line marked for automatic recalculation.")
  5632. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  5633. "Detects a table line marked for automatic recalculation.")
  5634. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  5635. "Detects a table line marked for automatic recalculation.")
  5636. (defconst org-table-hline-regexp "^[ \t]*|-"
  5637. "Detects an org-type table hline.")
  5638. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  5639. "Detects a table-type table hline.")
  5640. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  5641. "Detects an org-type or table-type table.")
  5642. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  5643. "Searching from within a table (any type) this finds the first line
  5644. outside the table.")
  5645. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  5646. "Searching from within a table (any type) this finds the first line
  5647. outside the table.")
  5648. (defvar org-table-last-highlighted-reference nil)
  5649. (defvar org-table-formula-history nil)
  5650. (defvar org-table-column-names nil
  5651. "Alist with column names, derived from the `!' line.")
  5652. (defvar org-table-column-name-regexp nil
  5653. "Regular expression matching the current column names.")
  5654. (defvar org-table-local-parameters nil
  5655. "Alist with parameter names, derived from the `$' line.")
  5656. (defvar org-table-named-field-locations nil
  5657. "Alist with locations of named fields.")
  5658. (defvar org-table-current-line-types nil
  5659. "Table row types, non-nil only for the duration of a comand.")
  5660. (defvar org-table-current-begin-line nil
  5661. "Table begin line, non-nil only for the duration of a comand.")
  5662. (defvar org-table-current-begin-pos nil
  5663. "Table begin position, non-nil only for the duration of a comand.")
  5664. (defvar org-table-dlines nil
  5665. "Vector of data line line numbers in the current table.")
  5666. (defvar org-table-hlines nil
  5667. "Vector of hline line numbers in the current table.")
  5668. (defconst org-table-range-regexp
  5669. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  5670. ;; 1 2 3 4 5
  5671. "Regular expression for matching ranges in formulas.")
  5672. (defconst org-table-range-regexp2
  5673. (concat
  5674. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  5675. "\\.\\."
  5676. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  5677. "Match a range for reference display.")
  5678. (defconst org-table-translate-regexp
  5679. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  5680. "Match a reference that needs translation, for reference display.")
  5681. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  5682. (defun org-table-create-with-table.el ()
  5683. "Use the table.el package to insert a new table.
  5684. If there is already a table at point, convert between Org-mode tables
  5685. and table.el tables."
  5686. (interactive)
  5687. (require 'table)
  5688. (cond
  5689. ((org-at-table.el-p)
  5690. (if (y-or-n-p "Convert table to Org-mode table? ")
  5691. (org-table-convert)))
  5692. ((org-at-table-p)
  5693. (if (y-or-n-p "Convert table to table.el table? ")
  5694. (org-table-convert)))
  5695. (t (call-interactively 'table-insert))))
  5696. (defun org-table-create-or-convert-from-region (arg)
  5697. "Convert region to table, or create an empty table.
  5698. If there is an active region, convert it to a table, using the function
  5699. `org-table-convert-region'.
  5700. If there is no such region, create an empty table with `org-table-create'."
  5701. (interactive "P")
  5702. (if (org-region-active-p)
  5703. (org-table-convert-region (region-beginning) (region-end) arg)
  5704. (org-table-create arg)))
  5705. (defun org-table-create (&optional size)
  5706. "Query for a size and insert a table skeleton.
  5707. SIZE is a string Columns x Rows like for example \"3x2\"."
  5708. (interactive "P")
  5709. (unless size
  5710. (setq size (read-string
  5711. (concat "Table size Columns x Rows [e.g. "
  5712. org-table-default-size "]: ")
  5713. "" nil org-table-default-size)))
  5714. (let* ((pos (point))
  5715. (indent (make-string (current-column) ?\ ))
  5716. (split (org-split-string size " *x *"))
  5717. (rows (string-to-number (nth 1 split)))
  5718. (columns (string-to-number (car split)))
  5719. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  5720. "\n")))
  5721. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  5722. (point-at-bol) (point)))
  5723. (beginning-of-line 1)
  5724. (newline))
  5725. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  5726. (dotimes (i rows) (insert line))
  5727. (goto-char pos)
  5728. (if (> rows 1)
  5729. ;; Insert a hline after the first row.
  5730. (progn
  5731. (end-of-line 1)
  5732. (insert "\n|-")
  5733. (goto-char pos)))
  5734. (org-table-align)))
  5735. (defun org-table-convert-region (beg0 end0 &optional nspace)
  5736. "Convert region to a table.
  5737. The region goes from BEG0 to END0, but these borders will be moved
  5738. slightly, to make sure a beginning of line in the first line is included.
  5739. When NSPACE is non-nil, it indicates the minimum number of spaces that
  5740. separate columns. By default, the function first checks if every line
  5741. contains at lease one TAB. If yes, it assumes that the material is TAB
  5742. separated. If not, it assumes a single space as separator."
  5743. (interactive "rP")
  5744. (let* ((beg (min beg0 end0))
  5745. (end (max beg0 end0))
  5746. (tabsep t)
  5747. re)
  5748. (goto-char beg)
  5749. (beginning-of-line 1)
  5750. (setq beg (move-marker (make-marker) (point)))
  5751. (goto-char end)
  5752. (if (bolp) (backward-char 1) (end-of-line 1))
  5753. (setq end (move-marker (make-marker) (point)))
  5754. ;; Lets see if this is tab-separated material. If every nonempty line
  5755. ;; contains a tab, we will assume that it is tab-separated material
  5756. (if nspace
  5757. (setq tabsep nil)
  5758. (goto-char beg)
  5759. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  5760. (if nspace (setq tabsep nil))
  5761. (if tabsep
  5762. (setq re "^\\|\t")
  5763. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  5764. (max 1 (prefix-numeric-value nspace)))))
  5765. (goto-char beg)
  5766. (while (re-search-forward re end t)
  5767. (replace-match "| " t t))
  5768. (goto-char beg)
  5769. (insert " ")
  5770. (org-table-align)))
  5771. (defun org-table-import (file arg)
  5772. "Import FILE as a table.
  5773. The file is assumed to be tab-separated. Such files can be produced by most
  5774. spreadsheet and database applications. If no tabs (at least one per line)
  5775. are found, lines will be split on whitespace into fields."
  5776. (interactive "f\nP")
  5777. (or (bolp) (newline))
  5778. (let ((beg (point))
  5779. (pm (point-max)))
  5780. (insert-file-contents file)
  5781. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  5782. (defun org-table-export ()
  5783. "Export table as a tab-separated file.
  5784. Such a file can be imported into a spreadsheet program like Excel."
  5785. (interactive)
  5786. (let* ((beg (org-table-begin))
  5787. (end (org-table-end))
  5788. (table (buffer-substring beg end))
  5789. (file (read-file-name "Export table to: "))
  5790. buf)
  5791. (unless (or (not (file-exists-p file))
  5792. (y-or-n-p (format "Overwrite file %s? " file)))
  5793. (error "Abort"))
  5794. (with-current-buffer (find-file-noselect file)
  5795. (setq buf (current-buffer))
  5796. (erase-buffer)
  5797. (fundamental-mode)
  5798. (insert table)
  5799. (goto-char (point-min))
  5800. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  5801. (replace-match "" t t)
  5802. (end-of-line 1))
  5803. (goto-char (point-min))
  5804. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  5805. (replace-match "" t t)
  5806. (goto-char (min (1+ (point)) (point-max))))
  5807. (goto-char (point-min))
  5808. (while (re-search-forward "^-[-+]*$" nil t)
  5809. (replace-match "")
  5810. (if (looking-at "\n")
  5811. (delete-char 1)))
  5812. (goto-char (point-min))
  5813. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  5814. (replace-match "\t" t t))
  5815. (save-buffer))
  5816. (kill-buffer buf)))
  5817. (defvar org-table-aligned-begin-marker (make-marker)
  5818. "Marker at the beginning of the table last aligned.
  5819. Used to check if cursor still is in that table, to minimize realignment.")
  5820. (defvar org-table-aligned-end-marker (make-marker)
  5821. "Marker at the end of the table last aligned.
  5822. Used to check if cursor still is in that table, to minimize realignment.")
  5823. (defvar org-table-last-alignment nil
  5824. "List of flags for flushright alignment, from the last re-alignment.
  5825. This is being used to correctly align a single field after TAB or RET.")
  5826. (defvar org-table-last-column-widths nil
  5827. "List of max width of fields in each column.
  5828. This is being used to correctly align a single field after TAB or RET.")
  5829. (defvar org-table-overlay-coordinates nil
  5830. "Overlay coordinates after each align of a table.")
  5831. (make-variable-buffer-local 'org-table-overlay-coordinates)
  5832. (defvar org-last-recalc-line nil)
  5833. (defconst org-narrow-column-arrow "=>"
  5834. "Used as display property in narrowed table columns.")
  5835. (defun org-table-align ()
  5836. "Align the table at point by aligning all vertical bars."
  5837. (interactive)
  5838. (let* (
  5839. ;; Limits of table
  5840. (beg (org-table-begin))
  5841. (end (org-table-end))
  5842. ;; Current cursor position
  5843. (linepos (org-current-line))
  5844. (colpos (org-table-current-column))
  5845. (winstart (window-start))
  5846. (winstartline (org-current-line (min winstart (1- (point-max)))))
  5847. lines (new "") lengths l typenums ty fields maxfields i
  5848. column
  5849. (indent "") cnt frac
  5850. rfmt hfmt
  5851. (spaces '(1 . 1))
  5852. (sp1 (car spaces))
  5853. (sp2 (cdr spaces))
  5854. (rfmt1 (concat
  5855. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  5856. (hfmt1 (concat
  5857. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  5858. emptystrings links dates narrow fmax f1 len c e)
  5859. (untabify beg end)
  5860. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  5861. ;; Check if we have links or dates
  5862. (goto-char beg)
  5863. (setq links (re-search-forward org-bracket-link-regexp end t))
  5864. (goto-char beg)
  5865. (setq dates (and org-display-custom-times
  5866. (re-search-forward org-ts-regexp-both end t)))
  5867. ;; Make sure the link properties are right
  5868. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  5869. ;; Make sure the date properties are right
  5870. (when dates (goto-char beg) (while (org-activate-dates end)))
  5871. ;; Check if we are narrowing any columns
  5872. (goto-char beg)
  5873. (setq narrow (and org-format-transports-properties-p
  5874. (re-search-forward "<[0-9]+>" end t)))
  5875. ;; Get the rows
  5876. (setq lines (org-split-string
  5877. (buffer-substring beg end) "\n"))
  5878. ;; Store the indentation of the first line
  5879. (if (string-match "^ *" (car lines))
  5880. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  5881. ;; Mark the hlines by setting the corresponding element to nil
  5882. ;; At the same time, we remove trailing space.
  5883. (setq lines (mapcar (lambda (l)
  5884. (if (string-match "^ *|-" l)
  5885. nil
  5886. (if (string-match "[ \t]+$" l)
  5887. (substring l 0 (match-beginning 0))
  5888. l)))
  5889. lines))
  5890. ;; Get the data fields by splitting the lines.
  5891. (setq fields (mapcar
  5892. (lambda (l)
  5893. (org-split-string l " *| *"))
  5894. (delq nil (copy-sequence lines))))
  5895. ;; How many fields in the longest line?
  5896. (condition-case nil
  5897. (setq maxfields (apply 'max (mapcar 'length fields)))
  5898. (error
  5899. (kill-region beg end)
  5900. (org-table-create org-table-default-size)
  5901. (error "Empty table - created default table")))
  5902. ;; A list of empty strings to fill any short rows on output
  5903. (setq emptystrings (make-list maxfields ""))
  5904. ;; Check for special formatting.
  5905. (setq i -1)
  5906. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  5907. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  5908. ;; Check if there is an explicit width specified
  5909. (when narrow
  5910. (setq c column fmax nil)
  5911. (while c
  5912. (setq e (pop c))
  5913. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  5914. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  5915. ;; Find fields that are wider than fmax, and shorten them
  5916. (when fmax
  5917. (loop for xx in column do
  5918. (when (and (stringp xx)
  5919. (> (org-string-width xx) fmax))
  5920. (org-add-props xx nil
  5921. 'help-echo
  5922. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  5923. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  5924. (unless (> f1 1)
  5925. (error "Cannot narrow field starting with wide link \"%s\""
  5926. (match-string 0 xx)))
  5927. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  5928. (add-text-properties (- f1 2) f1
  5929. (list 'display org-narrow-column-arrow)
  5930. xx)))))
  5931. ;; Get the maximum width for each column
  5932. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  5933. ;; Get the fraction of numbers, to decide about alignment of the column
  5934. (setq cnt 0 frac 0.0)
  5935. (loop for x in column do
  5936. (if (equal x "")
  5937. nil
  5938. (setq frac ( / (+ (* frac cnt)
  5939. (if (string-match org-table-number-regexp x) 1 0))
  5940. (setq cnt (1+ cnt))))))
  5941. (push (>= frac org-table-number-fraction) typenums))
  5942. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  5943. ;; Store the alignment of this table, for later editing of single fields
  5944. (setq org-table-last-alignment typenums
  5945. org-table-last-column-widths lengths)
  5946. ;; With invisible characters, `format' does not get the field width right
  5947. ;; So we need to make these fields wide by hand.
  5948. (when links
  5949. (loop for i from 0 upto (1- maxfields) do
  5950. (setq len (nth i lengths))
  5951. (loop for j from 0 upto (1- (length fields)) do
  5952. (setq c (nthcdr i (car (nthcdr j fields))))
  5953. (if (and (stringp (car c))
  5954. (string-match org-bracket-link-regexp (car c))
  5955. (< (org-string-width (car c)) len))
  5956. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  5957. ;; Compute the formats needed for output of the table
  5958. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  5959. (while (setq l (pop lengths))
  5960. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  5961. (setq rfmt (concat rfmt (format rfmt1 ty l))
  5962. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  5963. (setq rfmt (concat rfmt "\n")
  5964. hfmt (concat (substring hfmt 0 -1) "|\n"))
  5965. (setq new (mapconcat
  5966. (lambda (l)
  5967. (if l (apply 'format rfmt
  5968. (append (pop fields) emptystrings))
  5969. hfmt))
  5970. lines ""))
  5971. ;; Replace the old one
  5972. (delete-region beg end)
  5973. (move-marker end nil)
  5974. (move-marker org-table-aligned-begin-marker (point))
  5975. (insert new)
  5976. (move-marker org-table-aligned-end-marker (point))
  5977. (when (and orgtbl-mode (not (org-mode-p)))
  5978. (goto-char org-table-aligned-begin-marker)
  5979. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  5980. ;; Try to move to the old location
  5981. (goto-line winstartline)
  5982. (setq winstart (point-at-bol))
  5983. (goto-line linepos)
  5984. (set-window-start (selected-window) winstart 'noforce)
  5985. (org-table-goto-column colpos)
  5986. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  5987. (setq org-table-may-need-update nil)
  5988. ))
  5989. (defun org-string-width (s)
  5990. "Compute width of string, ignoring invisible characters.
  5991. This ignores character with invisibility property `org-link', and also
  5992. characters with property `org-cwidth', because these will become invisible
  5993. upon the next fontification round."
  5994. (let (b l)
  5995. (when (or (eq t buffer-invisibility-spec)
  5996. (assq 'org-link buffer-invisibility-spec))
  5997. (while (setq b (text-property-any 0 (length s)
  5998. 'invisible 'org-link s))
  5999. (setq s (concat (substring s 0 b)
  6000. (substring s (or (next-single-property-change
  6001. b 'invisible s) (length s)))))))
  6002. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  6003. (setq s (concat (substring s 0 b)
  6004. (substring s (or (next-single-property-change
  6005. b 'org-cwidth s) (length s))))))
  6006. (setq l (string-width s) b -1)
  6007. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  6008. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  6009. l))
  6010. (defun org-table-begin (&optional table-type)
  6011. "Find the beginning of the table and return its position.
  6012. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  6013. (save-excursion
  6014. (if (not (re-search-backward
  6015. (if table-type org-table-any-border-regexp
  6016. org-table-border-regexp)
  6017. nil t))
  6018. (progn (goto-char (point-min)) (point))
  6019. (goto-char (match-beginning 0))
  6020. (beginning-of-line 2)
  6021. (point))))
  6022. (defun org-table-end (&optional table-type)
  6023. "Find the end of the table and return its position.
  6024. With argument TABLE-TYPE, go to the end of a table.el-type table."
  6025. (save-excursion
  6026. (if (not (re-search-forward
  6027. (if table-type org-table-any-border-regexp
  6028. org-table-border-regexp)
  6029. nil t))
  6030. (goto-char (point-max))
  6031. (goto-char (match-beginning 0)))
  6032. (point-marker)))
  6033. (defun org-table-justify-field-maybe (&optional new)
  6034. "Justify the current field, text to left, number to right.
  6035. Optional argument NEW may specify text to replace the current field content."
  6036. (cond
  6037. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  6038. ((org-at-table-hline-p))
  6039. ((and (not new)
  6040. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  6041. (current-buffer)))
  6042. (< (point) org-table-aligned-begin-marker)
  6043. (>= (point) org-table-aligned-end-marker)))
  6044. ;; This is not the same table, force a full re-align
  6045. (setq org-table-may-need-update t))
  6046. (t ;; realign the current field, based on previous full realign
  6047. (let* ((pos (point)) s
  6048. (col (org-table-current-column))
  6049. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  6050. l f n o e)
  6051. (when (> col 0)
  6052. (skip-chars-backward "^|\n")
  6053. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  6054. (progn
  6055. (setq s (match-string 1)
  6056. o (match-string 0)
  6057. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  6058. e (not (= (match-beginning 2) (match-end 2))))
  6059. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  6060. l (if e "|" (setq org-table-may-need-update t) ""))
  6061. n (format f s))
  6062. (if new
  6063. (if (<= (length new) l) ;; FIXME: length -> str-width?
  6064. (setq n (format f new))
  6065. (setq n (concat new "|") org-table-may-need-update t)))
  6066. (or (equal n o)
  6067. (let (org-table-may-need-update)
  6068. (replace-match n))))
  6069. (setq org-table-may-need-update t))
  6070. (goto-char pos))))))
  6071. (defun org-table-next-field ()
  6072. "Go to the next field in the current table, creating new lines as needed.
  6073. Before doing so, re-align the table if necessary."
  6074. (interactive)
  6075. (org-table-maybe-eval-formula)
  6076. (org-table-maybe-recalculate-line)
  6077. (if (and org-table-automatic-realign
  6078. org-table-may-need-update)
  6079. (org-table-align))
  6080. (let ((end (org-table-end)))
  6081. (if (org-at-table-hline-p)
  6082. (end-of-line 1))
  6083. (condition-case nil
  6084. (progn
  6085. (re-search-forward "|" end)
  6086. (if (looking-at "[ \t]*$")
  6087. (re-search-forward "|" end))
  6088. (if (and (looking-at "-")
  6089. org-table-tab-jumps-over-hlines
  6090. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  6091. (goto-char (match-beginning 1)))
  6092. (if (looking-at "-")
  6093. (progn
  6094. (beginning-of-line 0)
  6095. (org-table-insert-row 'below))
  6096. (if (looking-at " ") (forward-char 1))))
  6097. (error
  6098. (org-table-insert-row 'below)))))
  6099. (defun org-table-previous-field ()
  6100. "Go to the previous field in the table.
  6101. Before doing so, re-align the table if necessary."
  6102. (interactive)
  6103. (org-table-justify-field-maybe)
  6104. (org-table-maybe-recalculate-line)
  6105. (if (and org-table-automatic-realign
  6106. org-table-may-need-update)
  6107. (org-table-align))
  6108. (if (org-at-table-hline-p)
  6109. (end-of-line 1))
  6110. (re-search-backward "|" (org-table-begin))
  6111. (re-search-backward "|" (org-table-begin))
  6112. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  6113. (re-search-backward "|" (org-table-begin)))
  6114. (if (looking-at "| ?")
  6115. (goto-char (match-end 0))))
  6116. (defun org-table-next-row ()
  6117. "Go to the next row (same column) in the current table.
  6118. Before doing so, re-align the table if necessary."
  6119. (interactive)
  6120. (org-table-maybe-eval-formula)
  6121. (org-table-maybe-recalculate-line)
  6122. (if (or (looking-at "[ \t]*$")
  6123. (save-excursion (skip-chars-backward " \t") (bolp)))
  6124. (newline)
  6125. (if (and org-table-automatic-realign
  6126. org-table-may-need-update)
  6127. (org-table-align))
  6128. (let ((col (org-table-current-column)))
  6129. (beginning-of-line 2)
  6130. (if (or (not (org-at-table-p))
  6131. (org-at-table-hline-p))
  6132. (progn
  6133. (beginning-of-line 0)
  6134. (org-table-insert-row 'below)))
  6135. (org-table-goto-column col)
  6136. (skip-chars-backward "^|\n\r")
  6137. (if (looking-at " ") (forward-char 1)))))
  6138. (defun org-table-copy-down (n)
  6139. "Copy a field down in the current column.
  6140. If the field at the cursor is empty, copy into it the content of the nearest
  6141. non-empty field above. With argument N, use the Nth non-empty field.
  6142. If the current field is not empty, it is copied down to the next row, and
  6143. the cursor is moved with it. Therefore, repeating this command causes the
  6144. column to be filled row-by-row.
  6145. If the variable `org-table-copy-increment' is non-nil and the field is an
  6146. integer or a timestamp, it will be incremented while copying. In the case of
  6147. a timestamp, if the cursor is on the year, change the year. If it is on the
  6148. month or the day, change that. Point will stay on the current date field
  6149. in order to easily repeat the interval."
  6150. (interactive "p")
  6151. (let* ((colpos (org-table-current-column))
  6152. (col (current-column))
  6153. (field (org-table-get-field))
  6154. (non-empty (string-match "[^ \t]" field))
  6155. (beg (org-table-begin))
  6156. txt)
  6157. (org-table-check-inside-data-field)
  6158. (if non-empty
  6159. (progn
  6160. (setq txt (org-trim field))
  6161. (org-table-next-row)
  6162. (org-table-blank-field))
  6163. (save-excursion
  6164. (setq txt
  6165. (catch 'exit
  6166. (while (progn (beginning-of-line 1)
  6167. (re-search-backward org-table-dataline-regexp
  6168. beg t))
  6169. (org-table-goto-column colpos t)
  6170. (if (and (looking-at
  6171. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  6172. (= (setq n (1- n)) 0))
  6173. (throw 'exit (match-string 1))))))))
  6174. (if txt
  6175. (progn
  6176. (if (and org-table-copy-increment
  6177. (string-match "^[0-9]+$" txt))
  6178. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  6179. (insert txt)
  6180. (move-to-column col)
  6181. (if (and org-table-copy-increment (org-at-timestamp-p t))
  6182. (org-timestamp-up 1)
  6183. (org-table-maybe-recalculate-line))
  6184. (org-table-align)
  6185. (move-to-column col))
  6186. (error "No non-empty field found"))))
  6187. (defun org-table-check-inside-data-field ()
  6188. "Is point inside a table data field?
  6189. I.e. not on a hline or before the first or after the last column?
  6190. This actually throws an error, so it aborts the current command."
  6191. (if (or (not (org-at-table-p))
  6192. (= (org-table-current-column) 0)
  6193. (org-at-table-hline-p)
  6194. (looking-at "[ \t]*$"))
  6195. (error "Not in table data field")))
  6196. (defvar org-table-clip nil
  6197. "Clipboard for table regions.")
  6198. (defun org-table-blank-field ()
  6199. "Blank the current table field or active region."
  6200. (interactive)
  6201. (org-table-check-inside-data-field)
  6202. (if (and (interactive-p) (org-region-active-p))
  6203. (let (org-table-clip)
  6204. (org-table-cut-region (region-beginning) (region-end)))
  6205. (skip-chars-backward "^|")
  6206. (backward-char 1)
  6207. (if (looking-at "|[^|\n]+")
  6208. (let* ((pos (match-beginning 0))
  6209. (match (match-string 0))
  6210. (len (org-string-width match)))
  6211. (replace-match (concat "|" (make-string (1- len) ?\ )))
  6212. (goto-char (+ 2 pos))
  6213. (substring match 1)))))
  6214. (defun org-table-get-field (&optional n replace)
  6215. "Return the value of the field in column N of current row.
  6216. N defaults to current field.
  6217. If REPLACE is a string, replace field with this value. The return value
  6218. is always the old value."
  6219. (and n (org-table-goto-column n))
  6220. (skip-chars-backward "^|\n")
  6221. (backward-char 1)
  6222. (if (looking-at "|[^|\r\n]*")
  6223. (let* ((pos (match-beginning 0))
  6224. (val (buffer-substring (1+ pos) (match-end 0))))
  6225. (if replace
  6226. (replace-match (concat "|" replace) t t))
  6227. (goto-char (min (point-at-eol) (+ 2 pos)))
  6228. val)
  6229. (forward-char 1) ""))
  6230. (defun org-table-field-info (arg)
  6231. "Show info about the current field, and highlight any reference at point."
  6232. (interactive "P")
  6233. (org-table-get-specials)
  6234. (save-excursion
  6235. (let* ((pos (point))
  6236. (col (org-table-current-column))
  6237. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  6238. (name (car (rassoc (list (org-current-line) col)
  6239. org-table-named-field-locations)))
  6240. (eql (org-table-get-stored-formulas))
  6241. (dline (org-table-current-dline))
  6242. (ref (format "@%d$%d" dline col))
  6243. (ref1 (org-table-convert-refs-to-an ref))
  6244. (fequation (or (assoc name eql) (assoc ref eql)))
  6245. (cequation (assoc (int-to-string col) eql))
  6246. (eqn (or fequation cequation)))
  6247. (goto-char pos)
  6248. (condition-case nil
  6249. (org-table-show-reference 'local)
  6250. (error nil))
  6251. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  6252. dline col
  6253. (if cname (concat " or $" cname) "")
  6254. dline col ref1
  6255. (if name (concat " or $" name) "")
  6256. ;; FIXME: formula info not correct if special table line
  6257. (if eqn
  6258. (concat ", formula: "
  6259. (org-table-formula-to-user
  6260. (concat
  6261. (if (string-match "^[$@]"(car eqn)) "" "$")
  6262. (car eqn) "=" (cdr eqn))))
  6263. "")))))
  6264. (defun org-table-current-column ()
  6265. "Find out which column we are in.
  6266. When called interactively, column is also displayed in echo area."
  6267. (interactive)
  6268. (if (interactive-p) (org-table-check-inside-data-field))
  6269. (save-excursion
  6270. (let ((cnt 0) (pos (point)))
  6271. (beginning-of-line 1)
  6272. (while (search-forward "|" pos t)
  6273. (setq cnt (1+ cnt)))
  6274. (if (interactive-p) (message "This is table column %d" cnt))
  6275. cnt)))
  6276. (defun org-table-current-dline ()
  6277. "Find out what table data line we are in.
  6278. Only datalins count for this."
  6279. (interactive)
  6280. (if (interactive-p) (org-table-check-inside-data-field))
  6281. (save-excursion
  6282. (let ((cnt 0) (pos (point)))
  6283. (goto-char (org-table-begin))
  6284. (while (<= (point) pos)
  6285. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  6286. (beginning-of-line 2))
  6287. (if (interactive-p) (message "This is table line %d" cnt))
  6288. cnt)))
  6289. (defun org-table-goto-column (n &optional on-delim force)
  6290. "Move the cursor to the Nth column in the current table line.
  6291. With optional argument ON-DELIM, stop with point before the left delimiter
  6292. of the field.
  6293. If there are less than N fields, just go to after the last delimiter.
  6294. However, when FORCE is non-nil, create new columns if necessary."
  6295. (interactive "p")
  6296. (let ((pos (point-at-eol)))
  6297. (beginning-of-line 1)
  6298. (when (> n 0)
  6299. (while (and (> (setq n (1- n)) -1)
  6300. (or (search-forward "|" pos t)
  6301. (and force
  6302. (progn (end-of-line 1)
  6303. (skip-chars-backward "^|")
  6304. (insert " | "))))))
  6305. ; (backward-char 2) t)))))
  6306. (when (and force (not (looking-at ".*|")))
  6307. (save-excursion (end-of-line 1) (insert " | ")))
  6308. (if on-delim
  6309. (backward-char 1)
  6310. (if (looking-at " ") (forward-char 1))))))
  6311. (defun org-at-table-p (&optional table-type)
  6312. "Return t if the cursor is inside an org-type table.
  6313. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  6314. (if org-enable-table-editor
  6315. (save-excursion
  6316. (beginning-of-line 1)
  6317. (looking-at (if table-type org-table-any-line-regexp
  6318. org-table-line-regexp)))
  6319. nil))
  6320. (defun org-at-table.el-p ()
  6321. "Return t if and only if we are at a table.el table."
  6322. (and (org-at-table-p 'any)
  6323. (save-excursion
  6324. (goto-char (org-table-begin 'any))
  6325. (looking-at org-table1-hline-regexp))))
  6326. (defun org-table-recognize-table.el ()
  6327. "If there is a table.el table nearby, recognize it and move into it."
  6328. (if org-table-tab-recognizes-table.el
  6329. (if (org-at-table.el-p)
  6330. (progn
  6331. (beginning-of-line 1)
  6332. (if (looking-at org-table-dataline-regexp)
  6333. nil
  6334. (if (looking-at org-table1-hline-regexp)
  6335. (progn
  6336. (beginning-of-line 2)
  6337. (if (looking-at org-table-any-border-regexp)
  6338. (beginning-of-line -1)))))
  6339. (if (re-search-forward "|" (org-table-end t) t)
  6340. (progn
  6341. (require 'table)
  6342. (if (table--at-cell-p (point))
  6343. t
  6344. (message "recognizing table.el table...")
  6345. (table-recognize-table)
  6346. (message "recognizing table.el table...done")))
  6347. (error "This should not happen..."))
  6348. t)
  6349. nil)
  6350. nil))
  6351. (defun org-at-table-hline-p ()
  6352. "Return t if the cursor is inside a hline in a table."
  6353. (if org-enable-table-editor
  6354. (save-excursion
  6355. (beginning-of-line 1)
  6356. (looking-at org-table-hline-regexp))
  6357. nil))
  6358. (defun org-table-insert-column ()
  6359. "Insert a new column into the table."
  6360. (interactive)
  6361. (if (not (org-at-table-p))
  6362. (error "Not at a table"))
  6363. (org-table-find-dataline)
  6364. (let* ((col (max 1 (org-table-current-column)))
  6365. (beg (org-table-begin))
  6366. (end (org-table-end))
  6367. ;; Current cursor position
  6368. (linepos (org-current-line))
  6369. (colpos col))
  6370. (goto-char beg)
  6371. (while (< (point) end)
  6372. (if (org-at-table-hline-p)
  6373. nil
  6374. (org-table-goto-column col t)
  6375. (insert "| "))
  6376. (beginning-of-line 2))
  6377. (move-marker end nil)
  6378. (goto-line linepos)
  6379. (org-table-goto-column colpos)
  6380. (org-table-align)
  6381. (org-table-fix-formulas "$" nil (1- col) 1)))
  6382. (defun org-table-find-dataline ()
  6383. "Find a dataline in the current table, which is needed for column commands."
  6384. (if (and (org-at-table-p)
  6385. (not (org-at-table-hline-p)))
  6386. t
  6387. (let ((col (current-column))
  6388. (end (org-table-end)))
  6389. (move-to-column col)
  6390. (while (and (< (point) end)
  6391. (or (not (= (current-column) col))
  6392. (org-at-table-hline-p)))
  6393. (beginning-of-line 2)
  6394. (move-to-column col))
  6395. (if (and (org-at-table-p)
  6396. (not (org-at-table-hline-p)))
  6397. t
  6398. (error
  6399. "Please position cursor in a data line for column operations")))))
  6400. (defun org-table-delete-column ()
  6401. "Delete a column from the table."
  6402. (interactive)
  6403. (if (not (org-at-table-p))
  6404. (error "Not at a table"))
  6405. (org-table-find-dataline)
  6406. (org-table-check-inside-data-field)
  6407. (let* ((col (org-table-current-column))
  6408. (beg (org-table-begin))
  6409. (end (org-table-end))
  6410. ;; Current cursor position
  6411. (linepos (org-current-line))
  6412. (colpos col))
  6413. (goto-char beg)
  6414. (while (< (point) end)
  6415. (if (org-at-table-hline-p)
  6416. nil
  6417. (org-table-goto-column col t)
  6418. (and (looking-at "|[^|\n]+|")
  6419. (replace-match "|")))
  6420. (beginning-of-line 2))
  6421. (move-marker end nil)
  6422. (goto-line linepos)
  6423. (org-table-goto-column colpos)
  6424. (org-table-align)
  6425. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  6426. col -1 col)))
  6427. (defun org-table-move-column-right ()
  6428. "Move column to the right."
  6429. (interactive)
  6430. (org-table-move-column nil))
  6431. (defun org-table-move-column-left ()
  6432. "Move column to the left."
  6433. (interactive)
  6434. (org-table-move-column 'left))
  6435. (defun org-table-move-column (&optional left)
  6436. "Move the current column to the right. With arg LEFT, move to the left."
  6437. (interactive "P")
  6438. (if (not (org-at-table-p))
  6439. (error "Not at a table"))
  6440. (org-table-find-dataline)
  6441. (org-table-check-inside-data-field)
  6442. (let* ((col (org-table-current-column))
  6443. (col1 (if left (1- col) col))
  6444. (beg (org-table-begin))
  6445. (end (org-table-end))
  6446. ;; Current cursor position
  6447. (linepos (org-current-line))
  6448. (colpos (if left (1- col) (1+ col))))
  6449. (if (and left (= col 1))
  6450. (error "Cannot move column further left"))
  6451. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  6452. (error "Cannot move column further right"))
  6453. (goto-char beg)
  6454. (while (< (point) end)
  6455. (if (org-at-table-hline-p)
  6456. nil
  6457. (org-table-goto-column col1 t)
  6458. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  6459. (replace-match "|\\2|\\1|")))
  6460. (beginning-of-line 2))
  6461. (move-marker end nil)
  6462. (goto-line linepos)
  6463. (org-table-goto-column colpos)
  6464. (org-table-align)
  6465. (org-table-fix-formulas
  6466. "$" (list (cons (number-to-string col) (number-to-string colpos))
  6467. (cons (number-to-string colpos) (number-to-string col))))))
  6468. (defun org-table-move-row-down ()
  6469. "Move table row down."
  6470. (interactive)
  6471. (org-table-move-row nil))
  6472. (defun org-table-move-row-up ()
  6473. "Move table row up."
  6474. (interactive)
  6475. (org-table-move-row 'up))
  6476. (defun org-table-move-row (&optional up)
  6477. "Move the current table line down. With arg UP, move it up."
  6478. (interactive "P")
  6479. (let* ((col (current-column))
  6480. (pos (point))
  6481. (hline1p (save-excursion (beginning-of-line 1)
  6482. (looking-at org-table-hline-regexp)))
  6483. (dline1 (org-table-current-dline))
  6484. (dline2 (+ dline1 (if up -1 1)))
  6485. (tonew (if up 0 2))
  6486. txt hline2p)
  6487. (beginning-of-line tonew)
  6488. (unless (org-at-table-p)
  6489. (goto-char pos)
  6490. (error "Cannot move row further"))
  6491. (setq hline2p (looking-at org-table-hline-regexp))
  6492. (goto-char pos)
  6493. (beginning-of-line 1)
  6494. (setq pos (point))
  6495. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  6496. (delete-region (point) (1+ (point-at-eol)))
  6497. (beginning-of-line tonew)
  6498. (insert txt)
  6499. (beginning-of-line 0)
  6500. (move-to-column col)
  6501. (unless (or hline1p hline2p)
  6502. (org-table-fix-formulas
  6503. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  6504. (cons (number-to-string dline2) (number-to-string dline1)))))))
  6505. (defun org-table-insert-row (&optional arg)
  6506. "Insert a new row above the current line into the table.
  6507. With prefix ARG, insert below the current line."
  6508. (interactive "P")
  6509. (if (not (org-at-table-p))
  6510. (error "Not at a table"))
  6511. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  6512. (new (org-table-clean-line line)))
  6513. ;; Fix the first field if necessary
  6514. (if (string-match "^[ \t]*| *[#$] *|" line)
  6515. (setq new (replace-match (match-string 0 line) t t new)))
  6516. (beginning-of-line (if arg 2 1))
  6517. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  6518. (beginning-of-line 0)
  6519. (re-search-forward "| ?" (point-at-eol) t)
  6520. (and (or org-table-may-need-update org-table-overlay-coordinates)
  6521. (org-table-align))
  6522. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  6523. (defun org-table-insert-hline (&optional above)
  6524. "Insert a horizontal-line below the current line into the table.
  6525. With prefix ABOVE, insert above the current line."
  6526. (interactive "P")
  6527. (if (not (org-at-table-p))
  6528. (error "Not at a table"))
  6529. (let ((line (org-table-clean-line
  6530. (buffer-substring (point-at-bol) (point-at-eol))))
  6531. (col (current-column)))
  6532. (while (string-match "|\\( +\\)|" line)
  6533. (setq line (replace-match
  6534. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  6535. ?-) "|") t t line)))
  6536. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  6537. (beginning-of-line (if above 1 2))
  6538. (insert line "\n")
  6539. (beginning-of-line (if above 1 -1))
  6540. (move-to-column col)
  6541. (and org-table-overlay-coordinates (org-table-align))))
  6542. (defun org-table-hline-and-move (&optional same-column)
  6543. "Insert a hline and move to the row below that line."
  6544. (interactive "P")
  6545. (let ((col (org-table-current-column)))
  6546. (org-table-maybe-eval-formula)
  6547. (org-table-maybe-recalculate-line)
  6548. (org-table-insert-hline)
  6549. (end-of-line 2)
  6550. (if (looking-at "\n[ \t]*|-")
  6551. (progn (insert "\n|") (org-table-align))
  6552. (org-table-next-field))
  6553. (if same-column (org-table-goto-column col))))
  6554. (defun org-table-clean-line (s)
  6555. "Convert a table line S into a string with only \"|\" and space.
  6556. In particular, this does handle wide and invisible characters."
  6557. (if (string-match "^[ \t]*|-" s)
  6558. ;; It's a hline, just map the characters
  6559. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  6560. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  6561. (setq s (replace-match
  6562. (concat "|" (make-string (org-string-width (match-string 1 s))
  6563. ?\ ) "|")
  6564. t t s)))
  6565. s))
  6566. (defun org-table-kill-row ()
  6567. "Delete the current row or horizontal line from the table."
  6568. (interactive)
  6569. (if (not (org-at-table-p))
  6570. (error "Not at a table"))
  6571. (let ((col (current-column))
  6572. (dline (org-table-current-dline)))
  6573. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  6574. (if (not (org-at-table-p)) (beginning-of-line 0))
  6575. (move-to-column col)
  6576. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  6577. dline -1 dline)))
  6578. (defun org-table-sort-lines (with-case &optional sorting-type)
  6579. "Sort table lines according to the column at point.
  6580. The position of point indicates the column to be used for
  6581. sorting, and the range of lines is the range between the nearest
  6582. horizontal separator lines, or the entire table of no such lines
  6583. exist. If point is before the first column, you will be prompted
  6584. for the sorting column. If there is an active region, the mark
  6585. specifies the first line and the sorting column, while point
  6586. should be in the last line to be included into the sorting.
  6587. The command then prompts for the sorting type which can be
  6588. alphabetically, numerically, or by time (as given in a time stamp
  6589. in the field). Sorting in reverse order is also possible.
  6590. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  6591. If SORTING-TYPE is specified when this function is called from a Lisp
  6592. program, no prompting will take place. SORTING-TYPE must be a character,
  6593. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  6594. should be done in reverse order."
  6595. (interactive "P")
  6596. (let* ((thisline (org-current-line))
  6597. (thiscol (org-table-current-column))
  6598. beg end bcol ecol tend tbeg column lns pos)
  6599. (when (equal thiscol 0)
  6600. (if (interactive-p)
  6601. (setq thiscol
  6602. (string-to-number
  6603. (read-string "Use column N for sorting: ")))
  6604. (setq thiscol 1))
  6605. (org-table-goto-column thiscol))
  6606. (org-table-check-inside-data-field)
  6607. (if (org-region-active-p)
  6608. (progn
  6609. (setq beg (region-beginning) end (region-end))
  6610. (goto-char beg)
  6611. (setq column (org-table-current-column)
  6612. beg (point-at-bol))
  6613. (goto-char end)
  6614. (setq end (point-at-bol 2)))
  6615. (setq column (org-table-current-column)
  6616. pos (point)
  6617. tbeg (org-table-begin)
  6618. tend (org-table-end))
  6619. (if (re-search-backward org-table-hline-regexp tbeg t)
  6620. (setq beg (point-at-bol 2))
  6621. (goto-char tbeg)
  6622. (setq beg (point-at-bol 1)))
  6623. (goto-char pos)
  6624. (if (re-search-forward org-table-hline-regexp tend t)
  6625. (setq end (point-at-bol 0))
  6626. (goto-char tend)
  6627. (setq end (point-at-bol))))
  6628. (setq beg (move-marker (make-marker) beg)
  6629. end (move-marker (make-marker) end))
  6630. (untabify beg end)
  6631. (goto-char beg)
  6632. (org-table-goto-column column)
  6633. (skip-chars-backward "^|")
  6634. (setq bcol (current-column))
  6635. (org-table-goto-column (1+ column))
  6636. (skip-chars-backward "^|")
  6637. (setq ecol (1- (current-column)))
  6638. (org-table-goto-column column)
  6639. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  6640. (org-split-string (buffer-substring beg end) "\n")))
  6641. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  6642. (delete-region beg end)
  6643. (move-marker beg nil)
  6644. (move-marker end nil)
  6645. (insert (mapconcat 'cdr lns "\n") "\n")
  6646. (goto-line thisline)
  6647. (org-table-goto-column thiscol)
  6648. (message "%d lines sorted, based on column %d" (length lns) column)))
  6649. (defun org-table-cut-region (beg end)
  6650. "Copy region in table to the clipboard and blank all relevant fields."
  6651. (interactive "r")
  6652. (org-table-copy-region beg end 'cut))
  6653. (defun org-table-copy-region (beg end &optional cut)
  6654. "Copy rectangular region in table to clipboard.
  6655. A special clipboard is used which can only be accessed
  6656. with `org-table-paste-rectangle'."
  6657. (interactive "rP")
  6658. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  6659. region cols
  6660. (rpl (if cut " " nil)))
  6661. (goto-char beg)
  6662. (org-table-check-inside-data-field)
  6663. (setq l01 (org-current-line)
  6664. c01 (org-table-current-column))
  6665. (goto-char end)
  6666. (org-table-check-inside-data-field)
  6667. (setq l02 (org-current-line)
  6668. c02 (org-table-current-column))
  6669. (setq l1 (min l01 l02) l2 (max l01 l02)
  6670. c1 (min c01 c02) c2 (max c01 c02))
  6671. (catch 'exit
  6672. (while t
  6673. (catch 'nextline
  6674. (if (> l1 l2) (throw 'exit t))
  6675. (goto-line l1)
  6676. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  6677. (setq cols nil ic1 c1 ic2 c2)
  6678. (while (< ic1 (1+ ic2))
  6679. (push (org-table-get-field ic1 rpl) cols)
  6680. (setq ic1 (1+ ic1)))
  6681. (push (nreverse cols) region)
  6682. (setq l1 (1+ l1)))))
  6683. (setq org-table-clip (nreverse region))
  6684. (if cut (org-table-align))
  6685. org-table-clip))
  6686. (defun org-table-paste-rectangle ()
  6687. "Paste a rectangular region into a table.
  6688. The upper right corner ends up in the current field. All involved fields
  6689. will be overwritten. If the rectangle does not fit into the present table,
  6690. the table is enlarged as needed. The process ignores horizontal separator
  6691. lines."
  6692. (interactive)
  6693. (unless (and org-table-clip (listp org-table-clip))
  6694. (error "First cut/copy a region to paste!"))
  6695. (org-table-check-inside-data-field)
  6696. (let* ((clip org-table-clip)
  6697. (line (org-current-line))
  6698. (col (org-table-current-column))
  6699. (org-enable-table-editor t)
  6700. (org-table-automatic-realign nil)
  6701. c cols field)
  6702. (while (setq cols (pop clip))
  6703. (while (org-at-table-hline-p) (beginning-of-line 2))
  6704. (if (not (org-at-table-p))
  6705. (progn (end-of-line 0) (org-table-next-field)))
  6706. (setq c col)
  6707. (while (setq field (pop cols))
  6708. (org-table-goto-column c nil 'force)
  6709. (org-table-get-field nil field)
  6710. (setq c (1+ c)))
  6711. (beginning-of-line 2))
  6712. (goto-line line)
  6713. (org-table-goto-column col)
  6714. (org-table-align)))
  6715. (defun org-table-convert ()
  6716. "Convert from `org-mode' table to table.el and back.
  6717. Obviously, this only works within limits. When an Org-mode table is
  6718. converted to table.el, all horizontal separator lines get lost, because
  6719. table.el uses these as cell boundaries and has no notion of horizontal lines.
  6720. A table.el table can be converted to an Org-mode table only if it does not
  6721. do row or column spanning. Multiline cells will become multiple cells.
  6722. Beware, Org-mode does not test if the table can be successfully converted - it
  6723. blindly applies a recipe that works for simple tables."
  6724. (interactive)
  6725. (require 'table)
  6726. (if (org-at-table.el-p)
  6727. ;; convert to Org-mode table
  6728. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  6729. (end (move-marker (make-marker) (org-table-end t))))
  6730. (table-unrecognize-region beg end)
  6731. (goto-char beg)
  6732. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  6733. (replace-match ""))
  6734. (goto-char beg))
  6735. (if (org-at-table-p)
  6736. ;; convert to table.el table
  6737. (let ((beg (move-marker (make-marker) (org-table-begin)))
  6738. (end (move-marker (make-marker) (org-table-end))))
  6739. ;; first, get rid of all horizontal lines
  6740. (goto-char beg)
  6741. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  6742. (replace-match ""))
  6743. ;; insert a hline before first
  6744. (goto-char beg)
  6745. (org-table-insert-hline 'above)
  6746. (beginning-of-line -1)
  6747. ;; insert a hline after each line
  6748. (while (progn (beginning-of-line 3) (< (point) end))
  6749. (org-table-insert-hline))
  6750. (goto-char beg)
  6751. (setq end (move-marker end (org-table-end)))
  6752. ;; replace "+" at beginning and ending of hlines
  6753. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  6754. (replace-match "\\1+-"))
  6755. (goto-char beg)
  6756. (while (re-search-forward "-|[ \t]*$" end t)
  6757. (replace-match "-+"))
  6758. (goto-char beg)))))
  6759. (defun org-table-wrap-region (arg)
  6760. "Wrap several fields in a column like a paragraph.
  6761. This is useful if you'd like to spread the contents of a field over several
  6762. lines, in order to keep the table compact.
  6763. If there is an active region, and both point and mark are in the same column,
  6764. the text in the column is wrapped to minimum width for the given number of
  6765. lines. Generally, this makes the table more compact. A prefix ARG may be
  6766. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  6767. formats the selected text to two lines. If the region was longer than two
  6768. lines, the remaining lines remain empty. A negative prefix argument reduces
  6769. the current number of lines by that amount. The wrapped text is pasted back
  6770. into the table. If you formatted it to more lines than it was before, fields
  6771. further down in the table get overwritten - so you might need to make space in
  6772. the table first.
  6773. If there is no region, the current field is split at the cursor position and
  6774. the text fragment to the right of the cursor is prepended to the field one
  6775. line down.
  6776. If there is no region, but you specify a prefix ARG, the current field gets
  6777. blank, and the content is appended to the field above."
  6778. (interactive "P")
  6779. (org-table-check-inside-data-field)
  6780. (if (org-region-active-p)
  6781. ;; There is a region: fill as a paragraph
  6782. (let* ((beg (region-beginning))
  6783. (cline (save-excursion (goto-char beg) (org-current-line)))
  6784. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  6785. nlines)
  6786. (org-table-cut-region (region-beginning) (region-end))
  6787. (if (> (length (car org-table-clip)) 1)
  6788. (error "Region must be limited to single column"))
  6789. (setq nlines (if arg
  6790. (if (< arg 1)
  6791. (+ (length org-table-clip) arg)
  6792. arg)
  6793. (length org-table-clip)))
  6794. (setq org-table-clip
  6795. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  6796. nil nlines)))
  6797. (goto-line cline)
  6798. (org-table-goto-column ccol)
  6799. (org-table-paste-rectangle))
  6800. ;; No region, split the current field at point
  6801. (if arg
  6802. ;; combine with field above
  6803. (let ((s (org-table-blank-field))
  6804. (col (org-table-current-column)))
  6805. (beginning-of-line 0)
  6806. (while (org-at-table-hline-p) (beginning-of-line 0))
  6807. (org-table-goto-column col)
  6808. (skip-chars-forward "^|")
  6809. (skip-chars-backward " ")
  6810. (insert " " (org-trim s))
  6811. (org-table-align))
  6812. ;; split field
  6813. (when (looking-at "\\([^|]+\\)+|")
  6814. (let ((s (match-string 1)))
  6815. (replace-match " |")
  6816. (goto-char (match-beginning 0))
  6817. (org-table-next-row)
  6818. (insert (org-trim s) " ")
  6819. (org-table-align))))))
  6820. (defvar org-field-marker nil)
  6821. (defun org-table-edit-field (arg)
  6822. "Edit table field in a different window.
  6823. This is mainly useful for fields that contain hidden parts.
  6824. When called with a \\[universal-argument] prefix, just make the full field visible so that
  6825. it can be edited in place."
  6826. (interactive "P")
  6827. (if arg
  6828. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  6829. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  6830. (remove-text-properties b e '(org-cwidth t invisible t
  6831. display t intangible t))
  6832. (if (and (boundp 'font-lock-mode) font-lock-mode)
  6833. (font-lock-fontify-block)))
  6834. (let ((pos (move-marker (make-marker) (point)))
  6835. (field (org-table-get-field))
  6836. (cw (current-window-configuration))
  6837. p)
  6838. (switch-to-buffer-other-window "*Org tmp*")
  6839. (erase-buffer)
  6840. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  6841. (let ((org-inhibit-startup t)) (org-mode))
  6842. (goto-char (setq p (point-max)))
  6843. (insert (org-trim field))
  6844. (remove-text-properties p (point-max)
  6845. '(invisible t org-cwidth t display t
  6846. intangible t))
  6847. (goto-char p)
  6848. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  6849. (org-set-local 'org-window-configuration cw)
  6850. (org-set-local 'org-field-marker pos)
  6851. (message "Edit and finish with C-c C-c"))))
  6852. (defun org-table-finish-edit-field ()
  6853. "Finish editing a table data field.
  6854. Remove all newline characters, insert the result into the table, realign
  6855. the table and kill the editing buffer."
  6856. (let ((pos org-field-marker)
  6857. (cw org-window-configuration)
  6858. (cb (current-buffer))
  6859. text)
  6860. (goto-char (point-min))
  6861. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  6862. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  6863. (replace-match " "))
  6864. (setq text (org-trim (buffer-string)))
  6865. (set-window-configuration cw)
  6866. (kill-buffer cb)
  6867. (select-window (get-buffer-window (marker-buffer pos)))
  6868. (goto-char pos)
  6869. (move-marker pos nil)
  6870. (org-table-check-inside-data-field)
  6871. (org-table-get-field nil text)
  6872. (org-table-align)
  6873. (message "New field value inserted")))
  6874. (defun org-trim (s)
  6875. "Remove whitespace at beginning and end of string."
  6876. (if (string-match "^[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  6877. (if (string-match "[ \t\n\r]+$" s) (setq s (replace-match "" t t s)))
  6878. s)
  6879. (defun org-wrap (string &optional width lines)
  6880. "Wrap string to either a number of lines, or a width in characters.
  6881. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  6882. that costs. If there is a word longer than WIDTH, the text is actually
  6883. wrapped to the length of that word.
  6884. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  6885. many lines, whatever width that takes.
  6886. The return value is a list of lines, without newlines at the end."
  6887. (let* ((words (org-split-string string "[ \t\n]+"))
  6888. (maxword (apply 'max (mapcar 'org-string-width words)))
  6889. w ll)
  6890. (cond (width
  6891. (org-do-wrap words (max maxword width)))
  6892. (lines
  6893. (setq w maxword)
  6894. (setq ll (org-do-wrap words maxword))
  6895. (if (<= (length ll) lines)
  6896. ll
  6897. (setq ll words)
  6898. (while (> (length ll) lines)
  6899. (setq w (1+ w))
  6900. (setq ll (org-do-wrap words w)))
  6901. ll))
  6902. (t (error "Cannot wrap this")))))
  6903. (defun org-do-wrap (words width)
  6904. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  6905. (let (lines line)
  6906. (while words
  6907. (setq line (pop words))
  6908. (while (and words (< (+ (length line) (length (car words))) width))
  6909. (setq line (concat line " " (pop words))))
  6910. (setq lines (push line lines)))
  6911. (nreverse lines)))
  6912. (defun org-split-string (string &optional separators)
  6913. "Splits STRING into substrings at SEPARATORS.
  6914. No empty strings are returned if there are matches at the beginning
  6915. and end of string."
  6916. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  6917. (start 0)
  6918. notfirst
  6919. (list nil))
  6920. (while (and (string-match rexp string
  6921. (if (and notfirst
  6922. (= start (match-beginning 0))
  6923. (< start (length string)))
  6924. (1+ start) start))
  6925. (< (match-beginning 0) (length string)))
  6926. (setq notfirst t)
  6927. (or (eq (match-beginning 0) 0)
  6928. (and (eq (match-beginning 0) (match-end 0))
  6929. (eq (match-beginning 0) start))
  6930. (setq list
  6931. (cons (substring string start (match-beginning 0))
  6932. list)))
  6933. (setq start (match-end 0)))
  6934. (or (eq start (length string))
  6935. (setq list
  6936. (cons (substring string start)
  6937. list)))
  6938. (nreverse list)))
  6939. (defun org-table-map-tables (function)
  6940. "Apply FUNCTION to the start of all tables in the buffer."
  6941. (save-excursion
  6942. (save-restriction
  6943. (widen)
  6944. (goto-char (point-min))
  6945. (while (re-search-forward org-table-any-line-regexp nil t)
  6946. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  6947. (beginning-of-line 1)
  6948. (if (looking-at org-table-line-regexp)
  6949. (save-excursion (funcall function)))
  6950. (re-search-forward org-table-any-border-regexp nil 1))))
  6951. (message "Mapping tables: done"))
  6952. (defvar org-timecnt) ; dynamically scoped parameter
  6953. (defun org-table-sum (&optional beg end nlast)
  6954. "Sum numbers in region of current table column.
  6955. The result will be displayed in the echo area, and will be available
  6956. as kill to be inserted with \\[yank].
  6957. If there is an active region, it is interpreted as a rectangle and all
  6958. numbers in that rectangle will be summed. If there is no active
  6959. region and point is located in a table column, sum all numbers in that
  6960. column.
  6961. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  6962. numbers are assumed to be times as well (in decimal hours) and the
  6963. numbers are added as such.
  6964. If NLAST is a number, only the NLAST fields will actually be summed."
  6965. (interactive)
  6966. (save-excursion
  6967. (let (col (org-timecnt 0) diff h m s org-table-clip)
  6968. (cond
  6969. ((and beg end)) ; beg and end given explicitly
  6970. ((org-region-active-p)
  6971. (setq beg (region-beginning) end (region-end)))
  6972. (t
  6973. (setq col (org-table-current-column))
  6974. (goto-char (org-table-begin))
  6975. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  6976. (error "No table data"))
  6977. (org-table-goto-column col)
  6978. (setq beg (point))
  6979. (goto-char (org-table-end))
  6980. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  6981. (error "No table data"))
  6982. (org-table-goto-column col)
  6983. (setq end (point))))
  6984. (let* ((items (apply 'append (org-table-copy-region beg end)))
  6985. (items1 (cond ((not nlast) items)
  6986. ((>= nlast (length items)) items)
  6987. (t (setq items (reverse items))
  6988. (setcdr (nthcdr (1- nlast) items) nil)
  6989. (nreverse items))))
  6990. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  6991. items1)))
  6992. (res (apply '+ numbers))
  6993. (sres (if (= org-timecnt 0)
  6994. (format "%g" res)
  6995. (setq diff (* 3600 res)
  6996. h (floor (/ diff 3600)) diff (mod diff 3600)
  6997. m (floor (/ diff 60)) diff (mod diff 60)
  6998. s diff)
  6999. (format "%d:%02d:%02d" h m s))))
  7000. (kill-new sres)
  7001. (if (interactive-p)
  7002. (message "%s"
  7003. (substitute-command-keys
  7004. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  7005. (length numbers) sres))))
  7006. sres))))
  7007. (defun org-table-get-number-for-summing (s)
  7008. (let (n)
  7009. (if (string-match "^ *|? *" s)
  7010. (setq s (replace-match "" nil nil s)))
  7011. (if (string-match " *|? *$" s)
  7012. (setq s (replace-match "" nil nil s)))
  7013. (setq n (string-to-number s))
  7014. (cond
  7015. ((and (string-match "0" s)
  7016. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  7017. ((string-match "\\`[ \t]+\\'" s) nil)
  7018. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  7019. (let ((h (string-to-number (or (match-string 1 s) "0")))
  7020. (m (string-to-number (or (match-string 2 s) "0")))
  7021. (s (string-to-number (or (match-string 4 s) "0"))))
  7022. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  7023. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  7024. ((equal n 0) nil)
  7025. (t n))))
  7026. (defun org-table-current-field-formula (&optional key noerror)
  7027. "Return the formula active for the current field.
  7028. Assumes that specials are in place.
  7029. If KEY is given, return the key to this formula.
  7030. Otherwise return the formula preceeded with \"=\" or \":=\"."
  7031. (let* ((name (car (rassoc (list (org-current-line)
  7032. (org-table-current-column))
  7033. org-table-named-field-locations)))
  7034. (col (org-table-current-column))
  7035. (scol (int-to-string col))
  7036. (ref (format "@%d$%d" (org-table-current-dline) col))
  7037. (stored-list (org-table-get-stored-formulas noerror))
  7038. (ass (or (assoc name stored-list)
  7039. (assoc ref stored-list)
  7040. (assoc scol stored-list))))
  7041. (if key
  7042. (car ass)
  7043. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  7044. (cdr ass))))))
  7045. (defun org-table-get-formula (&optional equation named)
  7046. "Read a formula from the minibuffer, offer stored formula as default.
  7047. When NAMED is non-nil, look for a named equation."
  7048. (let* ((stored-list (org-table-get-stored-formulas))
  7049. (name (car (rassoc (list (org-current-line)
  7050. (org-table-current-column))
  7051. org-table-named-field-locations)))
  7052. (ref (format "@%d$%d" (org-table-current-dline)
  7053. (org-table-current-column)))
  7054. (refass (assoc ref stored-list))
  7055. (scol (if named
  7056. (if name name ref)
  7057. (int-to-string (org-table-current-column))))
  7058. (dummy (and (or name refass) (not named)
  7059. (not (y-or-n-p "Replace field formula with column formula? " ))
  7060. (error "Abort")))
  7061. (name (or name ref))
  7062. (org-table-may-need-update nil)
  7063. (stored (cdr (assoc scol stored-list)))
  7064. (eq (cond
  7065. ((and stored equation (string-match "^ *=? *$" equation))
  7066. stored)
  7067. ((stringp equation)
  7068. equation)
  7069. (t (org-table-formula-from-user
  7070. (read-string
  7071. (org-table-formula-to-user
  7072. (format "%s formula %s%s="
  7073. (if named "Field" "Column")
  7074. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  7075. scol))
  7076. (if stored (org-table-formula-to-user stored) "")
  7077. 'org-table-formula-history
  7078. )))))
  7079. mustsave)
  7080. (when (not (string-match "\\S-" eq))
  7081. ;; remove formula
  7082. (setq stored-list (delq (assoc scol stored-list) stored-list))
  7083. (org-table-store-formulas stored-list)
  7084. (error "Formula removed"))
  7085. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  7086. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  7087. (if (and name (not named))
  7088. ;; We set the column equation, delete the named one.
  7089. (setq stored-list (delq (assoc name stored-list) stored-list)
  7090. mustsave t))
  7091. (if stored
  7092. (setcdr (assoc scol stored-list) eq)
  7093. (setq stored-list (cons (cons scol eq) stored-list)))
  7094. (if (or mustsave (not (equal stored eq)))
  7095. (org-table-store-formulas stored-list))
  7096. eq))
  7097. (defun org-table-store-formulas (alist)
  7098. "Store the list of formulas below the current table."
  7099. (setq alist (sort alist 'org-table-formula-less-p))
  7100. (save-excursion
  7101. (goto-char (org-table-end))
  7102. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  7103. (progn
  7104. ;; don't overwrite TBLFM, we might use text properties to store stuff
  7105. (goto-char (match-beginning 2))
  7106. (delete-region (match-beginning 2) (match-end 0)))
  7107. (insert "#+TBLFM:"))
  7108. (insert " "
  7109. (mapconcat (lambda (x)
  7110. (concat
  7111. (if (equal (string-to-char (car x)) ?@) "" "$")
  7112. (car x) "=" (cdr x)))
  7113. alist "::")
  7114. "\n")))
  7115. (defsubst org-table-formula-make-cmp-string (a)
  7116. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  7117. (concat
  7118. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  7119. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  7120. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  7121. (defun org-table-formula-less-p (a b)
  7122. "Compare two formulas for sorting."
  7123. (let ((as (org-table-formula-make-cmp-string (car a)))
  7124. (bs (org-table-formula-make-cmp-string (car b))))
  7125. (and as bs (string< as bs))))
  7126. (defun org-table-get-stored-formulas (&optional noerror)
  7127. "Return an alist with the stored formulas directly after current table."
  7128. (interactive)
  7129. (let (scol eq eq-alist strings string seen)
  7130. (save-excursion
  7131. (goto-char (org-table-end))
  7132. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  7133. (setq strings (org-split-string (match-string 2) " *:: *"))
  7134. (while (setq string (pop strings))
  7135. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  7136. (setq scol (if (match-end 2)
  7137. (match-string 2 string)
  7138. (match-string 1 string))
  7139. eq (match-string 3 string)
  7140. eq-alist (cons (cons scol eq) eq-alist))
  7141. (if (member scol seen)
  7142. (if noerror
  7143. (progn
  7144. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  7145. (ding)
  7146. (sit-for 2))
  7147. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  7148. (push scol seen))))))
  7149. (nreverse eq-alist)))
  7150. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  7151. "Modify the equations after the table structure has been edited.
  7152. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  7153. For all numbers larger than LIMIT, shift them by DELTA."
  7154. (save-excursion
  7155. (goto-char (org-table-end))
  7156. (when (looking-at "#\\+TBLFM:")
  7157. (let ((re (concat key "\\([0-9]+\\)"))
  7158. (re2
  7159. (when remove
  7160. (if (equal key "$")
  7161. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  7162. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  7163. s n a)
  7164. (when remove
  7165. (while (re-search-forward re2 (point-at-eol) t)
  7166. (replace-match "")))
  7167. (while (re-search-forward re (point-at-eol) t)
  7168. (setq s (match-string 1) n (string-to-number s))
  7169. (cond
  7170. ((setq a (assoc s replace))
  7171. (replace-match (concat key (cdr a)) t t))
  7172. ((and limit (> n limit))
  7173. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  7174. (defun org-table-get-specials ()
  7175. "Get the column names and local parameters for this table."
  7176. (save-excursion
  7177. (let ((beg (org-table-begin)) (end (org-table-end))
  7178. names name fields fields1 field cnt
  7179. c v l line col types dlines hlines)
  7180. (setq org-table-column-names nil
  7181. org-table-local-parameters nil
  7182. org-table-named-field-locations nil
  7183. org-table-current-begin-line nil
  7184. org-table-current-begin-pos nil
  7185. org-table-current-line-types nil)
  7186. (goto-char beg)
  7187. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  7188. (setq names (org-split-string (match-string 1) " *| *")
  7189. cnt 1)
  7190. (while (setq name (pop names))
  7191. (setq cnt (1+ cnt))
  7192. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  7193. (push (cons name (int-to-string cnt)) org-table-column-names))))
  7194. (setq org-table-column-names (nreverse org-table-column-names))
  7195. (setq org-table-column-name-regexp
  7196. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  7197. (goto-char beg)
  7198. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  7199. (setq fields (org-split-string (match-string 1) " *| *"))
  7200. (while (setq field (pop fields))
  7201. (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  7202. (push (cons (match-string 1 field) (match-string 2 field))
  7203. org-table-local-parameters))))
  7204. (goto-char beg)
  7205. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  7206. (setq c (match-string 1)
  7207. fields (org-split-string (match-string 2) " *| *"))
  7208. (save-excursion
  7209. (beginning-of-line (if (equal c "_") 2 0))
  7210. (setq line (org-current-line) col 1)
  7211. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  7212. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  7213. (while (and fields1 (setq field (pop fields)))
  7214. (setq v (pop fields1) col (1+ col))
  7215. (when (and (stringp field) (stringp v)
  7216. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  7217. (push (cons field v) org-table-local-parameters)
  7218. (push (list field line col) org-table-named-field-locations))))
  7219. ;; Analyse the line types
  7220. (goto-char beg)
  7221. (setq org-table-current-begin-line (org-current-line)
  7222. org-table-current-begin-pos (point)
  7223. l org-table-current-begin-line)
  7224. (while (looking-at "[ \t]*|\\(-\\)?")
  7225. (push (if (match-end 1) 'hline 'dline) types)
  7226. (if (match-end 1) (push l hlines) (push l dlines))
  7227. (beginning-of-line 2)
  7228. (setq l (1+ l)))
  7229. (setq org-table-current-line-types (apply 'vector (nreverse types))
  7230. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  7231. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  7232. (defun org-table-maybe-eval-formula ()
  7233. "Check if the current field starts with \"=\" or \":=\".
  7234. If yes, store the formula and apply it."
  7235. ;; We already know we are in a table. Get field will only return a formula
  7236. ;; when appropriate. It might return a separator line, but no problem.
  7237. (when org-table-formula-evaluate-inline
  7238. (let* ((field (org-trim (or (org-table-get-field) "")))
  7239. named eq)
  7240. (when (string-match "^:?=\\(.*\\)" field)
  7241. (setq named (equal (string-to-char field) ?:)
  7242. eq (match-string 1 field))
  7243. (if (or (fboundp 'calc-eval)
  7244. (equal (substring eq 0 (min 2 (length eq))) "'("))
  7245. (org-table-eval-formula (if named '(4) nil)
  7246. (org-table-formula-from-user eq))
  7247. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  7248. (defvar org-recalc-commands nil
  7249. "List of commands triggering the recalculation of a line.
  7250. Will be filled automatically during use.")
  7251. (defvar org-recalc-marks
  7252. '((" " . "Unmarked: no special line, no automatic recalculation")
  7253. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  7254. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  7255. ("!" . "Column name definition line. Reference in formula as $name.")
  7256. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  7257. ("_" . "Names for values in row below this one.")
  7258. ("^" . "Names for values in row above this one.")))
  7259. (defun org-table-rotate-recalc-marks (&optional newchar)
  7260. "Rotate the recalculation mark in the first column.
  7261. If in any row, the first field is not consistent with a mark,
  7262. insert a new column for the markers.
  7263. When there is an active region, change all the lines in the region,
  7264. after prompting for the marking character.
  7265. After each change, a message will be displayed indicating the meaning
  7266. of the new mark."
  7267. (interactive)
  7268. (unless (org-at-table-p) (error "Not at a table"))
  7269. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  7270. (beg (org-table-begin))
  7271. (end (org-table-end))
  7272. (l (org-current-line))
  7273. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  7274. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  7275. (have-col
  7276. (save-excursion
  7277. (goto-char beg)
  7278. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  7279. (col (org-table-current-column))
  7280. (forcenew (car (assoc newchar org-recalc-marks)))
  7281. epos new)
  7282. (when l1
  7283. (message "Change region to what mark? Type # * ! $ or SPC: ")
  7284. (setq newchar (char-to-string (read-char-exclusive))
  7285. forcenew (car (assoc newchar org-recalc-marks))))
  7286. (if (and newchar (not forcenew))
  7287. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  7288. newchar))
  7289. (if l1 (goto-line l1))
  7290. (save-excursion
  7291. (beginning-of-line 1)
  7292. (unless (looking-at org-table-dataline-regexp)
  7293. (error "Not at a table data line")))
  7294. (unless have-col
  7295. (org-table-goto-column 1)
  7296. (org-table-insert-column)
  7297. (org-table-goto-column (1+ col)))
  7298. (setq epos (point-at-eol))
  7299. (save-excursion
  7300. (beginning-of-line 1)
  7301. (org-table-get-field
  7302. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  7303. (concat " "
  7304. (setq new (or forcenew
  7305. (cadr (member (match-string 1) marks))))
  7306. " ")
  7307. " # ")))
  7308. (if (and l1 l2)
  7309. (progn
  7310. (goto-line l1)
  7311. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  7312. (and (looking-at org-table-dataline-regexp)
  7313. (org-table-get-field 1 (concat " " new " "))))
  7314. (goto-line l1)))
  7315. (if (not (= epos (point-at-eol))) (org-table-align))
  7316. (goto-line l)
  7317. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  7318. (defun org-table-maybe-recalculate-line ()
  7319. "Recompute the current line if marked for it, and if we haven't just done it."
  7320. (interactive)
  7321. (and org-table-allow-automatic-line-recalculation
  7322. (not (and (memq last-command org-recalc-commands)
  7323. (equal org-last-recalc-line (org-current-line))))
  7324. (save-excursion (beginning-of-line 1)
  7325. (looking-at org-table-auto-recalculate-regexp))
  7326. (org-table-recalculate) t))
  7327. (defvar org-table-formula-debug nil
  7328. "Non-nil means, debug table formulas.
  7329. When nil, simply write \"#ERROR\" in corrupted fields.")
  7330. (make-variable-buffer-local 'org-table-formula-debug)
  7331. (defvar modes)
  7332. (defsubst org-set-calc-mode (var &optional value)
  7333. (if (stringp var)
  7334. (setq var (assoc var '(("D" calc-angle-mode deg)
  7335. ("R" calc-angle-mode rad)
  7336. ("F" calc-prefer-frac t)
  7337. ("S" calc-symbolic-mode t)))
  7338. value (nth 2 var) var (nth 1 var)))
  7339. (if (memq var modes)
  7340. (setcar (cdr (memq var modes)) value)
  7341. (cons var (cons value modes)))
  7342. modes)
  7343. (defun org-table-eval-formula (&optional arg equation
  7344. suppress-align suppress-const
  7345. suppress-store suppress-analysis)
  7346. "Replace the table field value at the cursor by the result of a calculation.
  7347. This function makes use of Dave Gillespie's Calc package, in my view the
  7348. most exciting program ever written for GNU Emacs. So you need to have Calc
  7349. installed in order to use this function.
  7350. In a table, this command replaces the value in the current field with the
  7351. result of a formula. It also installs the formula as the \"current\" column
  7352. formula, by storing it in a special line below the table. When called
  7353. with a `C-u' prefix, the current field must ba a named field, and the
  7354. formula is installed as valid in only this specific field.
  7355. When called with two `C-u' prefixes, insert the active equation
  7356. for the field back into the current field, so that it can be
  7357. edited there. This is useful in order to use \\[org-table-show-reference]
  7358. to check the referenced fields.
  7359. When called, the command first prompts for a formula, which is read in
  7360. the minibuffer. Previously entered formulas are available through the
  7361. history list, and the last used formula is offered as a default.
  7362. These stored formulas are adapted correctly when moving, inserting, or
  7363. deleting columns with the corresponding commands.
  7364. The formula can be any algebraic expression understood by the Calc package.
  7365. For details, see the Org-mode manual.
  7366. This function can also be called from Lisp programs and offers
  7367. additional arguments: EQUATION can be the formula to apply. If this
  7368. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  7369. used to speed-up recursive calls by by-passing unnecessary aligns.
  7370. SUPPRESS-CONST suppresses the interpretation of constants in the
  7371. formula, assuming that this has been done already outside the function.
  7372. SUPPRESS-STORE means the formula should not be stored, either because
  7373. it is already stored, or because it is a modified equation that should
  7374. not overwrite the stored one."
  7375. (interactive "P")
  7376. (org-table-check-inside-data-field)
  7377. (or suppress-analysis (org-table-get-specials))
  7378. (if (equal arg '(16))
  7379. (let ((eq (org-table-current-field-formula)))
  7380. (or eq (error "No equation active for current field"))
  7381. (org-table-get-field nil eq)
  7382. (org-table-align)
  7383. (setq org-table-may-need-update t))
  7384. (let* (fields
  7385. (ndown (if (integerp arg) arg 1))
  7386. (org-table-automatic-realign nil)
  7387. (case-fold-search nil)
  7388. (down (> ndown 1))
  7389. (formula (if (and equation suppress-store)
  7390. equation
  7391. (org-table-get-formula equation (equal arg '(4)))))
  7392. (n0 (org-table-current-column))
  7393. (modes (copy-sequence org-calc-default-modes))
  7394. (numbers nil) ; was a variable, now fixed default
  7395. (keep-empty nil)
  7396. n form form0 bw fmt x ev orig c lispp literal)
  7397. ;; Parse the format string. Since we have a lot of modes, this is
  7398. ;; a lot of work. However, I think calc still uses most of the time.
  7399. (if (string-match ";" formula)
  7400. (let ((tmp (org-split-string formula ";")))
  7401. (setq formula (car tmp)
  7402. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  7403. (nth 1 tmp)))
  7404. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  7405. (setq c (string-to-char (match-string 1 fmt))
  7406. n (string-to-number (match-string 2 fmt)))
  7407. (if (= c ?p)
  7408. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  7409. (setq modes (org-set-calc-mode
  7410. 'calc-float-format
  7411. (list (cdr (assoc c '((?n . float) (?f . fix)
  7412. (?s . sci) (?e . eng))))
  7413. n))))
  7414. (setq fmt (replace-match "" t t fmt)))
  7415. (if (string-match "[NT]" fmt)
  7416. (setq numbers (equal (match-string 0 fmt) "N"))
  7417. fmt (replace-match "" t t fmt))
  7418. (if (string-match "L" fmt)
  7419. (setq literal t
  7420. fmt (replace-match "" t t fmt)))
  7421. (if (string-match "E" fmt)
  7422. (setq keep-empty t
  7423. fmt (replace-match "" t t fmt)))
  7424. (while (string-match "[DRFS]" fmt)
  7425. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  7426. (setq fmt (replace-match "" t t fmt)))
  7427. (unless (string-match "\\S-" fmt)
  7428. (setq fmt nil))))
  7429. (if (and (not suppress-const) org-table-formula-use-constants)
  7430. (setq formula (org-table-formula-substitute-names formula)))
  7431. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  7432. (while (> ndown 0)
  7433. (setq fields (org-split-string
  7434. (org-no-properties
  7435. (buffer-substring (point-at-bol) (point-at-eol)))
  7436. " *| *"))
  7437. (if (eq numbers t)
  7438. (setq fields (mapcar
  7439. (lambda (x) (number-to-string (string-to-number x)))
  7440. fields)))
  7441. (setq ndown (1- ndown))
  7442. (setq form (copy-sequence formula)
  7443. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  7444. (if (and lispp literal) (setq lispp 'literal))
  7445. ;; Check for old vertical references
  7446. (setq form (org-rewrite-old-row-references form))
  7447. ;; Insert complex ranges
  7448. (while (string-match org-table-range-regexp form)
  7449. (setq form
  7450. (replace-match
  7451. (save-match-data
  7452. (org-table-make-reference
  7453. (org-table-get-range (match-string 0 form) nil n0)
  7454. keep-empty numbers lispp))
  7455. t t form)))
  7456. ;; Insert simple ranges
  7457. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  7458. (setq form
  7459. (replace-match
  7460. (save-match-data
  7461. (org-table-make-reference
  7462. (org-sublist
  7463. fields (string-to-number (match-string 1 form))
  7464. (string-to-number (match-string 2 form)))
  7465. keep-empty numbers lispp))
  7466. t t form)))
  7467. (setq form0 form)
  7468. ;; Insert the references to fields in same row
  7469. (while (string-match "\\$\\([0-9]+\\)" form)
  7470. (setq n (string-to-number (match-string 1 form))
  7471. x (nth (1- (if (= n 0) n0 n)) fields))
  7472. (unless x (error "Invalid field specifier \"%s\""
  7473. (match-string 0 form)))
  7474. (setq form (replace-match
  7475. (save-match-data
  7476. (org-table-make-reference x nil numbers lispp))
  7477. t t form)))
  7478. (if lispp
  7479. (setq ev (condition-case nil
  7480. (eval (eval (read form)))
  7481. (error "#ERROR"))
  7482. ev (if (numberp ev) (number-to-string ev) ev))
  7483. (or (fboundp 'calc-eval)
  7484. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  7485. (setq ev (calc-eval (cons form modes)
  7486. (if numbers 'num))))
  7487. (when org-table-formula-debug
  7488. (with-output-to-temp-buffer "*Substitution History*"
  7489. (princ (format "Substitution history of formula
  7490. Orig: %s
  7491. $xyz-> %s
  7492. @r$c-> %s
  7493. $1-> %s\n" orig formula form0 form))
  7494. (if (listp ev)
  7495. (princ (format " %s^\nError: %s"
  7496. (make-string (car ev) ?\-) (nth 1 ev)))
  7497. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  7498. ev (or fmt "NONE")
  7499. (if fmt (format fmt (string-to-number ev)) ev)))))
  7500. (setq bw (get-buffer-window "*Substitution History*"))
  7501. (shrink-window-if-larger-than-buffer bw)
  7502. (unless (and (interactive-p) (not ndown))
  7503. (unless (let (inhibit-redisplay)
  7504. (y-or-n-p "Debugging Formula. Continue to next? "))
  7505. (org-table-align)
  7506. (error "Abort"))
  7507. (delete-window bw)
  7508. (message "")))
  7509. (if (listp ev) (setq fmt nil ev "#ERROR"))
  7510. (org-table-justify-field-maybe
  7511. (if fmt (format fmt (string-to-number ev)) ev))
  7512. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  7513. (call-interactively 'org-return)
  7514. (setq ndown 0)))
  7515. (and down (org-table-maybe-recalculate-line))
  7516. (or suppress-align (and org-table-may-need-update
  7517. (org-table-align))))))
  7518. (defun org-table-get-range (desc &optional tbeg col highlight)
  7519. "Get a calc vector from a column, accorting to descriptor DESC.
  7520. Optional arguments TBEG and COL can give the beginning of the table and
  7521. the current column, to avoid unnecessary parsing.
  7522. HIGHLIGHT means, just highlight the range."
  7523. (if (not (equal (string-to-char desc) ?@))
  7524. (setq desc (concat "@" desc)))
  7525. (save-excursion
  7526. (or tbeg (setq tbeg (org-table-begin)))
  7527. (or col (setq col (org-table-current-column)))
  7528. (let ((thisline (org-current-line))
  7529. beg end c1 c2 r1 r2 rangep tmp)
  7530. (unless (string-match org-table-range-regexp desc)
  7531. (error "Invalid table range specifier `%s'" desc))
  7532. (setq rangep (match-end 3)
  7533. r1 (and (match-end 1) (match-string 1 desc))
  7534. r2 (and (match-end 4) (match-string 4 desc))
  7535. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  7536. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  7537. (and c1 (setq c1 (+ (string-to-number c1)
  7538. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  7539. (and c2 (setq c2 (+ (string-to-number c2)
  7540. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  7541. (if (equal r1 "") (setq r1 nil))
  7542. (if (equal r2 "") (setq r2 nil))
  7543. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  7544. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  7545. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  7546. (if (not r1) (setq r1 thisline))
  7547. (if (not r2) (setq r2 thisline))
  7548. (if (not c1) (setq c1 col))
  7549. (if (not c2) (setq c2 col))
  7550. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  7551. ;; just one field
  7552. (progn
  7553. (goto-line r1)
  7554. (while (not (looking-at org-table-dataline-regexp))
  7555. (beginning-of-line 2))
  7556. (prog1 (org-table-get-field c1)
  7557. (if highlight (org-table-highlight-rectangle (point) (point)))))
  7558. ;; A range, return a vector
  7559. ;; First sort the numbers to get a regular ractangle
  7560. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  7561. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  7562. (goto-line r1)
  7563. (while (not (looking-at org-table-dataline-regexp))
  7564. (beginning-of-line 2))
  7565. (org-table-goto-column c1)
  7566. (setq beg (point))
  7567. (goto-line r2)
  7568. (while (not (looking-at org-table-dataline-regexp))
  7569. (beginning-of-line 0))
  7570. (org-table-goto-column c2)
  7571. (setq end (point))
  7572. (if highlight
  7573. (org-table-highlight-rectangle
  7574. beg (progn (skip-chars-forward "^|\n") (point))))
  7575. ;; return string representation of calc vector
  7576. (apply 'append (org-table-copy-region beg end))))))
  7577. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  7578. "Analyze descriptor DESC and retrieve the corresponding line number.
  7579. The cursor is currently in line CLINE, the table begins in line BLINE,
  7580. and TABLE is a vector with line types."
  7581. (if (string-match "^[0-9]+$" desc)
  7582. (aref org-table-dlines (string-to-number desc))
  7583. (setq cline (or cline (org-current-line))
  7584. bline (or bline org-table-current-begin-line)
  7585. table (or table org-table-current-line-types))
  7586. (if (or
  7587. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  7588. ;; 1 2 3 4 5 6
  7589. (and (not (match-end 3)) (not (match-end 6)))
  7590. (and (match-end 3) (match-end 6) (not (match-end 5))))
  7591. (error "invalid row descriptor `%s'" desc))
  7592. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  7593. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  7594. (odir (and (match-end 5) (match-string 5 desc)))
  7595. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  7596. (i (- cline bline))
  7597. (rel (and (match-end 6)
  7598. (or (and (match-end 1) (not (match-end 3)))
  7599. (match-end 5)))))
  7600. (if (and hn (not hdir))
  7601. (progn
  7602. (setq i 0 hdir "+")
  7603. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  7604. (if (and (not hn) on (not odir))
  7605. (error "should never happen");;(aref org-table-dlines on)
  7606. (if (and hn (> hn 0))
  7607. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  7608. (if on
  7609. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  7610. (+ bline i)))))
  7611. (defun org-find-row-type (table i type backwards relative n)
  7612. (let ((l (length table)))
  7613. (while (> n 0)
  7614. (while (and (setq i (+ i (if backwards -1 1)))
  7615. (>= i 0) (< i l)
  7616. (not (eq (aref table i) type))
  7617. (if (and relative (eq (aref table i) 'hline))
  7618. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  7619. t)))
  7620. (setq n (1- n)))
  7621. (if (or (< i 0) (>= i l))
  7622. (error "Row descriptior leads outside table")
  7623. i)))
  7624. (defun org-rewrite-old-row-references (s)
  7625. (if (string-match "&[-+0-9I]" s)
  7626. (error "Formula contains old &row reference, please rewrite using @-syntax")
  7627. s))
  7628. (defun org-table-make-reference (elements keep-empty numbers lispp)
  7629. "Convert list ELEMENTS to something appropriate to insert into formula.
  7630. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  7631. NUMBERS indicates that everything should be converted to numbers.
  7632. LISPP means to return something appropriate for a Lisp list."
  7633. (if (stringp elements) ; just a single val
  7634. (if lispp
  7635. (if (eq lispp 'literal)
  7636. elements
  7637. (prin1-to-string (if numbers (string-to-number elements) elements)))
  7638. (if (equal elements "") (setq elements "0"))
  7639. (if numbers (number-to-string (string-to-number elements)) elements))
  7640. (unless keep-empty
  7641. (setq elements
  7642. (delq nil
  7643. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  7644. elements))))
  7645. (setq elements (or elements '("0")))
  7646. (if lispp
  7647. (mapconcat
  7648. (lambda (x)
  7649. (if (eq lispp 'literal)
  7650. x
  7651. (prin1-to-string (if numbers (string-to-number x) x))))
  7652. " ")
  7653. (concat "[" (mapconcat
  7654. (lambda (x)
  7655. (if numbers (number-to-string (string-to-number x)) x))
  7656. elements
  7657. ",") "]"))))
  7658. (defun org-table-recalculate (&optional all noalign)
  7659. "Recalculate the current table line by applying all stored formulas.
  7660. With prefix arg ALL, do this for all lines in the table."
  7661. (interactive "P")
  7662. (or (memq this-command org-recalc-commands)
  7663. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  7664. (unless (org-at-table-p) (error "Not at a table"))
  7665. (if (equal all '(16))
  7666. (org-table-iterate)
  7667. (org-table-get-specials)
  7668. (let* ((eqlist (sort (org-table-get-stored-formulas)
  7669. (lambda (a b) (string< (car a) (car b)))))
  7670. (inhibit-redisplay (not debug-on-error))
  7671. (line-re org-table-dataline-regexp)
  7672. (thisline (org-current-line))
  7673. (thiscol (org-table-current-column))
  7674. beg end entry eqlnum eqlname eql (cnt 0) eq a name)
  7675. ;; Insert constants in all formulas
  7676. (setq eqlist
  7677. (mapcar (lambda (x)
  7678. (setcdr x (org-table-formula-substitute-names (cdr x)))
  7679. x)
  7680. eqlist))
  7681. ;; Split the equation list
  7682. (while (setq eq (pop eqlist))
  7683. (if (<= (string-to-char (car eq)) ?9)
  7684. (push eq eqlnum)
  7685. (push eq eqlname)))
  7686. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  7687. (if all
  7688. (progn
  7689. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  7690. (goto-char (setq beg (org-table-begin)))
  7691. (if (re-search-forward org-table-calculate-mark-regexp end t)
  7692. ;; This is a table with marked lines, compute selected lines
  7693. (setq line-re org-table-recalculate-regexp)
  7694. ;; Move forward to the first non-header line
  7695. (if (and (re-search-forward org-table-dataline-regexp end t)
  7696. (re-search-forward org-table-hline-regexp end t)
  7697. (re-search-forward org-table-dataline-regexp end t))
  7698. (setq beg (match-beginning 0))
  7699. nil))) ;; just leave beg where it is
  7700. (setq beg (point-at-bol)
  7701. end (move-marker (make-marker) (1+ (point-at-eol)))))
  7702. (goto-char beg)
  7703. (and all (message "Re-applying formulas to full table..."))
  7704. (while (re-search-forward line-re end t)
  7705. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  7706. ;; Unprotected line, recalculate
  7707. (and all (message "Re-applying formulas to full table...(line %d)"
  7708. (setq cnt (1+ cnt))))
  7709. (setq org-last-recalc-line (org-current-line))
  7710. (setq eql eqlnum)
  7711. (while (setq entry (pop eql))
  7712. (goto-line org-last-recalc-line)
  7713. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  7714. (org-table-eval-formula nil (cdr entry)
  7715. 'noalign 'nocst 'nostore 'noanalysis))))
  7716. (goto-line thisline)
  7717. (org-table-goto-column thiscol)
  7718. (or noalign (and org-table-may-need-update (org-table-align))
  7719. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  7720. ;; Now do the named fields
  7721. (while (setq eq (pop eqlname))
  7722. (setq name (car eq)
  7723. a (assoc name org-table-named-field-locations))
  7724. (and (not a)
  7725. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  7726. (setq a
  7727. (list
  7728. name
  7729. (aref org-table-dlines
  7730. (string-to-number (match-string 1 name)))
  7731. (string-to-number (match-string 2 name)))))
  7732. (when (and a (or all (equal (nth 1 a) thisline)))
  7733. (message "Re-applying formula to field: %s" name)
  7734. (goto-line (nth 1 a))
  7735. (org-table-goto-column (nth 2 a))
  7736. (org-table-eval-formula nil (cdr eq) 'noalign 'nocst
  7737. 'nostore 'noanalysis)))
  7738. ;; back to initial position
  7739. (message "Re-applying formulas...done")
  7740. (goto-line thisline)
  7741. (org-table-goto-column thiscol)
  7742. (or noalign (and org-table-may-need-update (org-table-align))
  7743. (and all (message "Re-applying formulas...done"))))))
  7744. (defun org-table-iterate (&optional arg)
  7745. "Recalculate the table until it does not change anymore."
  7746. (interactive "P")
  7747. (let ((imax (if arg (prefix-numeric-value arg) 10))
  7748. (i 0)
  7749. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  7750. thistbl)
  7751. (catch 'exit
  7752. (while (< i imax)
  7753. (setq i (1+ i))
  7754. (org-table-recalculate 'all)
  7755. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  7756. (if (not (string= lasttbl thistbl))
  7757. (setq lasttbl thistbl)
  7758. (if (> i 1)
  7759. (message "Convergence after %d iterations" i)
  7760. (message "Table was already stable"))
  7761. (throw 'exit t)))
  7762. (error "No convergence after %d iterations" i))))
  7763. (defun org-table-formula-substitute-names (f)
  7764. "Replace $const with values in string F."
  7765. (let ((start 0) a (f1 f))
  7766. ;; First, check for column names
  7767. (while (setq start (string-match org-table-column-name-regexp f start))
  7768. (setq start (1+ start))
  7769. (setq a (assoc (match-string 1 f) org-table-column-names))
  7770. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  7771. ;; Parameters and constants
  7772. (setq start 0)
  7773. (while (setq start (string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start))
  7774. (setq start (1+ start))
  7775. (if (setq a (save-match-data
  7776. (org-table-get-constant (match-string 1 f))))
  7777. (setq f (replace-match (concat "(" a ")") t t f))))
  7778. (if org-table-formula-debug
  7779. (put-text-property 0 (length f) :orig-formula f1 f))
  7780. f))
  7781. (defun org-table-get-constant (const)
  7782. "Find the value for a parameter or constant in a formula.
  7783. Parameters get priority."
  7784. (or (cdr (assoc const org-table-local-parameters))
  7785. (cdr (assoc const org-table-formula-constants))
  7786. (and (fboundp 'constants-get) (constants-get const))
  7787. "#UNDEFINED_NAME"))
  7788. (defvar org-table-fedit-map (make-sparse-keymap))
  7789. (org-defkey org-table-fedit-map "\C-x\C-s" 'org-table-fedit-finish)
  7790. (org-defkey org-table-fedit-map "\C-c\C-s" 'org-table-fedit-finish)
  7791. (org-defkey org-table-fedit-map "\C-c\C-c" 'org-table-fedit-finish)
  7792. (org-defkey org-table-fedit-map "\C-c\C-q" 'org-table-fedit-abort)
  7793. (org-defkey org-table-fedit-map "\C-c?" 'org-table-show-reference)
  7794. (org-defkey org-table-fedit-map [(meta shift up)] 'org-table-fedit-line-up)
  7795. (org-defkey org-table-fedit-map [(meta shift down)] 'org-table-fedit-line-down)
  7796. (org-defkey org-table-fedit-map [(shift up)] 'org-table-fedit-ref-up)
  7797. (org-defkey org-table-fedit-map [(shift down)] 'org-table-fedit-ref-down)
  7798. (org-defkey org-table-fedit-map [(shift left)] 'org-table-fedit-ref-left)
  7799. (org-defkey org-table-fedit-map [(shift right)] 'org-table-fedit-ref-right)
  7800. (org-defkey org-table-fedit-map [(meta up)] 'org-table-fedit-scroll-down)
  7801. (org-defkey org-table-fedit-map [(meta down)] 'org-table-fedit-scroll)
  7802. (org-defkey org-table-fedit-map [(meta tab)] 'lisp-complete-symbol)
  7803. (org-defkey org-table-fedit-map "\M-\C-i" 'lisp-complete-symbol)
  7804. (org-defkey org-table-fedit-map [(tab)] 'org-table-fedit-lisp-indent)
  7805. (org-defkey org-table-fedit-map "\C-i" 'org-table-fedit-lisp-indent)
  7806. (org-defkey org-table-fedit-map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  7807. (org-defkey org-table-fedit-map "\C-c}" 'org-table-fedit-toggle-coordinates)
  7808. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  7809. '("Edit-Formulas"
  7810. ["Finish and Install" org-table-fedit-finish t]
  7811. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  7812. ["Abort" org-table-fedit-abort t]
  7813. "--"
  7814. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  7815. ["Complete Lisp Symbol" lisp-complete-symbol t]
  7816. "--"
  7817. "Shift Reference at Point"
  7818. ["Up" org-table-fedit-ref-up t]
  7819. ["Down" org-table-fedit-ref-down t]
  7820. ["Left" org-table-fedit-ref-left t]
  7821. ["Right" org-table-fedit-ref-right t]
  7822. "-"
  7823. "Change Test Row for Column Formulas"
  7824. ["Up" org-table-fedit-line-up t]
  7825. ["Down" org-table-fedit-line-down t]
  7826. "--"
  7827. ["Scroll Table Window" org-table-fedit-scroll t]
  7828. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  7829. ["Show Table Grid" org-table-fedit-toggle-coordinates
  7830. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  7831. org-table-overlay-coordinates)]
  7832. "--"
  7833. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  7834. :style toggle :selected org-table-buffer-is-an]))
  7835. (defvar org-pos)
  7836. (defun org-table-edit-formulas ()
  7837. "Edit the formulas of the current table in a separate buffer."
  7838. (interactive)
  7839. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  7840. (beginning-of-line 0))
  7841. (unless (org-at-table-p) (error "Not at a table"))
  7842. (org-table-get-specials)
  7843. (let ((key (org-table-current-field-formula 'key 'noerror))
  7844. (eql (sort (org-table-get-stored-formulas 'noerror)
  7845. 'org-table-formula-less-p))
  7846. (pos (move-marker (make-marker) (point)))
  7847. (startline 1)
  7848. (wc (current-window-configuration))
  7849. (titles '((column . "# Column Formulas\n")
  7850. (field . "# Field Formulas\n")
  7851. (named . "# Named Field Formulas\n")))
  7852. entry s type title)
  7853. (switch-to-buffer-other-window "*Edit Formulas*")
  7854. (erase-buffer)
  7855. ;; Keep global-font-lock-mode from turning on font-lock-mode
  7856. (let ((font-lock-global-modes '(not fundamental-mode)))
  7857. (fundamental-mode))
  7858. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  7859. (org-set-local 'org-pos pos)
  7860. (org-set-local 'org-window-configuration wc)
  7861. (use-local-map org-table-fedit-map)
  7862. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  7863. (easy-menu-add org-table-fedit-menu)
  7864. (setq startline (org-current-line))
  7865. (while (setq entry (pop eql))
  7866. (setq type (cond
  7867. ((equal (string-to-char (car entry)) ?@) 'field)
  7868. ((string-match "^[0-9]" (car entry)) 'column)
  7869. (t 'named)))
  7870. (when (setq title (assq type titles))
  7871. (or (bobp) (insert "\n"))
  7872. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  7873. (setq titles (delq title titles)))
  7874. (if (equal key (car entry)) (setq startline (org-current-line)))
  7875. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  7876. (car entry) " = " (cdr entry) "\n"))
  7877. (remove-text-properties 0 (length s) '(face nil) s)
  7878. (insert s))
  7879. (if (eq org-table-use-standard-references t)
  7880. (org-table-fedit-toggle-ref-type))
  7881. (goto-line startline)
  7882. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  7883. (defun org-table-fedit-post-command ()
  7884. (when (not (memq this-command '(lisp-complete-symbol)))
  7885. (let ((win (selected-window)))
  7886. (save-excursion
  7887. (condition-case nil
  7888. (org-table-show-reference)
  7889. (error nil))
  7890. (select-window win)))))
  7891. (defun org-table-formula-to-user (s)
  7892. "Convert a formula from internal to user representation."
  7893. (if (eq org-table-use-standard-references t)
  7894. (org-table-convert-refs-to-an s)
  7895. s))
  7896. (defun org-table-formula-from-user (s)
  7897. "Convert a formula from user to internal representation."
  7898. (if org-table-use-standard-references
  7899. (org-table-convert-refs-to-rc s)
  7900. s))
  7901. (defun org-table-convert-refs-to-rc (s)
  7902. "Convert spreadsheet references from AB7 to @7$28.
  7903. Works for single references, but also for entire formulas and even the
  7904. full TBLFM line."
  7905. (let ((start 0))
  7906. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  7907. (cond
  7908. ((match-end 3)
  7909. ;; format match, just advance
  7910. (setq start (match-end 0)))
  7911. ((and (> (match-beginning 0) 0)
  7912. (equal ?. (aref s (max (1- (match-beginning 0)) 0))))
  7913. ;; 3.e5 or something like this. FIXME: is this ok????
  7914. (setq start (match-end 0)))
  7915. (t
  7916. (setq start (match-beginning 0)
  7917. s (replace-match
  7918. (if (equal (match-string 2 s) "&")
  7919. (format "$%d" (org-letters-to-number (match-string 1 s)))
  7920. (format "@%d$%d"
  7921. (string-to-number (match-string 2 s))
  7922. (org-letters-to-number (match-string 1 s))))
  7923. t t s)))))
  7924. s))
  7925. (defun org-table-convert-refs-to-an (s)
  7926. "Convert spreadsheet references from to @7$28 to AB7.
  7927. Works for single references, but also for entire formulas and even the
  7928. full TBLFM line."
  7929. (while (string-match "@\\([0-9]+\\)$\\([0-9]+\\)" s)
  7930. (setq s (replace-match
  7931. (format "%s%d"
  7932. (org-number-to-letters
  7933. (string-to-number (match-string 2 s)))
  7934. (string-to-number (match-string 1 s)))
  7935. t t s)))
  7936. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  7937. (setq s (replace-match (concat "\\1"
  7938. (org-number-to-letters
  7939. (string-to-number (match-string 2 s))) "&")
  7940. t nil s)))
  7941. s)
  7942. (defun org-letters-to-number (s)
  7943. "Convert a base 26 number represented by letters into an integer.
  7944. For example: AB -> 28."
  7945. (let ((n 0))
  7946. (setq s (upcase s))
  7947. (while (> (length s) 0)
  7948. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  7949. s (substring s 1)))
  7950. n))
  7951. (defun org-number-to-letters (n)
  7952. "Convert an integer into a base 26 number represented by letters.
  7953. For example: 28 -> AB."
  7954. (let ((s ""))
  7955. (while (> n 0)
  7956. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  7957. n (/ (1- n) 26)))
  7958. s))
  7959. (defun org-table-fedit-convert-buffer (function)
  7960. "Convert all references in this buffer, using FUNTION."
  7961. (let ((line (org-current-line)))
  7962. (goto-char (point-min))
  7963. (while (not (eobp))
  7964. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  7965. (delete-region (point) (point-at-eol))
  7966. (or (eobp) (forward-char 1)))
  7967. (goto-line line)))
  7968. (defun org-table-fedit-toggle-ref-type ()
  7969. "Convert all references in the buffer from B3 to @3$2 and back."
  7970. (interactive)
  7971. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  7972. (org-table-fedit-convert-buffer
  7973. (if org-table-buffer-is-an
  7974. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  7975. (message "Reference type switched to %s"
  7976. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  7977. (defun org-table-fedit-ref-up ()
  7978. "Shift the reference at point one row/hline up."
  7979. (interactive)
  7980. (org-table-fedit-shift-reference 'up))
  7981. (defun org-table-fedit-ref-down ()
  7982. "Shift the reference at point one row/hline down."
  7983. (interactive)
  7984. (org-table-fedit-shift-reference 'down))
  7985. (defun org-table-fedit-ref-left ()
  7986. "Shift the reference at point one field to the left."
  7987. (interactive)
  7988. (org-table-fedit-shift-reference 'left))
  7989. (defun org-table-fedit-ref-right ()
  7990. "Shift the reference at point one field to the right."
  7991. (interactive)
  7992. (org-table-fedit-shift-reference 'right))
  7993. (defun org-table-fedit-shift-reference (dir)
  7994. (cond
  7995. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  7996. (if (memq dir '(left right))
  7997. (org-rematch-and-replace 1 (eq dir 'left))
  7998. (error "Cannot shift reference in this direction")))
  7999. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  8000. ;; A B3-like reference
  8001. (if (memq dir '(up down))
  8002. (org-rematch-and-replace 2 (eq dir 'up))
  8003. (org-rematch-and-replace 1 (eq dir 'left))))
  8004. ((org-at-regexp-p
  8005. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  8006. ;; An internal reference
  8007. (if (memq dir '(up down))
  8008. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  8009. (org-rematch-and-replace 5 (eq dir 'left))))))
  8010. (defun org-rematch-and-replace (n &optional decr hline)
  8011. "Re-match the group N, and replace it with the shifted refrence."
  8012. (or (match-end n) (error "Cannot shift reference in this direction"))
  8013. (goto-char (match-beginning n))
  8014. (and (looking-at (regexp-quote (match-string n)))
  8015. (replace-match (org-shift-refpart (match-string 0) decr hline)
  8016. t t)))
  8017. (defun org-shift-refpart (ref &optional decr hline)
  8018. "Shift a refrence part REF.
  8019. If DECR is set, decrease the references row/column, else increase.
  8020. If HLINE is set, this may be a hline reference, it certainly is not
  8021. a translation reference."
  8022. (save-match-data
  8023. (let* ((sign (string-match "^[-+]" ref)) n)
  8024. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  8025. (cond
  8026. ((and hline (string-match "^I+" ref))
  8027. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  8028. (setq n (+ n (if decr -1 1)))
  8029. (if (= n 0) (setq n (+ n (if decr -1 1))))
  8030. (if sign
  8031. (setq sign (if (< n 0) "-" "+") n (abs n))
  8032. (setq n (max 1 n)))
  8033. (concat sign (make-string n ?I)))
  8034. ((string-match "^[0-9]+" ref)
  8035. (setq n (string-to-number (concat sign ref)))
  8036. (setq n (+ n (if decr -1 1)))
  8037. (if sign
  8038. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  8039. (number-to-string (max 1 n))))
  8040. ((string-match "^[a-zA-Z]+" ref)
  8041. (org-number-to-letters
  8042. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  8043. (t (error "Cannot shift reference"))))))
  8044. (defun org-table-fedit-toggle-coordinates ()
  8045. "Toggle the display of coordinates in the refrenced table."
  8046. (interactive)
  8047. (let ((pos (marker-position org-pos)))
  8048. (with-current-buffer (marker-buffer org-pos)
  8049. (save-excursion
  8050. (goto-char pos)
  8051. (org-table-toggle-coordinate-overlays)))))
  8052. (defun org-table-fedit-finish (&optional arg)
  8053. "Parse the buffer for formula definitions and install them.
  8054. With prefix ARG, apply the new formulas to the table."
  8055. (interactive "P")
  8056. (org-table-remove-rectangle-highlight)
  8057. (if org-table-use-standard-references
  8058. (progn
  8059. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  8060. (setq org-table-buffer-is-an nil)))
  8061. (let ((pos org-pos) eql var form)
  8062. (goto-char (point-min))
  8063. (while (re-search-forward
  8064. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  8065. nil t)
  8066. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  8067. form (match-string 3))
  8068. (setq form (org-trim form))
  8069. (when (not (equal form ""))
  8070. (while (string-match "[ \t]*\n[ \t]*" form)
  8071. (setq form (replace-match " " t t form)))
  8072. (when (assoc var eql)
  8073. (error "Double formulas for %s" var))
  8074. (push (cons var form) eql)))
  8075. (setq org-pos nil)
  8076. (set-window-configuration org-window-configuration)
  8077. (select-window (get-buffer-window (marker-buffer pos)))
  8078. (goto-char pos)
  8079. (unless (org-at-table-p)
  8080. (error "Lost table position - cannot install formulae"))
  8081. (org-table-store-formulas eql)
  8082. (move-marker pos nil)
  8083. (kill-buffer "*Edit Formulas*")
  8084. (if arg
  8085. (org-table-recalculate 'all)
  8086. (message "New formulas installed - press C-u C-c C-c to apply."))))
  8087. (defun org-table-fedit-abort ()
  8088. "Abort editing formulas, without installing the changes."
  8089. (interactive)
  8090. (org-table-remove-rectangle-highlight)
  8091. (let ((pos org-pos))
  8092. (set-window-configuration org-window-configuration)
  8093. (select-window (get-buffer-window (marker-buffer pos)))
  8094. (goto-char pos)
  8095. (move-marker pos nil)
  8096. (message "Formula editing aborted without installing changes")))
  8097. (defun org-table-fedit-lisp-indent ()
  8098. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  8099. (interactive)
  8100. (let ((pos (point)) beg end ind)
  8101. (beginning-of-line 1)
  8102. (cond
  8103. ((looking-at "[ \t]")
  8104. (goto-char pos)
  8105. (call-interactively 'lisp-indent-line))
  8106. ((looking-at "[$@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  8107. ((not (fboundp 'pp-buffer))
  8108. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  8109. ((looking-at "[$@0-9a-zA-Z]+ *= *'(")
  8110. (goto-char (- (match-end 0) 2))
  8111. (setq beg (point))
  8112. (setq ind (make-string (current-column) ?\ ))
  8113. (condition-case nil (forward-sexp 1)
  8114. (error
  8115. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  8116. (setq end (point))
  8117. (save-restriction
  8118. (narrow-to-region beg end)
  8119. (if (eq last-command this-command)
  8120. (progn
  8121. (goto-char (point-min))
  8122. (setq this-command nil)
  8123. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  8124. (replace-match " ")))
  8125. (pp-buffer)
  8126. (untabify (point-min) (point-max))
  8127. (goto-char (1+ (point-min)))
  8128. (while (re-search-forward "^." nil t)
  8129. (beginning-of-line 1)
  8130. (insert ind))
  8131. (goto-char (point-max))
  8132. (backward-delete-char 1)))
  8133. (goto-char beg))
  8134. (t nil))))
  8135. (defvar org-show-positions nil)
  8136. (defun org-table-show-reference (&optional local)
  8137. "Show the location/value of the $ expression at point."
  8138. (interactive)
  8139. (org-table-remove-rectangle-highlight)
  8140. (catch 'exit
  8141. (let ((pos (if local (point) org-pos))
  8142. (face2 'highlight)
  8143. (org-inhibit-highlight-removal t)
  8144. (win (selected-window))
  8145. (org-show-positions nil)
  8146. var name e what match dest)
  8147. (if local (org-table-get-specials))
  8148. (setq what (cond
  8149. ((or (org-at-regexp-p org-table-range-regexp2)
  8150. (org-at-regexp-p org-table-translate-regexp)
  8151. (org-at-regexp-p org-table-range-regexp))
  8152. (setq match
  8153. (save-match-data
  8154. (org-table-convert-refs-to-rc (match-string 0))))
  8155. 'range)
  8156. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  8157. ((org-at-regexp-p "\\$[0-9]+") 'column)
  8158. ((not local) nil)
  8159. (t (error "No reference at point")))
  8160. match (and what (or match (match-string 0))))
  8161. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  8162. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  8163. 'secondary-selection))
  8164. (org-add-hook 'before-change-functions
  8165. 'org-table-remove-rectangle-highlight)
  8166. (if (eq what 'name) (setq var (substring match 1)))
  8167. (when (eq what 'range)
  8168. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  8169. (setq match (org-table-formula-substitute-names match)))
  8170. (unless local
  8171. (save-excursion
  8172. (end-of-line 1)
  8173. (re-search-backward "^\\S-" nil t)
  8174. (beginning-of-line 1)
  8175. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  8176. (setq dest
  8177. (save-match-data
  8178. (org-table-convert-refs-to-rc (match-string 1))))
  8179. (org-table-add-rectangle-overlay
  8180. (match-beginning 1) (match-end 1) face2))))
  8181. (if (and (markerp pos) (marker-buffer pos))
  8182. (if (get-buffer-window (marker-buffer pos))
  8183. (select-window (get-buffer-window (marker-buffer pos)))
  8184. (switch-to-buffer-other-window (get-buffer-window
  8185. (marker-buffer pos)))))
  8186. (goto-char pos)
  8187. (org-table-force-dataline)
  8188. (when dest
  8189. (setq name (substring dest 1))
  8190. (cond
  8191. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  8192. (setq e (assoc name org-table-named-field-locations))
  8193. (goto-line (nth 1 e))
  8194. (org-table-goto-column (nth 2 e)))
  8195. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  8196. (let ((l (string-to-number (match-string 1 dest)))
  8197. (c (string-to-number (match-string 2 dest))))
  8198. (goto-line (aref org-table-dlines l))
  8199. (org-table-goto-column c)))
  8200. (t (org-table-goto-column (string-to-number name))))
  8201. (move-marker pos (point))
  8202. (org-table-highlight-rectangle nil nil face2))
  8203. (cond
  8204. ((equal dest match))
  8205. ((not match))
  8206. ((eq what 'range)
  8207. (condition-case nil
  8208. (save-excursion
  8209. (org-table-get-range match nil nil 'highlight))
  8210. (error nil)))
  8211. ((setq e (assoc var org-table-named-field-locations))
  8212. (goto-line (nth 1 e))
  8213. (org-table-goto-column (nth 2 e))
  8214. (org-table-highlight-rectangle (point) (point))
  8215. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  8216. ((setq e (assoc var org-table-column-names))
  8217. (org-table-goto-column (string-to-number (cdr e)))
  8218. (org-table-highlight-rectangle (point) (point))
  8219. (goto-char (org-table-begin))
  8220. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  8221. (org-table-end) t)
  8222. (progn
  8223. (goto-char (match-beginning 1))
  8224. (org-table-highlight-rectangle)
  8225. (message "Named column (column %s)" (cdr e)))
  8226. (error "Column name not found")))
  8227. ((eq what 'column)
  8228. ;; column number
  8229. (org-table-goto-column (string-to-number (substring match 1)))
  8230. (org-table-highlight-rectangle (point) (point))
  8231. (message "Column %s" (substring match 1)))
  8232. ((setq e (assoc var org-table-local-parameters))
  8233. (goto-char (org-table-begin))
  8234. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  8235. (progn
  8236. (goto-char (match-beginning 1))
  8237. (org-table-highlight-rectangle)
  8238. (message "Local parameter."))
  8239. (error "Parameter not found")))
  8240. (t
  8241. (cond
  8242. ((not var) (error "No reference at point"))
  8243. ((setq e (assoc var org-table-formula-constants))
  8244. (message "Constant: $%s=%s in `org-table-formula-constants'."
  8245. var (cdr e)))
  8246. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  8247. (message "Constant: $%s=%s, from `constants.el'%s."
  8248. var e (format " (%s units)" constants-unit-system)))
  8249. (t (error "Undefined name $%s" var)))))
  8250. (goto-char pos)
  8251. (when (and org-show-positions
  8252. (not (memq this-command '(org-table-fedit-scroll
  8253. org-table-fedit-scroll-down))))
  8254. (push pos org-show-positions)
  8255. (push org-table-current-begin-pos org-show-positions)
  8256. (let ((min (apply 'min org-show-positions))
  8257. (max (apply 'max org-show-positions)))
  8258. (goto-char min) (recenter 0)
  8259. (goto-char max)
  8260. (or (pos-visible-in-window-p max) (recenter -1))))
  8261. (select-window win))))
  8262. (defun org-table-force-dataline ()
  8263. "Make sure the cursor is in a dataline in a table."
  8264. (unless (save-excursion
  8265. (beginning-of-line 1)
  8266. (looking-at org-table-dataline-regexp))
  8267. (let* ((re org-table-dataline-regexp)
  8268. (p1 (save-excursion (re-search-forward re nil 'move)))
  8269. (p2 (save-excursion (re-search-backward re nil 'move))))
  8270. (cond ((and p1 p2)
  8271. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  8272. p1 p2)))
  8273. ((or p1 p2) (goto-char (or p1 p2)))
  8274. (t (error "No table dataline around here"))))))
  8275. (defun org-table-fedit-line-up ()
  8276. "Move cursor one line up in the window showing the table."
  8277. (interactive)
  8278. (org-table-fedit-move 'previous-line))
  8279. (defun org-table-fedit-line-down ()
  8280. "Move cursor one line down in the window showing the table."
  8281. (interactive)
  8282. (org-table-fedit-move 'next-line))
  8283. (defun org-table-fedit-move (command)
  8284. "Move the cursor in the window shoinw the table.
  8285. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  8286. (let ((org-table-allow-automatic-line-recalculation nil)
  8287. (pos org-pos) (win (selected-window)) p)
  8288. (select-window (get-buffer-window (marker-buffer org-pos)))
  8289. (setq p (point))
  8290. (call-interactively command)
  8291. (while (and (org-at-table-p)
  8292. (org-at-table-hline-p))
  8293. (call-interactively command))
  8294. (or (org-at-table-p) (goto-char p))
  8295. (move-marker pos (point))
  8296. (select-window win)))
  8297. (defun org-table-fedit-scroll (N)
  8298. (interactive "p")
  8299. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  8300. (scroll-other-window N)))
  8301. (defun org-table-fedit-scroll-down (N)
  8302. (interactive "p")
  8303. (org-table-fedit-scroll (- N)))
  8304. (defvar org-table-rectangle-overlays nil)
  8305. (defun org-table-add-rectangle-overlay (beg end &optional face)
  8306. "Add a new overlay."
  8307. (let ((ov (org-make-overlay beg end)))
  8308. (org-overlay-put ov 'face (or face 'secondary-selection))
  8309. (push ov org-table-rectangle-overlays)))
  8310. (defun org-table-highlight-rectangle (&optional beg end face)
  8311. "Highlight rectangular region in a table."
  8312. (setq beg (or beg (point)) end (or end (point)))
  8313. (let ((b (min beg end))
  8314. (e (max beg end))
  8315. l1 c1 l2 c2 tmp)
  8316. (and (boundp 'org-show-positions)
  8317. (setq org-show-positions (cons b (cons e org-show-positions))))
  8318. (goto-char (min beg end))
  8319. (setq l1 (org-current-line)
  8320. c1 (org-table-current-column))
  8321. (goto-char (max beg end))
  8322. (setq l2 (org-current-line)
  8323. c2 (org-table-current-column))
  8324. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  8325. (goto-line l1)
  8326. (beginning-of-line 1)
  8327. (loop for line from l1 to l2 do
  8328. (when (looking-at org-table-dataline-regexp)
  8329. (org-table-goto-column c1)
  8330. (skip-chars-backward "^|\n") (setq beg (point))
  8331. (org-table-goto-column c2)
  8332. (skip-chars-forward "^|\n") (setq end (point))
  8333. (org-table-add-rectangle-overlay beg end face))
  8334. (beginning-of-line 2))
  8335. (goto-char b))
  8336. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  8337. (defun org-table-remove-rectangle-highlight (&rest ignore)
  8338. "Remove the rectangle overlays."
  8339. (unless org-inhibit-highlight-removal
  8340. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  8341. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  8342. (setq org-table-rectangle-overlays nil)))
  8343. (defvar org-table-coordinate-overlays nil
  8344. "Collects the cooordinate grid overlays, so that they can be removed.")
  8345. (make-variable-buffer-local 'org-table-coordinate-overlays)
  8346. (defun org-table-overlay-coordinates ()
  8347. "Add overlays to the table at point, to show row/column coordinates."
  8348. (interactive)
  8349. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  8350. (setq org-table-coordinate-overlays nil)
  8351. (save-excursion
  8352. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  8353. (goto-char (org-table-begin))
  8354. (while (org-at-table-p)
  8355. (setq eol (point-at-eol))
  8356. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  8357. (push ov org-table-coordinate-overlays)
  8358. (setq hline (looking-at org-table-hline-regexp))
  8359. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  8360. (format "%4d" (setq id (1+ id)))))
  8361. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  8362. (when hline
  8363. (setq ic 0)
  8364. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  8365. (setq beg (1+ (match-beginning 0))
  8366. ic (1+ ic)
  8367. s1 (concat "$" (int-to-string ic))
  8368. s2 (org-number-to-letters ic)
  8369. str (if (eq org-table-use-standard-references t) s2 s1))
  8370. (setq ov (org-make-overlay beg (+ beg (length str))))
  8371. (push ov org-table-coordinate-overlays)
  8372. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  8373. (beginning-of-line 2)))))
  8374. (defun org-table-toggle-coordinate-overlays ()
  8375. "Toggle the display of Row/Column numbers in tables."
  8376. (interactive)
  8377. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  8378. (message "Row/Column number display turned %s"
  8379. (if org-table-overlay-coordinates "on" "off"))
  8380. (if (and (org-at-table-p) org-table-overlay-coordinates)
  8381. (org-table-align))
  8382. (unless org-table-overlay-coordinates
  8383. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  8384. (setq org-table-coordinate-overlays nil)))
  8385. (defun org-table-toggle-formula-debugger ()
  8386. "Toggle the formula debugger in tables."
  8387. (interactive)
  8388. (setq org-table-formula-debug (not org-table-formula-debug))
  8389. (message "Formula debugging has been turned %s"
  8390. (if org-table-formula-debug "on" "off")))
  8391. ;;; The orgtbl minor mode
  8392. ;; Define a minor mode which can be used in other modes in order to
  8393. ;; integrate the org-mode table editor.
  8394. ;; This is really a hack, because the org-mode table editor uses several
  8395. ;; keys which normally belong to the major mode, for example the TAB and
  8396. ;; RET keys. Here is how it works: The minor mode defines all the keys
  8397. ;; necessary to operate the table editor, but wraps the commands into a
  8398. ;; function which tests if the cursor is currently inside a table. If that
  8399. ;; is the case, the table editor command is executed. However, when any of
  8400. ;; those keys is used outside a table, the function uses `key-binding' to
  8401. ;; look up if the key has an associated command in another currently active
  8402. ;; keymap (minor modes, major mode, global), and executes that command.
  8403. ;; There might be problems if any of the keys used by the table editor is
  8404. ;; otherwise used as a prefix key.
  8405. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  8406. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  8407. ;; addresses this by checking explicitly for both bindings.
  8408. ;; The optimized version (see variable `orgtbl-optimized') takes over
  8409. ;; all keys which are bound to `self-insert-command' in the *global map*.
  8410. ;; Some modes bind other commands to simple characters, for example
  8411. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  8412. ;; active, this binding is ignored inside tables and replaced with a
  8413. ;; modified self-insert.
  8414. (defvar orgtbl-mode nil
  8415. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  8416. table editor in arbitrary modes.")
  8417. (make-variable-buffer-local 'orgtbl-mode)
  8418. (defvar orgtbl-mode-map (make-keymap)
  8419. "Keymap for `orgtbl-mode'.")
  8420. ;;;###autoload
  8421. (defun turn-on-orgtbl ()
  8422. "Unconditionally turn on `orgtbl-mode'."
  8423. (orgtbl-mode 1))
  8424. (defvar org-old-auto-fill-inhibit-regexp nil
  8425. "Local variable used by `orgtbl-mode'")
  8426. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  8427. "Matches a line belonging to an orgtbl.")
  8428. (defconst orgtbl-extra-font-lock-keywords
  8429. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  8430. 0 (quote 'org-table) 'prepend))
  8431. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  8432. ;;;###autoload
  8433. (defun orgtbl-mode (&optional arg)
  8434. "The `org-mode' table editor as a minor mode for use in other modes."
  8435. (interactive)
  8436. (if (org-mode-p)
  8437. ;; Exit without error, in case some hook functions calls this
  8438. ;; by accident in org-mode.
  8439. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  8440. (setq orgtbl-mode
  8441. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  8442. (if orgtbl-mode
  8443. (progn
  8444. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  8445. ;; Make sure we are first in minor-mode-map-alist
  8446. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  8447. (and c (setq minor-mode-map-alist
  8448. (cons c (delq c minor-mode-map-alist)))))
  8449. (org-set-local (quote org-table-may-need-update) t)
  8450. (org-add-hook 'before-change-functions 'org-before-change-function
  8451. nil 'local)
  8452. (org-set-local 'org-old-auto-fill-inhibit-regexp
  8453. auto-fill-inhibit-regexp)
  8454. (org-set-local 'auto-fill-inhibit-regexp
  8455. (if auto-fill-inhibit-regexp
  8456. (concat orgtbl-line-start-regexp "\\|"
  8457. auto-fill-inhibit-regexp)
  8458. orgtbl-line-start-regexp))
  8459. (org-add-to-invisibility-spec '(org-cwidth))
  8460. (when (fboundp 'font-lock-add-keywords)
  8461. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  8462. (org-restart-font-lock))
  8463. (easy-menu-add orgtbl-mode-menu)
  8464. (run-hooks 'orgtbl-mode-hook))
  8465. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  8466. (org-cleanup-narrow-column-properties)
  8467. (org-remove-from-invisibility-spec '(org-cwidth))
  8468. (remove-hook 'before-change-functions 'org-before-change-function t)
  8469. (when (fboundp 'font-lock-remove-keywords)
  8470. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  8471. (org-restart-font-lock))
  8472. (easy-menu-remove orgtbl-mode-menu)
  8473. (force-mode-line-update 'all))))
  8474. (defun org-cleanup-narrow-column-properties ()
  8475. "Remove all properties related to narrow-column invisibility."
  8476. (let ((s 1))
  8477. (while (setq s (text-property-any s (point-max)
  8478. 'display org-narrow-column-arrow))
  8479. (remove-text-properties s (1+ s) '(display t)))
  8480. (setq s 1)
  8481. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  8482. (remove-text-properties s (1+ s) '(org-cwidth t)))
  8483. (setq s 1)
  8484. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  8485. (remove-text-properties s (1+ s) '(invisible t)))))
  8486. ;; Install it as a minor mode.
  8487. (put 'orgtbl-mode :included t)
  8488. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  8489. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  8490. (defun orgtbl-make-binding (fun n &rest keys)
  8491. "Create a function for binding in the table minor mode.
  8492. FUN is the command to call inside a table. N is used to create a unique
  8493. command name. KEYS are keys that should be checked in for a command
  8494. to execute outside of tables."
  8495. (eval
  8496. (list 'defun
  8497. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  8498. '(arg)
  8499. (concat "In tables, run `" (symbol-name fun) "'.\n"
  8500. "Outside of tables, run the binding of `"
  8501. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  8502. "'.")
  8503. '(interactive "p")
  8504. (list 'if
  8505. '(org-at-table-p)
  8506. (list 'call-interactively (list 'quote fun))
  8507. (list 'let '(orgtbl-mode)
  8508. (list 'call-interactively
  8509. (append '(or)
  8510. (mapcar (lambda (k)
  8511. (list 'key-binding k))
  8512. keys)
  8513. '('orgtbl-error))))))))
  8514. (defun orgtbl-error ()
  8515. "Error when there is no default binding for a table key."
  8516. (interactive)
  8517. (error "This key is has no function outside tables"))
  8518. (defun orgtbl-setup ()
  8519. "Setup orgtbl keymaps."
  8520. (let ((nfunc 0)
  8521. (bindings
  8522. (list
  8523. '([(meta shift left)] org-table-delete-column)
  8524. '([(meta left)] org-table-move-column-left)
  8525. '([(meta right)] org-table-move-column-right)
  8526. '([(meta shift right)] org-table-insert-column)
  8527. '([(meta shift up)] org-table-kill-row)
  8528. '([(meta shift down)] org-table-insert-row)
  8529. '([(meta up)] org-table-move-row-up)
  8530. '([(meta down)] org-table-move-row-down)
  8531. '("\C-c\C-w" org-table-cut-region)
  8532. '("\C-c\M-w" org-table-copy-region)
  8533. '("\C-c\C-y" org-table-paste-rectangle)
  8534. '("\C-c-" org-table-insert-hline)
  8535. '("\C-c}" org-table-toggle-coordinate-overlays)
  8536. '("\C-c{" org-table-toggle-formula-debugger)
  8537. '("\C-m" org-table-next-row)
  8538. '([(shift return)] org-table-copy-down)
  8539. '("\C-c\C-q" org-table-wrap-region)
  8540. '("\C-c?" org-table-field-info)
  8541. '("\C-c " org-table-blank-field)
  8542. '("\C-c+" org-table-sum)
  8543. '("\C-c=" org-table-eval-formula)
  8544. '("\C-c'" org-table-edit-formulas)
  8545. '("\C-c`" org-table-edit-field)
  8546. '("\C-c*" org-table-recalculate)
  8547. '("\C-c|" org-table-create-or-convert-from-region)
  8548. '("\C-c^" org-table-sort-lines)
  8549. '([(control ?#)] org-table-rotate-recalc-marks)))
  8550. elt key fun cmd)
  8551. (while (setq elt (pop bindings))
  8552. (setq nfunc (1+ nfunc))
  8553. (setq key (org-key (car elt))
  8554. fun (nth 1 elt)
  8555. cmd (orgtbl-make-binding fun nfunc key))
  8556. (org-defkey orgtbl-mode-map key cmd))
  8557. ;; Special treatment needed for TAB and RET
  8558. (org-defkey orgtbl-mode-map [(return)]
  8559. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  8560. (org-defkey orgtbl-mode-map "\C-m"
  8561. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  8562. (org-defkey orgtbl-mode-map [(tab)]
  8563. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  8564. (org-defkey orgtbl-mode-map "\C-i"
  8565. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  8566. (org-defkey orgtbl-mode-map [(shift tab)]
  8567. (orgtbl-make-binding 'org-table-previous-field 104
  8568. [(shift tab)] [(tab)] "\C-i"))
  8569. (org-defkey orgtbl-mode-map "\M-\C-m"
  8570. (orgtbl-make-binding 'org-table-wrap-region 105
  8571. "\M-\C-m" [(meta return)]))
  8572. (org-defkey orgtbl-mode-map [(meta return)]
  8573. (orgtbl-make-binding 'org-table-wrap-region 106
  8574. [(meta return)] "\M-\C-m"))
  8575. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  8576. (when orgtbl-optimized
  8577. ;; If the user wants maximum table support, we need to hijack
  8578. ;; some standard editing functions
  8579. (org-remap orgtbl-mode-map
  8580. 'self-insert-command 'orgtbl-self-insert-command
  8581. 'delete-char 'org-delete-char
  8582. 'delete-backward-char 'org-delete-backward-char)
  8583. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  8584. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  8585. '("OrgTbl"
  8586. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  8587. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  8588. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  8589. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  8590. "--"
  8591. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  8592. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  8593. ["Copy Field from Above"
  8594. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  8595. "--"
  8596. ("Column"
  8597. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  8598. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  8599. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  8600. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  8601. ("Row"
  8602. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  8603. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  8604. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  8605. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  8606. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  8607. "--"
  8608. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  8609. ("Rectangle"
  8610. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  8611. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  8612. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  8613. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  8614. "--"
  8615. ("Radio tables"
  8616. ["Insert table template" orgtbl-insert-radio-table
  8617. (assq major-mode orgtbl-radio-table-templates)]
  8618. ["Comment/uncomment table" orgtbl-toggle-comment t])
  8619. "--"
  8620. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  8621. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  8622. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  8623. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  8624. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  8625. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  8626. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  8627. ["Sum Column/Rectangle" org-table-sum
  8628. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  8629. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  8630. ["Debug Formulas"
  8631. org-table-toggle-formula-debugger :active (org-at-table-p)
  8632. :keys "C-c {"
  8633. :style toggle :selected org-table-formula-debug]
  8634. ["Show Col/Row Numbers"
  8635. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  8636. :keys "C-c }"
  8637. :style toggle :selected org-table-overlay-coordinates]
  8638. ))
  8639. t))
  8640. (defun orgtbl-ctrl-c-ctrl-c (arg)
  8641. "If the cursor is inside a table, realign the table.
  8642. It it is a table to be sent away to a receiver, do it.
  8643. With prefix arg, also recompute table."
  8644. (interactive "P")
  8645. (let ((pos (point)) action)
  8646. (save-excursion
  8647. (beginning-of-line 1)
  8648. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  8649. ((looking-at "[ \t]*|") pos)
  8650. ((looking-at "#\\+TBLFM:") 'recalc))))
  8651. (cond
  8652. ((integerp action)
  8653. (goto-char action)
  8654. (org-table-maybe-eval-formula)
  8655. (if arg
  8656. (call-interactively 'org-table-recalculate)
  8657. (org-table-maybe-recalculate-line))
  8658. (call-interactively 'org-table-align)
  8659. (orgtbl-send-table 'maybe))
  8660. ((eq action 'recalc)
  8661. (save-excursion
  8662. (beginning-of-line 1)
  8663. (skip-chars-backward " \r\n\t")
  8664. (if (org-at-table-p)
  8665. (org-call-with-arg 'org-table-recalculate t))))
  8666. (t (let (orgtbl-mode)
  8667. (call-interactively (key-binding "\C-c\C-c")))))))
  8668. (defun orgtbl-tab (arg)
  8669. "Justification and field motion for `orgtbl-mode'."
  8670. (interactive "P")
  8671. (if arg (org-table-edit-field t)
  8672. (org-table-justify-field-maybe)
  8673. (org-table-next-field)))
  8674. (defun orgtbl-ret ()
  8675. "Justification and field motion for `orgtbl-mode'."
  8676. (interactive)
  8677. (org-table-justify-field-maybe)
  8678. (org-table-next-row))
  8679. (defun orgtbl-self-insert-command (N)
  8680. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  8681. If the cursor is in a table looking at whitespace, the whitespace is
  8682. overwritten, and the table is not marked as requiring realignment."
  8683. (interactive "p")
  8684. (if (and (org-at-table-p)
  8685. (or
  8686. (and org-table-auto-blank-field
  8687. (member last-command
  8688. '(orgtbl-hijacker-command-100
  8689. orgtbl-hijacker-command-101
  8690. orgtbl-hijacker-command-102
  8691. orgtbl-hijacker-command-103
  8692. orgtbl-hijacker-command-104
  8693. orgtbl-hijacker-command-105))
  8694. (org-table-blank-field))
  8695. t)
  8696. (eq N 1)
  8697. (looking-at "[^|\n]* +|"))
  8698. (let (org-table-may-need-update)
  8699. (goto-char (1- (match-end 0)))
  8700. (delete-backward-char 1)
  8701. (goto-char (match-beginning 0))
  8702. (self-insert-command N))
  8703. (setq org-table-may-need-update t)
  8704. (let (orgtbl-mode)
  8705. (call-interactively (key-binding (vector last-input-event))))))
  8706. (defun org-force-self-insert (N)
  8707. "Needed to enforce self-insert under remapping."
  8708. (interactive "p")
  8709. (self-insert-command N))
  8710. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  8711. "Regula expression matching exponentials as produced by calc.")
  8712. (defvar org-table-clean-did-remove-column nil)
  8713. (defun orgtbl-export (table target)
  8714. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  8715. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  8716. org-table-last-alignment org-table-last-column-widths
  8717. maxcol column)
  8718. (if (not (fboundp func))
  8719. (error "Cannot export orgtbl table to %s" target))
  8720. (setq lines (org-table-clean-before-export lines))
  8721. (setq table
  8722. (mapcar
  8723. (lambda (x)
  8724. (if (string-match org-table-hline-regexp x)
  8725. 'hline
  8726. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  8727. lines))
  8728. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  8729. table)))
  8730. (loop for i from (1- maxcol) downto 0 do
  8731. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  8732. (setq column (delq nil column))
  8733. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  8734. (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))
  8735. (funcall func table nil)))
  8736. (defun orgtbl-send-table (&optional maybe)
  8737. "Send a tranformed version of this table to the receiver position.
  8738. With argument MAYBE, fail quietly if no transformation is defined for
  8739. this table."
  8740. (interactive)
  8741. (catch 'exit
  8742. (unless (org-at-table-p) (error "Not at a table"))
  8743. ;; when non-interactive, we assume align has just happened.
  8744. (when (interactive-p) (org-table-align))
  8745. (save-excursion
  8746. (goto-char (org-table-begin))
  8747. (beginning-of-line 0)
  8748. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  8749. (if maybe
  8750. (throw 'exit nil)
  8751. (error "Don't know how to transform this table."))))
  8752. (let* ((name (match-string 1))
  8753. beg
  8754. (transform (intern (match-string 2)))
  8755. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  8756. (skip (plist-get params :skip))
  8757. (skipcols (plist-get params :skipcols))
  8758. (txt (buffer-substring-no-properties
  8759. (org-table-begin) (org-table-end)))
  8760. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  8761. (lines (org-table-clean-before-export lines))
  8762. (i0 (if org-table-clean-did-remove-column 2 1))
  8763. (table (mapcar
  8764. (lambda (x)
  8765. (if (string-match org-table-hline-regexp x)
  8766. 'hline
  8767. (org-remove-by-index
  8768. (org-split-string (org-trim x) "\\s-*|\\s-*")
  8769. skipcols i0)))
  8770. lines))
  8771. (fun (if (= i0 2) 'cdr 'identity))
  8772. (org-table-last-alignment
  8773. (org-remove-by-index (funcall fun org-table-last-alignment)
  8774. skipcols i0))
  8775. (org-table-last-column-widths
  8776. (org-remove-by-index (funcall fun org-table-last-column-widths)
  8777. skipcols i0)))
  8778. (unless (fboundp transform)
  8779. (error "No such transformation function %s" transform))
  8780. (setq txt (funcall transform table params))
  8781. ;; Find the insertion place
  8782. (save-excursion
  8783. (goto-char (point-min))
  8784. (unless (re-search-forward
  8785. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  8786. (error "Don't know where to insert translated table"))
  8787. (goto-char (match-beginning 0))
  8788. (beginning-of-line 2)
  8789. (setq beg (point))
  8790. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  8791. (error "Cannot find end of insertion region"))
  8792. (beginning-of-line 1)
  8793. (delete-region beg (point))
  8794. (goto-char beg)
  8795. (insert txt "\n"))
  8796. (message "Table converted and installed at receiver location"))))
  8797. (defun org-remove-by-index (list indices &optional i0)
  8798. "Remove the elements in LIST with indices in INDICES.
  8799. First element has index 0, or I0 if given."
  8800. (if (not indices)
  8801. list
  8802. (if (integerp indices) (setq indices (list indices)))
  8803. (setq i0 (1- (or i0 0)))
  8804. (delq :rm (mapcar (lambda (x)
  8805. (setq i0 (1+ i0))
  8806. (if (memq i0 indices) :rm x))
  8807. list))))
  8808. (defun orgtbl-toggle-comment ()
  8809. "Comment or uncomment the orgtbl at point."
  8810. (interactive)
  8811. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  8812. (re2 (concat "^" orgtbl-line-start-regexp))
  8813. (commented (save-excursion (beginning-of-line 1)
  8814. (cond ((looking-at re1) t)
  8815. ((looking-at re2) nil)
  8816. (t (error "Not at an org table")))))
  8817. (re (if commented re1 re2))
  8818. beg end)
  8819. (save-excursion
  8820. (beginning-of-line 1)
  8821. (while (looking-at re) (beginning-of-line 0))
  8822. (beginning-of-line 2)
  8823. (setq beg (point))
  8824. (while (looking-at re) (beginning-of-line 2))
  8825. (setq end (point)))
  8826. (comment-region beg end (if commented '(4) nil))))
  8827. (defun orgtbl-insert-radio-table ()
  8828. "Insert a radio table template appropriate for this major mode."
  8829. (interactive)
  8830. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  8831. (txt (nth 1 e))
  8832. name pos)
  8833. (unless e (error "No radio table setup defined for %s" major-mode))
  8834. (setq name (read-string "Table name: "))
  8835. (while (string-match "%n" txt)
  8836. (setq txt (replace-match name t t txt)))
  8837. (or (bolp) (insert "\n"))
  8838. (setq pos (point))
  8839. (insert txt)
  8840. (goto-char pos)))
  8841. (defun org-get-param (params header i sym &optional hsym)
  8842. "Get parameter value for symbol SYM.
  8843. If this is a header line, actually get the value for the symbol with an
  8844. additional \"h\" inserted after the colon.
  8845. If the value is a protperty list, get the element for the current column.
  8846. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  8847. (let ((val (plist-get params sym)))
  8848. (and hsym header (setq val (or (plist-get params hsym) val)))
  8849. (if (consp val) (plist-get val i) val)))
  8850. (defun orgtbl-to-generic (table params)
  8851. "Convert the orgtbl-mode TABLE to some other format.
  8852. This generic routine can be used for many standard cases.
  8853. TABLE is a list, each entry either the symbol `hline' for a horizontal
  8854. separator line, or a list of fields for that line.
  8855. PARAMS is a property list of parameters that can influence the conversion.
  8856. For the generic converter, some parameters are obligatory: You need to
  8857. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  8858. :splice, you must have :tstart and :tend.
  8859. Valid parameters are
  8860. :tstart String to start the table. Ignored when :splice is t.
  8861. :tend String to end the table. Ignored when :splice is t.
  8862. :splice When set to t, return only table body lines, don't wrap
  8863. them into :tstart and :tend. Default is nil.
  8864. :hline String to be inserted on horizontal separation lines.
  8865. May be nil to ignore hlines.
  8866. :lstart String to start a new table line.
  8867. :lend String to end a table line
  8868. :sep Separator between two fields
  8869. :lfmt Format for entire line, with enough %s to capture all fields.
  8870. If this is present, :lstart, :lend, and :sep are ignored.
  8871. :fmt A format to be used to wrap the field, should contain
  8872. %s for the original field value. For example, to wrap
  8873. everything in dollars, you could use :fmt \"$%s$\".
  8874. This may also be a property list with column numbers and
  8875. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  8876. :hlstart :hlend :hlsep :hlfmt :hfmt
  8877. Same as above, specific for the header lines in the table.
  8878. All lines before the first hline are treated as header.
  8879. If any of these is not present, the data line value is used.
  8880. :efmt Use this format to print numbers with exponentials.
  8881. The format should have %s twice for inserting mantissa
  8882. and exponent, for example \"%s\\\\times10^{%s}\". This
  8883. may also be a property list with column numbers and
  8884. formats. :fmt will still be applied after :efmt.
  8885. In addition to this, the parameters :skip and :skipcols are always handled
  8886. directly by `orgtbl-send-table'. See manual."
  8887. (interactive)
  8888. (let* ((p params)
  8889. (splicep (plist-get p :splice))
  8890. (hline (plist-get p :hline))
  8891. rtn line i fm efm lfmt h)
  8892. ;; Do we have a header?
  8893. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  8894. (setq h t))
  8895. ;; Put header
  8896. (unless splicep
  8897. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  8898. ;; Now loop over all lines
  8899. (while (setq line (pop table))
  8900. (if (eq line 'hline)
  8901. ;; A horizontal separator line
  8902. (progn (if hline (push hline rtn))
  8903. (setq h nil)) ; no longer in header
  8904. ;; A normal line. Convert the fields, push line onto the result list
  8905. (setq i 0)
  8906. (setq line
  8907. (mapcar
  8908. (lambda (f)
  8909. (setq i (1+ i)
  8910. fm (org-get-param p h i :fmt :hfmt)
  8911. efm (org-get-param p h i :efmt))
  8912. (if (and efm (string-match orgtbl-exp-regexp f))
  8913. (setq f (format
  8914. efm (match-string 1 f) (match-string 2 f))))
  8915. (if fm (setq f (format fm f)))
  8916. f)
  8917. line))
  8918. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  8919. (push (apply 'format lfmt line) rtn)
  8920. (push (concat
  8921. (org-get-param p h i :lstart :hlstart)
  8922. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  8923. (org-get-param p h i :lend :hlend))
  8924. rtn))))
  8925. (unless splicep
  8926. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  8927. (mapconcat 'identity (nreverse rtn) "\n")))
  8928. (defun orgtbl-to-latex (table params)
  8929. "Convert the orgtbl-mode TABLE to LaTeX.
  8930. TABLE is a list, each entry either the symbol `hline' for a horizontal
  8931. separator line, or a list of fields for that line.
  8932. PARAMS is a property list of parameters that can influence the conversion.
  8933. Supports all parameters from `orgtbl-to-generic'. Most important for
  8934. LaTeX are:
  8935. :splice When set to t, return only table body lines, don't wrap
  8936. them into a tabular environment. Default is nil.
  8937. :fmt A format to be used to wrap the field, should contain %s for the
  8938. original field value. For example, to wrap everything in dollars,
  8939. use :fmt \"$%s$\". This may also be a property list with column
  8940. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  8941. :efmt Format for transforming numbers with exponentials. The format
  8942. should have %s twice for inserting mantissa and exponent, for
  8943. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  8944. This may also be a property list with column numbers and formats.
  8945. The general parameters :skip and :skipcols have already been applied when
  8946. this function is called."
  8947. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  8948. org-table-last-alignment ""))
  8949. (params2
  8950. (list
  8951. :tstart (concat "\\begin{tabular}{" alignment "}")
  8952. :tend "\\end{tabular}"
  8953. :lstart "" :lend " \\\\" :sep " & "
  8954. :efmt "%s\\,(%s)" :hline "\\hline")))
  8955. (orgtbl-to-generic table (org-combine-plists params2 params))))
  8956. (defun orgtbl-to-html (table params)
  8957. "Convert the orgtbl-mode TABLE to LaTeX.
  8958. TABLE is a list, each entry either the symbol `hline' for a horizontal
  8959. separator line, or a list of fields for that line.
  8960. PARAMS is a property list of parameters that can influence the conversion.
  8961. Currently this function recognizes the following parameters:
  8962. :splice When set to t, return only table body lines, don't wrap
  8963. them into a <table> environment. Default is nil.
  8964. The general parameters :skip and :skipcols have already been applied when
  8965. this function is called. The function does *not* use `orgtbl-to-generic',
  8966. so you cannot specify parameters for it."
  8967. (let* ((splicep (plist-get params :splice))
  8968. html)
  8969. ;; Just call the formatter we already have
  8970. ;; We need to make text lines for it, so put the fields back together.
  8971. (setq html (org-format-org-table-html
  8972. (mapcar
  8973. (lambda (x)
  8974. (if (eq x 'hline)
  8975. "|----+----|"
  8976. (concat "| " (mapconcat 'identity x " | ") " |")))
  8977. table)
  8978. splicep))
  8979. (if (string-match "\n+\\'" html)
  8980. (setq html (replace-match "" t t html)))
  8981. html))
  8982. (defun orgtbl-to-texinfo (table params)
  8983. "Convert the orgtbl-mode TABLE to TeXInfo.
  8984. TABLE is a list, each entry either the symbol `hline' for a horizontal
  8985. separator line, or a list of fields for that line.
  8986. PARAMS is a property list of parameters that can influence the conversion.
  8987. Supports all parameters from `orgtbl-to-generic'. Most important for
  8988. TeXInfo are:
  8989. :splice nil/t When set to t, return only table body lines, don't wrap
  8990. them into a multitable environment. Default is nil.
  8991. :fmt fmt A format to be used to wrap the field, should contain
  8992. %s for the original field value. For example, to wrap
  8993. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  8994. This may also be a property list with column numbers and
  8995. formats. for example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  8996. :cf \"f1 f2..\" The column fractions for the table. Bye default these
  8997. are computed automatically from the width of the columns
  8998. under org-mode.
  8999. The general parameters :skip and :skipcols have already been applied when
  9000. this function is called."
  9001. (let* ((total (float (apply '+ org-table-last-column-widths)))
  9002. (colfrac (or (plist-get params :cf)
  9003. (mapconcat
  9004. (lambda (x) (format "%.3f" (/ (float x) total)))
  9005. org-table-last-column-widths " ")))
  9006. (params2
  9007. (list
  9008. :tstart (concat "@multitable @columnfractions " colfrac)
  9009. :tend "@end multitable"
  9010. :lstart "@item " :lend "" :sep " @tab "
  9011. :hlstart "@headitem ")))
  9012. (orgtbl-to-generic table (org-combine-plists params2 params))))
  9013. ;;;; Link Stuff
  9014. ;;; Link abbreviations
  9015. (defun org-link-expand-abbrev (link)
  9016. "Apply replacements as defined in `org-link-abbrev-alist."
  9017. (if (string-match "^\\([a-zA-Z]+\\)\\(::?\\(.*\\)\\)?$" link)
  9018. (let* ((key (match-string 1 link))
  9019. (as (or (assoc key org-link-abbrev-alist-local)
  9020. (assoc key org-link-abbrev-alist)))
  9021. (tag (and (match-end 2) (match-string 3 link)))
  9022. rpl)
  9023. (if (not as)
  9024. link
  9025. (setq rpl (cdr as))
  9026. (cond
  9027. ((symbolp rpl) (funcall rpl tag))
  9028. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  9029. (t (concat rpl tag)))))
  9030. link))
  9031. ;;; Storing and inserting links
  9032. (defvar org-insert-link-history nil
  9033. "Minibuffer history for links inserted with `org-insert-link'.")
  9034. (defvar org-stored-links nil
  9035. "Contains the links stored with `org-store-link'.")
  9036. (defvar org-store-link-plist nil
  9037. "Plist with info about the most recently link created with `org-store-link'.")
  9038. ;;;###autoload
  9039. (defun org-store-link (arg)
  9040. "\\<org-mode-map>Store an org-link to the current location.
  9041. This link can later be inserted into an org-buffer with
  9042. \\[org-insert-link].
  9043. For some link types, a prefix arg is interpreted:
  9044. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  9045. For file links, arg negates `org-context-in-file-links'."
  9046. (interactive "P")
  9047. (setq org-store-link-plist nil) ; reset
  9048. (let (link cpltxt desc description search txt)
  9049. (cond
  9050. ((eq major-mode 'bbdb-mode)
  9051. (let ((name (bbdb-record-name (bbdb-current-record)))
  9052. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  9053. (setq cpltxt (concat "bbdb:" (or name company))
  9054. link (org-make-link cpltxt))
  9055. (org-store-link-props :type "bbdb" :name name :company company)))
  9056. ((eq major-mode 'Info-mode)
  9057. (setq link (org-make-link "info:"
  9058. (file-name-nondirectory Info-current-file)
  9059. ":" Info-current-node))
  9060. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  9061. ":" Info-current-node))
  9062. (org-store-link-props :type "info" :file Info-current-file
  9063. :node Info-current-node))
  9064. ((eq major-mode 'calendar-mode)
  9065. (let ((cd (calendar-cursor-to-date)))
  9066. (setq link
  9067. (format-time-string
  9068. (car org-time-stamp-formats)
  9069. (apply 'encode-time
  9070. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  9071. nil nil nil))))
  9072. (org-store-link-props :type "calendar" :date cd)))
  9073. ((or (eq major-mode 'vm-summary-mode)
  9074. (eq major-mode 'vm-presentation-mode))
  9075. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  9076. (vm-follow-summary-cursor)
  9077. (save-excursion
  9078. (vm-select-folder-buffer)
  9079. (let* ((message (car vm-message-pointer))
  9080. (folder buffer-file-name)
  9081. (subject (vm-su-subject message))
  9082. (to (vm-get-header-contents message "To"))
  9083. (from (vm-get-header-contents message "From"))
  9084. (message-id (vm-su-message-id message)))
  9085. (org-store-link-props :type "vm" :from from :to to :subject subject
  9086. :message-id message-id)
  9087. (setq message-id (org-remove-angle-brackets message-id))
  9088. (setq folder (abbreviate-file-name folder))
  9089. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  9090. folder)
  9091. (setq folder (replace-match "" t t folder)))
  9092. (setq cpltxt (org-email-link-description))
  9093. (setq link (org-make-link "vm:" folder "#" message-id)))))
  9094. ((eq major-mode 'wl-summary-mode)
  9095. (let* ((msgnum (wl-summary-message-number))
  9096. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  9097. msgnum 'message-id))
  9098. (wl-message-entity
  9099. (if (fboundp 'elmo-message-entity)
  9100. (elmo-message-entity
  9101. wl-summary-buffer-elmo-folder msgnum)
  9102. (elmo-msgdb-overview-get-entity
  9103. msgnum (wl-summary-buffer-msgdb))))
  9104. (from (wl-summary-line-from))
  9105. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  9106. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  9107. (wl-summary-line-subject))))
  9108. (org-store-link-props :type "wl" :from from :to to
  9109. :subject subject :message-id message-id)
  9110. (setq message-id (org-remove-angle-brackets message-id))
  9111. (setq cpltxt (org-email-link-description))
  9112. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  9113. "#" message-id))))
  9114. ((or (equal major-mode 'mh-folder-mode)
  9115. (equal major-mode 'mh-show-mode))
  9116. (let ((from (org-mhe-get-header "From:"))
  9117. (to (org-mhe-get-header "To:"))
  9118. (message-id (org-mhe-get-header "Message-Id:"))
  9119. (subject (org-mhe-get-header "Subject:")))
  9120. (org-store-link-props :type "mh" :from from :to to
  9121. :subject subject :message-id message-id)
  9122. (setq cpltxt (org-email-link-description))
  9123. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  9124. (org-remove-angle-brackets message-id)))))
  9125. ((eq major-mode 'rmail-mode)
  9126. (save-excursion
  9127. (save-restriction
  9128. (rmail-narrow-to-non-pruned-header)
  9129. (let ((folder buffer-file-name)
  9130. (message-id (mail-fetch-field "message-id"))
  9131. (from (mail-fetch-field "from"))
  9132. (to (mail-fetch-field "to"))
  9133. (subject (mail-fetch-field "subject")))
  9134. (org-store-link-props
  9135. :type "rmail" :from from :to to
  9136. :subject subject :message-id message-id)
  9137. (setq message-id (org-remove-angle-brackets message-id))
  9138. (setq cpltxt (org-email-link-description))
  9139. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  9140. ((eq major-mode 'gnus-group-mode)
  9141. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  9142. (gnus-group-group-name)) ; version
  9143. ((fboundp 'gnus-group-name)
  9144. (gnus-group-name))
  9145. (t "???"))))
  9146. (unless group (error "Not on a group"))
  9147. (org-store-link-props :type "gnus" :group group)
  9148. (setq cpltxt (concat
  9149. (if (org-xor arg org-usenet-links-prefer-google)
  9150. "http://groups.google.com/groups?group="
  9151. "gnus:")
  9152. group)
  9153. link (org-make-link cpltxt))))
  9154. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  9155. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  9156. (let* ((group gnus-newsgroup-name)
  9157. (article (gnus-summary-article-number))
  9158. (header (gnus-summary-article-header article))
  9159. (from (mail-header-from header))
  9160. (message-id (mail-header-id header))
  9161. (date (mail-header-date header))
  9162. (subject (gnus-summary-subject-string)))
  9163. (org-store-link-props :type "gnus" :from from :subject subject
  9164. :message-id message-id :group group)
  9165. (setq cpltxt (org-email-link-description))
  9166. (if (org-xor arg org-usenet-links-prefer-google)
  9167. (setq link
  9168. (concat
  9169. cpltxt "\n "
  9170. (format "http://groups.google.com/groups?as_umsgid=%s"
  9171. (org-fixup-message-id-for-http message-id))))
  9172. (setq link (org-make-link "gnus:" group
  9173. "#" (number-to-string article))))))
  9174. ((eq major-mode 'w3-mode)
  9175. (setq cpltxt (url-view-url t)
  9176. link (org-make-link cpltxt))
  9177. (org-store-link-props :type "w3" :url (url-view-url t)))
  9178. ((eq major-mode 'w3m-mode)
  9179. (setq cpltxt (or w3m-current-title w3m-current-url)
  9180. link (org-make-link w3m-current-url))
  9181. (org-store-link-props :type "w3m" :url (url-view-url t)))
  9182. ((setq search (run-hook-with-args-until-success
  9183. 'org-create-file-search-functions))
  9184. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  9185. "::" search))
  9186. (setq cpltxt (or description link)))
  9187. ((eq major-mode 'image-mode)
  9188. (setq cpltxt (concat "file:"
  9189. (abbreviate-file-name buffer-file-name))
  9190. link (org-make-link cpltxt))
  9191. (org-store-link-props :type "image" :file buffer-file-name))
  9192. ((eq major-mode 'dired-mode)
  9193. ;; link to the file in the current line
  9194. (setq cpltxt (concat "file:"
  9195. (abbreviate-file-name
  9196. (expand-file-name
  9197. (dired-get-filename nil t))))
  9198. link (org-make-link cpltxt)))
  9199. ((and buffer-file-name (org-mode-p))
  9200. ;; Just link to current headline
  9201. (setq cpltxt (concat "file:"
  9202. (abbreviate-file-name buffer-file-name)))
  9203. ;; Add a context search string
  9204. (when (org-xor org-context-in-file-links arg)
  9205. ;; Check if we are on a target
  9206. (if (org-in-regexp "<<\\(.*?\\)>>")
  9207. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  9208. (setq txt (cond
  9209. ((org-on-heading-p) nil)
  9210. ((org-region-active-p)
  9211. (buffer-substring (region-beginning) (region-end)))
  9212. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  9213. (when (or (null txt) (string-match "\\S-" txt))
  9214. (setq cpltxt
  9215. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  9216. desc "NONE"))))
  9217. (if (string-match "::\\'" cpltxt)
  9218. (setq cpltxt (substring cpltxt 0 -2)))
  9219. (setq link (org-make-link cpltxt)))
  9220. (buffer-file-name
  9221. ;; Just link to this file here.
  9222. (setq cpltxt (concat "file:"
  9223. (abbreviate-file-name buffer-file-name)))
  9224. ;; Add a context string
  9225. (when (org-xor org-context-in-file-links arg)
  9226. (setq txt (if (org-region-active-p)
  9227. (buffer-substring (region-beginning) (region-end))
  9228. (buffer-substring (point-at-bol) (point-at-eol))))
  9229. ;; Only use search option if there is some text.
  9230. (when (string-match "\\S-" txt)
  9231. (setq cpltxt
  9232. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  9233. desc "NONE")))
  9234. (setq link (org-make-link cpltxt)))
  9235. ((interactive-p)
  9236. (error "Cannot link to a buffer which is not visiting a file"))
  9237. (t (setq link nil)))
  9238. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  9239. (setq link (or link cpltxt)
  9240. desc (or desc cpltxt))
  9241. (if (equal desc "NONE") (setq desc nil))
  9242. (if (and (interactive-p) link)
  9243. (progn
  9244. (setq org-stored-links
  9245. (cons (list cpltxt link desc) org-stored-links))
  9246. (message "Stored: %s" (or cpltxt link)))
  9247. (org-make-link-string link desc))))
  9248. (defun org-store-link-props (&rest plist)
  9249. "Store link properties, extract names and addresses."
  9250. (let (x adr)
  9251. (when (setq x (plist-get plist :from))
  9252. (setq adr (mail-extract-address-components x))
  9253. (plist-put plist :fromname (car adr))
  9254. (plist-put plist :fromaddress (nth 1 adr)))
  9255. (when (setq x (plist-get plist :to))
  9256. (setq adr (mail-extract-address-components x))
  9257. (plist-put plist :toname (car adr))
  9258. (plist-put plist :toaddress (nth 1 adr))))
  9259. (let ((from (plist-get plist :from))
  9260. (to (plist-get plist :to)))
  9261. (when (and from to org-from-is-user-regexp)
  9262. (plist-put plist :fromto
  9263. (if (string-match org-from-is-user-regexp from)
  9264. (concat "to %t")
  9265. (concat "from %f")))))
  9266. (setq org-store-link-plist plist))
  9267. (defun org-email-link-description (&optional fmt)
  9268. "Return the description part of an email link.
  9269. This takes information from `org-store-link-plist' and formats it
  9270. according to FMT (default from `org-email-link-description-format')."
  9271. (setq fmt (or fmt org-email-link-description-format))
  9272. (let* ((p org-store-link-plist)
  9273. (to (plist-get p :toaddress))
  9274. (from (plist-get p :fromaddress))
  9275. (table
  9276. (list
  9277. (cons "%c" (plist-get p :fromto))
  9278. (cons "%F" (plist-get p :from))
  9279. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  9280. (cons "%T" (plist-get p :to))
  9281. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  9282. (cons "%s" (plist-get p :subject))
  9283. (cons "%m" (plist-get p :message-id)))))
  9284. (when (string-match "%c" fmt)
  9285. ;; Check if the user wrote this message
  9286. (if (and org-from-is-user-regexp from to
  9287. (save-match-data (string-match org-from-is-user-regexp from)))
  9288. (setq fmt (replace-match "to %t" t t fmt))
  9289. (setq fmt (replace-match "from %f" t t fmt))))
  9290. (org-replace-escapes fmt table)))
  9291. (defun org-make-org-heading-search-string (&optional string heading)
  9292. "Make search string for STRING or current headline."
  9293. (interactive)
  9294. (let ((s (or string (org-get-heading))))
  9295. (unless (and string (not heading))
  9296. ;; We are using a headline, clean up garbage in there.
  9297. (if (string-match org-todo-regexp s)
  9298. (setq s (replace-match "" t t s)))
  9299. (if (string-match ":[a-zA-Z0-9_@:]+:[ \t]*$" s)
  9300. (setq s (replace-match "" t t s)))
  9301. (setq s (org-trim s))
  9302. (if (string-match (concat "^\\(" org-quote-string "\\|"
  9303. org-comment-string "\\)") s)
  9304. (setq s (replace-match "" t t s)))
  9305. (while (string-match org-ts-regexp s)
  9306. (setq s (replace-match "" t t s))))
  9307. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  9308. (setq s (replace-match " " t t s)))
  9309. (or string (setq s (concat "*" s))) ; Add * for headlines
  9310. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  9311. (defun org-make-link (&rest strings)
  9312. "Concatenate STRINGS, format resulting string with `org-link-format'."
  9313. (apply 'concat strings))
  9314. (defun org-make-link-string (link &optional description)
  9315. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  9316. (when (stringp description)
  9317. ;; Remove brackets from the description, they are fatal.
  9318. (while (string-match "\\[\\|\\]" description)
  9319. (setq description (replace-match "" t t description))))
  9320. (when (equal (org-link-escape link) description)
  9321. ;; No description needed, it is identical
  9322. (setq description nil))
  9323. (when (and (not description)
  9324. (not (equal link (org-link-escape link))))
  9325. (setq description link))
  9326. (concat "[[" (org-link-escape link) "]"
  9327. (if description (concat "[" description "]") "")
  9328. "]"))
  9329. (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
  9330. "Association list of escapes for some characters problematic in links.")
  9331. (defun org-link-escape (text)
  9332. "Escape charaters in TEXT that are problematic for links."
  9333. (when text
  9334. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  9335. org-link-escape-chars "\\|")))
  9336. (while (string-match re text)
  9337. (setq text
  9338. (replace-match
  9339. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  9340. t t text)))
  9341. text)))
  9342. (defun org-link-unescape (text)
  9343. "Reverse the action of `org-link-escape'."
  9344. (when text
  9345. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  9346. org-link-escape-chars "\\|")))
  9347. (while (string-match re text)
  9348. (setq text
  9349. (replace-match
  9350. (car (rassoc (match-string 0 text) org-link-escape-chars))
  9351. t t text)))
  9352. text)))
  9353. (defun org-xor (a b)
  9354. "Exclusive or."
  9355. (if a (not b) b))
  9356. (defun org-get-header (header)
  9357. "Find a header field in the current buffer."
  9358. (save-excursion
  9359. (goto-char (point-min))
  9360. (let ((case-fold-search t) s)
  9361. (cond
  9362. ((eq header 'from)
  9363. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  9364. (setq s (match-string 1)))
  9365. (while (string-match "\"" s)
  9366. (setq s (replace-match "" t t s)))
  9367. (if (string-match "[<(].*" s)
  9368. (setq s (replace-match "" t t s))))
  9369. ((eq header 'message-id)
  9370. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  9371. (setq s (match-string 1))))
  9372. ((eq header 'subject)
  9373. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  9374. (setq s (match-string 1)))))
  9375. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  9376. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  9377. s)))
  9378. (defun org-fixup-message-id-for-http (s)
  9379. "Replace special characters in a message id, so it can be used in an http query."
  9380. (while (string-match "<" s)
  9381. (setq s (replace-match "%3C" t t s)))
  9382. (while (string-match ">" s)
  9383. (setq s (replace-match "%3E" t t s)))
  9384. (while (string-match "@" s)
  9385. (setq s (replace-match "%40" t t s)))
  9386. s)
  9387. (defun org-insert-link (&optional complete-file)
  9388. "Insert a link. At the prompt, enter the link.
  9389. Completion can be used to select a link previously stored with
  9390. `org-store-link'. When the empty string is entered (i.e. if you just
  9391. press RET at the prompt), the link defaults to the most recently
  9392. stored link. As SPC triggers completion in the minibuffer, you need to
  9393. use M-SPC or C-q SPC to force the insertion of a space character.
  9394. You will also be prompted for a description, and if one is given, it will
  9395. be displayed in the buffer instead of the link.
  9396. If there is already a link at point, this command will allow you to edit link
  9397. and description parts.
  9398. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  9399. selected using completion. The path to the file will be relative to
  9400. the current directory if the file is in the current directory or a
  9401. subdirectory. Otherwise, the link will be the absolute path as
  9402. completed in the minibuffer (i.e. normally ~/path/to/file).
  9403. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  9404. is in the current directory or below.
  9405. With three \\[universal-argument] prefixes, negate the meaning of
  9406. `org-keep-stored-link-after-insertion'."
  9407. (interactive "P")
  9408. (let ((wcf (current-window-configuration))
  9409. (region (if (org-region-active-p)
  9410. (prog1 (buffer-substring (region-beginning) (region-end))
  9411. (delete-region (region-beginning) (region-end)))))
  9412. tmphist ; byte-compile incorrectly complains about this
  9413. link desc entry remove file)
  9414. (cond
  9415. ((org-in-regexp org-bracket-link-regexp 1)
  9416. ;; We do have a link at point, and we are going to edit it.
  9417. (setq remove (list (match-beginning 0) (match-end 0)))
  9418. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9419. (setq link (read-string "Link: "
  9420. (org-link-unescape
  9421. (org-match-string-no-properties 1)))))
  9422. ((or (org-in-regexp org-angle-link-re)
  9423. (org-in-regexp org-plain-link-re))
  9424. ;; Convert to bracket link
  9425. (setq remove (list (match-beginning 0) (match-end 0))
  9426. link (read-string "Link: "
  9427. (org-remove-angle-brackets (match-string 0)))))
  9428. ((equal complete-file '(4))
  9429. ;; Completing read for file names.
  9430. (setq file (read-file-name "File: "))
  9431. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9432. (pwd1 (file-name-as-directory (abbreviate-file-name
  9433. (expand-file-name ".")))))
  9434. (cond
  9435. ((equal complete-file '(16))
  9436. (setq link (org-make-link
  9437. "file:"
  9438. (abbreviate-file-name (expand-file-name file)))))
  9439. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9440. (setq link (org-make-link "file:" (match-string 1 file))))
  9441. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9442. (expand-file-name file))
  9443. (setq link (org-make-link
  9444. "file:" (match-string 1 (expand-file-name file)))))
  9445. (t (setq link (org-make-link "file:" file))))))
  9446. (t
  9447. ;; Read link, with completion for stored links.
  9448. (with-output-to-temp-buffer "*Org Links*"
  9449. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  9450. (when org-stored-links
  9451. (princ "\nStored links are available with <up>/<down> (most recent with RET):\n\n")
  9452. (princ (mapconcat 'car (reverse org-stored-links) "\n"))))
  9453. (let ((cw (selected-window)))
  9454. (select-window (get-buffer-window "*Org Links*"))
  9455. (shrink-window-if-larger-than-buffer)
  9456. (setq truncate-lines t)
  9457. (select-window cw))
  9458. ;; Fake a link history, containing the stored links.
  9459. (setq tmphist (append (mapcar 'car org-stored-links)
  9460. org-insert-link-history))
  9461. (unwind-protect
  9462. (setq link (org-completing-read
  9463. "Link: "
  9464. (append
  9465. (mapcar (lambda (x) (concat (car x) ":"))
  9466. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  9467. (mapcar (lambda (x) (concat x ":")) org-link-types))
  9468. nil nil nil
  9469. 'tmphist
  9470. (or (car (car org-stored-links)))))
  9471. (set-window-configuration wcf)
  9472. (kill-buffer "*Org Links*"))
  9473. (setq entry (assoc link org-stored-links))
  9474. (or entry (push link org-insert-link-history))
  9475. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9476. (not org-keep-stored-link-after-insertion))
  9477. (setq org-stored-links (delq (assoc link org-stored-links)
  9478. org-stored-links)))
  9479. (setq link (if entry (nth 1 entry) link)
  9480. desc (or region desc (nth 2 entry)))))
  9481. (if (string-match org-plain-link-re link)
  9482. ;; URL-like link, normalize the use of angular brackets.
  9483. (setq link (org-make-link (org-remove-angle-brackets link))))
  9484. ;; Check if we are linking to the current file with a search option
  9485. ;; If yes, simplify the link by using only the search option.
  9486. (when (and buffer-file-name
  9487. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  9488. (let* ((path (match-string 1 link))
  9489. (case-fold-search nil)
  9490. (search (match-string 2 link)))
  9491. (save-match-data
  9492. (if (equal (file-truename buffer-file-name) (file-truename path))
  9493. ;; We are linking to this same file, with a search option
  9494. (setq link search)))))
  9495. ;; Check if we can/should use a relative path. If yes, simplify the link
  9496. (when (string-match "\\<file:\\(.*\\)" link)
  9497. (let* ((path (match-string 1 link))
  9498. (case-fold-search nil))
  9499. (cond
  9500. ((eq org-link-file-path-type 'absolute)
  9501. (setq path (abbreviate-file-name (expand-file-name path))))
  9502. ((eq org-link-file-path-type 'noabbrev)
  9503. (setq path (expand-file-name path)))
  9504. ((eq org-link-file-path-type 'relative)
  9505. (setq path (file-relative-name path)))
  9506. (t
  9507. (save-match-data
  9508. (if (string-match (concat "^" (regexp-quote
  9509. (file-name-as-directory
  9510. (expand-file-name "."))))
  9511. (expand-file-name path))
  9512. ;; We are linking a file with relative path name.
  9513. (setq path (substring (expand-file-name path)
  9514. (match-end 0)))))))
  9515. (setq link (concat "file:" path))))
  9516. (setq desc (read-string "Description: " desc))
  9517. (unless (string-match "\\S-" desc) (setq desc nil))
  9518. (if remove (apply 'delete-region remove))
  9519. (insert (org-make-link-string link desc))))
  9520. (defun org-completing-read (&rest args)
  9521. (let ((minibuffer-local-completion-map
  9522. (copy-keymap minibuffer-local-completion-map)))
  9523. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9524. (apply 'completing-read args)))
  9525. ;;; Opening/following a link
  9526. (defvar org-link-search-failed nil)
  9527. (defun org-next-link ()
  9528. "Move forward to the next link.
  9529. If the link is in hidden text, expose it."
  9530. (interactive)
  9531. (when (and org-link-search-failed (eq this-command last-command))
  9532. (goto-char (point-min))
  9533. (message "Link search wrapped back to beginning of buffer"))
  9534. (setq org-link-search-failed nil)
  9535. (let* ((pos (point))
  9536. (ct (org-context))
  9537. (a (assoc :link ct)))
  9538. (if a (goto-char (nth 2 a)))
  9539. (if (re-search-forward org-any-link-re nil t)
  9540. (progn
  9541. (goto-char (match-beginning 0))
  9542. (if (org-invisible-p) (org-show-context)))
  9543. (goto-char pos)
  9544. (setq org-link-search-failed t)
  9545. (error "No further link found"))))
  9546. (defun org-previous-link ()
  9547. "Move backward to the previous link.
  9548. If the link is in hidden text, expose it."
  9549. (interactive)
  9550. (when (and org-link-search-failed (eq this-command last-command))
  9551. (goto-char (point-max))
  9552. (message "Link search wrapped back to end of buffer"))
  9553. (setq org-link-search-failed nil)
  9554. (let* ((pos (point))
  9555. (ct (org-context))
  9556. (a (assoc :link ct)))
  9557. (if a (goto-char (nth 1 a)))
  9558. (if (re-search-backward org-any-link-re nil t)
  9559. (progn
  9560. (goto-char (match-beginning 0))
  9561. (if (org-invisible-p) (org-show-context)))
  9562. (goto-char pos)
  9563. (setq org-link-search-failed t)
  9564. (error "No further link found"))))
  9565. (defun org-find-file-at-mouse (ev)
  9566. "Open file link or URL at mouse."
  9567. (interactive "e")
  9568. (mouse-set-point ev)
  9569. (org-open-at-point 'in-emacs))
  9570. (defun org-open-at-mouse (ev)
  9571. "Open file link or URL at mouse."
  9572. (interactive "e")
  9573. (mouse-set-point ev)
  9574. (org-open-at-point))
  9575. (defvar org-window-config-before-follow-link nil
  9576. "The window configuration before following a link.
  9577. This is saved in case the need arises to restore it.")
  9578. (defvar org-open-link-marker (make-marker)
  9579. "Marker pointing to the location where `org-open-at-point; was called.")
  9580. (defun org-open-at-point (&optional in-emacs)
  9581. "Open link at or after point.
  9582. If there is no link at point, this function will search forward up to
  9583. the end of the current subtree.
  9584. Normally, files will be opened by an appropriate application. If the
  9585. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  9586. (interactive "P")
  9587. (move-marker org-open-link-marker (point))
  9588. (setq org-window-config-before-follow-link (current-window-configuration))
  9589. (org-remove-occur-highlights nil nil t)
  9590. (if (org-at-timestamp-p t)
  9591. (org-follow-timestamp-link)
  9592. (let (type path link line search (pos (point)))
  9593. (catch 'match
  9594. (save-excursion
  9595. (skip-chars-forward "^]\n\r")
  9596. (when (org-in-regexp org-bracket-link-regexp)
  9597. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  9598. (while (string-match " *\n *" link)
  9599. (setq link (replace-match " " t t link)))
  9600. (setq link (org-link-expand-abbrev link))
  9601. (if (string-match org-link-re-with-space2 link)
  9602. (setq type (match-string 1 link) path (match-string 2 link))
  9603. (setq type "thisfile" path link))
  9604. (throw 'match t)))
  9605. (when (get-text-property (point) 'org-linked-text)
  9606. (setq type "thisfile"
  9607. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9608. (1+ (point)) (point))
  9609. path (buffer-substring
  9610. (previous-single-property-change pos 'org-linked-text)
  9611. (next-single-property-change pos 'org-linked-text)))
  9612. (throw 'match t))
  9613. (save-excursion
  9614. (when (or (org-in-regexp org-angle-link-re)
  9615. (org-in-regexp org-plain-link-re))
  9616. (setq type (match-string 1) path (match-string 2))
  9617. (throw 'match t)))
  9618. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  9619. (setq type "tree-match"
  9620. path (match-string 1))
  9621. (throw 'match t))
  9622. (save-excursion
  9623. (when (org-in-regexp "\\(:[a-zA-Z0-9_@:]+\\):[ \t]*$")
  9624. (setq type "tags"
  9625. path (match-string 1))
  9626. (while (string-match ":" path)
  9627. (setq path (replace-match "+" t t path)))
  9628. (throw 'match t))))
  9629. (unless path
  9630. (error "No link found"))
  9631. ;; Remove any trailing spaces in path
  9632. (if (string-match " +\\'" path)
  9633. (setq path (replace-match "" t t path)))
  9634. (cond
  9635. ((equal type "mailto")
  9636. (let ((cmd (car org-link-mailto-program))
  9637. (args (cdr org-link-mailto-program)) args1
  9638. (address path) (subject "") a)
  9639. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9640. (setq address (match-string 1 path)
  9641. subject (org-link-escape (match-string 2 path))))
  9642. (while args
  9643. (cond
  9644. ((not (stringp (car args))) (push (pop args) args1))
  9645. (t (setq a (pop args))
  9646. (if (string-match "%a" a)
  9647. (setq a (replace-match address t t a)))
  9648. (if (string-match "%s" a)
  9649. (setq a (replace-match subject t t a)))
  9650. (push a args1))))
  9651. (apply cmd (nreverse args1))))
  9652. ((member type '("http" "https" "ftp" "news"))
  9653. (browse-url (concat type ":" path)))
  9654. ((string= type "tags")
  9655. (org-tags-view in-emacs path))
  9656. ((string= type "thisfile")
  9657. (if in-emacs
  9658. (switch-to-buffer-other-window
  9659. (org-get-buffer-for-internal-link (current-buffer)))
  9660. (org-mark-ring-push))
  9661. (org-link-search
  9662. path
  9663. (cond ((equal in-emacs '(4)) 'occur)
  9664. ((equal in-emacs '(16)) 'org-occur)
  9665. (t nil))
  9666. pos))
  9667. ((string= type "tree-match")
  9668. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9669. ((string= type "file")
  9670. (if (string-match "::\\([0-9]+\\)\\'" path)
  9671. (setq line (string-to-number (match-string 1 path))
  9672. path (substring path 0 (match-beginning 0)))
  9673. (if (string-match "::\\(.+\\)\\'" path)
  9674. (setq search (match-string 1 path)
  9675. path (substring path 0 (match-beginning 0)))))
  9676. (org-open-file path in-emacs line search))
  9677. ((string= type "news")
  9678. (org-follow-gnus-link path))
  9679. ((string= type "bbdb")
  9680. (org-follow-bbdb-link path))
  9681. ((string= type "info")
  9682. (org-follow-info-link path))
  9683. ((string= type "gnus")
  9684. (let (group article)
  9685. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  9686. (error "Error in Gnus link"))
  9687. (setq group (match-string 1 path)
  9688. article (match-string 3 path))
  9689. (org-follow-gnus-link group article)))
  9690. ((string= type "vm")
  9691. (let (folder article)
  9692. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  9693. (error "Error in VM link"))
  9694. (setq folder (match-string 1 path)
  9695. article (match-string 3 path))
  9696. ;; in-emacs is the prefix arg, will be interpreted as read-only
  9697. (org-follow-vm-link folder article in-emacs)))
  9698. ((string= type "wl")
  9699. (let (folder article)
  9700. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  9701. (error "Error in Wanderlust link"))
  9702. (setq folder (match-string 1 path)
  9703. article (match-string 3 path))
  9704. (org-follow-wl-link folder article)))
  9705. ((string= type "mhe")
  9706. (let (folder article)
  9707. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  9708. (error "Error in MHE link"))
  9709. (setq folder (match-string 1 path)
  9710. article (match-string 3 path))
  9711. (org-follow-mhe-link folder article)))
  9712. ((string= type "rmail")
  9713. (let (folder article)
  9714. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  9715. (error "Error in RMAIL link"))
  9716. (setq folder (match-string 1 path)
  9717. article (match-string 3 path))
  9718. (org-follow-rmail-link folder article)))
  9719. ((string= type "shell")
  9720. (let ((cmd path))
  9721. ;; FIXME: the following is only for backward compatibility
  9722. (while (string-match "@{" cmd) (setq cmd (replace-match "<" t t cmd)))
  9723. (while (string-match "@}" cmd) (setq cmd (replace-match ">" t t cmd)))
  9724. (if (or (not org-confirm-shell-link-function)
  9725. (funcall org-confirm-shell-link-function
  9726. (format "Execute \"%s\" in shell? "
  9727. (org-add-props cmd nil
  9728. 'face 'org-warning))))
  9729. (progn
  9730. (message "Executing %s" cmd)
  9731. (shell-command cmd))
  9732. (error "Abort"))))
  9733. ((string= type "elisp")
  9734. (let ((cmd path))
  9735. (if (or (not org-confirm-elisp-link-function)
  9736. (funcall org-confirm-elisp-link-function
  9737. (format "Execute \"%s\" as elisp? "
  9738. (org-add-props cmd nil
  9739. 'face 'org-warning))))
  9740. (message "%s => %s" cmd (eval (read cmd)))
  9741. (error "Abort"))))
  9742. (t
  9743. (browse-url-at-point)))))
  9744. (move-marker org-open-link-marker nil))
  9745. ;;; File search
  9746. (defvar org-create-file-search-functions nil
  9747. "List of functions to construct the right search string for a file link.
  9748. These functions are called in turn with point at the location to
  9749. which the link should point.
  9750. A function in the hook should first test if it would like to
  9751. handle this file type, for example by checking the major-mode or
  9752. the file extension. If it decides not to handle this file, it
  9753. should just return nil to give other functions a chance. If it
  9754. does handle the file, it must return the search string to be used
  9755. when following the link. The search string will be part of the
  9756. file link, given after a double colon, and `org-open-at-point'
  9757. will automatically search for it. If special measures must be
  9758. taken to make the search successful, another function should be
  9759. added to the companion hook `org-execute-file-search-functions',
  9760. which see.
  9761. A function in this hook may also use `setq' to set the variable
  9762. `description' to provide a suggestion for the descriptive text to
  9763. be used for this link when it gets inserted into an Org-mode
  9764. buffer with \\[org-insert-link].")
  9765. (defvar org-execute-file-search-functions nil
  9766. "List of functions to execute a file search triggered by a link.
  9767. Functions added to this hook must accept a single argument, the
  9768. search string that was part of the file link, the part after the
  9769. double colon. The function must first check if it would like to
  9770. handle this search, for example by checking the major-mode or the
  9771. file extension. If it decides not to handle this search, it
  9772. should just return nil to give other functions a chance. If it
  9773. does handle the search, it must return a non-nil value to keep
  9774. other functions from trying.
  9775. Each function can access the current prefix argument through the
  9776. variable `current-prefix-argument'. Note that a single prefix is
  9777. used to force opening a link in Emacs, so it may be good to only
  9778. use a numeric or double prefix to guide the search function.
  9779. In case this is needed, a function in this hook can also restore
  9780. the window configuration before `org-open-at-point' was called using:
  9781. (set-window-configuration org-window-config-before-follow-link)")
  9782. (defun org-link-search (s &optional type avoid-pos)
  9783. "Search for a link search option.
  9784. If S is surrounded by forward slashes, it is interpreted as a
  9785. regular expression. In org-mode files, this will create an `org-occur'
  9786. sparse tree. In ordinary files, `occur' will be used to list matches.
  9787. If the current buffer is in `dired-mode', grep will be used to search
  9788. in all files. If AVOID-POS is given, ignore matches near that position."
  9789. (let ((case-fold-search t)
  9790. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9791. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9792. (append '((" ") ("\t") ("\n"))
  9793. org-emphasis-alist)
  9794. "\\|") "\\)"))
  9795. (pos (point))
  9796. (pre "") (post "")
  9797. words re0 re1 re2 re3 re4 re5 re2a reall)
  9798. (cond
  9799. ;; First check if there are any special
  9800. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9801. ;; Now try the builtin stuff
  9802. ((save-excursion
  9803. (goto-char (point-min))
  9804. (and
  9805. (re-search-forward
  9806. (concat "<<" (regexp-quote s0) ">>") nil t)
  9807. (setq pos (match-beginning 0))))
  9808. ;; There is an exact target for this
  9809. (goto-char pos))
  9810. ((string-match "^/\\(.*\\)/$" s)
  9811. ;; A regular expression
  9812. (cond
  9813. ((org-mode-p)
  9814. (org-occur (match-string 1 s)))
  9815. ;;((eq major-mode 'dired-mode)
  9816. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9817. (t (org-do-occur (match-string 1 s)))))
  9818. (t
  9819. ;; A normal search string
  9820. (when (equal (string-to-char s) ?*)
  9821. ;; Anchor on headlines, post may include tags.
  9822. (setq pre "^\\*+[ \t]*\\(?:\\sw+\\)?[ \t]*"
  9823. post "[ \t]*\\(?:[ \t]+:[a-zA-Z0-9_@:+]:[ \t]*\\)?$"
  9824. s (substring s 1)))
  9825. (remove-text-properties
  9826. 0 (length s)
  9827. '(face nil mouse-face nil keymap nil fontified nil) s)
  9828. ;; Make a series of regular expressions to find a match
  9829. (setq words (org-split-string s "[ \n\r\t]+")
  9830. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9831. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9832. "\\)" markers)
  9833. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9834. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9835. re1 (concat pre re2 post)
  9836. re3 (concat pre re4 post)
  9837. re5 (concat pre ".*" re4)
  9838. re2 (concat pre re2)
  9839. re2a (concat pre re2a)
  9840. re4 (concat pre re4)
  9841. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9842. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9843. re5 "\\)"
  9844. ))
  9845. (cond
  9846. ((eq type 'org-occur) (org-occur reall))
  9847. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9848. (t (goto-char (point-min))
  9849. (if (or (org-search-not-self 1 re0 nil t)
  9850. (org-search-not-self 1 re1 nil t)
  9851. (org-search-not-self 1 re2 nil t)
  9852. (org-search-not-self 1 re2a nil t)
  9853. (org-search-not-self 1 re3 nil t)
  9854. (org-search-not-self 1 re4 nil t)
  9855. (org-search-not-self 1 re5 nil t)
  9856. )
  9857. (goto-char (match-beginning 1))
  9858. (goto-char pos)
  9859. (error "No match")))))
  9860. (t
  9861. ;; Normal string-search
  9862. (goto-char (point-min))
  9863. (if (search-forward s nil t)
  9864. (goto-char (match-beginning 0))
  9865. (error "No match"))))
  9866. (and (org-mode-p) (org-show-context 'link-search))))
  9867. (defun org-search-not-self (group &rest args)
  9868. "Execute `re-search-forward', but only accept matches that do not
  9869. enclose the position of `org-open-link-marker'."
  9870. (let ((m org-open-link-marker))
  9871. (catch 'exit
  9872. (while (apply 're-search-forward args)
  9873. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9874. (goto-char (match-end group))
  9875. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9876. (> (match-beginning 0) (marker-position m))
  9877. (< (match-end 0) (marker-position m)))
  9878. (save-match-data
  9879. (or (not (org-in-regexp
  9880. org-bracket-link-analytic-regexp 1))
  9881. (not (match-end 4)) ; no description
  9882. (and (<= (match-beginning 4) (point))
  9883. (>= (match-end 4) (point))))))
  9884. (throw 'exit (point))))))))
  9885. (defun org-get-buffer-for-internal-link (buffer)
  9886. "Return a buffer to be used for displaying the link target of internal links."
  9887. (cond
  9888. ((not org-display-internal-link-with-indirect-buffer)
  9889. buffer)
  9890. ((string-match "(Clone)$" (buffer-name buffer))
  9891. (message "Buffer is already a clone, not making another one")
  9892. ;; we also do not modify visibility in this case
  9893. buffer)
  9894. (t ; make a new indirect buffer for displaying the link
  9895. (let* ((bn (buffer-name buffer))
  9896. (ibn (concat bn "(Clone)"))
  9897. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9898. (with-current-buffer ib (org-overview))
  9899. ib))))
  9900. (defun org-do-occur (regexp &optional cleanup)
  9901. "Call the Emacs command `occur'.
  9902. If CLEANUP is non-nil, remove the printout of the regular expression
  9903. in the *Occur* buffer. This is useful if the regex is long and not useful
  9904. to read."
  9905. (occur regexp)
  9906. (when cleanup
  9907. (let ((cwin (selected-window)) win beg end)
  9908. (when (setq win (get-buffer-window "*Occur*"))
  9909. (select-window win))
  9910. (goto-char (point-min))
  9911. (when (re-search-forward "match[a-z]+" nil t)
  9912. (setq beg (match-end 0))
  9913. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9914. (setq end (1- (match-beginning 0)))))
  9915. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  9916. (goto-char (point-min))
  9917. (select-window cwin))))
  9918. ;;; The mark ring for links jumps
  9919. (defvar org-mark-ring nil
  9920. "Mark ring for positions before jumps in Org-mode.")
  9921. (defvar org-mark-ring-last-goto nil
  9922. "Last position in the mark ring used to go back.")
  9923. ;; Fill and close the ring
  9924. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9925. (loop for i from 1 to org-mark-ring-length do
  9926. (push (make-marker) org-mark-ring))
  9927. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9928. org-mark-ring)
  9929. (defun org-mark-ring-push (&optional pos buffer)
  9930. "Put the current position or POS into the mark ring and rotate it."
  9931. (interactive)
  9932. (setq pos (or pos (point)))
  9933. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9934. (move-marker (car org-mark-ring)
  9935. (or pos (point))
  9936. (or buffer (current-buffer)))
  9937. (message
  9938. (substitute-command-keys
  9939. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9940. (defun org-mark-ring-goto (&optional n)
  9941. "Jump to the previous position in the mark ring.
  9942. With prefix arg N, jump back that many stored positions. When
  9943. called several times in succession, walk through the entire ring.
  9944. Org-mode commands jumping to a different position in the current file,
  9945. or to another Org-mode file, automatically push the old position
  9946. onto the ring."
  9947. (interactive "p")
  9948. (let (p m)
  9949. (if (eq last-command this-command)
  9950. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9951. (setq p org-mark-ring))
  9952. (setq org-mark-ring-last-goto p)
  9953. (setq m (car p))
  9954. (switch-to-buffer (marker-buffer m))
  9955. (goto-char m)
  9956. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9957. (defun org-remove-angle-brackets (s)
  9958. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9959. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9960. s)
  9961. (defun org-add-angle-brackets (s)
  9962. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9963. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9964. s)
  9965. ;;; Following specific links
  9966. (defun org-follow-timestamp-link ()
  9967. (cond
  9968. ((org-at-date-range-p t)
  9969. (let ((org-agenda-start-on-weekday)
  9970. (t1 (match-string 1))
  9971. (t2 (match-string 2)))
  9972. (setq t1 (time-to-days (org-time-string-to-time t1))
  9973. t2 (time-to-days (org-time-string-to-time t2)))
  9974. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9975. ((org-at-timestamp-p t)
  9976. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9977. (substring (match-string 1) 0 10)))
  9978. 1))
  9979. (t (error "This should not happen"))))
  9980. (defun org-follow-bbdb-link (name)
  9981. "Follow a BBDB link to NAME."
  9982. (require 'bbdb)
  9983. (let ((inhibit-redisplay (not debug-on-error))
  9984. (bbdb-electric-p nil))
  9985. (catch 'exit
  9986. ;; Exact match on name
  9987. (bbdb-name (concat "\\`" name "\\'") nil)
  9988. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  9989. ;; Exact match on name
  9990. (bbdb-company (concat "\\`" name "\\'") nil)
  9991. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  9992. ;; Partial match on name
  9993. (bbdb-name name nil)
  9994. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  9995. ;; Partial match on company
  9996. (bbdb-company name nil)
  9997. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  9998. ;; General match including network address and notes
  9999. (bbdb name nil)
  10000. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  10001. (delete-window (get-buffer-window "*BBDB*"))
  10002. (error "No matching BBDB record")))))
  10003. (defun org-follow-info-link (name)
  10004. "Follow an info file & node link to NAME."
  10005. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  10006. (string-match "\\(.*\\)" name))
  10007. (progn
  10008. (require 'info)
  10009. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  10010. (Info-find-node (match-string 1 name) (match-string 2 name))
  10011. (Info-find-node (match-string 1 name) "Top")))
  10012. (message (concat "Could not open: " name))))
  10013. (defun org-follow-gnus-link (&optional group article)
  10014. "Follow a Gnus link to GROUP and ARTICLE."
  10015. (require 'gnus)
  10016. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  10017. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  10018. (cond ((and group article)
  10019. (gnus-group-read-group 1 nil group)
  10020. (gnus-summary-goto-article (string-to-number article) nil t))
  10021. (group (gnus-group-jump-to-group group))))
  10022. (defun org-follow-vm-link (&optional folder article readonly)
  10023. "Follow a VM link to FOLDER and ARTICLE."
  10024. (require 'vm)
  10025. (setq article (org-add-angle-brackets article))
  10026. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  10027. ;; ange-ftp or efs or tramp access
  10028. (let ((user (or (match-string 1 folder) (user-login-name)))
  10029. (host (match-string 2 folder))
  10030. (file (match-string 3 folder)))
  10031. (cond
  10032. ((featurep 'tramp)
  10033. ;; use tramp to access the file
  10034. (if (featurep 'xemacs)
  10035. (setq folder (format "[%s@%s]%s" user host file))
  10036. (setq folder (format "/%s@%s:%s" user host file))))
  10037. (t
  10038. ;; use ange-ftp or efs
  10039. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  10040. (setq folder (format "/%s@%s:%s" user host file))))))
  10041. (when folder
  10042. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  10043. (sit-for 0.1)
  10044. (when article
  10045. (vm-select-folder-buffer)
  10046. (widen)
  10047. (let ((case-fold-search t))
  10048. (goto-char (point-min))
  10049. (if (not (re-search-forward
  10050. (concat "^" "message-id: *" (regexp-quote article))))
  10051. (error "Could not find the specified message in this folder"))
  10052. (vm-isearch-update)
  10053. (vm-isearch-narrow)
  10054. (vm-beginning-of-message)
  10055. (vm-summarize)))))
  10056. (defun org-follow-wl-link (folder article)
  10057. "Follow a Wanderlust link to FOLDER and ARTICLE."
  10058. (if (and (string= folder "%")
  10059. article
  10060. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  10061. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  10062. ;; Thus, we recompose folder and article ids.
  10063. (setq folder (format "%s#%s" folder (match-string 1 article))
  10064. article (match-string 3 article)))
  10065. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  10066. (error "No such folder: %s" folder))
  10067. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  10068. (and article
  10069. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  10070. (wl-summary-redisplay)))
  10071. (defun org-follow-rmail-link (folder article)
  10072. "Follow an RMAIL link to FOLDER and ARTICLE."
  10073. (setq article (org-add-angle-brackets article))
  10074. (let (message-number)
  10075. (save-excursion
  10076. (save-window-excursion
  10077. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  10078. (setq message-number
  10079. (save-restriction
  10080. (widen)
  10081. (goto-char (point-max))
  10082. (if (re-search-backward
  10083. (concat "^Message-ID:\\s-+" (regexp-quote
  10084. (or article "")))
  10085. nil t)
  10086. (rmail-what-message))))))
  10087. (if message-number
  10088. (progn
  10089. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  10090. (rmail-show-message message-number)
  10091. message-number)
  10092. (error "Message not found"))))
  10093. ;;; mh-e integration based on planner-mode
  10094. (defun org-mhe-get-message-real-folder ()
  10095. "Return the name of the current message real folder, so if you use
  10096. sequences, it will now work."
  10097. (save-excursion
  10098. (let* ((folder
  10099. (if (equal major-mode 'mh-folder-mode)
  10100. mh-current-folder
  10101. ;; Refer to the show buffer
  10102. mh-show-folder-buffer))
  10103. (end-index
  10104. (if (boundp 'mh-index-folder)
  10105. (min (length mh-index-folder) (length folder))))
  10106. )
  10107. ;; a simple test on mh-index-data does not work, because
  10108. ;; mh-index-data is always nil in a show buffer.
  10109. (if (and (boundp 'mh-index-folder)
  10110. (string= mh-index-folder (substring folder 0 end-index)))
  10111. (if (equal major-mode 'mh-show-mode)
  10112. (save-window-excursion
  10113. (when (buffer-live-p (get-buffer folder))
  10114. (progn
  10115. (pop-to-buffer folder)
  10116. (org-mhe-get-message-folder-from-index)
  10117. )
  10118. ))
  10119. (org-mhe-get-message-folder-from-index)
  10120. )
  10121. folder
  10122. )
  10123. )))
  10124. (defun org-mhe-get-message-folder-from-index ()
  10125. "Returns the name of the message folder in a index folder buffer."
  10126. (save-excursion
  10127. (mh-index-previous-folder)
  10128. (re-search-forward "^\\(+.*\\)$" nil t)
  10129. (message (match-string 1))))
  10130. (defun org-mhe-get-message-folder ()
  10131. "Return the name of the current message folder. Be careful if you
  10132. use sequences."
  10133. (save-excursion
  10134. (if (equal major-mode 'mh-folder-mode)
  10135. mh-current-folder
  10136. ;; Refer to the show buffer
  10137. mh-show-folder-buffer)))
  10138. (defun org-mhe-get-message-num ()
  10139. "Return the number of the current message. Be careful if you
  10140. use sequences."
  10141. (save-excursion
  10142. (if (equal major-mode 'mh-folder-mode)
  10143. (mh-get-msg-num nil)
  10144. ;; Refer to the show buffer
  10145. (mh-show-buffer-message-number))))
  10146. (defun org-mhe-get-header (header)
  10147. "Return a header of the message in folder mode. This will create a
  10148. show buffer for the corresponding message. If you have a more clever
  10149. idea..."
  10150. (let* ((folder (org-mhe-get-message-folder))
  10151. (num (org-mhe-get-message-num))
  10152. (buffer (get-buffer-create (concat "show-" folder)))
  10153. (header-field))
  10154. (with-current-buffer buffer
  10155. (mh-display-msg num folder)
  10156. (if (equal major-mode 'mh-folder-mode)
  10157. (mh-header-display)
  10158. (mh-show-header-display))
  10159. (set-buffer buffer)
  10160. (setq header-field (mh-get-header-field header))
  10161. (if (equal major-mode 'mh-folder-mode)
  10162. (mh-show)
  10163. (mh-show-show))
  10164. header-field)))
  10165. (defun org-follow-mhe-link (folder article)
  10166. "Follow an MHE link to FOLDER and ARTICLE.
  10167. If ARTICLE is nil FOLDER is shown. If the configuration variable
  10168. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  10169. ARTICLE is searched in all folders. Indexed searches (swish++,
  10170. namazu, and others supported by MH-E) will always search in all
  10171. folders."
  10172. (require 'mh-e)
  10173. (require 'mh-search)
  10174. (require 'mh-utils)
  10175. (mh-find-path)
  10176. (if (not article)
  10177. (mh-visit-folder (mh-normalize-folder-name folder))
  10178. (setq article (org-add-angle-brackets article))
  10179. (mh-search-choose)
  10180. (if (equal mh-searcher 'pick)
  10181. (progn
  10182. (mh-search folder (list "--message-id" article))
  10183. (when (and org-mhe-search-all-folders
  10184. (not (org-mhe-get-message-real-folder)))
  10185. (kill-this-buffer)
  10186. (mh-search "+" (list "--message-id" article))))
  10187. (mh-search "+" article))
  10188. (if (org-mhe-get-message-real-folder)
  10189. (mh-show-msg 1)
  10190. (kill-this-buffer)
  10191. (error "Message not found"))))
  10192. ;;; BibTeX links
  10193. ;; Use the custom search meachnism to construct and use search strings for
  10194. ;; file links to BibTeX database entries.
  10195. (defun org-create-file-search-in-bibtex ()
  10196. "Create the search string and description for a BibTeX database entry."
  10197. (when (eq major-mode 'bibtex-mode)
  10198. ;; yes, we want to construct this search string.
  10199. ;; Make a good description for this entry, using names, year and the title
  10200. ;; Put it into the `description' variable which is dynamically scoped.
  10201. (let ((bibtex-autokey-names 1)
  10202. (bibtex-autokey-names-stretch 1)
  10203. (bibtex-autokey-name-case-convert-function 'identity)
  10204. (bibtex-autokey-name-separator " & ")
  10205. (bibtex-autokey-additional-names " et al.")
  10206. (bibtex-autokey-year-length 4)
  10207. (bibtex-autokey-name-year-separator " ")
  10208. (bibtex-autokey-titlewords 3)
  10209. (bibtex-autokey-titleword-separator " ")
  10210. (bibtex-autokey-titleword-case-convert-function 'identity)
  10211. (bibtex-autokey-titleword-length 'infty)
  10212. (bibtex-autokey-year-title-separator ": "))
  10213. (setq description (bibtex-generate-autokey)))
  10214. ;; Now parse the entry, get the key and return it.
  10215. (save-excursion
  10216. (bibtex-beginning-of-entry)
  10217. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  10218. (defun org-execute-file-search-in-bibtex (s)
  10219. "Find the link search string S as a key for a database entry."
  10220. (when (eq major-mode 'bibtex-mode)
  10221. ;; Yes, we want to do the search in this file.
  10222. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  10223. (goto-char (point-min))
  10224. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  10225. (regexp-quote s) "[ \t\n]*,") nil t)
  10226. (goto-char (match-beginning 0)))
  10227. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  10228. ;; Use double prefix to indicate that any web link should be browsed
  10229. (let ((b (current-buffer)) (p (point)))
  10230. ;; Restore the window configuration because we just use the web link
  10231. (set-window-configuration org-window-config-before-follow-link)
  10232. (save-excursion (set-buffer b) (goto-char p)
  10233. (bibtex-url)))
  10234. (recenter 0)) ; Move entry start to beginning of window
  10235. ;; return t to indicate that the search is done.
  10236. t))
  10237. ;; Finally add the functions to the right hooks.
  10238. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  10239. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  10240. ;; end of Bibtex link setup
  10241. ;;; Following file links
  10242. (defun org-open-file (path &optional in-emacs line search)
  10243. "Open the file at PATH.
  10244. First, this expands any special file name abbreviations. Then the
  10245. configuration variable `org-file-apps' is checked if it contains an
  10246. entry for this file type, and if yes, the corresponding command is launched.
  10247. If no application is found, Emacs simply visits the file.
  10248. With optional argument IN-EMACS, Emacs will visit the file.
  10249. Optional LINE specifies a line to go to, optional SEARCH a string to
  10250. search for. If LINE or SEARCH is given, the file will always be
  10251. opened in Emacs.
  10252. If the file does not exist, an error is thrown."
  10253. (setq in-emacs (or in-emacs line search))
  10254. (let* ((file (if (equal path "")
  10255. buffer-file-name
  10256. (substitute-in-file-name (expand-file-name path))))
  10257. (apps (append org-file-apps (org-default-apps)))
  10258. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10259. (dirp (if remp nil (file-directory-p file)))
  10260. (dfile (downcase file))
  10261. (old-buffer (current-buffer))
  10262. (old-pos (point))
  10263. (old-mode major-mode)
  10264. ext cmd)
  10265. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10266. (setq ext (match-string 1 dfile))
  10267. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10268. (setq ext (match-string 1 dfile))))
  10269. (if in-emacs
  10270. (setq cmd 'emacs)
  10271. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10272. (and dirp (cdr (assoc 'directory apps)))
  10273. (cdr (assoc ext apps))
  10274. (cdr (assoc t apps)))))
  10275. (when (eq cmd 'mailcap)
  10276. (require 'mailcap)
  10277. (mailcap-parse-mailcaps)
  10278. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10279. (command (mailcap-mime-info mime-type)))
  10280. (if (stringp command)
  10281. (setq cmd command)
  10282. (setq cmd 'emacs))))
  10283. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10284. (not (file-exists-p file))
  10285. (not org-open-non-existing-files))
  10286. (error "No such file: %s" file))
  10287. (cond
  10288. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10289. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10290. (if (string-match "['\"]%s['\"]" cmd)
  10291. (setq cmd (replace-match "%s" t t cmd)))
  10292. (setq cmd (format cmd (shell-quote-argument file)))
  10293. (save-window-excursion
  10294. (shell-command (concat cmd " &"))))
  10295. ((or (stringp cmd)
  10296. (eq cmd 'emacs))
  10297. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10298. (if line (goto-line line)
  10299. (if search (org-link-search search))))
  10300. ((consp cmd)
  10301. (eval cmd))
  10302. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10303. (and (org-mode-p) (eq old-mode 'org-mode)
  10304. (or (not (equal old-buffer (current-buffer)))
  10305. (not (equal old-pos (point))))
  10306. (org-mark-ring-push old-pos old-buffer))))
  10307. (defun org-default-apps ()
  10308. "Return the default applications for this operating system."
  10309. (cond
  10310. ((eq system-type 'darwin)
  10311. org-file-apps-defaults-macosx)
  10312. ((eq system-type 'windows-nt)
  10313. org-file-apps-defaults-windowsnt)
  10314. (t org-file-apps-defaults-gnu)))
  10315. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10316. (defun org-file-remote-p (file)
  10317. "Test whether FILE specifies a location on a remote system.
  10318. Return non-nil if the location is indeed remote.
  10319. For example, the filename \"/user@host:/foo\" specifies a location
  10320. on the system \"/user@host:\"."
  10321. (cond ((fboundp 'file-remote-p)
  10322. (file-remote-p file))
  10323. ((fboundp 'tramp-handle-file-remote-p)
  10324. (tramp-handle-file-remote-p file))
  10325. ((and (boundp 'ange-ftp-name-format)
  10326. (string-match (car ange-ftp-name-format) file))
  10327. t)
  10328. (t nil)))
  10329. ;;;; Hooks for remember.el
  10330. ;;;###autoload
  10331. (defun org-remember-annotation ()
  10332. "Return a link to the current location as an annotation for remember.el.
  10333. If you are using Org-mode files as target for data storage with
  10334. remember.el, then the annotations should include a link compatible with the
  10335. conventions in Org-mode. This function returns such a link."
  10336. (org-store-link nil))
  10337. (defconst org-remember-help
  10338. "Select a destination location for the note.
  10339. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  10340. RET at beg-of-buf -> Append to file as level 2 headline
  10341. RET on headline -> Store as sublevel entry to current headline
  10342. <left>/<right> -> before/after current headline, same headings level")
  10343. ;;;###autoload
  10344. (defun org-remember-apply-template (&optional use-char skip-interactive)
  10345. "Initialize *remember* buffer with template, invoke `org-mode'.
  10346. This function should be placed into `remember-mode-hook' and in fact requires
  10347. to be run from that hook to fucntion properly."
  10348. (if org-remember-templates
  10349. (let* ((char (or use-char
  10350. (if (= (length org-remember-templates) 1)
  10351. (caar org-remember-templates)
  10352. (message "Select template: %s"
  10353. (mapconcat
  10354. (lambda (x) (char-to-string (car x)))
  10355. org-remember-templates " "))
  10356. (read-char-exclusive))))
  10357. (entry (cdr (assoc char org-remember-templates)))
  10358. (tpl (car entry))
  10359. (plist-p (if org-store-link-plist t nil))
  10360. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  10361. (string-match "\\S-" (nth 1 entry)))
  10362. (nth 1 entry)
  10363. org-default-notes-file))
  10364. (headline (nth 2 entry))
  10365. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  10366. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  10367. (v-u (concat "[" (substring v-t 1 -1) "]"))
  10368. (v-U (concat "[" (substring v-T 1 -1) "]"))
  10369. (v-i initial) ; defined in `remember-mode'
  10370. (v-a (if (equal annotation "[[]]") "" annotation)) ; likewise
  10371. (v-n user-full-name)
  10372. (org-startup-folded nil)
  10373. org-time-was-given org-end-time-was-given x prompt char time)
  10374. (setq org-store-link-plist
  10375. (append (list :annotation v-a :initial v-i)))
  10376. (unless tpl (setq tpl "") (message "No template") (ding))
  10377. (erase-buffer)
  10378. (insert (substitute-command-keys
  10379. (format
  10380. "## `C-c C-c' to file interactively, `C-u C-c C-c' to file directly.
  10381. ## Target file \"%s\", headline \"%s\"
  10382. ## To switch templates, use `\\[org-remember]'.\n\n"
  10383. (abbreviate-file-name (or file org-default-notes-file))
  10384. (or headline ""))))
  10385. (insert tpl) (goto-char (point-min))
  10386. ;; Simple %-escapes
  10387. (while (re-search-forward "%\\([tTuUai]\\)" nil t)
  10388. (when (and initial (equal (match-string 0) "%i"))
  10389. (save-match-data
  10390. (let* ((lead (buffer-substring
  10391. (point-at-bol) (match-beginning 0))))
  10392. (setq v-i (mapconcat 'identity
  10393. (org-split-string initial "\n")
  10394. (concat "\n" lead))))))
  10395. (replace-match
  10396. (or (eval (intern (concat "v-" (match-string 1)))) "")
  10397. t t))
  10398. ;; From the property list
  10399. (when plist-p
  10400. (goto-char (point-min))
  10401. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  10402. (and (setq x (plist-get org-store-link-plist
  10403. (intern (match-string 1))))
  10404. (replace-match x t t))))
  10405. ;; Turn on org-mode in the remember buffer, set local variables
  10406. (org-mode)
  10407. (org-set-local 'org-finish-function 'remember-buffer)
  10408. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  10409. (org-set-local 'org-default-notes-file file))
  10410. (if (and headline (stringp headline) (string-match "\\S-" headline))
  10411. (org-set-local 'org-remember-default-headline headline))
  10412. ;; Interactive template entries
  10413. (goto-char (point-min))
  10414. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([uUtT]\\)?" nil t)
  10415. (setq char (if (match-end 3) (match-string 3))
  10416. prompt (if (match-end 2) (match-string 2)))
  10417. (goto-char (match-beginning 0))
  10418. (replace-match "")
  10419. (if char
  10420. (progn
  10421. (setq org-time-was-given (equal (upcase char) char))
  10422. (setq time (org-read-date (equal (upcase char) "U") t nil
  10423. prompt))
  10424. (org-insert-time-stamp time org-time-was-given
  10425. (member char '("u" "U"))
  10426. nil nil (list org-end-time-was-given)))
  10427. (insert (read-string
  10428. (if prompt (concat prompt ": ") "Enter string")))))
  10429. (goto-char (point-min))
  10430. (if (re-search-forward "%\\?" nil t)
  10431. (replace-match "")
  10432. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  10433. (org-mode)
  10434. (org-set-local 'org-finish-function 'remember-buffer)))
  10435. ;;;###autoload
  10436. (defun org-remember ()
  10437. "Call `remember'. If this is already a remember buffer, re-apply template.
  10438. If there is an active region, make sure remember uses it as initial content
  10439. of the remember buffer."
  10440. (interactive)
  10441. (if (eq org-finish-function 'remember-buffer)
  10442. (progn
  10443. (when (< (length org-remember-templates) 2)
  10444. (error "No other template available"))
  10445. (erase-buffer)
  10446. (let ((annotation (plist-get org-store-link-plist :annotation))
  10447. (initial (plist-get org-store-link-plist :initial)))
  10448. (org-remember-apply-template))
  10449. (message "Press C-c C-c to remember data"))
  10450. (if (org-region-active-p)
  10451. (remember (buffer-substring (point) (mark)))
  10452. (call-interactively 'remember))))
  10453. ;;;###autoload
  10454. (defun org-remember-handler ()
  10455. "Store stuff from remember.el into an org file.
  10456. First prompts for an org file. If the user just presses return, the value
  10457. of `org-default-notes-file' is used.
  10458. Then the command offers the headings tree of the selected file in order to
  10459. file the text at a specific location.
  10460. You can either immediately press RET to get the note appended to the
  10461. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  10462. find a better place. Then press RET or <left> or <right> in insert the note.
  10463. Key Cursor position Note gets inserted
  10464. -----------------------------------------------------------------------------
  10465. RET buffer-start as level 2 heading at end of file
  10466. RET on headline as sublevel of the heading at cursor
  10467. RET no heading at cursor position, level taken from context.
  10468. Or use prefix arg to specify level manually.
  10469. <left> on headline as same level, before current heading
  10470. <right> on headline as same level, after current heading
  10471. So the fastest way to store the note is to press RET RET to append it to
  10472. the default file. This way your current train of thought is not
  10473. interrupted, in accordance with the principles of remember.el. But with
  10474. little extra effort, you can push it directly to the correct location.
  10475. Before being stored away, the function ensures that the text has a
  10476. headline, i.e. a first line that starts with a \"*\". If not, a headline
  10477. is constructed from the current date and some additional data.
  10478. If the variable `org-adapt-indentation' is non-nil, the entire text is
  10479. also indented so that it starts in the same column as the headline
  10480. \(i.e. after the stars).
  10481. See also the variable `org-reverse-note-order'."
  10482. (goto-char (point-min))
  10483. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  10484. (replace-match ""))
  10485. (catch 'quit
  10486. (let* ((txt (buffer-substring (point-min) (point-max)))
  10487. (fastp (equal current-prefix-arg '(4)))
  10488. (file (if fastp org-default-notes-file (org-get-org-file)))
  10489. (heading org-remember-default-headline)
  10490. (visiting (org-find-base-buffer-visiting file))
  10491. (org-startup-folded nil)
  10492. (org-startup-align-all-tables nil)
  10493. (org-goto-start-pos 1)
  10494. spos level indent reversed)
  10495. (setq current-prefix-arg nil)
  10496. ;; Modify text so that it becomes a nice subtree which can be inserted
  10497. ;; into an org tree.
  10498. (let* ((lines (split-string txt "\n"))
  10499. first)
  10500. (setq first (car lines) lines (cdr lines))
  10501. (if (string-match "^\\*+" first)
  10502. ;; Is already a headline
  10503. (setq indent nil)
  10504. ;; We need to add a headline: Use time and first buffer line
  10505. (setq lines (cons first lines)
  10506. first (concat "* " (current-time-string)
  10507. " (" (remember-buffer-desc) ")")
  10508. indent " "))
  10509. (if (and org-adapt-indentation indent)
  10510. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  10511. (setq txt (concat first "\n"
  10512. (mapconcat 'identity lines "\n"))))
  10513. ;; Find the file
  10514. (if (not visiting) (find-file-noselect file))
  10515. (with-current-buffer (or visiting (get-file-buffer file))
  10516. (save-excursion
  10517. (save-restriction
  10518. (widen)
  10519. (and (goto-char (point-min))
  10520. (not (re-search-forward "^\\* " nil t))
  10521. (insert "\n* Notes\n"))
  10522. (setq reversed (org-notes-order-reversed-p))
  10523. ;; Find the default location
  10524. (when (and heading (stringp heading) (string-match "\\S-" heading))
  10525. (goto-char (point-min))
  10526. (if (re-search-forward
  10527. (concat "^\\*+[ \t]+" (regexp-quote heading)
  10528. "\\([ \t]+:[a-zA-Z0-9@_:]*\\)?[ \t]*$")
  10529. nil t)
  10530. (setq org-goto-start-pos (match-beginning 0))))
  10531. ;; Ask the User for a location
  10532. (setq spos (if fastp
  10533. org-goto-start-pos
  10534. (org-get-location (current-buffer) org-remember-help)))
  10535. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  10536. ; not handle this note
  10537. (goto-char spos)
  10538. (cond ((and (bobp) (not reversed))
  10539. ;; Put it at the end, one level below level 1
  10540. (save-restriction
  10541. (widen)
  10542. (goto-char (point-max))
  10543. (if (not (bolp)) (newline))
  10544. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  10545. ((and (bobp) reversed)
  10546. ;; Put it at the start, as level 1
  10547. (save-restriction
  10548. (widen)
  10549. (goto-char (point-min))
  10550. (re-search-forward "^\\*" nil t)
  10551. (beginning-of-line 1)
  10552. (org-paste-subtree 1 txt)))
  10553. ((and (org-on-heading-p t) (not current-prefix-arg))
  10554. ;; Put it below this entry, at the beg/end of the subtree
  10555. (org-back-to-heading t)
  10556. (setq level (funcall outline-level))
  10557. (if reversed
  10558. (outline-next-heading)
  10559. (org-end-of-subtree t))
  10560. (if (not (bolp)) (newline))
  10561. (beginning-of-line 1)
  10562. (org-paste-subtree (org-get-legal-level level 1) txt))
  10563. (t
  10564. ;; Put it right there, with automatic level determined by
  10565. ;; org-paste-subtree or from prefix arg
  10566. (org-paste-subtree
  10567. (if (numberp current-prefix-arg) current-prefix-arg)
  10568. txt)))
  10569. (when remember-save-after-remembering
  10570. (save-buffer)
  10571. (if (not visiting) (kill-buffer (current-buffer)))))))))
  10572. t) ;; return t to indicate that we took care of this note.
  10573. (defun org-get-org-file ()
  10574. "Read a filename, with default directory `org-directory'."
  10575. (let ((default (or org-default-notes-file remember-data-file)))
  10576. (read-file-name (format "File name [%s]: " default)
  10577. (file-name-as-directory org-directory)
  10578. default)))
  10579. (defun org-notes-order-reversed-p ()
  10580. "Check if the current file should receive notes in reversed order."
  10581. (cond
  10582. ((not org-reverse-note-order) nil)
  10583. ((eq t org-reverse-note-order) t)
  10584. ((not (listp org-reverse-note-order)) nil)
  10585. (t (catch 'exit
  10586. (let ((all org-reverse-note-order)
  10587. entry)
  10588. (while (setq entry (pop all))
  10589. (if (string-match (car entry) buffer-file-name)
  10590. (throw 'exit (cdr entry))))
  10591. nil)))))
  10592. ;;;; Dynamic blocks
  10593. (defun org-find-dblock (name)
  10594. "Find the first dynamic block with name NAME in the buffer.
  10595. If not found, stay at current position and return nil."
  10596. (let (pos)
  10597. (save-excursion
  10598. (goto-char (point-min))
  10599. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  10600. nil t)
  10601. (match-beginning 0))))
  10602. (if pos (goto-char pos))
  10603. pos))
  10604. (defconst org-dblock-start-re
  10605. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10606. "Matches the startline of a dynamic block, with parameters.")
  10607. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  10608. "Matches the end of a dyhamic block.")
  10609. (defun org-create-dblock (plist)
  10610. "Create a dynamic block section, with parameters taken from PLIST.
  10611. PLIST must containe a :name entry which is used as name of the block."
  10612. (unless (bolp) (newline))
  10613. (let ((name (plist-get plist :name)))
  10614. (insert "#+BEGIN: " name)
  10615. (while plist
  10616. (if (eq (car plist) :name)
  10617. (setq plist (cddr plist))
  10618. (insert " " (prin1-to-string (pop plist)))))
  10619. (insert "\n\n#+END:\n")
  10620. (beginning-of-line -2)))
  10621. (defun org-prepare-dblock ()
  10622. "Prepare dynamic block for refresh.
  10623. This empties the block, puts the cursor at the insert position and returns
  10624. the property list including an extra property :name with the block name."
  10625. (unless (looking-at org-dblock-start-re)
  10626. (error "Not at a dynamic block"))
  10627. (let* ((begdel (1+ (match-end 0)))
  10628. (name (match-string 1))
  10629. (params (append (list :name name)
  10630. (read (concat "(" (match-string 3) ")")))))
  10631. (unless (re-search-forward org-dblock-end-re nil t)
  10632. (error "Dynamic block not terminated"))
  10633. (delete-region begdel (match-beginning 0))
  10634. (goto-char begdel)
  10635. (open-line 1)
  10636. params))
  10637. (defun org-map-dblocks (&optional command)
  10638. "Apply COMMAND to all dynamic blocks in the current buffer.
  10639. If COMMAND is not given, use `org-update-dblock'."
  10640. (let ((cmd (or command 'org-update-dblock))
  10641. pos)
  10642. (save-excursion
  10643. (goto-char (point-min))
  10644. (while (re-search-forward org-dblock-start-re nil t)
  10645. (goto-char (setq pos (match-beginning 0)))
  10646. (condition-case nil
  10647. (funcall cmd)
  10648. (error (message "Error during update of dynamic block")))
  10649. (goto-char pos)
  10650. (unless (re-search-forward org-dblock-end-re nil t)
  10651. (error "Dynamic block not terminated"))))))
  10652. (defun org-dblock-update (&optional arg)
  10653. "User command for updating dynamic blocks.
  10654. Update the dynamic block at point. With prefix ARG, update all dynamic
  10655. blocks in the buffer."
  10656. (interactive "P")
  10657. (if arg
  10658. (org-update-all-dblocks)
  10659. (or (looking-at org-dblock-start-re)
  10660. (org-beginning-of-dblock))
  10661. (org-update-dblock)))
  10662. (defun org-update-dblock ()
  10663. "Update the dynamic block at point
  10664. This means to empty the block, parse for parameters and then call
  10665. the correct writing function."
  10666. (let* ((pos (point))
  10667. (params (org-prepare-dblock))
  10668. (name (plist-get params :name))
  10669. (cmd (intern (concat "org-dblock-write:" name))))
  10670. (funcall cmd params)
  10671. (goto-char pos)))
  10672. (defun org-beginning-of-dblock ()
  10673. "Find the beginning of the dynamic block at point.
  10674. Error if there is no scuh block at point."
  10675. (let ((pos (point))
  10676. beg)
  10677. (end-of-line 1)
  10678. (if (and (re-search-backward org-dblock-start-re nil t)
  10679. (setq beg (match-beginning 0))
  10680. (re-search-forward org-dblock-end-re nil t)
  10681. (> (match-end 0) pos))
  10682. (goto-char beg)
  10683. (goto-char pos)
  10684. (error "Not in a dynamic block"))))
  10685. (defun org-update-all-dblocks ()
  10686. "Update all dynamic blocks in the buffer.
  10687. This function can be used in a hook."
  10688. (when (org-mode-p)
  10689. (org-map-dblocks 'org-update-dblock)))
  10690. ;;;; Completion
  10691. (defun org-complete (&optional arg)
  10692. "Perform completion on word at point.
  10693. At the beginning of a headline, this completes TODO keywords as given in
  10694. `org-todo-keywords'.
  10695. If the current word is preceded by a backslash, completes the TeX symbols
  10696. that are supported for HTML support.
  10697. If the current word is preceded by \"#+\", completes special words for
  10698. setting file options.
  10699. In the line after \"#+STARTUP:, complete valid keywords.\"
  10700. At all other locations, this simply calls `ispell-complete-word'."
  10701. (interactive "P")
  10702. (catch 'exit
  10703. (let* ((end (point))
  10704. (beg1 (save-excursion
  10705. (skip-chars-backward "a-zA-Z0-9_@")
  10706. (point)))
  10707. (beg (save-excursion
  10708. (skip-chars-backward "a-zA-Z0-9_:$")
  10709. (point)))
  10710. (confirm (lambda (x) (stringp (car x))))
  10711. (searchhead (equal (char-before beg) ?*))
  10712. (tag (equal (char-before beg1) ?:))
  10713. (texp (equal (char-before beg) ?\\))
  10714. (link (equal (char-before beg) ?\[))
  10715. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  10716. beg)
  10717. "#+"))
  10718. (startup (string-match "^#\\+STARTUP:.*"
  10719. (buffer-substring (point-at-bol) (point))))
  10720. (completion-ignore-case opt)
  10721. (type nil)
  10722. (tbl nil)
  10723. (table (cond
  10724. (opt
  10725. (setq type :opt)
  10726. (mapcar (lambda (x)
  10727. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  10728. (cons (match-string 2 x) (match-string 1 x)))
  10729. (org-split-string (org-get-current-options) "\n")))
  10730. (startup
  10731. (setq type :startup)
  10732. org-startup-options)
  10733. (link (append org-link-abbrev-alist-local
  10734. org-link-abbrev-alist))
  10735. (texp
  10736. (setq type :tex)
  10737. org-html-entities)
  10738. ((string-match "\\`\\*+[ \t]*\\'"
  10739. (buffer-substring (point-at-bol) beg))
  10740. (setq type :todo)
  10741. (mapcar 'list org-todo-keywords-1))
  10742. (searchhead
  10743. (setq type :searchhead)
  10744. (save-excursion
  10745. (goto-char (point-min))
  10746. (while (re-search-forward org-todo-line-regexp nil t)
  10747. (push (list
  10748. (org-make-org-heading-search-string
  10749. (match-string 3) t))
  10750. tbl)))
  10751. tbl)
  10752. (tag (setq type :tag beg beg1)
  10753. (or org-tag-alist (org-get-buffer-tags)))
  10754. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  10755. (pattern (buffer-substring-no-properties beg end))
  10756. (completion (try-completion pattern table confirm)))
  10757. (cond ((eq completion t)
  10758. (if (equal type :opt)
  10759. (insert (substring (cdr (assoc (upcase pattern) table))
  10760. (length pattern)))
  10761. (if (equal type :tag) (insert ":"))))
  10762. ((null completion)
  10763. (message "Can't find completion for \"%s\"" pattern)
  10764. (ding))
  10765. ((not (string= pattern completion))
  10766. (delete-region beg end)
  10767. (if (string-match " +$" completion)
  10768. (setq completion (replace-match "" t t completion)))
  10769. (insert completion)
  10770. (if (get-buffer-window "*Completions*")
  10771. (delete-window (get-buffer-window "*Completions*")))
  10772. (if (assoc completion table)
  10773. (if (eq type :todo) (insert " ")
  10774. (if (eq type :tag) (insert ":"))))
  10775. (if (and (equal type :opt) (assoc completion table))
  10776. (message "%s" (substitute-command-keys
  10777. "Press \\[org-complete] again to insert example settings"))))
  10778. (t
  10779. (message "Making completion list...")
  10780. (let ((list (sort (all-completions pattern table confirm)
  10781. 'string<)))
  10782. (with-output-to-temp-buffer "*Completions*"
  10783. (condition-case nil
  10784. ;; Protection needed for XEmacs and emacs 21
  10785. (display-completion-list list pattern)
  10786. (error (display-completion-list list)))))
  10787. (message "Making completion list...%s" "done"))))))
  10788. ;;;; TODO, DEADLINE, Comments
  10789. (defun org-toggle-comment ()
  10790. "Change the COMMENT state of an entry."
  10791. (interactive)
  10792. (save-excursion
  10793. (org-back-to-heading)
  10794. (if (looking-at (concat outline-regexp
  10795. "\\( +\\<" org-comment-string "\\>\\)"))
  10796. (replace-match "" t t nil 1)
  10797. (if (looking-at outline-regexp)
  10798. (progn
  10799. (goto-char (match-end 0))
  10800. (insert " " org-comment-string))))))
  10801. (defvar org-last-todo-state-is-todo nil
  10802. "This is non-nil when the last TODO state change led to a TODO state.
  10803. If the last change removed the TODO tag or switched to DONE, then
  10804. this is nil.")
  10805. (defun org-todo (&optional arg)
  10806. "Change the TODO state of an item.
  10807. The state of an item is given by a keyword at the start of the heading,
  10808. like
  10809. *** TODO Write paper
  10810. *** DONE Call mom
  10811. The different keywords are specified in the variable `org-todo-keywords'.
  10812. By default the available states are \"TODO\" and \"DONE\".
  10813. So for this example: when the item starts with TODO, it is changed to DONE.
  10814. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10815. DONE are present, add TODO at the beginning of the heading.
  10816. With C-u prefix arg, use completion to determine the new state.
  10817. With numeric prefix arg, switch to that state.
  10818. For calling through lisp, arg is also interpreted in the following way:
  10819. 'none -> empty state
  10820. \"\"(empty string) -> switch to empty state
  10821. 'done -> switch to DONE
  10822. 'nextset -> switch to the next set of keywords
  10823. 'previousset -> switch to the previous set of keywords
  10824. \"WAITING\" -> switch to the specified keyword, but only if it
  10825. really is a member of `org-todo-keywords'."
  10826. (interactive "P")
  10827. (save-excursion
  10828. (org-back-to-heading)
  10829. (if (looking-at outline-regexp) (goto-char (match-end 0)))
  10830. (or (looking-at (concat " +" org-todo-regexp " *"))
  10831. (looking-at " *"))
  10832. (let* ((this (match-string 1))
  10833. (head (org-get-todo-sequence-head this))
  10834. (ass (assoc head org-todo-kwd-alist))
  10835. (interpret (nth 1 ass))
  10836. (done-word (nth 3 ass))
  10837. (final-done-word (nth 4 ass))
  10838. (last-state (or this ""))
  10839. (completion-ignore-case t)
  10840. (member (member this org-todo-keywords-1))
  10841. (tail (cdr member))
  10842. (state (cond
  10843. ((equal arg '(4))
  10844. ;; Read a state with completion
  10845. (completing-read "State: " (mapcar (lambda(x) (list x))
  10846. org-todo-keywords-1)
  10847. nil t))
  10848. ((eq arg 'right)
  10849. (if this
  10850. (if tail (car tail) nil)
  10851. (car org-todo-keywords-1)))
  10852. ((eq arg 'left)
  10853. (if (equal member org-todo-keywords-1)
  10854. nil
  10855. (if this
  10856. (nth (- (length org-todo-keywords-1) (length tail) 2)
  10857. org-todo-keywords-1)
  10858. (org-last org-todo-keywords-1))))
  10859. (arg
  10860. ;; user or caller requests a specific state
  10861. (cond
  10862. ((equal arg "") nil)
  10863. ((eq arg 'none) nil)
  10864. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10865. ((eq arg 'nextset)
  10866. (or (car (cdr (member head org-todo-heads)))
  10867. (car org-todo-heads)))
  10868. ((eq arg 'previousset)
  10869. (let ((org-todo-heads (reverse org-todo-heads)))
  10870. (or (car (cdr (member head org-todo-heads)))
  10871. (car org-todo-heads))))
  10872. ((car (member arg org-todo-keywords-1)))
  10873. ((nth (1- (prefix-numeric-value arg))
  10874. org-todo-keywords-1))))
  10875. ((null member) (or head (car org-todo-keywords-1)))
  10876. ((equal this final-done-word) nil) ;; -> make empty
  10877. ((null tail) nil) ;; -> first entry
  10878. ((eq interpret 'sequence)
  10879. (car tail))
  10880. ((memq interpret '(type priority))
  10881. (if (eq this-command last-command)
  10882. (car tail)
  10883. (if (> (length tail) 0)
  10884. (or done-word (car org-done-keywords))
  10885. nil)))
  10886. (t nil)))
  10887. (next (if state (concat " " state " ") " "))
  10888. dostates)
  10889. (replace-match next t t)
  10890. (unless head
  10891. (setq head (org-get-todo-sequence-head state)
  10892. ass (assoc head org-todo-kwd-alist)
  10893. interpret (nth 1 ass)
  10894. done-word (nth 3 ass)
  10895. final-done-word (nth 4 ass)))
  10896. (when (memq arg '(nextset previousset))
  10897. (message "Keyword set: %s"
  10898. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  10899. (setq org-last-todo-state-is-todo
  10900. (not (member state org-done-keywords)))
  10901. (when (and org-log-done (not (memq arg '(nextset previousset))))
  10902. (setq dostates (and (eq interpret 'sequence)
  10903. (listp org-log-done) (memq 'state org-log-done)))
  10904. (cond
  10905. ((and state (not this))
  10906. (org-add-planning-info nil nil 'closed)
  10907. (and dostates (org-add-log-maybe 'state state 'findpos)))
  10908. ((and state dostates)
  10909. (org-add-log-maybe 'state state 'findpos))
  10910. ((member state org-done-keywords)
  10911. ;; Planning info calls the note-setting command.
  10912. (org-add-planning-info 'closed (org-current-time)
  10913. (if (org-get-repeat) nil 'scheduled))
  10914. (org-add-log-maybe 'done state 'findpos))))
  10915. ;; Fixup tag positioning
  10916. (and org-auto-align-tags (org-set-tags nil t))
  10917. (run-hooks 'org-after-todo-state-change-hook)
  10918. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  10919. (if (and arg (not (member state org-done-keywords)))
  10920. (setq head (org-get-todo-sequence-head state)))
  10921. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)))
  10922. ;; Fixup cursor location if close to the keyword
  10923. (if (and (outline-on-heading-p)
  10924. (not (bolp))
  10925. (save-excursion (beginning-of-line 1)
  10926. (looking-at org-todo-line-regexp))
  10927. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10928. (progn
  10929. (goto-char (or (match-end 2) (match-end 1)))
  10930. (just-one-space))))
  10931. (defun org-get-todo-sequence-head (kwd)
  10932. "Return the head of the TODO sequence to which KWD belongs.
  10933. If KWD is not set, check if there is a text property remembering the
  10934. right sequence."
  10935. (let (p)
  10936. (cond
  10937. ((not kwd)
  10938. (or (get-text-property (point-at-bol) 'org-todo-head)
  10939. (progn
  10940. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10941. nil (point-at-eol)))
  10942. (get-text-property p 'org-todo-head))))
  10943. ((not (member kwd org-todo-keywords-1))
  10944. (car org-todo-keywords-1))
  10945. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10946. (defun org-get-repeat ()
  10947. "Check if tere is a deadline/schedule with repeater in this entry."
  10948. (save-match-data
  10949. (save-excursion
  10950. (org-back-to-heading t)
  10951. (if (re-search-forward
  10952. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  10953. (match-string 1)))))
  10954. (defvar org-last-changed-timestamp)
  10955. (defvar org-log-post-message)
  10956. (defun org-auto-repeat-maybe ()
  10957. "Check if the current headline contains a repeated deadline/schedule.
  10958. If yes, set TODO state back to what it was and change the base date
  10959. of repeating deadline/scheduled time stamps to new date.
  10960. This function should be run in the `org-after-todo-state-change-hook'."
  10961. ;; last-state is dynamically scoped into this function
  10962. (let* ((repeat (org-get-repeat))
  10963. (aa (assoc last-state org-todo-kwd-alist))
  10964. (interpret (nth 1 aa))
  10965. (head (nth 2 aa))
  10966. (done-word (nth 3 aa))
  10967. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10968. (msg "Entry repeats: ")
  10969. (org-log-done)
  10970. re type n what ts)
  10971. (when repeat
  10972. (org-todo (if (eq interpret 'type) last-state head))
  10973. (when (and org-log-repeat
  10974. (not (memq 'org-add-log-note
  10975. (default-value 'post-command-hook))))
  10976. ;; Make sure a note is taken
  10977. (let ((org-log-done '(done)))
  10978. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  10979. 'findpos)))
  10980. (org-back-to-heading t)
  10981. (org-add-planning-info nil nil 'closed)
  10982. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10983. org-deadline-time-regexp "\\)"))
  10984. (while (re-search-forward
  10985. re (save-excursion (outline-next-heading) (point)) t)
  10986. (setq type (if (match-end 1) org-scheduled-string org-deadline-string)
  10987. ts (match-string (if (match-end 2) 2 4)))
  10988. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  10989. (setq n (string-to-number (match-string 1 ts))
  10990. what (match-string 2 ts))
  10991. (if (equal what "w") (setq n (* n 7) what "d"))
  10992. (org-timestamp-change n (cdr (assoc what whata))))
  10993. (setq msg (concat msg type org-last-changed-timestamp " ")))
  10994. (setq org-log-post-message msg)
  10995. (message msg))))
  10996. (defun org-show-todo-tree (arg)
  10997. "Make a compact tree which shows all headlines marked with TODO.
  10998. The tree will show the lines where the regexp matches, and all higher
  10999. headlines above the match.
  11000. With \\[universal-argument] prefix, also show the DONE entries.
  11001. With a numeric prefix N, construct a sparse tree for the Nth element
  11002. of `org-todo-keywords-1'."
  11003. (interactive "P")
  11004. (let ((case-fold-search nil)
  11005. (kwd-re
  11006. (cond ((null arg) org-not-done-regexp)
  11007. ((equal arg '(4))
  11008. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  11009. (mapcar 'list org-todo-keywords-1))))
  11010. (concat "\\("
  11011. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11012. "\\)\\>")))
  11013. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11014. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11015. org-todo-keywords-1)))
  11016. (t (error "Invalid prefix argument: %s" arg)))))
  11017. (message "%d TODO entries found"
  11018. (org-occur (concat "^" outline-regexp " +" kwd-re )))))
  11019. (defun org-deadline ()
  11020. "Insert the DEADLINE: string to make a deadline.
  11021. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  11022. to modify it to the correct date."
  11023. (interactive)
  11024. (org-add-planning-info 'deadline nil 'closed))
  11025. (defun org-schedule ()
  11026. "Insert the SCHEDULED: string to schedule a TODO item.
  11027. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  11028. to modify it to the correct date."
  11029. (interactive)
  11030. (org-add-planning-info 'scheduled nil 'closed))
  11031. (defun org-add-planning-info (what &optional time &rest remove)
  11032. "Insert new timestamp with keyword in the line directly after the headline.
  11033. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  11034. If non is given, the user is prompted for a date.
  11035. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11036. be removed."
  11037. (interactive)
  11038. (let (org-time-was-given org-end-time-was-given)
  11039. (when what (setq time (or time (org-read-date nil 'to-time))))
  11040. (when (and org-insert-labeled-timestamps-at-point
  11041. (member what '(scheduled deadline)))
  11042. (insert
  11043. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11044. (org-insert-time-stamp time org-time-was-given
  11045. nil nil nil (list org-end-time-was-given))
  11046. (setq what nil))
  11047. (save-excursion
  11048. (save-restriction
  11049. (let (col list elt ts buffer-invisibility-spec)
  11050. (org-back-to-heading t)
  11051. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  11052. (goto-char (match-end 1))
  11053. (setq col (current-column))
  11054. (goto-char (1+ (match-end 0)))
  11055. (if (and (not (looking-at outline-regexp))
  11056. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11057. "[^\r\n]*"))
  11058. (not (equal (match-string 1) org-clock-string)))
  11059. (narrow-to-region (match-beginning 0) (match-end 0))
  11060. (insert "\n")
  11061. (backward-char 1)
  11062. (narrow-to-region (point) (point))
  11063. (indent-to-column col))
  11064. ;; Check if we have to remove something.
  11065. (setq list (cons what remove))
  11066. (while list
  11067. (setq elt (pop list))
  11068. (goto-char (point-min))
  11069. (when (or (and (eq elt 'scheduled)
  11070. (re-search-forward org-scheduled-time-regexp nil t))
  11071. (and (eq elt 'deadline)
  11072. (re-search-forward org-deadline-time-regexp nil t))
  11073. (and (eq elt 'closed)
  11074. (re-search-forward org-closed-time-regexp nil t)))
  11075. (replace-match "")
  11076. (if (looking-at "--+<[^>]+>") (replace-match ""))
  11077. (if (looking-at " +") (replace-match ""))))
  11078. (goto-char (point-max))
  11079. (when what
  11080. (insert
  11081. (if (not (equal (char-before) ?\ )) " " "")
  11082. (cond ((eq what 'scheduled) org-scheduled-string)
  11083. ((eq what 'deadline) org-deadline-string)
  11084. ((eq what 'closed) org-closed-string))
  11085. " ")
  11086. (org-insert-time-stamp
  11087. time
  11088. (or org-time-was-given
  11089. (and (eq what 'closed) org-log-done-with-time))
  11090. (eq what 'closed)
  11091. nil nil (list org-end-time-was-given))
  11092. (end-of-line 1))
  11093. (goto-char (point-min))
  11094. (widen)
  11095. (if (looking-at "[ \t]+\r?\n")
  11096. (replace-match ""))
  11097. ts)))))
  11098. (defvar org-log-note-marker (make-marker))
  11099. (defvar org-log-note-purpose nil)
  11100. (defvar org-log-note-state nil)
  11101. (defvar org-log-note-window-configuration nil)
  11102. (defvar org-log-note-return-to (make-marker))
  11103. (defvar org-log-post-message nil
  11104. "Message to be displayed after a log note has been stored.
  11105. The auto-repeater uses this.")
  11106. (defun org-add-log-maybe (&optional purpose state findpos)
  11107. "Set up the post command hook to take a note."
  11108. (save-excursion
  11109. (when (and (listp org-log-done)
  11110. (memq purpose org-log-done))
  11111. (when findpos
  11112. (org-back-to-heading t)
  11113. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  11114. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11115. "[^\r\n]*\\)?"))
  11116. (goto-char (match-end 0)))
  11117. (move-marker org-log-note-marker (point))
  11118. (setq org-log-note-purpose purpose)
  11119. (setq org-log-note-state state)
  11120. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  11121. (defun org-add-log-note (&optional purpose)
  11122. "Pop up a window for taking a note, and add this note later at point."
  11123. (remove-hook 'post-command-hook 'org-add-log-note)
  11124. (setq org-log-note-window-configuration (current-window-configuration))
  11125. (delete-other-windows)
  11126. (move-marker org-log-note-return-to (point))
  11127. (switch-to-buffer (marker-buffer org-log-note-marker))
  11128. (goto-char org-log-note-marker)
  11129. (switch-to-buffer-other-window "*Org Note*")
  11130. (erase-buffer)
  11131. (let ((org-inhibit-startup t)) (org-mode))
  11132. (insert (format "# Insert note for %s, finish with C-c C-c.\n\n"
  11133. (cond
  11134. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11135. ((eq org-log-note-purpose 'done) "closed todo item")
  11136. ((eq org-log-note-purpose 'state) "state change")
  11137. (t (error "This should not happen")))))
  11138. (org-set-local 'org-finish-function 'org-store-log-note))
  11139. (defun org-store-log-note ()
  11140. "Finish taking a log note, and insert it to where it belongs."
  11141. (let ((txt (buffer-string))
  11142. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11143. lines ind)
  11144. (kill-buffer (current-buffer))
  11145. (if (string-match "^#.*\n[ \t\n]*" txt)
  11146. (setq txt (replace-match "" t t txt)))
  11147. (if (string-match "\\s-+\\'" txt)
  11148. (setq txt (replace-match "" t t txt)))
  11149. (setq lines (org-split-string txt "\n"))
  11150. (when (and note (string-match "\\S-" note))
  11151. (setq note
  11152. (org-replace-escapes
  11153. note
  11154. (list (cons "%u" (user-login-name))
  11155. (cons "%U" user-full-name)
  11156. (cons "%t" (format-time-string
  11157. (org-time-stamp-format 'long 'inactive)
  11158. (current-time)))
  11159. (cons "%s" (if org-log-note-state
  11160. (concat "\"" org-log-note-state "\"")
  11161. "")))))
  11162. (if lines (setq note (concat note " \\\\")))
  11163. (push note lines))
  11164. (when lines
  11165. (save-excursion
  11166. (set-buffer (marker-buffer org-log-note-marker))
  11167. (save-excursion
  11168. (goto-char org-log-note-marker)
  11169. (move-marker org-log-note-marker nil)
  11170. (end-of-line 1)
  11171. (if (not (bolp)) (insert "\n")) (indent-relative nil)
  11172. (setq ind (concat (buffer-substring (point-at-bol) (point)) " "))
  11173. (insert " - " (pop lines))
  11174. (while lines
  11175. (insert "\n" ind (pop lines)))))))
  11176. (set-window-configuration org-log-note-window-configuration)
  11177. (with-current-buffer (marker-buffer org-log-note-return-to)
  11178. (goto-char org-log-note-return-to))
  11179. (move-marker org-log-note-return-to nil)
  11180. (and org-log-post-message (message org-log-post-message)))
  11181. (defvar org-occur-highlights nil)
  11182. (make-variable-buffer-local 'org-occur-highlights)
  11183. (defun org-occur (regexp &optional keep-previous callback)
  11184. "Make a compact tree which shows all matches of REGEXP.
  11185. The tree will show the lines where the regexp matches, and all higher
  11186. headlines above the match. It will also show the heading after the match,
  11187. to make sure editing the matching entry is easy.
  11188. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11189. call to `org-occur' will be kept, to allow stacking of calls to this
  11190. command.
  11191. If CALLBACK is non-nil, it is a function which is called to confirm
  11192. that the match should indeed be shown."
  11193. (interactive "sRegexp: \nP")
  11194. (or keep-previous (org-remove-occur-highlights nil nil t))
  11195. (let ((cnt 0))
  11196. (save-excursion
  11197. (goto-char (point-min))
  11198. (if (or (not keep-previous) ; do not want to keep
  11199. (not org-occur-highlights)) ; no previous matches
  11200. ;; hide everything
  11201. (org-overview))
  11202. (while (re-search-forward regexp nil t)
  11203. (when (or (not callback)
  11204. (save-match-data (funcall callback)))
  11205. (setq cnt (1+ cnt))
  11206. (when org-highlight-sparse-tree-matches
  11207. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11208. (org-show-context 'occur-tree))))
  11209. (when org-remove-highlights-with-change
  11210. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11211. nil 'local))
  11212. (unless org-sparse-tree-open-archived-trees
  11213. (org-hide-archived-subtrees (point-min) (point-max)))
  11214. (run-hooks 'org-occur-hook)
  11215. (if (interactive-p)
  11216. (message "%d match(es) for regexp %s" cnt regexp))
  11217. cnt))
  11218. (defun org-show-context (&optional key)
  11219. "Make sure point and context and visible.
  11220. How much context is shown depends upon the variables
  11221. `org-show-hierarchy-above', `org-show-following-heading'. and
  11222. `org-show-siblings'."
  11223. (let ((heading-p (org-on-heading-p t))
  11224. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11225. (following-p (org-get-alist-option org-show-following-heading key))
  11226. (siblings-p (org-get-alist-option org-show-siblings key)))
  11227. (catch 'exit
  11228. ;; Show heading or entry text
  11229. (if heading-p
  11230. (org-flag-heading nil) ; only show the heading
  11231. (and (or (org-invisible-p) (org-invisible-p2))
  11232. (org-show-hidden-entry))) ; show entire entry
  11233. (when following-p
  11234. ;; Show next sibling, or heading below text
  11235. (save-excursion
  11236. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11237. (org-flag-heading nil))))
  11238. (when siblings-p (org-show-siblings))
  11239. (when hierarchy-p
  11240. ;; show all higher headings, possibly with siblings
  11241. (save-excursion
  11242. (while (and (condition-case nil
  11243. (progn (org-up-heading-all 1) t)
  11244. (error nil))
  11245. (not (bobp)))
  11246. (org-flag-heading nil)
  11247. (when siblings-p (org-show-siblings))))))))
  11248. (defun org-reveal (&optional siblings)
  11249. "Show current entry, hierarchy above it, and the following headline.
  11250. This can be used to show a consistent set of context around locations
  11251. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11252. not t for the search context.
  11253. With optional argument SIBLINGS, on each level of the hierarchy all
  11254. siblings are shown. This repairs the tree structure to what it would
  11255. look like when opened with hierarchical calls to `org-cycle'."
  11256. (interactive "P")
  11257. (let ((org-show-hierarchy-above t)
  11258. (org-show-following-heading t)
  11259. (org-show-siblings (if siblings t org-show-siblings)))
  11260. (org-show-context nil)))
  11261. (defun org-highlight-new-match (beg end)
  11262. "Highlight from BEG to END and mark the highlight is an occur headline."
  11263. (let ((ov (org-make-overlay beg end)))
  11264. (org-overlay-put ov 'face 'secondary-selection)
  11265. (push ov org-occur-highlights)))
  11266. (defun org-remove-occur-highlights (&optional beg end noremove)
  11267. "Remove the occur highlights from the buffer.
  11268. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11269. from the `before-change-functions' in the current buffer."
  11270. (interactive)
  11271. (unless org-inhibit-highlight-removal
  11272. (mapc 'org-delete-overlay org-occur-highlights)
  11273. (setq org-occur-highlights nil)
  11274. (unless noremove
  11275. (remove-hook 'before-change-functions
  11276. 'org-remove-occur-highlights 'local))))
  11277. ;;;; Priorities
  11278. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11279. "Regular expression matching the priority indicator.")
  11280. (defvar org-remove-priority-next-time nil)
  11281. (defun org-priority-up ()
  11282. "Increase the priority of the current item."
  11283. (interactive)
  11284. (org-priority 'up))
  11285. (defun org-priority-down ()
  11286. "Decrease the priority of the current item."
  11287. (interactive)
  11288. (org-priority 'down))
  11289. (defun org-priority (&optional action)
  11290. "Change the priority of an item by ARG.
  11291. ACTION can be set, up, or down."
  11292. (interactive)
  11293. (setq action (or action 'set))
  11294. (let (current new news have remove)
  11295. (save-excursion
  11296. (org-back-to-heading)
  11297. (if (looking-at org-priority-regexp)
  11298. (setq current (string-to-char (match-string 2))
  11299. have t)
  11300. (setq current org-default-priority))
  11301. (cond
  11302. ((eq action 'set)
  11303. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  11304. (setq new (read-char-exclusive))
  11305. (cond ((equal new ?\ ) (setq remove t))
  11306. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11307. (error "Priority must be between `%c' and `%c'"
  11308. org-highest-priority org-lowest-priority))))
  11309. ((eq action 'up)
  11310. (setq new (1- current)))
  11311. ((eq action 'down)
  11312. (setq new (1+ current)))
  11313. (t (error "Invalid action")))
  11314. (setq new (min (max org-highest-priority (upcase new)) org-lowest-priority))
  11315. (setq news (format "%c" new))
  11316. (if have
  11317. (if remove
  11318. (replace-match "" t t nil 1)
  11319. (replace-match news t t nil 2))
  11320. (if remove
  11321. (error "No priority cookie found in line")
  11322. (looking-at org-todo-line-regexp)
  11323. (if (match-end 2)
  11324. (progn
  11325. (goto-char (match-end 2))
  11326. (insert " [#" news "]"))
  11327. (goto-char (match-beginning 3))
  11328. (insert "[#" news "] ")))))
  11329. (if remove
  11330. (message "Priority removed")
  11331. (message "Priority of current item set to %s" news))))
  11332. (defun org-get-priority (s)
  11333. "Find priority cookie and return priority."
  11334. (save-match-data
  11335. (if (not (string-match org-priority-regexp s))
  11336. (* 1000 (- org-lowest-priority org-default-priority))
  11337. (* 1000 (- org-lowest-priority
  11338. (string-to-char (match-string 2 s)))))))
  11339. ;;;; Tags
  11340. (defun org-scan-tags (action matcher &optional todo-only)
  11341. "Scan headline tags with inheritance and produce output ACTION.
  11342. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  11343. evaluated, testing if a given set of tags qualifies a headline for
  11344. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  11345. are included in the output."
  11346. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  11347. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11348. "\\>\\)\\)? *\\(.*?\\)\\(:[a-zA-Z0-9_@:]+:\\)?[ \t]*$"))
  11349. (props (list 'face nil
  11350. 'done-face 'org-done
  11351. 'undone-face nil
  11352. 'mouse-face 'highlight
  11353. 'org-not-done-regexp org-not-done-regexp
  11354. 'org-todo-regexp org-todo-regexp
  11355. 'keymap org-agenda-keymap
  11356. 'help-echo
  11357. (format "mouse-2 or RET jump to org file %s"
  11358. (abbreviate-file-name buffer-file-name))))
  11359. (case-fold-search nil)
  11360. lspos
  11361. tags tags-list tags-alist (llast 0) rtn level category i txt
  11362. todo marker entry priority)
  11363. (save-excursion
  11364. (goto-char (point-min))
  11365. (when (eq action 'sparse-tree) (org-overview))
  11366. (while (re-search-forward re nil t)
  11367. (catch :skip
  11368. (setq todo (if (match-end 1) (match-string 2))
  11369. tags (if (match-end 4) (match-string 4)))
  11370. (goto-char (setq lspos (1+ (match-beginning 0))))
  11371. (setq level (funcall outline-level)
  11372. category (org-get-category))
  11373. (setq i llast llast level)
  11374. ;; remove tag lists from same and sublevels
  11375. (while (>= i level)
  11376. (when (setq entry (assoc i tags-alist))
  11377. (setq tags-alist (delete entry tags-alist)))
  11378. (setq i (1- i)))
  11379. ;; add the nex tags
  11380. (when tags
  11381. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  11382. tags-alist
  11383. (cons (cons level tags) tags-alist)))
  11384. ;; compile tags for current headline
  11385. (setq tags-list
  11386. (if org-use-tag-inheritance
  11387. (apply 'append (mapcar 'cdr tags-alist))
  11388. tags))
  11389. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  11390. (eval matcher)
  11391. (or (not org-agenda-skip-archived-trees)
  11392. (not (member org-archive-tag tags-list))))
  11393. (and (eq action 'agenda) (org-agenda-skip))
  11394. ;; list this headline
  11395. (if (eq action 'sparse-tree)
  11396. (progn
  11397. (org-show-context 'tags-tree))
  11398. (setq txt (org-format-agenda-item
  11399. ""
  11400. (concat
  11401. (if org-tags-match-list-sublevels
  11402. (make-string (1- level) ?.) "")
  11403. (org-get-heading))
  11404. category tags-list)
  11405. priority (org-get-priority txt))
  11406. (goto-char lspos)
  11407. (setq marker (org-agenda-new-marker))
  11408. (org-add-props txt props
  11409. 'org-marker marker 'org-hd-marker marker 'org-category category
  11410. 'priority priority 'type "tagsmatch")
  11411. (push txt rtn))
  11412. ;; if we are to skip sublevels, jump to end of subtree
  11413. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  11414. (when (and (eq action 'sparse-tree)
  11415. (not org-sparse-tree-open-archived-trees))
  11416. (org-hide-archived-subtrees (point-min) (point-max)))
  11417. (nreverse rtn)))
  11418. (defvar todo-only) ;; dynamically scoped
  11419. (defun org-tags-sparse-tree (&optional todo-only match)
  11420. "Create a sparse tree according to tags string MATCH.
  11421. MATCH can contain positive and negative selection of tags, like
  11422. \"+WORK+URGENT-WITHBOSS\".
  11423. If optional argument TODO_ONLY is non-nil, only select lines that are
  11424. also TODO lines."
  11425. (interactive "P")
  11426. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11427. (defun org-make-tags-matcher (match)
  11428. "Create the TAGS//TODO matcher form for the selection string MATCH."
  11429. ;; todo-only is scoped dynamically into this function, and the function
  11430. ;; may change it it the matcher asksk for it.
  11431. (unless match
  11432. ;; Get a new match request, with completion
  11433. (setq org-last-tags-completion-table
  11434. (or org-tag-alist
  11435. org-last-tags-completion-table))
  11436. (setq match (completing-read
  11437. "Match: " 'org-tags-completion-function nil nil nil
  11438. 'org-tags-history)))
  11439. ;; Parse the string and create a lisp form
  11440. (let ((match0 match)
  11441. (re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|[a-zA-Z0-9_@]+\\)")
  11442. minus tag mm
  11443. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11444. orterms term orlist re-p level-p)
  11445. (if (string-match "/+" match)
  11446. ;; match contains also a todo-matching request
  11447. (progn
  11448. (setq tagsmatch (substring match 0 (match-beginning 0))
  11449. todomatch (substring match (match-end 0)))
  11450. (if (string-match "^!" todomatch)
  11451. (setq todo-only t todomatch (substring todomatch 1)))
  11452. (if (string-match "^\\s-*$" todomatch)
  11453. (setq todomatch nil)))
  11454. ;; only matching tags
  11455. (setq tagsmatch match todomatch nil))
  11456. ;; Make the tags matcher
  11457. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11458. (setq tagsmatcher t)
  11459. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11460. (while (setq term (pop orterms))
  11461. (while (and (equal (substring term -1) "\\") orterms)
  11462. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11463. (while (string-match re term)
  11464. (setq minus (and (match-end 1)
  11465. (equal (match-string 1 term) "-"))
  11466. tag (match-string 2 term)
  11467. re-p (equal (string-to-char tag) ?{)
  11468. level-p (match-end 3)
  11469. mm (cond
  11470. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11471. (level-p `(= level ,(string-to-number
  11472. (match-string 3 term))))
  11473. (t `(member ,(downcase tag) tags-list)))
  11474. mm (if minus (list 'not mm) mm)
  11475. term (substring term (match-end 0)))
  11476. (push mm tagsmatcher))
  11477. (push (if (> (length tagsmatcher) 1)
  11478. (cons 'and tagsmatcher)
  11479. (car tagsmatcher))
  11480. orlist)
  11481. (setq tagsmatcher nil))
  11482. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist))))
  11483. ;; Make the todo matcher
  11484. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11485. (setq todomatcher t)
  11486. (setq orterms (org-split-string todomatch "|") orlist nil)
  11487. (while (setq term (pop orterms))
  11488. (while (string-match re term)
  11489. (setq minus (and (match-end 1)
  11490. (equal (match-string 1 term) "-"))
  11491. kwd (match-string 2 term)
  11492. re-p (equal (string-to-char kwd) ?{)
  11493. term (substring term (match-end 0))
  11494. mm (if re-p
  11495. `(string-match ,(substring kwd 1 -1) todo)
  11496. (list 'equal 'todo kwd))
  11497. mm (if minus (list 'not mm) mm))
  11498. (push mm todomatcher))
  11499. (push (if (> (length todomatcher) 1)
  11500. (cons 'and todomatcher)
  11501. (car todomatcher))
  11502. orlist)
  11503. (setq todomatcher nil))
  11504. (setq todomatcher (if (> (length orlist) 1)
  11505. (cons 'or orlist) (car orlist))))
  11506. ;; Return the string and lisp forms of the matcher
  11507. (setq matcher (if todomatcher
  11508. (list 'and tagsmatcher todomatcher)
  11509. tagsmatcher))
  11510. (cons match0 matcher)))
  11511. (defun org-match-any-p (re list)
  11512. "Does re match any element of list?"
  11513. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11514. (delq nil list))
  11515. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  11516. (defvar org-tags-overlay (org-make-overlay 1 1))
  11517. (org-detach-overlay org-tags-overlay)
  11518. (defun org-set-tags (&optional arg just-align)
  11519. "Set the tags for the current headline.
  11520. With prefix ARG, realign all tags in headings in the current buffer."
  11521. (interactive "P")
  11522. (let* ((re (concat "^" outline-regexp))
  11523. (current (org-get-tags))
  11524. table current-tags inherited-tags ; computed below when needed
  11525. tags p0 c0 c1 rpl)
  11526. (if arg
  11527. (save-excursion
  11528. (goto-char (point-min))
  11529. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11530. (while (re-search-forward re nil t)
  11531. (org-set-tags nil t)
  11532. (end-of-line 1)))
  11533. (message "All tags realigned to column %d" org-tags-column))
  11534. (if just-align
  11535. (setq tags current)
  11536. ;; Get a new set of tags from the user
  11537. (save-excursion
  11538. (setq table (or org-tag-alist (org-get-buffer-tags))
  11539. org-last-tags-completion-table table
  11540. current-tags (org-split-string current ":")
  11541. inherited-tags (nreverse
  11542. (nthcdr (length current-tags)
  11543. (nreverse (org-get-tags-at))))
  11544. tags
  11545. (if (or (eq t org-use-fast-tag-selection)
  11546. (and org-use-fast-tag-selection
  11547. (delq nil (mapcar 'cdr table))))
  11548. (org-fast-tag-selection current-tags inherited-tags table)
  11549. (let ((org-add-colon-after-tag-completion t))
  11550. (org-trim
  11551. (completing-read "Tags: " 'org-tags-completion-function
  11552. nil nil current 'org-tags-history))))))
  11553. (while (string-match "[-+&]+" tags)
  11554. ;; No boolean logic, just a list
  11555. (setq tags (replace-match ":" t t tags))))
  11556. (if (string-match "\\`[\t ]*\\'" tags)
  11557. (setq tags "")
  11558. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11559. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11560. ;; Insert new tags at the correct column
  11561. (beginning-of-line 1)
  11562. (if (re-search-forward
  11563. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11564. (point-at-eol) t)
  11565. (progn
  11566. (if (equal tags "")
  11567. (setq rpl "")
  11568. (goto-char (match-beginning 0))
  11569. (setq c0 (current-column) p0 (point)
  11570. c1 (max (1+ c0) (if (> org-tags-column 0)
  11571. org-tags-column
  11572. (- (- org-tags-column) (length tags))))
  11573. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11574. (replace-match rpl t t)
  11575. (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
  11576. tags)
  11577. (error "Tags alignment failed")))))
  11578. (defun org-tags-completion-function (string predicate &optional flag)
  11579. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11580. (confirm (lambda (x) (stringp (car x)))))
  11581. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11582. (setq s1 (match-string 1 string)
  11583. s2 (match-string 2 string))
  11584. (setq s1 "" s2 string))
  11585. (cond
  11586. ((eq flag nil)
  11587. ;; try completion
  11588. (setq rtn (try-completion s2 ctable confirm))
  11589. (if (stringp rtn)
  11590. (concat s1 s2 (substring rtn (length s2))
  11591. (if (and org-add-colon-after-tag-completion
  11592. (assoc rtn ctable))
  11593. ":" "")))
  11594. )
  11595. ((eq flag t)
  11596. ;; all-completions
  11597. (all-completions s2 ctable confirm)
  11598. )
  11599. ((eq flag 'lambda)
  11600. ;; exact match?
  11601. (assoc s2 ctable)))
  11602. ))
  11603. (defun org-fast-tag-insert (kwd tags face &optional end)
  11604. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  11605. (insert (format "%-12s" (concat kwd ":"))
  11606. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11607. (or end "")))
  11608. (defun org-fast-tag-show-exit (flag)
  11609. (save-excursion
  11610. (goto-line 3)
  11611. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11612. (replace-match ""))
  11613. (when flag
  11614. (end-of-line 1)
  11615. (move-to-column (- (window-width) 19) t)
  11616. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11617. (defun org-set-current-tags-overlay (current prefix)
  11618. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11619. (if (featurep 'xemacs)
  11620. (org-overlay-display org-tags-overlay (concat prefix s)
  11621. 'secondary-selection)
  11622. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11623. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11624. (defun org-fast-tag-selection (current inherited table)
  11625. "Fast tag selection with single keys.
  11626. CURRENT is the current list of tags in the headline, INHERITED is the
  11627. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11628. possibly with grouping information.
  11629. If the keys are nil, a-z are automatically assigned.
  11630. Returns the new tags string, or nil to not change the current settings."
  11631. (let* ((maxlen (apply 'max (mapcar
  11632. (lambda (x)
  11633. (if (stringp (car x)) (string-width (car x)) 0))
  11634. table)))
  11635. (buf (current-buffer))
  11636. (expert (eq org-fast-tag-selection-single-key 'expert))
  11637. (buffer-tags nil)
  11638. (fwidth (+ maxlen 3 1 3))
  11639. (ncol (/ (- (window-width) 4) fwidth))
  11640. (i-face 'org-done)
  11641. (c-face 'org-todo)
  11642. tg cnt e c char c1 c2 ntable tbl rtn
  11643. ov-start ov-end ov-prefix
  11644. (exit-after-next org-fast-tag-selection-single-key)
  11645. groups ingroup)
  11646. (save-excursion
  11647. (beginning-of-line 1)
  11648. (if (looking-at ".*[ \t]\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*\\(\r\\|$\\)")
  11649. (setq ov-start (match-beginning 1)
  11650. ov-end (match-end 1)
  11651. ov-prefix "")
  11652. (setq ov-start (1- (point-at-eol))
  11653. ov-end (1+ ov-start))
  11654. (skip-chars-forward "^\n\r")
  11655. (setq ov-prefix
  11656. (concat
  11657. (buffer-substring (1- (point)) (point))
  11658. (if (> (current-column) org-tags-column)
  11659. " "
  11660. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11661. (org-move-overlay org-tags-overlay ov-start ov-end)
  11662. (save-window-excursion
  11663. (if expert
  11664. (set-buffer (get-buffer-create " *Org tags*"))
  11665. (delete-other-windows)
  11666. (split-window-vertically)
  11667. (switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11668. (erase-buffer)
  11669. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11670. (org-fast-tag-insert "Current" current c-face "\n\n")
  11671. (org-fast-tag-show-exit exit-after-next)
  11672. (org-set-current-tags-overlay current ov-prefix)
  11673. (setq tbl table char ?a cnt 0)
  11674. (while (setq e (pop tbl))
  11675. (cond
  11676. ((equal e '(:startgroup))
  11677. (push '() groups) (setq ingroup t)
  11678. (when (not (= cnt 0))
  11679. (setq cnt 0)
  11680. (insert "\n"))
  11681. (insert "{ "))
  11682. ((equal e '(:endgroup))
  11683. (setq ingroup nil cnt 0)
  11684. (insert "}\n"))
  11685. (t
  11686. (setq tg (car e) c2 nil)
  11687. (if (cdr e)
  11688. (setq c (cdr e))
  11689. ;; automatically assign a character.
  11690. (setq c1 (string-to-char
  11691. (downcase (substring
  11692. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11693. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11694. (while (or (rassoc char ntable) (rassoc char table))
  11695. (setq char (1+ char)))
  11696. (setq c2 c1))
  11697. (setq c (or c2 char)))
  11698. (if ingroup (push tg (car groups)))
  11699. (setq tg (org-add-props tg nil 'face
  11700. (cond
  11701. ((member tg current) c-face)
  11702. ((member tg inherited) i-face)
  11703. (t nil))))
  11704. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11705. (insert "[" c "] " tg (make-string
  11706. (- fwidth 4 (length tg)) ?\ ))
  11707. (push (cons tg c) ntable)
  11708. (when (= (setq cnt (1+ cnt)) ncol)
  11709. (insert "\n")
  11710. (if ingroup (insert " "))
  11711. (setq cnt 0)))))
  11712. (setq ntable (nreverse ntable))
  11713. (insert "\n")
  11714. (goto-char (point-min))
  11715. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  11716. (fit-window-to-buffer))
  11717. (setq rtn
  11718. (catch 'exit
  11719. (while t
  11720. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  11721. (if groups " [!] no groups" " [!]groups")
  11722. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11723. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11724. (cond
  11725. ((= c ?\r) (throw 'exit t))
  11726. ((= c ?!)
  11727. (setq groups (not groups))
  11728. (goto-char (point-min))
  11729. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11730. ((= c ?\C-c)
  11731. (if (not expert)
  11732. (org-fast-tag-show-exit
  11733. (setq exit-after-next (not exit-after-next)))
  11734. (setq expert nil)
  11735. (delete-other-windows)
  11736. (split-window-vertically)
  11737. (switch-to-buffer-other-window " *Org tags*")
  11738. (and (fboundp 'fit-window-to-buffer)
  11739. (fit-window-to-buffer))))
  11740. ((or (= c ?\C-g)
  11741. (and (= c ?q) (not (rassoc c ntable))))
  11742. (org-detach-overlay org-tags-overlay)
  11743. (setq quit-flag t))
  11744. ((= c ?\ )
  11745. (setq current nil)
  11746. (if exit-after-next (setq exit-after-next 'now)))
  11747. ((= c ?\t)
  11748. (condition-case nil
  11749. (setq tg (completing-read
  11750. "Tag: "
  11751. (or buffer-tags
  11752. (with-current-buffer buf
  11753. (org-get-buffer-tags)))))
  11754. (quit (setq tg "")))
  11755. (when (string-match "\\S-" tg)
  11756. (add-to-list 'buffer-tags (list tg))
  11757. (if (member tg current)
  11758. (setq current (delete tg current))
  11759. (push tg current)))
  11760. (if exit-after-next (setq exit-after-next 'now)))
  11761. ((setq e (rassoc c ntable) tg (car e))
  11762. (if (member tg current)
  11763. (setq current (delete tg current))
  11764. (loop for g in groups do
  11765. (if (member tg g)
  11766. (mapcar (lambda (x)
  11767. (setq current (delete x current)))
  11768. g)))
  11769. (push tg current))
  11770. (if exit-after-next (setq exit-after-next 'now))))
  11771. ;; Create a sorted list
  11772. (setq current
  11773. (sort current
  11774. (lambda (a b)
  11775. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11776. (if (eq exit-after-next 'now) (throw 'exit t))
  11777. (goto-char (point-min))
  11778. (beginning-of-line 2)
  11779. (delete-region (point) (point-at-eol))
  11780. (org-fast-tag-insert "Current" current c-face)
  11781. (org-set-current-tags-overlay current ov-prefix)
  11782. (while (re-search-forward "\\[.\\] \\([a-zA-Z0-9_@]+\\)" nil t)
  11783. (setq tg (match-string 1))
  11784. (add-text-properties (match-beginning 1) (match-end 1)
  11785. (list 'face
  11786. (cond
  11787. ((member tg current) c-face)
  11788. ((member tg inherited) i-face)
  11789. (t nil)))))
  11790. (goto-char (point-min)))))
  11791. (org-detach-overlay org-tags-overlay)
  11792. (if rtn
  11793. (mapconcat 'identity current ":")
  11794. nil))))
  11795. (defun org-get-tags ()
  11796. "Get the TAGS string in the current headline."
  11797. (unless (org-on-heading-p t)
  11798. (error "Not on a heading"))
  11799. (save-excursion
  11800. (beginning-of-line 1)
  11801. (if (looking-at ".*[ \t]\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*\\(\r\\|$\\)")
  11802. (org-match-string-no-properties 1)
  11803. "")))
  11804. (defun org-get-buffer-tags ()
  11805. "Get a table of all tags used in the buffer, for completion."
  11806. (let (tags)
  11807. (save-excursion
  11808. (goto-char (point-min))
  11809. (while (re-search-forward "[ \t]:\\([a-zA-Z0-9_@:]+\\):[ \t\r\n]" nil t)
  11810. (mapc (lambda (x) (add-to-list 'tags x))
  11811. (org-split-string (org-match-string-no-properties 1) ":"))))
  11812. (mapcar 'list tags)))
  11813. ;;;; Timestamps
  11814. (defvar org-last-changed-timestamp nil)
  11815. (defvar org-time-was-given) ; dynamically scoped parameter
  11816. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11817. (defvar org-ts-what) ; dynamically scoped parameter
  11818. (defun org-time-stamp (arg)
  11819. "Prompt for a date/time and insert a time stamp.
  11820. If the user specifies a time like HH:MM, or if this command is called
  11821. with a prefix argument, the time stamp will contain date and time.
  11822. Otherwise, only the date will be included. All parts of a date not
  11823. specified by the user will be filled in from the current date/time.
  11824. So if you press just return without typing anything, the time stamp
  11825. will represent the current date/time. If there is already a timestamp
  11826. at the cursor, it will be modified."
  11827. (interactive "P")
  11828. (let (org-time-was-given org-end-time-was-given time)
  11829. (cond
  11830. ((and (org-at-timestamp-p)
  11831. (eq last-command 'org-time-stamp)
  11832. (eq this-command 'org-time-stamp))
  11833. (insert "--")
  11834. (setq time (let ((this-command this-command))
  11835. (org-read-date arg 'totime)))
  11836. (org-insert-time-stamp time (or org-time-was-given arg)))
  11837. ((org-at-timestamp-p)
  11838. (setq time (let ((this-command this-command))
  11839. (org-read-date arg 'totime)))
  11840. (when (org-at-timestamp-p) ; just to get the match data
  11841. (replace-match "")
  11842. (setq org-last-changed-timestamp
  11843. (org-insert-time-stamp
  11844. time (or org-time-was-given arg)
  11845. nil nil nil (list org-end-time-was-given))))
  11846. (message "Timestamp updated"))
  11847. (t
  11848. (setq time (let ((this-command this-command))
  11849. (org-read-date arg 'totime)))
  11850. (org-insert-time-stamp time (or org-time-was-given arg)
  11851. nil nil nil (list org-end-time-was-given))))))
  11852. (defun org-time-stamp-inactive (&optional arg)
  11853. "Insert an inactive time stamp.
  11854. An inactive time stamp is enclosed in square brackets instead of angle
  11855. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11856. does not link to the calendar and cannot be changed with the S-cursor keys.
  11857. So these are more for recording a certain time/date."
  11858. (interactive "P")
  11859. (let (org-time-was-given org-end-time-was-given time)
  11860. (setq time (org-read-date arg 'totime))
  11861. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  11862. nil nil (list org-end-time-was-given))))
  11863. (defvar org-date-ovl (org-make-overlay 1 1))
  11864. (org-overlay-put org-date-ovl 'face 'org-warning)
  11865. (org-detach-overlay org-date-ovl)
  11866. (defvar org-ans1) ; dynamically scoped parameter
  11867. (defvar org-ans2) ; dynamically scoped parameter
  11868. (defvar org-plain-time-of-day-regexp) ; defined below
  11869. (defun org-read-date (&optional with-time to-time from-string prompt)
  11870. "Read a date and make things smooth for the user.
  11871. The prompt will suggest to enter an ISO date, but you can also enter anything
  11872. which will at least partially be understood by `parse-time-string'.
  11873. Unrecognized parts of the date will default to the current day, month, year,
  11874. hour and minute. For example,
  11875. 3-2-5 --> 2003-02-05
  11876. feb 15 --> currentyear-02-15
  11877. sep 12 9 --> 2009-09-12
  11878. 12:45 --> today 12:45
  11879. 22 sept 0:34 --> currentyear-09-22 0:34
  11880. 12 --> currentyear-currentmonth-12
  11881. Fri --> nearest Friday (today or later)
  11882. +4 --> four days from today (only if +N is the only thing given)
  11883. etc.
  11884. The function understands only English month and weekday abbreviations,
  11885. but this can be configured with the variables `parse-time-months' and
  11886. `parse-time-weekdays'.
  11887. While prompting, a calendar is popped up - you can also select the
  11888. date with the mouse (button 1). The calendar shows a period of three
  11889. months. To scroll it to other months, use the keys `>' and `<'.
  11890. If you don't like the calendar, turn it off with
  11891. \(setq org-popup-calendar-for-date-prompt nil)
  11892. With optional argument TO-TIME, the date will immediately be converted
  11893. to an internal time.
  11894. With an optional argument WITH-TIME, the prompt will suggest to also
  11895. insert a time. Note that when WITH-TIME is not set, you can still
  11896. enter a time, and this function will inform the calling routine about
  11897. this change. The calling routine may then choose to change the format
  11898. used to insert the time stamp into the buffer to include the time."
  11899. (require 'parse-time)
  11900. (let* ((org-time-stamp-rounding-minutes
  11901. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  11902. (ct (org-current-time))
  11903. (default-time
  11904. ;; Default time is either today, or, when entering a range,
  11905. ;; the range start.
  11906. (if (save-excursion
  11907. (re-search-backward
  11908. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11909. (- (point) 20) t))
  11910. (apply
  11911. 'encode-time
  11912. (mapcar (lambda(x) (or x 0))
  11913. (parse-time-string (match-string 1))))
  11914. ct))
  11915. (calendar-move-hook nil)
  11916. (view-diary-entries-initially nil)
  11917. (view-calendar-holidays-initially nil)
  11918. (timestr (format-time-string
  11919. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  11920. (prompt (concat (if prompt (concat prompt " ") "")
  11921. (format "Date and/or time (default [%s]): " timestr)))
  11922. ans (org-ans0 "") org-ans1 org-ans2 (deltadays 0)
  11923. second minute hour day month year tl wday wday1 pm)
  11924. (cond
  11925. (from-string (setq ans from-string))
  11926. (org-popup-calendar-for-date-prompt
  11927. (save-excursion
  11928. (save-window-excursion
  11929. (calendar)
  11930. (calendar-forward-day (- (time-to-days default-time)
  11931. (calendar-absolute-from-gregorian
  11932. (calendar-current-date))))
  11933. (org-eval-in-calendar nil t)
  11934. (let* ((old-map (current-local-map))
  11935. (map (copy-keymap calendar-mode-map))
  11936. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11937. (org-defkey map (kbd "RET") 'org-calendar-select)
  11938. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11939. 'org-calendar-select-mouse)
  11940. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11941. 'org-calendar-select-mouse)
  11942. (org-defkey minibuffer-local-map [(meta shift left)]
  11943. (lambda () (interactive)
  11944. (org-eval-in-calendar '(calendar-backward-month 1))))
  11945. (org-defkey minibuffer-local-map [(meta shift right)]
  11946. (lambda () (interactive)
  11947. (org-eval-in-calendar '(calendar-forward-month 1))))
  11948. (org-defkey minibuffer-local-map [(shift up)]
  11949. (lambda () (interactive)
  11950. (org-eval-in-calendar '(calendar-backward-week 1))))
  11951. (org-defkey minibuffer-local-map [(shift down)]
  11952. (lambda () (interactive)
  11953. (org-eval-in-calendar '(calendar-forward-week 1))))
  11954. (org-defkey minibuffer-local-map [(shift left)]
  11955. (lambda () (interactive)
  11956. (org-eval-in-calendar '(calendar-backward-day 1))))
  11957. (org-defkey minibuffer-local-map [(shift right)]
  11958. (lambda () (interactive)
  11959. (org-eval-in-calendar '(calendar-forward-day 1))))
  11960. (org-defkey minibuffer-local-map ">"
  11961. (lambda () (interactive)
  11962. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11963. (org-defkey minibuffer-local-map "<"
  11964. (lambda () (interactive)
  11965. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11966. (unwind-protect
  11967. (progn
  11968. (use-local-map map)
  11969. (setq org-ans0 (read-string prompt "" nil nil))
  11970. ;; org-ans0: from prompt
  11971. ;; org-ans1: from mouse click
  11972. ;; org-ans2: from calendar motion
  11973. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11974. (use-local-map old-map))))))
  11975. (t ; Naked prompt only
  11976. (setq ans (read-string prompt "" nil timestr))))
  11977. (org-detach-overlay org-date-ovl)
  11978. (if (string-match "^[ \t]*[-+][0-9]+[ \t]*$" org-ans0)
  11979. (setq deltadays (string-to-number ans) ans ""))
  11980. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11981. (when (string-match
  11982. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11983. (setq year (if (match-end 2)
  11984. (string-to-number (match-string 2 ans))
  11985. (string-to-number (format-time-string "%Y")))
  11986. month (string-to-number (match-string 3 ans))
  11987. day (string-to-number (match-string 4 ans)))
  11988. (if (< year 100) (setq year (+ 2000 year)))
  11989. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11990. t nil ans)))
  11991. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11992. ;; If there is a time with am/pm, and *no* time without it, we convert
  11993. ;; so that matching will be successful.
  11994. ;; FIXME: make this replace twoce, so that we catch the end time.
  11995. (when (and (not (string-match "[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11996. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11997. (setq hour (string-to-number (match-string 1 ans))
  11998. minute (if (match-end 3) (string-to-number (match-string 3 ans)) 0)
  11999. pm (equal ?p (string-to-char (downcase (match-string 4 ans)))))
  12000. (if (and (= hour 12) (not pm))
  12001. (setq hour 0)
  12002. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12003. (setq ans (replace-match (format "%02d:%02d" hour minute) t t ans)))
  12004. ;; Check if there is a time range
  12005. (when (and (boundp 'org-end-time-was-given)
  12006. (string-match org-plain-time-of-day-regexp ans)
  12007. (match-end 8))
  12008. (setq org-end-time-was-given (match-string 8 ans))
  12009. (setq ans (concat (substring ans 0 (match-beginning 7))
  12010. (substring ans (match-end 7)))))
  12011. (setq tl (parse-time-string ans)
  12012. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  12013. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  12014. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  12015. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  12016. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  12017. second (or (nth 0 tl) 0)
  12018. wday (nth 6 tl))
  12019. (setq day (+ day deltadays))
  12020. (when (and wday (not (nth 3 tl)))
  12021. ;; Weekday was given, but no day, so pick that day in the week
  12022. ;; on or after the derived date.
  12023. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12024. (unless (equal wday wday1)
  12025. (setq day (+ day (% (- wday wday1 -7) 7)))))
  12026. (if (and (boundp 'org-time-was-given)
  12027. (nth 2 tl))
  12028. (setq org-time-was-given t))
  12029. (if (< year 100) (setq year (+ 2000 year)))
  12030. (if to-time
  12031. (encode-time second minute hour day month year)
  12032. (if (or (nth 1 tl) (nth 2 tl))
  12033. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  12034. (format "%04d-%02d-%02d" year month day)))))
  12035. (defun org-eval-in-calendar (form &optional keepdate)
  12036. "Eval FORM in the calendar window and return to current window.
  12037. Also, store the cursor date in variable org-ans2."
  12038. (let ((sw (selected-window)))
  12039. (select-window (get-buffer-window "*Calendar*"))
  12040. (eval form)
  12041. (when (and (not keepdate) (calendar-cursor-to-date))
  12042. (let* ((date (calendar-cursor-to-date))
  12043. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12044. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12045. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12046. (select-window sw)
  12047. ;; Update the prompt to show new default date
  12048. (save-excursion
  12049. (goto-char (point-min))
  12050. (when (and org-ans2
  12051. (re-search-forward "\\[[-0-9]+\\]" nil t)
  12052. (get-text-property (match-end 0) 'field))
  12053. (let ((inhibit-read-only t))
  12054. (replace-match (concat "[" org-ans2 "]") t t)
  12055. (add-text-properties (point-min) (1+ (match-end 0))
  12056. (text-properties-at (1+ (point-min)))))))))
  12057. (defun org-calendar-select ()
  12058. "Return to `org-read-date' with the date currently selected.
  12059. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12060. (interactive)
  12061. (when (calendar-cursor-to-date)
  12062. (let* ((date (calendar-cursor-to-date))
  12063. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12064. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12065. (if (active-minibuffer-window) (exit-minibuffer))))
  12066. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12067. "Insert a date stamp for the date given by the internal TIME.
  12068. WITH-HM means, use the stamp format that includes the time of the day.
  12069. INACTIVE means use square brackets instead of angular ones, so that the
  12070. stamp will not contribute to the agenda.
  12071. PRE and POST are optional strings to be inserted before and after the
  12072. stamp.
  12073. The command returns the inserted time stamp."
  12074. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12075. stamp)
  12076. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12077. (insert (or pre ""))
  12078. (insert (setq stamp (format-time-string fmt time)))
  12079. (when (listp extra)
  12080. (setq extra (car extra))
  12081. (if (and (stringp extra)
  12082. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12083. (setq extra (format "-%02d:%02d"
  12084. (string-to-number (match-string 1 extra))
  12085. (string-to-number (match-string 2 extra))))
  12086. (setq extra nil)))
  12087. (when extra
  12088. (backward-char 1)
  12089. (insert extra)
  12090. (forward-char 1))
  12091. (insert (or post ""))
  12092. stamp))
  12093. (defun org-toggle-time-stamp-overlays ()
  12094. "Toggle the use of custom time stamp formats."
  12095. (interactive)
  12096. (setq org-display-custom-times (not org-display-custom-times))
  12097. (unless org-display-custom-times
  12098. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12099. (while (setq p (next-single-property-change p 'display))
  12100. (if (and (get-text-property p 'display)
  12101. (eq (get-text-property p 'face) 'org-date))
  12102. (remove-text-properties
  12103. p (setq p (next-single-property-change p 'display))
  12104. '(display t))))
  12105. (set-buffer-modified-p bmp)))
  12106. (if (featurep 'xemacs)
  12107. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12108. (org-restart-font-lock)
  12109. (setq org-table-may-need-update t)
  12110. (if org-display-custom-times
  12111. (message "Time stamps are overlayed with custom format")
  12112. (message "Time stamp overlays removed")))
  12113. (defun org-display-custom-time (beg end)
  12114. "Overlay modified time stamp format over timestamp between BED and END."
  12115. (let* ((ts (buffer-substring beg end))
  12116. t1 w1 with-hm tf time str w2 (off 0))
  12117. (save-match-data
  12118. (setq t1 (org-parse-time-string ts t))
  12119. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  12120. (setq off (- (match-end 0) (match-beginning 0)))))
  12121. (setq end (- end off))
  12122. (setq w1 (- end beg)
  12123. with-hm (and (nth 1 t1) (nth 2 t1))
  12124. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12125. time (org-fix-decoded-time t1)
  12126. str (org-add-props
  12127. (format-time-string
  12128. (substring tf 1 -1) (apply 'encode-time time))
  12129. nil 'mouse-face 'highlight)
  12130. w2 (length str))
  12131. (if (not (= w2 w1))
  12132. (add-text-properties (1+ beg) (+ 2 beg)
  12133. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12134. (if (featurep 'xemacs)
  12135. (progn
  12136. (put-text-property beg end 'invisible t)
  12137. (put-text-property beg end 'end-glyph (make-glyph str)))
  12138. (put-text-property beg end 'display str))))
  12139. (defun org-translate-time (string)
  12140. "Translate all timestamps in STRING to custom format.
  12141. But do this only if the variable `org-display-custom-times' is set."
  12142. (when org-display-custom-times
  12143. (save-match-data
  12144. (let* ((start 0)
  12145. (re org-ts-regexp-both)
  12146. t1 with-hm inactive tf time str beg end)
  12147. (while (setq start (string-match re string start))
  12148. (setq beg (match-beginning 0)
  12149. end (match-end 0)
  12150. t1 (save-match-data
  12151. (org-parse-time-string (substring string beg end) t))
  12152. with-hm (and (nth 1 t1) (nth 2 t1))
  12153. inactive (equal (substring string beg (1+ beg)) "[")
  12154. tf (funcall (if with-hm 'cdr 'car)
  12155. org-time-stamp-custom-formats)
  12156. time (org-fix-decoded-time t1)
  12157. str (format-time-string
  12158. (concat
  12159. (if inactive "[" "<") (substring tf 1 -1)
  12160. (if inactive "]" ">"))
  12161. (apply 'encode-time time))
  12162. string (replace-match str t t string)
  12163. start (+ start (length str)))))))
  12164. string)
  12165. (defun org-fix-decoded-time (time)
  12166. "Set 0 instead of nil for the first 6 elements of time.
  12167. Don't touch the rest."
  12168. (let ((n 0))
  12169. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12170. (defun org-days-to-time (timestamp-string)
  12171. "Difference between TIMESTAMP-STRING and now in days."
  12172. (- (time-to-days (org-time-string-to-time timestamp-string))
  12173. (time-to-days (current-time))))
  12174. (defun org-deadline-close (timestamp-string &optional ndays)
  12175. "Is the time in TIMESTAMP-STRING close to the current date?"
  12176. (and (< (org-days-to-time timestamp-string)
  12177. (or ndays org-deadline-warning-days))
  12178. (not (org-entry-is-done-p))))
  12179. (defun org-calendar-select-mouse (ev)
  12180. "Return to `org-read-date' with the date currently selected.
  12181. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12182. (interactive "e")
  12183. (mouse-set-point ev)
  12184. (when (calendar-cursor-to-date)
  12185. (let* ((date (calendar-cursor-to-date))
  12186. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12187. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12188. (if (active-minibuffer-window) (exit-minibuffer))))
  12189. (defun org-check-deadlines (ndays)
  12190. "Check if there are any deadlines due or past due.
  12191. A deadline is considered due if it happens within `org-deadline-warning-days'
  12192. days from today's date. If the deadline appears in an entry marked DONE,
  12193. it is not shown. The prefix arg NDAYS can be used to test that many
  12194. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12195. (interactive "P")
  12196. (let* ((org-warn-days
  12197. (cond
  12198. ((equal ndays '(4)) 100000)
  12199. (ndays (prefix-numeric-value ndays))
  12200. (t org-deadline-warning-days)))
  12201. (case-fold-search nil)
  12202. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12203. (callback
  12204. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12205. (message "%d deadlines past-due or due within %d days"
  12206. (org-occur regexp nil callback)
  12207. org-warn-days)))
  12208. (defun org-evaluate-time-range (&optional to-buffer)
  12209. "Evaluate a time range by computing the difference between start and end.
  12210. Normally the result is just printed in the echo area, but with prefix arg
  12211. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12212. If the time range is actually in a table, the result is inserted into the
  12213. next column.
  12214. For time difference computation, a year is assumed to be exactly 365
  12215. days in order to avoid rounding problems."
  12216. (interactive "P")
  12217. (or
  12218. (org-clock-update-time-maybe)
  12219. (save-excursion
  12220. (unless (org-at-date-range-p)
  12221. (goto-char (point-at-bol))
  12222. (re-search-forward org-tr-regexp (point-at-eol) t))
  12223. (if (not (org-at-date-range-p))
  12224. (error "Not at a time-stamp range, and none found in current line")))
  12225. (let* ((ts1 (match-string 1))
  12226. (ts2 (match-string 2))
  12227. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12228. (match-end (match-end 0))
  12229. (time1 (org-time-string-to-time ts1))
  12230. (time2 (org-time-string-to-time ts2))
  12231. (t1 (time-to-seconds time1))
  12232. (t2 (time-to-seconds time2))
  12233. (diff (abs (- t2 t1)))
  12234. (negative (< (- t2 t1) 0))
  12235. ;; (ys (floor (* 365 24 60 60)))
  12236. (ds (* 24 60 60))
  12237. (hs (* 60 60))
  12238. (fy "%dy %dd %02d:%02d")
  12239. (fy1 "%dy %dd")
  12240. (fd "%dd %02d:%02d")
  12241. (fd1 "%dd")
  12242. (fh "%02d:%02d")
  12243. y d h m align)
  12244. (if havetime
  12245. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12246. y 0
  12247. d (floor (/ diff ds)) diff (mod diff ds)
  12248. h (floor (/ diff hs)) diff (mod diff hs)
  12249. m (floor (/ diff 60)))
  12250. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12251. y 0
  12252. d (floor (+ (/ diff ds) 0.5))
  12253. h 0 m 0))
  12254. (if (not to-buffer)
  12255. (message (org-make-tdiff-string y d h m))
  12256. (when (org-at-table-p)
  12257. (goto-char match-end)
  12258. (setq align t)
  12259. (and (looking-at " *|") (goto-char (match-end 0))))
  12260. (if (looking-at
  12261. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12262. (replace-match ""))
  12263. (if negative (insert " -"))
  12264. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12265. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12266. (insert " " (format fh h m))))
  12267. (if align (org-table-align))
  12268. (message "Time difference inserted")))))
  12269. (defun org-make-tdiff-string (y d h m)
  12270. (let ((fmt "")
  12271. (l nil))
  12272. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12273. l (push y l)))
  12274. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12275. l (push d l)))
  12276. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12277. l (push h l)))
  12278. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12279. l (push m l)))
  12280. (apply 'format fmt (nreverse l))))
  12281. (defun org-time-string-to-time (s)
  12282. (apply 'encode-time (org-parse-time-string s)))
  12283. (defun org-time-string-to-absolute (s &optional daynr)
  12284. "Convert a time stamp to an absolute day number.
  12285. If there is a specifyer for a cyclic time stamp, get the closest date to
  12286. DATE."
  12287. (cond
  12288. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12289. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12290. daynr
  12291. (+ daynr 1000)))
  12292. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12293. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12294. (time-to-days (current-time))) (match-string 0 s)))
  12295. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12296. (defun org-calendar-holiday ()
  12297. "List of holidays, for Diary display in Org-mode."
  12298. (let ((hl (check-calendar-holidays date)))
  12299. (if hl (mapconcat 'identity hl "; "))))
  12300. (defun org-diary-sexp-entry (sexp entry date)
  12301. "Process a SEXP diary ENTRY for DATE."
  12302. (let ((result (if calendar-debug-sexp
  12303. (let ((stack-trace-on-error t))
  12304. (eval (car (read-from-string sexp))))
  12305. (condition-case nil
  12306. (eval (car (read-from-string sexp)))
  12307. (error
  12308. (beep)
  12309. (message "Bad sexp at line %d in %s: %s"
  12310. (org-current-line)
  12311. (buffer-file-name) sexp)
  12312. (sleep-for 2))))))
  12313. (cond ((stringp result) result)
  12314. ((and (consp result)
  12315. (stringp (cdr result))) (cdr result))
  12316. (result entry)
  12317. (t nil))))
  12318. (defun org-diary-to-ical-string (frombuf)
  12319. "FIXME"
  12320. (let* ((tmpdir (if (featurep 'xemacs)
  12321. (temp-directory)
  12322. temporary-file-directory))
  12323. (tmpfile (make-temp-name
  12324. (expand-file-name "orgics" tmpdir)))
  12325. buf rtn b e)
  12326. (save-excursion
  12327. (set-buffer frombuf)
  12328. (icalendar-export-region (point-min) (point-max) tmpfile)
  12329. (setq buf (find-buffer-visiting tmpfile))
  12330. (set-buffer buf)
  12331. (goto-char (point-min))
  12332. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12333. (setq b (match-beginning 0)))
  12334. (goto-char (point-max))
  12335. (if (re-search-backward "^END:VEVENT" nil t)
  12336. (setq e (match-end 0)))
  12337. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12338. (kill-buffer buf)
  12339. (kill-buffer frombuf)
  12340. (delete-file tmpfile)
  12341. rtn))
  12342. (defun org-closest-date (start current change)
  12343. "Find the date closest to CURRENT that is consistent with START and CHANGE."
  12344. ;; Make the proper lists from the dates
  12345. (catch 'exit
  12346. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12347. dn dw sday cday n1 n2
  12348. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12349. (setq start (org-date-to-gregorian start)
  12350. current (org-date-to-gregorian current)
  12351. sday (calendar-absolute-from-gregorian start)
  12352. cday (calendar-absolute-from-gregorian current))
  12353. (if (<= cday sday) (throw 'exit sday))
  12354. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12355. (setq dn (string-to-number (match-string 1 change))
  12356. dw (cdr (assoc (match-string 2 change) a1)))
  12357. (error "Invalid change specifyer: %s" change))
  12358. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12359. (cond
  12360. ((eq dw 'day)
  12361. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12362. n2 (+ n1 dn)))
  12363. ((eq dw 'year)
  12364. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12365. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12366. (setq date1 (list m d y1)
  12367. n1 (calendar-absolute-from-gregorian date1)
  12368. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12369. n2 (calendar-absolute-from-gregorian date2)))
  12370. ((eq dw 'month)
  12371. ;; approx number of month between the tow dates
  12372. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12373. ;; How often does dn fit in there?
  12374. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12375. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12376. m (+ m nm)
  12377. ny (floor (/ m 12))
  12378. y (+ y ny)
  12379. m (- m (* ny 12)))
  12380. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12381. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12382. (setq m2 (+ m dn) y2 y)
  12383. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12384. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12385. (while (< n2 cday)
  12386. (setq n1 n2 m m2 y y2)
  12387. (setq m2 (+ m dn) y2 y)
  12388. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12389. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12390. (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1))))
  12391. (defun org-date-to-gregorian (date)
  12392. "Turn any specification of DATE into a gregorian date for the calendar."
  12393. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12394. ((and (listp date) (= (length date) 3)) date)
  12395. ((stringp date)
  12396. (setq date (org-parse-time-string date))
  12397. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12398. ((listp date)
  12399. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12400. (defun org-parse-time-string (s &optional nodefault)
  12401. "Parse the standard Org-mode time string.
  12402. This should be a lot faster than the normal `parse-time-string'.
  12403. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12404. hour and minute fields will be nil if not given."
  12405. (if (string-match org-ts-regexp0 s)
  12406. (list 0
  12407. (if (or (match-beginning 8) (not nodefault))
  12408. (string-to-number (or (match-string 8 s) "0")))
  12409. (if (or (match-beginning 7) (not nodefault))
  12410. (string-to-number (or (match-string 7 s) "0")))
  12411. (string-to-number (match-string 4 s))
  12412. (string-to-number (match-string 3 s))
  12413. (string-to-number (match-string 2 s))
  12414. nil nil nil)
  12415. (make-list 9 0)))
  12416. (defun org-timestamp-up (&optional arg)
  12417. "Increase the date item at the cursor by one.
  12418. If the cursor is on the year, change the year. If it is on the month or
  12419. the day, change that.
  12420. With prefix ARG, change by that many units."
  12421. (interactive "p")
  12422. (org-timestamp-change (prefix-numeric-value arg)))
  12423. (defun org-timestamp-down (&optional arg)
  12424. "Decrease the date item at the cursor by one.
  12425. If the cursor is on the year, change the year. If it is on the month or
  12426. the day, change that.
  12427. With prefix ARG, change by that many units."
  12428. (interactive "p")
  12429. (org-timestamp-change (- (prefix-numeric-value arg))))
  12430. (defun org-timestamp-up-day (&optional arg)
  12431. "Increase the date in the time stamp by one day.
  12432. With prefix ARG, change that many days."
  12433. (interactive "p")
  12434. (if (and (not (org-at-timestamp-p t))
  12435. (org-on-heading-p))
  12436. (org-todo 'up)
  12437. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12438. (defun org-timestamp-down-day (&optional arg)
  12439. "Decrease the date in the time stamp by one day.
  12440. With prefix ARG, change that many days."
  12441. (interactive "p")
  12442. (if (and (not (org-at-timestamp-p t))
  12443. (org-on-heading-p))
  12444. (org-todo 'down)
  12445. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12446. (defsubst org-pos-in-match-range (pos n)
  12447. (and (match-beginning n)
  12448. (<= (match-beginning n) pos)
  12449. (>= (match-end n) pos)))
  12450. (defun org-at-timestamp-p (&optional inactive-ok)
  12451. "Determine if the cursor is in or at a timestamp."
  12452. (interactive)
  12453. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12454. (pos (point))
  12455. (ans (or (looking-at tsr)
  12456. (save-excursion
  12457. (skip-chars-backward "^[<\n\r\t")
  12458. (if (> (point) 1) (backward-char 1))
  12459. (and (looking-at tsr)
  12460. (> (- (match-end 0) pos) -1))))))
  12461. (and (boundp 'org-ts-what)
  12462. (setq org-ts-what
  12463. (cond
  12464. ((org-pos-in-match-range pos 2) 'year)
  12465. ((org-pos-in-match-range pos 3) 'month)
  12466. ((org-pos-in-match-range pos 7) 'hour)
  12467. ((org-pos-in-match-range pos 8) 'minute)
  12468. ((or (org-pos-in-match-range pos 4)
  12469. (org-pos-in-match-range pos 5)) 'day)
  12470. ((and (> pos (match-end 8)) (< pos (match-end 0)))
  12471. (- pos (match-end 8)))
  12472. (t 'day))))
  12473. ans))
  12474. (defun org-timestamp-change (n &optional what)
  12475. "Change the date in the time stamp at point.
  12476. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12477. `year', `minute', `second'. If WHAT is not given, the cursor position
  12478. in the timestamp determines what will be changed."
  12479. (let ((pos (point))
  12480. with-hm inactive
  12481. org-ts-what
  12482. extra
  12483. ts time time0)
  12484. (if (not (org-at-timestamp-p t))
  12485. (error "Not at a timestamp"))
  12486. (if (and (not what) (not (eq org-ts-what 'day))
  12487. org-display-custom-times
  12488. (get-text-property (point) 'display)
  12489. (not (get-text-property (1- (point)) 'display)))
  12490. (setq org-ts-what 'day))
  12491. (setq org-ts-what (or what org-ts-what)
  12492. inactive (= (char-after (match-beginning 0)) ?\[)
  12493. ts (match-string 0))
  12494. (replace-match "")
  12495. (if (string-match
  12496. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( \\+[0-9]+[dwmy]\\)?\\)[]>]"
  12497. ts)
  12498. (setq extra (match-string 1 ts)))
  12499. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12500. (setq with-hm t))
  12501. (setq time0 (org-parse-time-string ts))
  12502. (setq time
  12503. (apply 'encode-time
  12504. (append
  12505. (list (or (car time0) 0))
  12506. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  12507. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  12508. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  12509. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  12510. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  12511. (nthcdr 6 time0))))
  12512. (when (integerp org-ts-what)
  12513. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  12514. (if (eq what 'calendar)
  12515. (let ((cal-date
  12516. (save-excursion
  12517. (save-match-data
  12518. (set-buffer "*Calendar*")
  12519. (calendar-cursor-to-date)))))
  12520. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12521. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12522. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12523. (setcar time0 (or (car time0) 0))
  12524. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12525. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  12526. (setq time (apply 'encode-time time0))))
  12527. (setq org-last-changed-timestamp
  12528. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12529. (org-clock-update-time-maybe)
  12530. (goto-char pos)
  12531. ;; Try to recenter the calendar window, if any
  12532. (if (and org-calendar-follow-timestamp-change
  12533. (get-buffer-window "*Calendar*" t)
  12534. (memq org-ts-what '(day month year)))
  12535. (org-recenter-calendar (time-to-days time)))))
  12536. (defun org-modify-ts-extra (s pos n)
  12537. "FIXME"
  12538. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12539. ng h m new)
  12540. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12541. (cond
  12542. ((or (org-pos-in-match-range pos 2)
  12543. (org-pos-in-match-range pos 3))
  12544. (setq m (string-to-number (match-string 3 s))
  12545. h (string-to-number (match-string 2 s)))
  12546. (if (org-pos-in-match-range pos 2)
  12547. (setq h (+ h n))
  12548. (setq m (+ m n)))
  12549. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12550. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12551. (setq h (min 24 (max 0 h)))
  12552. (setq ng 1 new (format "-%02d:%02d" h m)))
  12553. ((org-pos-in-match-range pos 6)
  12554. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12555. ((org-pos-in-match-range pos 5)
  12556. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  12557. (when ng
  12558. (setq s (concat
  12559. (substring s 0 (match-beginning ng))
  12560. new
  12561. (substring s (match-end ng))))))
  12562. s))
  12563. (defun org-recenter-calendar (date)
  12564. "If the calendar is visible, recenter it to DATE."
  12565. (let* ((win (selected-window))
  12566. (cwin (get-buffer-window "*Calendar*" t))
  12567. (calendar-move-hook nil))
  12568. (when cwin
  12569. (select-window cwin)
  12570. (calendar-goto-date (if (listp date) date
  12571. (calendar-gregorian-from-absolute date)))
  12572. (select-window win))))
  12573. (defun org-goto-calendar (&optional arg)
  12574. "Go to the Emacs calendar at the current date.
  12575. If there is a time stamp in the current line, go to that date.
  12576. A prefix ARG can be used to force the current date."
  12577. (interactive "P")
  12578. (let ((tsr org-ts-regexp) diff
  12579. (calendar-move-hook nil)
  12580. (view-calendar-holidays-initially nil)
  12581. (view-diary-entries-initially nil))
  12582. (if (or (org-at-timestamp-p)
  12583. (save-excursion
  12584. (beginning-of-line 1)
  12585. (looking-at (concat ".*" tsr))))
  12586. (let ((d1 (time-to-days (current-time)))
  12587. (d2 (time-to-days
  12588. (org-time-string-to-time (match-string 1)))))
  12589. (setq diff (- d2 d1))))
  12590. (calendar)
  12591. (calendar-goto-today)
  12592. (if (and diff (not arg)) (calendar-forward-day diff))))
  12593. (defun org-date-from-calendar ()
  12594. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12595. If there is already a time stamp at the cursor position, update it."
  12596. (interactive)
  12597. (org-timestamp-change 0 'calendar))
  12598. ;;; The clock for measuring work time.
  12599. (defvar org-mode-line-string "")
  12600. (put 'org-mode-line-string 'risky-local-variable t)
  12601. (defvar org-mode-line-timer nil)
  12602. (defvar org-clock-heading "")
  12603. (defvar org-clock-start-time "")
  12604. (defun org-update-mode-line ()
  12605. (let* ((delta (- (time-to-seconds (current-time))
  12606. (time-to-seconds org-clock-start-time)))
  12607. (h (floor delta 3600))
  12608. (m (floor (- delta (* 3600 h)) 60)))
  12609. (setq org-mode-line-string
  12610. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  12611. 'help-echo "Org-mode clock is running"))
  12612. (force-mode-line-update)))
  12613. (defvar org-clock-marker (make-marker)
  12614. "Marker recording the last clock-in.")
  12615. (defvar org-clock-mode-line-entry nil
  12616. "Information for the modeline about the running clock.")
  12617. (defun org-clock-in ()
  12618. "Start the clock on the current item.
  12619. If necessary, clock-out of the currently active clock."
  12620. (interactive)
  12621. (org-clock-out t)
  12622. (let (ts)
  12623. (save-excursion
  12624. (org-back-to-heading t)
  12625. (if (looking-at org-todo-line-regexp)
  12626. (setq org-clock-heading (match-string 3))
  12627. (setq org-clock-heading "???"))
  12628. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  12629. (beginning-of-line 2)
  12630. (when (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  12631. (not (equal (match-string 1) org-clock-string)))
  12632. ;; First line hast scheduling info, move one further
  12633. (beginning-of-line 2)
  12634. (or (bolp) (newline)))
  12635. (insert "\n") (backward-char 1)
  12636. (indent-relative)
  12637. (insert org-clock-string " ")
  12638. (setq org-clock-start-time (current-time))
  12639. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  12640. (move-marker org-clock-marker (point) (buffer-base-buffer))
  12641. (or global-mode-string (setq global-mode-string '("")))
  12642. (or (memq 'org-mode-line-string global-mode-string)
  12643. (setq global-mode-string
  12644. (append global-mode-string '(org-mode-line-string))))
  12645. (org-update-mode-line)
  12646. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  12647. (message "Clock started at %s" ts))))
  12648. (defun org-clock-out (&optional fail-quietly)
  12649. "Stop the currently running clock.
  12650. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  12651. (interactive)
  12652. (catch 'exit
  12653. (if (not (marker-buffer org-clock-marker))
  12654. (if fail-quietly (throw 'exit t) (error "No active clock")))
  12655. (let (ts te s h m)
  12656. (save-excursion
  12657. (set-buffer (marker-buffer org-clock-marker))
  12658. (goto-char org-clock-marker)
  12659. (beginning-of-line 1)
  12660. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  12661. (equal (match-string 1) org-clock-string))
  12662. (setq ts (match-string 2))
  12663. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  12664. (goto-char (match-end 0))
  12665. (delete-region (point) (point-at-eol))
  12666. (insert "--")
  12667. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  12668. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  12669. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  12670. h (floor (/ s 3600))
  12671. s (- s (* 3600 h))
  12672. m (floor (/ s 60))
  12673. s (- s (* 60 s)))
  12674. (insert " => " (format "%2d:%02d" h m))
  12675. (move-marker org-clock-marker nil)
  12676. (org-add-log-maybe 'clock-out)
  12677. (when org-mode-line-timer
  12678. (cancel-timer org-mode-line-timer)
  12679. (setq org-mode-line-timer nil))
  12680. (setq global-mode-string
  12681. (delq 'org-mode-line-string global-mode-string))
  12682. (force-mode-line-update)
  12683. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  12684. (defun org-clock-cancel ()
  12685. "Cancel the running clock be removing the start timestamp."
  12686. (interactive)
  12687. (if (not (marker-buffer org-clock-marker))
  12688. (error "No active clock"))
  12689. (save-excursion
  12690. (set-buffer (marker-buffer org-clock-marker))
  12691. (goto-char org-clock-marker)
  12692. (delete-region (1- (point-at-bol)) (point-at-eol)))
  12693. (message "Clock canceled"))
  12694. (defvar org-clock-file-total-minutes nil
  12695. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  12696. (make-variable-buffer-local 'org-clock-file-total-minutes)
  12697. (defun org-clock-sum (&optional tstart tend)
  12698. "Sum the times for each subtree.
  12699. Puts the resulting times in minutes as a text property on each headline."
  12700. (interactive)
  12701. (let* ((bmp (buffer-modified-p))
  12702. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  12703. org-clock-string
  12704. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  12705. (lmax 30)
  12706. (ltimes (make-vector lmax 0))
  12707. (t1 0)
  12708. (level 0)
  12709. ts te dt
  12710. time)
  12711. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  12712. (save-excursion
  12713. (goto-char (point-max))
  12714. (while (re-search-backward re nil t)
  12715. (cond
  12716. ((match-end 2)
  12717. ;; Two time stamps
  12718. (setq ts (match-string 2)
  12719. te (match-string 3)
  12720. ts (time-to-seconds
  12721. (apply 'encode-time (org-parse-time-string ts)))
  12722. te (time-to-seconds
  12723. (apply 'encode-time (org-parse-time-string te)))
  12724. ts (if tstart (max ts tstart) ts)
  12725. te (if tend (min te tend) te)
  12726. dt (- te ts)
  12727. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  12728. ((match-end 4)
  12729. ;; A naket time
  12730. (setq t1 (+ t1 (string-to-number (match-string 5))
  12731. (* 60 (string-to-number (match-string 4))))))
  12732. (t ;; A headline
  12733. (setq level (- (match-end 1) (match-beginning 1)))
  12734. (when (or (> t1 0) (> (aref ltimes level) 0))
  12735. (loop for l from 0 to level do
  12736. (aset ltimes l (+ (aref ltimes l) t1)))
  12737. (setq t1 0 time (aref ltimes level))
  12738. (loop for l from level to (1- lmax) do
  12739. (aset ltimes l 0))
  12740. (goto-char (match-beginning 0))
  12741. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  12742. (setq org-clock-file-total-minutes (aref ltimes 0)))
  12743. (set-buffer-modified-p bmp)))
  12744. (defun org-clock-display (&optional total-only)
  12745. "Show subtree times in the entire buffer.
  12746. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  12747. in the echo area."
  12748. (interactive)
  12749. (org-remove-clock-overlays)
  12750. (let (time h m p)
  12751. (org-clock-sum)
  12752. (unless total-only
  12753. (save-excursion
  12754. (goto-char (point-min))
  12755. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  12756. (goto-char p)
  12757. (when (setq time (get-text-property p :org-clock-minutes))
  12758. (org-put-clock-overlay time (funcall outline-level))))
  12759. (setq h (/ org-clock-file-total-minutes 60)
  12760. m (- org-clock-file-total-minutes (* 60 h)))
  12761. ;; Arrange to remove the overlays upon next change.
  12762. (when org-remove-highlights-with-change
  12763. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  12764. nil 'local))))
  12765. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  12766. (defvar org-clock-overlays nil)
  12767. (make-variable-buffer-local 'org-clock-overlays)
  12768. (defun org-put-clock-overlay (time &optional level)
  12769. "Put an overlays on the current line, displaying TIME.
  12770. If LEVEL is given, prefix time with a corresponding number of stars.
  12771. This creates a new overlay and stores it in `org-clock-overlays', so that it
  12772. will be easy to remove."
  12773. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  12774. (l (if level (org-get-legal-level level 0) 0))
  12775. (off 0)
  12776. ov tx)
  12777. (move-to-column c)
  12778. (unless (eolp) (skip-chars-backward "^ \t"))
  12779. (skip-chars-backward " \t")
  12780. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  12781. tx (concat (buffer-substring (1- (point)) (point))
  12782. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  12783. (org-add-props (format "%s %2d:%02d%s"
  12784. (make-string l ?*) h m
  12785. (make-string (- 10 l) ?\ ))
  12786. '(face secondary-selection))
  12787. ""))
  12788. (if (not (featurep 'xemacs))
  12789. (org-overlay-put ov 'display tx)
  12790. (org-overlay-put ov 'invisible t)
  12791. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  12792. (push ov org-clock-overlays)))
  12793. (defun org-remove-clock-overlays (&optional beg end noremove)
  12794. "Remove the occur highlights from the buffer.
  12795. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12796. from the `before-change-functions' in the current buffer."
  12797. (interactive)
  12798. (unless org-inhibit-highlight-removal
  12799. (mapc 'org-delete-overlay org-clock-overlays)
  12800. (setq org-clock-overlays nil)
  12801. (unless noremove
  12802. (remove-hook 'before-change-functions
  12803. 'org-remove-clock-overlays 'local))))
  12804. (defun org-clock-out-if-current ()
  12805. "Clock out if the current entry contains the running clock.
  12806. This is used to stop the clock after a TODO entry is marked DONE."
  12807. (when (and (member state org-done-keywords)
  12808. (equal (marker-buffer org-clock-marker) (current-buffer))
  12809. (< (point) org-clock-marker)
  12810. (> (save-excursion (outline-next-heading) (point))
  12811. org-clock-marker))
  12812. ;; Clock out, but don't accept a logging message for this.
  12813. (let ((org-log-done (if (and (listp org-log-done)
  12814. (member 'clock-out org-log-done))
  12815. '(done)
  12816. org-log-done)))
  12817. (org-clock-out))))
  12818. (add-hook 'org-after-todo-state-change-hook
  12819. 'org-clock-out-if-current)
  12820. (defun org-check-running-clock ()
  12821. "Check if the current buffer contains the running clock.
  12822. If yes, offer to stop it and to save the buffer with the changes."
  12823. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  12824. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  12825. (buffer-name))))
  12826. (org-clock-out)
  12827. (when (y-or-n-p "Save changed buffer?")
  12828. (save-buffer))))
  12829. (defun org-clock-report ()
  12830. "Create a table containing a report about clocked time.
  12831. If the buffer contains lines
  12832. #+BEGIN: clocktable :maxlevel 3 :emphasize nil
  12833. #+END: clocktable
  12834. then the table will be inserted between these lines, replacing whatever
  12835. is was there before. If these lines are not in the buffer, the table
  12836. is inserted at point, surrounded by the special lines.
  12837. The BEGIN line can contain parameters. Allowed are:
  12838. :maxlevel The maximum level to be included in the table. Default is 3.
  12839. :emphasize t/nil, if levell 1 and level 2 should be bold/italic in the table."
  12840. (interactive)
  12841. (org-remove-clock-overlays)
  12842. (unless (org-find-dblock "clocktable")
  12843. (org-create-dblock (list :name "clocktable"
  12844. :maxlevel 2 :emphasize nil)))
  12845. (org-update-dblock))
  12846. (defun org-clock-update-time-maybe ()
  12847. "If this is a CLOCK line, update it and return t.
  12848. Otherwise, return nil."
  12849. (interactive)
  12850. (save-excursion
  12851. (beginning-of-line 1)
  12852. (skip-chars-forward " \t")
  12853. (when (looking-at org-clock-string)
  12854. (let ((re (concat "[ \t]*" org-clock-string
  12855. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  12856. "\\([ \t]*=>.*\\)?"))
  12857. ts te h m s)
  12858. (if (not (looking-at re))
  12859. nil
  12860. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  12861. (end-of-line 1)
  12862. (setq ts (match-string 1)
  12863. te (match-string 2))
  12864. (setq s (- (time-to-seconds
  12865. (apply 'encode-time (org-parse-time-string te)))
  12866. (time-to-seconds
  12867. (apply 'encode-time (org-parse-time-string ts))))
  12868. h (floor (/ s 3600))
  12869. s (- s (* 3600 h))
  12870. m (floor (/ s 60))
  12871. s (- s (* 60 s)))
  12872. (insert " => " (format "%2d:%02d" h m))
  12873. t)))))
  12874. (defun org-clock-special-range (key &optional time as-strings)
  12875. "Return two times bordering a special time range.
  12876. Key is a symbol specifying the range and can be one of `today', `yesterday',
  12877. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  12878. A week starts Monday 0:00 and ends Sunday 24:00.
  12879. The range is determined relative to TIME. TIME defaults to the current time.
  12880. The return value is a cons cell with two internal times like the ones
  12881. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  12882. the returned times will be formatted strings."
  12883. (let* ((tm (decode-time (or time (current-time))))
  12884. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  12885. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  12886. (dow (nth 6 tm))
  12887. s1 m1 h1 d1 month1 y1 diff ts te fm)
  12888. (cond
  12889. ((eq key 'today)
  12890. (setq h 0 m 0 h1 24 m1 0))
  12891. ((eq key 'yesterday)
  12892. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  12893. ((eq key 'thisweek)
  12894. (setq diff (if (= dow 0) 6 (1- dow))
  12895. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  12896. ((eq key 'lastweek)
  12897. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  12898. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  12899. ((eq key 'thismonth)
  12900. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  12901. ((eq key 'lastmonth)
  12902. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  12903. ((eq key 'thisyear)
  12904. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  12905. ((eq key 'lastyear)
  12906. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  12907. (t (error "No such time block %s" key)))
  12908. (setq ts (encode-time s m h d month y)
  12909. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  12910. (or d1 d) (or month1 month) (or y1 y)))
  12911. (setq fm (cdr org-time-stamp-formats))
  12912. (if as-strings
  12913. (cons (format-time-string fm ts) (format-time-string fm te))
  12914. (cons ts te))))
  12915. (defun org-dblock-write:clocktable (params)
  12916. "Write the standard clocktable."
  12917. (let ((hlchars '((1 . "*") (2 . ?/)))
  12918. (emph nil)
  12919. (ins (make-marker))
  12920. ipos time h m p level hlc hdl maxlevel
  12921. ts te cc block)
  12922. (setq maxlevel (or (plist-get params :maxlevel) 3)
  12923. emph (plist-get params :emphasize)
  12924. ts (plist-get params :tstart)
  12925. te (plist-get params :tend)
  12926. block (plist-get params :block))
  12927. (when block
  12928. (setq cc (org-clock-special-range block nil t)
  12929. ts (car cc) te (cdr cc)))
  12930. (if ts (setq ts (time-to-seconds
  12931. (apply 'encode-time (org-parse-time-string ts)))))
  12932. (if te (setq te (time-to-seconds
  12933. (apply 'encode-time (org-parse-time-string te)))))
  12934. (move-marker ins (point))
  12935. (setq ipos (point))
  12936. (insert-before-markers "Clock summary at ["
  12937. (substring
  12938. (format-time-string (cdr org-time-stamp-formats))
  12939. 1 -1)
  12940. "]."
  12941. (if block
  12942. (format " Considered range is /%s/." block)
  12943. "")
  12944. "\n\n|L|Headline|Time|\n")
  12945. (org-clock-sum ts te)
  12946. (setq h (/ org-clock-file-total-minutes 60)
  12947. m (- org-clock-file-total-minutes (* 60 h)))
  12948. (insert-before-markers "|-\n|0|" "*Total file time*| "
  12949. (format "*%d:%02d*" h m)
  12950. "|\n")
  12951. (goto-char (point-min))
  12952. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  12953. (goto-char p)
  12954. (when (setq time (get-text-property p :org-clock-minutes))
  12955. (save-excursion
  12956. (beginning-of-line 1)
  12957. (when (and (looking-at "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[a-zA-Z0-9_@:]+:\\)?[ \t]*$")
  12958. (setq level (- (match-end 1) (match-beginning 1)))
  12959. (<= level maxlevel))
  12960. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  12961. hdl (match-string 2)
  12962. h (/ time 60)
  12963. m (- time (* 60 h)))
  12964. (goto-char ins)
  12965. (if (= level 1) (insert-before-markers "|-\n"))
  12966. (insert-before-markers
  12967. "| " (int-to-string level) "|" hlc hdl hlc " |"
  12968. (make-string (1- level) ?|)
  12969. hlc
  12970. (format "%d:%02d" h m)
  12971. hlc
  12972. " |\n")))))
  12973. (goto-char ins)
  12974. (backward-delete-char 1)
  12975. (goto-char ipos)
  12976. (skip-chars-forward "^|")
  12977. (org-table-align)))
  12978. ;; FIXME: I don't think anybody uses this, ask David
  12979. (defun org-collect-clock-time-entries ()
  12980. "Return an internal list with clocking information.
  12981. This list has one entry for each CLOCK interval.
  12982. FIXME: describe the elements."
  12983. (interactive)
  12984. (let ((re (concat "^[ \t]*" org-clock-string
  12985. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  12986. rtn beg end next cont level title total closedp leafp
  12987. clockpos titlepos h m donep)
  12988. (save-excursion
  12989. (org-clock-sum)
  12990. (goto-char (point-min))
  12991. (while (re-search-forward re nil t)
  12992. (setq clockpos (match-beginning 0)
  12993. beg (match-string 1) end (match-string 2)
  12994. cont (match-end 0))
  12995. (setq beg (apply 'encode-time (org-parse-time-string beg))
  12996. end (apply 'encode-time (org-parse-time-string end)))
  12997. (org-back-to-heading t)
  12998. (setq donep (org-entry-is-done-p))
  12999. (setq titlepos (point)
  13000. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  13001. h (/ total 60) m (- total (* 60 h))
  13002. total (cons h m))
  13003. (looking-at "\\(\\*+\\) +\\(.*\\)")
  13004. (setq level (- (match-end 1) (match-beginning 1))
  13005. title (org-match-string-no-properties 2))
  13006. (save-excursion (outline-next-heading) (setq next (point)))
  13007. (setq closedp (re-search-forward org-closed-time-regexp next t))
  13008. (goto-char next)
  13009. (setq leafp (and (looking-at "^\\*+ ")
  13010. (<= (- (match-end 0) (point)) level)))
  13011. (push (list beg end clockpos closedp donep
  13012. total title titlepos level leafp)
  13013. rtn)
  13014. (goto-char cont)))
  13015. (nreverse rtn)))
  13016. ;;;; Agenda, and Diary Integration
  13017. ;;; Define the Org-agenda-mode
  13018. (defvar org-agenda-mode-map (make-sparse-keymap)
  13019. "Keymap for `org-agenda-mode'.")
  13020. (defvar org-agenda-menu) ; defined later in this file.
  13021. (defvar org-agenda-follow-mode nil)
  13022. (defvar org-agenda-show-log nil)
  13023. (defvar org-agenda-redo-command nil)
  13024. (defvar org-agenda-mode-hook nil)
  13025. (defvar org-agenda-type nil)
  13026. (defvar org-agenda-force-single-file nil)
  13027. (defun org-agenda-mode ()
  13028. "Mode for time-sorted view on action items in Org-mode files.
  13029. The following commands are available:
  13030. \\{org-agenda-mode-map}"
  13031. (interactive)
  13032. (kill-all-local-variables)
  13033. (setq org-agenda-undo-list nil
  13034. org-agenda-pending-undo-list nil)
  13035. (setq major-mode 'org-agenda-mode)
  13036. ;; Keep global-font-lock-mode from turning on font-lock-mode
  13037. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  13038. (setq mode-name "Org-Agenda")
  13039. (use-local-map org-agenda-mode-map)
  13040. (easy-menu-add org-agenda-menu)
  13041. (if org-startup-truncated (setq truncate-lines t))
  13042. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  13043. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  13044. ;; Make sure properties are removed when copying text
  13045. (when (boundp 'buffer-substring-filters)
  13046. (org-set-local 'buffer-substring-filters
  13047. (cons (lambda (x)
  13048. (set-text-properties 0 (length x) nil x) x)
  13049. buffer-substring-filters)))
  13050. (unless org-agenda-keep-modes
  13051. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  13052. org-agenda-show-log nil))
  13053. (easy-menu-change
  13054. '("Agenda") "Agenda Files"
  13055. (append
  13056. (list
  13057. (vector
  13058. (if (get 'org-agenda-files 'org-restrict)
  13059. "Restricted to single file"
  13060. "Edit File List")
  13061. '(org-edit-agenda-file-list)
  13062. (not (get 'org-agenda-files 'org-restrict)))
  13063. "--")
  13064. (mapcar 'org-file-menu-entry (org-agenda-files))))
  13065. (org-agenda-set-mode-name)
  13066. (apply
  13067. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  13068. (list 'org-agenda-mode-hook)))
  13069. (substitute-key-definition 'undo 'org-agenda-undo
  13070. org-agenda-mode-map global-map)
  13071. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  13072. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  13073. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  13074. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  13075. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  13076. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  13077. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  13078. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  13079. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  13080. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  13081. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  13082. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  13083. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  13084. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  13085. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  13086. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  13087. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  13088. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  13089. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  13090. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  13091. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  13092. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  13093. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  13094. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  13095. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  13096. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  13097. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  13098. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  13099. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  13100. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  13101. (while l (org-defkey org-agenda-mode-map
  13102. (int-to-string (pop l)) 'digit-argument)))
  13103. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  13104. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  13105. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  13106. (org-defkey org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  13107. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  13108. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  13109. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  13110. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  13111. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  13112. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  13113. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  13114. (org-defkey org-agenda-mode-map "n" 'next-line)
  13115. (org-defkey org-agenda-mode-map "p" 'previous-line)
  13116. (org-defkey org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  13117. (org-defkey org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  13118. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  13119. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  13120. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  13121. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  13122. (eval-after-load "calendar"
  13123. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  13124. 'org-calendar-goto-agenda))
  13125. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  13126. (org-defkey org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
  13127. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  13128. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  13129. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  13130. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  13131. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  13132. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  13133. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  13134. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  13135. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  13136. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  13137. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  13138. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  13139. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  13140. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  13141. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  13142. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  13143. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  13144. "Local keymap for agenda entries from Org-mode.")
  13145. (org-defkey org-agenda-keymap
  13146. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  13147. (org-defkey org-agenda-keymap
  13148. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  13149. (when org-agenda-mouse-1-follows-link
  13150. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  13151. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  13152. '("Agenda"
  13153. ("Agenda Files")
  13154. "--"
  13155. ["Show" org-agenda-show t]
  13156. ["Go To (other window)" org-agenda-goto t]
  13157. ["Go To (this window)" org-agenda-switch-to t]
  13158. ["Follow Mode" org-agenda-follow-mode
  13159. :style toggle :selected org-agenda-follow-mode :active t]
  13160. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  13161. "--"
  13162. ["Cycle TODO" org-agenda-todo t]
  13163. ["Archive subtree" org-agenda-archive t]
  13164. ["Delete subtree" org-agenda-kill t]
  13165. "--"
  13166. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  13167. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  13168. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  13169. "--"
  13170. ("Tags"
  13171. ["Show all Tags" org-agenda-show-tags t]
  13172. ["Set Tags" org-agenda-set-tags t])
  13173. ("Date/Schedule"
  13174. ["Schedule" org-agenda-schedule t]
  13175. ["Set Deadline" org-agenda-deadline t]
  13176. "--"
  13177. ["Change date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  13178. ["Change date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  13179. ["Change date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  13180. ("Priority"
  13181. ["Set Priority" org-agenda-priority t]
  13182. ["Increase Priority" org-agenda-priority-up t]
  13183. ["Decrease Priority" org-agenda-priority-down t]
  13184. ["Show Priority" org-agenda-show-priority t])
  13185. ("Calendar/Diary"
  13186. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  13187. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  13188. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  13189. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  13190. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  13191. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  13192. "--"
  13193. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  13194. "--"
  13195. ("View"
  13196. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  13197. :style radio :selected (equal org-agenda-ndays 1)]
  13198. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  13199. :style radio :selected (equal org-agenda-ndays 7)]
  13200. "--"
  13201. ["Show Logbook entries" org-agenda-log-mode
  13202. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  13203. ["Include Diary" org-agenda-toggle-diary
  13204. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  13205. ["Use Time Grid" org-agenda-toggle-time-grid
  13206. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  13207. ["Write view to file" org-write-agenda t]
  13208. ["Rebuild buffer" org-agenda-redo t]
  13209. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  13210. "--"
  13211. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  13212. "--"
  13213. ["Quit" org-agenda-quit t]
  13214. ["Exit and Release Buffers" org-agenda-exit t]
  13215. ))
  13216. ;;; Agenda undo
  13217. (defvar org-agenda-allow-remote-undo t
  13218. "Non-nil means, allow remote undo from the agenda buffer.")
  13219. (defvar org-agenda-undo-list nil
  13220. "List of undoable operations in the agenda since last refresh.")
  13221. (defvar org-agenda-undo-has-started-in nil
  13222. "Buffers that have already seen `undo-start' in the current undo sequence.")
  13223. (defvar org-agenda-pending-undo-list nil
  13224. "In a series of undo commands, this is the list of remaning undo items.")
  13225. (defmacro org-if-unprotected (&rest body)
  13226. "Execute BODY if ther is no `org-protected' text property at point."
  13227. (declare (debug t))
  13228. `(unless (get-text-property (point) 'org-protected)
  13229. ,@body))
  13230. (defmacro org-unmodified (&rest body)
  13231. "Execute body without changing buffer-modified-p."
  13232. `(set-buffer-modified-p
  13233. (prog1 (buffer-modified-p) ,@body)))
  13234. (defmacro org-with-remote-undo (_buffer &rest _body)
  13235. "Execute BODY while recording undo information in two buffers."
  13236. (declare (indent 1) (debug t))
  13237. `(let ((_cline (org-current-line))
  13238. (_cmd this-command)
  13239. (_buf1 (current-buffer))
  13240. (_buf2 ,_buffer)
  13241. (_undo1 buffer-undo-list)
  13242. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  13243. _c1 _c2)
  13244. ,@_body
  13245. (when org-agenda-allow-remote-undo
  13246. (setq _c1 (org-verify-change-for-undo
  13247. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  13248. _c2 (org-verify-change-for-undo
  13249. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  13250. (when (or _c1 _c2)
  13251. ;; make sure there are undo boundaries
  13252. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  13253. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  13254. ;; remember which buffer to undo
  13255. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  13256. org-agenda-undo-list)))))
  13257. (defun org-agenda-undo ()
  13258. "Undo a remote editing step in the agenda.
  13259. This undoes changes both in the agenda buffer and in the remote buffer
  13260. that have been changed along."
  13261. (interactive)
  13262. (or org-agenda-allow-remote-undo
  13263. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  13264. (if (not (eq this-command last-command))
  13265. (setq org-agenda-undo-has-started-in nil
  13266. org-agenda-pending-undo-list org-agenda-undo-list))
  13267. (if (not org-agenda-pending-undo-list)
  13268. (error "No further undo information"))
  13269. (let* ((entry (pop org-agenda-pending-undo-list))
  13270. buf line cmd rembuf)
  13271. (setq cmd (pop entry) line (pop entry))
  13272. (setq rembuf (nth 2 entry))
  13273. (org-with-remote-undo rembuf
  13274. (while (bufferp (setq buf (pop entry)))
  13275. (if (pop entry)
  13276. (with-current-buffer buf
  13277. (let ((last-undo-buffer buf)
  13278. buffer-read-only)
  13279. (unless (memq buf org-agenda-undo-has-started-in)
  13280. (push buf org-agenda-undo-has-started-in)
  13281. (make-local-variable 'pending-undo-list)
  13282. (undo-start))
  13283. (while (and pending-undo-list
  13284. (listp pending-undo-list)
  13285. (not (car pending-undo-list)))
  13286. (pop pending-undo-list))
  13287. (undo-more 1))))))
  13288. (goto-line line)
  13289. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  13290. (defun org-verify-change-for-undo (l1 l2)
  13291. "Verify that a real change occurred between the undo lists L1 and L2."
  13292. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  13293. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  13294. (not (eq l1 l2)))
  13295. ;;; Agenda dispatch
  13296. (defvar org-agenda-restrict nil)
  13297. (defvar org-agenda-restrict-begin (make-marker))
  13298. (defvar org-agenda-restrict-end (make-marker))
  13299. (defvar org-agenda-last-dispatch-buffer nil)
  13300. ;;;###autoload
  13301. (defun org-agenda (arg)
  13302. "Dispatch agenda commands to collect entries to the agenda buffer.
  13303. Prompts for a character to select a command. Any prefix arg will be passed
  13304. on to the selected command. The default selections are:
  13305. g
  13306. a Call `org-agenda-list' to display the agenda for current day or week.
  13307. t Call `org-todo-list' to display the global todo list.
  13308. T Call `org-todo-list' to display the global todo list, select only
  13309. entries with a specific TODO keyword (the user gets a prompt).
  13310. m Call `org-tags-view' to display headlines with tags matching
  13311. a condition (the user is prompted for the condition).
  13312. M Like `m', but select only TODO entries, no ordinary headlines.
  13313. l Create a timeline for the current buffer.
  13314. e Export views to associated files.
  13315. More commands can be added by configuring the variable
  13316. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  13317. searches can be pre-defined in this way.
  13318. If the current buffer is in Org-mode and visiting a file, you can also
  13319. first press `1' to indicate that the agenda should be temporarily (until the
  13320. next use of \\[org-agenda]) restricted to the current file."
  13321. (interactive "P")
  13322. (catch 'exit
  13323. (let* ((buf (current-buffer))
  13324. (bfn (buffer-file-name (buffer-base-buffer)))
  13325. (restrict-ok (and bfn (org-mode-p)))
  13326. (custom org-agenda-custom-commands)
  13327. c entry key type match lprops)
  13328. ;; Turn off restriction
  13329. (put 'org-agenda-files 'org-restrict nil)
  13330. (setq org-agenda-restrict nil)
  13331. (move-marker org-agenda-restrict-begin nil)
  13332. (move-marker org-agenda-restrict-end nil)
  13333. ;; Remember where this call originated
  13334. (setq org-agenda-last-dispatch-buffer (current-buffer))
  13335. (save-window-excursion
  13336. (delete-other-windows)
  13337. (switch-to-buffer-other-window " *Agenda Commands*")
  13338. (erase-buffer)
  13339. (insert (eval-when-compile
  13340. (let ((header
  13341. "Press key for an agenda command:
  13342. -------------------------------- C Configure custom agenda commands
  13343. a Agenda for current week or day e Export agenda views
  13344. t List of all TODO entries T Entries with special TODO kwd
  13345. m Match a TAGS query M Like m, but only TODO entries
  13346. L Timeline for current buffer # List stuck projects (!=configure)
  13347. ")
  13348. (start 0))
  13349. (while (string-match "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)" header start)
  13350. (setq start (match-end 0))
  13351. (add-text-properties (match-beginning 2) (match-end 2)
  13352. '(face bold) header))
  13353. header)))
  13354. (while (setq entry (pop custom))
  13355. (setq key (car entry) type (nth 1 entry) match (nth 2 entry))
  13356. (insert (format "\n%-4s%-14s: %s"
  13357. (org-add-props (copy-sequence key)
  13358. '(face bold))
  13359. (cond
  13360. ((stringp type) type)
  13361. ((eq type 'agenda) "Agenda for current week or day")
  13362. ((eq type 'alltodo) "List of all TODO entries")
  13363. ((eq type 'stuck) "List of stuck projects")
  13364. ((eq type 'todo) "TODO keyword")
  13365. ((eq type 'tags) "Tags query")
  13366. ((eq type 'tags-todo) "Tags (TODO)")
  13367. ((eq type 'tags-tree) "Tags tree")
  13368. ((eq type 'todo-tree) "TODO kwd tree")
  13369. ((eq type 'occur-tree) "Occur tree")
  13370. ((functionp type) (symbol-name type))
  13371. (t "???"))
  13372. (if (stringp match)
  13373. (org-add-props match nil 'face 'org-warning)
  13374. (format "set of %d commands" (length match))))))
  13375. (if restrict-ok
  13376. (insert "\n"
  13377. (org-add-props "1 Restrict call to current buffer 0 Restrict call to region or subtree" nil 'face 'org-table)))
  13378. (goto-char (point-min))
  13379. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  13380. (message "Press key for agenda command%s"
  13381. (if restrict-ok ", or [1] or [0] to restrict" ""))
  13382. (setq c (read-char-exclusive))
  13383. (message "")
  13384. (when (memq c '(?L ?1 ?0))
  13385. (if restrict-ok
  13386. (put 'org-agenda-files 'org-restrict (list bfn))
  13387. (error "Cannot restrict agenda to current buffer"))
  13388. (with-current-buffer " *Agenda Commands*"
  13389. (goto-char (point-max))
  13390. (delete-region (point-at-bol) (point))
  13391. (goto-char (point-min)))
  13392. (when (eq c ?0)
  13393. (setq org-agenda-restrict t)
  13394. (with-current-buffer buf
  13395. (if (org-region-active-p)
  13396. (progn
  13397. (move-marker org-agenda-restrict-begin (region-beginning))
  13398. (move-marker org-agenda-restrict-end (region-end)))
  13399. (save-excursion
  13400. (org-back-to-heading t)
  13401. (move-marker org-agenda-restrict-begin (point))
  13402. (move-marker org-agenda-restrict-end
  13403. (progn (org-end-of-subtree t)))))))
  13404. (unless (eq c ?L)
  13405. (message "Press key for agenda command%s"
  13406. (if restrict-ok " (restricted to current file)" ""))
  13407. (setq c (read-char-exclusive)))
  13408. (message "")))
  13409. (require 'calendar) ; FIXME: can we avoid this for some commands?
  13410. ;; For example the todo list should not need it (but does...)
  13411. (cond
  13412. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  13413. (if (symbolp (nth 1 entry))
  13414. (progn
  13415. (setq type (nth 1 entry) match (nth 2 entry) lprops (nth 3 entry)
  13416. lprops (nth 3 entry))
  13417. (cond
  13418. ((eq type 'agenda)
  13419. (org-let lprops '(org-agenda-list current-prefix-arg)))
  13420. ((eq type 'alltodo)
  13421. (org-let lprops '(org-todo-list current-prefix-arg)))
  13422. ((eq type 'stuck)
  13423. (org-let lprops '(org-agenda-list-stuck-projects
  13424. current-prefix-arg)))
  13425. ((eq type 'tags)
  13426. (org-let lprops '(org-tags-view current-prefix-arg match)))
  13427. ((eq type 'tags-todo)
  13428. (org-let lprops '(org-tags-view '(4) match)))
  13429. ((eq type 'todo)
  13430. (org-let lprops '(org-todo-list match)))
  13431. ((eq type 'tags-tree)
  13432. (org-check-for-org-mode)
  13433. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  13434. ((eq type 'todo-tree)
  13435. (org-check-for-org-mode)
  13436. (org-let lprops
  13437. '(org-occur (concat "^" outline-regexp "[ \t]*"
  13438. (regexp-quote match) "\\>"))))
  13439. ((eq type 'occur-tree)
  13440. (org-check-for-org-mode)
  13441. (org-let lprops '(org-occur match)))
  13442. ((fboundp type)
  13443. (org-let lprops '(funcall type match)))
  13444. (t (error "Invalid custom agenda command type %s" type))))
  13445. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  13446. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  13447. ((equal c ?a) (call-interactively 'org-agenda-list))
  13448. ((equal c ?t) (call-interactively 'org-todo-list))
  13449. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  13450. ((equal c ?m) (call-interactively 'org-tags-view))
  13451. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  13452. ((equal c ?e) (call-interactively 'org-store-agenda-views))
  13453. ((equal c ?L)
  13454. (unless restrict-ok
  13455. (error "This is not an Org-mode file"))
  13456. (org-call-with-arg 'org-timeline arg))
  13457. ((equal c ?#) (call-interactively 'org-agenda-list-stuck-projects))
  13458. ((equal c ?!) (customize-variable 'org-stuck-projects))
  13459. (t (error "Invalid key"))))))
  13460. (defun org-run-agenda-series (name series)
  13461. (org-prepare-agenda name)
  13462. (let* ((org-agenda-multi t)
  13463. (redo (list 'org-run-agenda-series name (list 'quote series)))
  13464. (cmds (car series))
  13465. (gprops (nth 1 series))
  13466. match ;; The byte compiler incorrectly complains about this. Keep it!
  13467. cmd type lprops)
  13468. (while (setq cmd (pop cmds))
  13469. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  13470. (cond
  13471. ((eq type 'agenda)
  13472. (org-let2 gprops lprops
  13473. '(call-interactively 'org-agenda-list)))
  13474. ((eq type 'alltodo)
  13475. (org-let2 gprops lprops
  13476. '(call-interactively 'org-todo-list)))
  13477. ((eq type 'stuck)
  13478. (org-let2 gprops lprops
  13479. '(call-interactively 'org-agenda-list-stuck-projects)))
  13480. ((eq type 'tags)
  13481. (org-let2 gprops lprops
  13482. '(org-tags-view current-prefix-arg match)))
  13483. ((eq type 'tags-todo)
  13484. (org-let2 gprops lprops
  13485. '(org-tags-view '(4) match)))
  13486. ((eq type 'todo)
  13487. (org-let2 gprops lprops
  13488. '(org-todo-list match)))
  13489. ((fboundp type)
  13490. (org-let2 gprops lprops
  13491. '(funcall type match)))
  13492. (t (error "Invalid type in command series"))))
  13493. (widen)
  13494. (setq org-agenda-redo-command redo)
  13495. (goto-char (point-min)))
  13496. (org-finalize-agenda))
  13497. ;;;###autoload
  13498. (defmacro org-batch-agenda (cmd-key &rest parameters)
  13499. "Run an agenda command in batch mode and send the result to STDOUT.
  13500. If CMD-KEY is a string of length 1, it is used as a key in
  13501. `org-agenda-custom-commands' and triggers this command. If it is a
  13502. longer string is is used as a tags/todo match string.
  13503. Paramters are alternating variable names and values that will be bound
  13504. before running the agenda command."
  13505. (let (pars)
  13506. (while parameters
  13507. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  13508. (if (> (length cmd-key) 1)
  13509. (eval (list 'let (nreverse pars)
  13510. (list 'org-tags-view nil cmd-key)))
  13511. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  13512. (eval (list 'let (nreverse pars) '(org-agenda nil)))))
  13513. (set-buffer "*Org Agenda*")
  13514. (princ (org-encode-for-stdout (buffer-string)))))
  13515. (defun org-encode-for-stdout (string)
  13516. (if (fboundp 'encode-coding-string)
  13517. (encode-coding-string string buffer-file-coding-system)
  13518. string))
  13519. (defvar org-agenda-info nil)
  13520. ;;;###autoload
  13521. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  13522. "Run an agenda command in batch mode and send the result to STDOUT.
  13523. If CMD-KEY is a string of length 1, it is used as a key in
  13524. `org-agenda-custom-commands' and triggers this command. If it is a
  13525. longer string is is used as a tags/todo match string.
  13526. Paramters are alternating variable names and values that will be bound
  13527. before running the agenda command.
  13528. The output gives a line for each selected agenda item. Each
  13529. item is a list of comma-separated values, like this:
  13530. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  13531. category The category of the item
  13532. head The headline, without TODO kwd, TAGS and PRIORITY
  13533. type The type of the agenda entry, can be
  13534. todo selected in TODO match
  13535. tagsmatch selected in tags match
  13536. diary imported from diary
  13537. deadline a deadline on given date
  13538. scheduled scheduled on given date
  13539. timestamp entry has timestamp on given date
  13540. closed entry was closed on given date
  13541. upcoming-deadline warning about deadline
  13542. past-scheduled forwarded scheduled item
  13543. block entry has date block including g. date
  13544. todo The todo keyword, if any
  13545. tags All tags including inherited ones, separated by colons
  13546. date The relevant date, like 2007-2-14
  13547. time The time, like 15:00-16:50
  13548. extra Sting with extra planning info
  13549. priority-l The priority letter if any was given
  13550. priority-n The computed numerical priority
  13551. agenda-day The day in the agenda where this is listed"
  13552. (let (pars)
  13553. (while parameters
  13554. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  13555. (push (list 'org-agenda-remove-tags t) pars)
  13556. (if (> (length cmd-key) 1)
  13557. (eval (list 'let (nreverse pars)
  13558. (list 'org-tags-view nil cmd-key)))
  13559. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  13560. (eval (list 'let (nreverse pars) '(org-agenda nil)))))
  13561. (set-buffer "*Org Agenda*")
  13562. (let* ((lines (org-split-string (buffer-string) "\n"))
  13563. line)
  13564. (while (setq line (pop lines))
  13565. (catch 'next
  13566. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  13567. (setq org-agenda-info
  13568. (org-fix-agenda-info (text-properties-at 0 line)))
  13569. (princ
  13570. (org-encode-for-stdout
  13571. (mapconcat 'org-agenda-export-csv-mapper
  13572. '(org-category txt type todo tags date time-of-day extra
  13573. priority-letter priority agenda-day)
  13574. ",")))
  13575. (princ "\n"))))))
  13576. (defun org-fix-agenda-info (props)
  13577. "FIXME"
  13578. (let (tmp re)
  13579. (when (setq tmp (plist-get props 'tags))
  13580. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  13581. (when (setq tmp (plist-get props 'date))
  13582. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  13583. (let ((calendar-date-display-form '(year "-" month "-" day)))
  13584. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  13585. (setq tmp (calendar-date-string tmp)))
  13586. (setq props (plist-put props 'date tmp)))
  13587. (when (setq tmp (plist-get props 'day))
  13588. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  13589. (let ((calendar-date-display-form '(year "-" month "-" day)))
  13590. (setq tmp (calendar-date-string tmp)))
  13591. (setq props (plist-put props 'day tmp))
  13592. (setq props (plist-put props 'agenda-day tmp)))
  13593. (when (setq tmp (plist-get props 'txt))
  13594. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  13595. (plist-put props 'priority-letter (match-string 1 tmp))
  13596. (setq tmp (replace-match "" t t tmp)))
  13597. (when (and (setq re (plist-get props 'org-todo-regexp))
  13598. (setq re (concat "\\`\\.*" re " ?"))
  13599. (string-match re tmp))
  13600. (plist-put props 'todo (match-string 1 tmp))
  13601. (setq tmp (replace-match "" t t tmp)))
  13602. (plist-put props 'txt tmp)))
  13603. props)
  13604. (defun org-agenda-export-csv-mapper (prop)
  13605. (let ((res (plist-get org-agenda-info prop)))
  13606. (setq res
  13607. (cond
  13608. ((not res) "")
  13609. ((stringp res) res)
  13610. (t (prin1-to-string res))))
  13611. (while (string-match "," res)
  13612. (setq res (replace-match ";" t t res)))
  13613. (org-trim res)))
  13614. ;;;###autoload
  13615. (defun org-store-agenda-views (&rest parameters)
  13616. (interactive)
  13617. (eval (list 'org-batch-store-agenda-views)))
  13618. (defvar org-agenda-buffer-name)
  13619. ;; FIXME, why is this a macro?????
  13620. ;;;###autoload
  13621. (defmacro org-batch-store-agenda-views (&rest parameters)
  13622. "Run all custom agenda commands that have a file argument."
  13623. (let ((cmds org-agenda-custom-commands)
  13624. (dir (default-directory))
  13625. pars cmd thiscmdkey files opts)
  13626. (while parameters
  13627. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  13628. (setq pars (reverse pars))
  13629. (save-window-excursion
  13630. (while cmds
  13631. (setq cmd (pop cmds)
  13632. thiscmdkey (car cmd)
  13633. opts (nth 3 cmd)
  13634. files (org-last cmd))
  13635. (if (stringp files) (setq files (list files)))
  13636. (when files
  13637. (flet ((read-char-exclusive () (string-to-char thiscmdkey)))
  13638. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  13639. '(org-agenda nil))))
  13640. (set-buffer "*Org Agenda*")
  13641. (while files
  13642. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  13643. (list 'org-write-agenda
  13644. (expand-file-name (pop files) dir) t)))))
  13645. (kill-buffer org-agenda-buffer-name)))))
  13646. (defun org-write-agenda (file &optional nosettings)
  13647. "Write the current buffer (an agenda view) as a file.
  13648. Depending on the extension of the file name, plain text (.txt),
  13649. HTML (.html or .htm) or Postscript (.ps) is produced.
  13650. If NOSETTINGS is given, do not scope the settings of
  13651. `org-agenda-exporter-settings' into the export commands. This is used when
  13652. the settings have already been scoped and we do not wish to overrule other,
  13653. higher priority settings."
  13654. (interactive "FWrite agenda to file: ")
  13655. (if (not (file-writable-p file))
  13656. (error "Cannot write agenda to file %s" file))
  13657. (cond
  13658. ((string-match "\\.html?\\'" file) (require 'htmlize))
  13659. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  13660. (org-let (if nosettings nil org-agenda-exporter-settings)
  13661. '(save-excursion
  13662. (save-window-excursion
  13663. (cond
  13664. ((string-match "\\.html?\\'" file)
  13665. (set-buffer (htmlize-buffer (current-buffer)))
  13666. (when (and org-agenda-export-html-style
  13667. (string-match "<style>" org-agenda-export-html-style))
  13668. ;; replace <style> section with org-agenda-export-html-style
  13669. (goto-char (point-min))
  13670. (kill-region (- (search-forward "<style") 6)
  13671. (search-forward "</style>"))
  13672. (insert org-agenda-export-html-style))
  13673. (write-file file)
  13674. (kill-buffer (current-buffer))
  13675. (message "HTML written to %s" file))
  13676. ((string-match "\\.ps\\'" file)
  13677. (ps-print-buffer-with-faces file)
  13678. (message "Postscript written to %s" file))
  13679. (t
  13680. (let ((bs (buffer-string)))
  13681. (find-file file)
  13682. (insert bs)
  13683. (save-buffer 0)
  13684. (kill-buffer (current-buffer))
  13685. (message "Plain text written to %s" file))))))
  13686. (set-buffer org-agenda-buffer-name)))
  13687. (defmacro org-no-read-only (&rest body)
  13688. "Inhibit read-only for BODY."
  13689. `(let ((inhibit-read-only t)) ,@body))
  13690. (defun org-check-for-org-mode ()
  13691. "Make sure current buffer is in org-mode. Error if not."
  13692. (or (org-mode-p)
  13693. (error "Cannot execute org-mode agenda command on buffer in %s."
  13694. major-mode)))
  13695. (defun org-fit-agenda-window ()
  13696. "Fit the window to the buffer size."
  13697. (and (memq org-agenda-window-setup '(reorganize-frame))
  13698. (fboundp 'fit-window-to-buffer)
  13699. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  13700. (/ (frame-height) 2))))
  13701. ;;; Agenda file list
  13702. (defun org-agenda-files (&optional unrestricted)
  13703. "Get the list of agenda files.
  13704. Optional UNRESTRICTED means return the full list even if a restriction
  13705. is currently in place."
  13706. (cond
  13707. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  13708. ((stringp org-agenda-files) (org-read-agenda-file-list))
  13709. ((listp org-agenda-files) org-agenda-files)
  13710. (t (error "Invalid value of `org-agenda-files'"))))
  13711. (defun org-edit-agenda-file-list ()
  13712. "Edit the list of agenda files.
  13713. Depending on setup, this either uses customize to edit the variable
  13714. `org-agenda-files', or it visits the file that is holding the list. In the
  13715. latter case, the buffer is set up in a way that saving it automatically kills
  13716. the buffer and restores the previous window configuration."
  13717. (interactive)
  13718. (if (stringp org-agenda-files)
  13719. (let ((cw (current-window-configuration)))
  13720. (find-file org-agenda-files)
  13721. (org-set-local 'org-window-configuration cw)
  13722. (org-add-hook 'after-save-hook
  13723. (lambda ()
  13724. (set-window-configuration
  13725. (prog1 org-window-configuration
  13726. (kill-buffer (current-buffer))))
  13727. (org-install-agenda-files-menu)
  13728. (message "New agenda file list installed"))
  13729. nil 'local)
  13730. (message (substitute-command-keys
  13731. "Edit list and finish with \\[save-buffer]")))
  13732. (customize-variable 'org-agenda-files)))
  13733. (defun org-store-new-agenda-file-list (list)
  13734. "Set new value for the agenda file list and save it correcly."
  13735. (if (stringp org-agenda-files)
  13736. (let ((f org-agenda-files) b)
  13737. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  13738. (with-temp-file f
  13739. (insert (mapconcat 'identity list "\n") "\n")))
  13740. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  13741. (setq org-agenda-files list)
  13742. (customize-save-variable 'org-agenda-files org-agenda-files))))
  13743. (defun org-read-agenda-file-list ()
  13744. "Read the list of agenda files from a file."
  13745. (when (stringp org-agenda-files)
  13746. (with-temp-buffer
  13747. (insert-file-contents org-agenda-files)
  13748. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  13749. ;;;###autoload
  13750. (defun org-cycle-agenda-files ()
  13751. "Cycle through the files in `org-agenda-files'.
  13752. If the current buffer visits an agenda file, find the next one in the list.
  13753. If the current buffer does not, find the first agenda file."
  13754. (interactive)
  13755. (let* ((fs (org-agenda-files t))
  13756. (files (append fs (list (car fs))))
  13757. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  13758. file)
  13759. (unless files (error "No agenda files"))
  13760. (catch 'exit
  13761. (while (setq file (pop files))
  13762. (if (equal (file-truename file) tcf)
  13763. (when (car files)
  13764. (find-file (car files))
  13765. (throw 'exit t))))
  13766. (find-file (car fs)))
  13767. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  13768. (defun org-agenda-file-to-front (&optional to-end)
  13769. "Move/add the current file to the top of the agenda file list.
  13770. If the file is not present in the list, it is added to the front. If it is
  13771. present, it is moved there. With optional argument TO-END, add/move to the
  13772. end of the list."
  13773. (interactive "P")
  13774. (let ((file-alist (mapcar (lambda (x)
  13775. (cons (file-truename x) x))
  13776. (org-agenda-files t)))
  13777. (ctf (file-truename buffer-file-name))
  13778. x had)
  13779. (setq x (assoc ctf file-alist) had x)
  13780. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  13781. (if to-end
  13782. (setq file-alist (append (delq x file-alist) (list x)))
  13783. (setq file-alist (cons x (delq x file-alist))))
  13784. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  13785. (org-install-agenda-files-menu)
  13786. (message "File %s to %s of agenda file list"
  13787. (if had "moved" "added") (if to-end "end" "front"))))
  13788. (defun org-remove-file (&optional file)
  13789. "Remove current file from the list of files in variable `org-agenda-files'.
  13790. These are the files which are being checked for agenda entries.
  13791. Optional argument FILE means, use this file instead of the current."
  13792. (interactive)
  13793. (let* ((file (or file buffer-file-name))
  13794. (true-file (file-truename file))
  13795. (afile (abbreviate-file-name file))
  13796. (files (delq nil (mapcar
  13797. (lambda (x)
  13798. (if (equal true-file
  13799. (file-truename x))
  13800. nil x))
  13801. (org-agenda-files t)))))
  13802. (if (not (= (length files) (length (org-agenda-files t))))
  13803. (progn
  13804. (org-store-new-agenda-file-list files)
  13805. (org-install-agenda-files-menu)
  13806. (message "Removed file: %s" afile))
  13807. (message "File was not in list: %s" afile))))
  13808. (defun org-file-menu-entry (file)
  13809. (vector file (list 'find-file file) t))
  13810. (defun org-check-agenda-file (file)
  13811. "Make sure FILE exists. If not, ask user what to do."
  13812. (when (not (file-exists-p file))
  13813. (message "non-existent file %s. [R]emove from list or [A]bort?"
  13814. (abbreviate-file-name file))
  13815. (let ((r (downcase (read-char-exclusive))))
  13816. (cond
  13817. ((equal r ?r)
  13818. (org-remove-file file)
  13819. (throw 'nextfile t))
  13820. (t (error "Abort"))))))
  13821. ;;; Agenda prepare and finalize
  13822. (defvar org-agenda-multi nil) ; dynammically scoped
  13823. (defvar org-agenda-buffer-name "*Org Agenda*")
  13824. (defvar org-pre-agenda-window-conf nil)
  13825. (defvar org-agenda-name nil)
  13826. (defun org-prepare-agenda (&optional name)
  13827. (setq org-todo-keywords-for-agenda nil)
  13828. (setq org-done-keywords-for-agenda nil)
  13829. (if org-agenda-multi
  13830. (progn
  13831. (setq buffer-read-only nil)
  13832. (goto-char (point-max))
  13833. (unless (= (point) 1)
  13834. (insert "\n" (make-string (window-width) ?=) "\n"))
  13835. (narrow-to-region (point) (point-max)))
  13836. (org-agenda-maybe-reset-markers 'force)
  13837. (org-prepare-agenda-buffers (org-agenda-files))
  13838. (setq org-todo-keywords-for-agenda
  13839. (org-uniquify org-todo-keywords-for-agenda))
  13840. (setq org-done-keywords-for-agenda
  13841. (org-uniquify org-done-keywords-for-agenda))
  13842. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  13843. (awin (get-buffer-window abuf)))
  13844. (cond
  13845. ((equal (current-buffer) abuf) nil)
  13846. (awin (select-window awin))
  13847. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  13848. ((equal org-agenda-window-setup 'current-window)
  13849. (switch-to-buffer abuf))
  13850. ((equal org-agenda-window-setup 'other-window)
  13851. (switch-to-buffer-other-window abuf))
  13852. ((equal org-agenda-window-setup 'other-frame)
  13853. (switch-to-buffer-other-frame abuf))
  13854. ((equal org-agenda-window-setup 'reorganize-frame)
  13855. (delete-other-windows)
  13856. (switch-to-buffer-other-window abuf))))
  13857. (setq buffer-read-only nil)
  13858. (erase-buffer)
  13859. (org-agenda-mode)
  13860. (and name (not org-agenda-name)
  13861. (org-set-local 'org-agenda-name name)))
  13862. (setq buffer-read-only nil))
  13863. (defun org-finalize-agenda ()
  13864. "Finishing touch for the agenda buffer, called just before displaying it."
  13865. (unless org-agenda-multi
  13866. (org-agenda-align-tags)
  13867. (save-excursion
  13868. (let ((buffer-read-only))
  13869. (goto-char (point-min))
  13870. (while (org-activate-bracket-links (point-max))
  13871. (add-text-properties (match-beginning 0) (match-end 0)
  13872. '(face org-link)))
  13873. (unless org-agenda-with-colors
  13874. (remove-text-properties (point-min) (point-max) '(face nil))))
  13875. (run-hooks 'org-finalize-agenda-hook))))
  13876. (defun org-prepare-agenda-buffers (files)
  13877. "Create buffers for all agenda files, protect archived trees and comments."
  13878. (interactive)
  13879. (let ((pa '(:org-archived t))
  13880. (pc '(:org-comment t))
  13881. (pall '(:org-archived t :org-comment t))
  13882. (rea (concat ":" org-archive-tag ":"))
  13883. bmp file re)
  13884. (save-excursion
  13885. (save-restriction
  13886. (while (setq file (pop files))
  13887. (org-check-agenda-file file)
  13888. (set-buffer (org-get-agenda-file-buffer file))
  13889. (widen)
  13890. (setq bmp (buffer-modified-p))
  13891. (setq org-todo-keywords-for-agenda
  13892. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13893. (setq org-done-keywords-for-agenda
  13894. (append org-done-keywords-for-agenda org-done-keywords))
  13895. (save-excursion
  13896. (remove-text-properties (point-min) (point-max) pall)
  13897. (when org-agenda-skip-archived-trees
  13898. (goto-char (point-min))
  13899. (while (re-search-forward rea nil t)
  13900. (if (org-on-heading-p t)
  13901. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13902. (goto-char (point-min))
  13903. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13904. (while (re-search-forward re nil t)
  13905. (add-text-properties
  13906. (match-beginning 0) (org-end-of-subtree t) pc)))
  13907. (set-buffer-modified-p bmp))))))
  13908. (defvar org-agenda-skip-function nil
  13909. "Function to be called at each match during agenda construction.
  13910. If this function return nil, the current match should not be skipped.
  13911. Otherwise, the function must return a position from where the search
  13912. should be continued.
  13913. Never set this variable using `setq' or so, because then it will apply
  13914. to all future agenda commands. Instead, bind it with `let' to scope
  13915. it dynamically into the agenda-constructing command.")
  13916. (defun org-agenda-skip ()
  13917. "Throw to `:skip' in places that should be skipped.
  13918. Also moves point to the end of the skipped region, so that search can
  13919. continue from there."
  13920. (let ((p (point-at-bol)) to)
  13921. (and org-agenda-skip-archived-trees
  13922. (get-text-property p :org-archived)
  13923. (org-end-of-subtree t)
  13924. (throw :skip t))
  13925. (and (get-text-property p :org-comment)
  13926. (org-end-of-subtree t)
  13927. (throw :skip t))
  13928. (if (equal (char-after p) ?#) (throw :skip t))
  13929. (when (and (functionp org-agenda-skip-function)
  13930. (setq to (save-excursion
  13931. (save-match-data
  13932. (funcall org-agenda-skip-function)))))
  13933. (goto-char to)
  13934. (throw :skip t))))
  13935. (defvar org-agenda-markers nil
  13936. "List of all currently active markers created by `org-agenda'.")
  13937. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  13938. "Creation time of the last agenda marker.")
  13939. (defun org-agenda-new-marker (&optional pos)
  13940. "Return a new agenda marker.
  13941. Org-mode keeps a list of these markers and resets them when they are
  13942. no longer in use."
  13943. (let ((m (copy-marker (or pos (point)))))
  13944. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  13945. (push m org-agenda-markers)
  13946. m))
  13947. (defun org-agenda-maybe-reset-markers (&optional force)
  13948. "Reset markers created by `org-agenda'. But only if they are old enough."
  13949. (if (or (and force (not org-agenda-multi))
  13950. (> (- (time-to-seconds (current-time))
  13951. org-agenda-last-marker-time)
  13952. 5))
  13953. (while org-agenda-markers
  13954. (move-marker (pop org-agenda-markers) nil))))
  13955. (defvar org-agenda-new-buffers nil
  13956. "Buffers created to visit agenda files.")
  13957. (defun org-get-agenda-file-buffer (file)
  13958. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13959. it to the list of buffers which might be released later."
  13960. (let ((buf (org-find-base-buffer-visiting file)))
  13961. (if buf
  13962. buf ; just return it
  13963. ;; Make a new buffer and remember it
  13964. (setq buf (find-file-noselect file))
  13965. (if buf (push buf org-agenda-new-buffers))
  13966. buf)))
  13967. (defun org-release-buffers (blist)
  13968. "Release all buffers in list, asking the user for confirmation when needed.
  13969. When a buffer is unmodified, it is just killed. When modified, it is saved
  13970. \(if the user agrees) and then killed."
  13971. (let (buf file)
  13972. (while (setq buf (pop blist))
  13973. (setq file (buffer-file-name buf))
  13974. (when (and (buffer-modified-p buf)
  13975. file
  13976. (y-or-n-p (format "Save file %s? " file)))
  13977. (with-current-buffer buf (save-buffer)))
  13978. (kill-buffer buf))))
  13979. (defvar org-category-table nil)
  13980. (defun org-get-category-table ()
  13981. "Get the table of categories and positions in current buffer."
  13982. (let (tbl)
  13983. (save-excursion
  13984. (save-restriction
  13985. (widen)
  13986. (goto-char (point-min))
  13987. (while (re-search-forward "^#\\+CATEGORY:[ \t]*\\(.*\\)"
  13988. nil t)
  13989. (push (cons (match-beginning 1)
  13990. (org-trim (match-string 1))) tbl))))
  13991. tbl))
  13992. (defun org-get-category (&optional pos)
  13993. "Get the category applying to position POS."
  13994. (if (not org-category-table)
  13995. (cond
  13996. ((null org-category)
  13997. (setq org-category
  13998. (if buffer-file-name
  13999. (file-name-sans-extension
  14000. (file-name-nondirectory buffer-file-name))
  14001. "???")))
  14002. ((symbolp org-category) (symbol-name org-category))
  14003. (t org-category))
  14004. (let ((tbl org-category-table)
  14005. (pos (or pos (point))))
  14006. (while (and tbl (> (caar tbl) pos))
  14007. (pop tbl))
  14008. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  14009. org-category-table))))))
  14010. ;;; Agenda timeline
  14011. (defun org-timeline (&optional include-all)
  14012. "Show a time-sorted view of the entries in the current org file.
  14013. Only entries with a time stamp of today or later will be listed. With
  14014. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  14015. under the current date.
  14016. If the buffer contains an active region, only check the region for
  14017. dates."
  14018. (interactive "P")
  14019. (require 'calendar)
  14020. (org-compile-prefix-format 'timeline)
  14021. (org-set-sorting-strategy 'timeline)
  14022. (let* ((dopast t)
  14023. (dotodo include-all)
  14024. (doclosed org-agenda-show-log)
  14025. (entry buffer-file-name)
  14026. (date (calendar-current-date))
  14027. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  14028. (end (if (org-region-active-p) (region-end) (point-max)))
  14029. (day-numbers (org-get-all-dates beg end 'no-ranges
  14030. t doclosed ; always include today
  14031. org-timeline-show-empty-dates))
  14032. (today (time-to-days (current-time)))
  14033. (past t)
  14034. args
  14035. s e rtn d emptyp)
  14036. (setq org-agenda-redo-command
  14037. (list 'progn
  14038. (list 'switch-to-buffer-other-window (current-buffer))
  14039. (list 'org-timeline (list 'quote include-all))))
  14040. (if (not dopast)
  14041. ;; Remove past dates from the list of dates.
  14042. (setq day-numbers (delq nil (mapcar (lambda(x)
  14043. (if (>= x today) x nil))
  14044. day-numbers))))
  14045. (org-prepare-agenda (concat "Timeline "
  14046. (file-name-nondirectory buffer-file-name)))
  14047. (if doclosed (push :closed args))
  14048. (push :timestamp args)
  14049. (push :sexp args)
  14050. (if dotodo (push :todo args))
  14051. (while (setq d (pop day-numbers))
  14052. (if (and (listp d) (eq (car d) :omitted))
  14053. (progn
  14054. (setq s (point))
  14055. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  14056. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  14057. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  14058. (if (and (>= d today)
  14059. dopast
  14060. past)
  14061. (progn
  14062. (setq past nil)
  14063. (insert (make-string 79 ?-) "\n")))
  14064. (setq date (calendar-gregorian-from-absolute d))
  14065. (setq s (point))
  14066. (setq rtn (and (not emptyp)
  14067. (apply 'org-agenda-get-day-entries
  14068. entry date args)))
  14069. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  14070. (progn
  14071. (insert (calendar-day-name date) " "
  14072. (number-to-string (extract-calendar-day date)) " "
  14073. (calendar-month-name (extract-calendar-month date)) " "
  14074. (number-to-string (extract-calendar-year date)) "\n")
  14075. ; FIXME: this gives a timezone problem
  14076. ; (insert (format-time-string org-agenda-date-format
  14077. ; (calendar-time-from-absolute d 0))
  14078. ; "\n")
  14079. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  14080. (put-text-property s (1- (point)) 'org-date-line t)
  14081. (if (equal d today)
  14082. (put-text-property s (1- (point)) 'org-today t))
  14083. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  14084. (put-text-property s (1- (point)) 'day d)))))
  14085. (goto-char (point-min))
  14086. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  14087. (point-min)))
  14088. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  14089. (org-finalize-agenda)
  14090. (setq buffer-read-only t)))
  14091. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  14092. "Return a list of all relevant day numbers from BEG to END buffer positions.
  14093. If NO-RANGES is non-nil, include only the start and end dates of a range,
  14094. not every single day in the range. If FORCE-TODAY is non-nil, make
  14095. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  14096. inactive time stamps (those in square brackets) are included.
  14097. When EMPTY is non-nil, also include days without any entries."
  14098. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  14099. dates dates1 date day day1 day2 ts1 ts2)
  14100. (if force-today
  14101. (setq dates (list (time-to-days (current-time)))))
  14102. (save-excursion
  14103. (goto-char beg)
  14104. (while (re-search-forward re end t)
  14105. (setq day (time-to-days (org-time-string-to-time
  14106. (substring (match-string 1) 0 10))))
  14107. (or (memq day dates) (push day dates)))
  14108. (unless no-ranges
  14109. (goto-char beg)
  14110. (while (re-search-forward org-tr-regexp end t)
  14111. (setq ts1 (substring (match-string 1) 0 10)
  14112. ts2 (substring (match-string 2) 0 10)
  14113. day1 (time-to-days (org-time-string-to-time ts1))
  14114. day2 (time-to-days (org-time-string-to-time ts2)))
  14115. (while (< (setq day1 (1+ day1)) day2)
  14116. (or (memq day1 dates) (push day1 dates)))))
  14117. (setq dates (sort dates '<))
  14118. (when empty
  14119. (while (setq day (pop dates))
  14120. (setq day2 (car dates))
  14121. (push day dates1)
  14122. (when (and day2 empty)
  14123. (if (or (eq empty t)
  14124. (and (numberp empty) (<= (- day2 day) empty)))
  14125. (while (< (setq day (1+ day)) day2)
  14126. (push (list day) dates1))
  14127. (push (cons :omitted (- day2 day)) dates1))))
  14128. (setq dates (nreverse dates1)))
  14129. dates)))
  14130. ;;; Agenda Daily/Weekly
  14131. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  14132. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  14133. (defvar org-agenda-last-arguments nil
  14134. "The arguments of the previous call to org-agenda")
  14135. (defvar org-starting-day nil) ; local variable in the agenda buffer
  14136. (defvar org-include-all-loc nil) ; local variable
  14137. ;;;###autoload
  14138. (defun org-agenda-list (&optional include-all start-day ndays)
  14139. "Produce a weekly view from all files in variable `org-agenda-files'.
  14140. The view will be for the current week, but from the overview buffer you
  14141. will be able to go to other weeks.
  14142. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  14143. also be shown, under the current date.
  14144. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  14145. on the days are also shown. See the variable `org-log-done' for how
  14146. to turn on logging.
  14147. START-DAY defaults to TODAY, or to the most recent match for the weekday
  14148. given in `org-agenda-start-on-weekday'.
  14149. NDAYS defaults to `org-agenda-ndays'."
  14150. (interactive "P")
  14151. (setq ndays (or ndays org-agenda-ndays)
  14152. start-day (or start-day org-agenda-start-day))
  14153. (if org-agenda-overriding-arguments
  14154. (setq include-all (car org-agenda-overriding-arguments)
  14155. start-day (nth 1 org-agenda-overriding-arguments)
  14156. ndays (nth 2 org-agenda-overriding-arguments)))
  14157. (if (stringp start-day)
  14158. ;; Convert to an absolute day number
  14159. (setq start-day (time-to-days (org-read-date nil t start-day))))
  14160. (setq org-agenda-last-arguments (list include-all start-day ndays))
  14161. (org-compile-prefix-format 'agenda)
  14162. (org-set-sorting-strategy 'agenda)
  14163. (require 'calendar)
  14164. (let* ((org-agenda-start-on-weekday
  14165. (if (or (equal ndays 1)
  14166. (and (null ndays) (equal 1 org-agenda-ndays)))
  14167. nil org-agenda-start-on-weekday))
  14168. (thefiles (org-agenda-files))
  14169. (files thefiles)
  14170. (today (time-to-days (current-time)))
  14171. (sd (or start-day today))
  14172. (start (if (or (null org-agenda-start-on-weekday)
  14173. (< org-agenda-ndays 7))
  14174. sd
  14175. (let* ((nt (calendar-day-of-week
  14176. (calendar-gregorian-from-absolute sd)))
  14177. (n1 org-agenda-start-on-weekday)
  14178. (d (- nt n1)))
  14179. (- sd (+ (if (< d 0) 7 0) d)))))
  14180. (day-numbers (list start))
  14181. (inhibit-redisplay (not debug-on-error))
  14182. s e rtn rtnall file date d start-pos end-pos todayp nd)
  14183. (setq org-agenda-redo-command
  14184. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  14185. ;; Make the list of days
  14186. (setq ndays (or ndays org-agenda-ndays)
  14187. nd ndays)
  14188. (while (> ndays 1)
  14189. (push (1+ (car day-numbers)) day-numbers)
  14190. (setq ndays (1- ndays)))
  14191. (setq day-numbers (nreverse day-numbers))
  14192. (org-prepare-agenda "Day/Week")
  14193. (org-set-local 'org-starting-day (car day-numbers))
  14194. (org-set-local 'org-include-all-loc include-all)
  14195. (when (and (or include-all org-agenda-include-all-todo)
  14196. (member today day-numbers))
  14197. (setq files thefiles
  14198. rtnall nil)
  14199. (while (setq file (pop files))
  14200. (catch 'nextfile
  14201. (org-check-agenda-file file)
  14202. (setq date (calendar-gregorian-from-absolute today)
  14203. rtn (org-agenda-get-day-entries
  14204. file date :todo))
  14205. (setq rtnall (append rtnall rtn))))
  14206. (when rtnall
  14207. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  14208. (add-text-properties (point-min) (1- (point))
  14209. (list 'face 'org-agenda-structure))
  14210. (insert (org-finalize-agenda-entries rtnall) "\n")))
  14211. (setq s (point))
  14212. (insert (if (= nd 7) "Week-" "Day-") "agenda:\n")
  14213. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  14214. 'org-date-line t))
  14215. (while (setq d (pop day-numbers))
  14216. (setq date (calendar-gregorian-from-absolute d)
  14217. s (point))
  14218. (if (or (setq todayp (= d today))
  14219. (and (not start-pos) (= d sd)))
  14220. (setq start-pos (point))
  14221. (if (and start-pos (not end-pos))
  14222. (setq end-pos (point))))
  14223. (setq files thefiles
  14224. rtnall nil)
  14225. (while (setq file (pop files))
  14226. (catch 'nextfile
  14227. (org-check-agenda-file file)
  14228. (if org-agenda-show-log
  14229. (setq rtn (org-agenda-get-day-entries
  14230. file date
  14231. :deadline :scheduled :timestamp :sexp :closed))
  14232. (setq rtn (org-agenda-get-day-entries
  14233. file date
  14234. :deadline :scheduled :sexp :timestamp)))
  14235. (setq rtnall (append rtnall rtn))))
  14236. (if org-agenda-include-diary
  14237. (progn
  14238. (require 'diary-lib)
  14239. (setq rtn (org-get-entries-from-diary date))
  14240. (setq rtnall (append rtnall rtn))))
  14241. (if (or rtnall org-agenda-show-all-dates)
  14242. (progn
  14243. (insert (format "%-9s %2d %s %4d\n"
  14244. (calendar-day-name date)
  14245. (extract-calendar-day date)
  14246. (calendar-month-name (extract-calendar-month date))
  14247. (extract-calendar-year date)))
  14248. ; FIXME: this gives a timezone problem
  14249. ; (insert (format-time-string org-agenda-date-format
  14250. ; (calendar-time-from-absolute d 0)) "\n")
  14251. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  14252. (put-text-property s (1- (point)) 'org-date-line t)
  14253. (if todayp (put-text-property s (1- (point)) 'org-today t))
  14254. (if rtnall (insert
  14255. (org-finalize-agenda-entries
  14256. (org-agenda-add-time-grid-maybe
  14257. rtnall nd todayp))
  14258. "\n"))
  14259. (put-text-property s (1- (point)) 'day d))))
  14260. (goto-char (point-min))
  14261. (org-fit-agenda-window)
  14262. (unless (and (pos-visible-in-window-p (point-min))
  14263. (pos-visible-in-window-p (point-max)))
  14264. (goto-char (1- (point-max)))
  14265. (recenter -1)
  14266. (if (not (pos-visible-in-window-p (or start-pos 1)))
  14267. (progn
  14268. (goto-char (or start-pos 1))
  14269. (recenter 1))))
  14270. (goto-char (or start-pos 1))
  14271. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  14272. (org-finalize-agenda)
  14273. (setq buffer-read-only t)
  14274. (message "")))
  14275. ;;; Agenda TODO list
  14276. (defvar org-select-this-todo-keyword nil)
  14277. (defvar org-last-arg nil)
  14278. ;;;###autoload
  14279. (defun org-todo-list (arg)
  14280. "Show all TODO entries from all agenda file in a single list.
  14281. The prefix arg can be used to select a specific TODO keyword and limit
  14282. the list to these. When using \\[universal-argument], you will be prompted
  14283. for a keyword. A numeric prefix directly selects the Nth keyword in
  14284. `org-todo-keywords-1'."
  14285. (interactive "P")
  14286. (require 'calendar)
  14287. (org-compile-prefix-format 'todo)
  14288. (org-set-sorting-strategy 'todo)
  14289. (org-prepare-agenda "TODO")
  14290. (let* ((today (time-to-days (current-time)))
  14291. (date (calendar-gregorian-from-absolute today))
  14292. (kwds org-todo-keywords-for-agenda)
  14293. (completion-ignore-case t)
  14294. (org-select-this-todo-keyword
  14295. (if (stringp arg) arg
  14296. (and arg (integerp arg) (> arg 0)
  14297. (nth (1- arg) kwds))))
  14298. rtn rtnall files file pos)
  14299. (when (equal arg '(4))
  14300. (setq org-select-this-todo-keyword
  14301. (completing-read "Keyword (or KWD1|K2D2|...): "
  14302. (mapcar 'list kwds) nil nil)))
  14303. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  14304. (org-set-local 'org-last-arg arg)
  14305. ;FIXME (org-set-local 'org-todo-keywords-for-agenda kwds)
  14306. (setq org-agenda-redo-command
  14307. '(org-todo-list (or current-prefix-arg org-last-arg)))
  14308. (setq files (org-agenda-files)
  14309. rtnall nil)
  14310. (while (setq file (pop files))
  14311. (catch 'nextfile
  14312. (org-check-agenda-file file)
  14313. (setq rtn (org-agenda-get-day-entries file date :todo))
  14314. (setq rtnall (append rtnall rtn))))
  14315. (if org-agenda-overriding-header
  14316. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  14317. nil 'face 'org-agenda-structure) "\n")
  14318. (insert "Global list of TODO items of type: ")
  14319. (add-text-properties (point-min) (1- (point))
  14320. (list 'face 'org-agenda-structure))
  14321. (setq pos (point))
  14322. (insert (or org-select-this-todo-keyword "ALL") "\n")
  14323. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  14324. (setq pos (point))
  14325. (unless org-agenda-multi
  14326. (insert "Available with `N r': (0)ALL")
  14327. (let ((n 0) s)
  14328. (mapc (lambda (x)
  14329. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  14330. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  14331. (insert "\n "))
  14332. (insert " " s))
  14333. kwds))
  14334. (insert "\n"))
  14335. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  14336. (when rtnall
  14337. (insert (org-finalize-agenda-entries rtnall) "\n"))
  14338. (goto-char (point-min))
  14339. (org-fit-agenda-window)
  14340. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  14341. (org-finalize-agenda)
  14342. (setq buffer-read-only t)))
  14343. ;;; Agenda tags match
  14344. ;;;###autoload
  14345. (defun org-tags-view (&optional todo-only match)
  14346. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  14347. The prefix arg TODO-ONLY limits the search to TODO entries."
  14348. (interactive "P")
  14349. (org-compile-prefix-format 'tags)
  14350. (org-set-sorting-strategy 'tags)
  14351. (let* ((org-tags-match-list-sublevels
  14352. (if todo-only t org-tags-match-list-sublevels))
  14353. (completion-ignore-case t)
  14354. rtn rtnall files file pos matcher
  14355. buffer)
  14356. (setq matcher (org-make-tags-matcher match)
  14357. match (car matcher) matcher (cdr matcher))
  14358. (org-prepare-agenda (concat "TAGS " match))
  14359. (setq org-agenda-redo-command
  14360. (list 'org-tags-view (list 'quote todo-only)
  14361. (list 'if 'current-prefix-arg nil match)))
  14362. (setq files (org-agenda-files)
  14363. rtnall nil)
  14364. (while (setq file (pop files))
  14365. (catch 'nextfile
  14366. (org-check-agenda-file file)
  14367. (setq buffer (if (file-exists-p file)
  14368. (org-get-agenda-file-buffer file)
  14369. (error "No such file %s" file)))
  14370. (if (not buffer)
  14371. ;; If file does not exist, merror message to agenda
  14372. (setq rtn (list
  14373. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  14374. rtnall (append rtnall rtn))
  14375. (with-current-buffer buffer
  14376. (unless (org-mode-p)
  14377. (error "Agenda file %s is not in `org-mode'" file))
  14378. (setq org-category-table (org-get-category-table))
  14379. (save-excursion
  14380. (save-restriction
  14381. (if org-agenda-restrict
  14382. (narrow-to-region org-agenda-restrict-begin
  14383. org-agenda-restrict-end)
  14384. (widen))
  14385. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  14386. (setq rtnall (append rtnall rtn))))))))
  14387. (if org-agenda-overriding-header
  14388. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  14389. nil 'face 'org-agenda-structure) "\n")
  14390. (insert "Headlines with TAGS match: ")
  14391. (add-text-properties (point-min) (1- (point))
  14392. (list 'face 'org-agenda-structure))
  14393. (setq pos (point))
  14394. (insert match "\n")
  14395. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  14396. (setq pos (point))
  14397. (unless org-agenda-multi
  14398. (insert "Press `C-u r' to search again with new search string\n"))
  14399. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  14400. (when rtnall
  14401. (insert (org-finalize-agenda-entries rtnall) "\n"))
  14402. (goto-char (point-min))
  14403. (org-fit-agenda-window)
  14404. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  14405. (org-finalize-agenda)
  14406. (setq buffer-read-only t)))
  14407. ;;; Agenda Finding stuck projects
  14408. (defvar org-agenda-skip-regexp nil
  14409. "Regular expression used in skipping subtrees for the agenda.
  14410. This is basically a temporary global variable that can be set and then
  14411. used by user-defined selections using `org-agenda-skip-function'.")
  14412. (defvar org-agenda-overriding-header nil
  14413. "When this is set during todo and tags searches, will replace header.")
  14414. (defun org-agenda-skip-subtree-when-regexp-matches ()
  14415. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  14416. If yes, it returns the end position of this tree, causing agenda commands
  14417. to skip this subtree. This is a function that can be put into
  14418. `org-agenda-skip-function' for the duration of a command."
  14419. (save-match-data
  14420. (let ((end (save-excursion (org-end-of-subtree t)))
  14421. skip)
  14422. (save-excursion
  14423. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  14424. (and skip end))))
  14425. (defun org-agenda-list-stuck-projects (&rest ignore)
  14426. "Create agenda view for projects that are stuck.
  14427. Stuck projects are project that have no next actions. For the definitions
  14428. of what a project is and how to check if it stuck, customize the variable
  14429. `org-stuck-projects'.
  14430. MATCH is being ignored."
  14431. (interactive)
  14432. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  14433. (org-agenda-overriding-header "List of stuck projects: ")
  14434. (matcher (nth 0 org-stuck-projects))
  14435. (todo (nth 1 org-stuck-projects))
  14436. (todo-wds (if (member "*" todo)
  14437. (progn
  14438. (org-prepare-agenda-buffers (org-agenda-files))
  14439. (org-delete-all
  14440. org-done-keywords-for-agenda
  14441. (copy-sequence org-todo-keywords-for-agenda)))
  14442. todo))
  14443. (todo-re (concat "^\\*+[ \t]+\\("
  14444. (mapconcat 'identity todo-wds "\\|")
  14445. "\\)\\>"))
  14446. (tags (nth 2 org-stuck-projects))
  14447. (tags-re (if (member "*" tags)
  14448. "^\\*+.*:[a-zA-Z0-9_@]+:[ \t]*$"
  14449. (concat "^\\*+.*:\\("
  14450. (mapconcat 'identity tags "\\|")
  14451. "\\):[a-zA-Z0-9_@:]*[ \t]*$")))
  14452. (gen-re (nth 3 org-stuck-projects))
  14453. (re-list
  14454. (delq nil
  14455. (list
  14456. (if todo todo-re)
  14457. (if tags tags-re)
  14458. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  14459. gen-re)))))
  14460. (setq org-agenda-skip-regexp
  14461. (if re-list
  14462. (mapconcat 'identity re-list "\\|")
  14463. (error "No information how to identify unstuck projects")))
  14464. (org-tags-view nil matcher)
  14465. (with-current-buffer org-agenda-buffer-name
  14466. (setq org-agenda-redo-command
  14467. '(org-agenda-list-stuck-projects
  14468. (or current-prefix-arg org-last-arg))))))
  14469. ;;; Diary integration
  14470. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  14471. (defun org-get-entries-from-diary (date)
  14472. "Get the (Emacs Calendar) diary entries for DATE."
  14473. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  14474. (diary-display-hook '(fancy-diary-display))
  14475. (list-diary-entries-hook
  14476. (cons 'org-diary-default-entry list-diary-entries-hook))
  14477. (diary-file-name-prefix-function nil) ; turn this feature off
  14478. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  14479. entries
  14480. (org-disable-agenda-to-diary t))
  14481. (save-excursion
  14482. (save-window-excursion
  14483. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  14484. (if (not (get-buffer fancy-diary-buffer))
  14485. (setq entries nil)
  14486. (with-current-buffer fancy-diary-buffer
  14487. (setq buffer-read-only nil)
  14488. (if (= (point-max) 1)
  14489. ;; No entries
  14490. (setq entries nil)
  14491. ;; Omit the date and other unnecessary stuff
  14492. (org-agenda-cleanup-fancy-diary)
  14493. ;; Add prefix to each line and extend the text properties
  14494. (if (= (point-max) 1)
  14495. (setq entries nil)
  14496. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  14497. (set-buffer-modified-p nil)
  14498. (kill-buffer fancy-diary-buffer)))
  14499. (when entries
  14500. (setq entries (org-split-string entries "\n"))
  14501. (setq entries
  14502. (mapcar
  14503. (lambda (x)
  14504. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  14505. ;; Extend the text properties to the beginning of the line
  14506. (org-add-props x (text-properties-at (1- (length x)) x)
  14507. 'type "diary" 'date date))
  14508. entries)))))
  14509. (defun org-agenda-cleanup-fancy-diary ()
  14510. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  14511. This gets rid of the date, the underline under the date, and
  14512. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  14513. date. It also removes lines that contain only whitespace."
  14514. (goto-char (point-min))
  14515. (if (looking-at ".*?:[ \t]*")
  14516. (progn
  14517. (replace-match "")
  14518. (re-search-forward "\n=+$" nil t)
  14519. (replace-match "")
  14520. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  14521. (re-search-forward "\n=+$" nil t)
  14522. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  14523. (goto-char (point-min))
  14524. (while (re-search-forward "^ +\n" nil t)
  14525. (replace-match ""))
  14526. (goto-char (point-min))
  14527. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  14528. (replace-match "")))
  14529. ;; Make sure entries from the diary have the right text properties.
  14530. (eval-after-load "diary-lib"
  14531. '(if (boundp 'diary-modify-entry-list-string-function)
  14532. ;; We can rely on the hook, nothing to do
  14533. nil
  14534. ;; Hook not avaiable, must use advice to make this work
  14535. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  14536. "Make the position visible."
  14537. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  14538. (stringp string)
  14539. buffer-file-name)
  14540. (setq string (org-modify-diary-entry-string string))))))
  14541. (defun org-modify-diary-entry-string (string)
  14542. "Add text properties to string, allowing org-mode to act on it."
  14543. (org-add-props string nil
  14544. 'mouse-face 'highlight
  14545. 'keymap org-agenda-keymap
  14546. 'help-echo (if buffer-file-name
  14547. (format "mouse-2 or RET jump to diary file %s"
  14548. (abbreviate-file-name buffer-file-name))
  14549. "")
  14550. 'org-agenda-diary-link t
  14551. 'org-marker (org-agenda-new-marker (point-at-bol))))
  14552. (defun org-diary-default-entry ()
  14553. "Add a dummy entry to the diary.
  14554. Needed to avoid empty dates which mess up holiday display."
  14555. ;; Catch the error if dealing with the new add-to-diary-alist
  14556. (when org-disable-agenda-to-diary
  14557. (condition-case nil
  14558. (add-to-diary-list original-date "Org-mode dummy" "")
  14559. (error
  14560. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  14561. ;;;###autoload
  14562. (defun org-diary (&rest args)
  14563. "Return diary information from org-files.
  14564. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  14565. It accesses org files and extracts information from those files to be
  14566. listed in the diary. The function accepts arguments specifying what
  14567. items should be listed. The following arguments are allowed:
  14568. :timestamp List the headlines of items containing a date stamp or
  14569. date range matching the selected date. Deadlines will
  14570. also be listed, on the expiration day.
  14571. :sexp FIXME
  14572. :deadline List any deadlines past due, or due within
  14573. `org-deadline-warning-days'. The listing occurs only
  14574. in the diary for *today*, not at any other date. If
  14575. an entry is marked DONE, it is no longer listed.
  14576. :scheduled List all items which are scheduled for the given date.
  14577. The diary for *today* also contains items which were
  14578. scheduled earlier and are not yet marked DONE.
  14579. :todo List all TODO items from the org-file. This may be a
  14580. long list - so this is not turned on by default.
  14581. Like deadlines, these entries only show up in the
  14582. diary for *today*, not at any other date.
  14583. The call in the diary file should look like this:
  14584. &%%(org-diary) ~/path/to/some/orgfile.org
  14585. Use a separate line for each org file to check. Or, if you omit the file name,
  14586. all files listed in `org-agenda-files' will be checked automatically:
  14587. &%%(org-diary)
  14588. If you don't give any arguments (as in the example above), the default
  14589. arguments (:deadline :scheduled :timestamp :sexp) are used.
  14590. So the example above may also be written as
  14591. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  14592. The function expects the lisp variables `entry' and `date' to be provided
  14593. by the caller, because this is how the calendar works. Don't use this
  14594. function from a program - use `org-agenda-get-day-entries' instead."
  14595. (org-agenda-maybe-reset-markers)
  14596. (org-compile-prefix-format 'agenda)
  14597. (org-set-sorting-strategy 'agenda)
  14598. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  14599. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  14600. (list entry)
  14601. (org-agenda-files t)))
  14602. file rtn results)
  14603. (org-prepare-agenda-buffers files)
  14604. ;; If this is called during org-agenda, don't return any entries to
  14605. ;; the calendar. Org Agenda will list these entries itself.
  14606. (if org-disable-agenda-to-diary (setq files nil))
  14607. (while (setq file (pop files))
  14608. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  14609. (setq results (append results rtn)))
  14610. (if results
  14611. (concat (org-finalize-agenda-entries results) "\n"))))
  14612. ;;; Agenda entry finders
  14613. (defun org-agenda-get-day-entries (file date &rest args)
  14614. "Does the work for `org-diary' and `org-agenda'.
  14615. FILE is the path to a file to be checked for entries. DATE is date like
  14616. the one returned by `calendar-current-date'. ARGS are symbols indicating
  14617. which kind of entries should be extracted. For details about these, see
  14618. the documentation of `org-diary'."
  14619. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  14620. (let* ((org-startup-folded nil)
  14621. (org-startup-align-all-tables nil)
  14622. (buffer (if (file-exists-p file)
  14623. (org-get-agenda-file-buffer file)
  14624. (error "No such file %s" file)))
  14625. arg results rtn)
  14626. (if (not buffer)
  14627. ;; If file does not exist, make sure an error message ends up in diary
  14628. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  14629. (with-current-buffer buffer
  14630. (unless (org-mode-p)
  14631. (error "Agenda file %s is not in `org-mode'" file))
  14632. (setq org-category-table (org-get-category-table))
  14633. (let ((case-fold-search nil))
  14634. (save-excursion
  14635. (save-restriction
  14636. (if org-agenda-restrict
  14637. (narrow-to-region org-agenda-restrict-begin
  14638. org-agenda-restrict-end)
  14639. (widen))
  14640. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  14641. (while (setq arg (pop args))
  14642. (cond
  14643. ((and (eq arg :todo)
  14644. (equal date (calendar-current-date)))
  14645. (setq rtn (org-agenda-get-todos))
  14646. (setq results (append results rtn)))
  14647. ((eq arg :timestamp)
  14648. (setq rtn (org-agenda-get-blocks))
  14649. (setq results (append results rtn))
  14650. (setq rtn (org-agenda-get-timestamps))
  14651. (setq results (append results rtn)))
  14652. ((eq arg :sexp)
  14653. (setq rtn (org-agenda-get-sexps))
  14654. (setq results (append results rtn)))
  14655. ((eq arg :scheduled)
  14656. (setq rtn (org-agenda-get-scheduled))
  14657. (setq results (append results rtn)))
  14658. ((eq arg :closed)
  14659. (setq rtn (org-agenda-get-closed))
  14660. (setq results (append results rtn)))
  14661. ((and (eq arg :deadline)
  14662. (equal date (calendar-current-date)))
  14663. (setq rtn (org-agenda-get-deadlines))
  14664. (setq results (append results rtn))))))))
  14665. results))))
  14666. (defun org-entry-is-done-p ()
  14667. "Is the current entry marked DONE?"
  14668. (save-excursion
  14669. (and (re-search-backward "[\r\n]\\*" nil t)
  14670. (looking-at org-nl-done-regexp))))
  14671. (defun org-at-date-range-p (&optional inactive-ok)
  14672. "Is the cursor inside a date range?"
  14673. (interactive)
  14674. (save-excursion
  14675. (catch 'exit
  14676. (let ((pos (point)))
  14677. (skip-chars-backward "^[<\r\n")
  14678. (skip-chars-backward "<[")
  14679. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  14680. (>= (match-end 0) pos)
  14681. (throw 'exit t))
  14682. (skip-chars-backward "^<[\r\n")
  14683. (skip-chars-backward "<[")
  14684. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  14685. (>= (match-end 0) pos)
  14686. (throw 'exit t)))
  14687. nil)))
  14688. (defun org-agenda-get-todos ()
  14689. "Return the TODO information for agenda display."
  14690. (let* ((props (list 'face nil
  14691. 'done-face 'org-done
  14692. 'org-not-done-regexp org-not-done-regexp
  14693. 'org-todo-regexp org-todo-regexp
  14694. 'mouse-face 'highlight
  14695. 'keymap org-agenda-keymap
  14696. 'help-echo
  14697. (format "mouse-2 or RET jump to org file %s"
  14698. (abbreviate-file-name buffer-file-name))))
  14699. ;; FIXME: get rid of the \n at some point but watch out
  14700. (regexp (concat "[\n\r]\\*+ *\\("
  14701. (if org-select-this-todo-keyword
  14702. (if (equal org-select-this-todo-keyword "*")
  14703. org-todo-regexp
  14704. (concat "\\<\\("
  14705. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  14706. "\\)\\>"))
  14707. org-not-done-regexp)
  14708. "[^\n\r]*\\)"))
  14709. marker priority category tags
  14710. ee txt beg end)
  14711. (goto-char (point-min))
  14712. (while (re-search-forward regexp nil t)
  14713. (catch :skip
  14714. (save-match-data
  14715. (beginning-of-line)
  14716. (setq beg (point) end (progn (outline-next-heading) (point)))
  14717. (when (or (and org-agenda-todo-ignore-scheduled (goto-char beg)
  14718. (re-search-forward org-scheduled-time-regexp end t))
  14719. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  14720. (re-search-forward org-deadline-time-regexp end t)
  14721. (org-deadline-close (match-string 1))))
  14722. (goto-char beg)
  14723. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  14724. (throw :skip nil)))
  14725. (goto-char beg)
  14726. (org-agenda-skip)
  14727. (goto-char (match-beginning 1))
  14728. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  14729. category (org-get-category)
  14730. tags (org-get-tags-at (point))
  14731. txt (org-format-agenda-item "" (match-string 1) category tags)
  14732. priority (1+ (org-get-priority txt)))
  14733. (org-add-props txt props
  14734. 'org-marker marker 'org-hd-marker marker
  14735. 'priority priority 'org-category category
  14736. 'type "todo")
  14737. (push txt ee)
  14738. (if org-agenda-todo-list-sublevels
  14739. (goto-char (match-end 1))
  14740. (org-end-of-subtree 'invisible))))
  14741. (nreverse ee)))
  14742. (defconst org-agenda-no-heading-message
  14743. "No heading for this item in buffer or region.")
  14744. (defun org-agenda-get-timestamps ()
  14745. "Return the date stamp information for agenda display."
  14746. (let* ((props (list 'face nil
  14747. 'org-not-done-regexp org-not-done-regexp
  14748. 'org-todo-regexp org-todo-regexp
  14749. 'mouse-face 'highlight
  14750. 'keymap org-agenda-keymap
  14751. 'help-echo
  14752. (format "mouse-2 or RET jump to org file %s"
  14753. (abbreviate-file-name buffer-file-name))))
  14754. ;???? (regexp (regexp-quote
  14755. ; (substring
  14756. ; (format-time-string
  14757. ; (car org-time-stamp-formats)
  14758. ; (apply 'encode-time ; DATE bound by calendar
  14759. ; (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  14760. ; 0 11)))
  14761. (d1 (calendar-absolute-from-gregorian date))
  14762. (regexp
  14763. (concat
  14764. (regexp-quote
  14765. (substring
  14766. (format-time-string
  14767. (car org-time-stamp-formats)
  14768. (apply 'encode-time ; DATE bound by calendar
  14769. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  14770. 0 11))
  14771. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  14772. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  14773. marker hdmarker deadlinep scheduledp donep tmp priority category
  14774. ee txt timestr tags b0 b3 e3)
  14775. (goto-char (point-min))
  14776. (while (re-search-forward regexp nil t)
  14777. (setq b0 (match-beginning 0)
  14778. b3 (match-beginning 3) e3 (match-end 3))
  14779. (catch :skip
  14780. (and (org-at-date-range-p) (throw :skip nil))
  14781. (org-agenda-skip)
  14782. (if (and (match-end 1)
  14783. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  14784. (throw :skip nil))
  14785. (if (and e3
  14786. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  14787. (throw :skip nil))
  14788. (setq marker (org-agenda-new-marker b0)
  14789. category (org-get-category b0)
  14790. tmp (buffer-substring (max (point-min)
  14791. (- b0 org-ds-keyword-length))
  14792. b0)
  14793. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  14794. deadlinep (string-match org-deadline-regexp tmp)
  14795. scheduledp (string-match org-scheduled-regexp tmp)
  14796. donep (org-entry-is-done-p))
  14797. (and org-agenda-skip-scheduled-if-done
  14798. scheduledp donep
  14799. (throw :skip t))
  14800. (and org-agenda-skip-deadline-if-done
  14801. deadlinep donep
  14802. (throw :skip t))
  14803. (if (string-match ">" timestr)
  14804. ;; substring should only run to end of time stamp
  14805. (setq timestr (substring timestr 0 (match-end 0))))
  14806. (save-excursion
  14807. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  14808. (progn
  14809. (goto-char (match-end 1))
  14810. (setq hdmarker (org-agenda-new-marker)
  14811. tags (org-get-tags-at))
  14812. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  14813. (setq txt (org-format-agenda-item
  14814. (format "%s%s"
  14815. (if deadlinep "Deadline: " "")
  14816. (if scheduledp "Scheduled: " ""))
  14817. (match-string 1) category tags timestr)))
  14818. (setq txt org-agenda-no-heading-message))
  14819. (setq priority (org-get-priority txt))
  14820. (org-add-props txt props
  14821. 'org-marker marker 'org-hd-marker hdmarker)
  14822. (if deadlinep
  14823. (org-add-props txt nil
  14824. 'face (if donep 'org-done 'org-warning)
  14825. 'type "deadline" 'date date
  14826. 'undone-face 'org-warning 'done-face 'org-done
  14827. 'org-category category 'priority (+ 100 priority))
  14828. (if scheduledp
  14829. (org-add-props txt nil
  14830. 'face 'org-scheduled-today
  14831. 'type "scheduled" 'date date
  14832. 'undone-face 'org-scheduled-today 'done-face 'org-done
  14833. 'org-category category 'priority (+ 99 priority))
  14834. (org-add-props txt nil 'priority priority
  14835. 'org-category category 'date date
  14836. 'type "timestamp")))
  14837. (push txt ee))
  14838. (outline-next-heading)))
  14839. (nreverse ee)))
  14840. (defun org-agenda-get-sexps ()
  14841. "Return the sexp information for agenda display."
  14842. (require 'diary-lib)
  14843. (let* ((props (list 'face nil
  14844. 'mouse-face 'highlight
  14845. 'keymap org-agenda-keymap
  14846. 'help-echo
  14847. (format "mouse-2 or RET jump to org file %s"
  14848. (abbreviate-file-name buffer-file-name))))
  14849. (regexp "^&?%%(")
  14850. marker category ee txt tags entry result beg b sexp sexp-entry)
  14851. (goto-char (point-min))
  14852. (while (re-search-forward regexp nil t)
  14853. (catch :skip
  14854. (org-agenda-skip)
  14855. (setq beg (match-beginning 0))
  14856. (goto-char (1- (match-end 0)))
  14857. (setq b (point))
  14858. (forward-sexp 1)
  14859. (setq sexp (buffer-substring b (point)))
  14860. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  14861. (org-trim (match-string 1))
  14862. ""))
  14863. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  14864. (when result
  14865. (setq marker (org-agenda-new-marker beg)
  14866. category (org-get-category beg))
  14867. (if (string-match "\\S-" result)
  14868. (setq txt result)
  14869. (setq txt "SEXP entry returned empty string"))
  14870. (setq txt (org-format-agenda-item
  14871. "" txt category tags 'time))
  14872. (org-add-props txt props 'org-marker marker)
  14873. (org-add-props txt nil
  14874. 'org-category category 'date date
  14875. 'type "sexp")
  14876. (push txt ee))))
  14877. (nreverse ee)))
  14878. (defun org-agenda-get-closed ()
  14879. "Return the logged TODO entries for agenda display."
  14880. (let* ((props (list 'mouse-face 'highlight
  14881. 'org-not-done-regexp org-not-done-regexp
  14882. 'org-todo-regexp org-todo-regexp
  14883. 'keymap org-agenda-keymap
  14884. 'help-echo
  14885. (format "mouse-2 or RET jump to org file %s"
  14886. (abbreviate-file-name buffer-file-name))))
  14887. (regexp (concat
  14888. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  14889. (regexp-quote
  14890. (substring
  14891. (format-time-string
  14892. (car org-time-stamp-formats)
  14893. (apply 'encode-time ; DATE bound by calendar
  14894. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  14895. 1 11))))
  14896. marker hdmarker priority category tags closedp
  14897. ee txt timestr)
  14898. (goto-char (point-min))
  14899. (while (re-search-forward regexp nil t)
  14900. (catch :skip
  14901. (org-agenda-skip)
  14902. (setq marker (org-agenda-new-marker (match-beginning 0))
  14903. closedp (equal (match-string 1) org-closed-string)
  14904. category (org-get-category (match-beginning 0))
  14905. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  14906. ;; donep (org-entry-is-done-p)
  14907. )
  14908. (if (string-match "\\]" timestr)
  14909. ;; substring should only run to end of time stamp
  14910. (setq timestr (substring timestr 0 (match-end 0))))
  14911. (save-excursion
  14912. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  14913. (progn
  14914. (goto-char (match-end 1))
  14915. (setq hdmarker (org-agenda-new-marker)
  14916. tags (org-get-tags-at))
  14917. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  14918. (setq txt (org-format-agenda-item
  14919. (if closedp "Closed: " "Clocked: ")
  14920. (match-string 1) category tags timestr)))
  14921. (setq txt org-agenda-no-heading-message))
  14922. (setq priority 100000)
  14923. (org-add-props txt props
  14924. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  14925. 'priority priority 'org-category category
  14926. 'type "closed" 'date date
  14927. 'undone-face 'org-warning 'done-face 'org-done)
  14928. (push txt ee))
  14929. (outline-next-heading)))
  14930. (nreverse ee)))
  14931. (defun org-agenda-get-deadlines ()
  14932. "Return the deadline information for agenda display."
  14933. (let* ((wdays org-deadline-warning-days)
  14934. (props (list 'mouse-face 'highlight
  14935. 'org-not-done-regexp org-not-done-regexp
  14936. 'org-todo-regexp org-todo-regexp
  14937. 'keymap org-agenda-keymap
  14938. 'help-echo
  14939. (format "mouse-2 or RET jump to org file %s"
  14940. (abbreviate-file-name buffer-file-name))))
  14941. (regexp org-deadline-time-regexp)
  14942. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  14943. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  14944. d2 diff pos pos1 category tags
  14945. ee txt head face)
  14946. (goto-char (point-min))
  14947. (while (re-search-forward regexp nil t)
  14948. (catch :skip
  14949. (org-agenda-skip)
  14950. (setq pos (1- (match-beginning 1))
  14951. ;??? d2 (time-to-days
  14952. ;??? (org-time-string-to-time (match-string 1)))
  14953. d2 (org-time-string-to-absolute (match-string 1) d1)
  14954. diff (- d2 d1))
  14955. ;; When to show a deadline in the calendar:
  14956. ;; If the expiration is within wdays warning time.
  14957. ;; Past-due deadlines are only shown on the current date
  14958. (if (and (< diff wdays) todayp (not (= diff 0)))
  14959. (save-excursion
  14960. (setq category (org-get-category))
  14961. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  14962. (progn
  14963. (goto-char (match-end 0))
  14964. (setq pos1 (match-end 1))
  14965. (setq tags (org-get-tags-at pos1))
  14966. (setq head (buffer-substring-no-properties
  14967. (point)
  14968. (progn (skip-chars-forward "^\r\n")
  14969. (point))))
  14970. (if (string-match org-looking-at-done-regexp head)
  14971. (setq txt nil)
  14972. (setq txt (org-format-agenda-item
  14973. (format "In %3d d.: " diff) head category tags))))
  14974. (setq txt org-agenda-no-heading-message))
  14975. (when txt
  14976. (setq face (cond ((<= diff 0) 'org-warning)
  14977. ((<= diff 5) 'org-upcoming-deadline)
  14978. (t nil)))
  14979. (org-add-props txt props
  14980. 'org-marker (org-agenda-new-marker pos)
  14981. 'org-hd-marker (org-agenda-new-marker pos1)
  14982. 'priority (+ (- 10 diff) (org-get-priority txt))
  14983. 'org-category category
  14984. 'type "upcoming-deadline" 'date d2
  14985. 'face face 'undone-face face 'done-face 'org-done)
  14986. (push txt ee))))))
  14987. ee))
  14988. (defun org-agenda-get-scheduled ()
  14989. "Return the scheduled information for agenda display."
  14990. (let* ((props (list 'face 'org-scheduled-previously
  14991. 'org-not-done-regexp org-not-done-regexp
  14992. 'org-todo-regexp org-todo-regexp
  14993. 'undone-face 'org-scheduled-previously
  14994. 'done-face 'org-done
  14995. 'mouse-face 'highlight
  14996. 'keymap org-agenda-keymap
  14997. 'help-echo
  14998. (format "mouse-2 or RET jump to org file %s"
  14999. (abbreviate-file-name buffer-file-name))))
  15000. (regexp org-scheduled-time-regexp)
  15001. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  15002. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  15003. d2 diff pos pos1 category tags
  15004. ee txt head)
  15005. (goto-char (point-min))
  15006. (while (re-search-forward regexp nil t)
  15007. (catch :skip
  15008. (org-agenda-skip)
  15009. (setq pos (1- (match-beginning 1))
  15010. d2 (org-time-string-to-absolute (match-string 1) d1)
  15011. ;??? d2 (time-to-days
  15012. ;??? (org-time-string-to-time (match-string 1)))
  15013. diff (- d2 d1))
  15014. ;; When to show a scheduled item in the calendar:
  15015. ;; If it is on or past the date.
  15016. (if (and (< diff 0) todayp)
  15017. (save-excursion
  15018. (setq category (org-get-category))
  15019. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  15020. (progn
  15021. (goto-char (match-end 0))
  15022. (setq pos1 (match-end 1))
  15023. (setq tags (org-get-tags-at))
  15024. (setq head (buffer-substring-no-properties
  15025. (point)
  15026. (progn (skip-chars-forward "^\r\n") (point))))
  15027. (if (string-match org-looking-at-done-regexp head)
  15028. (setq txt nil)
  15029. (setq txt (org-format-agenda-item
  15030. (format "Sched.%2dx: " (- 1 diff)) head
  15031. category tags))))
  15032. (setq txt org-agenda-no-heading-message))
  15033. (when txt
  15034. (org-add-props txt props
  15035. 'org-marker (org-agenda-new-marker pos)
  15036. 'org-hd-marker (org-agenda-new-marker pos1)
  15037. 'type "past-scheduled" 'date d2
  15038. 'priority (+ (- 5 diff) (org-get-priority txt))
  15039. 'org-category category)
  15040. (push txt ee))))))
  15041. ee))
  15042. (defun org-agenda-get-blocks ()
  15043. "Return the date-range information for agenda display."
  15044. (let* ((props (list 'face nil
  15045. 'org-not-done-regexp org-not-done-regexp
  15046. 'org-todo-regexp org-todo-regexp
  15047. 'mouse-face 'highlight
  15048. 'keymap org-agenda-keymap
  15049. 'help-echo
  15050. (format "mouse-2 or RET jump to org file %s"
  15051. (abbreviate-file-name buffer-file-name))))
  15052. (regexp org-tr-regexp)
  15053. (d0 (calendar-absolute-from-gregorian date))
  15054. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos)
  15055. (goto-char (point-min))
  15056. (while (re-search-forward regexp nil t)
  15057. (catch :skip
  15058. (org-agenda-skip)
  15059. (setq pos (point))
  15060. (setq timestr (match-string 0)
  15061. s1 (match-string 1)
  15062. s2 (match-string 2)
  15063. d1 (time-to-days (org-time-string-to-time s1))
  15064. d2 (time-to-days (org-time-string-to-time s2)))
  15065. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  15066. ;; Only allow days between the limits, because the normal
  15067. ;; date stamps will catch the limits.
  15068. (save-excursion
  15069. (setq marker (org-agenda-new-marker (point)))
  15070. (setq category (org-get-category))
  15071. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  15072. (progn
  15073. (setq hdmarker (org-agenda-new-marker (match-end 1)))
  15074. (goto-char (match-end 1))
  15075. (setq tags (org-get-tags-at))
  15076. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  15077. (setq txt (org-format-agenda-item
  15078. (format (if (= d1 d2) "" "(%d/%d): ")
  15079. (1+ (- d0 d1)) (1+ (- d2 d1)))
  15080. (match-string 1) category tags
  15081. (if (= d0 d1) timestr))))
  15082. (setq txt org-agenda-no-heading-message))
  15083. (org-add-props txt props
  15084. 'org-marker marker 'org-hd-marker hdmarker
  15085. 'type "block" 'date date
  15086. 'priority (org-get-priority txt) 'org-category category)
  15087. (push txt ee)))
  15088. (goto-char pos)))
  15089. ;; Sort the entries by expiration date.
  15090. (nreverse ee)))
  15091. ;;; Agenda presentation and sorting
  15092. (defconst org-plain-time-of-day-regexp
  15093. (concat
  15094. "\\(\\<[012]?[0-9]"
  15095. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  15096. "\\(--?"
  15097. "\\(\\<[012]?[0-9]"
  15098. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  15099. "\\)?")
  15100. "Regular expression to match a plain time or time range.
  15101. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  15102. groups carry important information:
  15103. 0 the full match
  15104. 1 the first time, range or not
  15105. 8 the second time, if it is a range.")
  15106. (defconst org-stamp-time-of-day-regexp
  15107. (concat
  15108. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  15109. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  15110. "\\(--?"
  15111. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  15112. "Regular expression to match a timestamp time or time range.
  15113. After a match, the following groups carry important information:
  15114. 0 the full match
  15115. 1 date plus weekday, for backreferencing to make sure both times on same day
  15116. 2 the first time, range or not
  15117. 4 the second time, if it is a range.")
  15118. (defvar org-prefix-has-time nil
  15119. "A flag, set by `org-compile-prefix-format'.
  15120. The flag is set if the currently compiled format contains a `%t'.")
  15121. (defvar org-prefix-has-tag nil
  15122. "A flag, set by `org-compile-prefix-format'.
  15123. The flag is set if the currently compiled format contains a `%T'.")
  15124. (defun org-format-agenda-item (extra txt &optional category tags dotime
  15125. noprefix)
  15126. "Format TXT to be inserted into the agenda buffer.
  15127. In particular, it adds the prefix and corresponding text properties. EXTRA
  15128. must be a string and replaces the `%s' specifier in the prefix format.
  15129. CATEGORY (string, symbol or nil) may be used to overrule the default
  15130. category taken from local variable or file name. It will replace the `%c'
  15131. specifier in the format. DOTIME, when non-nil, indicates that a
  15132. time-of-day should be extracted from TXT for sorting of this entry, and for
  15133. the `%t' specifier in the format. When DOTIME is a string, this string is
  15134. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  15135. only the correctly processes TXT should be returned - this is used by
  15136. `org-agenda-change-all-lines'. TAGS can be the tags of the headline."
  15137. (save-match-data
  15138. ;; Diary entries sometimes have extra whitespace at the beginning
  15139. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  15140. (let* ((category (or category
  15141. org-category
  15142. (if buffer-file-name
  15143. (file-name-sans-extension
  15144. (file-name-nondirectory buffer-file-name))
  15145. "")))
  15146. (tag (if tags (nth (1- (length tags)) tags) ""))
  15147. time ; time and tag are needed for the eval of the prefix format
  15148. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  15149. (time-of-day (and dotime (org-get-time-of-day ts)))
  15150. stamp plain s0 s1 s2 rtn srp)
  15151. (when (and dotime time-of-day org-prefix-has-time)
  15152. ;; Extract starting and ending time and move them to prefix
  15153. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  15154. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  15155. (setq s0 (match-string 0 ts)
  15156. srp (and stamp (match-end 3))
  15157. s1 (match-string (if plain 1 2) ts)
  15158. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  15159. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  15160. ;; them, we might want to remove them there to avoid duplication.
  15161. ;; The user can turn this off with a variable.
  15162. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  15163. (string-match (concat (regexp-quote s0) " *") txt)
  15164. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  15165. (= (match-beginning 0) 0)
  15166. t))
  15167. (setq txt (replace-match "" nil nil txt))))
  15168. ;; Normalize the time(s) to 24 hour
  15169. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  15170. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  15171. (when (string-match "\\([ \t]+\\)\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" txt)
  15172. ;; Tags are in the string
  15173. (if (or (eq org-agenda-remove-tags t)
  15174. (and org-agenda-remove-tags
  15175. org-prefix-has-tag))
  15176. (setq txt (replace-match "" t t txt))
  15177. (setq txt (replace-match
  15178. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  15179. (match-string 2 txt))
  15180. t t txt))))
  15181. ;; Create the final string
  15182. (if noprefix
  15183. (setq rtn txt)
  15184. ;; Prepare the variables needed in the eval of the compiled format
  15185. (setq time (cond (s2 (concat s1 "-" s2))
  15186. (s1 (concat s1 "......"))
  15187. (t ""))
  15188. extra (or extra "")
  15189. category (if (symbolp category) (symbol-name category) category))
  15190. ;; Evaluate the compiled format
  15191. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  15192. ;; And finally add the text properties
  15193. (org-add-props rtn nil
  15194. 'org-category (downcase category) 'tags tags
  15195. 'prefix-length (- (length rtn) (length txt))
  15196. 'time-of-day time-of-day
  15197. 'txt txt
  15198. 'time time
  15199. 'extra extra
  15200. 'dotime dotime))))
  15201. (defvar org-agenda-sorting-strategy) ;; FIXME: can be removed?
  15202. (defvar org-agenda-sorting-strategy-selected nil)
  15203. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  15204. (catch 'exit
  15205. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  15206. ((and todayp (member 'today (car org-agenda-time-grid))))
  15207. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  15208. ((member 'weekly (car org-agenda-time-grid)))
  15209. (t (throw 'exit list)))
  15210. (let* ((have (delq nil (mapcar
  15211. (lambda (x) (get-text-property 1 'time-of-day x))
  15212. list)))
  15213. (string (nth 1 org-agenda-time-grid))
  15214. (gridtimes (nth 2 org-agenda-time-grid))
  15215. (req (car org-agenda-time-grid))
  15216. (remove (member 'remove-match req))
  15217. new time)
  15218. (if (and (member 'require-timed req) (not have))
  15219. ;; don't show empty grid
  15220. (throw 'exit list))
  15221. (while (setq time (pop gridtimes))
  15222. (unless (and remove (member time have))
  15223. (setq time (int-to-string time))
  15224. (push (org-format-agenda-item
  15225. nil string "" nil
  15226. (concat (substring time 0 -2) ":" (substring time -2)))
  15227. new)
  15228. (put-text-property
  15229. 1 (length (car new)) 'face 'org-time-grid (car new))))
  15230. (if (member 'time-up org-agenda-sorting-strategy-selected)
  15231. (append new list)
  15232. (append list new)))))
  15233. (defun org-compile-prefix-format (key)
  15234. "Compile the prefix format into a Lisp form that can be evaluated.
  15235. The resulting form is returned and stored in the variable
  15236. `org-prefix-format-compiled'."
  15237. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  15238. (let ((s (cond
  15239. ((stringp org-agenda-prefix-format)
  15240. org-agenda-prefix-format)
  15241. ((assq key org-agenda-prefix-format)
  15242. (cdr (assq key org-agenda-prefix-format)))
  15243. (t " %-12:c%?-12t% s")))
  15244. (start 0)
  15245. varform vars var e c f opt)
  15246. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  15247. s start)
  15248. (setq var (cdr (assoc (match-string 4 s)
  15249. '(("c" . category) ("t" . time) ("s" . extra)
  15250. ("T" . tag))))
  15251. c (or (match-string 3 s) "")
  15252. opt (match-beginning 1)
  15253. start (1+ (match-beginning 0)))
  15254. (if (equal var 'time) (setq org-prefix-has-time t))
  15255. (if (equal var 'tag) (setq org-prefix-has-tag t))
  15256. (setq f (concat "%" (match-string 2 s) "s"))
  15257. (if opt
  15258. (setq varform
  15259. `(if (equal "" ,var)
  15260. ""
  15261. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  15262. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  15263. (setq s (replace-match "%s" t nil s))
  15264. (push varform vars))
  15265. (setq vars (nreverse vars))
  15266. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  15267. (defun org-set-sorting-strategy (key)
  15268. (if (symbolp (car org-agenda-sorting-strategy))
  15269. ;; the old format
  15270. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  15271. (setq org-agenda-sorting-strategy-selected
  15272. (or (cdr (assq key org-agenda-sorting-strategy))
  15273. (cdr (assq 'agenda org-agenda-sorting-strategy))
  15274. '(time-up category-keep priority-down)))))
  15275. (defun org-get-time-of-day (s &optional string mod24)
  15276. "Check string S for a time of day.
  15277. If found, return it as a military time number between 0 and 2400.
  15278. If not found, return nil.
  15279. The optional STRING argument forces conversion into a 5 character wide string
  15280. HH:MM."
  15281. (save-match-data
  15282. (when
  15283. (or
  15284. (string-match
  15285. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  15286. (string-match
  15287. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  15288. (let* ((h (string-to-number (match-string 1 s)))
  15289. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  15290. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  15291. (am-p (equal ampm "am"))
  15292. (h1 (cond ((not ampm) h)
  15293. ((= h 12) (if am-p 0 12))
  15294. (t (+ h (if am-p 0 12)))))
  15295. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  15296. (mod h1 24) h1))
  15297. (t0 (+ (* 100 h2) m))
  15298. (t1 (concat (if (>= h1 24) "+" " ")
  15299. (if (< t0 100) "0" "")
  15300. (if (< t0 10) "0" "")
  15301. (int-to-string t0))))
  15302. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  15303. (defun org-finalize-agenda-entries (list &optional nosort)
  15304. "Sort and concatenate the agenda items."
  15305. (setq list (mapcar 'org-agenda-highlight-todo list))
  15306. (if nosort
  15307. list
  15308. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  15309. (defun org-agenda-highlight-todo (x)
  15310. (let (re pl)
  15311. (if (eq x 'line)
  15312. (save-excursion
  15313. (beginning-of-line 1)
  15314. (setq re (get-text-property (point) 'org-not-done-regexp))
  15315. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  15316. (and (looking-at (concat "[ \t]*\\.*" re))
  15317. (add-text-properties (match-beginning 0) (match-end 0)
  15318. '(face org-todo))))
  15319. (setq re (concat (get-text-property 0 'org-not-done-regexp x))
  15320. pl (get-text-property 0 'prefix-length x))
  15321. (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  15322. (add-text-properties (or (match-end 1) (match-end 0)) (match-end 0)
  15323. '(face org-todo) x))
  15324. x)))
  15325. (defsubst org-cmp-priority (a b)
  15326. "Compare the priorities of string A and B."
  15327. (let ((pa (or (get-text-property 1 'priority a) 0))
  15328. (pb (or (get-text-property 1 'priority b) 0)))
  15329. (cond ((> pa pb) +1)
  15330. ((< pa pb) -1)
  15331. (t nil))))
  15332. (defsubst org-cmp-category (a b)
  15333. "Compare the string values of categories of strings A and B."
  15334. (let ((ca (or (get-text-property 1 'category a) ""))
  15335. (cb (or (get-text-property 1 'category b) "")))
  15336. (cond ((string-lessp ca cb) -1)
  15337. ((string-lessp cb ca) +1)
  15338. (t nil))))
  15339. (defsubst org-cmp-tag (a b)
  15340. "Compare the string values of categories of strings A and B."
  15341. (let ((ta (car (last (get-text-property 1 'tags a))))
  15342. (tb (car (last (get-text-property 1 'tags b)))))
  15343. (cond ((not ta) +1)
  15344. ((not tb) -1)
  15345. ((string-lessp ta tb) -1)
  15346. ((string-lessp tb ta) +1)
  15347. (t nil))))
  15348. (defsubst org-cmp-time (a b)
  15349. "Compare the time-of-day values of strings A and B."
  15350. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  15351. (ta (or (get-text-property 1 'time-of-day a) def))
  15352. (tb (or (get-text-property 1 'time-of-day b) def)))
  15353. (cond ((< ta tb) -1)
  15354. ((< tb ta) +1)
  15355. (t nil))))
  15356. (defun org-entries-lessp (a b)
  15357. "Predicate for sorting agenda entries."
  15358. ;; The following variables will be used when the form is evaluated.
  15359. ;; So even though the compiler complains, keep them.
  15360. (let* ((time-up (org-cmp-time a b))
  15361. (time-down (if time-up (- time-up) nil))
  15362. (priority-up (org-cmp-priority a b))
  15363. (priority-down (if priority-up (- priority-up) nil))
  15364. (category-up (org-cmp-category a b))
  15365. (category-down (if category-up (- category-up) nil))
  15366. (category-keep (if category-up +1 nil))
  15367. (tag-up (org-cmp-tag a b))
  15368. (tag-down (if tag-up (- tag-up) nil)))
  15369. (cdr (assoc
  15370. (eval (cons 'or org-agenda-sorting-strategy-selected))
  15371. '((-1 . t) (1 . nil) (nil . nil))))))
  15372. ;;; Agenda commands
  15373. (defun org-agenda-check-type (error &rest types)
  15374. "Check if agenda buffer is of allowed type.
  15375. If ERROR is non-nil, throw an error, otherwise just return nil."
  15376. (if (memq org-agenda-type types)
  15377. t
  15378. (if error
  15379. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  15380. nil)))
  15381. (defun org-agenda-quit ()
  15382. "Exit agenda by removing the window or the buffer."
  15383. (interactive)
  15384. (let ((buf (current-buffer)))
  15385. (if (not (one-window-p)) (delete-window))
  15386. (kill-buffer buf)
  15387. (org-agenda-maybe-reset-markers 'force))
  15388. ;; Maybe restore the pre-agenda window configuration.
  15389. (and org-agenda-restore-windows-after-quit
  15390. (not (eq org-agenda-window-setup 'other-frame))
  15391. org-pre-agenda-window-conf
  15392. (set-window-configuration org-pre-agenda-window-conf)))
  15393. (defun org-agenda-exit ()
  15394. "Exit agenda by removing the window or the buffer.
  15395. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  15396. Org-mode buffers visited directly by the user will not be touched."
  15397. (interactive)
  15398. (org-release-buffers org-agenda-new-buffers)
  15399. (setq org-agenda-new-buffers nil)
  15400. (org-agenda-quit))
  15401. (defun org-save-all-org-buffers ()
  15402. "Save all Org-mode buffers without user confirmation."
  15403. (interactive)
  15404. (message "Saving all Org-mode buffers...")
  15405. (save-some-buffers t 'org-mode-p)
  15406. (message "Saving all Org-mode buffers... done"))
  15407. (defun org-agenda-redo ()
  15408. "Rebuild Agenda.
  15409. When this is the global TODO list, a prefix argument will be interpreted."
  15410. (interactive)
  15411. (let* ((org-agenda-keep-modes t)
  15412. (line (org-current-line))
  15413. (window-line (- line (org-current-line (window-start)))))
  15414. (message "Rebuilding agenda buffer...")
  15415. (eval org-agenda-redo-command)
  15416. (setq org-agenda-undo-list nil
  15417. org-agenda-pending-undo-list nil)
  15418. (message "Rebuilding agenda buffer...done")
  15419. (goto-line line)
  15420. (recenter window-line)))
  15421. (defun org-agenda-goto-today ()
  15422. "Go to today."
  15423. (interactive)
  15424. (org-agenda-check-type t 'timeline 'agenda)
  15425. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  15426. (cond
  15427. (tdpos (goto-char tdpos))
  15428. ((eq org-agenda-type 'agenda)
  15429. (let ((org-agenda-overriding-arguments org-agenda-last-arguments))
  15430. (setf (nth 1 org-agenda-overriding-arguments) nil)
  15431. (org-agenda-redo)
  15432. (org-agenda-find-today-or-agenda)))
  15433. (t (error "Cannot find today")))))
  15434. (defun org-agenda-find-today-or-agenda ()
  15435. (goto-char
  15436. (or (text-property-any (point-min) (point-max) 'org-today t)
  15437. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  15438. (point-min))))
  15439. (defun org-agenda-later (arg)
  15440. "Go forward in time by `org-agenda-ndays' days.
  15441. With prefix ARG, go forward that many times `org-agenda-ndays'."
  15442. (interactive "p")
  15443. (org-agenda-check-type t 'agenda)
  15444. (let ((org-agenda-overriding-arguments
  15445. (list (car org-agenda-last-arguments)
  15446. (+ org-starting-day (* arg org-agenda-ndays))
  15447. nil t)))
  15448. (org-agenda-redo)
  15449. (org-agenda-find-today-or-agenda)))
  15450. (defun org-agenda-earlier (arg)
  15451. "Go back in time by `org-agenda-ndays' days.
  15452. With prefix ARG, go back that many times `org-agenda-ndays'."
  15453. (interactive "p")
  15454. (org-agenda-check-type t 'agenda)
  15455. (let ((org-agenda-overriding-arguments
  15456. (list (car org-agenda-last-arguments)
  15457. (- org-starting-day (* arg org-agenda-ndays))
  15458. nil t)))
  15459. (org-agenda-redo)
  15460. (org-agenda-find-today-or-agenda)))
  15461. (defun org-agenda-week-view ()
  15462. "Switch to weekly view for agenda."
  15463. (interactive)
  15464. (org-agenda-check-type t 'agenda)
  15465. (if (= org-agenda-ndays 7)
  15466. (error "This is already the week view"))
  15467. (setq org-agenda-ndays 7)
  15468. (let ((org-agenda-overriding-arguments
  15469. (list (car org-agenda-last-arguments)
  15470. (or (get-text-property (point) 'day)
  15471. org-starting-day)
  15472. nil t)))
  15473. (org-agenda-redo)
  15474. (org-agenda-find-today-or-agenda))
  15475. (org-agenda-set-mode-name)
  15476. (message "Switched to week view"))
  15477. (defun org-agenda-day-view ()
  15478. "Switch to daily view for agenda."
  15479. (interactive)
  15480. (org-agenda-check-type t 'agenda)
  15481. (if (= org-agenda-ndays 1)
  15482. (error "This is already the day view"))
  15483. (setq org-agenda-ndays 1)
  15484. (let ((org-agenda-overriding-arguments
  15485. (list (car org-agenda-last-arguments)
  15486. (or (get-text-property (point) 'day)
  15487. org-starting-day)
  15488. nil t)))
  15489. (org-agenda-redo)
  15490. (org-agenda-find-today-or-agenda))
  15491. (org-agenda-set-mode-name)
  15492. (message "Switched to day view"))
  15493. ;; FIXME: this no longer works if user make date format that starts with a blank
  15494. (defun org-agenda-next-date-line (&optional arg)
  15495. "Jump to the next line indicating a date in agenda buffer."
  15496. (interactive "p")
  15497. (org-agenda-check-type t 'agenda 'timeline)
  15498. (beginning-of-line 1)
  15499. (if (looking-at "^\\S-") (forward-char 1))
  15500. (if (not (re-search-forward "^\\S-" nil t arg))
  15501. (progn
  15502. (backward-char 1)
  15503. (error "No next date after this line in this buffer")))
  15504. (goto-char (match-beginning 0)))
  15505. (defun org-agenda-previous-date-line (&optional arg)
  15506. "Jump to the previous line indicating a date in agenda buffer."
  15507. (interactive "p")
  15508. (org-agenda-check-type t 'agenda 'timeline)
  15509. (beginning-of-line 1)
  15510. (if (not (re-search-backward "^\\S-" nil t arg))
  15511. (error "No previous date before this line in this buffer")))
  15512. ;; Initialize the highlight
  15513. (defvar org-hl (org-make-overlay 1 1))
  15514. (org-overlay-put org-hl 'face 'highlight)
  15515. (defun org-highlight (begin end &optional buffer)
  15516. "Highlight a region with overlay."
  15517. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  15518. org-hl begin end (or buffer (current-buffer))))
  15519. (defun org-unhighlight ()
  15520. "Detach overlay INDEX."
  15521. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  15522. ;; FIXME this is currently not used.
  15523. (defun org-highlight-until-next-command (beg end &optional buffer)
  15524. (org-highlight beg end buffer)
  15525. (add-hook 'pre-command-hook 'org-unhighlight-once))
  15526. (defun org-unhighlight-once ()
  15527. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  15528. (org-unhighlight))
  15529. (defun org-agenda-follow-mode ()
  15530. "Toggle follow mode in an agenda buffer."
  15531. (interactive)
  15532. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  15533. (org-agenda-set-mode-name)
  15534. (message "Follow mode is %s"
  15535. (if org-agenda-follow-mode "on" "off")))
  15536. (defun org-agenda-log-mode ()
  15537. "Toggle log mode in an agenda buffer."
  15538. (interactive)
  15539. (org-agenda-check-type t 'agenda 'timeline)
  15540. (setq org-agenda-show-log (not org-agenda-show-log))
  15541. (org-agenda-set-mode-name)
  15542. (org-agenda-redo)
  15543. (message "Log mode is %s"
  15544. (if org-agenda-show-log "on" "off")))
  15545. (defun org-agenda-toggle-diary ()
  15546. "Toggle diary inclusion in an agenda buffer."
  15547. (interactive)
  15548. (org-agenda-check-type t 'agenda)
  15549. (setq org-agenda-include-diary (not org-agenda-include-diary))
  15550. (org-agenda-redo)
  15551. (org-agenda-set-mode-name)
  15552. (message "Diary inclusion turned %s"
  15553. (if org-agenda-include-diary "on" "off")))
  15554. (defun org-agenda-toggle-time-grid ()
  15555. "Toggle time grid in an agenda buffer."
  15556. (interactive)
  15557. (org-agenda-check-type t 'agenda)
  15558. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  15559. (org-agenda-redo)
  15560. (org-agenda-set-mode-name)
  15561. (message "Time-grid turned %s"
  15562. (if org-agenda-use-time-grid "on" "off")))
  15563. (defun org-agenda-set-mode-name ()
  15564. "Set the mode name to indicate all the small mode settings."
  15565. (setq mode-name
  15566. (concat "Org-Agenda"
  15567. (if (equal org-agenda-ndays 1) " Day" "")
  15568. (if (equal org-agenda-ndays 7) " Week" "")
  15569. (if org-agenda-follow-mode " Follow" "")
  15570. (if org-agenda-include-diary " Diary" "")
  15571. (if org-agenda-use-time-grid " Grid" "")
  15572. (if org-agenda-show-log " Log" "")))
  15573. (force-mode-line-update))
  15574. (defun org-agenda-post-command-hook ()
  15575. (and (eolp) (not (bolp)) (backward-char 1))
  15576. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  15577. (if (and org-agenda-follow-mode
  15578. (get-text-property (point) 'org-marker))
  15579. (org-agenda-show)))
  15580. (defun org-agenda-show-priority ()
  15581. "Show the priority of the current item.
  15582. This priority is composed of the main priority given with the [#A] cookies,
  15583. and by additional input from the age of a schedules or deadline entry."
  15584. (interactive)
  15585. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  15586. (message "Priority is %d" (if pri pri -1000))))
  15587. (defun org-agenda-show-tags ()
  15588. "Show the tags applicable to the current item."
  15589. (interactive)
  15590. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  15591. (if tags
  15592. (message "Tags are :%s:"
  15593. (org-no-properties (mapconcat 'identity tags ":")))
  15594. (message "No tags associated with this line"))))
  15595. (defun org-agenda-goto (&optional highlight)
  15596. "Go to the Org-mode file which contains the item at point."
  15597. (interactive)
  15598. (let* ((marker (or (get-text-property (point) 'org-marker)
  15599. (org-agenda-error)))
  15600. (buffer (marker-buffer marker))
  15601. (pos (marker-position marker)))
  15602. (switch-to-buffer-other-window buffer)
  15603. (widen)
  15604. (goto-char pos)
  15605. (when (org-mode-p)
  15606. (org-show-context 'agenda)
  15607. (save-excursion
  15608. (and (outline-next-heading)
  15609. (org-flag-heading nil)))) ; show the next heading
  15610. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  15611. (defun org-agenda-kill ()
  15612. "Kill the entry or subtree belonging to the current agenda entry."
  15613. (interactive)
  15614. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  15615. (let* ((marker (or (get-text-property (point) 'org-marker)
  15616. (org-agenda-error)))
  15617. (buffer (marker-buffer marker))
  15618. (pos (marker-position marker))
  15619. (type (get-text-property (point) 'type))
  15620. dbeg dend (n 0) conf)
  15621. (org-with-remote-undo buffer
  15622. (with-current-buffer buffer
  15623. (save-excursion
  15624. (goto-char pos)
  15625. (if (and (org-mode-p) (not (member type '("sexp"))))
  15626. (setq dbeg (progn (org-back-to-heading t) (point))
  15627. dend (org-end-of-subtree t))
  15628. (setq dbeg (point-at-bol)
  15629. dend (min (point-max) (1+ (point-at-eol)))))
  15630. (goto-char dbeg)
  15631. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  15632. (setq conf (or (eq t org-agenda-confirm-kill)
  15633. (and (numberp org-agenda-confirm-kill)
  15634. (> n org-agenda-confirm-kill))))
  15635. (and conf
  15636. (not (y-or-n-p
  15637. (format "Delete entry with %d lines in buffer \"%s\"? "
  15638. n (buffer-name buffer))))
  15639. (error "Abort"))
  15640. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  15641. (with-current-buffer buffer (delete-region dbeg dend))
  15642. (message "Agenda item and source killed"))))
  15643. (defun org-agenda-archive ()
  15644. "Kill the entry or subtree belonging to the current agenda entry."
  15645. (interactive)
  15646. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  15647. (let* ((marker (or (get-text-property (point) 'org-marker)
  15648. (org-agenda-error)))
  15649. (buffer (marker-buffer marker))
  15650. (pos (marker-position marker)))
  15651. (org-with-remote-undo buffer
  15652. (with-current-buffer buffer
  15653. (if (org-mode-p)
  15654. (save-excursion
  15655. (goto-char pos)
  15656. (org-remove-subtree-entries-from-agenda)
  15657. (org-back-to-heading t)
  15658. (org-archive-subtree))
  15659. (error "Archiving works only in Org-mode files"))))))
  15660. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  15661. "Remove all lines in the agenda that correspond to a given subtree.
  15662. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  15663. If this information is not given, the function uses the tree at point."
  15664. (let ((buf (or buf (current-buffer))) m p)
  15665. (save-excursion
  15666. (unless (and beg end)
  15667. (org-back-to-heading t)
  15668. (setq beg (point))
  15669. (org-end-of-subtree t)
  15670. (setq end (point)))
  15671. (set-buffer (get-buffer org-agenda-buffer-name))
  15672. (save-excursion
  15673. (goto-char (point-max))
  15674. (beginning-of-line 1)
  15675. (while (not (bobp))
  15676. (when (and (setq m (get-text-property (point) 'org-marker))
  15677. (equal buf (marker-buffer m))
  15678. (setq p (marker-position m))
  15679. (>= p beg)
  15680. (<= p end))
  15681. (let (buffer-read-only)
  15682. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  15683. (beginning-of-line 0))))))
  15684. (defun org-agenda-open-link ()
  15685. "Follow the link in the current line, if any."
  15686. (interactive)
  15687. (let ((eol (point-at-eol)))
  15688. (save-excursion
  15689. (if (or (re-search-forward org-bracket-link-regexp eol t)
  15690. (re-search-forward org-angle-link-re eol t)
  15691. (re-search-forward org-plain-link-re eol t))
  15692. (call-interactively 'org-open-at-point)
  15693. (error "No link in current line")))))
  15694. (defun org-agenda-switch-to (&optional delete-other-windows)
  15695. "Go to the Org-mode file which contains the item at point."
  15696. (interactive)
  15697. (let* ((marker (or (get-text-property (point) 'org-marker)
  15698. (org-agenda-error)))
  15699. (buffer (marker-buffer marker))
  15700. (pos (marker-position marker)))
  15701. (switch-to-buffer buffer)
  15702. (and delete-other-windows (delete-other-windows))
  15703. (widen)
  15704. (goto-char pos)
  15705. (when (org-mode-p)
  15706. (org-show-context 'agenda)
  15707. (save-excursion
  15708. (and (outline-next-heading)
  15709. (org-flag-heading nil)))))) ; show the next heading
  15710. (defun org-agenda-goto-mouse (ev)
  15711. "Go to the Org-mode file which contains the item at the mouse click."
  15712. (interactive "e")
  15713. (mouse-set-point ev)
  15714. (org-agenda-goto))
  15715. (defun org-agenda-show ()
  15716. "Display the Org-mode file which contains the item at point."
  15717. (interactive)
  15718. (let ((win (selected-window)))
  15719. (org-agenda-goto t)
  15720. (select-window win)))
  15721. (defun org-agenda-recenter (arg)
  15722. "Display the Org-mode file which contains the item at point and recenter."
  15723. (interactive "P")
  15724. (let ((win (selected-window)))
  15725. (org-agenda-goto t)
  15726. (recenter arg)
  15727. (select-window win)))
  15728. (defun org-agenda-show-mouse (ev)
  15729. "Display the Org-mode file which contains the item at the mouse click."
  15730. (interactive "e")
  15731. (mouse-set-point ev)
  15732. (org-agenda-show))
  15733. (defun org-agenda-check-no-diary ()
  15734. "Check if the entry is a diary link and abort if yes."
  15735. (if (get-text-property (point) 'org-agenda-diary-link)
  15736. (org-agenda-error)))
  15737. (defun org-agenda-error ()
  15738. (error "Command not allowed in this line"))
  15739. (defun org-agenda-tree-to-indirect-buffer ()
  15740. "Show the subtree corresponding to the current entry in an indirect buffer.
  15741. This calls the command `org-tree-to-indirect-buffer' from the original
  15742. Org-mode buffer.
  15743. With numerical prefix arg ARG, go up to this level and then take that tree.
  15744. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  15745. dedicated frame)."
  15746. (interactive)
  15747. (org-agenda-check-no-diary)
  15748. (let* ((marker (or (get-text-property (point) 'org-marker)
  15749. (org-agenda-error)))
  15750. (buffer (marker-buffer marker))
  15751. (pos (marker-position marker)))
  15752. (with-current-buffer buffer
  15753. (save-excursion
  15754. (goto-char pos)
  15755. (call-interactively 'org-tree-to-indirect-buffer)))))
  15756. (defvar org-last-heading-marker (make-marker)
  15757. "Marker pointing to the headline that last changed its TODO state
  15758. by a remote command from the agenda.")
  15759. (defun org-agenda-todo-nextset ()
  15760. "Switch TODO entry to next sequence."
  15761. (interactive)
  15762. (org-agenda-todo 'nextset))
  15763. (defun org-agenda-todo-previousset ()
  15764. "Switch TODO entry to previous sequence."
  15765. (interactive)
  15766. (org-agenda-todo 'previousset))
  15767. (defun org-agenda-todo (&optional arg)
  15768. "Cycle TODO state of line at point, also in Org-mode file.
  15769. This changes the line at point, all other lines in the agenda referring to
  15770. the same tree node, and the headline of the tree node in the Org-mode file."
  15771. (interactive "P")
  15772. (org-agenda-check-no-diary)
  15773. (let* ((col (current-column))
  15774. (marker (or (get-text-property (point) 'org-marker)
  15775. (org-agenda-error)))
  15776. (buffer (marker-buffer marker))
  15777. (pos (marker-position marker))
  15778. (hdmarker (get-text-property (point) 'org-hd-marker))
  15779. (buffer-read-only nil)
  15780. newhead)
  15781. (org-with-remote-undo buffer
  15782. (with-current-buffer buffer
  15783. (widen)
  15784. (goto-char pos)
  15785. (org-show-context 'agenda)
  15786. (save-excursion
  15787. (and (outline-next-heading)
  15788. (org-flag-heading nil))) ; show the next heading
  15789. (org-todo arg)
  15790. (and (bolp) (forward-char 1))
  15791. (setq newhead (org-get-heading))
  15792. (save-excursion
  15793. (org-back-to-heading)
  15794. (move-marker org-last-heading-marker (point))))
  15795. (beginning-of-line 1)
  15796. (save-excursion
  15797. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  15798. (move-to-column col))))
  15799. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  15800. "Change all lines in the agenda buffer which match HDMARKER.
  15801. The new content of the line will be NEWHEAD (as modified by
  15802. `org-format-agenda-item'). HDMARKER is checked with
  15803. `equal' against all `org-hd-marker' text properties in the file.
  15804. If FIXFACE is non-nil, the face of each item is modified acording to
  15805. the new TODO state."
  15806. (let* ((buffer-read-only nil)
  15807. props m pl undone-face done-face finish new dotime cat tags)
  15808. (save-excursion
  15809. (goto-char (point-max))
  15810. (beginning-of-line 1)
  15811. (while (not finish)
  15812. (setq finish (bobp))
  15813. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  15814. (equal m hdmarker))
  15815. (setq props (text-properties-at (point))
  15816. dotime (get-text-property (point) 'dotime)
  15817. cat (get-text-property (point) 'org-category)
  15818. tags (get-text-property (point) 'tags)
  15819. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  15820. pl (get-text-property (point) 'prefix-length)
  15821. undone-face (get-text-property (point) 'undone-face)
  15822. done-face (get-text-property (point) 'done-face))
  15823. (move-to-column pl)
  15824. (cond
  15825. ((equal new "")
  15826. (beginning-of-line 1)
  15827. (and (looking-at ".*\n?") (replace-match "")))
  15828. ((looking-at ".*")
  15829. (replace-match new t t)
  15830. (beginning-of-line 1)
  15831. (add-text-properties (point-at-bol) (point-at-eol) props)
  15832. (when fixface
  15833. (add-text-properties
  15834. (point-at-bol) (point-at-eol)
  15835. (list 'face
  15836. (if org-last-todo-state-is-todo
  15837. undone-face done-face))))
  15838. (org-agenda-highlight-todo 'line)
  15839. (beginning-of-line 1))
  15840. (t (error "Line update did not work"))))
  15841. (beginning-of-line 0)))
  15842. (org-finalize-agenda)))
  15843. ;; FIXME: allow negative value for org-agenda-align-tags-to-column
  15844. ;; See the code in set-tags for the way to do this.
  15845. (defun org-agenda-align-tags (&optional line)
  15846. "Align all tags in agenda items to `org-agenda-align-tags-to-column'."
  15847. (let ((buffer-read-only))
  15848. (save-excursion
  15849. (goto-char (if line (point-at-bol) (point-min)))
  15850. (while (re-search-forward "\\([ \t]+\\):[a-zA-Z0-9_@:]+:[ \t]*$"
  15851. (if line (point-at-eol) nil) t)
  15852. (delete-region (match-beginning 1) (match-end 1))
  15853. (goto-char (match-beginning 1))
  15854. (insert (org-add-props
  15855. (make-string (max 1 (- org-agenda-align-tags-to-column
  15856. (current-column))) ?\ )
  15857. (text-properties-at (point))))))))
  15858. (defun org-agenda-priority-up ()
  15859. "Increase the priority of line at point, also in Org-mode file."
  15860. (interactive)
  15861. (org-agenda-priority 'up))
  15862. (defun org-agenda-priority-down ()
  15863. "Decrease the priority of line at point, also in Org-mode file."
  15864. (interactive)
  15865. (org-agenda-priority 'down))
  15866. (defun org-agenda-priority (&optional force-direction)
  15867. "Set the priority of line at point, also in Org-mode file.
  15868. This changes the line at point, all other lines in the agenda referring to
  15869. the same tree node, and the headline of the tree node in the Org-mode file."
  15870. (interactive)
  15871. (org-agenda-check-no-diary)
  15872. (let* ((marker (or (get-text-property (point) 'org-marker)
  15873. (org-agenda-error)))
  15874. (buffer (marker-buffer marker))
  15875. (pos (marker-position marker))
  15876. (hdmarker (get-text-property (point) 'org-hd-marker))
  15877. (buffer-read-only nil)
  15878. newhead)
  15879. (org-with-remote-undo buffer
  15880. (with-current-buffer buffer
  15881. (widen)
  15882. (goto-char pos)
  15883. (org-show-context 'agenda)
  15884. (save-excursion
  15885. (and (outline-next-heading)
  15886. (org-flag-heading nil))) ; show the next heading
  15887. (funcall 'org-priority force-direction)
  15888. (end-of-line 1)
  15889. (setq newhead (org-get-heading)))
  15890. (org-agenda-change-all-lines newhead hdmarker)
  15891. (beginning-of-line 1))))
  15892. (defun org-get-tags-at (&optional pos)
  15893. "Get a list of all headline tags applicable at POS.
  15894. POS defaults to point. If tags are inherited, the list contains
  15895. the targets in the same sequence as the headlines appear, i.e.
  15896. the tags of the current headline come last."
  15897. (interactive)
  15898. (let (tags)
  15899. (save-excursion
  15900. (save-restriction
  15901. (widen)
  15902. (goto-char (or pos (point)))
  15903. (save-match-data
  15904. (org-back-to-heading t)
  15905. (condition-case nil
  15906. (while t
  15907. (if (looking-at "[^\r\n]+?:\\([a-zA-Z0-9_@:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
  15908. (setq tags (append (org-split-string
  15909. (org-match-string-no-properties 1) ":")
  15910. tags)))
  15911. (or org-use-tag-inheritance (error ""))
  15912. (org-up-heading-all 1))
  15913. (error nil))))
  15914. tags)))
  15915. ;; FIXME: should fix the tags property of the agenda line.
  15916. (defun org-agenda-set-tags ()
  15917. "Set tags for the current headline."
  15918. (interactive)
  15919. (org-agenda-check-no-diary)
  15920. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  15921. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  15922. (org-agenda-error)))
  15923. (buffer (marker-buffer hdmarker))
  15924. (pos (marker-position hdmarker))
  15925. (buffer-read-only nil)
  15926. newhead)
  15927. (org-with-remote-undo buffer
  15928. (with-current-buffer buffer
  15929. (widen)
  15930. (goto-char pos)
  15931. (save-excursion
  15932. (org-show-context 'agenda))
  15933. (save-excursion
  15934. (and (outline-next-heading)
  15935. (org-flag-heading nil))) ; show the next heading
  15936. (goto-char pos)
  15937. (call-interactively 'org-set-tags)
  15938. (end-of-line 1)
  15939. (setq newhead (org-get-heading)))
  15940. (org-agenda-change-all-lines newhead hdmarker)
  15941. (beginning-of-line 1))))
  15942. (defun org-agenda-toggle-archive-tag ()
  15943. "Toggle the archive tag for the current entry."
  15944. (interactive)
  15945. (org-agenda-check-no-diary)
  15946. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  15947. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  15948. (org-agenda-error)))
  15949. (buffer (marker-buffer hdmarker))
  15950. (pos (marker-position hdmarker))
  15951. (buffer-read-only nil)
  15952. newhead)
  15953. (org-with-remote-undo buffer
  15954. (with-current-buffer buffer
  15955. (widen)
  15956. (goto-char pos)
  15957. (org-show-context 'agenda)
  15958. (save-excursion
  15959. (and (outline-next-heading)
  15960. (org-flag-heading nil))) ; show the next heading
  15961. (call-interactively 'org-toggle-archive-tag)
  15962. (end-of-line 1)
  15963. (setq newhead (org-get-heading)))
  15964. (org-agenda-change-all-lines newhead hdmarker)
  15965. (beginning-of-line 1))))
  15966. (defun org-agenda-date-later (arg &optional what)
  15967. "Change the date of this item to one day later."
  15968. (interactive "p")
  15969. (org-agenda-check-type t 'agenda 'timeline)
  15970. (org-agenda-check-no-diary)
  15971. (let* ((marker (or (get-text-property (point) 'org-marker)
  15972. (org-agenda-error)))
  15973. (buffer (marker-buffer marker))
  15974. (pos (marker-position marker)))
  15975. (org-with-remote-undo buffer
  15976. (with-current-buffer buffer
  15977. (widen)
  15978. (goto-char pos)
  15979. (if (not (org-at-timestamp-p))
  15980. (error "Cannot find time stamp"))
  15981. (org-timestamp-change arg (or what 'day)))
  15982. (org-agenda-show-new-time marker org-last-changed-timestamp))
  15983. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  15984. (defun org-agenda-date-earlier (arg &optional what)
  15985. "Change the date of this item to one day earlier."
  15986. (interactive "p")
  15987. (org-agenda-date-later (- arg) what))
  15988. (defun org-agenda-show-new-time (marker stamp)
  15989. "Show new date stamp via text properties."
  15990. ;; We use text properties to make this undoable
  15991. (let ((buffer-read-only nil))
  15992. (setq stamp (concat " => " stamp))
  15993. (save-excursion
  15994. (goto-char (point-max))
  15995. (while (not (bobp))
  15996. (when (equal marker (get-text-property (point) 'org-marker))
  15997. (move-to-column (- (window-width) (length stamp)) t)
  15998. (if (featurep 'xemacs)
  15999. ;; Use `duplicable' property to trigger undo recording
  16000. (let ((ex (make-extent nil nil))
  16001. (gl (make-glyph stamp)))
  16002. (set-glyph-face gl 'secondary-selection)
  16003. (set-extent-properties
  16004. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  16005. (insert-extent ex (1- (point)) (point-at-eol)))
  16006. (add-text-properties
  16007. (1- (point)) (point-at-eol)
  16008. (list 'display (org-add-props stamp nil
  16009. 'face 'secondary-selection))))
  16010. (beginning-of-line 1))
  16011. (beginning-of-line 0)))))
  16012. (defun org-agenda-date-prompt (arg)
  16013. "Change the date of this item. Date is prompted for, with default today.
  16014. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  16015. be used to request time specification in the time stamp."
  16016. (interactive "P")
  16017. (org-agenda-check-type t 'agenda 'timeline)
  16018. (org-agenda-check-no-diary)
  16019. (let* ((marker (or (get-text-property (point) 'org-marker)
  16020. (org-agenda-error)))
  16021. (buffer (marker-buffer marker))
  16022. (pos (marker-position marker)))
  16023. (org-with-remote-undo buffer
  16024. (with-current-buffer buffer
  16025. (widen)
  16026. (goto-char pos)
  16027. (if (not (org-at-timestamp-p))
  16028. (error "Cannot find time stamp"))
  16029. (org-time-stamp arg)
  16030. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  16031. (defun org-agenda-schedule (arg)
  16032. "Schedule the item at point."
  16033. (interactive "P")
  16034. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  16035. (org-agenda-check-no-diary)
  16036. (let* ((marker (or (get-text-property (point) 'org-marker)
  16037. (org-agenda-error)))
  16038. (buffer (marker-buffer marker))
  16039. (pos (marker-position marker))
  16040. (org-insert-labeled-timestamps-at-point nil)
  16041. ts)
  16042. (org-with-remote-undo buffer
  16043. (with-current-buffer buffer
  16044. (widen)
  16045. (goto-char pos)
  16046. (setq ts (org-schedule))
  16047. (message "Item scheduled for %s" ts)))))
  16048. (defun org-agenda-deadline (arg)
  16049. "Schedule the item at point."
  16050. (interactive "P")
  16051. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  16052. (org-agenda-check-no-diary)
  16053. (let* ((marker (or (get-text-property (point) 'org-marker)
  16054. (org-agenda-error)))
  16055. (buffer (marker-buffer marker))
  16056. (pos (marker-position marker))
  16057. (org-insert-labeled-timestamps-at-point nil)
  16058. ts)
  16059. (org-with-remote-undo buffer
  16060. (with-current-buffer buffer
  16061. (widen)
  16062. (goto-char pos)
  16063. (setq ts (org-deadline))
  16064. (message "Deadline for this item set to %s" ts)))))
  16065. (defun org-get-heading ()
  16066. "Return the heading of the current entry, without the stars."
  16067. (save-excursion
  16068. (org-back-to-heading t)
  16069. (if (looking-at "\\*+[ \t]+\\([^\r\n]*\\)") (match-string 1) "")))
  16070. (defun org-agenda-clock-in (&optional arg)
  16071. "Start the clock on the currently selected item."
  16072. (interactive "P")
  16073. (org-agenda-check-no-diary)
  16074. (let* ((marker (or (get-text-property (point) 'org-marker)
  16075. (org-agenda-error)))
  16076. (pos (marker-position marker)))
  16077. (org-with-remote-undo (marker-buffer marker)
  16078. (with-current-buffer (marker-buffer marker)
  16079. (widen)
  16080. (goto-char pos)
  16081. (org-clock-in)))))
  16082. (defun org-agenda-clock-out (&optional arg)
  16083. "Stop the currently running clock."
  16084. (interactive "P")
  16085. (unless (marker-buffer org-clock-marker)
  16086. (error "No running clock"))
  16087. (org-with-remote-undo (marker-buffer org-clock-marker)
  16088. (org-clock-out)))
  16089. (defun org-agenda-clock-cancel (&optional arg)
  16090. "Cancel the currently running clock."
  16091. (interactive "P")
  16092. (unless (marker-buffer org-clock-marker)
  16093. (error "No running clock"))
  16094. (org-with-remote-undo (marker-buffer org-clock-marker)
  16095. (org-clock-cancel)))
  16096. (defun org-agenda-diary-entry ()
  16097. "Make a diary entry, like the `i' command from the calendar.
  16098. All the standard commands work: block, weekly etc."
  16099. (interactive)
  16100. (org-agenda-check-type t 'agenda 'timeline)
  16101. (require 'diary-lib)
  16102. (let* ((char (progn
  16103. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  16104. (read-char-exclusive)))
  16105. (cmd (cdr (assoc char
  16106. '((?d . insert-diary-entry)
  16107. (?w . insert-weekly-diary-entry)
  16108. (?m . insert-monthly-diary-entry)
  16109. (?y . insert-yearly-diary-entry)
  16110. (?a . insert-anniversary-diary-entry)
  16111. (?b . insert-block-diary-entry)
  16112. (?c . insert-cyclic-diary-entry)))))
  16113. (oldf (symbol-function 'calendar-cursor-to-date))
  16114. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  16115. (point (point))
  16116. (mark (or (mark t) (point))))
  16117. (unless cmd
  16118. (error "No command associated with <%c>" char))
  16119. (unless (and (get-text-property point 'day)
  16120. (or (not (equal ?b char))
  16121. (get-text-property mark 'day)))
  16122. (error "Don't know which date to use for diary entry"))
  16123. ;; We implement this by hacking the `calendar-cursor-to-date' function
  16124. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  16125. (let ((calendar-mark-ring
  16126. (list (calendar-gregorian-from-absolute
  16127. (or (get-text-property mark 'day)
  16128. (get-text-property point 'day))))))
  16129. (unwind-protect
  16130. (progn
  16131. (fset 'calendar-cursor-to-date
  16132. (lambda (&optional error)
  16133. (calendar-gregorian-from-absolute
  16134. (get-text-property point 'day))))
  16135. (call-interactively cmd))
  16136. (fset 'calendar-cursor-to-date oldf)))))
  16137. (defun org-agenda-execute-calendar-command (cmd)
  16138. "Execute a calendar command from the agenda, with the date associated to
  16139. the cursor position."
  16140. (org-agenda-check-type t 'agenda 'timeline)
  16141. (require 'diary-lib)
  16142. (unless (get-text-property (point) 'day)
  16143. (error "Don't know which date to use for calendar command"))
  16144. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  16145. (point (point))
  16146. (date (calendar-gregorian-from-absolute
  16147. (get-text-property point 'day)))
  16148. ;; the following 3 vars are needed in the calendar
  16149. (displayed-day (extract-calendar-day date))
  16150. (displayed-month (extract-calendar-month date))
  16151. (displayed-year (extract-calendar-year date)))
  16152. (unwind-protect
  16153. (progn
  16154. (fset 'calendar-cursor-to-date
  16155. (lambda (&optional error)
  16156. (calendar-gregorian-from-absolute
  16157. (get-text-property point 'day))))
  16158. (call-interactively cmd))
  16159. (fset 'calendar-cursor-to-date oldf))))
  16160. (defun org-agenda-phases-of-moon ()
  16161. "Display the phases of the moon for the 3 months around the cursor date."
  16162. (interactive)
  16163. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  16164. (defun org-agenda-holidays ()
  16165. "Display the holidays for the 3 months around the cursor date."
  16166. (interactive)
  16167. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  16168. (defun org-agenda-sunrise-sunset (arg)
  16169. "Display sunrise and sunset for the cursor date.
  16170. Latitude and longitude can be specified with the variables
  16171. `calendar-latitude' and `calendar-longitude'. When called with prefix
  16172. argument, latitude and longitude will be prompted for."
  16173. (interactive "P")
  16174. (let ((calendar-longitude (if arg nil calendar-longitude))
  16175. (calendar-latitude (if arg nil calendar-latitude))
  16176. (calendar-location-name
  16177. (if arg "the given coordinates" calendar-location-name)))
  16178. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  16179. (defun org-agenda-goto-calendar ()
  16180. "Open the Emacs calendar with the date at the cursor."
  16181. (interactive)
  16182. (org-agenda-check-type t 'agenda 'timeline)
  16183. (let* ((day (or (get-text-property (point) 'day)
  16184. (error "Don't know which date to open in calendar")))
  16185. (date (calendar-gregorian-from-absolute day))
  16186. (calendar-move-hook nil)
  16187. (view-calendar-holidays-initially nil)
  16188. (view-diary-entries-initially nil))
  16189. (calendar)
  16190. (calendar-goto-date date)))
  16191. (defun org-calendar-goto-agenda ()
  16192. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  16193. This is a command that has to be installed in `calendar-mode-map'."
  16194. (interactive)
  16195. (org-agenda-list nil (calendar-absolute-from-gregorian
  16196. (calendar-cursor-to-date))
  16197. nil))
  16198. (defun org-agenda-convert-date ()
  16199. (interactive)
  16200. (org-agenda-check-type t 'agenda 'timeline)
  16201. (let ((day (get-text-property (point) 'day))
  16202. date s)
  16203. (unless day
  16204. (error "Don't know which date to convert"))
  16205. (setq date (calendar-gregorian-from-absolute day))
  16206. (setq s (concat
  16207. "Gregorian: " (calendar-date-string date) "\n"
  16208. "ISO: " (calendar-iso-date-string date) "\n"
  16209. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  16210. "Julian: " (calendar-julian-date-string date) "\n"
  16211. "Astron. JD: " (calendar-astro-date-string date)
  16212. " (Julian date number at noon UTC)\n"
  16213. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  16214. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  16215. "French: " (calendar-french-date-string date) "\n"
  16216. "Mayan: " (calendar-mayan-date-string date) "\n"
  16217. "Coptic: " (calendar-coptic-date-string date) "\n"
  16218. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  16219. "Persian: " (calendar-persian-date-string date) "\n"
  16220. "Chinese: " (calendar-chinese-date-string date) "\n"))
  16221. (with-output-to-temp-buffer "*Dates*"
  16222. (princ s))
  16223. (if (fboundp 'fit-window-to-buffer)
  16224. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  16225. ;;;; Embedded LaTeX
  16226. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16227. "Keymap for the minor `org-cdlatex-mode'.")
  16228. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16229. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16230. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16231. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16232. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16233. (defvar org-cdlatex-texmathp-advice-is-done nil
  16234. "Flag remembering if we have applied the advice to texmathp already.")
  16235. (define-minor-mode org-cdlatex-mode
  16236. "Toggle the minor `org-cdlatex-mode'.
  16237. This mode supports entering LaTeX environment and math in LaTeX fragments
  16238. in Org-mode.
  16239. \\{org-cdlatex-mode-map}"
  16240. nil " OCDL" nil
  16241. (when org-cdlatex-mode (require 'cdlatex))
  16242. (unless org-cdlatex-texmathp-advice-is-done
  16243. (setq org-cdlatex-texmathp-advice-is-done t)
  16244. (defadvice texmathp (around org-math-always-on activate)
  16245. "Always return t in org-mode buffers.
  16246. This is because we want to insert math symbols without dollars even outside
  16247. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16248. en embedded LaTeX fragement, let texmathp do its job.
  16249. \\[org-cdlatex-mode-map]"
  16250. (interactive)
  16251. (let (p)
  16252. (cond
  16253. ((not (org-mode-p)) ad-do-it)
  16254. ((eq this-command 'cdlatex-math-symbol)
  16255. (setq ad-return-value t
  16256. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16257. (t
  16258. (let ((p (org-inside-LaTeX-fragment-p)))
  16259. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16260. (setq ad-return-value t
  16261. texmathp-why '("Org-mode embedded math" . 0))
  16262. (if p ad-do-it)))))))))
  16263. (defun turn-on-org-cdlatex ()
  16264. "Unconditionally turn on `org-cdlatex-mode'."
  16265. (org-cdlatex-mode 1))
  16266. (defun org-inside-LaTeX-fragment-p ()
  16267. "Test if point is inside a LaTeX fragment.
  16268. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16269. sequence appearing also before point.
  16270. Even though the matchers for math are configurable, this function assumes
  16271. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16272. delimiters are skipped when they have been removed by customization.
  16273. The return value is nil, or a cons cell with the delimiter and
  16274. and the position of this delimiter.
  16275. This function does a reasonably good job, but can locally be fooled by
  16276. for example currency specifications. For example it will assume being in
  16277. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16278. fragments that are properly closed, but during editing, we have to live
  16279. with the uncertainty caused by missing closing delimiters. This function
  16280. looks only before point, not after."
  16281. (catch 'exit
  16282. (let ((pos (point))
  16283. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16284. (lim (progn
  16285. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16286. (point)))
  16287. dd-on str (start 0) m re)
  16288. (goto-char pos)
  16289. (when dodollar
  16290. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16291. re (nth 1 (assoc "$" org-latex-regexps)))
  16292. (while (string-match re str start)
  16293. (cond
  16294. ((= (match-end 0) (length str))
  16295. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16296. ((= (match-end 0) (- (length str) 5))
  16297. (throw 'exit nil))
  16298. (t (setq start (match-end 0))))))
  16299. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16300. (goto-char pos)
  16301. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16302. (and (match-beginning 2) (throw 'exit nil))
  16303. ;; count $$
  16304. (while (re-search-backward "\\$\\$" lim t)
  16305. (setq dd-on (not dd-on)))
  16306. (goto-char pos)
  16307. (if dd-on (cons "$$" m))))))
  16308. (defun org-try-cdlatex-tab ()
  16309. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16310. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16311. - inside a LaTeX fragment, or
  16312. - after the first word in a line, where an abbreviation expansion could
  16313. insert a LaTeX environment."
  16314. (when org-cdlatex-mode
  16315. (cond
  16316. ((save-excursion
  16317. (skip-chars-backward "a-zA-Z0-9*")
  16318. (skip-chars-backward " \t")
  16319. (bolp))
  16320. (cdlatex-tab) t)
  16321. ((org-inside-LaTeX-fragment-p)
  16322. (cdlatex-tab) t)
  16323. (t nil))))
  16324. (defun org-cdlatex-underscore-caret (&optional arg)
  16325. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16326. Revert to the normal definition outside of these fragments."
  16327. (interactive "P")
  16328. (if (org-inside-LaTeX-fragment-p)
  16329. (call-interactively 'cdlatex-sub-superscript)
  16330. (let (org-cdlatex-mode)
  16331. (call-interactively (key-binding (vector last-input-event))))))
  16332. (defun org-cdlatex-math-modify (&optional arg)
  16333. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16334. Revert to the normal definition outside of these fragments."
  16335. (interactive "P")
  16336. (if (org-inside-LaTeX-fragment-p)
  16337. (call-interactively 'cdlatex-math-modify)
  16338. (let (org-cdlatex-mode)
  16339. (call-interactively (key-binding (vector last-input-event))))))
  16340. (defvar org-latex-fragment-image-overlays nil
  16341. "List of overlays carrying the images of latex fragments.")
  16342. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16343. (defun org-remove-latex-fragment-image-overlays ()
  16344. "Remove all overlays with LaTeX fragment images in current buffer."
  16345. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  16346. (setq org-latex-fragment-image-overlays nil))
  16347. (defun org-preview-latex-fragment (&optional subtree)
  16348. "Preview the LaTeX fragment at point, or all locally or globally.
  16349. If the cursor is in a LaTeX fragment, create the image and overlay
  16350. it over the source code. If there is no fragment at point, display
  16351. all fragments in the current text, from one headline to the next. With
  16352. prefix SUBTREE, display all fragments in the current subtree. With a
  16353. double prefix `C-u C-u', or when the cursor is before the first headline,
  16354. display all fragments in the buffer.
  16355. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16356. (interactive "P")
  16357. (org-remove-latex-fragment-image-overlays)
  16358. (save-excursion
  16359. (save-restriction
  16360. (let (beg end at msg)
  16361. (cond
  16362. ((or (equal subtree '(16))
  16363. (not (save-excursion
  16364. (re-search-backward (concat "^" outline-regexp) nil t))))
  16365. (setq beg (point-min) end (point-max)
  16366. msg "Creating images for buffer...%s"))
  16367. ((equal subtree '(4))
  16368. (org-back-to-heading)
  16369. (setq beg (point) end (org-end-of-subtree t)
  16370. msg "Creating images for subtree...%s"))
  16371. (t
  16372. (if (setq at (org-inside-LaTeX-fragment-p))
  16373. (goto-char (max (point-min) (- (cdr at) 2)))
  16374. (org-back-to-heading))
  16375. (setq beg (point) end (progn (outline-next-heading) (point))
  16376. msg (if at "Creating image...%s"
  16377. "Creating images for entry...%s"))))
  16378. (message msg "")
  16379. (narrow-to-region beg end)
  16380. (goto-char beg)
  16381. (org-format-latex
  16382. (concat "ltxpng/" (file-name-sans-extension
  16383. (file-name-nondirectory
  16384. buffer-file-name)))
  16385. default-directory 'overlays msg at 'forbuffer)
  16386. (message msg "done. Use `C-c C-c' to remove images.")))))
  16387. (defvar org-latex-regexps
  16388. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16389. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16390. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16391. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  16392. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16393. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  16394. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  16395. "Regular expressions for matching embedded LaTeX.")
  16396. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  16397. "Replace LaTeX fragments with links to an image, and produce images."
  16398. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16399. (let* ((prefixnodir (file-name-nondirectory prefix))
  16400. (absprefix (expand-file-name prefix dir))
  16401. (todir (file-name-directory absprefix))
  16402. (opt org-format-latex-options)
  16403. (matchers (plist-get opt :matchers))
  16404. (re-list org-latex-regexps)
  16405. (cnt 0) txt link beg end re e checkdir
  16406. m n block linkfile movefile ov)
  16407. ;; Check if there are old images files with this prefix, and remove them
  16408. (when (file-directory-p todir)
  16409. (mapc 'delete-file
  16410. (directory-files
  16411. todir 'full
  16412. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  16413. ;; Check the different regular expressions
  16414. (while (setq e (pop re-list))
  16415. (setq m (car e) re (nth 1 e) n (nth 2 e)
  16416. block (if (nth 3 e) "\n\n" ""))
  16417. (when (member m matchers)
  16418. (goto-char (point-min))
  16419. (while (re-search-forward re nil t)
  16420. (when (or (not at) (equal (cdr at) (match-beginning n)))
  16421. (setq txt (match-string n)
  16422. beg (match-beginning n) end (match-end n)
  16423. cnt (1+ cnt)
  16424. linkfile (format "%s_%04d.png" prefix cnt)
  16425. movefile (format "%s_%04d.png" absprefix cnt)
  16426. link (concat block "[[file:" linkfile "]]" block))
  16427. (if msg (message msg cnt))
  16428. (goto-char beg)
  16429. (unless checkdir ; make sure the directory exists
  16430. (setq checkdir t)
  16431. (or (file-directory-p todir) (make-directory todir)))
  16432. (org-create-formula-image
  16433. txt movefile opt forbuffer)
  16434. (if overlays
  16435. (progn
  16436. (setq ov (org-make-overlay beg end))
  16437. (if (featurep 'xemacs)
  16438. (progn
  16439. (org-overlay-put ov 'invisible t)
  16440. (org-overlay-put
  16441. ov 'end-glyph
  16442. (make-glyph (vector 'png :file movefile))))
  16443. (org-overlay-put
  16444. ov 'display
  16445. (list 'image :type 'png :file movefile :ascent 'center)))
  16446. (push ov org-latex-fragment-image-overlays)
  16447. (goto-char end))
  16448. (delete-region beg end)
  16449. (insert link))))))))
  16450. ;; This function borrows from Ganesh Swami's latex2png.el
  16451. (defun org-create-formula-image (string tofile options buffer)
  16452. (let* ((tmpdir (if (featurep 'xemacs)
  16453. (temp-directory)
  16454. temporary-file-directory))
  16455. (texfilebase (make-temp-name
  16456. (expand-file-name "orgtex" tmpdir)))
  16457. (texfile (concat texfilebase ".tex"))
  16458. (dvifile (concat texfilebase ".dvi"))
  16459. (pngfile (concat texfilebase ".png"))
  16460. (fnh (face-attribute 'default :height nil))
  16461. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16462. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16463. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16464. "Black"))
  16465. (bg (or (plist-get options (if buffer :background :html-background))
  16466. "Transparent")))
  16467. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  16468. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  16469. (with-temp-file texfile
  16470. (insert org-format-latex-header
  16471. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  16472. (let ((dir default-directory))
  16473. (condition-case nil
  16474. (progn
  16475. (cd tmpdir)
  16476. (call-process "latex" nil nil nil texfile))
  16477. (error nil))
  16478. (cd dir))
  16479. (if (not (file-exists-p dvifile))
  16480. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16481. (call-process "dvipng" nil nil nil
  16482. "-E" "-fg" fg "-bg" bg
  16483. "-D" dpi
  16484. ;;"-x" scale "-y" scale
  16485. "-T" "tight"
  16486. "-o" pngfile
  16487. dvifile)
  16488. (if (not (file-exists-p pngfile))
  16489. (progn (message "Failed to create png file from %s" texfile) nil)
  16490. ;; Use the requested file name and clean up
  16491. (copy-file pngfile tofile 'replace)
  16492. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  16493. (delete-file (concat texfilebase e)))
  16494. pngfile))))
  16495. (defun org-dvipng-color (attr)
  16496. "Return an rgb color specification for dvipng."
  16497. (apply 'format "rgb %s %s %s"
  16498. (mapcar 'org-normalize-color
  16499. (color-values (face-attribute 'default attr nil)))))
  16500. (defun org-normalize-color (value)
  16501. "Return string to be used as color value for an RGB component."
  16502. (format "%g" (/ value 65535.0)))
  16503. ;;;; Exporting
  16504. ;;; Variables, constants, and parameter plists
  16505. (defconst org-level-max 20)
  16506. (defvar org-export-html-preamble nil
  16507. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  16508. (defvar org-export-html-postamble nil
  16509. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  16510. (defvar org-export-html-auto-preamble t
  16511. "Should default preamble be inserted? Set by publishing functions.")
  16512. (defvar org-export-html-auto-postamble t
  16513. "Should default postamble be inserted? Set by publishing functions.")
  16514. (defvar org-current-export-file nil) ; dynamically scoped parameter
  16515. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  16516. (defconst org-export-plist-vars
  16517. '((:language . org-export-default-language)
  16518. (:customtime . org-display-custom-times)
  16519. (:headline-levels . org-export-headline-levels)
  16520. (:section-numbers . org-export-with-section-numbers)
  16521. (:table-of-contents . org-export-with-toc)
  16522. (:preserve-breaks . org-export-preserve-breaks)
  16523. (:archived-trees . org-export-with-archived-trees)
  16524. (:emphasize . org-export-with-emphasize)
  16525. (:sub-superscript . org-export-with-sub-superscripts)
  16526. (:TeX-macros . org-export-with-TeX-macros)
  16527. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  16528. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  16529. (:fixed-width . org-export-with-fixed-width)
  16530. (:timestamps . org-export-with-timestamps)
  16531. (:tables . org-export-with-tables)
  16532. (:table-auto-headline . org-export-highlight-first-table-line)
  16533. (:style . org-export-html-style)
  16534. (:agenda-style . org-agenda-export-html-style) ;; FIXME: Does this work????
  16535. (:convert-org-links . org-export-html-link-org-files-as-html)
  16536. (:inline-images . org-export-html-inline-images)
  16537. (:expand-quoted-html . org-export-html-expand)
  16538. (:timestamp . org-export-html-with-timestamp)
  16539. (:publishing-directory . org-export-publishing-directory)
  16540. (:preamble . org-export-html-preamble)
  16541. (:postamble . org-export-html-postamble)
  16542. (:auto-preamble . org-export-html-auto-preamble)
  16543. (:auto-postamble . org-export-html-auto-postamble)
  16544. (:author . user-full-name)
  16545. (:email . user-mail-address)))
  16546. (defun org-default-export-plist ()
  16547. "Return the property list with default settings for the export variables."
  16548. (let ((l org-export-plist-vars) rtn e)
  16549. (while (setq e (pop l))
  16550. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  16551. rtn))
  16552. (defun org-infile-export-plist ()
  16553. "Return the property list with file-local settings for export."
  16554. (save-excursion
  16555. (goto-char 0)
  16556. (let ((re (org-make-options-regexp
  16557. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  16558. p key val text options)
  16559. (while (re-search-forward re nil t)
  16560. (setq key (org-match-string-no-properties 1)
  16561. val (org-match-string-no-properties 2))
  16562. (cond
  16563. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  16564. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  16565. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  16566. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  16567. ((string-equal key "TEXT")
  16568. (setq text (if text (concat text "\n" val) val)))
  16569. ((string-equal key "OPTIONS") (setq options val))))
  16570. (setq p (plist-put p :text text))
  16571. (when options
  16572. (let ((op '(("H" . :headline-levels)
  16573. ("num" . :section-numbers)
  16574. ("toc" . :table-of-contents)
  16575. ("\\n" . :preserve-breaks)
  16576. ("@" . :expand-quoted-html)
  16577. (":" . :fixed-width)
  16578. ("|" . :tables)
  16579. ("^" . :sub-superscript)
  16580. ("*" . :emphasize)
  16581. ("TeX" . :TeX-macros)
  16582. ("LaTeX" . :LaTeX-fragments)
  16583. ("skip" . :skip-before-1st-heading)))
  16584. o)
  16585. (while (setq o (pop op))
  16586. (if (string-match (concat (regexp-quote (car o))
  16587. ":\\([^ \t\n\r;,.]*\\)")
  16588. options)
  16589. (setq p (plist-put p (cdr o)
  16590. (car (read-from-string
  16591. (match-string 1 options)))))))))
  16592. p)))
  16593. (defun org-export-directory (type plist)
  16594. (let* ((val (plist-get plist :publishing-directory))
  16595. (dir (if (listp val)
  16596. (or (cdr (assoc type val)) ".")
  16597. val)))
  16598. dir))
  16599. (defun org-skip-comments (lines)
  16600. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  16601. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  16602. (re2 "^\\(\\*+\\)[ \t\n\r]")
  16603. (case-fold-search nil)
  16604. rtn line level)
  16605. (while (setq line (pop lines))
  16606. (cond
  16607. ((and (string-match re1 line)
  16608. (setq level (- (match-end 1) (match-beginning 1))))
  16609. ;; Beginning of a COMMENT subtree. Skip it.
  16610. (while (and (setq line (pop lines))
  16611. (or (not (string-match re2 line))
  16612. (> (- (match-end 1) (match-beginning 1)) level))))
  16613. (setq lines (cons line lines)))
  16614. ((string-match "^#" line)
  16615. ;; an ordinary comment line
  16616. )
  16617. ((and org-export-table-remove-special-lines
  16618. (string-match "^[ \t]*|" line)
  16619. (or (string-match "^[ \t]*| *[!_^] *|" line)
  16620. (and (string-match "| *<[0-9]+> *|" line)
  16621. (not (string-match "| *[^ <|]" line)))))
  16622. ;; a special table line that should be removed
  16623. )
  16624. (t (setq rtn (cons line rtn)))))
  16625. (nreverse rtn)))
  16626. (defun org-export (&optional arg)
  16627. (interactive)
  16628. (let ((help "[t] insert the export option template
  16629. \[v] limit export to visible part of outline tree
  16630. \[a] export as ASCII
  16631. \[h] export as HTML
  16632. \[H] export as HTML to temporary buffer
  16633. \[b] export as HTML and browse immediately
  16634. \[x] export as XOXO
  16635. \[i] export current file as iCalendar file
  16636. \[I] export all agenda files as iCalendar files
  16637. \[c] export agenda files into combined iCalendar file
  16638. \[F] publish current file
  16639. \[P] publish current project
  16640. \[X] publish... (project will be prompted for)
  16641. \[A] publish all projects")
  16642. (cmds
  16643. '((?t . org-insert-export-options-template)
  16644. (?v . org-export-visible)
  16645. (?a . org-export-as-ascii)
  16646. (?h . org-export-as-html)
  16647. (?b . org-export-as-html-and-open)
  16648. (?H . org-export-as-html-to-buffer)
  16649. (?R . org-export-region-as-html)
  16650. (?x . org-export-as-xoxo)
  16651. (?i . org-export-icalendar-this-file)
  16652. (?I . org-export-icalendar-all-agenda-files)
  16653. (?c . org-export-icalendar-combine-agenda-files)
  16654. (?F . org-publish-current-file)
  16655. (?P . org-publish-current-project)
  16656. (?X . org-publish)
  16657. (?A . org-publish-all)))
  16658. r1 r2 ass)
  16659. (save-window-excursion
  16660. (delete-other-windows)
  16661. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  16662. (princ help))
  16663. (message "Select command: ")
  16664. (setq r1 (read-char-exclusive)))
  16665. (setq r2 (if (< r1 27) (+ r1 96) r1))
  16666. (if (setq ass (assq r2 cmds))
  16667. (call-interactively (cdr ass))
  16668. (error "No command associated with key %c" r1))))
  16669. (defconst org-html-entities
  16670. '(("nbsp")
  16671. ("iexcl")
  16672. ("cent")
  16673. ("pound")
  16674. ("curren")
  16675. ("yen")
  16676. ("brvbar")
  16677. ("vert" . "&#124;")
  16678. ("sect")
  16679. ("uml")
  16680. ("copy")
  16681. ("ordf")
  16682. ("laquo")
  16683. ("not")
  16684. ("shy")
  16685. ("reg")
  16686. ("macr")
  16687. ("deg")
  16688. ("plusmn")
  16689. ("sup2")
  16690. ("sup3")
  16691. ("acute")
  16692. ("micro")
  16693. ("para")
  16694. ("middot")
  16695. ("odot"."o")
  16696. ("star"."*")
  16697. ("cedil")
  16698. ("sup1")
  16699. ("ordm")
  16700. ("raquo")
  16701. ("frac14")
  16702. ("frac12")
  16703. ("frac34")
  16704. ("iquest")
  16705. ("Agrave")
  16706. ("Aacute")
  16707. ("Acirc")
  16708. ("Atilde")
  16709. ("Auml")
  16710. ("Aring") ("AA"."&Aring;")
  16711. ("AElig")
  16712. ("Ccedil")
  16713. ("Egrave")
  16714. ("Eacute")
  16715. ("Ecirc")
  16716. ("Euml")
  16717. ("Igrave")
  16718. ("Iacute")
  16719. ("Icirc")
  16720. ("Iuml")
  16721. ("ETH")
  16722. ("Ntilde")
  16723. ("Ograve")
  16724. ("Oacute")
  16725. ("Ocirc")
  16726. ("Otilde")
  16727. ("Ouml")
  16728. ("times")
  16729. ("Oslash")
  16730. ("Ugrave")
  16731. ("Uacute")
  16732. ("Ucirc")
  16733. ("Uuml")
  16734. ("Yacute")
  16735. ("THORN")
  16736. ("szlig")
  16737. ("agrave")
  16738. ("aacute")
  16739. ("acirc")
  16740. ("atilde")
  16741. ("auml")
  16742. ("aring")
  16743. ("aelig")
  16744. ("ccedil")
  16745. ("egrave")
  16746. ("eacute")
  16747. ("ecirc")
  16748. ("euml")
  16749. ("igrave")
  16750. ("iacute")
  16751. ("icirc")
  16752. ("iuml")
  16753. ("eth")
  16754. ("ntilde")
  16755. ("ograve")
  16756. ("oacute")
  16757. ("ocirc")
  16758. ("otilde")
  16759. ("ouml")
  16760. ("divide")
  16761. ("oslash")
  16762. ("ugrave")
  16763. ("uacute")
  16764. ("ucirc")
  16765. ("uuml")
  16766. ("yacute")
  16767. ("thorn")
  16768. ("yuml")
  16769. ("fnof")
  16770. ("Alpha")
  16771. ("Beta")
  16772. ("Gamma")
  16773. ("Delta")
  16774. ("Epsilon")
  16775. ("Zeta")
  16776. ("Eta")
  16777. ("Theta")
  16778. ("Iota")
  16779. ("Kappa")
  16780. ("Lambda")
  16781. ("Mu")
  16782. ("Nu")
  16783. ("Xi")
  16784. ("Omicron")
  16785. ("Pi")
  16786. ("Rho")
  16787. ("Sigma")
  16788. ("Tau")
  16789. ("Upsilon")
  16790. ("Phi")
  16791. ("Chi")
  16792. ("Psi")
  16793. ("Omega")
  16794. ("alpha")
  16795. ("beta")
  16796. ("gamma")
  16797. ("delta")
  16798. ("epsilon")
  16799. ("varepsilon"."&epsilon;")
  16800. ("zeta")
  16801. ("eta")
  16802. ("theta")
  16803. ("iota")
  16804. ("kappa")
  16805. ("lambda")
  16806. ("mu")
  16807. ("nu")
  16808. ("xi")
  16809. ("omicron")
  16810. ("pi")
  16811. ("rho")
  16812. ("sigmaf") ("varsigma"."&sigmaf;")
  16813. ("sigma")
  16814. ("tau")
  16815. ("upsilon")
  16816. ("phi")
  16817. ("chi")
  16818. ("psi")
  16819. ("omega")
  16820. ("thetasym") ("vartheta"."&thetasym;")
  16821. ("upsih")
  16822. ("piv")
  16823. ("bull") ("bullet"."&bull;")
  16824. ("hellip") ("dots"."&hellip;")
  16825. ("prime")
  16826. ("Prime")
  16827. ("oline")
  16828. ("frasl")
  16829. ("weierp")
  16830. ("image")
  16831. ("real")
  16832. ("trade")
  16833. ("alefsym")
  16834. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  16835. ("uarr") ("uparrow"."&uarr;")
  16836. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  16837. ("darr")("downarrow"."&darr;")
  16838. ("harr") ("leftrightarrow"."&harr;")
  16839. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  16840. ("lArr") ("Leftarrow"."&lArr;")
  16841. ("uArr") ("Uparrow"."&uArr;")
  16842. ("rArr") ("Rightarrow"."&rArr;")
  16843. ("dArr") ("Downarrow"."&dArr;")
  16844. ("hArr") ("Leftrightarrow"."&hArr;")
  16845. ("forall")
  16846. ("part") ("partial"."&part;")
  16847. ("exist") ("exists"."&exist;")
  16848. ("empty") ("emptyset"."&empty;")
  16849. ("nabla")
  16850. ("isin") ("in"."&isin;")
  16851. ("notin")
  16852. ("ni")
  16853. ("prod")
  16854. ("sum")
  16855. ("minus")
  16856. ("lowast") ("ast"."&lowast;")
  16857. ("radic")
  16858. ("prop") ("proptp"."&prop;")
  16859. ("infin") ("infty"."&infin;")
  16860. ("ang") ("angle"."&ang;")
  16861. ("and") ("vee"."&and;")
  16862. ("or") ("wedge"."&or;")
  16863. ("cap")
  16864. ("cup")
  16865. ("int")
  16866. ("there4")
  16867. ("sim")
  16868. ("cong") ("simeq"."&cong;")
  16869. ("asymp")("approx"."&asymp;")
  16870. ("ne") ("neq"."&ne;")
  16871. ("equiv")
  16872. ("le")
  16873. ("ge")
  16874. ("sub") ("subset"."&sub;")
  16875. ("sup") ("supset"."&sup;")
  16876. ("nsub")
  16877. ("sube")
  16878. ("supe")
  16879. ("oplus")
  16880. ("otimes")
  16881. ("perp")
  16882. ("sdot") ("cdot"."&sdot;")
  16883. ("lceil")
  16884. ("rceil")
  16885. ("lfloor")
  16886. ("rfloor")
  16887. ("lang")
  16888. ("rang")
  16889. ("loz") ("Diamond"."&loz;")
  16890. ("spades") ("spadesuit"."&spades;")
  16891. ("clubs") ("clubsuit"."&clubs;")
  16892. ("hearts") ("diamondsuit"."&hearts;")
  16893. ("diams") ("diamondsuit"."&diams;")
  16894. ("quot")
  16895. ("amp")
  16896. ("lt")
  16897. ("gt")
  16898. ("OElig")
  16899. ("oelig")
  16900. ("Scaron")
  16901. ("scaron")
  16902. ("Yuml")
  16903. ("circ")
  16904. ("tilde")
  16905. ("ensp")
  16906. ("emsp")
  16907. ("thinsp")
  16908. ("zwnj")
  16909. ("zwj")
  16910. ("lrm")
  16911. ("rlm")
  16912. ("ndash")
  16913. ("mdash")
  16914. ("lsquo")
  16915. ("rsquo")
  16916. ("sbquo")
  16917. ("ldquo")
  16918. ("rdquo")
  16919. ("bdquo")
  16920. ("dagger")
  16921. ("Dagger")
  16922. ("permil")
  16923. ("lsaquo")
  16924. ("rsaquo")
  16925. ("euro")
  16926. ("arccos"."arccos")
  16927. ("arcsin"."arcsin")
  16928. ("arctan"."arctan")
  16929. ("arg"."arg")
  16930. ("cos"."cos")
  16931. ("cosh"."cosh")
  16932. ("cot"."cot")
  16933. ("coth"."coth")
  16934. ("csc"."csc")
  16935. ("deg"."deg")
  16936. ("det"."det")
  16937. ("dim"."dim")
  16938. ("exp"."exp")
  16939. ("gcd"."gcd")
  16940. ("hom"."hom")
  16941. ("inf"."inf")
  16942. ("ker"."ker")
  16943. ("lg"."lg")
  16944. ("lim"."lim")
  16945. ("liminf"."liminf")
  16946. ("limsup"."limsup")
  16947. ("ln"."ln")
  16948. ("log"."log")
  16949. ("max"."max")
  16950. ("min"."min")
  16951. ("Pr"."Pr")
  16952. ("sec"."sec")
  16953. ("sin"."sin")
  16954. ("sinh"."sinh")
  16955. ("sup"."sup")
  16956. ("tan"."tan")
  16957. ("tanh"."tanh")
  16958. )
  16959. "Entities for TeX->HTML translation.
  16960. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  16961. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  16962. In that case, \"\\ent\" will be translated to \"&other;\".
  16963. The list contains HTML entities for Latin-1, Greek and other symbols.
  16964. It is supplemented by a number of commonly used TeX macros with appropriate
  16965. translations. There is currently no way for users to extend this.")
  16966. ;;; General functions for all backends
  16967. (defun org-cleaned-string-for-export (string &rest parameters)
  16968. "Cleanup a buffer substring so that links can be created safely."
  16969. (interactive)
  16970. (let* ((re-radio (and org-target-link-regexp
  16971. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  16972. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  16973. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  16974. (re-archive (concat ":" org-archive-tag ":"))
  16975. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  16976. (htmlp (plist-get parameters :for-html))
  16977. (outline-regexp "\\*+")
  16978. a b
  16979. rtn p)
  16980. (save-excursion
  16981. (set-buffer (get-buffer-create " org-mode-tmp"))
  16982. (erase-buffer)
  16983. (insert string)
  16984. ;; Remove license-to-kill stuff
  16985. (while (setq p (text-property-any (point-min) (point-max)
  16986. :org-license-to-kill t))
  16987. (delete-region p (next-single-property-change p :org-license-to-kill)))
  16988. (let ((org-inhibit-startup t)) (org-mode))
  16989. (untabify (point-min) (point-max))
  16990. ;; Get the correct stuff before the first headline
  16991. (when (plist-get parameters :skip-before-1st-heading)
  16992. (goto-char (point-min))
  16993. (when (re-search-forward "^\\*+[ \t]" nil t)
  16994. (delete-region (point-min) (match-beginning 0))
  16995. (goto-char (point-min))
  16996. (insert "\n")))
  16997. (when (plist-get parameters :add-text)
  16998. (goto-char (point-min))
  16999. (insert (plist-get parameters :add-text) "\n"))
  17000. ;; Get rid of archived trees
  17001. (when (not (eq org-export-with-archived-trees t))
  17002. (goto-char (point-min))
  17003. (while (re-search-forward re-archive nil t)
  17004. (if (not (org-on-heading-p t))
  17005. (org-end-of-subtree t)
  17006. (beginning-of-line 1)
  17007. (setq a (if org-export-with-archived-trees
  17008. (1+ (point-at-eol)) (point))
  17009. b (org-end-of-subtree t))
  17010. (if (> b a) (delete-region a b)))))
  17011. ;; Protect stuff from HTML processing
  17012. (goto-char (point-min))
  17013. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  17014. (add-text-properties (match-beginning 0) (match-end 0)
  17015. '(org-protected t)))
  17016. (when htmlp
  17017. (goto-char (point-min))
  17018. (while (re-search-forward "^#\\+HTML:[ \t]*\\(.*\\)" nil t)
  17019. (replace-match "\\1" t)
  17020. (add-text-properties
  17021. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  17022. '(org-protected t))))
  17023. (goto-char (point-min))
  17024. (while (re-search-forward
  17025. "^#\\+BEGIN_HTML\\>.*\\(\\(\n.*\\)*?\n\\)#\\+END_HTML\\>.*\n?" nil t)
  17026. (if htmlp
  17027. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  17028. '(org-protected t))
  17029. (delete-region (match-beginning 0) (match-end 0))))
  17030. (goto-char (point-min))
  17031. (while (re-search-forward re-quote nil t)
  17032. (goto-char (match-beginning 0))
  17033. (end-of-line 1)
  17034. (add-text-properties (point) (org-end-of-subtree t)
  17035. '(org-protected t)))
  17036. ;; Find targets in comments and move them out of comments,
  17037. ;; but mark them as targets that should be invisible
  17038. (goto-char (point-min))
  17039. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  17040. (replace-match "\\1(INVISIBLE)"))
  17041. ;; Remove comments
  17042. (goto-char (point-min))
  17043. (while (re-search-forward "^#.*\n?" nil t)
  17044. (replace-match ""))
  17045. ;; Find matches for radio targets and turn them into internal links
  17046. (goto-char (point-min))
  17047. (when re-radio
  17048. (while (re-search-forward re-radio nil t)
  17049. (org-if-unprotected
  17050. (replace-match "\\1[[\\2]]"))))
  17051. ;; Find all links that contain a newline and put them into a single line
  17052. (goto-char (point-min))
  17053. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  17054. (org-if-unprotected
  17055. (replace-match "\\1 \\3")
  17056. (goto-char (match-beginning 0))))
  17057. ;; Convert LaTeX fragments to images
  17058. (when (plist-get parameters :LaTeX-fragments)
  17059. (org-format-latex
  17060. (concat "ltxpng/" (file-name-sans-extension
  17061. (file-name-nondirectory
  17062. org-current-export-file)))
  17063. org-current-export-dir nil "Creating LaTeX image %s"))
  17064. (message "Exporting...")
  17065. ;; Normalize links: Convert angle and plain links into bracket links
  17066. ;; Expand link abbreviations
  17067. (goto-char (point-min))
  17068. (while (re-search-forward re-plain-link nil t)
  17069. (goto-char (1- (match-end 0)))
  17070. (org-if-unprotected
  17071. (replace-match
  17072. (concat
  17073. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  17074. t t)))
  17075. (goto-char (point-min))
  17076. (while (re-search-forward re-angle-link nil t)
  17077. (goto-char (1- (match-end 0)))
  17078. (org-if-unprotected
  17079. (replace-match
  17080. (concat
  17081. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  17082. t t)))
  17083. (goto-char (point-min))
  17084. (while (re-search-forward org-bracket-link-regexp nil t)
  17085. (org-if-unprotected
  17086. (replace-match
  17087. (concat "[[" (save-match-data
  17088. (org-link-expand-abbrev (match-string 1)))
  17089. "]"
  17090. (if (match-end 3)
  17091. (match-string 2)
  17092. (concat "[" (match-string 1) "]"))
  17093. "]")
  17094. t t)))
  17095. ;; Find multiline emphasis and put them into single line
  17096. (when (plist-get parameters :emph-multiline)
  17097. (goto-char (point-min))
  17098. (while (re-search-forward org-emph-re nil t)
  17099. (if (not (= (char-after (match-beginning 3))
  17100. (char-after (match-beginning 4))))
  17101. (org-if-unprotected
  17102. (subst-char-in-region (match-beginning 0) (match-end 0)
  17103. ?\n ?\ t)
  17104. (goto-char (1- (match-end 0))))
  17105. (goto-char (1+ (match-beginning 0))))))
  17106. (setq rtn (buffer-string)))
  17107. (kill-buffer " org-mode-tmp")
  17108. rtn))
  17109. (defun org-export-grab-title-from-buffer ()
  17110. "Get a title for the current document, from looking at the buffer."
  17111. (let (buffer-read-only)
  17112. (save-excursion
  17113. (goto-char (point-min))
  17114. (let ((end (save-excursion (outline-next-heading) (point))))
  17115. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  17116. ;; Mark the line so that it will not be exported as normal text.
  17117. (org-unmodified
  17118. (add-text-properties (match-beginning 0) (match-end 0)
  17119. (list :org-license-to-kill t)))
  17120. ;; Return the title string
  17121. (org-trim (match-string 0)))))))
  17122. (defun org-solidify-link-text (s &optional alist)
  17123. "Take link text and make a safe target out of it."
  17124. (save-match-data
  17125. (let* ((rtn
  17126. (mapconcat
  17127. 'identity
  17128. (org-split-string s "[ \t\r\n]+") "--"))
  17129. (a (assoc rtn alist)))
  17130. (or (cdr a) rtn))))
  17131. ;; Variable holding the vector with section numbers
  17132. (defvar org-section-numbers (make-vector org-level-max 0))
  17133. (defun org-init-section-numbers ()
  17134. "Initialize the vector for the section numbers."
  17135. (let* ((level -1)
  17136. (numbers (nreverse (org-split-string "" "\\.")))
  17137. (depth (1- (length org-section-numbers)))
  17138. (i depth) number-string)
  17139. (while (>= i 0)
  17140. (if (> i level)
  17141. (aset org-section-numbers i 0)
  17142. (setq number-string (or (car numbers) "0"))
  17143. (if (string-match "\\`[A-Z]\\'" number-string)
  17144. (aset org-section-numbers i
  17145. (- (string-to-char number-string) ?A -1))
  17146. (aset org-section-numbers i (string-to-number number-string)))
  17147. (pop numbers))
  17148. (setq i (1- i)))))
  17149. (defun org-section-number (&optional level)
  17150. "Return a string with the current section number.
  17151. When LEVEL is non-nil, increase section numbers on that level."
  17152. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  17153. (when level
  17154. (when (> level -1)
  17155. (aset org-section-numbers
  17156. level (1+ (aref org-section-numbers level))))
  17157. (setq idx (1+ level))
  17158. (while (<= idx depth)
  17159. (if (not (= idx 1))
  17160. (aset org-section-numbers idx 0))
  17161. (setq idx (1+ idx))))
  17162. (setq idx 0)
  17163. (while (<= idx depth)
  17164. (setq n (aref org-section-numbers idx))
  17165. (setq string (concat string (if (not (string= string "")) "." "")
  17166. (int-to-string n)))
  17167. (setq idx (1+ idx)))
  17168. (save-match-data
  17169. (if (string-match "\\`\\([@0]\\.\\)+" string)
  17170. (setq string (replace-match "" t nil string)))
  17171. (if (string-match "\\(\\.0\\)+\\'" string)
  17172. (setq string (replace-match "" t nil string))))
  17173. string))
  17174. ;;; ASCII export
  17175. (defvar org-last-level nil) ; dynamically scoped variable
  17176. (defvar org-levels-open nil) ; dynamically scoped parameter
  17177. (defvar org-ascii-current-indentation nil) ; For communication
  17178. (defun org-export-as-ascii (arg)
  17179. "Export the outline as a pretty ASCII file.
  17180. If there is an active region, export only the region.
  17181. The prefix ARG specifies how many levels of the outline should become
  17182. underlined headlines. The default is 3."
  17183. (interactive "P")
  17184. (setq-default org-todo-line-regexp org-todo-line-regexp)
  17185. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  17186. (org-infile-export-plist)))
  17187. (custom-times org-display-custom-times)
  17188. (org-ascii-current-indentation '(0 . 0))
  17189. (level 0) line txt
  17190. (umax nil)
  17191. (umax-toc nil)
  17192. (case-fold-search nil)
  17193. (filename (concat (file-name-as-directory
  17194. (org-export-directory :ascii opt-plist))
  17195. (file-name-sans-extension
  17196. (file-name-nondirectory buffer-file-name))
  17197. ".txt"))
  17198. (buffer (find-file-noselect filename))
  17199. (org-levels-open (make-vector org-level-max nil))
  17200. (odd org-odd-levels-only)
  17201. (date (format-time-string "%Y/%m/%d" (current-time)))
  17202. (time (format-time-string "%X" (org-current-time)))
  17203. (author (plist-get opt-plist :author))
  17204. (title (or (plist-get opt-plist :title)
  17205. (and (not
  17206. (plist-get opt-plist :skip-before-1st-heading))
  17207. (org-export-grab-title-from-buffer))
  17208. (file-name-sans-extension
  17209. (file-name-nondirectory buffer-file-name))))
  17210. (email (plist-get opt-plist :email))
  17211. (language (plist-get opt-plist :language))
  17212. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  17213. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  17214. (todo nil)
  17215. (lang-words nil)
  17216. (region
  17217. (buffer-substring
  17218. (if (org-region-active-p) (region-beginning) (point-min))
  17219. (if (org-region-active-p) (region-end) (point-max))))
  17220. (lines (org-skip-comments
  17221. (org-split-string
  17222. (org-cleaned-string-for-export
  17223. region
  17224. :skip-before-1st-heading
  17225. (plist-get opt-plist :skip-before-1st-heading)
  17226. :add-text (plist-get opt-plist :text))
  17227. "[\r\n]")))
  17228. thetoc have-headings first-heading-pos
  17229. table-open table-buffer)
  17230. (let (buffer-read-only)
  17231. (org-unmodified
  17232. (remove-text-properties (point-min) (point-max)
  17233. '(:org-license-to-kill t))))
  17234. (setq org-last-level 1)
  17235. (org-init-section-numbers)
  17236. (find-file-noselect filename)
  17237. (setq lang-words (or (assoc language org-export-language-setup)
  17238. (assoc "en" org-export-language-setup)))
  17239. (switch-to-buffer-other-window buffer)
  17240. (erase-buffer)
  17241. (fundamental-mode)
  17242. ;; create local variables for all options, to make sure all called
  17243. ;; functions get the correct information
  17244. (mapcar (lambda (x)
  17245. (set (make-local-variable (cdr x))
  17246. (plist-get opt-plist (car x))))
  17247. org-export-plist-vars)
  17248. (org-set-local 'org-odd-levels-only odd)
  17249. (setq umax (if arg (prefix-numeric-value arg)
  17250. org-export-headline-levels))
  17251. (setq umax-toc (if (integerp org-export-with-toc)
  17252. (min org-export-with-toc umax)
  17253. umax))
  17254. ;; File header
  17255. (if title (org-insert-centered title ?=))
  17256. (insert "\n")
  17257. (if (or author email)
  17258. (insert (concat (nth 1 lang-words) ": " (or author "")
  17259. (if email (concat " <" email ">") "")
  17260. "\n")))
  17261. (if (and date time)
  17262. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  17263. (insert "\n\n")
  17264. (if org-export-with-toc
  17265. (progn
  17266. (push (concat (nth 3 lang-words) "\n") thetoc)
  17267. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  17268. (mapcar '(lambda (line)
  17269. (if (string-match org-todo-line-regexp
  17270. line)
  17271. ;; This is a headline
  17272. (progn
  17273. (setq have-headings t)
  17274. (setq level (- (match-end 1) (match-beginning 1))
  17275. level (org-tr-level level)
  17276. txt (match-string 3 line)
  17277. todo
  17278. (or (and org-export-mark-todo-in-toc
  17279. (match-beginning 2)
  17280. (not (member (match-string 2 line)
  17281. org-done-keywords)))
  17282. ; TODO, not DONE
  17283. (and org-export-mark-todo-in-toc
  17284. (= level umax-toc)
  17285. (org-search-todo-below
  17286. line lines level))))
  17287. (setq txt (org-html-expand-for-ascii txt))
  17288. (if (and (memq org-export-with-tags '(not-in-toc nil))
  17289. (string-match "[ \t]+:[a-zA-Z0-9_@:]+:[ \t]*$" txt))
  17290. (setq txt (replace-match "" t t txt)))
  17291. (if (string-match quote-re0 txt)
  17292. (setq txt (replace-match "" t t txt)))
  17293. (if org-export-with-section-numbers
  17294. (setq txt (concat (org-section-number level)
  17295. " " txt)))
  17296. (if (<= level umax-toc)
  17297. (progn
  17298. (push
  17299. (concat
  17300. (make-string (* (1- level) 4) ?\ )
  17301. (format (if todo "%s (*)\n" "%s\n") txt))
  17302. thetoc)
  17303. (setq org-last-level level))
  17304. ))))
  17305. lines)
  17306. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  17307. (org-init-section-numbers)
  17308. (while (setq line (pop lines))
  17309. ;; Remove the quoted HTML tags.
  17310. (setq line (org-html-expand-for-ascii line))
  17311. ;; Remove targets
  17312. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  17313. (setq line (replace-match "" t t line)))
  17314. ;; Replace internal links
  17315. (while (string-match org-bracket-link-regexp line)
  17316. (setq line (replace-match
  17317. (if (match-end 3) "[\\3]" "[\\1]")
  17318. t nil line)))
  17319. (when custom-times
  17320. (setq line (org-translate-time line)))
  17321. (cond
  17322. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  17323. ;; a Headline
  17324. (setq first-heading-pos (or first-heading-pos (point)))
  17325. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  17326. txt (match-string 2 line))
  17327. (org-ascii-level-start level txt umax lines))
  17328. ((and org-export-with-tables
  17329. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  17330. (if (not table-open)
  17331. ;; New table starts
  17332. (setq table-open t table-buffer nil))
  17333. ;; Accumulate lines
  17334. (setq table-buffer (cons line table-buffer))
  17335. (when (or (not lines)
  17336. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  17337. (car lines))))
  17338. (setq table-open nil
  17339. table-buffer (nreverse table-buffer))
  17340. (insert (mapconcat
  17341. (lambda (x)
  17342. (org-fix-indentation x org-ascii-current-indentation))
  17343. (org-format-table-ascii table-buffer)
  17344. "\n") "\n")))
  17345. (t
  17346. (insert (org-fix-indentation line org-ascii-current-indentation) "\n"))))
  17347. (normal-mode)
  17348. ;; insert the table of contents
  17349. (when thetoc
  17350. (goto-char (point-min))
  17351. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  17352. (progn
  17353. (goto-char (match-beginning 0))
  17354. (replace-match ""))
  17355. (goto-char first-heading-pos))
  17356. (mapc 'insert thetoc)
  17357. (or (looking-at "[ \t]*\n[ \t]*\n")
  17358. (insert "\n\n")))
  17359. (save-buffer)
  17360. ;; remove display and invisible chars
  17361. (let (beg end)
  17362. (goto-char (point-min))
  17363. (while (setq beg (next-single-property-change (point) 'display))
  17364. (setq end (next-single-property-change beg 'display))
  17365. (delete-region beg end)
  17366. (goto-char beg)
  17367. (insert "=>"))
  17368. (goto-char (point-min))
  17369. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  17370. (setq end (next-single-property-change beg 'org-cwidth))
  17371. (delete-region beg end)
  17372. (goto-char beg)))
  17373. (goto-char (point-min))))
  17374. (defun org-search-todo-below (line lines level)
  17375. "Search the subtree below LINE for any TODO entries."
  17376. (let ((rest (cdr (memq line lines)))
  17377. (re org-todo-line-regexp)
  17378. line lv todo)
  17379. (catch 'exit
  17380. (while (setq line (pop rest))
  17381. (if (string-match re line)
  17382. (progn
  17383. (setq lv (- (match-end 1) (match-beginning 1))
  17384. todo (and (match-beginning 2)
  17385. (not (member (match-string 2 line)
  17386. org-done-keywords))))
  17387. ; TODO, not DONE
  17388. (if (<= lv level) (throw 'exit nil))
  17389. (if todo (throw 'exit t))))))))
  17390. (defun org-html-expand-for-ascii (line)
  17391. "Handle quoted HTML for ASCII export."
  17392. (if org-export-html-expand
  17393. (while (string-match "@<[^<>\n]*>" line)
  17394. ;; We just remove the tags for now.
  17395. (setq line (replace-match "" nil nil line))))
  17396. line)
  17397. (defun org-insert-centered (s &optional underline)
  17398. "Insert the string S centered and underline it with character UNDERLINE."
  17399. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  17400. (insert (make-string ind ?\ ) s "\n")
  17401. (if underline
  17402. (insert (make-string ind ?\ )
  17403. (make-string (string-width s) underline)
  17404. "\n"))))
  17405. (defun org-ascii-level-start (level title umax &optional lines)
  17406. "Insert a new level in ASCII export."
  17407. (let (char (n (- level umax 1)) (ind 0))
  17408. (if (> level umax)
  17409. (progn
  17410. (insert (make-string (* 2 n) ?\ )
  17411. (char-to-string (nth (% n (length org-export-ascii-bullets))
  17412. org-export-ascii-bullets))
  17413. " " title "\n")
  17414. ;; find the indentation of the next non-empty line
  17415. (catch 'stop
  17416. (while lines
  17417. (if (string-match "^\\*" (car lines)) (throw 'stop nil))
  17418. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  17419. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  17420. (pop lines)))
  17421. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  17422. (if (or (not (equal (char-before) ?\n))
  17423. (not (equal (char-before (1- (point))) ?\n)))
  17424. (insert "\n"))
  17425. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  17426. (unless org-export-with-tags
  17427. (if (string-match "[ \t]+\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  17428. (setq title (replace-match "" t t title))))
  17429. (if org-export-with-section-numbers
  17430. (setq title (concat (org-section-number level) " " title)))
  17431. (insert title "\n" (make-string (string-width title) char) "\n")
  17432. (setq org-ascii-current-indentation '(0 . 0)))))
  17433. (defun org-export-visible (type arg)
  17434. "Create a copy of the visible part of the current buffer, and export it.
  17435. The copy is created in a temporary buffer and removed after use.
  17436. TYPE is the final key (as a string) that also select the export command in
  17437. the `C-c C-e' export dispatcher.
  17438. As a special case, if the you type SPC at the prompt, the temporary
  17439. org-mode file will not be removed but presented to you so that you can
  17440. continue to use it. The prefix arg ARG is passed through to the exporting
  17441. command."
  17442. (interactive
  17443. (list (progn
  17444. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  17445. (read-char-exclusive))
  17446. current-prefix-arg))
  17447. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  17448. (error "Invalid export key"))
  17449. (let* ((binding (cdr (assoc type
  17450. '((?a . org-export-as-ascii)
  17451. (?\C-a . org-export-as-ascii)
  17452. (?b . org-export-as-html-and-open)
  17453. (?\C-b . org-export-as-html-and-open)
  17454. (?h . org-export-as-html)
  17455. (?H . org-export-as-html-to-buffer)
  17456. (?R . org-export-region-as-html)
  17457. (?x . org-export-as-xoxo)))))
  17458. (keepp (equal type ?\ ))
  17459. (file buffer-file-name)
  17460. (buffer (get-buffer-create "*Org Export Visible*"))
  17461. s e)
  17462. (with-current-buffer buffer (erase-buffer))
  17463. (save-excursion
  17464. (setq s (goto-char (point-min)))
  17465. (while (not (= (point) (point-max)))
  17466. (goto-char (org-find-invisible))
  17467. (append-to-buffer buffer s (point))
  17468. (setq s (goto-char (org-find-visible))))
  17469. (goto-char (point-min))
  17470. (unless keepp
  17471. ;; Copy all comment lines to the end, to make sure #+ settings are
  17472. ;; still available for the second export step. Kind of a hack, but
  17473. ;; does do the trick.
  17474. (if (looking-at "#[^\r\n]*")
  17475. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  17476. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  17477. (append-to-buffer buffer (1+ (match-beginning 0))
  17478. (min (point-max) (1+ (match-end 0))))))
  17479. (set-buffer buffer)
  17480. (let ((buffer-file-name file)
  17481. (org-inhibit-startup t))
  17482. (org-mode)
  17483. (show-all)
  17484. (unless keepp (funcall binding arg))))
  17485. (if (not keepp)
  17486. (kill-buffer buffer)
  17487. (switch-to-buffer-other-window buffer)
  17488. (goto-char (point-min)))))
  17489. (defun org-find-visible ()
  17490. (let ((s (point)))
  17491. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17492. (get-char-property s 'invisible)))
  17493. s))
  17494. (defun org-find-invisible ()
  17495. (let ((s (point)))
  17496. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17497. (not (get-char-property s 'invisible))))
  17498. s))
  17499. ;;; HTML export
  17500. (defun org-get-current-options ()
  17501. "Return a string with current options as keyword options.
  17502. Does include HTML export options as well as TODO and CATEGORY stuff."
  17503. (format
  17504. "#+TITLE: %s
  17505. #+AUTHOR: %s
  17506. #+EMAIL: %s
  17507. #+LANGUAGE: %s
  17508. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  17509. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s LaTeX:%s skip:%s
  17510. #+CATEGORY: %s
  17511. #+SEQ_TODO: %s
  17512. #+TYP_TODO: %s
  17513. #+PRIORITIES: %c %c %c
  17514. #+STARTUP: %s %s %s %s %s
  17515. #+TAGS: %s
  17516. #+ARCHIVE: %s
  17517. #+LINK: %s
  17518. "
  17519. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  17520. org-export-headline-levels
  17521. org-export-with-section-numbers
  17522. org-export-with-toc
  17523. org-export-preserve-breaks
  17524. org-export-html-expand
  17525. org-export-with-fixed-width
  17526. org-export-with-tables
  17527. org-export-with-sub-superscripts
  17528. org-export-with-emphasize
  17529. org-export-with-TeX-macros
  17530. org-export-with-LaTeX-fragments
  17531. org-export-skip-text-before-1st-heading
  17532. (file-name-nondirectory buffer-file-name)
  17533. "TODO FEEDBACK VERIFY DONE"
  17534. "Me Jason Marie DONE"
  17535. org-highest-priority org-lowest-priority org-default-priority
  17536. (cdr (assoc org-startup-folded
  17537. '((nil . "showall") (t . "overview") (content . "content"))))
  17538. (if org-odd-levels-only "odd" "oddeven")
  17539. (if org-hide-leading-stars "hidestars" "showstars")
  17540. (if org-startup-align-all-tables "align" "noalign")
  17541. (cond ((eq t org-log-done) "logdone")
  17542. ((not org-log-done) "nologging")
  17543. ((listp org-log-done)
  17544. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  17545. org-log-done " ")))
  17546. (or (mapconcat (lambda (x)
  17547. (cond
  17548. ((equal '(:startgroup) x) "{")
  17549. ((equal '(:endgroup) x) "}")
  17550. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  17551. (t (car x))))
  17552. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  17553. org-archive-location
  17554. "org file:~/org/%s.org"
  17555. ))
  17556. (defun org-insert-export-options-template ()
  17557. "Insert into the buffer a template with information for exporting."
  17558. (interactive)
  17559. (if (not (bolp)) (newline))
  17560. (let ((s (org-get-current-options)))
  17561. (and (string-match "#\\+CATEGORY" s)
  17562. (setq s (substring s 0 (match-beginning 0))))
  17563. (insert s)))
  17564. (defun org-toggle-fixed-width-section (arg)
  17565. "Toggle the fixed-width export.
  17566. If there is no active region, the QUOTE keyword at the current headline is
  17567. inserted or removed. When present, it causes the text between this headline
  17568. and the next to be exported as fixed-width text, and unmodified.
  17569. If there is an active region, this command adds or removes a colon as the
  17570. first character of this line. If the first character of a line is a colon,
  17571. this line is also exported in fixed-width font."
  17572. (interactive "P")
  17573. (let* ((cc 0)
  17574. (regionp (org-region-active-p))
  17575. (beg (if regionp (region-beginning) (point)))
  17576. (end (if regionp (region-end)))
  17577. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  17578. (re "[ \t]*\\(:\\)")
  17579. off)
  17580. (if regionp
  17581. (save-excursion
  17582. (goto-char beg)
  17583. (setq cc (current-column))
  17584. (beginning-of-line 1)
  17585. (setq off (looking-at re))
  17586. (while (> nlines 0)
  17587. (setq nlines (1- nlines))
  17588. (beginning-of-line 1)
  17589. (cond
  17590. (arg
  17591. (move-to-column cc t)
  17592. (insert ":\n")
  17593. (forward-line -1))
  17594. ((and off (looking-at re))
  17595. (replace-match "" t t nil 1))
  17596. ((not off) (move-to-column cc t) (insert ":")))
  17597. (forward-line 1)))
  17598. (save-excursion
  17599. (org-back-to-heading)
  17600. (if (looking-at (concat outline-regexp
  17601. "\\( +\\<" org-quote-string "\\>\\)"))
  17602. (replace-match "" t t nil 1)
  17603. (if (looking-at outline-regexp)
  17604. (progn
  17605. (goto-char (match-end 0))
  17606. (insert " " org-quote-string))))))))
  17607. (defun org-export-as-html-and-open (arg)
  17608. "Export the outline as HTML and immediately open it with a browser.
  17609. If there is an active region, export only the region.
  17610. The prefix ARG specifies how many levels of the outline should become
  17611. headlines. The default is 3. Lower levels will become bulleted lists."
  17612. (interactive "P")
  17613. (org-export-as-html arg 'hidden)
  17614. (org-open-file buffer-file-name))
  17615. (defun org-export-as-html-batch ()
  17616. "Call `org-export-as-html', may be used in batch processing as
  17617. emacs --batch
  17618. --load=$HOME/lib/emacs/org.el
  17619. --eval \"(setq org-export-headline-levels 2)\"
  17620. --visit=MyFile --funcall org-export-as-html-batch"
  17621. (org-export-as-html org-export-headline-levels 'hidden))
  17622. (defun org-export-as-html-to-buffer (arg)
  17623. "Call `org-exort-as-html` with output to a temporary buffer.
  17624. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  17625. (interactive "P")
  17626. (org-export-as-html arg nil nil "*Org HTML Export*")
  17627. (switch-to-buffer-other-window "*Org HTML Export*"))
  17628. (defun org-replace-region-by-html (beg end)
  17629. "Assume the current region has org-mode syntax, and convert it to HTML.
  17630. This can be used in any buffer. For example, you could write an
  17631. itemized list in org-mode syntax in an HTML buffer and then use this
  17632. command to convert it."
  17633. (interactive "r")
  17634. (let (reg html buf)
  17635. (if (org-mode-p)
  17636. (setq html (org-export-region-as-html
  17637. beg end t 'string))
  17638. (setq reg (buffer-substring beg end)
  17639. buf (get-buffer-create "*Org tmp*"))
  17640. (save-excursion
  17641. (set-buffer buf)
  17642. (erase-buffer)
  17643. (insert reg)
  17644. (org-mode)
  17645. (setq html (org-export-region-as-html
  17646. (point-min) (point-max) t 'string)))
  17647. (kill-buffer buf))
  17648. (delete-region beg end)
  17649. (insert html)))
  17650. (defun org-export-region-as-html (beg end &optional body-only buffer)
  17651. "Convert region from BEG to END in org-mode buffer to HTML.
  17652. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  17653. contents, and only produce the region of converted text, useful for
  17654. cut-and-paste operations.
  17655. If BUFFER is a buffer or a string, use/create that buffer as a target
  17656. of the converted HTML. If BUFFER is the symbol `string', return the
  17657. produced HTML as a string and leave not buffer behind. For example,
  17658. a Lisp program could call this function in the following way:
  17659. (setq html (org-export-region-as-html beg end t 'string))
  17660. When called interactively, the output buffer is selected, and shown
  17661. in a window. A non-interactive call will only retunr the buffer."
  17662. (interactive "r\nP")
  17663. (when (interactive-p)
  17664. (setq buffer "*Org HTML EXPORT*"))
  17665. (let ((transient-mark-mode t) (zmacs-regions t)
  17666. rtn)
  17667. (goto-char end)
  17668. (set-mark (point)) ;; to activate the region
  17669. (goto-char beg)
  17670. (setq rtn (org-export-as-html
  17671. nil nil nil
  17672. buffer body-only))
  17673. (if (fboundp 'deactivate-mark) (deactivate-mark))
  17674. (if (and (interactive-p) (bufferp rtn))
  17675. (switch-to-buffer-other-window rtn)
  17676. rtn)))
  17677. (defun org-export-as-html (arg &optional hidden ext-plist
  17678. to-buffer body-only)
  17679. "Export the outline as a pretty HTML file.
  17680. If there is an active region, export only the region. The prefix
  17681. ARG specifies how many levels of the outline should become
  17682. headlines. The default is 3. Lower levels will become bulleted
  17683. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  17684. EXT-PLIST is a property list with external parameters overriding
  17685. org-mode's default settings, but still inferior to file-local
  17686. settings. When TO-BUFFER is non-nil, create a buffer with that
  17687. name and export to that buffer. If TO-BUFFER is the symbol `string',
  17688. don't leave any buffer behind but just return the resulting HTML as
  17689. a string. When BODY-ONLY is set, don't produce the file header and footer,
  17690. simply return the content of <body>...</body>, without even
  17691. the body tags themselves."
  17692. (interactive "P")
  17693. ;; Make sure we have a file name when we need it.
  17694. (when (and (not (or to-buffer body-only))
  17695. (not buffer-file-name))
  17696. (if (buffer-base-buffer)
  17697. (org-set-local 'buffer-file-name
  17698. (with-current-buffer (buffer-base-buffer)
  17699. buffer-file-name))
  17700. (error "Need a file name to be able to export.")))
  17701. (message "Exporting...")
  17702. (setq-default org-todo-line-regexp org-todo-line-regexp)
  17703. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  17704. (setq-default org-done-keywords org-done-keywords)
  17705. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  17706. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  17707. ext-plist
  17708. (org-infile-export-plist)))
  17709. (style (plist-get opt-plist :style))
  17710. (link-validate (plist-get opt-plist :link-validation-function))
  17711. valid thetoc have-headings first-heading-pos
  17712. (odd org-odd-levels-only)
  17713. (region-p (org-region-active-p))
  17714. ;; The following two are dynamically scoped into other
  17715. ;; routines below.
  17716. (org-current-export-dir (org-export-directory :html opt-plist))
  17717. (org-current-export-file buffer-file-name)
  17718. (level 0) (line "") (origline "") txt todo
  17719. (umax nil)
  17720. (umax-toc nil)
  17721. (filename (if to-buffer nil
  17722. (concat (file-name-as-directory
  17723. (org-export-directory :html opt-plist))
  17724. (file-name-sans-extension
  17725. (file-name-nondirectory buffer-file-name))
  17726. ".html")))
  17727. (current-dir (if buffer-file-name
  17728. (file-name-directory buffer-file-name)
  17729. default-directory))
  17730. (buffer (if to-buffer
  17731. (cond
  17732. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  17733. (t (get-buffer-create to-buffer)))
  17734. (find-file-noselect filename)))
  17735. (org-levels-open (make-vector org-level-max nil))
  17736. (date (format-time-string "%Y/%m/%d" (current-time)))
  17737. (time (format-time-string "%X" (org-current-time)))
  17738. (author (plist-get opt-plist :author))
  17739. (title (or (plist-get opt-plist :title)
  17740. (and (not
  17741. (plist-get opt-plist :skip-before-1st-heading))
  17742. (org-export-grab-title-from-buffer))
  17743. (and buffer-file-name
  17744. (file-name-sans-extension
  17745. (file-name-nondirectory buffer-file-name)))
  17746. "UNTITLED"))
  17747. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  17748. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  17749. (inquote nil)
  17750. (infixed nil)
  17751. (in-local-list nil)
  17752. (local-list-num nil)
  17753. (local-list-indent nil)
  17754. (llt org-plain-list-ordered-item-terminator)
  17755. (email (plist-get opt-plist :email))
  17756. (language (plist-get opt-plist :language))
  17757. (lang-words nil)
  17758. (target-alist nil) tg
  17759. (head-count 0) cnt
  17760. (start 0)
  17761. (coding-system (and (boundp 'buffer-file-coding-system)
  17762. buffer-file-coding-system))
  17763. (coding-system-for-write (or org-export-html-coding-system
  17764. coding-system))
  17765. (save-buffer-coding-system (or org-export-html-coding-system
  17766. coding-system))
  17767. (charset (and coding-system-for-write
  17768. (fboundp 'coding-system-get)
  17769. (coding-system-get coding-system-for-write
  17770. 'mime-charset)))
  17771. (region
  17772. (buffer-substring
  17773. (if region-p (region-beginning) (point-min))
  17774. (if region-p (region-end) (point-max))))
  17775. (lines
  17776. (org-skip-comments (org-split-string
  17777. (org-cleaned-string-for-export
  17778. region
  17779. :emph-multiline t
  17780. :for-html t
  17781. :skip-before-1st-heading
  17782. (plist-get opt-plist :skip-before-1st-heading)
  17783. :add-text
  17784. (plist-get opt-plist :text)
  17785. :LaTeX-fragments
  17786. (plist-get opt-plist :LaTeX-fragments))
  17787. "[\r\n]")))
  17788. table-open type
  17789. table-buffer table-orig-buffer
  17790. ind start-is-num starter didclose
  17791. rpl path desc descp desc1 desc2 link
  17792. )
  17793. (let (buffer-read-only)
  17794. (org-unmodified
  17795. (remove-text-properties (point-min) (point-max)
  17796. '(:org-license-to-kill t))))
  17797. (message "Exporting...")
  17798. (setq org-last-level 1)
  17799. (org-init-section-numbers)
  17800. ;; Get the language-dependent settings
  17801. (setq lang-words (or (assoc language org-export-language-setup)
  17802. (assoc "en" org-export-language-setup)))
  17803. ;; Switch to the output buffer
  17804. (set-buffer buffer)
  17805. (erase-buffer)
  17806. (fundamental-mode)
  17807. (let ((case-fold-search nil)
  17808. (org-odd-levels-only odd))
  17809. ;; create local variables for all options, to make sure all called
  17810. ;; functions get the correct information
  17811. (mapcar (lambda (x)
  17812. (set (make-local-variable (cdr x))
  17813. (plist-get opt-plist (car x))))
  17814. org-export-plist-vars)
  17815. (setq umax (if arg (prefix-numeric-value arg)
  17816. org-export-headline-levels))
  17817. (setq umax-toc (if (integerp org-export-with-toc)
  17818. (min org-export-with-toc umax)
  17819. umax))
  17820. (unless body-only
  17821. ;; File header
  17822. (insert (format
  17823. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  17824. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  17825. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  17826. lang=\"%s\" xml:lang=\"%s\">
  17827. <head>
  17828. <title>%s</title>
  17829. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  17830. <meta name=\"generator\" content=\"Org-mode\"/>
  17831. <meta name=\"generated\" content=\"%s %s\"/>
  17832. <meta name=\"author\" content=\"%s\"/>
  17833. %s
  17834. </head><body>
  17835. "
  17836. language language (org-html-expand title)
  17837. (or charset "iso-8859-1") date time author style))
  17838. (insert (or (plist-get opt-plist :preamble) ""))
  17839. (when (plist-get opt-plist :auto-preamble)
  17840. (if title (insert (format org-export-html-title-format
  17841. (org-html-expand title))))))
  17842. (if (and org-export-with-toc (not body-only))
  17843. (progn
  17844. (push (format "<h%d>%s</h%d>\n"
  17845. org-export-html-toplevel-hlevel
  17846. (nth 3 lang-words)
  17847. org-export-html-toplevel-hlevel)
  17848. thetoc)
  17849. (push "<ul>\n<li>" thetoc)
  17850. (setq lines
  17851. (mapcar '(lambda (line)
  17852. (if (string-match org-todo-line-regexp line)
  17853. ;; This is a headline
  17854. (progn
  17855. (setq have-headings t)
  17856. (setq level (- (match-end 1) (match-beginning 1))
  17857. level (org-tr-level level)
  17858. txt (save-match-data
  17859. (org-html-expand
  17860. (org-export-cleanup-toc-line
  17861. (match-string 3 line))))
  17862. todo
  17863. (or (and org-export-mark-todo-in-toc
  17864. (match-beginning 2)
  17865. (not (member (match-string 2 line)
  17866. org-done-keywords)))
  17867. ; TODO, not DONE
  17868. (and org-export-mark-todo-in-toc
  17869. (= level umax-toc)
  17870. (org-search-todo-below
  17871. line lines level))))
  17872. (if (and (memq org-export-with-tags '(not-in-toc nil))
  17873. (string-match "[ \t]+:[a-zA-Z0-9_@:]+:[ \t]*$" txt))
  17874. (setq txt (replace-match "" t t txt)))
  17875. (if (string-match quote-re0 txt)
  17876. (setq txt (replace-match "" t t txt)))
  17877. (if org-export-with-section-numbers
  17878. (setq txt (concat (org-section-number level)
  17879. " " txt)))
  17880. (if (<= level (max umax umax-toc))
  17881. (setq head-count (+ head-count 1)))
  17882. (if (<= level umax-toc)
  17883. (progn
  17884. (if (> level org-last-level)
  17885. (progn
  17886. (setq cnt (- level org-last-level))
  17887. (while (>= (setq cnt (1- cnt)) 0)
  17888. (push "\n<ul>\n<li>" thetoc))
  17889. (push "\n" thetoc)))
  17890. (if (< level org-last-level)
  17891. (progn
  17892. (setq cnt (- org-last-level level))
  17893. (while (>= (setq cnt (1- cnt)) 0)
  17894. (push "</li>\n</ul>" thetoc))
  17895. (push "\n" thetoc)))
  17896. ;; Check for targets
  17897. (while (string-match org-target-regexp line)
  17898. (setq tg (match-string 1 line)
  17899. line (replace-match
  17900. (concat "@<span class=\"target\">" tg "@</span> ")
  17901. t t line))
  17902. (push (cons (org-solidify-link-text tg)
  17903. (format "sec-%d" head-count))
  17904. target-alist))
  17905. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  17906. (setq txt (replace-match "" t t txt)))
  17907. (push
  17908. (format
  17909. (if todo
  17910. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  17911. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  17912. head-count txt) thetoc)
  17913. (setq org-last-level level))
  17914. )))
  17915. line)
  17916. lines))
  17917. (while (> org-last-level 0)
  17918. (setq org-last-level (1- org-last-level))
  17919. (push "</li>\n</ul>\n" thetoc))
  17920. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  17921. (setq head-count 0)
  17922. (org-init-section-numbers)
  17923. (while (setq line (pop lines) origline line)
  17924. (catch 'nextline
  17925. ;; end of quote section?
  17926. (when (and inquote (string-match "^\\*+" line))
  17927. (insert "</pre>\n")
  17928. (setq inquote nil))
  17929. ;; inside a quote section?
  17930. (when inquote
  17931. (insert (org-html-protect line) "\n")
  17932. (throw 'nextline nil))
  17933. ;; verbatim lines
  17934. (when (and org-export-with-fixed-width
  17935. (string-match "^[ \t]*:\\(.*\\)" line))
  17936. (when (not infixed)
  17937. (setq infixed t)
  17938. (insert "<pre>\n"))
  17939. (insert (org-html-protect (match-string 1 line)) "\n")
  17940. (when (and lines
  17941. (not (string-match "^[ \t]*\\(:.*\\)"
  17942. (car lines))))
  17943. (setq infixed nil)
  17944. (insert "</pre>\n"))
  17945. (throw 'nextline nil))
  17946. ;; Protected HTML
  17947. (when (get-text-property 0 'org-protected line)
  17948. (let (par)
  17949. (when (re-search-backward
  17950. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  17951. (setq par (match-string 1))
  17952. (replace-match "\\2\n"))
  17953. (insert line "\n")
  17954. (while (and lines
  17955. (get-text-property 0 'org-protected (car lines)))
  17956. (insert (pop lines) "\n"))
  17957. (and par (insert "<p>\n")))
  17958. (throw 'nextline nil))
  17959. ;; Horizontal line
  17960. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  17961. (insert "\n<hr/>\n")
  17962. (throw 'nextline nil))
  17963. ;; make targets to anchors
  17964. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  17965. (cond
  17966. ((match-end 2)
  17967. (setq line (replace-match
  17968. (concat "@<a name=\""
  17969. (org-solidify-link-text (match-string 1 line))
  17970. "\">\\nbsp@</a>")
  17971. t t line)))
  17972. ((and org-export-with-toc (equal (string-to-char line) ?*))
  17973. (setq line (replace-match
  17974. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  17975. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  17976. t t line)))
  17977. (t
  17978. (setq line (replace-match
  17979. (concat "@<a name=\""
  17980. (org-solidify-link-text (match-string 1 line))
  17981. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  17982. t t line)))))
  17983. (setq line (org-html-handle-time-stamps line))
  17984. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  17985. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  17986. ;; Also handle sub_superscripts and checkboxes
  17987. (setq line (org-html-expand line))
  17988. ;; Format the links
  17989. (setq start 0)
  17990. (while (string-match org-bracket-link-analytic-regexp line start)
  17991. (setq start (match-beginning 0))
  17992. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  17993. (setq path (match-string 3 line))
  17994. (setq desc1 (if (match-end 5) (match-string 5 line))
  17995. desc2 (if (match-end 2) (concat type ":" path) path)
  17996. descp (and desc1 (not (equal desc1 desc2)))
  17997. desc (or desc1 desc2))
  17998. ;; Make an image out of the description if that is so wanted
  17999. (when (and descp (org-file-image-p desc))
  18000. (save-match-data
  18001. (if (string-match "^file:" desc)
  18002. (setq desc (substring desc (match-end 0)))))
  18003. (setq desc (concat "<img src=\"" desc "\"/>")))
  18004. ;; FIXME: do we need to unescape here somewhere?
  18005. (cond
  18006. ((equal type "internal")
  18007. (setq rpl
  18008. (concat
  18009. "<a href=\"#"
  18010. (org-solidify-link-text
  18011. (save-match-data (org-link-unescape path)) target-alist)
  18012. "\">" desc "</a>")))
  18013. ((member type '("http" "https")) ; FIXME: need to test this.
  18014. ;; standard URL, just check if we need to inline an image
  18015. (if (and (or (eq t org-export-html-inline-images)
  18016. (and org-export-html-inline-images (not descp)))
  18017. (org-file-image-p path))
  18018. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  18019. (setq link (concat type ":" path))
  18020. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  18021. ((member type '("ftp" "mailto" "news"))
  18022. ;; standard URL
  18023. (setq link (concat type ":" path))
  18024. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  18025. ((string= type "file")
  18026. ;; FILE link
  18027. (let* ((filename path)
  18028. (abs-p (file-name-absolute-p filename))
  18029. thefile file-is-image-p search)
  18030. (save-match-data
  18031. (if (string-match "::\\(.*\\)" filename)
  18032. (setq search (match-string 1 filename)
  18033. filename (replace-match "" t nil filename)))
  18034. (setq valid
  18035. (if (functionp link-validate)
  18036. (funcall link-validate filename current-dir)
  18037. t))
  18038. (setq file-is-image-p (org-file-image-p filename))
  18039. (setq thefile (if abs-p (expand-file-name filename) filename))
  18040. (when (and org-export-html-link-org-files-as-html
  18041. (string-match "\\.org$" thefile))
  18042. (setq thefile (concat (substring thefile 0
  18043. (match-beginning 0))
  18044. ".html"))
  18045. (if (and search
  18046. ;; make sure this is can be used as target search
  18047. (not (string-match "^[0-9]*$" search))
  18048. (not (string-match "^\\*" search))
  18049. (not (string-match "^/.*/$" search)))
  18050. (setq thefile (concat thefile "#"
  18051. (org-solidify-link-text
  18052. (org-link-unescape search)))))
  18053. (when (string-match "^file:" desc)
  18054. (setq desc (replace-match "" t t desc))
  18055. (if (string-match "\\.org$" desc)
  18056. (setq desc (replace-match "" t t desc))))))
  18057. (setq rpl (if (and file-is-image-p
  18058. (or (eq t org-export-html-inline-images)
  18059. (and org-export-html-inline-images
  18060. (not descp))))
  18061. (concat "<img src=\"" thefile "\"/>")
  18062. (concat "<a href=\"" thefile "\">" desc "</a>")))
  18063. (if (not valid) (setq rpl desc))))
  18064. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  18065. (setq rpl (concat "<i>&lt;" type ":"
  18066. (save-match-data (org-link-unescape path))
  18067. "&gt;</i>"))))
  18068. (setq line (replace-match rpl t t line)
  18069. start (+ start (length rpl))))
  18070. ;; TODO items
  18071. (if (and (string-match org-todo-line-regexp line)
  18072. (match-beginning 2))
  18073. (if (member (match-string 2 line) org-done-keywords)
  18074. (setq line (replace-match
  18075. "<span class=\"done\">\\2</span>"
  18076. t nil line 2))
  18077. (setq line
  18078. (concat (substring line 0 (match-beginning 2))
  18079. "<span class=\"todo\">" (match-string 2 line)
  18080. "</span>" (substring line (match-end 2))))))
  18081. ;; Does this contain a reference to a footnote?
  18082. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line)
  18083. (let ((n (match-string 2 line)))
  18084. (setq line
  18085. (replace-match
  18086. (format
  18087. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  18088. (match-string 1 line) n n n)
  18089. t t line))))
  18090. (cond
  18091. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  18092. ;; This is a headline
  18093. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  18094. txt (match-string 2 line))
  18095. (if (string-match quote-re0 txt)
  18096. (setq txt (replace-match "" t t txt)))
  18097. (if (<= level (max umax umax-toc))
  18098. (setq head-count (+ head-count 1)))
  18099. (when in-local-list
  18100. ;; Close any local lists before inserting a new header line
  18101. (while local-list-num
  18102. (org-close-li)
  18103. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  18104. (pop local-list-num))
  18105. (setq local-list-indent nil
  18106. in-local-list nil))
  18107. (setq first-heading-pos (or first-heading-pos (point)))
  18108. (org-html-level-start level txt umax
  18109. (and org-export-with-toc (<= level umax))
  18110. head-count)
  18111. ;; QUOTES
  18112. (when (string-match quote-re line)
  18113. (insert "<pre>")
  18114. (setq inquote t)))
  18115. ((and org-export-with-tables
  18116. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  18117. (if (not table-open)
  18118. ;; New table starts
  18119. (setq table-open t table-buffer nil table-orig-buffer nil))
  18120. ;; Accumulate lines
  18121. (setq table-buffer (cons line table-buffer)
  18122. table-orig-buffer (cons origline table-orig-buffer))
  18123. (when (or (not lines)
  18124. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  18125. (car lines))))
  18126. (setq table-open nil
  18127. table-buffer (nreverse table-buffer)
  18128. table-orig-buffer (nreverse table-orig-buffer))
  18129. (org-close-par-maybe)
  18130. (insert (org-format-table-html table-buffer table-orig-buffer))))
  18131. (t
  18132. ;; Normal lines
  18133. (when (string-match
  18134. (cond
  18135. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  18136. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  18137. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  18138. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  18139. line)
  18140. (setq ind (org-get-string-indentation line)
  18141. start-is-num (match-beginning 4)
  18142. starter (if (match-beginning 2)
  18143. (substring (match-string 2 line) 0 -1))
  18144. line (substring line (match-beginning 5)))
  18145. (unless (string-match "[^ \t]" line)
  18146. ;; empty line. Pretend indentation is large.
  18147. (setq ind (if org-empty-line-terminates-plain-lists
  18148. 0
  18149. (1+ (or (car local-list-indent) 1)))))
  18150. (setq didclose nil)
  18151. (while (and in-local-list
  18152. (or (and (= ind (car local-list-indent))
  18153. (not starter))
  18154. (< ind (car local-list-indent))))
  18155. (setq didclose t)
  18156. (org-close-li)
  18157. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  18158. (pop local-list-num) (pop local-list-indent)
  18159. (setq in-local-list local-list-indent))
  18160. (cond
  18161. ((and starter
  18162. (or (not in-local-list)
  18163. (> ind (car local-list-indent))))
  18164. ;; Start new (level of) list
  18165. (org-close-par-maybe)
  18166. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  18167. (push start-is-num local-list-num)
  18168. (push ind local-list-indent)
  18169. (setq in-local-list t))
  18170. (starter
  18171. ;; continue current list
  18172. (org-close-li)
  18173. (insert "<li>\n"))
  18174. (didclose
  18175. ;; we did close a list, normal text follows: need <p>
  18176. (org-open-par)))
  18177. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  18178. (setq line
  18179. (replace-match
  18180. (if (equal (match-string 1 line) "X")
  18181. "<b>[X]</b>"
  18182. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  18183. t t line))))
  18184. ;; Empty lines start a new paragraph. If hand-formatted lists
  18185. ;; are not fully interpreted, lines starting with "-", "+", "*"
  18186. ;; also start a new paragraph.
  18187. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  18188. ;; Is this the start of a footnote?
  18189. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  18190. (org-close-par-maybe)
  18191. (let ((n (match-string 1 line)))
  18192. (setq line (replace-match
  18193. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line))))
  18194. ;; Check if the line break needs to be conserved
  18195. (cond
  18196. ((string-match "\\\\\\\\[ \t]*$" line)
  18197. (setq line (replace-match "<br/>" t t line)))
  18198. (org-export-preserve-breaks
  18199. (setq line (concat line "<br/>"))))
  18200. (insert line "\n")))))
  18201. ;; Properly close all local lists and other lists
  18202. (when inquote (insert "</pre>\n"))
  18203. (when in-local-list
  18204. ;; Close any local lists before inserting a new header line
  18205. (while local-list-num
  18206. (org-close-li)
  18207. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  18208. (pop local-list-num))
  18209. (setq local-list-indent nil
  18210. in-local-list nil))
  18211. (org-html-level-start 1 nil umax
  18212. (and org-export-with-toc (<= level umax))
  18213. head-count)
  18214. (unless body-only
  18215. (when (plist-get opt-plist :auto-postamble)
  18216. (when author
  18217. (insert "<p class=\"author\"> "
  18218. (nth 1 lang-words) ": " author "\n")
  18219. (when email
  18220. (insert "<a href=\"mailto:" email "\">&lt;"
  18221. email "&gt;</a>\n"))
  18222. (insert "</p>\n"))
  18223. (when (and date time)
  18224. (insert "<p class=\"date\"> "
  18225. (nth 2 lang-words) ": "
  18226. date " " time "</p>\n")))
  18227. (if org-export-html-with-timestamp
  18228. (insert org-export-html-html-helper-timestamp))
  18229. (insert (or (plist-get opt-plist :postamble) ""))
  18230. (insert "</body>\n</html>\n"))
  18231. (normal-mode)
  18232. (if (eq major-mode default-major-mode) (html-mode))
  18233. ;; insert the table of contents
  18234. (goto-char (point-min))
  18235. (when thetoc
  18236. (if (or (re-search-forward
  18237. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  18238. (re-search-forward
  18239. "\\[TABLE-OF-CONTENTS\\]" nil t))
  18240. (progn
  18241. (goto-char (match-beginning 0))
  18242. (replace-match ""))
  18243. (goto-char first-heading-pos)
  18244. (when (looking-at "\\s-*</p>")
  18245. (goto-char (match-end 0))
  18246. (insert "\n")))
  18247. (mapc 'insert thetoc))
  18248. ;; remove empty paragraphs and lists
  18249. (goto-char (point-min))
  18250. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  18251. (replace-match ""))
  18252. (goto-char (point-min))
  18253. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  18254. (replace-match ""))
  18255. (or to-buffer (save-buffer))
  18256. (goto-char (point-min))
  18257. (message "Exporting... done")
  18258. (if (eq to-buffer 'string)
  18259. (prog1 (buffer-substring (point-min) (point-max))
  18260. (kill-buffer (current-buffer)))
  18261. (current-buffer)))))
  18262. (defvar org-table-colgroup-info nil) ;; FIXME: mode to a better place
  18263. (defun org-format-table-ascii (lines)
  18264. "Format a table for ascii export."
  18265. (if (stringp lines)
  18266. (setq lines (org-split-string lines "\n")))
  18267. (if (not (string-match "^[ \t]*|" (car lines)))
  18268. ;; Table made by table.el - test for spanning
  18269. lines
  18270. ;; A normal org table
  18271. ;; Get rid of hlines at beginning and end
  18272. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  18273. (setq lines (nreverse lines))
  18274. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  18275. (setq lines (nreverse lines))
  18276. (when org-export-table-remove-special-lines
  18277. ;; Check if the table has a marking column. If yes remove the
  18278. ;; column and the special lines
  18279. (setq lines (org-table-clean-before-export lines)))
  18280. ;; Get rid of the vertical lines except for grouping
  18281. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  18282. rtn line vl1 start)
  18283. (while (setq line (pop lines))
  18284. (if (string-match org-table-hline-regexp line)
  18285. (and (string-match "|\\(.*\\)|" line)
  18286. (setq line (replace-match " \\1" t nil line)))
  18287. (setq start 0 vl1 vl)
  18288. (while (string-match "|" line start)
  18289. (setq start (match-end 0))
  18290. (or (pop vl1) (setq line (replace-match " " t t line)))))
  18291. (push line rtn))
  18292. (nreverse rtn))))
  18293. (defun org-colgroup-info-to-vline-list (info)
  18294. (let (vl new last rtn line)
  18295. (while info
  18296. (setq last new new (pop info))
  18297. (if (or (memq last '(:end :startend))
  18298. (memq new '(:start :startend)))
  18299. (push t vl)
  18300. (push nil vl)))
  18301. (setq vl (cons nil (nreverse vl)))))
  18302. (defun org-format-table-html (lines olines)
  18303. "Find out which HTML converter to use and return the HTML code."
  18304. (if (stringp lines)
  18305. (setq lines (org-split-string lines "\n")))
  18306. (if (string-match "^[ \t]*|" (car lines))
  18307. ;; A normal org table
  18308. (org-format-org-table-html lines)
  18309. ;; Table made by table.el - test for spanning
  18310. (let* ((hlines (delq nil (mapcar
  18311. (lambda (x)
  18312. (if (string-match "^[ \t]*\\+-" x) x
  18313. nil))
  18314. lines)))
  18315. (first (car hlines))
  18316. (ll (and (string-match "\\S-+" first)
  18317. (match-string 0 first)))
  18318. (re (concat "^[ \t]*" (regexp-quote ll)))
  18319. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  18320. hlines))))
  18321. (if (and (not spanning)
  18322. (not org-export-prefer-native-exporter-for-tables))
  18323. ;; We can use my own converter with HTML conversions
  18324. (org-format-table-table-html lines)
  18325. ;; Need to use the code generator in table.el, with the original text.
  18326. (org-format-table-table-html-using-table-generate-source olines)))))
  18327. (defun org-format-org-table-html (lines &optional splice)
  18328. "Format a table into HTML."
  18329. ;; Get rid of hlines at beginning and end
  18330. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  18331. (setq lines (nreverse lines))
  18332. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  18333. (setq lines (nreverse lines))
  18334. (when org-export-table-remove-special-lines
  18335. ;; Check if the table has a marking column. If yes remove the
  18336. ;; column and the special lines
  18337. (setq lines (org-table-clean-before-export lines)))
  18338. (let ((head (and org-export-highlight-first-table-line
  18339. (delq nil (mapcar
  18340. (lambda (x) (string-match "^[ \t]*|-" x))
  18341. (cdr lines)))))
  18342. (nlines 0) fnum i
  18343. tbopen line fields html gr colgropen)
  18344. (if splice (setq head nil))
  18345. (unless splice (push (if head "<thead>" "<tbody>") html))
  18346. (setq tbopen t)
  18347. (while (setq line (pop lines))
  18348. (catch 'next-line
  18349. (if (string-match "^[ \t]*|-" line)
  18350. (progn
  18351. (unless splice
  18352. (push (if head "</thead>" "</tbody>") html)
  18353. (if lines (push "<tbody>" html) (setq tbopen nil)))
  18354. (setq head nil) ;; head ends here, first time around
  18355. ;; ignore this line
  18356. (throw 'next-line t)))
  18357. ;; Break the line into fields
  18358. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  18359. (unless fnum (setq fnum (make-vector (length fields) 0)))
  18360. (setq nlines (1+ nlines) i -1)
  18361. (push (concat "<tr>"
  18362. (mapconcat
  18363. (lambda (x)
  18364. (setq i (1+ i))
  18365. (if (and (< i nlines)
  18366. (string-match org-table-number-regexp x))
  18367. (incf (aref fnum i)))
  18368. (if head
  18369. (concat (car org-export-table-header-tags) x
  18370. (cdr org-export-table-header-tags))
  18371. (concat (car org-export-table-data-tags) x
  18372. (cdr org-export-table-data-tags))))
  18373. fields "")
  18374. "</tr>")
  18375. html)))
  18376. (unless splice (if tbopen (push "</tbody>" html)))
  18377. (unless splice (push "</table>\n" html))
  18378. (setq html (nreverse html))
  18379. (unless splice
  18380. ;; Put in COL tags with the alignment (unfortuntely often ignored...)
  18381. (push (mapconcat
  18382. (lambda (x)
  18383. (setq gr (pop org-table-colgroup-info))
  18384. (format "%s<COL align=\"%s\"></COL>%s"
  18385. (if (memq gr '(:start :startend))
  18386. (prog1
  18387. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  18388. (setq colgropen t))
  18389. "")
  18390. (if (> (/ (float x) nlines) org-table-number-fraction)
  18391. "right" "left")
  18392. (if (memq gr '(:end :startend))
  18393. (progn (setq colgropen nil) "</colgroup>")
  18394. "")))
  18395. fnum "")
  18396. html)
  18397. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  18398. (push org-export-html-table-tag html))
  18399. (concat (mapconcat 'identity html "\n") "\n")))
  18400. (defun org-table-clean-before-export (lines)
  18401. "Check if the table has a marking column.
  18402. If yes remove the column and the special lines."
  18403. (setq org-table-colgroup-info nil)
  18404. (if (memq nil
  18405. (mapcar
  18406. (lambda (x) (or (string-match "^[ \t]*|-" x)
  18407. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  18408. lines))
  18409. (progn
  18410. (setq org-table-clean-did-remove-column nil)
  18411. (delq nil
  18412. (mapcar
  18413. (lambda (x)
  18414. (cond
  18415. ((string-match "^[ \t]*| */ *|" x)
  18416. (setq org-table-colgroup-info
  18417. (mapcar (lambda (x)
  18418. (cond ((member x '("<" "&lt;")) :start)
  18419. ((member x '(">" "&gt;")) :end)
  18420. ((member x '("<>" "&lt;&gt;")) :startend)
  18421. (t nil)))
  18422. (org-split-string x "[ \t]*|[ \t]*")))
  18423. nil)
  18424. (t x)))
  18425. lines)))
  18426. (setq org-table-clean-did-remove-column t)
  18427. (delq nil
  18428. (mapcar
  18429. (lambda (x)
  18430. (cond
  18431. ((string-match "^[ \t]*| */ *|" x)
  18432. (setq org-table-colgroup-info
  18433. (mapcar (lambda (x)
  18434. (cond ((member x '("<" "&lt;")) :start)
  18435. ((member x '(">" "&gt;")) :end)
  18436. ((member x '("<>" "&lt;&gt;")) :startend)
  18437. (t nil)))
  18438. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  18439. nil)
  18440. ((string-match "^[ \t]*| *[!_^/] *|" x)
  18441. nil) ; ignore this line
  18442. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  18443. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  18444. ;; remove the first column
  18445. (replace-match "\\1|" t nil x))
  18446. (t (error "This should not happen"))))
  18447. lines))))
  18448. (defun org-format-table-table-html (lines)
  18449. "Format a table generated by table.el into HTML.
  18450. This conversion does *not* use `table-generate-source' from table.el.
  18451. This has the advantage that Org-mode's HTML conversions can be used.
  18452. But it has the disadvantage, that no cell- or row-spanning is allowed."
  18453. (let (line field-buffer
  18454. (head org-export-highlight-first-table-line)
  18455. fields html empty)
  18456. (setq html (concat org-export-html-table-tag "\n"))
  18457. (while (setq line (pop lines))
  18458. (setq empty "&nbsp;")
  18459. (catch 'next-line
  18460. (if (string-match "^[ \t]*\\+-" line)
  18461. (progn
  18462. (if field-buffer
  18463. (progn
  18464. (setq
  18465. html
  18466. (concat
  18467. html
  18468. "<tr>"
  18469. (mapconcat
  18470. (lambda (x)
  18471. (if (equal x "") (setq x empty))
  18472. (if head
  18473. (concat (car org-export-table-header-tags) x
  18474. (cdr org-export-table-header-tags))
  18475. (concat (car org-export-table-data-tags) x
  18476. (cdr org-export-table-data-tags))))
  18477. field-buffer "\n")
  18478. "</tr>\n"))
  18479. (setq head nil)
  18480. (setq field-buffer nil)))
  18481. ;; Ignore this line
  18482. (throw 'next-line t)))
  18483. ;; Break the line into fields and store the fields
  18484. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  18485. (if field-buffer
  18486. (setq field-buffer (mapcar
  18487. (lambda (x)
  18488. (concat x "<br/>" (pop fields)))
  18489. field-buffer))
  18490. (setq field-buffer fields))))
  18491. (setq html (concat html "</table>\n"))
  18492. html))
  18493. (defun org-format-table-table-html-using-table-generate-source (lines)
  18494. "Format a table into html, using `table-generate-source' from table.el.
  18495. This has the advantage that cell- or row-spanning is allowed.
  18496. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  18497. (require 'table)
  18498. (with-current-buffer (get-buffer-create " org-tmp1 ")
  18499. (erase-buffer)
  18500. (insert (mapconcat 'identity lines "\n"))
  18501. (goto-char (point-min))
  18502. (if (not (re-search-forward "|[^+]" nil t))
  18503. (error "Error processing table"))
  18504. (table-recognize-table)
  18505. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  18506. (table-generate-source 'html " org-tmp2 ")
  18507. (set-buffer " org-tmp2 ")
  18508. (buffer-substring (point-min) (point-max))))
  18509. (defun org-html-handle-time-stamps (s)
  18510. "Format time stamps in string S, or remove them."
  18511. (catch 'exit
  18512. (let (r b)
  18513. (while (string-match org-maybe-keyword-time-regexp s)
  18514. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  18515. ;; never export CLOCK
  18516. (throw 'exit ""))
  18517. (or b (setq b (substring s 0 (match-beginning 0))))
  18518. (if (not org-export-with-timestamps)
  18519. (setq r (concat r (substring s 0 (match-beginning 0)))
  18520. s (substring s (match-end 0)))
  18521. (setq r (concat
  18522. r (substring s 0 (match-beginning 0))
  18523. (if (match-end 1)
  18524. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  18525. (match-string 1 s)))
  18526. (format " @<span class=\"timestamp\">%s@</span>"
  18527. (substring
  18528. (org-translate-time (match-string 3 s)) 1 -1)))
  18529. s (substring s (match-end 0)))))
  18530. ;; Line break if line started and ended with time stamp stuff
  18531. (if (not r)
  18532. s
  18533. (setq r (concat r s))
  18534. (unless (string-match "\\S-" (concat b s))
  18535. (setq r (concat r "@<br/>")))
  18536. r))))
  18537. (defun org-html-protect (s)
  18538. ;; convert & to &amp;, < to &lt; and > to &gt;
  18539. (let ((start 0))
  18540. (while (string-match "&" s start)
  18541. (setq s (replace-match "&amp;" t t s)
  18542. start (1+ (match-beginning 0))))
  18543. (while (string-match "<" s)
  18544. (setq s (replace-match "&lt;" t t s)))
  18545. (while (string-match ">" s)
  18546. (setq s (replace-match "&gt;" t t s))))
  18547. s)
  18548. (defun org-export-cleanup-toc-line (s)
  18549. "Remove tags and time staps from lines going into the toc."
  18550. (if (string-match " +:[a-zA-Z0-9_@:]+: *$" s)
  18551. (setq s (replace-match "" t t s)))
  18552. (when org-export-remove-timestamps-from-toc
  18553. (while (string-match org-maybe-keyword-time-regexp s)
  18554. (setq s (replace-match "" t t s))))
  18555. (while (string-match org-bracket-link-regexp s)
  18556. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  18557. t t s)))
  18558. s)
  18559. (defun org-html-expand (string)
  18560. "Prepare STRING for HTML export. Applies all active conversions.
  18561. If there are links in the string, don't modify these."
  18562. (let* (m s l res)
  18563. (while (setq m (string-match org-bracket-link-regexp string))
  18564. (setq s (substring string 0 m)
  18565. l (match-string 0 string)
  18566. string (substring string (match-end 0)))
  18567. (push (org-html-do-expand s) res)
  18568. (push l res))
  18569. (push (org-html-do-expand string) res)
  18570. (apply 'concat (nreverse res))))
  18571. (defun org-html-do-expand (s)
  18572. "Apply all active conversions to translate special ASCII to HTML."
  18573. (setq s (org-html-protect s))
  18574. (if org-export-html-expand
  18575. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  18576. (setq s (replace-match "<\\1>" t nil s))))
  18577. (if org-export-with-emphasize
  18578. (setq s (org-export-html-convert-emphasize s)))
  18579. (if org-export-with-sub-superscripts
  18580. (setq s (org-export-html-convert-sub-super s)))
  18581. (if org-export-with-TeX-macros
  18582. (let ((start 0) wd ass)
  18583. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  18584. (setq wd (match-string 1 s))
  18585. (if (setq ass (assoc wd org-html-entities))
  18586. (setq s (replace-match (or (cdr ass)
  18587. (concat "&" (car ass) ";"))
  18588. t t s))
  18589. (setq start (+ start (length wd)))))))
  18590. s)
  18591. (defun org-create-multibrace-regexp (left right n)
  18592. "Create a regular expression which will match a balanced sexp.
  18593. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  18594. as single character strings.
  18595. The regexp returned will match the entire expression including the
  18596. delimiters. It will also define a single group which contains the
  18597. match except for the outermost delimiters. The maximum depth of
  18598. stacked delimiters is N. Escaping delimiters is not possible."
  18599. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  18600. (or "\\|")
  18601. (re nothing)
  18602. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  18603. (while (> n 1)
  18604. (setq n (1- n)
  18605. re (concat re or next)
  18606. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  18607. (concat left "\\(" re "\\)" right)))
  18608. (defvar org-match-substring-regexp
  18609. (concat
  18610. "\\([^\\]\\)\\([_^]\\)\\("
  18611. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  18612. "\\|"
  18613. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  18614. "\\|"
  18615. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  18616. "The regular expression matching a sub- or superscript.")
  18617. ;(let ((s "a\\_b"))
  18618. ; (and (string-match org-match-substring-regexp s)
  18619. ; (conca t (match-string 1 s) ":::" (match-string 2 s))))
  18620. (defun org-export-html-convert-sub-super (string)
  18621. "Convert sub- and superscripts in STRING to HTML."
  18622. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  18623. (while (string-match org-match-substring-regexp string s)
  18624. (if (and requireb (match-end 8))
  18625. (setq s (match-end 2))
  18626. (setq s (match-end 1)
  18627. key (if (string= (match-string 2 string) "_") "sub" "sup")
  18628. c (or (match-string 8 string)
  18629. (match-string 6 string)
  18630. (match-string 5 string))
  18631. string (replace-match
  18632. (concat (match-string 1 string)
  18633. "<" key ">" c "</" key ">")
  18634. t t string))))
  18635. (while (string-match "\\\\\\([_^]\\)" string)
  18636. (setq string (replace-match (match-string 1 string) t t string)))
  18637. string))
  18638. (defun org-export-html-convert-emphasize (string)
  18639. "Apply emphasis."
  18640. (let ((s 0))
  18641. (while (string-match org-emph-re string s)
  18642. (if (not (equal
  18643. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  18644. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  18645. (setq string (replace-match
  18646. (concat "\\1" (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  18647. "\\4" (nth 3 (assoc (match-string 3 string) org-emphasis-alist))
  18648. "\\5") t nil string))
  18649. (setq s (1+ s))))
  18650. string))
  18651. (defvar org-par-open nil)
  18652. (defun org-open-par ()
  18653. "Insert <p>, but first close previous paragraph if any."
  18654. (org-close-par-maybe)
  18655. (insert "\n<p>")
  18656. (setq org-par-open t))
  18657. (defun org-close-par-maybe ()
  18658. "Close paragraph if there is one open."
  18659. (when org-par-open
  18660. (insert "</p>")
  18661. (setq org-par-open nil)))
  18662. (defun org-close-li ()
  18663. "Close <li> if necessary."
  18664. (org-close-par-maybe)
  18665. (insert "</li>\n"))
  18666. (defvar body-only) ; dynamically scoped into this.
  18667. (defun org-html-level-start (level title umax with-toc head-count)
  18668. "Insert a new level in HTML export.
  18669. When TITLE is nil, just close all open levels."
  18670. (org-close-par-maybe)
  18671. (let ((l (1+ (max level umax))))
  18672. (while (<= l org-level-max)
  18673. (if (aref org-levels-open (1- l))
  18674. (progn
  18675. (org-html-level-close l)
  18676. (aset org-levels-open (1- l) nil)))
  18677. (setq l (1+ l)))
  18678. (when title
  18679. ;; If title is nil, this means this function is called to close
  18680. ;; all levels, so the rest is done only if title is given
  18681. (when (string-match "\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  18682. (setq title (replace-match
  18683. (if org-export-with-tags
  18684. (save-match-data
  18685. (concat
  18686. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  18687. (mapconcat 'identity (org-split-string
  18688. (match-string 1 title) ":")
  18689. "&nbsp;")
  18690. "</span>"))
  18691. "")
  18692. t t title)))
  18693. (if (> level umax)
  18694. (progn
  18695. (if (aref org-levels-open (1- level))
  18696. (progn
  18697. (org-close-li)
  18698. (insert "<li>" title "<br/>\n"))
  18699. (aset org-levels-open (1- level) t)
  18700. (org-close-par-maybe)
  18701. (insert "<ul>\n<li>" title "<br/>\n")))
  18702. (if (and org-export-with-section-numbers (not body-only))
  18703. (setq title (concat (org-section-number level) " " title)))
  18704. (setq level (+ level org-export-html-toplevel-hlevel -1))
  18705. (if with-toc
  18706. (insert (format "\n<h%d id=\"sec-%d\">%s</h%d>\n"
  18707. level head-count title level))
  18708. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  18709. (org-open-par)))))
  18710. (defun org-html-level-close (&rest args)
  18711. "Terminate one level in HTML export."
  18712. (org-close-li)
  18713. (insert "</ul>\n"))
  18714. ;;; iCalendar export
  18715. ;;;###autoload
  18716. (defun org-export-icalendar-this-file ()
  18717. "Export current file as an iCalendar file.
  18718. The iCalendar file will be located in the same directory as the Org-mode
  18719. file, but with extension `.ics'."
  18720. (interactive)
  18721. (org-export-icalendar nil buffer-file-name))
  18722. ;;;###autoload
  18723. (defun org-export-icalendar-all-agenda-files ()
  18724. "Export all files in `org-agenda-files' to iCalendar .ics files.
  18725. Each iCalendar file will be located in the same directory as the Org-mode
  18726. file, but with extension `.ics'."
  18727. (interactive)
  18728. (apply 'org-export-icalendar nil (org-agenda-files t)))
  18729. ;;;###autoload
  18730. (defun org-export-icalendar-combine-agenda-files ()
  18731. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  18732. The file is stored under the name `org-combined-agenda-icalendar-file'."
  18733. (interactive)
  18734. (apply 'org-export-icalendar t (org-agenda-files t)))
  18735. (defun org-export-icalendar (combine &rest files)
  18736. "Create iCalendar files for all elements of FILES.
  18737. If COMBINE is non-nil, combine all calendar entries into a single large
  18738. file and store it under the name `org-combined-agenda-icalendar-file'."
  18739. (save-excursion
  18740. (org-prepare-agenda-buffers files)
  18741. (let* ((dir (org-export-directory
  18742. :ical (list :publishing-directory
  18743. org-export-publishing-directory)))
  18744. file ical-file ical-buffer category started org-agenda-new-buffers)
  18745. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  18746. (when combine
  18747. (setq ical-file
  18748. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  18749. org-combined-agenda-icalendar-file
  18750. (expand-file-name org-combined-agenda-icalendar-file dir))
  18751. ical-buffer (org-get-agenda-file-buffer ical-file))
  18752. (set-buffer ical-buffer) (erase-buffer))
  18753. (while (setq file (pop files))
  18754. (catch 'nextfile
  18755. (org-check-agenda-file file)
  18756. (set-buffer (org-get-agenda-file-buffer file))
  18757. (unless combine
  18758. (setq ical-file (concat (file-name-as-directory dir)
  18759. (file-name-sans-extension
  18760. (file-name-nondirectory buffer-file-name))
  18761. ".ics"))
  18762. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  18763. (with-current-buffer ical-buffer (erase-buffer)))
  18764. (setq category (or org-category
  18765. (file-name-sans-extension
  18766. (file-name-nondirectory buffer-file-name))))
  18767. (if (symbolp category) (setq category (symbol-name category)))
  18768. (let ((standard-output ical-buffer))
  18769. (if combine
  18770. (and (not started) (setq started t)
  18771. (org-start-icalendar-file org-icalendar-combined-name))
  18772. (org-start-icalendar-file category))
  18773. (org-print-icalendar-entries combine)
  18774. (when (or (and combine (not files)) (not combine))
  18775. (org-finish-icalendar-file)
  18776. (set-buffer ical-buffer)
  18777. (save-buffer)
  18778. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  18779. (org-release-buffers org-agenda-new-buffers))))
  18780. (defvar org-after-save-iCalendar-file-hook nil
  18781. "Hook run after an iCalendar file has been saved.
  18782. The iCalendar buffer is still current when this hook is run.
  18783. A good way to use this is to tell a desktop calenndar application to re-read
  18784. the iCalendar file.")
  18785. (defun org-print-icalendar-entries (&optional combine)
  18786. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  18787. When COMBINE is non nil, add the category to each line."
  18788. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  18789. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  18790. (org-category-table (org-get-category-table))
  18791. (dts (org-ical-ts-to-string
  18792. (format-time-string (cdr org-time-stamp-formats) (current-time))
  18793. "DTSTART"))
  18794. hd ts ts2 state status (inc t) pos b sexp rrule
  18795. scheduledp deadlinep tmp pri category
  18796. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  18797. (save-excursion
  18798. (goto-char (point-min))
  18799. (while (re-search-forward re1 nil t)
  18800. (catch :skip
  18801. (org-agenda-skip)
  18802. (setq pos (match-beginning 0)
  18803. ts (match-string 0)
  18804. inc t
  18805. hd (org-get-heading)
  18806. category (org-get-category))
  18807. (if (looking-at re2)
  18808. (progn
  18809. (goto-char (match-end 0))
  18810. (setq ts2 (match-string 1) inc nil))
  18811. (setq ts2 ts
  18812. tmp (buffer-substring (max (point-min)
  18813. (- pos org-ds-keyword-length))
  18814. pos)
  18815. deadlinep (string-match org-deadline-regexp tmp)
  18816. scheduledp (string-match org-scheduled-regexp tmp)
  18817. ;; donep (org-entry-is-done-p)
  18818. ))
  18819. (if (or (string-match org-tr-regexp hd)
  18820. (string-match org-ts-regexp hd))
  18821. (setq hd (replace-match "" t t hd)))
  18822. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  18823. (setq rrule
  18824. (concat "\nRRULE:FREQ="
  18825. (cdr (assoc
  18826. (match-string 2 ts)
  18827. '(("d" . "DAILY")("w" . "WEEKLY")
  18828. ("m" . "MONTHLY")("y" . "YEARLY"))))
  18829. ";INTERVAL=" (match-string 1 ts)))
  18830. (setq rrule ""))
  18831. (if (string-match org-bracket-link-regexp hd)
  18832. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  18833. (match-string 1 hd))
  18834. t t hd)))
  18835. (if deadlinep (setq hd (concat "DL: " hd)))
  18836. (if scheduledp (setq hd (concat "S: " hd)))
  18837. (if (string-match "\\`<%%" ts)
  18838. (with-current-buffer sexp-buffer
  18839. (insert (substring ts 1 -1) " " hd "\n"))
  18840. (princ (format "BEGIN:VEVENT
  18841. %s
  18842. %s%s
  18843. SUMMARY:%s
  18844. CATEGORIES:%s
  18845. END:VEVENT\n"
  18846. (org-ical-ts-to-string ts "DTSTART")
  18847. (org-ical-ts-to-string ts2 "DTEND" inc)
  18848. rrule hd category)))))
  18849. (when (and org-icalendar-include-sexps
  18850. (condition-case nil (require 'icalendar) (error nil))
  18851. (fboundp 'icalendar-export-region))
  18852. ;; Get all the literal sexps
  18853. (goto-char (point-min))
  18854. (while (re-search-forward "^&?%%(" nil t)
  18855. (catch :skip
  18856. (org-agenda-skip)
  18857. (setq b (match-beginning 0))
  18858. (goto-char (1- (match-end 0)))
  18859. (forward-sexp 1)
  18860. (end-of-line 1)
  18861. (setq sexp (buffer-substring b (point)))
  18862. (with-current-buffer sexp-buffer
  18863. (insert sexp "\n"))
  18864. (princ (org-diary-to-ical-string sexp-buffer)))))
  18865. (when org-icalendar-include-todo
  18866. (goto-char (point-min))
  18867. (while (re-search-forward org-todo-line-regexp nil t)
  18868. (catch :skip
  18869. (org-agenda-skip)
  18870. (setq state (match-string 2))
  18871. (setq status (if (member state org-done-keywords)
  18872. "COMPLETED" "NEEDS-ACTION"))
  18873. (when (and state
  18874. (or (not (member state org-done-keywords))
  18875. (eq org-icalendar-include-todo 'all))
  18876. (not (member org-archive-tag (org-get-tags-at)))
  18877. )
  18878. (setq hd (match-string 3))
  18879. (if (string-match org-bracket-link-regexp hd)
  18880. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  18881. (match-string 1 hd))
  18882. t t hd)))
  18883. (if (string-match org-priority-regexp hd)
  18884. (setq pri (string-to-char (match-string 2 hd))
  18885. hd (concat (substring hd 0 (match-beginning 1))
  18886. (substring hd (match-end 1))))
  18887. (setq pri org-default-priority))
  18888. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  18889. (- org-lowest-priority org-highest-priority))))))
  18890. (princ (format "BEGIN:VTODO
  18891. %s
  18892. SUMMARY:%s
  18893. CATEGORIES:%s
  18894. SEQUENCE:1
  18895. PRIORITY:%d
  18896. STATUS:%s
  18897. END:VTODO\n"
  18898. dts hd category pri status)))))))))
  18899. (defun org-start-icalendar-file (name)
  18900. "Start an iCalendar file by inserting the header."
  18901. (let ((user user-full-name)
  18902. (name (or name "unknown"))
  18903. (timezone (cadr (current-time-zone))))
  18904. (princ
  18905. (format "BEGIN:VCALENDAR
  18906. VERSION:2.0
  18907. X-WR-CALNAME:%s
  18908. PRODID:-//%s//Emacs with Org-mode//EN
  18909. X-WR-TIMEZONE:%s
  18910. CALSCALE:GREGORIAN\n" name user timezone))))
  18911. (defun org-finish-icalendar-file ()
  18912. "Finish an iCalendar file by inserting the END statement."
  18913. (princ "END:VCALENDAR\n"))
  18914. (defun org-ical-ts-to-string (s keyword &optional inc)
  18915. "Take a time string S and convert it to iCalendar format.
  18916. KEYWORD is added in front, to make a complete line like DTSTART....
  18917. When INC is non-nil, increase the hour by two (if time string contains
  18918. a time), or the day by one (if it does not contain a time)."
  18919. (let ((t1 (org-parse-time-string s 'nodefault))
  18920. t2 fmt have-time time)
  18921. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  18922. (setq t2 t1 have-time t)
  18923. (setq t2 (org-parse-time-string s)))
  18924. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  18925. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  18926. (when inc
  18927. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  18928. (setq time (encode-time s mi h d m y)))
  18929. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  18930. (concat keyword (format-time-string fmt time))))
  18931. ;;; XOXO export
  18932. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  18933. (with-current-buffer buffer
  18934. (apply 'insert output)))
  18935. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  18936. (defun org-export-as-xoxo (&optional buffer)
  18937. "Export the org buffer as XOXO.
  18938. The XOXO buffer is named *xoxo-<source buffer name>*"
  18939. (interactive (list (current-buffer)))
  18940. ;; A quickie abstraction
  18941. ;; Output everything as XOXO
  18942. (with-current-buffer (get-buffer buffer)
  18943. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  18944. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  18945. (org-infile-export-plist)))
  18946. (filename (concat (file-name-as-directory
  18947. (org-export-directory :xoxo opt-plist))
  18948. (file-name-sans-extension
  18949. (file-name-nondirectory buffer-file-name))
  18950. ".html"))
  18951. (out (find-file-noselect filename))
  18952. (last-level 1)
  18953. (hanging-li nil))
  18954. ;; Check the output buffer is empty.
  18955. (with-current-buffer out (erase-buffer))
  18956. ;; Kick off the output
  18957. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  18958. (while (re-search-forward "^\\(\\*+\\) \\(.+\\)" (point-max) 't)
  18959. (let* ((hd (match-string-no-properties 1))
  18960. (level (length hd))
  18961. (text (concat
  18962. (match-string-no-properties 2)
  18963. (save-excursion
  18964. (goto-char (match-end 0))
  18965. (let ((str ""))
  18966. (catch 'loop
  18967. (while 't
  18968. (forward-line)
  18969. (if (looking-at "^[ \t]\\(.*\\)")
  18970. (setq str (concat str (match-string-no-properties 1)))
  18971. (throw 'loop str)))))))))
  18972. ;; Handle level rendering
  18973. (cond
  18974. ((> level last-level)
  18975. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  18976. ((< level last-level)
  18977. (dotimes (- (- last-level level) 1)
  18978. (if hanging-li
  18979. (org-export-as-xoxo-insert-into out "</li>\n"))
  18980. (org-export-as-xoxo-insert-into out "</ol>\n"))
  18981. (when hanging-li
  18982. (org-export-as-xoxo-insert-into out "</li>\n")
  18983. (setq hanging-li nil)))
  18984. ((equal level last-level)
  18985. (if hanging-li
  18986. (org-export-as-xoxo-insert-into out "</li>\n")))
  18987. )
  18988. (setq last-level level)
  18989. ;; And output the new li
  18990. (setq hanging-li 't)
  18991. (if (equal ?+ (elt text 0))
  18992. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  18993. (org-export-as-xoxo-insert-into out "<li>" text))))
  18994. ;; Finally finish off the ol
  18995. (dotimes (- last-level 1)
  18996. (if hanging-li
  18997. (org-export-as-xoxo-insert-into out "</li>\n"))
  18998. (org-export-as-xoxo-insert-into out "</ol>\n"))
  18999. ;; Finish the buffer off and clean it up.
  19000. (switch-to-buffer-other-window out)
  19001. (indent-region (point-min) (point-max) nil)
  19002. (save-buffer)
  19003. (goto-char (point-min))
  19004. )))
  19005. ;;;; Key bindings
  19006. ;; Make `C-c C-x' a prefix key
  19007. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  19008. ;; TAB key with modifiers
  19009. (org-defkey org-mode-map "\C-i" 'org-cycle)
  19010. (org-defkey org-mode-map [(tab)] 'org-cycle)
  19011. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  19012. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  19013. (org-defkey org-mode-map "\M-\t" 'org-complete)
  19014. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  19015. ;; The following line is necessary under Suse GNU/Linux
  19016. (unless (featurep 'xemacs)
  19017. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  19018. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  19019. (define-key org-mode-map (kbd "<backtab>") 'org-shifttab)
  19020. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  19021. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  19022. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  19023. ;; Cursor keys with modifiers
  19024. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  19025. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  19026. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  19027. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  19028. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  19029. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  19030. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  19031. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  19032. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  19033. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  19034. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  19035. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  19036. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  19037. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  19038. ;;; Extra keys for tty access.
  19039. ;; We only set them when really needed because otherwise the
  19040. ;; menus don't show the simple keys
  19041. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  19042. (not window-system))
  19043. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  19044. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  19045. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  19046. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  19047. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  19048. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  19049. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  19050. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  19051. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  19052. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  19053. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  19054. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  19055. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  19056. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  19057. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  19058. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  19059. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  19060. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  19061. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  19062. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  19063. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  19064. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  19065. ;; All the other keys
  19066. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  19067. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  19068. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  19069. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  19070. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  19071. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  19072. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  19073. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  19074. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  19075. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  19076. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  19077. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  19078. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  19079. (org-defkey org-mode-map "\C-c\C-w" 'org-check-deadlines)
  19080. (org-defkey org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  19081. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  19082. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  19083. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  19084. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  19085. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  19086. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  19087. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  19088. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  19089. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  19090. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  19091. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  19092. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  19093. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  19094. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  19095. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  19096. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  19097. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  19098. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  19099. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  19100. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  19101. (org-defkey org-mode-map "\C-c-" 'org-table-insert-hline)
  19102. (org-defkey org-mode-map "\C-c^" 'org-sort)
  19103. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  19104. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  19105. (org-defkey org-mode-map "\C-m" 'org-return)
  19106. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  19107. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  19108. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  19109. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  19110. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  19111. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  19112. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  19113. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  19114. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  19115. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  19116. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  19117. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  19118. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  19119. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  19120. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  19121. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  19122. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  19123. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  19124. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  19125. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  19126. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  19127. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  19128. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  19129. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  19130. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  19131. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  19132. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  19133. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  19134. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  19135. (when (featurep 'xemacs)
  19136. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  19137. (defsubst org-table-p () (org-at-table-p))
  19138. (defun org-self-insert-command (N)
  19139. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  19140. If the cursor is in a table looking at whitespace, the whitespace is
  19141. overwritten, and the table is not marked as requiring realignment."
  19142. (interactive "p")
  19143. (if (and (org-table-p)
  19144. (progn
  19145. ;; check if we blank the field, and if that triggers align
  19146. (and org-table-auto-blank-field
  19147. (member last-command
  19148. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  19149. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  19150. ;; got extra space, this field does not determine column width
  19151. (let (org-table-may-need-update) (org-table-blank-field))
  19152. ;; no extra space, this field may determine column width
  19153. (org-table-blank-field)))
  19154. t)
  19155. (eq N 1)
  19156. (looking-at "[^|\n]* |"))
  19157. (let (org-table-may-need-update)
  19158. (goto-char (1- (match-end 0)))
  19159. (delete-backward-char 1)
  19160. (goto-char (match-beginning 0))
  19161. (self-insert-command N))
  19162. (setq org-table-may-need-update t)
  19163. (self-insert-command N)))
  19164. (defun org-delete-backward-char (N)
  19165. "Like `delete-backward-char', insert whitespace at field end in tables.
  19166. When deleting backwards, in tables this function will insert whitespace in
  19167. front of the next \"|\" separator, to keep the table aligned. The table will
  19168. still be marked for re-alignment if the field did fill the entire column,
  19169. because, in this case the deletion might narrow the column."
  19170. (interactive "p")
  19171. (if (and (org-table-p)
  19172. (eq N 1)
  19173. (string-match "|" (buffer-substring (point-at-bol) (point)))
  19174. (looking-at ".*?|"))
  19175. (let ((pos (point))
  19176. (noalign (looking-at "[^|\n\r]* |"))
  19177. (c org-table-may-need-update))
  19178. (backward-delete-char N)
  19179. (skip-chars-forward "^|")
  19180. (insert " ")
  19181. (goto-char (1- pos))
  19182. ;; noalign: if there were two spaces at the end, this field
  19183. ;; does not determine the width of the column.
  19184. (if noalign (setq org-table-may-need-update c)))
  19185. (backward-delete-char N)))
  19186. (defun org-delete-char (N)
  19187. "Like `delete-char', but insert whitespace at field end in tables.
  19188. When deleting characters, in tables this function will insert whitespace in
  19189. front of the next \"|\" separator, to keep the table aligned. The table will
  19190. still be marked for re-alignment if the field did fill the entire column,
  19191. because, in this case the deletion might narrow the column."
  19192. (interactive "p")
  19193. (if (and (org-table-p)
  19194. (not (bolp))
  19195. (not (= (char-after) ?|))
  19196. (eq N 1))
  19197. (if (looking-at ".*?|")
  19198. (let ((pos (point))
  19199. (noalign (looking-at "[^|\n\r]* |"))
  19200. (c org-table-may-need-update))
  19201. (replace-match (concat
  19202. (substring (match-string 0) 1 -1)
  19203. " |"))
  19204. (goto-char pos)
  19205. ;; noalign: if there were two spaces at the end, this field
  19206. ;; does not determine the width of the column.
  19207. (if noalign (setq org-table-may-need-update c)))
  19208. (delete-char N))
  19209. (delete-char N)))
  19210. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  19211. (put 'org-self-insert-command 'delete-selection t)
  19212. (put 'orgtbl-self-insert-command 'delete-selection t)
  19213. (put 'org-delete-char 'delete-selection 'supersede)
  19214. (put 'org-delete-backward-char 'delete-selection 'supersede)
  19215. ;; Make `flyspell-mode' delay after some commands
  19216. (put 'org-self-insert-command 'flyspell-delayed t)
  19217. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  19218. (put 'org-delete-char 'flyspell-delayed t)
  19219. (put 'org-delete-backward-char 'flyspell-delayed t)
  19220. ;; How to do this: Measure non-white length of current string
  19221. ;; If equal to column width, we should realign.
  19222. (defun org-remap (map &rest commands)
  19223. "In MAP, remap the functions given in COMMANDS.
  19224. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  19225. (let (new old)
  19226. (while commands
  19227. (setq old (pop commands) new (pop commands))
  19228. (if (fboundp 'command-remapping)
  19229. (org-defkey map (vector 'remap old) new)
  19230. (substitute-key-definition old new map global-map)))))
  19231. (when (eq org-enable-table-editor 'optimized)
  19232. ;; If the user wants maximum table support, we need to hijack
  19233. ;; some standard editing functions
  19234. (org-remap org-mode-map
  19235. 'self-insert-command 'org-self-insert-command
  19236. 'delete-char 'org-delete-char
  19237. 'delete-backward-char 'org-delete-backward-char)
  19238. (org-defkey org-mode-map "|" 'org-force-self-insert))
  19239. (defun org-shiftcursor-error ()
  19240. "Throw an error because Shift-Cursor command was applied in wrong context."
  19241. (error "This command is active in special context like tables, headlines or timestamps"))
  19242. (defun org-shifttab (&optional arg)
  19243. "Global visibility cycling or move to previous table field.
  19244. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  19245. on context.
  19246. See the individual commands for more information."
  19247. (interactive "P")
  19248. (cond
  19249. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  19250. (arg (message "Content view to level: ")
  19251. (org-content (prefix-numeric-value arg))
  19252. (setq org-cycle-global-status 'overview))
  19253. (t (call-interactively 'org-global-cycle))))
  19254. (defun org-shiftmetaleft ()
  19255. "Promote subtree or delete table column.
  19256. Calls `org-promote-subtree', `org-outdent-item',
  19257. or `org-table-delete-column', depending on context.
  19258. See the individual commands for more information."
  19259. (interactive)
  19260. (cond
  19261. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  19262. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  19263. ((org-at-item-p) (call-interactively 'org-outdent-item))
  19264. (t (org-shiftcursor-error))))
  19265. (defun org-shiftmetaright ()
  19266. "Demote subtree or insert table column.
  19267. Calls `org-demote-subtree', `org-indent-item',
  19268. or `org-table-insert-column', depending on context.
  19269. See the individual commands for more information."
  19270. (interactive)
  19271. (cond
  19272. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  19273. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  19274. ((org-at-item-p) (call-interactively 'org-indent-item))
  19275. (t (org-shiftcursor-error))))
  19276. (defun org-shiftmetaup (&optional arg)
  19277. "Move subtree up or kill table row.
  19278. Calls `org-move-subtree-up' or `org-table-kill-row' or
  19279. `org-move-item-up' depending on context. See the individual commands
  19280. for more information."
  19281. (interactive "P")
  19282. (cond
  19283. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  19284. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  19285. ((org-at-item-p) (call-interactively 'org-move-item-up))
  19286. (t (org-shiftcursor-error))))
  19287. (defun org-shiftmetadown (&optional arg)
  19288. "Move subtree down or insert table row.
  19289. Calls `org-move-subtree-down' or `org-table-insert-row' or
  19290. `org-move-item-down', depending on context. See the individual
  19291. commands for more information."
  19292. (interactive "P")
  19293. (cond
  19294. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  19295. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  19296. ((org-at-item-p) (call-interactively 'org-move-item-down))
  19297. (t (org-shiftcursor-error))))
  19298. (defun org-metaleft (&optional arg)
  19299. "Promote heading or move table column to left.
  19300. Calls `org-do-promote' or `org-table-move-column', depending on context.
  19301. With no specific context, calls the Emacs default `backward-word'.
  19302. See the individual commands for more information."
  19303. (interactive "P")
  19304. (cond
  19305. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  19306. ((or (org-on-heading-p) (org-region-active-p))
  19307. (call-interactively 'org-do-promote))
  19308. ((org-at-item-p) (call-interactively 'org-outdent-item))
  19309. (t (call-interactively 'backward-word))))
  19310. (defun org-metaright (&optional arg)
  19311. "Demote subtree or move table column to right.
  19312. Calls `org-do-demote' or `org-table-move-column', depending on context.
  19313. With no specific context, calls the Emacs default `forward-word'.
  19314. See the individual commands for more information."
  19315. (interactive "P")
  19316. (cond
  19317. ((org-at-table-p) (call-interactively 'org-table-move-column))
  19318. ((or (org-on-heading-p) (org-region-active-p))
  19319. (call-interactively 'org-do-demote))
  19320. ((org-at-item-p) (call-interactively 'org-indent-item))
  19321. (t (call-interactively 'forward-word))))
  19322. (defun org-metaup (&optional arg)
  19323. "Move subtree up or move table row up.
  19324. Calls `org-move-subtree-up' or `org-table-move-row' or
  19325. `org-move-item-up', depending on context. See the individual commands
  19326. for more information."
  19327. (interactive "P")
  19328. (cond
  19329. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  19330. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  19331. ((org-at-item-p) (call-interactively 'org-move-item-up))
  19332. (t (org-shiftcursor-error))))
  19333. (defun org-metadown (&optional arg)
  19334. "Move subtree down or move table row down.
  19335. Calls `org-move-subtree-down' or `org-table-move-row' or
  19336. `org-move-item-down', depending on context. See the individual
  19337. commands for more information."
  19338. (interactive "P")
  19339. (cond
  19340. ((org-at-table-p) (call-interactively 'org-table-move-row))
  19341. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  19342. ((org-at-item-p) (call-interactively 'org-move-item-down))
  19343. (t (org-shiftcursor-error))))
  19344. (defun org-shiftup (&optional arg)
  19345. "Increase item in timestamp or increase priority of current headline.
  19346. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  19347. depending on context. See the individual commands for more information."
  19348. (interactive "P")
  19349. (cond
  19350. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up))
  19351. ((org-on-heading-p) (call-interactively 'org-priority-up))
  19352. ((org-at-item-p) (call-interactively 'org-previous-item))
  19353. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  19354. (defun org-shiftdown (&optional arg)
  19355. "Decrease item in timestamp or decrease priority of current headline.
  19356. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  19357. depending on context. See the individual commands for more information."
  19358. (interactive "P")
  19359. (cond
  19360. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down))
  19361. ((org-on-heading-p) (call-interactively 'org-priority-down))
  19362. (t (call-interactively 'org-next-item))))
  19363. (defun org-shiftright ()
  19364. "Next TODO keyword or timestamp one day later, depending on context."
  19365. (interactive)
  19366. (cond
  19367. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  19368. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  19369. (t (org-shiftcursor-error))))
  19370. (defun org-shiftleft ()
  19371. "Previous TODO keyword or timestamp one day earlier, depending on context."
  19372. (interactive)
  19373. (cond
  19374. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19375. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  19376. (t (org-shiftcursor-error))))
  19377. (defun org-shiftcontrolright ()
  19378. "Switch to next TODO set."
  19379. (interactive)
  19380. (cond
  19381. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  19382. (t (org-shiftcursor-error))))
  19383. (defun org-shiftcontrolleft ()
  19384. "Switch to previous TODO set."
  19385. (interactive)
  19386. (cond
  19387. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  19388. (t (org-shiftcursor-error))))
  19389. (defun org-ctrl-c-ret ()
  19390. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19391. (interactive)
  19392. (cond
  19393. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19394. (t (call-interactively 'org-insert-heading))))
  19395. (defun org-copy-special ()
  19396. "Copy region in table or copy current subtree.
  19397. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19398. See the individual commands for more information."
  19399. (interactive)
  19400. (call-interactively
  19401. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19402. (defun org-cut-special ()
  19403. "Cut region in table or cut current subtree.
  19404. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19405. See the individual commands for more information."
  19406. (interactive)
  19407. (call-interactively
  19408. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19409. (defun org-paste-special (arg)
  19410. "Paste rectangular region into table, or past subtree relative to level.
  19411. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19412. See the individual commands for more information."
  19413. (interactive "P")
  19414. (if (org-at-table-p)
  19415. (org-table-paste-rectangle)
  19416. (org-paste-subtree arg)))
  19417. (defun org-ctrl-c-ctrl-c (&optional arg)
  19418. "Set tags in headline, or update according to changed information at point.
  19419. This command does many different things, depending on context:
  19420. - If the cursor is in a headline, prompt for tags and insert them
  19421. into the current line, aligned to `org-tags-column'. When called
  19422. with prefix arg, realign all tags in the current buffer.
  19423. - If the cursor is in one of the special #+KEYWORD lines, this
  19424. triggers scanning the buffer for these lines and updating the
  19425. information.
  19426. - If the cursor is inside a table, realign the table. This command
  19427. works even if the automatic table editor has been turned off.
  19428. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19429. the entire table.
  19430. - If the cursor is inside a table created by the table.el package,
  19431. activate that table.
  19432. - If the current buffer is a remember buffer, close note and file it.
  19433. with a prefix argument, file it without further interaction to the default
  19434. location.
  19435. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  19436. links in this buffer.
  19437. - If the cursor is on a numbered item in a plain list, renumber the
  19438. ordered list."
  19439. (interactive "P")
  19440. (let ((org-enable-table-editor t))
  19441. (cond
  19442. ((or org-clock-overlays
  19443. org-occur-highlights
  19444. org-latex-fragment-image-overlays)
  19445. (org-remove-clock-overlays)
  19446. (org-remove-occur-highlights)
  19447. (org-remove-latex-fragment-image-overlays)
  19448. (message "Temporary highlights/overlays removed from current buffer"))
  19449. ((and (local-variable-p 'org-finish-function (current-buffer))
  19450. (fboundp org-finish-function))
  19451. (funcall org-finish-function))
  19452. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  19453. ((org-on-heading-p) (call-interactively 'org-set-tags))
  19454. ((org-at-table.el-p)
  19455. (require 'table)
  19456. (beginning-of-line 1)
  19457. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  19458. (call-interactively 'table-recognize-table))
  19459. ((org-at-table-p)
  19460. (org-table-maybe-eval-formula)
  19461. (if arg
  19462. (call-interactively 'org-table-recalculate)
  19463. (org-table-maybe-recalculate-line))
  19464. (call-interactively 'org-table-align))
  19465. ((org-at-item-checkbox-p)
  19466. (call-interactively 'org-toggle-checkbox))
  19467. ((org-at-item-p)
  19468. (call-interactively 'org-maybe-renumber-ordered-list))
  19469. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  19470. (cond
  19471. ((equal (match-string 1) "TBLFM")
  19472. ;; Recalculate the table before this line
  19473. (save-excursion
  19474. (beginning-of-line 1)
  19475. (skip-chars-backward " \r\n\t")
  19476. (if (org-at-table-p)
  19477. (org-call-with-arg 'org-table-recalculate t))))
  19478. (t
  19479. (call-interactively 'org-mode-restart))))
  19480. (t (error "C-c C-c can do nothing useful at this location.")))))
  19481. (defun org-mode-restart ()
  19482. "Restart Org-mode, to scan again for special lines.
  19483. Also updates the keyword regular expressions."
  19484. (interactive)
  19485. (let ((org-inhibit-startup t)) (org-mode))
  19486. (message "Org-mode restarted to refresh keyword and special line setup"))
  19487. (defun org-return ()
  19488. "Goto next table row or insert a newline.
  19489. Calls `org-table-next-row' or `newline', depending on context.
  19490. See the individual commands for more information."
  19491. (interactive)
  19492. (cond
  19493. ((org-at-table-p)
  19494. (org-table-justify-field-maybe)
  19495. (call-interactively 'org-table-next-row))
  19496. (t (newline))))
  19497. (defun org-meta-return (&optional arg)
  19498. "Insert a new heading or wrap a region in a table.
  19499. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  19500. See the individual commands for more information."
  19501. (interactive "P")
  19502. (cond
  19503. ((org-at-table-p)
  19504. (call-interactively 'org-table-wrap-region))
  19505. (t (call-interactively 'org-insert-heading))))
  19506. ;;; Menu entries
  19507. ;; Define the Org-mode menus
  19508. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19509. '("Tbl"
  19510. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  19511. ["Next Field" org-cycle (org-at-table-p)]
  19512. ["Previous Field" org-shifttab (org-at-table-p)]
  19513. ["Next Row" org-return (org-at-table-p)]
  19514. "--"
  19515. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19516. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19517. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19518. "--"
  19519. ("Column"
  19520. ["Move Column Left" org-metaleft (org-at-table-p)]
  19521. ["Move Column Right" org-metaright (org-at-table-p)]
  19522. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19523. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19524. ("Row"
  19525. ["Move Row Up" org-metaup (org-at-table-p)]
  19526. ["Move Row Down" org-metadown (org-at-table-p)]
  19527. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19528. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19529. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19530. "--"
  19531. ["Insert Hline" org-table-insert-hline (org-at-table-p)])
  19532. ("Rectangle"
  19533. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19534. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19535. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19536. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19537. "--"
  19538. ("Calculate"
  19539. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19540. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19541. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  19542. "--"
  19543. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19544. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19545. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19546. "--"
  19547. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19548. "--"
  19549. ["Sum Column/Rectangle" org-table-sum
  19550. (or (org-at-table-p) (org-region-active-p))]
  19551. ["Which Column?" org-table-current-column (org-at-table-p)])
  19552. ["Debug Formulas"
  19553. org-table-toggle-formula-debugger
  19554. :style toggle :selected org-table-formula-debug]
  19555. ["Show Col/Row Numbers"
  19556. org-table-toggle-coordinate-overlays
  19557. :style toggle :selected org-table-overlay-coordinates]
  19558. "--"
  19559. ["Create" org-table-create (and (not (org-at-table-p))
  19560. org-enable-table-editor)]
  19561. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19562. ["Import from File" org-table-import (not (org-at-table-p))]
  19563. ["Export to File" org-table-export (org-at-table-p)]
  19564. "--"
  19565. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19566. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19567. '("Org"
  19568. ("Show/Hide"
  19569. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  19570. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  19571. ["Sparse Tree" org-occur t]
  19572. ["Reveal Context" org-reveal t]
  19573. ["Show All" show-all t]
  19574. "--"
  19575. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19576. "--"
  19577. ["New Heading" org-insert-heading t]
  19578. ("Navigate Headings"
  19579. ["Up" outline-up-heading t]
  19580. ["Next" outline-next-visible-heading t]
  19581. ["Previous" outline-previous-visible-heading t]
  19582. ["Next Same Level" outline-forward-same-level t]
  19583. ["Previous Same Level" outline-backward-same-level t]
  19584. "--"
  19585. ["Jump" org-goto t]
  19586. "--"
  19587. ["C-a finds headline start"
  19588. (setq org-special-ctrl-a (not org-special-ctrl-a))
  19589. :style toggle :selected org-special-ctrl-a])
  19590. ("Edit Structure"
  19591. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  19592. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  19593. "--"
  19594. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  19595. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  19596. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19597. "--"
  19598. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  19599. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  19600. ["Demote Heading" org-metaright (not (org-at-table-p))]
  19601. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  19602. "--"
  19603. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  19604. "--"
  19605. ["Convert to odd levels" org-convert-to-odd-levels t]
  19606. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19607. ("Editing"
  19608. ["Emphasis..." org-emphasize t])
  19609. ("Archive"
  19610. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  19611. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  19612. ; :active t :keys "C-u C-c C-x C-a"]
  19613. ["Sparse trees open ARCHIVE trees"
  19614. (setq org-sparse-tree-open-archived-trees
  19615. (not org-sparse-tree-open-archived-trees))
  19616. :style toggle :selected org-sparse-tree-open-archived-trees]
  19617. ["Cycling opens ARCHIVE trees"
  19618. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  19619. :style toggle :selected org-cycle-open-archived-trees]
  19620. ["Agenda includes ARCHIVE trees"
  19621. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  19622. :style toggle :selected (not org-agenda-skip-archived-trees)]
  19623. "--"
  19624. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  19625. ; ["Check and Move Children" (org-archive-subtree '(4))
  19626. ; :active t :keys "C-u C-c C-x C-s"]
  19627. )
  19628. "--"
  19629. ("TODO Lists"
  19630. ["TODO/DONE/-" org-todo t]
  19631. ("Select keyword"
  19632. ["Next keyword" org-shiftright (org-on-heading-p)]
  19633. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  19634. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  19635. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  19636. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  19637. ["Show TODO Tree" org-show-todo-tree t]
  19638. ["Global TODO list" org-todo-list t]
  19639. "--"
  19640. ["Set Priority" org-priority t]
  19641. ["Priority Up" org-shiftup t]
  19642. ["Priority Down" org-shiftdown t]
  19643. "--"
  19644. ;; FIXME: why is this still here????
  19645. ; ["Insert Checkbox" org-insert-todo-heading (org-in-item-p)]
  19646. ; ["Toggle Checkbox" org-ctrl-c-ctrl-c (org-at-item-checkbox-p)]
  19647. ; ["Insert [n/m] cookie" (progn (insert "[/]") (org-update-checkbox-count))
  19648. ; (or (org-on-heading-p) (org-at-item-p))]
  19649. ; ["Insert [%] cookie" (progn (insert "[%]") (org-update-checkbox-count))
  19650. ; (or (org-on-heading-p) (org-at-item-p))]
  19651. ; ["Update Statistics" org-update-checkbox-count t]
  19652. )
  19653. ("Dates and Scheduling"
  19654. ["Timestamp" org-time-stamp t]
  19655. ["Timestamp (inactive)" org-time-stamp-inactive t]
  19656. ("Change Date"
  19657. ["1 Day Later" org-shiftright t]
  19658. ["1 Day Earlier" org-shiftleft t]
  19659. ["1 ... Later" org-shiftup t]
  19660. ["1 ... Earlier" org-shiftdown t])
  19661. ["Compute Time Range" org-evaluate-time-range t]
  19662. ["Schedule Item" org-schedule t]
  19663. ["Deadline" org-deadline t]
  19664. "--"
  19665. ["Custom time format" org-toggle-time-stamp-overlays
  19666. :style radio :selected org-display-custom-times]
  19667. "--"
  19668. ["Goto Calendar" org-goto-calendar t]
  19669. ["Date from Calendar" org-date-from-calendar t])
  19670. ("Logging work"
  19671. ["Clock in" org-clock-in t]
  19672. ["Clock out" org-clock-out t]
  19673. ["Clock cancel" org-clock-cancel t]
  19674. ["Display times" org-clock-display t]
  19675. ["Create clock table" org-clock-report t]
  19676. "--"
  19677. ["Record DONE time"
  19678. (progn (setq org-log-done (not org-log-done))
  19679. (message "Switching to %s will %s record a timestamp"
  19680. (car org-done-keywords)
  19681. (if org-log-done "automatically" "not")))
  19682. :style toggle :selected org-log-done])
  19683. "--"
  19684. ["Agenda Command..." org-agenda t]
  19685. ("File List for Agenda")
  19686. ("Special views current file"
  19687. ["TODO Tree" org-show-todo-tree t]
  19688. ["Check Deadlines" org-check-deadlines t]
  19689. ["Timeline" org-timeline t]
  19690. ["Tags Tree" org-tags-sparse-tree t])
  19691. "--"
  19692. ("Hyperlinks"
  19693. ["Store Link (Global)" org-store-link t]
  19694. ["Insert Link" org-insert-link t]
  19695. ["Follow Link" org-open-at-point t]
  19696. "--"
  19697. ["Next link" org-next-link t]
  19698. ["Previous link" org-previous-link t]
  19699. "--"
  19700. ["Descriptive Links"
  19701. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  19702. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  19703. ["Literal Links"
  19704. (progn
  19705. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  19706. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  19707. "--"
  19708. ["Export/Publish..." org-export t]
  19709. ("LaTeX"
  19710. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19711. :selected org-cdlatex-mode]
  19712. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19713. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19714. ["Modify math symbol" org-cdlatex-math-modify
  19715. (org-inside-LaTeX-fragment-p)]
  19716. ["Export LaTeX fragments as images"
  19717. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  19718. :style toggle :selected org-export-with-LaTeX-fragments])
  19719. "--"
  19720. ("Documentation"
  19721. ["Show Version" org-version t]
  19722. ["Info Documentation" org-info t])
  19723. ("Customize"
  19724. ["Browse Org Group" org-customize t]
  19725. "--"
  19726. ["Expand This Menu" org-create-customize-menu
  19727. (fboundp 'customize-menu-create)])
  19728. "--"
  19729. ["Refresh setup" org-mode-restart t]
  19730. ))
  19731. (defun org-info (&optional node)
  19732. "Read documentation for Org-mode in the info system.
  19733. With optional NODE, go directly to that node."
  19734. (interactive)
  19735. (require 'info)
  19736. (Info-goto-node (format "(org)%s" (or node ""))))
  19737. (defun org-install-agenda-files-menu ()
  19738. (let ((bl (buffer-list)))
  19739. (save-excursion
  19740. (while bl
  19741. (set-buffer (pop bl))
  19742. (if (org-mode-p) (setq bl nil)))
  19743. (when (org-mode-p)
  19744. (easy-menu-change
  19745. '("Org") "File List for Agenda"
  19746. (append
  19747. (list
  19748. ["Edit File List" (org-edit-agenda-file-list) t]
  19749. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19750. ["Remove Current File from List" org-remove-file t]
  19751. ["Cycle through agenda files" org-cycle-agenda-files t]
  19752. "--")
  19753. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19754. ;;;; Documentation
  19755. (defun org-customize ()
  19756. "Call the customize function with org as argument."
  19757. (interactive)
  19758. (customize-browse 'org))
  19759. (defun org-create-customize-menu ()
  19760. "Create a full customization menu for Org-mode, insert it into the menu."
  19761. (interactive)
  19762. (if (fboundp 'customize-menu-create)
  19763. (progn
  19764. (easy-menu-change
  19765. '("Org") "Customize"
  19766. `(["Browse Org group" org-customize t]
  19767. "--"
  19768. ,(customize-menu-create 'org)
  19769. ["Set" Custom-set t]
  19770. ["Save" Custom-save t]
  19771. ["Reset to Current" Custom-reset-current t]
  19772. ["Reset to Saved" Custom-reset-saved t]
  19773. ["Reset to Standard Settings" Custom-reset-standard t]))
  19774. (message "\"Org\"-menu now contains full customization menu"))
  19775. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19776. ;;;; Miscellaneous stuff
  19777. ;;; Generally useful functions
  19778. (defun org-context ()
  19779. "Return a list of contexts of the current cursor position.
  19780. If several contexts apply, all are returned.
  19781. Each context entry is a list with a symbol naming the context, and
  19782. two positions indicating start and end of the context. Possible
  19783. contexts are:
  19784. :headline anywhere in a headline
  19785. :headline-stars on the leading stars in a headline
  19786. :todo-keyword on a TODO keyword (including DONE) in a headline
  19787. :tags on the TAGS in a headline
  19788. :priority on the priority cookie in a headline
  19789. :item on the first line of a plain list item
  19790. :item-bullet on the bullet/number of a plain list item
  19791. :checkbox on the checkbox in a plain list item
  19792. :table in an org-mode table
  19793. :table-special on a special filed in a table
  19794. :table-table in a table.el table
  19795. :link on a hyperlink
  19796. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  19797. :target on a <<target>>
  19798. :radio-target on a <<<radio-target>>>
  19799. :latex-fragment on a LaTeX fragment
  19800. :latex-preview on a LaTeX fragment with overlayed preview image
  19801. This function expects the position to be visible because it uses font-lock
  19802. faces as a help to recognize the following contexts: :table-special, :link,
  19803. and :keyword."
  19804. (let* ((f (get-text-property (point) 'face))
  19805. (faces (if (listp f) f (list f)))
  19806. (p (point)) clist o)
  19807. ;; First the large context
  19808. (cond
  19809. ((org-on-heading-p t)
  19810. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19811. (when (progn
  19812. (beginning-of-line 1)
  19813. (looking-at org-todo-line-tags-regexp))
  19814. (push (org-point-in-group p 1 :headline-stars) clist)
  19815. (push (org-point-in-group p 2 :todo-keyword) clist)
  19816. (push (org-point-in-group p 4 :tags) clist))
  19817. (goto-char p)
  19818. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  19819. (if (looking-at "\\[#[A-Z0-9]\\]")
  19820. (push (org-point-in-group p 0 :priority) clist)))
  19821. ((org-at-item-p)
  19822. (push (org-point-in-group p 2 :item-bullet) clist)
  19823. (push (list :item (point-at-bol)
  19824. (save-excursion (org-end-of-item) (point)))
  19825. clist)
  19826. (and (org-at-item-checkbox-p)
  19827. (push (org-point-in-group p 0 :checkbox) clist)))
  19828. ((org-at-table-p)
  19829. (push (list :table (org-table-begin) (org-table-end)) clist)
  19830. (if (memq 'org-formula faces)
  19831. (push (list :table-special
  19832. (previous-single-property-change p 'face)
  19833. (next-single-property-change p 'face)) clist)))
  19834. ((org-at-table-p 'any)
  19835. (push (list :table-table) clist)))
  19836. (goto-char p)
  19837. ;; Now the small context
  19838. (cond
  19839. ((org-at-timestamp-p)
  19840. (push (org-point-in-group p 0 :timestamp) clist))
  19841. ((memq 'org-link faces)
  19842. (push (list :link
  19843. (previous-single-property-change p 'face)
  19844. (next-single-property-change p 'face)) clist))
  19845. ((memq 'org-special-keyword faces)
  19846. (push (list :keyword
  19847. (previous-single-property-change p 'face)
  19848. (next-single-property-change p 'face)) clist))
  19849. ((org-on-target-p)
  19850. (push (org-point-in-group p 0 :target) clist)
  19851. (goto-char (1- (match-beginning 0)))
  19852. (if (looking-at org-radio-target-regexp)
  19853. (push (org-point-in-group p 0 :radio-target) clist))
  19854. (goto-char p))
  19855. ((setq o (car (delq nil
  19856. (mapcar
  19857. (lambda (x)
  19858. (if (memq x org-latex-fragment-image-overlays) x))
  19859. (org-overlays-at (point))))))
  19860. (push (list :latex-fragment
  19861. (org-overlay-start o) (org-overlay-end o)) clist)
  19862. (push (list :latex-preview
  19863. (org-overlay-start o) (org-overlay-end o)) clist))
  19864. ((org-inside-LaTeX-fragment-p)
  19865. ;; FIXME: positions wrong.
  19866. (push (list :latex-fragment (point) (point)) clist)))
  19867. (setq clist (nreverse (delq nil clist)))
  19868. clist))
  19869. ;; FIXME Compare with at-regexp-p
  19870. (defun org-in-regexp (re &optional nlines visually)
  19871. "Check if point is inside a match of regexp.
  19872. Normally only the current line is checked, but you can include NLINES extra
  19873. lines both before and after point into the search.
  19874. If VISUALLY is set, require that the cursor is not after the match but
  19875. really on, so that the block visually is on the match."
  19876. (catch 'exit
  19877. (let ((pos (point))
  19878. (eol (point-at-eol (+ 1 (or nlines 0))))
  19879. (inc (if visually 1 0)))
  19880. (save-excursion
  19881. (beginning-of-line (- 1 (or nlines 0)))
  19882. (while (re-search-forward re eol t)
  19883. (if (and (<= (match-beginning 0) pos)
  19884. (>= (+ inc (match-end 0)) pos))
  19885. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19886. (defun org-at-regexp-p (regexp)
  19887. "Is point inside a match of REGEXP in the current line?"
  19888. (catch 'exit
  19889. (save-excursion
  19890. (let ((pos (point)) (end (point-at-eol)))
  19891. (beginning-of-line 1)
  19892. (while (re-search-forward regexp end t)
  19893. (if (and (<= (match-beginning 0) pos)
  19894. (>= (match-end 0) pos))
  19895. (throw 'exit t)))
  19896. nil))))
  19897. (defun org-uniquify (list)
  19898. "Remove duplicate elements from LIST."
  19899. (let (res)
  19900. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19901. res))
  19902. (defun org-delete-all (elts list)
  19903. "Remove all elements in ELTS from LIST."
  19904. (while elts
  19905. (setq list (delete (pop elts) list)))
  19906. list)
  19907. (defun org-point-in-group (point group &optional context)
  19908. "Check if POINT is in match-group GROUP.
  19909. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19910. match. If the match group does ot exist or point is not inside it,
  19911. return nil."
  19912. (and (match-beginning group)
  19913. (>= point (match-beginning group))
  19914. (<= point (match-end group))
  19915. (if context
  19916. (list context (match-beginning group) (match-end group))
  19917. t)))
  19918. (defun org-combine-plists (&rest plists)
  19919. "Create a single property list from all plists in PLISTS.
  19920. The process starts by copying the first list, and then setting properties
  19921. from the other lists. Settings in the last list are the most significant
  19922. ones and overrule settings in the other lists."
  19923. (let ((rtn (copy-sequence (pop plists)))
  19924. p v ls)
  19925. (while plists
  19926. (setq ls (pop plists))
  19927. (while ls
  19928. (setq p (pop ls) v (pop ls))
  19929. (setq rtn (plist-put rtn p v))))
  19930. rtn))
  19931. (defun org-move-line-down (arg)
  19932. "Move the current line down. With prefix argument, move it past ARG lines."
  19933. (interactive "p")
  19934. (let ((col (current-column))
  19935. beg end pos)
  19936. (beginning-of-line 1) (setq beg (point))
  19937. (beginning-of-line 2) (setq end (point))
  19938. (beginning-of-line (+ 1 arg))
  19939. (setq pos (move-marker (make-marker) (point)))
  19940. (insert (delete-and-extract-region beg end))
  19941. (goto-char pos)
  19942. (move-to-column col)))
  19943. (defun org-move-line-up (arg)
  19944. "Move the current line up. With prefix argument, move it past ARG lines."
  19945. (interactive "p")
  19946. (let ((col (current-column))
  19947. beg end pos)
  19948. (beginning-of-line 1) (setq beg (point))
  19949. (beginning-of-line 2) (setq end (point))
  19950. (beginning-of-line (- arg))
  19951. (setq pos (move-marker (make-marker) (point)))
  19952. (insert (delete-and-extract-region beg end))
  19953. (goto-char pos)
  19954. (move-to-column col)))
  19955. (defun org-replace-escapes (string table)
  19956. "Replace %-escapes in STRING with values in TABLE.
  19957. TABLE is an association list with keys line \"%a\" and string values.
  19958. The sequences in STRING may contain normal field width and padding information,
  19959. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19960. so values can contain further %-escapes if they are define later in TABLE."
  19961. (let ((case-fold-search nil)
  19962. e re rpl)
  19963. (while (setq e (pop table))
  19964. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19965. (while (string-match re string)
  19966. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19967. (cdr e)))
  19968. (setq string (replace-match rpl t t string))))
  19969. string))
  19970. (defun org-sublist (list start end)
  19971. "Return a section of LIST, from START to END.
  19972. Counting starts at 1."
  19973. (let (rtn (c start))
  19974. (setq list (nthcdr (1- start) list))
  19975. (while (and list (<= c end))
  19976. (push (pop list) rtn)
  19977. (setq c (1+ c)))
  19978. (nreverse rtn)))
  19979. (defun org-find-base-buffer-visiting (file)
  19980. "Like `find-buffer-visiting' but alway return the base buffer and
  19981. not an indirect buffer"
  19982. (let ((buf (find-buffer-visiting file)))
  19983. (or (buffer-base-buffer buf) buf)))
  19984. (defun org-image-file-name-regexp ()
  19985. "Return regexp matching the file names of images."
  19986. (if (fboundp 'image-file-name-regexp)
  19987. (image-file-name-regexp)
  19988. (let ((image-file-name-extensions
  19989. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19990. "xbm" "xpm" "pbm" "pgm" "ppm")))
  19991. (concat "\\."
  19992. (regexp-opt (nconc (mapcar 'upcase
  19993. image-file-name-extensions)
  19994. image-file-name-extensions)
  19995. t)
  19996. "\\'"))))
  19997. (defun org-file-image-p (file)
  19998. "Return non-nil if FILE is an image."
  19999. (save-match-data
  20000. (string-match (org-image-file-name-regexp) file)))
  20001. ;;; Paragraph filling stuff.
  20002. ;; We want this to be just right, so use the full arsenal.
  20003. (defun org-indent-line-function ()
  20004. "Indent line like previous, but further if previous was headline or item."
  20005. (interactive)
  20006. (let* ((pos (point))
  20007. (itemp (org-at-item-p))
  20008. column bpos bcol tpos tcol bullet btype bullet-type)
  20009. ;; Find the previous relevant line
  20010. (beginning-of-line 1)
  20011. (cond
  20012. ((looking-at "#") (setq column 0))
  20013. (t
  20014. (beginning-of-line 0)
  20015. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  20016. (beginning-of-line 0))
  20017. (cond
  20018. ((looking-at "\\*+[ \t]+")
  20019. (goto-char (match-end 0))
  20020. (setq column (current-column)))
  20021. ((org-in-item-p)
  20022. (org-beginning-of-item)
  20023. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  20024. (setq bpos (match-beginning 1) tpos (match-end 0)
  20025. bcol (progn (goto-char bpos) (current-column))
  20026. tcol (progn (goto-char tpos) (current-column))
  20027. bullet (match-string 1)
  20028. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  20029. (if (not itemp)
  20030. (setq column tcol)
  20031. (goto-char pos)
  20032. (beginning-of-line 1)
  20033. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  20034. (setq bullet (match-string 1)
  20035. btype (if (string-match "[0-9]" bullet) "n" bullet))
  20036. (setq column (if (equal btype bullet-type) bcol tcol))))
  20037. (t (setq column (org-get-indentation))))))
  20038. (goto-char pos)
  20039. (if (<= (current-column) (current-indentation))
  20040. (indent-line-to column)
  20041. (save-excursion (indent-line-to column)))))
  20042. (defun org-set-autofill-regexps ()
  20043. (interactive)
  20044. ;; In the paragraph separator we include headlines, because filling
  20045. ;; text in a line directly attached to a headline would otherwise
  20046. ;; fill the headline as well.
  20047. (org-set-local 'comment-start-skip "^#+[ \t]*")
  20048. (org-set-local 'paragraph-separate "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
  20049. ;; FIXME!!!!!!! (org-set-local 'paragraph-separate "\f\\|[ ]*$")
  20050. ;; The paragraph starter includes hand-formatted lists.
  20051. (org-set-local 'paragraph-start
  20052. "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  20053. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  20054. ;; But only if the user has not turned off tables or fixed-width regions
  20055. (org-set-local
  20056. 'auto-fill-inhibit-regexp
  20057. (concat "\\*\\|#\\+"
  20058. "\\|[ \t]*" org-keyword-time-regexp
  20059. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  20060. (concat
  20061. "\\|[ \t]*["
  20062. (if org-enable-table-editor "|" "")
  20063. (if org-enable-fixed-width-editor ":" "")
  20064. "]"))))
  20065. ;; We use our own fill-paragraph function, to make sure that tables
  20066. ;; and fixed-width regions are not wrapped. That function will pass
  20067. ;; through to `fill-paragraph' when appropriate.
  20068. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20069. ; Adaptive filling: To get full control, first make sure that
  20070. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  20071. (org-set-local 'adaptive-fill-regexp "\000")
  20072. (org-set-local 'adaptive-fill-function
  20073. 'org-adaptive-fill-function))
  20074. (defun org-fill-paragraph (&optional justify)
  20075. "Re-align a table, pass through to fill-paragraph if no table."
  20076. (let ((table-p (org-at-table-p))
  20077. (table.el-p (org-at-table.el-p)))
  20078. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  20079. (table.el-p t) ; skip table.el tables
  20080. (table-p (org-table-align) t) ; align org-mode tables
  20081. (t nil)))) ; call paragraph-fill
  20082. ;; For reference, this is the default value of adaptive-fill-regexp
  20083. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  20084. (defun org-adaptive-fill-function ()
  20085. "Return a fill prefix for org-mode files.
  20086. In particular, this makes sure hanging paragraphs for hand-formatted lists
  20087. work correctly."
  20088. (cond ((looking-at "#[ \t]+")
  20089. (match-string 0))
  20090. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  20091. (save-excursion
  20092. (goto-char (match-end 0))
  20093. (make-string (current-column) ?\ )))
  20094. (t nil)))
  20095. ;;;; Functions extending outline functionality
  20096. ;; C-a should go to the beginning of a *visible* line, also in the
  20097. ;; new outline.el. I guess this should be patched into Emacs?
  20098. (defun org-beginning-of-line ()
  20099. "Go to the beginning of the current line. If that is invisible, continue
  20100. to a visible line beginning. This makes the function of C-a more intuitive."
  20101. (interactive)
  20102. (let ((pos (point)))
  20103. (beginning-of-line 1)
  20104. (if (bobp)
  20105. nil
  20106. (backward-char 1)
  20107. (if (org-invisible-p)
  20108. (while (and (not (bobp)) (org-invisible-p))
  20109. (backward-char 1)
  20110. (beginning-of-line 1))
  20111. (forward-char 1)))
  20112. (when (and org-special-ctrl-a (looking-at org-todo-line-regexp)
  20113. (= (char-after (match-end 1)) ?\ ))
  20114. (goto-char
  20115. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  20116. ((= pos (point)) (match-beginning 3))
  20117. (t (point)))))))
  20118. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20119. (defun org-invisible-p ()
  20120. "Check if point is at a character currently not visible."
  20121. ;; Early versions of noutline don't have `outline-invisible-p'.
  20122. (if (fboundp 'outline-invisible-p)
  20123. (outline-invisible-p)
  20124. (get-char-property (point) 'invisible)))
  20125. (defun org-invisible-p2 ()
  20126. "Check if point is at a character currently not visible."
  20127. (save-excursion
  20128. (if (and (eolp) (not (bobp))) (backward-char 1))
  20129. ;; Early versions of noutline don't have `outline-invisible-p'.
  20130. (if (fboundp 'outline-invisible-p)
  20131. (outline-invisible-p)
  20132. (get-char-property (point) 'invisible))))
  20133. (defalias 'org-back-to-heading 'outline-back-to-heading)
  20134. (defalias 'org-on-heading-p 'outline-on-heading-p)
  20135. (defalias 'org-at-heading-p 'outline-on-heading-p)
  20136. (defun org-at-heading-or-item-p ()
  20137. (or (org-on-heading-p) (org-at-item-p)))
  20138. (defun org-on-target-p ()
  20139. (or (org-in-regexp org-radio-target-regexp)
  20140. (org-in-regexp org-target-regexp)))
  20141. (defun org-up-heading-all (arg)
  20142. "Move to the heading line of which the present line is a subheading.
  20143. This function considers both visible and invisible heading lines.
  20144. With argument, move up ARG levels."
  20145. (if (fboundp 'outline-up-heading-all)
  20146. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20147. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20148. (defun org-goto-sibling (&optional previous)
  20149. "Goto the next sibling, even if it is invisible.
  20150. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20151. when a sibling was found. When none is found, return nil and don't
  20152. move point."
  20153. (let ((fun (if previous 're-search-backward 're-search-forward))
  20154. (pos (point))
  20155. (re (concat "^" outline-regexp))
  20156. level l)
  20157. (org-back-to-heading t)
  20158. (setq level (funcall outline-level))
  20159. (catch 'exit
  20160. (or previous (forward-char 1))
  20161. (while (funcall fun re nil t)
  20162. (setq l (funcall outline-level))
  20163. (when (< l level) (goto-char pos) (throw 'exit nil))
  20164. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20165. (goto-char pos)
  20166. nil)))
  20167. (defun org-show-siblings ()
  20168. "Show all siblings of the current headline."
  20169. (save-excursion
  20170. (while (org-goto-sibling) (org-flag-heading nil)))
  20171. (save-excursion
  20172. (while (org-goto-sibling 'previous)
  20173. (org-flag-heading nil))))
  20174. (defun org-show-hidden-entry ()
  20175. "Show an entry where even the heading is hidden."
  20176. (save-excursion
  20177. (org-show-entry)))
  20178. (defun org-flag-heading (flag &optional entry)
  20179. "Flag the current heading. FLAG non-nil means make invisible.
  20180. When ENTRY is non-nil, show the entire entry."
  20181. (save-excursion
  20182. (org-back-to-heading t)
  20183. ;; Check if we should show the entire entry
  20184. (if entry
  20185. (progn
  20186. (org-show-entry)
  20187. (save-excursion
  20188. (and (outline-next-heading)
  20189. (org-flag-heading nil))))
  20190. (outline-flag-region (max 1 (1- (point)))
  20191. (save-excursion (outline-end-of-heading) (point))
  20192. flag))))
  20193. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  20194. ;; This is an exact copy of the original function, but it uses
  20195. ;; `org-back-to-heading', to make it work also in invisible
  20196. ;; trees. And is uses an invisible-OK argument.
  20197. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20198. (org-back-to-heading invisible-OK)
  20199. (let ((first t)
  20200. (level (funcall outline-level)))
  20201. (while (and (not (eobp))
  20202. (or first (> (funcall outline-level) level)))
  20203. (setq first nil)
  20204. (outline-next-heading))
  20205. (unless to-heading
  20206. (if (memq (preceding-char) '(?\n ?\^M))
  20207. (progn
  20208. ;; Go to end of line before heading
  20209. (forward-char -1)
  20210. (if (memq (preceding-char) '(?\n ?\^M))
  20211. ;; leave blank line before heading
  20212. (forward-char -1))))))
  20213. (point))
  20214. (defun org-show-subtree ()
  20215. "Show everything after this heading at deeper levels."
  20216. (outline-flag-region
  20217. (point)
  20218. (save-excursion
  20219. (outline-end-of-subtree) (outline-next-heading) (point))
  20220. nil))
  20221. (defun org-show-entry ()
  20222. "Show the body directly following this heading.
  20223. Show the heading too, if it is currently invisible."
  20224. (interactive)
  20225. (save-excursion
  20226. (org-back-to-heading t)
  20227. (outline-flag-region
  20228. (max 1 (1- (point)))
  20229. (save-excursion
  20230. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  20231. (or (match-beginning 1) (point-max)))
  20232. nil)))
  20233. (defun org-make-options-regexp (kwds)
  20234. "Make a regular expression for keyword lines."
  20235. (concat
  20236. "^"
  20237. "#?[ \t]*\\+\\("
  20238. (mapconcat 'regexp-quote kwds "\\|")
  20239. "\\):[ \t]*"
  20240. "\\(.+\\)"))
  20241. ;; Make isearch reveal the necessary context
  20242. (defun org-isearch-end ()
  20243. "Reveal context after isearch exits."
  20244. (when isearch-success ; only if search was successful
  20245. (if (featurep 'xemacs)
  20246. ;; Under XEmacs, the hook is run in the correct place,
  20247. ;; we directly show the context.
  20248. (org-show-context 'isearch)
  20249. ;; In Emacs the hook runs *before* restoring the overlays.
  20250. ;; So we have to use a one-time post-command-hook to do this.
  20251. ;; (Emacs 22 has a special variable, see function `org-mode')
  20252. (unless (and (boundp 'isearch-mode-end-hook-quit)
  20253. isearch-mode-end-hook-quit)
  20254. ;; Only when the isearch was not quitted.
  20255. (org-add-hook 'post-command-hook 'org-isearch-post-command
  20256. 'append 'local)))))
  20257. (defun org-isearch-post-command ()
  20258. "Remove self from hook, and show context."
  20259. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  20260. (org-show-context 'isearch))
  20261. ;;;; Address problems with some other packages
  20262. ;; Make flyspell not check words in links, to not mess up our keymap
  20263. (defun org-mode-flyspell-verify ()
  20264. "Don't let flyspell put overlays at active buttons."
  20265. (not (get-text-property (point) 'keymap)))
  20266. ;; Make `bookmark-jump' show the jump location if it was hidden.
  20267. (eval-after-load "bookmark"
  20268. '(if (boundp 'bookmark-after-jump-hook)
  20269. ;; We can use the hook
  20270. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  20271. ;; Hook not available, use advice
  20272. (defadvice bookmark-jump (after org-make-visible activate)
  20273. "Make the position visible."
  20274. (org-bookmark-jump-unhide))))
  20275. (defun org-bookmark-jump-unhide ()
  20276. "Unhide the current position, to show the bookmark location."
  20277. (and (org-mode-p)
  20278. (or (org-invisible-p)
  20279. (save-excursion (goto-char (max (point-min) (1- (point))))
  20280. (org-invisible-p)))
  20281. (org-show-context 'bookmark-jump)))
  20282. ;; Make session.el ignore our circular variable
  20283. (eval-after-load "session"
  20284. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  20285. ;;;; Experimental code
  20286. (defun org-closed-in-range ()
  20287. "Sparse tree of items closed in a certain time range.
  20288. Still experimental, may disappear in the furture."
  20289. (interactive)
  20290. ;; Get the time interval from the user.
  20291. (let* ((time1 (time-to-seconds
  20292. (org-read-date nil 'to-time nil "Starting date: ")))
  20293. (time2 (time-to-seconds
  20294. (org-read-date nil 'to-time nil "End date:")))
  20295. ;; callback function
  20296. (callback (lambda ()
  20297. (let ((time
  20298. (time-to-seconds
  20299. (apply 'encode-time
  20300. (org-parse-time-string
  20301. (match-string 1))))))
  20302. ;; check if time in interval
  20303. (and (>= time time1) (<= time time2))))))
  20304. ;; make tree, check each match with the callback
  20305. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  20306. (defun test ()
  20307. (interactive)
  20308. (message "hihi: %s" (org-item-indent-positions)))
  20309. ;;;; Finish up
  20310. (provide 'org)
  20311. (run-hooks 'org-load-hook)
  20312. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  20313. ;;; org.el ends here