org.el 497 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596
  1. ;;; org.el --- Outline-based notes management and organize
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006 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.19b
  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 a NOTES file that contains
  32. ;; information about projects as plain text. Org-mode is implemented on top
  33. ;; of outline-mode - ideal to keep the content of large files well structured.
  34. ;; It supports ToDo items, deadlines and time stamps, which can be extracted
  35. ;; to create a daily/weekly agenda that also integrates the diary of the Emacs
  36. ;; calendar. Tables are easily created with a built-in table editor. Plain
  37. ;; text URL-like links connect to websites, emails (VM, RMAIL, WANDERLUST),
  38. ;; Usenet messages (Gnus), BBDB entries, and any files related to the
  39. ;; projects. For printing and sharing of notes, an Org-mode file (or a part
  40. ;; of it) can be exported as a structured ASCII file, or as HTML.
  41. ;;
  42. ;; Installation
  43. ;; ------------
  44. ;; If Org-mode is part of the Emacs distribution or an XEmacs package, you
  45. ;; only need to copy the following lines to your .emacs file. The last two
  46. ;; lines define *global* keys for the commands `org-store-link' and
  47. ;; `org-agenda' - please choose suitable keys yourself.
  48. ;;
  49. ;; (add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
  50. ;; (define-key global-map "\C-cl" 'org-store-link)
  51. ;; (define-key global-map "\C-ca" 'org-agenda)
  52. ;;
  53. ;; If you have downloaded Org-mode from the Web, you must byte-compile
  54. ;; org.el and put it on your load path. In addition to the Emacs Lisp
  55. ;; lines above, you also need to add the following lines to .emacs:
  56. ;;
  57. ;; (autoload 'org-mode "org" "Org mode" t)
  58. ;; (autoload 'org-diary "org" "Diary entries from Org mode")
  59. ;; (autoload 'org-agenda "org" "Multi-file agenda from Org mode" t)
  60. ;; (autoload 'org-store-link "org" "Store a link to the current location" t)
  61. ;; (autoload 'orgtbl-mode "org" "Org tables as a minor mode" t)
  62. ;; (autoload 'turn-on-orgtbl "org" "Org tables as a minor mode")
  63. ;;
  64. ;; This setup will put all files with extension ".org" into Org-mode. As
  65. ;; an alternative, make the first line of a file look like this:
  66. ;;
  67. ;; MY PROJECTS -*- mode: org; -*-
  68. ;;
  69. ;; which will select Org-mode for this buffer no matter what the file's
  70. ;; name is.
  71. ;;
  72. ;; Documentation
  73. ;; -------------
  74. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  75. ;; distribution also contains a PDF version of it. At the homepage of
  76. ;; Org-mode, you can read the same text online as HTML. There is also an
  77. ;; excellent reference card made by Philip Rooke. This card can be found
  78. ;; in the etc/ directory of Emacs 22.
  79. ;;
  80. ;; Changes since version 4.00:
  81. ;; ---------------------------
  82. ;; Version 4.20
  83. ;; - Links use now the [[link][description]] format by default.
  84. ;; When inserting links, the user is prompted for a description.
  85. ;; - If a link has a description, only the description is displayed
  86. ;; the link part is hidden. Use C-c C-l to edit the link part.
  87. ;; - TAGS are now bold, but in the same color as the headline.
  88. ;; - Bug fixes.
  89. ;;
  90. ;; Version 4.13
  91. ;; - The list of agenda files can be maintainted in an external file.
  92. ;; - Bug fixes.
  93. ;;
  94. ;; Version 4.12
  95. ;; - Templates for remember buffer. Note that the remember setup changes.
  96. ;; To set up templates, see `org-remember-templates'.
  97. ;; - The time in new time stamps can be rounded, see new option
  98. ;; `org-time-stamp-rounding-minutes'.
  99. ;; - Bug fixes (there are *always* more bugs).
  100. ;;
  101. ;; Version 4.10
  102. ;; - Bug fixes.
  103. ;;
  104. ;; Version 4.09
  105. ;; - Bug fixes.
  106. ;; - Small improvements to font-lock support.
  107. ;; - MHE support finalized.
  108. ;;
  109. ;; Version 4.08
  110. ;; - Bug fixes.
  111. ;; - Improved MHE support
  112. ;;
  113. ;; Version 4.07
  114. ;; - Bug fixes.
  115. ;; - Leading stars in headlines can be hidden, so make the outline look
  116. ;; cleaner.
  117. ;; - Mouse-1 can be used to follow links.
  118. ;;
  119. ;; Version 4.06
  120. ;; - HTML exporter treats targeted internal links.
  121. ;; - Bug fixes.
  122. ;;
  123. ;; Version 4.05
  124. ;; - Changes to internal link system (thanks to David Wainberg for ideas).
  125. ;; - in-file links: [[Search String]] instead of <file:::Search String>
  126. ;; - automatic links to "radio targets".
  127. ;; - CamelCase not longer active by default, configure org-activate-camels
  128. ;; if you want to turn it back on.
  129. ;; - After following a link, `C-c &' jumps back to it.
  130. ;; - MH-E link support (thanks to Thomas Baumann).
  131. ;; - Special table lines are no longer exported.
  132. ;; - Bug fixes and minor improvements.
  133. ;;
  134. ;; Version 4.04
  135. ;; - Cleanup tags display in agenda.
  136. ;; - Bug fixes.
  137. ;;
  138. ;; Version 4.03
  139. ;; - Table alignment fixed for use with wide characters.
  140. ;; - `C-c -' leaves cursor in current table line.
  141. ;; - The current TAG can be incorporated into the agenda prefix.
  142. ;; See option `org-agenda-prefix-format' for details.
  143. ;;
  144. ;; Version 4.02
  145. ;; - Minor bug fixes and improvements around tag searches.
  146. ;; - XEmacs compatibility fixes.
  147. ;;
  148. ;; Version 4.01
  149. ;; - Tags can also be set remotely from agenda buffer.
  150. ;; - Boolean logic for tag searches.
  151. ;; - Additional agenda commands can be configured through the variable
  152. ;; `org-agenda-custom-commands'.
  153. ;; - Minor bug fixes.
  154. ;;
  155. ;;; Code:
  156. (eval-when-compile
  157. (require 'cl)
  158. (require 'calendar))
  159. (require 'outline)
  160. (require 'time-date)
  161. (require 'easymenu)
  162. (defvar calc-embedded-close-formula) ; defined by the calc package
  163. (defvar calc-embedded-open-formula) ; defined by the calc package
  164. (defvar font-lock-unfontify-region-function) ; defined by font-lock.el
  165. ;;; Customization variables
  166. (defvar org-version "4.19b"
  167. "The version number of the file org.el.")
  168. (defun org-version ()
  169. (interactive)
  170. (message "Org-mode version %s" org-version))
  171. ;; The following constant is for compatibility with different versions
  172. ;; of outline.el.
  173. (defconst org-noutline-p (featurep 'noutline)
  174. "Are we using the new outline mode?")
  175. (defconst org-xemacs-p (featurep 'xemacs))
  176. (defconst org-format-transports-properties-p
  177. (let ((x "a"))
  178. (add-text-properties 0 1 '(test t) x)
  179. (get-text-property 0 'test (format "%s" x)))
  180. "Does format transport text properties?")
  181. (defgroup org nil
  182. "Outline-based notes management and organizer."
  183. :tag "Org"
  184. :group 'outlines
  185. :group 'hypermedia
  186. :group 'calendar)
  187. (defgroup org-startup nil
  188. "Options concerning startup of Org-mode."
  189. :tag "Org Startup"
  190. :group 'org)
  191. (defcustom org-startup-folded t
  192. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  193. This can also be configured on a per-file basis by adding one of
  194. the following lines anywhere in the buffer:
  195. #+STARTUP: fold
  196. #+STARTUP: nofold
  197. #+STARTUP: content"
  198. :group 'org-startup
  199. :type '(choice
  200. (const :tag "nofold: show all" nil)
  201. (const :tag "fold: overview" t)
  202. (const :tag "content: all headlines" content)))
  203. (defcustom org-startup-truncated t
  204. "Non-nil means, entering Org-mode will set `truncate-lines'.
  205. This is useful since some lines containing links can be very long and
  206. uninteresting. Also tables look terrible when wrapped."
  207. :group 'org-startup
  208. :type 'boolean)
  209. (defcustom org-startup-align-all-tables nil
  210. "Non-nil means, align all tables when visiting a file.
  211. This is useful when the column width in tables is forced with <N> cookies
  212. in table fields. Such tables will look correct only after the first re-align."
  213. :group 'org-startup
  214. :type 'boolean)
  215. (defcustom org-startup-with-deadline-check nil
  216. "Non-nil means, entering Org-mode will run the deadline check.
  217. This means, if you start editing an org file, you will get an
  218. immediate reminder of any due deadlines.
  219. This can also be configured on a per-file basis by adding one of
  220. the following lines anywhere in the buffer:
  221. #+STARTUP: dlcheck
  222. #+STARTUP: nodlcheck"
  223. :group 'org-startup
  224. :type 'boolean)
  225. (defcustom org-insert-mode-line-in-empty-file nil
  226. "Non-nil means insert the first line setting Org-mode in empty files.
  227. When the function `org-mode' is called interactively in an empty file, this
  228. normally means that the file name does not automatically trigger Org-mode.
  229. To ensure that the file will always be in Org-mode in the future, a
  230. line enforcing Org-mode will be inserted into the buffer, if this option
  231. has been set."
  232. :group 'org-startup
  233. :type 'boolean)
  234. (defcustom org-CUA-compatible nil
  235. "Non-nil means use alternative key bindings for S-<cursor movement>.
  236. Org-mode used S-<cursor movement> for changing timestamps and priorities.
  237. S-<cursor movement> is also used for example by `CUA-mode' to select text.
  238. If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
  239. alternative bindings. Setting this variable to t will replace the following
  240. keys both in Org-mode and in the Org-agenda buffer.
  241. S-RET -> C-S-RET
  242. S-up -> M-p
  243. S-down -> M-n
  244. S-left -> M--
  245. S-right -> M-+
  246. If you do not like the alternative keys, take a look at the variable
  247. `org-disputed-keys'.
  248. This option is only relevant at load-time of Org-mode. Changing it requires
  249. a restart of Emacs to become effective."
  250. :group 'org-startup
  251. :type 'boolean)
  252. (defvar org-disputed-keys
  253. '((S-up [(shift up)] [(meta ?p)])
  254. (S-down [(shift down)] [(meta ?n)])
  255. (S-left [(shift left)] [(meta ?-)])
  256. (S-right [(shift right)] [(meta ?+)])
  257. (S-return [(shift return)] [(control shift return)]))
  258. "Keys for which Org-mode and other modes compete.
  259. This is an alist, cars are symbols for lookup, 1st element is the default key,
  260. second element will be used when `org-CUA-compatible' is t.")
  261. (defun org-key (key)
  262. "Select a key according to `org-CUA-compatible'."
  263. (nth (if org-CUA-compatible 2 1)
  264. (or (assq key org-disputed-keys)
  265. (error "Invalid Key %s in `org-key'" key))))
  266. (defcustom org-ellipsis nil
  267. "The ellipsis to use in the Org-mode outline.
  268. When nil, just use the standard three dots. When a string, use that instead,
  269. and just in Org-mode (which will then use its own display table).
  270. Changing this requires executing `M-x org-mode' in a buffer to become
  271. effective."
  272. :group 'org-startup
  273. :type '(choice (const :tag "Default" nil)
  274. (string :tag "String" :value "...#")))
  275. (defvar org-display-table nil
  276. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  277. (defgroup org-keywords nil
  278. "Keywords in Org-mode."
  279. :tag "Org Keywords"
  280. :group 'org)
  281. (defcustom org-deadline-string "DEADLINE:"
  282. "String to mark deadline entries.
  283. A deadline is this string, followed by a time stamp. Should be a word,
  284. terminated by a colon. You can insert a schedule keyword and
  285. a timestamp with \\[org-deadline].
  286. Changes become only effective after restarting Emacs."
  287. :group 'org-keywords
  288. :type 'string)
  289. (defcustom org-scheduled-string "SCHEDULED:"
  290. "String to mark scheduled TODO entries.
  291. A schedule is this string, followed by a time stamp. Should be a word,
  292. terminated by a colon. You can insert a schedule keyword and
  293. a timestamp with \\[org-schedule].
  294. Changes become only effective after restarting Emacs."
  295. :group 'org-keywords
  296. :type 'string)
  297. (defcustom org-closed-string "CLOSED:"
  298. "String used as the prefix for timestamps logging closing a TODO entry."
  299. :group 'org-keywords
  300. :type 'string)
  301. (defcustom org-comment-string "COMMENT"
  302. "Entries starting with this keyword will never be exported.
  303. An entry can be toggled between COMMENT and normal with
  304. \\[org-toggle-comment].
  305. Changes become only effective after restarting Emacs."
  306. :group 'org-keywords
  307. :type 'string)
  308. (defcustom org-quote-string "QUOTE"
  309. "Entries starting with this keyword will be exported in fixed-width font.
  310. Quoting applies only to the text in the entry following the headline, and does
  311. not extend beyond the next headline, even if that is lower level.
  312. An entry can be toggled between QUOTE and normal with
  313. \\[org-toggle-fixed-width-section]."
  314. :group 'org-keywords
  315. :type 'string)
  316. (defgroup org-structure nil
  317. "Options concerning the general structure of Org-mode files."
  318. :tag "Org Structure"
  319. :group 'org)
  320. (defgroup org-cycle nil
  321. "Options concerning visibility cycling in Org-mode."
  322. :tag "Org Cycle"
  323. :group 'org-structure)
  324. (defcustom org-cycle-emulate-tab t
  325. "Where should `org-cycle' emulate TAB.
  326. nil Never
  327. white Only in completely white lines
  328. t Everywhere except in headlines"
  329. :group 'org-cycle
  330. :type '(choice (const :tag "Never" nil)
  331. (const :tag "Only in completely white lines" white)
  332. (const :tag "Everywhere except in headlines" t)
  333. ))
  334. (defcustom org-cycle-hook '(org-optimize-window-after-visibility-change)
  335. "Hook that is run after `org-cycle' has changed the buffer visibility.
  336. The function(s) in this hook must accept a single argument which indicates
  337. the new state that was set by the most recent `org-cycle' command. The
  338. argument is a symbol. After a global state change, it can have the values
  339. `overview', `content', or `all'. After a local state change, it can have
  340. the values `folded', `children', or `subtree'."
  341. :group 'org-cycle
  342. :type 'hook)
  343. (defgroup org-edit-structure nil
  344. "Options concerning structure editing in Org-mode."
  345. :tag "Org Edit Structure"
  346. :group 'org-structure)
  347. (defcustom org-odd-levels-only nil
  348. "Non-nil means, skip even levels and only use odd levels for the outline.
  349. This has the effect that two stars are being added/taken away in
  350. promotion/demotion commands. It also influences how levels are
  351. handled by the exporters.
  352. Changing it requires restart of `font-lock-mode' to become effective
  353. for fontification also in regions already fontified."
  354. :group 'org-edit-structure
  355. :group 'org-font-lock
  356. :type 'boolean)
  357. (defcustom org-adapt-indentation t
  358. "Non-nil means, adapt indentation when promoting and demoting.
  359. When this is set and the *entire* text in an entry is indented, the
  360. indentation is increased by one space in a demotion command, and
  361. decreased by one in a promotion command. If any line in the entry
  362. body starts at column 0, indentation is not changed at all."
  363. :group 'org-edit-structure
  364. :type 'boolean)
  365. ;; FIXME: in the wrong group, but where would it fit????
  366. (defcustom org-enable-fixed-width-editor t
  367. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  368. This currently only means, they are never auto-wrapped.
  369. When nil, such lines will be treated like ordinary lines.
  370. See also the QUOTE keyword."
  371. :group 'org-edit-structure
  372. :type 'boolean)
  373. (defgroup org-sparse-trees nil
  374. "Options concerning sparse trees in Org-mode."
  375. :tag "Org Sparse Trees"
  376. :group 'org-structure)
  377. (defcustom org-highlight-sparse-tree-matches t
  378. "Non-nil means, highlight all matches that define a sparse tree.
  379. The highlights will automatically disappear the next time the buffer is
  380. changed by an edit command."
  381. :group 'org-sparse-trees
  382. :type 'boolean)
  383. (defcustom org-show-hierarchy-above t
  384. "Non-nil means, show full hierarchy when showing a spot in the tree.
  385. Turning this off makes sparse trees more compact, but also less clear."
  386. :group 'org-sparse-trees
  387. :type 'boolean)
  388. (defcustom org-show-following-heading t
  389. "Non-nil means, show heading following match in `org-occur'.
  390. When doing an `org-occur' it is useful to show the headline which
  391. follows the match, even if they do not match the regexp. This makes it
  392. easier to edit directly inside the sparse tree. However, if you use
  393. `org-occur' mainly as an overview, the following headlines are
  394. unnecessary clutter."
  395. :group 'org-sparse-trees
  396. :type 'boolean)
  397. (defcustom org-occur-hook '(org-first-headline-recenter)
  398. "Hook that is run after `org-occur' has constructed a sparse tree.
  399. This can be used to recenter the window to show as much of the structure
  400. as possible."
  401. :group 'org-sparse-trees
  402. :type 'hook)
  403. (defgroup org-plain-lists nil
  404. "Options concerning plain lists in Org-mode."
  405. :tag "Org Plain lists"
  406. :group 'org-structure)
  407. (defcustom org-cycle-include-plain-lists nil
  408. "Non-nil means, include plain lists into visibility cycling.
  409. This means that during cycling, plain list items will *temporarily* be
  410. interpreted as outline headlines with a level given by 1000+i where i is the
  411. indentation of the bullet. In all other operations, plain list items are
  412. not seen as headlines. For example, you cannot assign a TODO keyword to
  413. such an item."
  414. :group 'org-plain-lists
  415. :type 'boolean)
  416. (defcustom org-plain-list-ordered-item-terminator t
  417. "The character that makes a line with leading number an ordered list item.
  418. Valid values are ?. and ?\). To get both terminators, use t. While
  419. ?. may look nicer, it creates the danger that a line with leading
  420. number may be incorrectly interpreted as an item. ?\) therefore is
  421. the safe choice."
  422. :group 'org-plain-lists
  423. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  424. (const :tag "paren like in \"2)\"" ?\))
  425. (const :tab "both" t)))
  426. (defcustom org-auto-renumber-ordered-lists t
  427. "Non-nil means, automatically renumber ordered plain lists.
  428. Renumbering happens when the sequence have been changed with
  429. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  430. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  431. :group 'org-plain-lists
  432. :type 'boolean)
  433. (defgroup org-archive nil
  434. "Options concerning archiving in Org-mode."
  435. :tag "Org Archive"
  436. :group 'org-structure)
  437. (defcustom org-archive-location "%s_archive::"
  438. "The location where subtrees should be archived.
  439. This string consists of two parts, separated by a double-colon.
  440. The first part is a file name - when omitted, archiving happens in the same
  441. file. %s will be replaced by the current file name (without directory part).
  442. Archiving to a different file is useful to keep archived entries from
  443. contributing to the Org-mode Agenda.
  444. The part after the double colon is a headline. The archived entries will be
  445. filed under that headline. When omitted, the subtrees are simply filed away
  446. at the end of the file, as top-level entries.
  447. Here are a few examples:
  448. \"%s_archive::\"
  449. If the current file is Projects.org, archive in file
  450. Projects.org_archive, as top-level trees. This is the default.
  451. \"::* Archived Tasks\"
  452. Archive in the current file, under the top-level headline
  453. \"* Archived Tasks\".
  454. \"~/org/archive.org::\"
  455. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  456. \"basement::** Finished Tasks\"
  457. Archive in file ./basement (relative path), as level 3 trees
  458. below the level 2 heading \"** Finished Tasks\".
  459. You may set this option on a per-file basis by adding to the buffer a
  460. line like
  461. #+ARCHIVE: basement::** Finished Tasks"
  462. :group 'org-archive
  463. :type 'string)
  464. (defcustom org-archive-mark-done t
  465. "Non-nil means, mark archived entries as DONE."
  466. :group 'org-archive
  467. :type 'boolean)
  468. (defcustom org-archive-stamp-time t
  469. "Non-nil means, add a time stamp to archived entries.
  470. The time stamp will be added directly after the TODO state keyword in the
  471. first line, so it is probably best to use this in combinations with
  472. `org-archive-mark-done'."
  473. :group 'org-archive
  474. :type 'boolean)
  475. (defgroup org-table nil
  476. "Options concerning tables in Org-mode."
  477. :tag "Org Table"
  478. :group 'org)
  479. (defcustom org-enable-table-editor 'optimized
  480. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  481. When nil, such lines will be treated like ordinary lines.
  482. When equal to the symbol `optimized', the table editor will be optimized to
  483. do the following:
  484. - Use automatic overwrite mode in front of whitespace in table fields.
  485. This make the structure of the table stay in tact as long as the edited
  486. field does not exceed the column width.
  487. - Minimize the number of realigns. Normally, the table is aligned each time
  488. TAB or RET are pressed to move to another field. With optimization this
  489. happens only if changes to a field might have changed the column width.
  490. Optimization requires replacing the functions `self-insert-command',
  491. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  492. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  493. very good at guessing when a re-align will be necessary, but you can always
  494. force one with \\[org-ctrl-c-ctrl-c].
  495. If you would like to use the optimized version in Org-mode, but the
  496. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  497. This variable can be used to turn on and off the table editor during a session,
  498. but in order to toggle optimization, a restart is required.
  499. See also the variable `org-table-auto-blank-field'."
  500. :group 'org-table
  501. :type '(choice
  502. (const :tag "off" nil)
  503. (const :tag "on" t)
  504. (const :tag "on, optimized" optimized)))
  505. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  506. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  507. In the optimized version, the table editor takes over all simple keys that
  508. normally just insert a character. In tables, the characters are inserted
  509. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  510. for empty fields). Outside tables, the correct binding of the keys is
  511. restored.
  512. The default for this option is t if the optimized version is also used in
  513. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  514. this variable requires a restart of Emacs to become effective."
  515. :group 'org-table
  516. :type 'boolean)
  517. (defgroup org-table-settings nil
  518. "Settings for tables in Org-mode."
  519. :tag "Org Table Settings"
  520. :group 'org-table)
  521. (defcustom org-table-default-size "5x2"
  522. "The default size for newly created tables, Columns x Rows."
  523. :group 'org-table-settings
  524. :type 'string)
  525. (defcustom org-table-spaces-around-separators '(1 . 1)
  526. "The number of spaces to be placed before and after separators."
  527. :group 'org-table-settings
  528. :type '(cons (number :tag "Before \"|\"") (number :tag " After \"|\"")))
  529. (defcustom org-table-spaces-around-invisible-separators '(1 . 2)
  530. "The number of spaces to be placed before and after separators.
  531. This option applies when the column separators have been made invisible."
  532. :group 'org-table-settings
  533. :type '(cons (number :tag "Before \"|\"") (number :tag " After \"|\"")))
  534. (defcustom org-table-number-regexp "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$"
  535. "Regular expression for recognizing numbers in table columns.
  536. If a table column contains mostly numbers, it will be aligned to the
  537. right. If not, it will be aligned to the left.
  538. The default value of this option is a regular expression which allows
  539. anything which looks remotely like a number as used in scientific
  540. context. For example, all of the following will be considered a
  541. number:
  542. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  543. Other options offered by the customize interface are more restrictive."
  544. :group 'org-table-settings
  545. :type '(choice
  546. (const :tag "Positive Integers"
  547. "^[0-9]+$")
  548. (const :tag "Integers"
  549. "^[-+]?[0-9]+$")
  550. (const :tag "Floating Point Numbers"
  551. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  552. (const :tag "Floating Point Number or Integer"
  553. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  554. (const :tag "Exponential, Floating point, Integer"
  555. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  556. (const :tag "Very General Number-Like"
  557. "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$")
  558. (string :tag "Regexp:")))
  559. (defcustom org-table-number-fraction 0.5
  560. "Fraction of numbers in a column required to make the column align right.
  561. In a column all non-white fields are considered. If at least this
  562. fraction of fields is matched by `org-table-number-fraction',
  563. alignment to the right border applies."
  564. :group 'org-table-settings
  565. :type 'number)
  566. (defgroup org-table-editing nil
  567. "Bahavior of tables during editing in Org-mode."
  568. :tag "Org Table Editing"
  569. :group 'org-table)
  570. (defcustom org-table-automatic-realign t
  571. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  572. When nil, aligning is only done with \\[org-table-align], or after column
  573. removal/insertion."
  574. :group 'org-table-editing
  575. :type 'boolean)
  576. (defcustom org-table-limit-column-width t
  577. "Non-nil means, allow to limit the width of table columns with <N> fields."
  578. :group 'org-table-editing
  579. :type 'boolean)
  580. (defcustom org-table-auto-blank-field t
  581. "Non-nil means, automatically blank table field when starting to type into it.
  582. This only happens when typing immediately after a field motion
  583. command (TAB, S-TAB or RET).
  584. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  585. :group 'org-table-editing
  586. :type 'boolean)
  587. (defcustom org-table-tab-jumps-over-hlines t
  588. "Non-nil means, tab in the last column of a table with jump over a hline.
  589. If a horizontal separator line is following the current line,
  590. `org-table-next-field' can either create a new row before that line, or jump
  591. over the line. When this option is nil, a new line will be created before
  592. this line."
  593. :group 'org-table-editing
  594. :type 'boolean)
  595. (defcustom org-table-tab-recognizes-table.el t
  596. "Non-nil means, TAB will automatically notice a table.el table.
  597. When it sees such a table, it moves point into it and - if necessary -
  598. calls `table-recognize-table'."
  599. :group 'org-table-editing
  600. :type 'boolean)
  601. (defgroup org-table-calculation nil
  602. "Options concerning tables in Org-mode."
  603. :tag "Org Table Calculation"
  604. :group 'org-table)
  605. (defcustom org-table-copy-increment t
  606. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  607. :group 'org-table-calculation
  608. :type 'boolean)
  609. (defcustom org-calc-default-modes
  610. '(calc-internal-prec 12
  611. calc-float-format (float 5)
  612. calc-angle-mode deg
  613. calc-prefer-frac nil
  614. calc-symbolic-mode nil
  615. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  616. calc-display-working-message t
  617. )
  618. "List with Calc mode settings for use in calc-eval for table formulas.
  619. The list must contain alternating symbols (Calc modes variables and values).
  620. Don't remove any of the default settings, just change the values. Org-mode
  621. relies on the variables to be present in the list."
  622. :group 'org-table-calculation
  623. :type 'plist)
  624. (defcustom org-table-formula-evaluate-inline t
  625. "Non-nil means, TAB and RET evaluate a formula in current table field.
  626. If the current field starts with an equal sign, it is assumed to be a formula
  627. which should be evaluated as described in the manual and in the documentation
  628. string of the command `org-table-eval-formula'. This feature requires the
  629. Emacs calc package.
  630. When this variable is nil, formula calculation is only available through
  631. the command \\[org-table-eval-formula]."
  632. :group 'org-table-calculation
  633. :type 'boolean)
  634. (defcustom org-table-formula-use-constants t
  635. "Non-nil means, interpret constants in formulas in tables.
  636. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  637. by the value given in `org-table-formula-constants', or by a value obtained
  638. from the `constants.el' package."
  639. :group 'org-table-calculation
  640. :type 'boolean)
  641. (defcustom org-table-formula-constants nil
  642. "Alist with constant names and values, for use in table formulas.
  643. The car of each element is a name of a constant, without the `$' before it.
  644. The cdr is the value as a string. For example, if you'd like to use the
  645. speed of light in a formula, you would configure
  646. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  647. and then use it in an equation like `$1*$c'."
  648. :group 'org-table-calculation
  649. :type '(repeat
  650. (cons (string :tag "name")
  651. (string :tag "value"))))
  652. (defcustom org-table-formula-numbers-only nil
  653. "Non-nil means, calculate only with numbers in table formulas.
  654. Then all input fields will be converted to a number, and the result
  655. must also be a number. When nil, calc's full potential is available
  656. in table calculations, including symbolics etc."
  657. :group 'org-table-calculation
  658. :type 'boolean)
  659. (defcustom org-table-allow-automatic-line-recalculation t
  660. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  661. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  662. :group 'org-table-calculation
  663. :type 'boolean)
  664. (defgroup org-link nil
  665. "Options concerning links in Org-mode."
  666. :tag "Org Link"
  667. :group 'org)
  668. (defcustom org-descriptive-links t
  669. "Non-nil means, hide link part and only show description of bracket links.
  670. Bracket links are like [[link][descritpion]]. This variable sets the initial
  671. state in new org-mode buffers. The setting can then be toggled on a
  672. per-buffer basis from the Org->Hyperlinks menu."
  673. :group 'org-link
  674. :type 'boolean)
  675. (defcustom org-link-style 'bracket
  676. "The style of links to be inserted with \\[org-insert-link].
  677. Possible values are:
  678. bracket [[link][description]]. This is recommended
  679. plain Description \\n link. The old way, no longer recommended."
  680. :group 'org-link
  681. :type '(choice
  682. (const :tag "Bracket (recommended)" bracket)
  683. (const :tag "Plain (no longer recommended)" plain)))
  684. (defcustom org-link-format "%s"
  685. "Default format for external, URL-like linkes in the buffer.
  686. This is a format string for printf, %s will be replaced by the link text.
  687. The recommended value is just \"%s\", since links will be protected by
  688. enclosing them in double brackets. If you prefer plain links (see variable
  689. `org-link-style'), \"<%s>\" is useful. Some people also recommend an
  690. additional URL: prefix, so the format would be \"<URL:%s>\"."
  691. :group 'org-link
  692. :type '(choice
  693. (const :tag "\"%s\" (e.g. http://www.there.com)" "%s")
  694. (const :tag "\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
  695. (const :tag "\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
  696. (string :tag "Other" :value "<%s>")))
  697. (defvaralias 'org-allow-space-in-links 'org-allow-space-in-plain-links)
  698. (defcustom org-allow-space-in-plain-links nil
  699. "Non-nil means, space characters may be part of a plain text link.
  700. Space characters are no problem in links enclosed by double square brackets.
  701. But in plain text, if space is allowed, org-mode does not know where to
  702. terminate the link and reads until the end of the line.
  703. This variable exists only to force compatibility with old org-mode files.
  704. I discourage setting this to t, and support for this may not be available
  705. in the future.
  706. Changing this requires a restart of Emacs to become effective."
  707. :group 'org-link
  708. :type 'boolean)
  709. (defcustom org-radio-targets t
  710. "Non-nil means activate text matching a link target.
  711. Radio targets are strings in triple angular brackets, like <<<My Target>>>.
  712. When this option is set, any occurrence of \"my target\" in normal text
  713. becomes a link."
  714. :group 'org-link
  715. :type 'boolean)
  716. (defcustom org-activate-camels nil
  717. "Non-nil means, treat words in CamelCase as in-file links.
  718. Changing this requires restart of Emacs to become effective."
  719. :group 'org-link
  720. :type 'boolean)
  721. (defgroup org-link-store nil
  722. "Options concerning storing links in Org-mode"
  723. :tag "Org Store Link"
  724. :group 'org-link)
  725. (defcustom org-context-in-file-links t
  726. "Non-nil means, file links from `org-store-link' contain context.
  727. A search string will be added to the file name with :: as separator and
  728. used to find the context when the link is activated by the command
  729. `org-open-at-point'.
  730. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  731. negates this setting for the duration of the command."
  732. :group 'org-link-store
  733. :type 'boolean)
  734. (defcustom org-file-link-context-use-camel-case nil
  735. "Non-nil means, use CamelCase to store a search context in a file link.
  736. When nil, the search string simply consists of the words of the string.
  737. CamelCase is deprecated, and support for it may be dropped in the future."
  738. :group 'org-link-store
  739. :type 'boolean)
  740. (defcustom org-keep-stored-link-after-insertion nil
  741. "Non-nil means, keep link in list for entire session.
  742. The command `org-store-link' adds a link pointing to the current
  743. location to an internal list. These links accumulate during a session.
  744. The command `org-insert-link' can be used to insert links into any
  745. Org-mode file (offering completion for all stored links). When this
  746. option is nil, every link which has been inserted once using \\[org-insert-link]
  747. will be removed from the list, to make completing the unused links
  748. more efficient."
  749. :group 'org-link-store
  750. :type 'boolean)
  751. (defcustom org-usenet-links-prefer-google nil
  752. "Non-nil means, `org-store-link' will create web links to Google groups.
  753. When nil, Gnus will be used for such links.
  754. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  755. negates this setting for the duration of the command."
  756. :group 'org-link-store
  757. :type 'boolean)
  758. (defgroup org-link-follow nil
  759. "Options concerning following links in Org-mode"
  760. :tag "Org Follow Link"
  761. :group 'org-link)
  762. (defcustom org-tab-follows-link nil
  763. "Non-nil means, on links TAB will follow the link.
  764. Needs to be set before org.el is loaded."
  765. :group 'org-link-follow
  766. :type 'boolean)
  767. (defcustom org-return-follows-link nil
  768. "Non-nil means, on links RET will follow the link.
  769. Needs to be set before org.el is loaded."
  770. :group 'org-link-follow
  771. :type 'boolean)
  772. (defcustom org-mark-ring-length 4
  773. "Number of different positions to be recorded in the ring
  774. Changing this requires a restart of Emacs to work correctly."
  775. :group 'org-link-follow
  776. :type 'interger)
  777. (defcustom org-link-frame-setup
  778. '((vm . vm-visit-folder-other-frame)
  779. (gnus . gnus-other-frame)
  780. (file . find-file-other-window))
  781. "Setup the frame configuration for following links.
  782. When following a link with Emacs, it may often be useful to display
  783. this link in another window or frame. This variable can be used to
  784. set this up for the different types of links.
  785. For VM, use any of
  786. `vm-visit-folder'
  787. `vm-visit-folder-other-frame'
  788. For Gnus, use any of
  789. `gnus'
  790. `gnus-other-frame'
  791. For FILE, use any of
  792. `find-file'
  793. `find-file-other-window'
  794. `find-file-other-frame'
  795. For the calendar, use the variable `calendar-setup'.
  796. For BBDB, it is currently only possible to display the matches in
  797. another window."
  798. :group 'org-link-follow
  799. :type '(list
  800. (cons (const vm)
  801. (choice
  802. (const vm-visit-folder)
  803. (const vm-visit-folder-other-window)
  804. (const vm-visit-folder-other-frame)))
  805. (cons (const gnus)
  806. (choice
  807. (const gnus)
  808. (const gnus-other-frame)))
  809. (cons (const file)
  810. (choice
  811. (const find-file)
  812. (const find-file-other-window)
  813. (const find-file-other-frame)))))
  814. (defcustom org-open-non-existing-files nil
  815. "Non-nil means, `org-open-file' will open non-existing file.
  816. When nil, an error will be generated."
  817. :group 'org-link-follow
  818. :type 'boolean)
  819. (defcustom org-confirm-shell-links t
  820. "Non-nil means, ask for confirmation before executing shell links.
  821. The default is true, to keep new users from shooting into their own foot."
  822. :group 'org-link-follow
  823. :type 'boolean)
  824. (defconst org-file-apps-defaults-gnu
  825. '((t . mailcap))
  826. "Default file applications on a UNIX or GNU/Linux system.
  827. See `org-file-apps'.")
  828. (defconst org-file-apps-defaults-macosx
  829. '((t . "open %s")
  830. ("ps" . "gv %s")
  831. ("ps.gz" . "gv %s")
  832. ("eps" . "gv %s")
  833. ("eps.gz" . "gv %s")
  834. ("dvi" . "xdvi %s")
  835. ("fig" . "xfig %s"))
  836. "Default file applications on a MacOS X system.
  837. The system \"open\" is known as a default, but we use X11 applications
  838. for some files for which the OS does not have a good default.
  839. See `org-file-apps'.")
  840. (defconst org-file-apps-defaults-windowsnt
  841. '((t . (w32-shell-execute "open" file)))
  842. "Default file applications on a Windows NT system.
  843. The system \"open\" is used for most files.
  844. See `org-file-apps'.")
  845. (defcustom org-file-apps
  846. '(
  847. ("txt" . emacs)
  848. ("tex" . emacs)
  849. ("ltx" . emacs)
  850. ("org" . emacs)
  851. ("el" . emacs)
  852. )
  853. "External applications for opening `file:path' items in a document.
  854. Org-mode uses system defaults for different file types, but
  855. you can use this variable to set the application for a given file
  856. extension. The entries in this list are cons cells with a file extension
  857. and the corresponding command. Possible values for the command are:
  858. `emacs' The file will be visited by the current Emacs process.
  859. `default' Use the default application for this file type.
  860. string A command to be executed by a shell; %s will be replaced
  861. by the path to the file.
  862. sexp A Lisp form which will be evaluated. The file path will
  863. be available in the Lisp variable `file'.
  864. For more examples, see the system specific constants
  865. `org-file-apps-defaults-macosx'
  866. `org-file-apps-defaults-windowsnt'
  867. `org-file-apps-defaults-gnu'."
  868. :group 'org-link-follow
  869. :type '(repeat
  870. (cons (choice :value ""
  871. (string :tag "Extension")
  872. (const :tag "Default for unrecognized files" t)
  873. (const :tag "Links to a directory" directory))
  874. (choice :value ""
  875. (const :tag "Visit with Emacs" emacs)
  876. (const :tag "Use system default" default)
  877. (string :tag "Command")
  878. (sexp :tag "Lisp form")))))
  879. (defcustom org-mhe-search-all-folders nil
  880. "Non-nil means, that the search for the mh-message will be extended to
  881. all folders if the message cannot be found in the folder given in the link.
  882. Searching all folders is very effective with one of the search engines
  883. supported by MH-E, but will be slow with pick."
  884. :group 'org-link-follow
  885. :type 'boolean)
  886. (defgroup org-remember nil
  887. "Options concerning interaction with remember.el."
  888. :tag "Org Remember"
  889. :group 'org)
  890. (defcustom org-directory "~/org"
  891. "Directory with org files.
  892. This directory will be used as default to prompt for org files.
  893. Used by the hooks for remember.el."
  894. :group 'org-remember
  895. :type 'directory)
  896. (defcustom org-default-notes-file "~/.notes"
  897. "Default target for storing notes.
  898. Used by the hooks for remember.el. This can be a string, or nil to mean
  899. the value of `remember-data-file'."
  900. :group 'org-remember
  901. :type '(choice
  902. (const :tag "Default from remember-data-file" nil)
  903. file))
  904. (defcustom org-remember-templates nil
  905. "Templates for the creation of remember buffers.
  906. When nil, just let remember make the buffer.
  907. When not nil, this is a list of 3-element lists. In each entry, the first
  908. element is a character, a unique key to select this template.
  909. The second element is the template. The third element is optional and can
  910. specify a destination file for remember items created with this template.
  911. The default file is given by `org-default-notes-file'.
  912. The template specifies the structure of the remember buffer. It should have
  913. a first line starting with a star, to act as the org-mode headline.
  914. Furthermore, the following %-escapes will be replaced with content:
  915. %t time stamp, date only
  916. %T time stamp with date and time
  917. %u inactive time stamp, date only
  918. %U inactive time stamp with date and time
  919. %n user name
  920. %a annotation, normally the link created with org-store-link
  921. %i initial content, the region when remember is called with C-u.
  922. If %i is indented, the entire inserted text will be indented as well.
  923. %? This will be removed, and the cursor placed at this position."
  924. :group 'org-remember
  925. :type '(repeat :tag "enabled"
  926. (list :value (?a "\n" nil)
  927. (character :tag "Selection Key")
  928. (string :tag "Template")
  929. (file :tag "Destination file (optional)"))))
  930. (defcustom org-reverse-note-order nil
  931. "Non-nil means, store new notes at the beginning of a file or entry.
  932. When nil, new notes will be filed to the end of a file or entry."
  933. :group 'org-remember
  934. :type '(choice
  935. (const :tag "Reverse always" t)
  936. (const :tag "Reverse never" nil)
  937. (repeat :tag "By file name regexp"
  938. (cons regexp boolean))))
  939. (defgroup org-todo nil
  940. "Options concerning TODO items in Org-mode."
  941. :tag "Org TODO"
  942. :group 'org)
  943. (defcustom org-todo-keywords '("TODO" "DONE")
  944. "List of TODO entry keywords.
  945. \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
  946. considered to mean that the entry is \"done\". All the other mean that
  947. action is required, and will make the entry show up in todo lists, diaries
  948. etc.
  949. The command \\[org-todo] cycles an entry through these states, and an
  950. additional state where no keyword is present. For details about this
  951. cycling, see also the variable `org-todo-interpretation'
  952. Changes become only effective after restarting Emacs."
  953. :group 'org-todo
  954. :group 'org-keywords
  955. :type '(repeat (string :tag "Keyword")))
  956. (defcustom org-todo-interpretation 'sequence
  957. "Controls how TODO keywords are interpreted.
  958. \\<org-mode-map>Possible values are `sequence' and `type'.
  959. This variable is only relevant if `org-todo-keywords' contains more than two
  960. states. There are two ways how these keywords can be used:
  961. - As a sequence in the process of working on a TODO item, for example
  962. (setq org-todo-keywords '(\"TODO\" \"STARTED\" \"VERIFY\" \"DONE\")
  963. org-todo-interpretation 'sequence)
  964. - As different types of TODO items, for example
  965. (setq org-todo-keywords '(\"URGENT\" \"RELAXED\" \"REMIND\" \"FOR_TOM\" \"DONE\")
  966. org-todo-interpretation 'type)
  967. When the states are interpreted as a sequence, \\[org-todo] always cycles
  968. to the next state, in order to walk through all different states. So with
  969. \\[org-todo], you turn an empty entry into the state TODO. When you started
  970. working on the item, you use \\[org-todo] again to switch it to \"STARTED\",
  971. later to VERIFY and finally to DONE.
  972. When the states are interpreted as types, \\[org-todo] still cycles through
  973. when it is called several times in direct succession, in order to initially
  974. select the type. However, if not called immediately after a previous
  975. \\[org-todo], it switches from each type directly to DONE. So with the
  976. above example, you could use `\\[org-todo] \\[org-todo]' to label an entry
  977. RELAXED. If you later return to this entry and press \\[org-todo] again,
  978. RELAXED will not be changed REMIND, but directly to DONE.
  979. You can create a large number of types. To initially select a
  980. type, it is then best to use \\[universal-argument] \\[org-todo] in order to specify the
  981. type with completion. Of course, you can also type the keyword
  982. directly into the buffer. M-TAB completes TODO keywords at the
  983. beginning of a headline."
  984. :group 'org-todo
  985. :group 'org-keywords
  986. :type '(choice (const sequence)
  987. (const type)))
  988. (defcustom org-after-todo-state-change-hook nil
  989. "Hook which is run after the state of a TODO item was changed.
  990. The new state (a string with a TODO keyword, or nil) is available in the
  991. Lisp variable `state'."
  992. :group 'org-todo
  993. :type 'hook)
  994. (defcustom org-log-done nil
  995. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  996. When the state of an entry is changed from nothing to TODO, remove a previous
  997. closing date."
  998. :group 'org-todo
  999. :type 'boolean)
  1000. ;; FIXME: Correct to put this into the TODO group???
  1001. (defgroup org-priorities nil
  1002. "Keywords in Org-mode."
  1003. :tag "Org Priorities"
  1004. :group 'org-todo)
  1005. (defcustom org-default-priority ?B
  1006. "The default priority of TODO items.
  1007. This is the priority an item get if no explicit priority is given."
  1008. :group 'org-priorities
  1009. :type 'character)
  1010. (defcustom org-lowest-priority ?C
  1011. "The lowest priority of TODO items. A character like ?A, ?B etc."
  1012. :group 'org-priorities
  1013. :type 'character)
  1014. ;; FIXME: move to agenda?????
  1015. (defvar org-category nil
  1016. "Variable used by org files to set a category for agenda display.
  1017. Such files should use a file variable to set it, for example
  1018. -*- mode: org; org-category: \"ELisp\"
  1019. or contain a special line
  1020. #+CATEGORY: ELisp
  1021. If the file does not specify a category, then file's base name
  1022. is used instead.")
  1023. (make-variable-buffer-local 'org-category)
  1024. (defgroup org-time nil
  1025. "Options concerning time stamps and deadlines in Org-mode."
  1026. :tag "Org Time"
  1027. :group 'org)
  1028. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1029. "Formats for `format-time-string' which are used for time stamps.
  1030. It is not recommended to change this constant.")
  1031. (defcustom org-time-stamp-rounding-minutes 0
  1032. "Number of minutes to round time stamps to upon insertion.
  1033. When zero, insert the time unmodified. Useful rounding numbers
  1034. should be factors of 60, so for example 5, 10, 15.
  1035. When this is not zero, you can still force an exact time-stamp by using
  1036. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1037. :group 'org-time
  1038. :type 'integer)
  1039. (defcustom org-deadline-warning-days 30
  1040. "No. of days before expiration during which a deadline becomes active.
  1041. This variable governs the display in the org file."
  1042. :group 'org-time
  1043. :type 'number)
  1044. (defcustom org-popup-calendar-for-date-prompt t
  1045. "Non-nil means, pop up a calendar when prompting for a date.
  1046. In the calendar, the date can be selected with mouse-1. However, the
  1047. minibuffer will also be active, and you can simply enter the date as well.
  1048. When nil, only the minibuffer will be available."
  1049. :group 'org-time
  1050. :type 'boolean)
  1051. (defcustom org-calendar-follow-timestamp-change t
  1052. "Non-nil means, make the calendar window follow timestamp changes.
  1053. When a timestamp is modified and the calendar window is visible, it will be
  1054. moved to the new date."
  1055. :group 'org-time
  1056. :type 'boolean)
  1057. (defgroup org-tags nil
  1058. "Options concerning startup of Org-mode."
  1059. :tag "Org Tags"
  1060. :group 'org)
  1061. (defcustom org-tags-column 48
  1062. "The column to which tags should be indented in a headline.
  1063. If this number is positive, it specifies the column. If it is negative,
  1064. it means that the tags should be flushright to that column. For example,
  1065. -79 works well for a normal 80 character screen."
  1066. :group 'org-tags
  1067. :type 'integer)
  1068. (defcustom org-auto-align-tags t
  1069. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1070. These operations change the length of a headline and therefore shift
  1071. the tags around. With this options turned on, after each such operation
  1072. the tags are again aligned to `org-tags-column'."
  1073. :group 'org-tags
  1074. :type 'boolean)
  1075. (defcustom org-use-tag-inheritance t
  1076. "Non-nil means, tags in levels apply also for sublevels.
  1077. When nil, only the tags directly given in a specific line apply there.
  1078. If you turn off this option, you very likely want to turn on the
  1079. companion option `org-tags-match-list-sublevels'."
  1080. :group 'org-tags
  1081. :type 'boolean)
  1082. (defcustom org-tags-match-list-sublevels nil
  1083. "Non-nil means list also sublevels of headlines matching tag search.
  1084. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1085. the sublevels of a headline matching a tag search often also match
  1086. the same search. Listing all of them can create very long lists.
  1087. Setting this variable to nil causes subtrees to be skipped.
  1088. This option is off by default, because inheritance in on. If you turn
  1089. inheritance off, you very likely want to turn this option on.
  1090. As a special case, if the tag search is restricted to TODO items, the
  1091. value of this variable is ignored and sublevels are always checked, to
  1092. make sure all corresponding TODO items find their way into the list."
  1093. :group 'org-tags
  1094. :type 'boolean)
  1095. (defvar org-tags-history nil
  1096. "History of minibuffer reads for tags.")
  1097. (defvar org-last-tags-completion-table nil
  1098. "The last used completion table for tags.")
  1099. (defgroup org-agenda nil
  1100. "Options concerning agenda display Org-mode."
  1101. :tag "Org Agenda"
  1102. :group 'org)
  1103. (defcustom org-agenda-files nil
  1104. "The files to be used for agenda display.
  1105. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1106. \\[org-remove-file]. You can also use customize to edit the list.
  1107. If the value of the variable is not a list but a single file name, then
  1108. the list of agenda files is actually stored in that file, one agenda file
  1109. per line"
  1110. :group 'org-agenda
  1111. :type '(choice
  1112. (repeat :tag "List of files" file)
  1113. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1114. (defcustom org-agenda-custom-commands '(("w" todo "WAITING"))
  1115. "Custom commands for the agenda.
  1116. These commands will be offered on the splash screen displayed by the
  1117. agenda dispatcher \\[org-agenda]. Each entry is a list of 3 items:
  1118. key The key (a single char as a string) to be associated with the command.
  1119. type The command type, any of the following symbols:
  1120. todo Entries with a specific TODO keyword, in all agenda files.
  1121. tags Tags match in all agenda files.
  1122. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1123. tags-tree Sparse tree with all tags matches in *current* file.
  1124. occur-tree Occur sparse tree for current file.
  1125. match What to search for:
  1126. - a single keyword for TODO keyword searches
  1127. - a tags match expression for tags searches
  1128. - a regular expression for occur searches"
  1129. :group 'org-agenda
  1130. :type '(repeat
  1131. (list (string :tag "Key")
  1132. (choice :tag "Type"
  1133. (const :tag "Tags search in all agenda files" tags)
  1134. (const :tag "TODO keyword search in all agenda files" todo)
  1135. (const :tag "Tags sparse tree in current buffer" tags-tree)
  1136. (const :tag "TODO keyword tree in current buffer" todo-tree)
  1137. (const :tag "Occur tree in current buffer" occur-tree))
  1138. (string :tag "Match"))))
  1139. (defcustom org-agenda-include-all-todo t
  1140. "Non-nil means, the agenda will always contain all TODO entries.
  1141. When nil, date-less entries will only be shown if `org-agenda' is called
  1142. with a prefix argument.
  1143. When non-nil, the TODO entries will be listed at the top of the agenda, before
  1144. the entries for specific days."
  1145. :group 'org-agenda
  1146. :type 'boolean)
  1147. (defcustom org-agenda-include-diary nil
  1148. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1149. :group 'org-agenda
  1150. :type 'boolean)
  1151. (defcustom org-calendar-to-agenda-key [?c]
  1152. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1153. The command `org-calendar-goto-agenda' will be bound to this key. The
  1154. default is the character `c' because then `c' can be used to switch back and
  1155. forth between agenda and calendar."
  1156. :group 'org-agenda
  1157. :type 'sexp)
  1158. (defgroup org-agenda-window-setup nil
  1159. "Options concerning setting up the Agenda window in Org Mode."
  1160. :tag "Org Agenda Window Setup"
  1161. :group 'org-agenda)
  1162. (defcustom org-select-timeline-window t
  1163. "Non-nil means, after creating a timeline, move cursor into Timeline window.
  1164. When nil, cursor will remain in the current window."
  1165. :group 'org-agenda-window-setup
  1166. :type 'boolean)
  1167. (defcustom org-select-agenda-window t
  1168. "Non-nil means, after creating an agenda, move cursor into Agenda window.
  1169. When nil, cursor will remain in the current window."
  1170. :group 'org-agenda-window-setup
  1171. :type 'boolean)
  1172. (defcustom org-fit-agenda-window t
  1173. "Non-nil means, change window size of agenda to fit content."
  1174. :group 'org-agenda-window-setup
  1175. :type 'boolean)
  1176. (defgroup org-agenda-display nil
  1177. "Options concerning what to display initially in Agenda."
  1178. :tag "Org Agenda Display"
  1179. :group 'org-agenda)
  1180. (defcustom org-agenda-show-all-dates t
  1181. "Non-nil means, `org-agenda' shows every day in the selected range.
  1182. When nil, only the days which actually have entries are shown."
  1183. :group 'org-agenda-display
  1184. :type 'boolean)
  1185. (defcustom org-agenda-start-on-weekday 1
  1186. "Non-nil means, start the overview always on the specified weekday.
  1187. 0 Denotes Sunday, 1 denotes Monday etc.
  1188. When nil, always start on the current day."
  1189. :group 'org-agenda-display
  1190. :type '(choice (const :tag "Today" nil)
  1191. (number :tag "Weekday No.")))
  1192. (defcustom org-agenda-ndays 7
  1193. "Number of days to include in overview display.
  1194. Should be 1 or 7."
  1195. :group 'org-agenda-display
  1196. :type 'number)
  1197. (defcustom org-agenda-use-time-grid t
  1198. "Non-nil means, show a time grid in the agenda schedule.
  1199. A time grid is a set of lines for specific times (like every two hours between
  1200. 8:00 and 20:00). The items scheduled for a day at specific times are
  1201. sorted in between these lines.
  1202. For details about when the grid will be shown, and what it will look like, see
  1203. the variable `org-agenda-time-grid'."
  1204. :group 'org-agenda-display
  1205. :type 'boolean)
  1206. (defcustom org-agenda-time-grid
  1207. '((daily today require-timed)
  1208. "----------------"
  1209. (800 1000 1200 1400 1600 1800 2000))
  1210. "The settings for time grid for agenda display.
  1211. This is a list of three items. The first item is again a list. It contains
  1212. symbols specifying conditions when the grid should be displayed:
  1213. daily if the agenda shows a single day
  1214. weekly if the agenda shows an entire week
  1215. today show grid on current date, independent of daily/weekly display
  1216. require-timed show grid only if at least on item has a time specification
  1217. The second item is a string which will be places behing the grid time.
  1218. The third item is a list of integers, indicating the times that should have
  1219. a grid line."
  1220. :group 'org-agenda-display
  1221. :type
  1222. '(list
  1223. (set :greedy t :tag "Grid Display Options"
  1224. (const :tag "Show grid in single day agenda display" daily)
  1225. (const :tag "Show grid in weekly agenda display" weekly)
  1226. (const :tag "Always show grid for today" today)
  1227. (const :tag "Show grid only if any timed entries are present"
  1228. require-timed)
  1229. (const :tag "Skip grid times already present in an entry"
  1230. remove-match))
  1231. (string :tag "Grid String")
  1232. (repeat :tag "Grid Times" (integer :tag "Time"))))
  1233. (defcustom org-agenda-sorting-strategy '(time-up category-keep priority-down)
  1234. "Sorting structure for the agenda items of a single day.
  1235. This is a list of symbols which will be used in sequence to determine
  1236. if an entry should be listed before another entry. The following
  1237. symbols are recognized:
  1238. time-up Put entries with time-of-day indications first, early first
  1239. time-down Put entries with time-of-day indications first, late first
  1240. category-keep Keep the default order of categories, corresponding to the
  1241. sequence in `org-agenda-files'.
  1242. category-up Sort alphabetically by category, A-Z.
  1243. category-down Sort alphabetically by category, Z-A.
  1244. priority-up Sort numerically by priority, high priority last.
  1245. priority-down Sort numerically by priority, high priority first.
  1246. The different possibilities will be tried in sequence, and testing stops
  1247. if one comparison returns a \"not-equal\". For example, the default
  1248. '(time-up category-keep priority-down)
  1249. means: Pull out all entries having a specified time of day and sort them,
  1250. in order to make a time schedule for the current day the first thing in the
  1251. agenda listing for the day. Of the entries without a time indication, keep
  1252. the grouped in categories, don't sort the categories, but keep them in
  1253. the sequence given in `org-agenda-files'. Within each category sort by
  1254. priority.
  1255. Leaving out `category-keep' would mean that items will be sorted across
  1256. categories by priority."
  1257. :group 'org-agenda-display
  1258. :type '(repeat
  1259. (choice
  1260. (const time-up)
  1261. (const time-down)
  1262. (const category-keep)
  1263. (const category-up)
  1264. (const category-down)
  1265. (const priority-up)
  1266. (const priority-down))))
  1267. (defcustom org-sort-agenda-notime-is-late t
  1268. "Non-nil means, items without time are considered late.
  1269. This is only relevant for sorting. When t, items which have no explicit
  1270. time like 15:30 will be considered as 24:01, i.e. later than any items which
  1271. do have a time. When nil, the default time is before 0:00. You can use this
  1272. option to decide if the schedule for today should come before or after timeless
  1273. agenda entries."
  1274. :group 'org-agenda-display
  1275. :type 'boolean)
  1276. (defgroup org-agenda-prefix nil
  1277. "Options concerning the entry prefix in the Org-mode agenda display."
  1278. :tag "Org Agenda Prefix"
  1279. :group 'org-agenda)
  1280. (defcustom org-agenda-prefix-format " %-12:c%?-12t% s"
  1281. "Format specification for the prefix of items in the agenda buffer.
  1282. This format works similar to a printf format, with the following meaning:
  1283. %c the category of the item, \"Diary\" for entries from the diary, or
  1284. as given by the CATEGORY keyword or derived from the file name.
  1285. %T the first tag of the item.
  1286. %t the time-of-day specification if one applies to the entry, in the
  1287. format HH:MM
  1288. %s Scheduling/Deadline information, a short string
  1289. All specifiers work basically like the standard `%s' of printf, but may
  1290. contain two additional characters: A question mark just after the `%' and
  1291. a whitespace/punctuation character just before the final letter.
  1292. If the first character after `%' is a question mark, the entire field
  1293. will only be included if the corresponding value applies to the
  1294. current entry. This is useful for fields which should have fixed
  1295. width when present, but zero width when absent. For example,
  1296. \"%?-12t\" will result in a 12 character time field if a time of the
  1297. day is specified, but will completely disappear in entries which do
  1298. not contain a time.
  1299. If there is punctuation or whitespace character just before the final
  1300. format letter, this character will be appended to the field value if
  1301. the value is not empty. For example, the format \"%-12:c\" leads to
  1302. \"Diary: \" if the category is \"Diary\". If the category were be
  1303. empty, no additional colon would be interted.
  1304. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  1305. - Indent the line with two space characters
  1306. - Give the category in a 12 chars wide field, padded with whitespace on
  1307. the right (because of `-'). Append a colon if there is a category
  1308. (because of `:').
  1309. - If there is a time-of-day, put it into a 12 chars wide field. If no
  1310. time, don't put in an empty field, just skip it (because of '?').
  1311. - Finally, put the scheduling information and append a whitespace.
  1312. As another example, if you don't want the time-of-day of entries in
  1313. the prefix, you could use:
  1314. (setq org-agenda-prefix-format \" %-11:c% s\")
  1315. See also the variables `org-agenda-remove-times-when-in-prefix' and
  1316. `org-agenda-remove-tags-when-in-prefix'."
  1317. :type 'string
  1318. :group 'org-agenda-prefix)
  1319. (defcustom org-timeline-prefix-format " % s"
  1320. "Like `org-agenda-prefix-format', but for the timeline of a single file."
  1321. :type 'string
  1322. :group 'org-agenda-prefix)
  1323. (defvar org-prefix-format-compiled nil
  1324. "The compiled version of the most recently used prefix format.
  1325. Depending on which command was used last, this may be the compiled version
  1326. of `org-agenda-prefix-format' or `org-timeline-prefix-format'.")
  1327. (defcustom org-agenda-remove-times-when-in-prefix t
  1328. "Non-nil means, remove duplicate time specifications in agenda items.
  1329. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  1330. time-of-day specification in a headline or diary entry is extracted and
  1331. placed into the prefix. If this option is non-nil, the original specification
  1332. \(a timestamp or -range, or just a plain time(range) specification like
  1333. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  1334. cluttered.
  1335. The option can be t or nil. It may also be the symbol `beg', indicating
  1336. that the time should only be removed what it is located at the beginning of
  1337. the headline/diary entry."
  1338. :group 'org-agenda-prefix
  1339. :type '(choice
  1340. (const :tag "Always" t)
  1341. (const :tag "Never" nil)
  1342. (const :tag "When at beginning of entry" beg)))
  1343. (defcustom org-agenda-remove-tags-when-in-prefix nil
  1344. "Non-nil means, the tags from copy of headline in agenda.
  1345. When this is the symbol `prefix', only remove tags when
  1346. `org-agenda-prefix-format' contains a `%T' specifier."
  1347. :group 'org-agenda-prefix
  1348. :type '(choice
  1349. (const :tag "Always" t)
  1350. (const :tag "Never" nil)
  1351. (const :tag "When prefix format contains %T" prefix)))
  1352. (defgroup org-export nil
  1353. "Options for exporting org-listings."
  1354. :tag "Org Export"
  1355. :group 'org)
  1356. (defgroup org-export-general nil
  1357. "General options for exporting Org-mode files."
  1358. :tag "Org Export General"
  1359. :group 'org-export)
  1360. (defcustom org-export-language-setup
  1361. '(("en" "Author" "Date" "Table of Contents")
  1362. ("da" "Ophavsmand" "Dato" "Indhold")
  1363. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  1364. ("es" "Autor" "Fecha" "\xccndice")
  1365. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  1366. ("it" "Autore" "Data" "Indice")
  1367. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  1368. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  1369. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  1370. "Terms used in export text, translated to different languages.
  1371. Use the variable `org-export-default-language' to set the language,
  1372. or use the +OPTION lines for a per-file setting."
  1373. :group 'org-export-general
  1374. :type '(repeat
  1375. (list
  1376. (string :tag "HTML language tag")
  1377. (string :tag "Author")
  1378. (string :tag "Date")
  1379. (string :tag "Table of Contents"))))
  1380. (defcustom org-export-default-language "en"
  1381. "The default language of HTML export, as a string.
  1382. This should have an association in `org-export-language-setup'."
  1383. :group 'org-export-general
  1384. :type 'string)
  1385. (defcustom org-export-headline-levels 3
  1386. "The last level which is still exported as a headline.
  1387. Inferior levels will produce itemize lists when exported.
  1388. Note that a numeric prefix argument to an exporter function overrides
  1389. this setting.
  1390. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  1391. :group 'org-export-general
  1392. :type 'number)
  1393. (defcustom org-export-with-section-numbers t
  1394. "Non-nil means, add section numbers to headlines when exporting.
  1395. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  1396. :group 'org-export-general
  1397. :type 'boolean)
  1398. (defcustom org-export-with-toc t
  1399. "Non-nil means, create a table of contents in exported files.
  1400. The TOC contains headlines with levels up to`org-export-headline-levels'.
  1401. Headlines which contain any TODO items will be marked with \"(*)\" in
  1402. ASCII export, and with red color in HTML output.
  1403. In HTML output, the TOC will be clickable.
  1404. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
  1405. :group 'org-export-general
  1406. :type 'boolean)
  1407. (defcustom org-export-plain-list-max-depth 20
  1408. "Maximum depth of hand-formatted lists in HTML export.
  1409. Org-mode parses hand-formatted enumeration and bullet lists and
  1410. transforms them to HTML open export. Different indentation of the
  1411. bullet or number indicates different list nesting levels. To avoid
  1412. confusion, only a single level is allowed by default. When this is
  1413. larger than 1, deeper indentation leads to deeper list nesting. For
  1414. example, the default value of 3 allows the following list to be
  1415. formatted correctly in HTML:
  1416. * Fruit
  1417. - Apple
  1418. - Banana
  1419. 1. from Africa
  1420. 2. from South America
  1421. - Pineapple
  1422. * Bread
  1423. * Dairy products"
  1424. :group 'org-export-general
  1425. :type 'integer)
  1426. (defcustom org-export-preserve-breaks nil
  1427. "Non-nil means, preserve all line breaks when exporting.
  1428. Normally, in HTML output paragraphs will be reformatted. In ASCII
  1429. export, line breaks will always be preserved, regardless of this variable.
  1430. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  1431. :group 'org-export-general
  1432. :type 'boolean)
  1433. (defcustom org-export-with-emphasize t
  1434. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  1435. If the export target supports emphasizing text, the word will be
  1436. typeset in bold, italic, or underlined, respectively. Works only for
  1437. single words, but you can say: I *really* *mean* *this*.
  1438. Not all export backends support this.
  1439. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  1440. :group 'org-export-general
  1441. :type 'boolean)
  1442. ;; FIXME: Is this in the right place????
  1443. (defcustom org-match-sexp-depth 3
  1444. "Number of stacked braces for sub/superscript matching.
  1445. This has to be set before loading org.el to be effective."
  1446. :group 'org-export-general
  1447. :type 'integer)
  1448. (defcustom org-export-with-sub-superscripts t
  1449. "Non-nil means, interpret \"_\" and \"^\" for export.
  1450. When this option is turned on, you can use TeX-like syntax for sub- and
  1451. superscripts. Several characters after \"_\" or \"^\" will be
  1452. considered as a single item - so grouping with {} is normally not
  1453. needed. For example, the following things will be parsed as single
  1454. sub- or superscripts.
  1455. 10^24 or 10^tau several digits will be considered 1 item.
  1456. 10^-12 or 10^-tau a leading sign with digits or a word
  1457. x^2-y^3 will be read as x^2 - y^3, because items are
  1458. terminated by almost any nonword/nondigit char.
  1459. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  1460. Still, ambiguity is possible - so when in doubt use {} to enclose the
  1461. sub/superscript.
  1462. Not all export backends support this.
  1463. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  1464. :group 'org-export-general
  1465. :type 'boolean)
  1466. (defcustom org-export-with-TeX-macros t
  1467. "Non-nil means, interpret simple TeX-like macros when exporting.
  1468. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  1469. No only real TeX macros will work here, but the standard HTML entities
  1470. for math can be used as macro names as well. For a list of supported
  1471. names in HTML export, see the constant `org-html-entities'.
  1472. Not all export backends support this.
  1473. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  1474. :group 'org-export-general
  1475. :type 'boolean)
  1476. (defcustom org-export-with-fixed-width t
  1477. "Non-nil means, lines starting with \":\" will be in fixed width font.
  1478. This can be used to have pre-formatted text, fragments of code etc. For
  1479. example:
  1480. : ;; Some Lisp examples
  1481. : (while (defc cnt)
  1482. : (ding))
  1483. will be looking just like this in also HTML.
  1484. Not all export backends support this.
  1485. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  1486. :group 'org-export-general
  1487. :type 'boolean)
  1488. (defcustom org-export-with-tables t
  1489. "If non-nil, lines starting with \"|\" define a table.
  1490. For example:
  1491. | Name | Address | Birthday |
  1492. |-------------+----------+-----------|
  1493. | Arthur Dent | England | 29.2.2100 |
  1494. Not all export backends support this.
  1495. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  1496. :group 'org-export-general
  1497. :type 'boolean)
  1498. (defcustom org-export-highlight-first-table-line t
  1499. "Non-nil means, highlight the first table line.
  1500. In HTML export, this means use <th> instead of <td>.
  1501. In tables created with table.el, this applies to the first table line.
  1502. In Org-mode tables, all lines before the first horizontal separator
  1503. line will be formatted with <th> tags."
  1504. :group 'org-export-general
  1505. :type 'boolean)
  1506. (defcustom org-export-table-remove-special-lines t
  1507. "Remove special lines and marking characters in calculating tables.
  1508. This removes the special marking character column from tables that are set
  1509. up for spreadsheet calculations. It also removes the entire lines
  1510. marked with `!', `_', or `^'. The lines with `$' are kept, because
  1511. the values of constants may be useful to have."
  1512. :group 'org-export-general
  1513. :type 'boolean)
  1514. (defcustom org-export-prefer-native-exporter-for-tables nil
  1515. "Non-nil means, always export tables created with table.el natively.
  1516. Natively means, use the HTML code generator in table.el.
  1517. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  1518. the table does not use row- or column-spanning). This has the
  1519. advantage, that the automatic HTML conversions for math symbols and
  1520. sub/superscripts can be applied. Org-mode's HTML generator is also
  1521. much faster."
  1522. :group 'org-export-general
  1523. :type 'boolean)
  1524. (defgroup org-export-ascii nil
  1525. "Options specific for ASCII export of Org-mode files."
  1526. :tag "Org Export ASCII"
  1527. :group 'org-export)
  1528. (defcustom org-export-ascii-show-new-buffer t
  1529. "Non-nil means, popup buffer containing the exported ASCII text.
  1530. Otherwise the buffer will just be saved to a file and stay hidden."
  1531. :group 'org-export-ascii
  1532. :type 'boolean)
  1533. (defgroup org-export-xml nil
  1534. "Options specific for XML export of Org-mode files."
  1535. :tag "Org Export XML"
  1536. :group 'org-export)
  1537. (defcustom org-export-xml-type 'xoxo
  1538. "The kind of XML to be produced by the XML exporter.
  1539. Allowed values are:
  1540. xoxo The XOXO exporter."
  1541. :group 'org-export-xml
  1542. :type '(choice
  1543. (const :tag "XOXO" xoxo)))
  1544. (defgroup org-export-html nil
  1545. "Options specific for HTML export of Org-mode files."
  1546. :tag "Org Export HTML"
  1547. :group 'org-export)
  1548. (defcustom org-export-html-style
  1549. "<style type=\"text/css\">
  1550. html {
  1551. font-family: Times, serif;
  1552. font-size: 12pt;
  1553. }
  1554. .title { text-align: center; }
  1555. .todo, .deadline { color: red; }
  1556. .done { color: green; }
  1557. .target { background-color: lavender; }
  1558. pre {
  1559. border: 1pt solid #AEBDCC;
  1560. background-color: #F3F5F7;
  1561. padding: 5pt;
  1562. font-family: courier, monospace;
  1563. }
  1564. table { border-collapse: collapse; }
  1565. td, th {
  1566. vertical-align: top;
  1567. border: 1pt solid #ADB9CC;
  1568. }
  1569. </style>"
  1570. "The default style specification for exported HTML files.
  1571. Since there are different ways of setting style information, this variable
  1572. needs to contain the full HTML structure to provide a style, including the
  1573. surrounding HTML tags. The style specifications should include definitions
  1574. for new classes todo, done, title, and deadline. For example, legal values
  1575. would be:
  1576. <style type=\"text/css\">
  1577. p { font-weight: normal; color: gray; }
  1578. h1 { color: black; }
  1579. .title { text-align: center; }
  1580. .todo, .deadline { color: red; }
  1581. .done { color: green; }
  1582. </style>
  1583. or, if you want to keep the style in a file,
  1584. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1585. As the value of this option simply gets inserted into the HTML <head> header,
  1586. you can \"misuse\" it to add arbitrary text to the header."
  1587. :group 'org-export-html
  1588. :type 'string)
  1589. (defcustom org-export-html-inline-images t
  1590. "Non-nil means, inline images into exported HTML pages.
  1591. The link will still be to the original location of the image file.
  1592. So if you are moving the page, lets say to your public HTML site,
  1593. you will have to move the image and maybe change the link."
  1594. :group 'org-export-html
  1595. :type 'boolean)
  1596. (defcustom org-export-html-expand t
  1597. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  1598. When nil, these tags will be exported as plain text and therefore
  1599. not be interpreted by a browser.
  1600. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  1601. :group 'org-export-html
  1602. :type 'boolean)
  1603. (defcustom org-export-html-table-tag
  1604. "<table border=1 cellspacing=0 cellpadding=6>"
  1605. "The HTML tag used to start a table.
  1606. This must be a <table> tag, but you may change the options like
  1607. borders and spacing."
  1608. :group 'org-export-html
  1609. :type 'string)
  1610. (defcustom org-export-html-with-timestamp nil
  1611. "If non-nil, write `org-export-html-html-helper-timestamp'
  1612. into the exported HTML text. Otherwise, the buffer will just be saved
  1613. to a file."
  1614. :group 'org-export-html
  1615. :type 'boolean)
  1616. (defcustom org-export-html-html-helper-timestamp
  1617. "<br><br><hr><p><!-- hhmts start --> <!-- hhmts end -->\n"
  1618. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  1619. :group 'org-export-html
  1620. :type 'string)
  1621. (defcustom org-export-html-show-new-buffer nil
  1622. "Non-nil means, popup buffer containing the exported html text.
  1623. Otherwise, the buffer will just be saved to a file and stay hidden."
  1624. :group 'org-export-html
  1625. :type 'boolean)
  1626. (defgroup org-export-icalendar nil
  1627. "Options specific for iCalendar export of Org-mode files."
  1628. :tag "Org Export iCalendar"
  1629. :group 'org-export)
  1630. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  1631. "The file name for the iCalendar file covering all agenda files.
  1632. This file is created with the command \\[org-export-icalendar-all-agenda-files]."
  1633. :group 'org-export-icalendar
  1634. :type 'file)
  1635. (defcustom org-icalendar-include-todo nil
  1636. "Non-nil means, export to iCalendar files should also cover TODO items."
  1637. :group 'org-export-icalendar
  1638. :type 'boolean)
  1639. (defcustom org-icalendar-combined-name "OrgMode"
  1640. "Calendar name for the combined iCalendar representing all agenda files."
  1641. :group 'org-export-icalendar
  1642. :type 'string)
  1643. (defgroup org-font-lock nil
  1644. "Font-lock settings for highlighting in Org-mode."
  1645. :tag "Org Font Lock"
  1646. :group 'org)
  1647. (defcustom org-level-color-stars-only nil
  1648. "Non-nil means fontify only the stars in each headline.
  1649. When nil, the entire headline is fontified.
  1650. Changing it requires restart of `font-lock-mode' to become effective
  1651. also in regions already fontified."
  1652. :group 'org-font-lock
  1653. :type 'boolean)
  1654. (defcustom org-hide-leading-stars nil
  1655. "Non-nil means, hide the first N-1 stars in a headline.
  1656. This works by using the face `org-hide' for these stars. This
  1657. face is white for a light background, and black for a dark
  1658. background. You may have to customize the face `org-hide' to
  1659. make this work.
  1660. Changing it requires restart of `font-lock-mode' to become effective
  1661. also in regions already fontified."
  1662. :group 'org-font-lock
  1663. :type 'boolean)
  1664. (defcustom org-fontify-done-headline nil
  1665. "Non-nil means, change the face of a headline if it is marked DONE.
  1666. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1667. When this is non-nil, the headline after the keyword is set to the
  1668. `org-headline-done' as an additional indication."
  1669. :group 'org-font-lock
  1670. :type 'boolean)
  1671. (defcustom org-fontify-emphasized-text t
  1672. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1673. Changing this variable requires a restart of Emacs to take effect."
  1674. :group 'org-font-lock
  1675. :type 'boolean)
  1676. (defgroup org-faces nil
  1677. "Faces in Org-mode."
  1678. :tag "Org Faces"
  1679. :group 'org-font-lock)
  1680. (defface org-hide
  1681. '(
  1682. (((type tty) (class color)) (:foreground "white"))
  1683. (((class color) (background light)) (:foreground "white"))
  1684. (((class color) (background dark)) (:foreground "black"))
  1685. (t (:inverse-video nil)))
  1686. "Face used for level 1 headlines."
  1687. :group 'org-faces)
  1688. (defface org-level-1 ;; font-lock-function-name-face
  1689. '((((type tty) (class color)) (:foreground "blue" :weight bold))
  1690. (((class color) (background light)) (:foreground "Blue"))
  1691. (((class color) (background dark)) (:foreground "LightSkyBlue"))
  1692. (t (:inverse-video t :bold t)))
  1693. "Face used for level 1 headlines."
  1694. :group 'org-faces)
  1695. (defface org-level-2 ;; font-lock-variable-name-face
  1696. '((((type tty) (class color)) (:foreground "yellow" :weight light))
  1697. (((class color) (background light)) (:foreground "DarkGoldenrod"))
  1698. (((class color) (background dark)) (:foreground "LightGoldenrod"))
  1699. (t (:bold t :italic t)))
  1700. "Face used for level 2 headlines."
  1701. :group 'org-faces)
  1702. (defface org-level-3 ;; font-lock-keyword-face
  1703. '((((type tty) (class color)) (:foreground "cyan" :weight bold))
  1704. (((class color) (background light)) (:foreground "Purple"))
  1705. (((class color) (background dark)) (:foreground "Cyan"))
  1706. (t (:bold t)))
  1707. "Face used for level 3 headlines."
  1708. :group 'org-faces)
  1709. (defface org-level-4 ;; font-lock-comment-face
  1710. '((((type tty pc) (class color) (background light)) (:foreground "red"))
  1711. (((type tty pc) (class color) (background dark)) (:foreground "red1"))
  1712. (((class color) (background light)) (:foreground "Firebrick"))
  1713. (((class color) (background dark)) (:foreground "chocolate1"))
  1714. (t (:bold t :italic t)))
  1715. "Face used for level 4 headlines."
  1716. :group 'org-faces)
  1717. (defface org-level-5 ;; font-lock-type-face
  1718. '((((type tty) (class color)) (:foreground "green"))
  1719. (((class color) (background light)) (:foreground "ForestGreen"))
  1720. (((class color) (background dark)) (:foreground "PaleGreen"))
  1721. (t (:bold t :underline t)))
  1722. "Face used for level 5 headlines."
  1723. :group 'org-faces)
  1724. (defface org-level-6 ;; font-lock-constant-face
  1725. '((((type tty) (class color)) (:foreground "magenta"))
  1726. (((class color) (background light)) (:foreground "CadetBlue"))
  1727. (((class color) (background dark)) (:foreground "Aquamarine"))
  1728. (t (:bold t :underline t)))
  1729. "Face used for level 6 headlines."
  1730. :group 'org-faces)
  1731. (defface org-level-7 ;; font-lock-builtin-face
  1732. '((((type tty) (class color)) (:foreground "blue" :weight light))
  1733. (((class color) (background light)) (:foreground "Orchid"))
  1734. (((class color) (background dark)) (:foreground "LightSteelBlue"))
  1735. (t (:bold t)))
  1736. "Face used for level 7 headlines."
  1737. :group 'org-faces)
  1738. (defface org-level-8 ;; font-lock-string-face
  1739. '((((type tty) (class color)) (:foreground "green"))
  1740. (((class color) (background light)) (:foreground "RosyBrown"))
  1741. (((class color) (background dark)) (:foreground "LightSalmon"))
  1742. (t (:italic t)))
  1743. "Face used for level 8 headlines."
  1744. :group 'org-faces)
  1745. (defface org-special-keyword ;; font-lock-string-face
  1746. '((((type tty) (class color)) (:foreground "green"))
  1747. (((class color) (background light)) (:foreground "RosyBrown"))
  1748. (((class color) (background dark)) (:foreground "LightSalmon"))
  1749. (t (:italic t)))
  1750. "Face used for special keywords."
  1751. :group 'org-faces)
  1752. (defface org-warning ;; font-lock-warning-face
  1753. '((((type tty) (class color)) (:foreground "red"))
  1754. (((class color) (background light)) (:foreground "Red" :bold t))
  1755. (((class color) (background dark)) (:foreground "Red1" :bold t))
  1756. ; (((class color) (background dark)) (:foreground "Pink" :bold t))
  1757. (t (:inverse-video t :bold t)))
  1758. "Face for deadlines and TODO keywords."
  1759. :group 'org-faces)
  1760. (defface org-headline-done ;; font-lock-string-face
  1761. '((((type tty) (class color)) (:foreground "green"))
  1762. (((class color) (background light)) (:foreground "RosyBrown"))
  1763. (((class color) (background dark)) (:foreground "LightSalmon"))
  1764. (t (:italic t)))
  1765. "Face used to indicate that a headline is DONE. See also the variable
  1766. `org-fontify-done-headline'."
  1767. :group 'org-faces)
  1768. ;; Inheritance does not work for xemacs. So we just copy...
  1769. (defface org-deadline-announce
  1770. '((((type tty) (class color)) (:foreground "blue" :weight bold))
  1771. (((class color) (background light)) (:foreground "Blue"))
  1772. (((class color) (background dark)) (:foreground "LightSkyBlue"))
  1773. (t (:inverse-video t :bold t)))
  1774. "Face for upcoming deadlines."
  1775. :group 'org-faces)
  1776. (defface org-scheduled-today
  1777. '((((type tty) (class color)) (:foreground "green"))
  1778. (((class color) (background light)) (:foreground "DarkGreen"))
  1779. (((class color) (background dark)) (:foreground "PaleGreen"))
  1780. (t (:bold t :underline t)))
  1781. "Face for items scheduled for a certain day."
  1782. :group 'org-faces)
  1783. (defface org-scheduled-previously
  1784. '((((type tty pc) (class color) (background light)) (:foreground "red"))
  1785. (((type tty pc) (class color) (background dark)) (:foreground "red1"))
  1786. (((class color) (background light)) (:foreground "Firebrick"))
  1787. (((class color) (background dark)) (:foreground "chocolate1"))
  1788. (t (:bold t :italic t)))
  1789. "Face for items scheduled previously, and not yet done."
  1790. :group 'org-faces)
  1791. (defface org-formula
  1792. '((((type tty pc) (class color) (background light)) (:foreground "red"))
  1793. (((type tty pc) (class color) (background dark)) (:foreground "red1"))
  1794. (((class color) (background light)) (:foreground "Firebrick"))
  1795. (((class color) (background dark)) (:foreground "chocolate1"))
  1796. (t (:bold t :italic t)))
  1797. "Face for formulas."
  1798. :group 'org-faces)
  1799. (defface org-link
  1800. '((((type tty) (class color)) (:foreground "cyan" :weight bold))
  1801. (((class color) (background light)) (:foreground "Purple" :underline t))
  1802. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  1803. (t (:bold t)))
  1804. "Face for links."
  1805. :group 'org-faces)
  1806. (defface org-tag
  1807. '((((type tty) (class color)) (:weight bold))
  1808. (((class color) (background light)) (:weight bold))
  1809. (((class color) (background dark)) (:weight bold))
  1810. (t (:bold t)))
  1811. "Face for tags."
  1812. :group 'org-faces)
  1813. (defface org-done ;; font-lock-type-face
  1814. '((((type tty) (class color)) (:foreground "green"))
  1815. (((class color) (background light)) (:foreground "ForestGreen" :bold t))
  1816. (((class color) (background dark)) (:foreground "PaleGreen" :bold t))
  1817. (t (:bold t :underline t)))
  1818. "Face used for DONE."
  1819. :group 'org-faces)
  1820. (defface org-table ;; font-lock-function-name-face
  1821. '((((type tty) (class color)) (:foreground "blue" :weight bold))
  1822. (((class color) (background light)) (:foreground "Blue"))
  1823. (((class color) (background dark)) (:foreground "LightSkyBlue"))
  1824. (t (:inverse-video t :bold t)))
  1825. "Face used for tables."
  1826. :group 'org-faces)
  1827. (defface org-time-grid ;; font-lock-variable-name-face
  1828. '((((type tty) (class color)) (:foreground "yellow" :weight light))
  1829. (((class color) (background light)) (:foreground "DarkGoldenrod"))
  1830. (((class color) (background dark)) (:foreground "LightGoldenrod"))
  1831. (t (:bold t :italic t)))
  1832. "Face used for time grids."
  1833. :group 'org-faces)
  1834. (defvar org-level-faces
  1835. '(org-level-1 org-level-2 org-level-3 org-level-4
  1836. org-level-5 org-level-6 org-level-7 org-level-8
  1837. ))
  1838. (defvar org-n-levels (length org-level-faces))
  1839. ;; Variables for pre-computed regular expressions, all buffer local
  1840. (defvar org-done-string nil
  1841. "The last string in `org-todo-keywords', indicating an item is DONE.")
  1842. (make-variable-buffer-local 'org-done-string)
  1843. (defvar org-todo-regexp nil
  1844. "Matches any of the TODO state keywords.")
  1845. (make-variable-buffer-local 'org-todo-regexp)
  1846. (defvar org-not-done-regexp nil
  1847. "Matches any of the TODO state keywords except the last one.")
  1848. (make-variable-buffer-local 'org-not-done-regexp)
  1849. (defvar org-todo-line-regexp nil
  1850. "Matches a headline and puts TODO state into group 2 if present.")
  1851. (make-variable-buffer-local 'org-todo-line-regexp)
  1852. (defvar org-nl-done-regexp nil
  1853. "Matches newline followed by a headline with the DONE keyword.")
  1854. (make-variable-buffer-local 'org-nl-done-regexp)
  1855. (defvar org-looking-at-done-regexp nil
  1856. "Matches the DONE keyword a point.")
  1857. (make-variable-buffer-local 'org-looking-at-done-regexp)
  1858. (defvar org-todo-kwd-priority-p nil
  1859. "Do TODO items have priorities?")
  1860. (make-variable-buffer-local 'org-todo-kwd-priority-p)
  1861. (defvar org-todo-kwd-max-priority nil
  1862. "Maximum priority of TODO items.")
  1863. (make-variable-buffer-local 'org-todo-kwd-max-priority)
  1864. (defvar org-ds-keyword-length 12
  1865. "Maximum length of the Deadline and SCHEDULED keywords.")
  1866. (make-variable-buffer-local 'org-ds-keyword-length)
  1867. (defvar org-deadline-regexp nil
  1868. "Matches the DEADLINE keyword.")
  1869. (make-variable-buffer-local 'org-deadline-regexp)
  1870. (defvar org-deadline-time-regexp nil
  1871. "Matches the DEADLINE keyword together with a time stamp.")
  1872. (make-variable-buffer-local 'org-deadline-time-regexp)
  1873. (defvar org-deadline-line-regexp nil
  1874. "Matches the DEADLINE keyword and the rest of the line.")
  1875. (make-variable-buffer-local 'org-deadline-line-regexp)
  1876. (defvar org-scheduled-regexp nil
  1877. "Matches the SCHEDULED keyword.")
  1878. (make-variable-buffer-local 'org-scheduled-regexp)
  1879. (defvar org-scheduled-time-regexp nil
  1880. "Matches the SCHEDULED keyword together with a time stamp.")
  1881. (make-variable-buffer-local 'org-scheduled-time-regexp)
  1882. (defun org-set-regexps-and-options ()
  1883. "Precompute regular expressions for current buffer."
  1884. (when (eq major-mode 'org-mode)
  1885. (let ((re (org-make-options-regexp
  1886. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  1887. "STARTUP" "ARCHIVE")))
  1888. (splitre "[ \t]+")
  1889. kwds int key value cat arch)
  1890. (save-excursion
  1891. (save-restriction
  1892. (widen)
  1893. (goto-char (point-min))
  1894. (while (re-search-forward re nil t)
  1895. (setq key (match-string 1) value (match-string 2))
  1896. (cond
  1897. ((equal key "CATEGORY")
  1898. (if (string-match "[ \t]+$" value)
  1899. (setq value (replace-match "" t t value)))
  1900. (setq cat (intern value)))
  1901. ((equal key "SEQ_TODO")
  1902. (setq int 'sequence
  1903. kwds (append kwds (org-split-string value splitre))))
  1904. ((equal key "PRI_TODO")
  1905. (setq int 'priority
  1906. kwds (append kwds (org-split-string value splitre))))
  1907. ((equal key "TYP_TODO")
  1908. (setq int 'type
  1909. kwds (append kwds (org-split-string value splitre))))
  1910. ((equal key "STARTUP")
  1911. (let ((opts (org-split-string value splitre))
  1912. (set '(("fold" org-startup-folded t)
  1913. ("nofold" org-startup-folded nil)
  1914. ("content" org-startup-folded content)
  1915. ("hidestars" org-hide-leading-stars t)
  1916. ("showstars" org-hide-leading-stars nil)
  1917. ("odd" org-odd-levels-only t)
  1918. ("oddeven" org-odd-levels-only nil)
  1919. ("align" org-startup-align-all-tables t)
  1920. ("noalign" org-startup-align-all-tables nil)
  1921. ("dlcheck" org-startup-with-deadline-check t)
  1922. ("nodlcheck" org-startup-with-deadline-check nil)))
  1923. l var val)
  1924. (while (setq l (assoc (pop opts) set))
  1925. (setq var (nth 1 l) val (nth 2 l))
  1926. (set (make-local-variable var) val))))
  1927. ((equal key "ARCHIVE")
  1928. (string-match " *$" value)
  1929. (setq arch (replace-match "" t t value))
  1930. (remove-text-properties 0 (length arch)
  1931. '(face t fontified t) arch)))
  1932. )))
  1933. (and cat (set (make-local-variable 'org-category) cat))
  1934. (and kwds (set (make-local-variable 'org-todo-keywords) kwds))
  1935. (and arch (set (make-local-variable 'org-archive-location) arch))
  1936. (and int (set (make-local-variable 'org-todo-interpretation) int)))
  1937. ;; Compute the regular expressions and other local variables
  1938. (setq org-todo-kwd-priority-p (equal org-todo-interpretation 'priority)
  1939. org-todo-kwd-max-priority (1- (length org-todo-keywords))
  1940. org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  1941. (length org-scheduled-string)))
  1942. org-done-string
  1943. (nth (1- (length org-todo-keywords)) org-todo-keywords)
  1944. org-todo-regexp
  1945. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
  1946. "\\|") "\\)\\>")
  1947. org-not-done-regexp
  1948. (concat "\\<\\("
  1949. (mapconcat 'regexp-quote
  1950. (nreverse (cdr (reverse org-todo-keywords)))
  1951. "\\|")
  1952. "\\)\\>")
  1953. org-todo-line-regexp
  1954. (concat "^\\(\\*+\\)[ \t]*\\("
  1955. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  1956. "\\)? *\\(.*\\)")
  1957. org-nl-done-regexp
  1958. (concat "[\r\n]\\*+[ \t]+" org-done-string "\\>")
  1959. org-looking-at-done-regexp (concat "^" org-done-string "\\>")
  1960. org-deadline-regexp (concat "\\<" org-deadline-string)
  1961. org-deadline-time-regexp
  1962. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  1963. org-deadline-line-regexp
  1964. (concat "\\<\\(" org-deadline-string "\\).*")
  1965. org-scheduled-regexp
  1966. (concat "\\<" org-scheduled-string)
  1967. org-scheduled-time-regexp
  1968. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  1969. (org-set-font-lock-defaults)))
  1970. ;; Tell the compiler about dynamically scoped variables,
  1971. ;; and variables from other packages
  1972. (defvar zmacs-regions) ; XEmacs regions
  1973. (defvar original-date) ; dynamically scoped in calendar
  1974. (defvar org-old-auto-fill-inhibit-regexp) ; local variable used by `orgtbl-mode'
  1975. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  1976. (defvar org-html-entities) ; defined later in this file
  1977. (defvar org-goto-start-pos) ; dynamically scoped parameter
  1978. (defvar org-time-was-given) ; dynamically scoped parameter
  1979. (defvar org-ts-what) ; dynamically scoped parameter
  1980. (defvar mark-active) ; Emacs only, not available in XEmacs.
  1981. (defvar timecnt) ; dynamically scoped parameter
  1982. (defvar levels-open) ; dynamically scoped parameter
  1983. (defvar title) ; dynamically scoped parameter
  1984. (defvar author) ; dynamically scoped parameter
  1985. (defvar email) ; dynamically scoped parameter
  1986. (defvar text) ; dynamically scoped parameter
  1987. (defvar entry) ; dynamically scoped parameter
  1988. (defvar date) ; dynamically scoped parameter
  1989. (defvar language) ; dynamically scoped parameter
  1990. (defvar options) ; dynamically scoped parameter
  1991. (defvar ans1) ; dynamically scoped parameter
  1992. (defvar ans2) ; dynamically scoped parameter
  1993. (defvar starting-day) ; local variable
  1994. (defvar include-all-loc) ; local variable
  1995. (defvar vm-message-pointer) ; from vm
  1996. (defvar vm-folder-directory) ; from vm
  1997. (defvar wl-summary-buffer-elmo-folder) ; from wanderlust
  1998. (defvar wl-summary-buffer-folder-name) ; from wanderlust
  1999. (defvar gnus-group-name) ; from gnus
  2000. (defvar gnus-article-current) ; from gnus
  2001. (defvar w3m-current-url) ; from w3m
  2002. (defvar mh-progs) ; from MH-E
  2003. (defvar mh-current-folder) ; from MH-E
  2004. (defvar mh-show-folder-buffer) ; from MH-E
  2005. (defvar mh-index-folder) ; from MH-E
  2006. (defvar mh-searcher) ; from MH-E
  2007. (defvar org-selected-point) ; dynamically scoped parameter
  2008. (defvar calendar-mode-map) ; from calendar.el
  2009. (defvar last-arg) ; local variable
  2010. (defvar remember-save-after-remembering) ; from remember.el
  2011. (defvar remember-data-file) ; from remember.el
  2012. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  2013. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  2014. (defvar orgtbl-mode) ; defined later in this file
  2015. ;;; Define the mode
  2016. (defvar org-mode-map (copy-keymap outline-mode-map)
  2017. "Keymap for Org-mode.")
  2018. (defvar org-struct-menu) ; defined later in this file
  2019. (defvar org-org-menu) ; defined later in this file
  2020. (defvar org-tbl-menu) ; defined later in this file
  2021. ;; We use a before-change function to check if a table might need
  2022. ;; an update.
  2023. (defvar org-table-may-need-update t
  2024. "Indicates that a table might need an update.
  2025. This variable is set by `org-before-change-function'.
  2026. `org-table-align' sets it back to nil.")
  2027. (defvar org-mode-hook nil)
  2028. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2029. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2030. ;;;###autoload
  2031. (define-derived-mode org-mode outline-mode "Org"
  2032. "Outline-based notes management and organizer, alias
  2033. \"Carsten's outline-mode for keeping track of everything.\"
  2034. Org-mode develops organizational tasks around a NOTES file which
  2035. contains information about projects as plain text. Org-mode is
  2036. implemented on top of outline-mode, which is ideal to keep the content
  2037. of large files well structured. It supports ToDo items, deadlines and
  2038. time stamps, which magically appear in the diary listing of the Emacs
  2039. calendar. Tables are easily created with a built-in table editor.
  2040. Plain text URL-like links connect to websites, emails (VM), Usenet
  2041. messages (Gnus), BBDB entries, and any files related to the project.
  2042. For printing and sharing of notes, an Org-mode file (or a part of it)
  2043. can be exported as a structured ASCII or HTML file.
  2044. The following commands are available:
  2045. \\{org-mode-map}"
  2046. (easy-menu-add org-org-menu)
  2047. (easy-menu-add org-tbl-menu)
  2048. (org-install-agenda-files-menu)
  2049. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link . nil)))
  2050. (org-add-to-invisibility-spec '(org-cwidth . nil))
  2051. (setq outline-regexp "\\*+")
  2052. ;;(setq outline-regexp "\\(?:\\*+\\|[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\) \\)")
  2053. (setq outline-level 'org-outline-level)
  2054. (when (and org-ellipsis (stringp org-ellipsis))
  2055. (unless org-display-table
  2056. (setq org-display-table (make-display-table)))
  2057. (set-display-table-slot org-display-table
  2058. 4 (string-to-vector org-ellipsis))
  2059. (setq buffer-display-table org-display-table))
  2060. (org-set-regexps-and-options)
  2061. (if org-startup-truncated (setq truncate-lines t))
  2062. (set (make-local-variable 'font-lock-unfontify-region-function)
  2063. 'org-unfontify-region)
  2064. ;; Activate before-change-function
  2065. (set (make-local-variable 'org-table-may-need-update) t)
  2066. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2067. 'local)
  2068. ;; Paragraphs and auto-filling
  2069. (org-set-autofill-regexps)
  2070. (org-update-radio-target-regexp)
  2071. ;; Settings for Calc embedded mode
  2072. (set (make-local-variable 'calc-embedded-open-formula) "|\\|\n")
  2073. (set (make-local-variable 'calc-embedded-close-formula) "|\\|\n")
  2074. (if (and org-insert-mode-line-in-empty-file
  2075. (interactive-p)
  2076. (= (point-min) (point-max)))
  2077. (insert " -*- mode: org -*-\n\n"))
  2078. ;; Get rid of Outline menus, they are not needed
  2079. ;; Need to do this here because define-derived-mode sets up
  2080. ;; the keymap so late.
  2081. (if (featurep 'xemacs)
  2082. (progn
  2083. (delete-menu-item '("Headings"))
  2084. (delete-menu-item '("Show"))
  2085. (delete-menu-item '("Hide"))
  2086. (set-menubar-dirty-flag))
  2087. (define-key org-mode-map [menu-bar headings] 'undefined)
  2088. (define-key org-mode-map [menu-bar hide] 'undefined)
  2089. (define-key org-mode-map [menu-bar show] 'undefined))
  2090. (unless org-inhibit-startup
  2091. (if org-startup-align-all-tables
  2092. (org-table-map-tables 'org-table-align))
  2093. (if org-startup-with-deadline-check
  2094. (call-interactively 'org-check-deadlines)
  2095. (cond
  2096. ((eq org-startup-folded t)
  2097. (org-cycle '(4)))
  2098. ((eq org-startup-folded 'content)
  2099. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2100. (org-cycle '(4)) (org-cycle '(4))))))))
  2101. (defsubst org-current-line (&optional pos)
  2102. (+ (if (bolp) 1 0) (count-lines (point-min) (or pos (point)))))
  2103. (defsubst org-match-string-no-properties (num &optional string)
  2104. (if (featurep 'xemacs)
  2105. (match-string num string)
  2106. (match-string-no-properties num string)))
  2107. (defun org-current-time ()
  2108. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2109. (if (> org-time-stamp-rounding-minutes 0)
  2110. (let ((r org-time-stamp-rounding-minutes)
  2111. (time (decode-time)))
  2112. (apply 'encode-time
  2113. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2114. (nthcdr 2 time))))
  2115. (current-time)))
  2116. ;; FIXME: `propertize' does copy the string first...
  2117. (defun org-string-props (string &rest properties)
  2118. "Add PROPERTIES to string."
  2119. (add-text-properties 0 (length string) properties string)
  2120. string)
  2121. ;;; Font-Lock stuff
  2122. (defvar org-mouse-map (make-sparse-keymap))
  2123. (define-key org-mouse-map
  2124. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2125. (define-key org-mouse-map
  2126. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2127. (define-key org-mouse-map [follow-link] 'mouse-face)
  2128. (when org-tab-follows-link
  2129. (define-key org-mouse-map [(tab)] 'org-open-at-point)
  2130. (define-key org-mouse-map "\C-i" 'org-open-at-point))
  2131. (when org-return-follows-link
  2132. (define-key org-mouse-map [(return)] 'org-open-at-point)
  2133. (define-key org-mouse-map "\C-m" 'org-open-at-point))
  2134. (require 'font-lock)
  2135. (defconst org-non-link-chars "]\t\n\r|<>\000")
  2136. (defconst org-link-types '("https?" "ftp" "mailto" "file" "news" "bbdb" "vm"
  2137. "wl" "mhe" "rmail" "gnus" "shell"))
  2138. (defconst org-link-re-with-space
  2139. (concat
  2140. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2141. "\\([^" org-non-link-chars " ]"
  2142. "[^" org-non-link-chars "]*"
  2143. "[^" org-non-link-chars " ]\\)>?"))
  2144. (defconst org-link-re-no-space
  2145. (concat
  2146. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2147. "\\([^" org-non-link-chars " ]+\\)>?"))
  2148. (defconst org-link-regexp
  2149. (if org-allow-space-in-plain-links
  2150. org-link-re-with-space
  2151. org-link-re-no-space)
  2152. "Regular expression for matching links in plain text.")
  2153. (defconst org-external-link-maybe-brackets
  2154. (concat "\\(\\[\\[\\)?\\(" org-link-regexp "\\)\\(\\]\\(\\]\\|\\[\\)\\)?")
  2155. "Matches a link and optionally surrounding brackets.")
  2156. (defconst org-bracket-link-regexp
  2157. "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
  2158. "Matches a link in double brackets.")
  2159. (defconst org-bracket-link-analytic-regexp
  2160. (concat
  2161. "\\[\\["
  2162. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2163. "\\([^]]+\\)"
  2164. "\\]"
  2165. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2166. "\\]"))
  2167. ; 1: http:
  2168. ; 2: http
  2169. ; 3: path
  2170. ; 4: [desc]
  2171. ; 5: desc
  2172. (defconst org-ts-lengths
  2173. (cons (length (format-time-string (car org-time-stamp-formats)))
  2174. (length (format-time-string (cdr org-time-stamp-formats))))
  2175. "This holds the lengths of the two different time formats.")
  2176. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)>"
  2177. "Regular expression for fast time stamp matching.")
  2178. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)[]>]"
  2179. "Regular expression for fast time stamp matching.")
  2180. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2181. "Regular expression matching time strings for analysis.")
  2182. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
  2183. "Regular expression matching time stamps, with groups.")
  2184. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2185. "Regular expression matching a time stamp range.")
  2186. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2187. org-ts-regexp "\\)?")
  2188. "Regular expression matching a time stamp or time stamp range.")
  2189. (defun org-activate-links (limit)
  2190. "Run through the buffer and add overlays to links."
  2191. (if (re-search-forward org-link-regexp limit t)
  2192. (progn
  2193. (add-text-properties (match-beginning 0) (match-end 0)
  2194. (list 'mouse-face 'highlight
  2195. 'keymap org-mouse-map
  2196. ))
  2197. t)))
  2198. (defun org-activate-links2 (limit)
  2199. "Run through the buffer and add overlays to bracketed links."
  2200. (if (re-search-forward org-bracket-link-regexp limit t)
  2201. (let* ((help (concat "LINK: " (org-match-string-no-properties 1)))
  2202. (ip (list 'invisible 'org-link 'intangible t 'rear-nonsticky t
  2203. 'keymap org-mouse-map 'mouse-face 'highlight
  2204. 'help-echo help))
  2205. (vp (list 'rear-nonsticky t
  2206. 'keymap org-mouse-map 'mouse-face 'highlight
  2207. 'help-echo help)))
  2208. ;; We need to remove the invisible property here. Table narrowing
  2209. ;; may have made some of this invisible.
  2210. (remove-text-properties (match-beginning 0) (match-end 0)
  2211. '(invisible nil))
  2212. (if (match-end 3)
  2213. (progn
  2214. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  2215. (add-text-properties (match-beginning 3) (match-end 3) vp)
  2216. (add-text-properties (match-end 3) (match-end 0) ip))
  2217. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  2218. (add-text-properties (match-beginning 1) (match-end 1) vp)
  2219. (add-text-properties (match-end 1) (match-end 0) ip))
  2220. t)))
  2221. (defun org-activate-dates (limit)
  2222. "Run through the buffer and add overlays to dates."
  2223. (if (re-search-forward org-tsr-regexp limit t)
  2224. (progn
  2225. (add-text-properties (match-beginning 0) (match-end 0)
  2226. (list 'mouse-face 'highlight
  2227. 'keymap org-mouse-map))
  2228. t)))
  2229. (defvar org-target-link-regexp nil
  2230. "Regular expression matching radio targets in plain text.")
  2231. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  2232. "Regular expression matching a link target.")
  2233. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  2234. "Regular expression matching a link target.")
  2235. (defun org-activate-target-links (limit)
  2236. "Run through the buffer and add overlays to target matches."
  2237. (when (and org-radio-targets org-target-link-regexp)
  2238. (let ((case-fold-search t))
  2239. (if (re-search-forward org-target-link-regexp limit t)
  2240. (progn
  2241. (add-text-properties (match-beginning 0) (match-end 0)
  2242. (list 'mouse-face 'highlight
  2243. 'keymap org-mouse-map
  2244. 'help-echo "Radio target link"
  2245. 'org-linked-text t))
  2246. t)))))
  2247. (defun org-update-radio-target-regexp ()
  2248. "Find all radio targets in this file and update the regular expression."
  2249. (interactive)
  2250. (when org-radio-targets
  2251. (setq org-target-link-regexp
  2252. (org-make-target-link-regexp (org-all-targets 'radio)))
  2253. (org-restart-font-lock)))
  2254. (defun org-hide-wide-columns (limit)
  2255. (when org-table-limit-column-width
  2256. (let (s e)
  2257. (setq s (text-property-any (point) (or limit (point-max))
  2258. 'org-cwidth t))
  2259. (when s
  2260. (setq e (next-single-property-change s 'org-cwidth))
  2261. (add-text-properties s e '(invisible org-cwidth intangible t))
  2262. (goto-char e)
  2263. t))))
  2264. (defun org-restart-font-lock ()
  2265. "Restart font-lock-mode, to force refontification."
  2266. (when (and (boundp 'font-lock-mode) font-lock-mode)
  2267. (font-lock-mode -1)
  2268. (font-lock-mode 1)))
  2269. (defun org-all-targets (&optional radio)
  2270. "Return a list of all targets in this file.
  2271. With optional argument RADIO, only find radio targets."
  2272. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  2273. rtn)
  2274. (save-excursion
  2275. (goto-char (point-min))
  2276. (while (re-search-forward re nil t)
  2277. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  2278. rtn)))
  2279. (defun org-make-target-link-regexp (targets)
  2280. "Make regular expression matching all strings in TARGETS.
  2281. The regular expression finds the targets also if there is a line break
  2282. between words."
  2283. (and targets
  2284. (concat
  2285. "\\<\\("
  2286. (mapconcat
  2287. (lambda (x)
  2288. (while (string-match " +" x)
  2289. (setq x (replace-match "\\s-+" t t x)))
  2290. x)
  2291. targets
  2292. "\\|")
  2293. "\\)\\>")))
  2294. (defvar org-camel-regexp "\\*?\\<[A-Z]+[a-z]+[A-Z][a-zA-Z]*\\>"
  2295. "Matches CamelCase words, possibly with a star before it.")
  2296. (defun org-activate-camels (limit)
  2297. "Run through the buffer and add overlays to dates."
  2298. (if org-activate-camels
  2299. (if (re-search-forward org-camel-regexp limit t)
  2300. (progn
  2301. (add-text-properties (match-beginning 0) (match-end 0)
  2302. (list 'mouse-face 'highlight
  2303. 'keymap org-mouse-map))
  2304. t))))
  2305. (defun org-activate-tags (limit)
  2306. (if (re-search-forward "[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \r\n]" limit t)
  2307. (progn
  2308. (add-text-properties (match-beginning 1) (match-end 1)
  2309. (list 'mouse-face 'highlight
  2310. 'keymap org-mouse-map))
  2311. t)))
  2312. (defun org-font-lock-level ()
  2313. (save-excursion
  2314. (org-back-to-heading t)
  2315. (- (match-end 0) (match-beginning 0))))
  2316. (defun org-outline-level ()
  2317. (save-excursion
  2318. (looking-at outline-regexp)
  2319. (if (match-beginning 1)
  2320. (+ (org-get-string-indentation (match-string 1)) 1000)
  2321. (- (match-end 0) (match-beginning 0)))))
  2322. (defvar org-font-lock-keywords nil)
  2323. (defun org-set-font-lock-defaults ()
  2324. (let* ((em org-fontify-emphasized-text)
  2325. (org-font-lock-extra-keywords
  2326. (list
  2327. '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
  2328. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  2329. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  2330. (1 'org-table))
  2331. '(org-activate-links (0 'org-link t))
  2332. '(org-activate-links2 (0 'org-link t))
  2333. '(org-activate-target-links (0 'org-link t))
  2334. '(org-activate-dates (0 'org-link t))
  2335. '(org-activate-camels (0 'org-link t))
  2336. '(org-activate-tags (1 'org-tag prepend))
  2337. '(org-hide-wide-columns (0 nil append))
  2338. (list (concat "^\\*+[ \t]*" org-not-done-regexp)
  2339. '(1 'org-warning t))
  2340. (list (concat "\\[#[A-Z]\\]") '(0 'org-special-keyword t))
  2341. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  2342. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  2343. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  2344. (if em '("\\(\\W\\|^\\)\\(\\*\\w+\\*\\)\\(\\W\\|$\\)" 2 'bold prepend))
  2345. (if em '("\\(\\W\\|^\\)\\(/\\w+/\\)\\(\\W\\|$\\)" 2 'italic prepend))
  2346. (if em '("\\(\\W\\|^\\)\\(_\\w+_\\)\\(\\W\\|$\\)" 2 'underline prepend))
  2347. (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
  2348. "\\|" org-quote-string "\\)\\>")
  2349. '(1 'org-special-keyword t))
  2350. '("^#.*" (0 'font-lock-comment-face t))
  2351. (if org-fontify-done-headline
  2352. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
  2353. '(1 'org-done t) '(2 'org-headline-done t))
  2354. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
  2355. '(1 'org-done t)))
  2356. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  2357. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  2358. '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
  2359. (if org-format-transports-properties-p
  2360. '("| *\\(<[0-9]+>\\) *|" (1 'org-formula t)))
  2361. )))
  2362. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  2363. ;; Now set the full font-lock-keywords
  2364. (set (make-local-variable 'org-font-lock-keywords)
  2365. org-font-lock-extra-keywords)
  2366. (set (make-local-variable 'font-lock-defaults)
  2367. '(org-font-lock-keywords t nil nil backward-paragraph))
  2368. (kill-local-variable 'font-lock-keywords) nil))
  2369. (defvar org-m nil)
  2370. (defvar org-l nil)
  2371. (defvar org-f nil)
  2372. (defun org-get-level-face (n)
  2373. "Get the right face for match N in font-lock matching of healdines."
  2374. (setq org-l (- (match-end 2) (match-beginning 1)))
  2375. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  2376. ; (setq org-f (nth (1- (% org-l org-n-levels)) org-level-faces))
  2377. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  2378. (cond
  2379. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  2380. ((eq n 2) org-f)
  2381. (t (if org-level-color-stars-only nil org-f))))
  2382. (defun org-unfontify-region (beg end &optional maybe_loudly)
  2383. "Remove fontification and activation overlays from links."
  2384. (font-lock-default-unfontify-region beg end)
  2385. (let* ((buffer-undo-list t)
  2386. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  2387. (inhibit-modification-hooks t)
  2388. deactivate-mark buffer-file-name buffer-file-truename)
  2389. (remove-text-properties beg end
  2390. '(mouse-face nil keymap nil org-linked-text nil
  2391. invisible nil intangible nil))))
  2392. ;;; Visibility cycling
  2393. (defvar org-cycle-global-status nil)
  2394. (defvar org-cycle-subtree-status nil)
  2395. (defun org-cycle (&optional arg)
  2396. "Visibility cycling for Org-mode.
  2397. - When this function is called with a prefix argument, rotate the entire
  2398. buffer through 3 states (global cycling)
  2399. 1. OVERVIEW: Show only top-level headlines.
  2400. 2. CONTENTS: Show all headlines of all levels, but no body text.
  2401. 3. SHOW ALL: Show everything.
  2402. - When point is at the beginning of a headline, rotate the subtree started
  2403. by this line through 3 different states (local cycling)
  2404. 1. FOLDED: Only the main headline is shown.
  2405. 2. CHILDREN: The main headline and the direct children are shown.
  2406. From this state, you can move to one of the children
  2407. and zoom in further.
  2408. 3. SUBTREE: Show the entire subtree, including body text.
  2409. - When there is a numeric prefix, go up to a heading with level ARG, do
  2410. a `show-subtree' and return to the previous cursor position. If ARG
  2411. is negative, go up that many levels.
  2412. - When point is not at the beginning of a headline, execute
  2413. `indent-relative', like TAB normally does. See the option
  2414. `org-cycle-emulate-tab' for details.
  2415. - Special case: if point is the the beginning of the buffer and there is
  2416. no headline in line 1, this function will act as if called with prefix arg."
  2417. (interactive "P")
  2418. (if (or (and (bobp) (not (looking-at outline-regexp)))
  2419. (equal arg '(4)))
  2420. ;; special case: use global cycling
  2421. (setq arg t))
  2422. (let ((outline-regexp
  2423. (if org-cycle-include-plain-lists
  2424. "\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) "
  2425. outline-regexp)))
  2426. (cond
  2427. ((org-at-table-p 'any)
  2428. ;; Enter the table or move to the next field in the table
  2429. (or (org-table-recognize-table.el)
  2430. (progn
  2431. (if arg (org-table-edit-field t)
  2432. (org-table-justify-field-maybe)
  2433. (org-table-next-field)))))
  2434. ((eq arg t) ;; Global cycling
  2435. (cond
  2436. ((and (eq last-command this-command)
  2437. (eq org-cycle-global-status 'overview))
  2438. ;; We just created the overview - now do table of contents
  2439. ;; This can be slow in very large buffers, so indicate action
  2440. (message "CONTENTS...")
  2441. (save-excursion
  2442. ;; Visit all headings and show their offspring
  2443. (goto-char (point-max))
  2444. (catch 'exit
  2445. (while (and (progn (condition-case nil
  2446. (outline-previous-visible-heading 1)
  2447. (error (goto-char (point-min))))
  2448. t)
  2449. (looking-at outline-regexp))
  2450. (show-branches)
  2451. (if (bobp) (throw 'exit nil))))
  2452. (message "CONTENTS...done"))
  2453. (setq org-cycle-global-status 'contents)
  2454. (run-hook-with-args 'org-cycle-hook 'contents))
  2455. ((and (eq last-command this-command)
  2456. (eq org-cycle-global-status 'contents))
  2457. ;; We just showed the table of contents - now show everything
  2458. (show-all)
  2459. (message "SHOW ALL")
  2460. (setq org-cycle-global-status 'all)
  2461. (run-hook-with-args 'org-cycle-hook 'all))
  2462. (t
  2463. ;; Default action: go to overview
  2464. (hide-sublevels 1)
  2465. (message "OVERVIEW")
  2466. (setq org-cycle-global-status 'overview)
  2467. (run-hook-with-args 'org-cycle-hook 'overview))))
  2468. ((integerp arg)
  2469. ;; Show-subtree, ARG levels up from here.
  2470. (save-excursion
  2471. (org-back-to-heading)
  2472. (outline-up-heading (if (< arg 0) (- arg)
  2473. (- (funcall outline-level) arg)))
  2474. (org-show-subtree)))
  2475. ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  2476. ;; At a heading: rotate between three different views
  2477. (org-back-to-heading)
  2478. (let ((goal-column 0) eoh eol eos)
  2479. ;; First, some boundaries
  2480. (save-excursion
  2481. (org-back-to-heading)
  2482. (save-excursion
  2483. (beginning-of-line 2)
  2484. (while (and (not (eobp)) ;; this is like `next-line'
  2485. (get-char-property (1- (point)) 'invisible))
  2486. (beginning-of-line 2)) (setq eol (point)))
  2487. (outline-end-of-heading) (setq eoh (point))
  2488. (org-end-of-subtree t) (setq eos (point))
  2489. (outline-next-heading))
  2490. ;; Find out what to do next and set `this-command'
  2491. (cond
  2492. ((= eos eoh)
  2493. ;; Nothing is hidden behind this heading
  2494. (message "EMPTY ENTRY")
  2495. (setq org-cycle-subtree-status nil))
  2496. ((>= eol eos)
  2497. ;; Entire subtree is hidden in one line: open it
  2498. (org-show-entry)
  2499. (show-children)
  2500. (message "CHILDREN")
  2501. (setq org-cycle-subtree-status 'children)
  2502. (run-hook-with-args 'org-cycle-hook 'children))
  2503. ((and (eq last-command this-command)
  2504. (eq org-cycle-subtree-status 'children))
  2505. ;; We just showed the children, now show everything.
  2506. (org-show-subtree)
  2507. (message "SUBTREE")
  2508. (setq org-cycle-subtree-status 'subtree)
  2509. (run-hook-with-args 'org-cycle-hook 'subtree))
  2510. (t
  2511. ;; Default action: hide the subtree.
  2512. (hide-subtree)
  2513. (message "FOLDED")
  2514. (setq org-cycle-subtree-status 'folded)
  2515. (run-hook-with-args 'org-cycle-hook 'folded)))))
  2516. ;; TAB emulation
  2517. (buffer-read-only (org-back-to-heading))
  2518. ((if (and (eq org-cycle-emulate-tab 'white)
  2519. (save-excursion (beginning-of-line 1) (looking-at "[ \t]+$")))
  2520. t
  2521. (eq org-cycle-emulate-tab t))
  2522. (if (and (looking-at "[ \n\r\t]")
  2523. (string-match "^[ \t]*$" (buffer-substring
  2524. (point-at-bol) (point))))
  2525. (progn
  2526. (beginning-of-line 1)
  2527. (and (looking-at "[ \t]+") (replace-match ""))))
  2528. (indent-relative))
  2529. (t (save-excursion
  2530. (org-back-to-heading)
  2531. (org-cycle))))))
  2532. (defun org-optimize-window-after-visibility-change (state)
  2533. "Adjust the window after a change in outline visibility.
  2534. This function is the default value of the hook `org-cycle-hook'."
  2535. (when (get-buffer-window (current-buffer))
  2536. (cond
  2537. ((eq state 'overview) (org-first-headline-recenter 1))
  2538. ((eq state 'content) nil)
  2539. ((eq state 'all) nil)
  2540. ((eq state 'folded) nil)
  2541. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  2542. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  2543. (defun org-subtree-end-visible-p ()
  2544. "Is the end of the current subtree visible?"
  2545. (pos-visible-in-window-p
  2546. (save-excursion (org-end-of-subtree t) (point))))
  2547. (defun org-first-headline-recenter (&optional N)
  2548. "Move cursor to the first headline and recenter the headline.
  2549. Optional argument N means, put the headline into the Nth line of the window."
  2550. (goto-char (point-min))
  2551. (when (re-search-forward (concat "^" outline-regexp) nil t)
  2552. (beginning-of-line)
  2553. (recenter (prefix-numeric-value N))))
  2554. (defvar org-goto-window-configuration nil)
  2555. (defvar org-goto-marker nil)
  2556. (defvar org-goto-map (make-sparse-keymap))
  2557. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  2558. (while (setq cmd (pop cmds))
  2559. (substitute-key-definition cmd cmd org-goto-map global-map)))
  2560. (define-key org-goto-map "\C-m" 'org-goto-ret)
  2561. (define-key org-goto-map [(left)] 'org-goto-left)
  2562. (define-key org-goto-map [(right)] 'org-goto-right)
  2563. (define-key org-goto-map [(?q)] 'org-goto-quit)
  2564. (define-key org-goto-map [(control ?g)] 'org-goto-quit)
  2565. (define-key org-goto-map "\C-i" 'org-cycle)
  2566. (define-key org-goto-map [(tab)] 'org-cycle)
  2567. (define-key org-goto-map [(down)] 'outline-next-visible-heading)
  2568. (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
  2569. (define-key org-goto-map "n" 'outline-next-visible-heading)
  2570. (define-key org-goto-map "p" 'outline-previous-visible-heading)
  2571. (define-key org-goto-map "f" 'outline-forward-same-level)
  2572. (define-key org-goto-map "b" 'outline-backward-same-level)
  2573. (define-key org-goto-map "u" 'outline-up-heading)
  2574. (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  2575. (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  2576. (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  2577. (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  2578. (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
  2579. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  2580. (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
  2581. (defconst org-goto-help
  2582. "Select a location to jump to, press RET
  2583. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  2584. (defun org-goto ()
  2585. "Go to a different location of the document, keeping current visibility.
  2586. When you want to go to a different location in a document, the fastest way
  2587. is often to fold the entire buffer and then dive into the tree. This
  2588. method has the disadvantage, that the previous location will be folded,
  2589. which may not be what you want.
  2590. This command works around this by showing a copy of the current buffer in
  2591. overview mode. You can dive into the tree in that copy, to find the
  2592. location you want to reach. When pressing RET, the command returns to the
  2593. original buffer in which the visibility is still unchanged. It then jumps
  2594. to the new location, making it and the headline hierarchy above it visible."
  2595. (interactive)
  2596. (let* ((org-goto-start-pos (point))
  2597. (selected-point
  2598. (org-get-location (current-buffer) org-goto-help)))
  2599. (if selected-point
  2600. (progn
  2601. (org-mark-ring-push org-goto-start-pos)
  2602. (goto-char selected-point)
  2603. (if (or (org-invisible-p) (org-invisible-p2))
  2604. (org-show-hierarchy-above)))
  2605. (error "Quit"))))
  2606. (defun org-get-location (buf help)
  2607. "Let the user select a location in the Org-mode buffer BUF.
  2608. This function uses a recursive edit. It returns the selected position
  2609. or nil."
  2610. (let (org-selected-point)
  2611. (save-excursion
  2612. (save-window-excursion
  2613. (delete-other-windows)
  2614. (switch-to-buffer (get-buffer-create "*org-goto*"))
  2615. (with-output-to-temp-buffer "*Help*"
  2616. (princ help))
  2617. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  2618. (setq buffer-read-only nil)
  2619. (erase-buffer)
  2620. (insert-buffer-substring buf)
  2621. (let ((org-startup-truncated t)
  2622. (org-startup-folded t)
  2623. (org-startup-align-all-tables nil)
  2624. (org-startup-with-deadline-check nil))
  2625. (org-mode))
  2626. (setq buffer-read-only t)
  2627. (if (boundp 'org-goto-start-pos)
  2628. (goto-char org-goto-start-pos)
  2629. (goto-char (point-min)))
  2630. (org-beginning-of-line)
  2631. (message "Select location and press RET")
  2632. ;; now we make sure that during selection, ony very few keys work
  2633. ;; and that it is impossible to switch to another window.
  2634. (let ((gm (current-global-map))
  2635. (overriding-local-map org-goto-map))
  2636. (unwind-protect
  2637. (progn
  2638. (use-global-map org-goto-map)
  2639. (recursive-edit))
  2640. (use-global-map gm)))))
  2641. (kill-buffer "*org-goto*")
  2642. org-selected-point))
  2643. ;; FIXME: It may not be a good idea to temper with the prefix argument...
  2644. (defun org-goto-ret (&optional arg)
  2645. "Finish `org-goto' by going to the new location."
  2646. (interactive "P")
  2647. (setq org-selected-point (point)
  2648. current-prefix-arg arg)
  2649. (throw 'exit nil))
  2650. (defun org-goto-left ()
  2651. "Finish `org-goto' by going to the new location."
  2652. (interactive)
  2653. (if (org-on-heading-p)
  2654. (progn
  2655. (beginning-of-line 1)
  2656. (setq org-selected-point (point)
  2657. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  2658. (throw 'exit nil))
  2659. (error "Not on a heading")))
  2660. (defun org-goto-right ()
  2661. "Finish `org-goto' by going to the new location."
  2662. (interactive)
  2663. (if (org-on-heading-p)
  2664. (progn
  2665. (outline-end-of-subtree)
  2666. (or (eobp) (forward-char 1))
  2667. (setq org-selected-point (point)
  2668. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  2669. (throw 'exit nil))
  2670. (error "Not on a heading")))
  2671. (defun org-goto-quit ()
  2672. "Finish `org-goto' without cursor motion."
  2673. (interactive)
  2674. (setq org-selected-point nil)
  2675. (throw 'exit nil))
  2676. ;;; Promotion, Demotion, Inserting new headlines
  2677. (defvar org-ignore-region nil
  2678. "To temporarily disable the active region.")
  2679. (defun org-insert-heading (&optional force-heading)
  2680. "Insert a new heading or item with same depth at point."
  2681. (interactive "P")
  2682. (when (or force-heading (not (org-insert-item)))
  2683. (let* ((head (save-excursion
  2684. (condition-case nil
  2685. (org-back-to-heading)
  2686. (error (outline-next-heading)))
  2687. (prog1 (match-string 0)
  2688. (funcall outline-level)))))
  2689. (unless (bolp) (newline))
  2690. (insert head)
  2691. (if (looking-at "[ \t]*")
  2692. (replace-match " "))
  2693. (run-hooks 'org-insert-heading-hook))))
  2694. (defun org-insert-item ()
  2695. "Insert a new item at the current level.
  2696. Return t when things worked, nil when we are not in an item."
  2697. (when (save-excursion
  2698. (condition-case nil
  2699. (progn
  2700. (org-beginning-of-item)
  2701. (org-at-item-p)
  2702. t)
  2703. (error nil)))
  2704. (unless (bolp) (newline))
  2705. (insert (match-string 0))
  2706. (org-maybe-renumber-ordered-list)
  2707. t))
  2708. (defun org-insert-todo-heading (arg)
  2709. "Insert a new heading with the same level and TODO state as current heading.
  2710. If the heading has no TODO state, or if the state is DONE, use the first
  2711. state (TODO by default). Also with prefix arg, force first state."
  2712. (interactive "P")
  2713. (org-insert-heading)
  2714. (save-excursion
  2715. (org-back-to-heading)
  2716. (outline-previous-heading)
  2717. (looking-at org-todo-line-regexp))
  2718. (if (or arg
  2719. (not (match-beginning 2))
  2720. (equal (match-string 2) org-done-string))
  2721. (insert (car org-todo-keywords) " ")
  2722. (insert (match-string 2) " ")))
  2723. (defun org-promote-subtree ()
  2724. "Promote the entire subtree.
  2725. See also `org-promote'."
  2726. (interactive)
  2727. (save-excursion
  2728. (org-map-tree 'org-promote)))
  2729. (defun org-demote-subtree ()
  2730. "Demote the entire subtree. See `org-demote'.
  2731. See also `org-promote'."
  2732. (interactive)
  2733. (save-excursion
  2734. (org-map-tree 'org-demote)))
  2735. (defun org-do-promote ()
  2736. "Promote the current heading higher up the tree.
  2737. If the region is active in `transient-mark-mode', promote all headings
  2738. in the region."
  2739. (interactive)
  2740. (save-excursion
  2741. (if (org-region-active-p)
  2742. (org-map-region 'org-promote (region-beginning) (region-end))
  2743. (org-promote)))
  2744. (org-fix-position-after-promote))
  2745. (defun org-do-demote ()
  2746. "Demote the current heading lower down the tree.
  2747. If the region is active in `transient-mark-mode', demote all headings
  2748. in the region."
  2749. (interactive)
  2750. (save-excursion
  2751. (if (org-region-active-p)
  2752. (org-map-region 'org-demote (region-beginning) (region-end))
  2753. (org-demote)))
  2754. (org-fix-position-after-promote))
  2755. (defun org-fix-position-after-promote ()
  2756. "Make sure that after pro/demotion cursor position is right."
  2757. (and (equal (char-after) ?\ )
  2758. (equal (char-before) ?*)
  2759. (forward-char 1)))
  2760. (defun org-get-legal-level (level change)
  2761. "Rectify a level change under the influence of `org-odd-levels-only'
  2762. LEVEL is a current level, CHANGE is by how much the level should be
  2763. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  2764. even level numbers will become the next higher odd number."
  2765. (if org-odd-levels-only
  2766. (cond ((not change) (1+ (* 2 (/ level 2))))
  2767. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  2768. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  2769. (max 1 (+ level change))))
  2770. (defun org-promote ()
  2771. "Promote the current heading higher up the tree.
  2772. If the region is active in `transient-mark-mode', promote all headings
  2773. in the region."
  2774. (org-back-to-heading t)
  2775. (let* ((level (save-match-data (funcall outline-level)))
  2776. (up-head (make-string (org-get-legal-level level -1) ?*))
  2777. (diff (abs (- level (length up-head)))))
  2778. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover"))
  2779. (replace-match up-head nil t)
  2780. ;; Fixup tag positioning
  2781. (and org-auto-align-tags (org-set-tags nil t))
  2782. (if org-adapt-indentation
  2783. (org-fixup-indentation (if (> diff 1) "^ " "^ ") ""
  2784. (if (> diff 1) "^ ? ?\\S-" "^ ?\\S-")))))
  2785. (defun org-demote ()
  2786. "Demote the current heading lower down the tree.
  2787. If the region is active in `transient-mark-mode', demote all headings
  2788. in the region."
  2789. (org-back-to-heading t)
  2790. (let* ((level (save-match-data (funcall outline-level)))
  2791. (down-head (make-string (org-get-legal-level level 1) ?*))
  2792. (diff (abs (- level (length down-head)))))
  2793. (replace-match down-head nil t)
  2794. ;; Fixup tag positioning
  2795. (and org-auto-align-tags (org-set-tags nil t))
  2796. (if org-adapt-indentation
  2797. (org-fixup-indentation "^ " (if (> diff 1) " " " ") "^\\S-"))))
  2798. (defun org-map-tree (fun)
  2799. "Call FUN for every heading underneath the current one."
  2800. (org-back-to-heading)
  2801. (let ((level (funcall outline-level)))
  2802. (save-excursion
  2803. (funcall fun)
  2804. (while (and (progn
  2805. (outline-next-heading)
  2806. (> (funcall outline-level) level))
  2807. (not (eobp)))
  2808. (funcall fun)))))
  2809. (defun org-map-region (fun beg end)
  2810. "Call FUN for every heading between BEG and END."
  2811. (let ((org-ignore-region t))
  2812. (save-excursion
  2813. (setq end (copy-marker end))
  2814. (goto-char beg)
  2815. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  2816. (< (point) end))
  2817. (funcall fun))
  2818. (while (and (progn
  2819. (outline-next-heading)
  2820. (< (point) end))
  2821. (not (eobp)))
  2822. (funcall fun)))))
  2823. (defun org-fixup-indentation (from to prohibit)
  2824. "Change the indentation in the current entry by re-replacing FROM with TO.
  2825. However, if the regexp PROHIBIT matches at all, don't do anything.
  2826. This is being used to change indentation along with the length of the
  2827. heading marker. But if there are any lines which are not indented, nothing
  2828. is changed at all."
  2829. (save-excursion
  2830. (let ((end (save-excursion (outline-next-heading)
  2831. (point-marker))))
  2832. (unless (save-excursion (re-search-forward prohibit end t))
  2833. (while (re-search-forward from end t)
  2834. (replace-match to)
  2835. (beginning-of-line 2)))
  2836. (move-marker end nil))))
  2837. ;;; Vertical tree motion, cutting and pasting of subtrees
  2838. (defun org-move-subtree-up (&optional arg)
  2839. "Move the current subtree up past ARG headlines of the same level."
  2840. (interactive "p")
  2841. (org-move-subtree-down (- (prefix-numeric-value arg))))
  2842. (defun org-move-subtree-down (&optional arg)
  2843. "Move the current subtree down past ARG headlines of the same level."
  2844. (interactive "p")
  2845. (setq arg (prefix-numeric-value arg))
  2846. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  2847. 'outline-get-last-sibling))
  2848. (ins-point (make-marker))
  2849. (cnt (abs arg))
  2850. beg end txt folded)
  2851. ;; Select the tree
  2852. (org-back-to-heading)
  2853. (setq beg (point))
  2854. (save-match-data
  2855. (save-excursion (outline-end-of-heading)
  2856. (setq folded (org-invisible-p)))
  2857. (outline-end-of-subtree))
  2858. (outline-next-heading)
  2859. (setq end (point))
  2860. ;; Find insertion point, with error handling
  2861. (goto-char beg)
  2862. (while (> cnt 0)
  2863. (or (and (funcall movfunc) (looking-at outline-regexp))
  2864. (progn (goto-char beg)
  2865. (error "Cannot move past superior level or buffer limit")))
  2866. (setq cnt (1- cnt)))
  2867. (if (> arg 0)
  2868. ;; Moving forward - still need to move over subtree
  2869. (progn (outline-end-of-subtree)
  2870. (outline-next-heading)
  2871. (if (not (or (looking-at (concat "^" outline-regexp))
  2872. (bolp)))
  2873. (newline))))
  2874. (move-marker ins-point (point))
  2875. (setq txt (buffer-substring beg end))
  2876. (delete-region beg end)
  2877. (insert txt)
  2878. (goto-char ins-point)
  2879. (if folded (hide-subtree))
  2880. (move-marker ins-point nil)))
  2881. (defvar org-subtree-clip ""
  2882. "Clipboard for cut and paste of subtrees.
  2883. This is actually only a copy of the kill, because we use the normal kill
  2884. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  2885. (defvar org-subtree-clip-folded nil
  2886. "Was the last copied subtree folded?
  2887. This is used to fold the tree back after pasting.")
  2888. (defun org-cut-subtree ()
  2889. "Cut the current subtree into the clipboard.
  2890. This is a short-hand for marking the subtree and then cutting it."
  2891. (interactive)
  2892. (org-copy-subtree 'cut))
  2893. (defun org-copy-subtree (&optional cut)
  2894. "Cut the current subtree into the clipboard.
  2895. This is a short-hand for marking the subtree and then copying it.
  2896. If CUT is non nil, actually cut the subtree."
  2897. (interactive)
  2898. (let (beg end folded)
  2899. (org-back-to-heading)
  2900. (setq beg (point))
  2901. (save-match-data
  2902. (save-excursion (outline-end-of-heading)
  2903. (setq folded (org-invisible-p)))
  2904. (outline-end-of-subtree))
  2905. (if (equal (char-after) ?\n) (forward-char 1))
  2906. (setq end (point))
  2907. (goto-char beg)
  2908. (when (> end beg)
  2909. (setq org-subtree-clip-folded folded)
  2910. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  2911. (setq org-subtree-clip (current-kill 0))
  2912. (message "%s: Subtree with %d characters"
  2913. (if cut "Cut" "Copied")
  2914. (length org-subtree-clip)))))
  2915. (defun org-paste-subtree (&optional level tree)
  2916. "Paste the clipboard as a subtree, with modification of headline level.
  2917. The entire subtree is promoted or demoted in order to match a new headline
  2918. level. By default, the new level is derived from the visible headings
  2919. before and after the insertion point, and taken to be the inferior headline
  2920. level of the two. So if the previous visible heading is level 3 and the
  2921. next is level 4 (or vice versa), level 4 will be used for insertion.
  2922. This makes sure that the subtree remains an independent subtree and does
  2923. not swallow low level entries.
  2924. You can also force a different level, either by using a numeric prefix
  2925. argument, or by inserting the heading marker by hand. For example, if the
  2926. cursor is after \"*****\", then the tree will be shifted to level 5.
  2927. If you want to insert the tree as is, just use \\[yank].
  2928. If optional TREE is given, use this text instead of the kill ring."
  2929. (interactive "P")
  2930. (unless (org-kill-is-subtree-p tree)
  2931. (error
  2932. (substitute-command-keys
  2933. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  2934. (let* ((txt (or tree (current-kill 0)))
  2935. (^re (concat "^\\(" outline-regexp "\\)"))
  2936. (re (concat "\\(" outline-regexp "\\)"))
  2937. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  2938. (old-level (if (string-match ^re txt)
  2939. (- (match-end 0) (match-beginning 0))
  2940. -1))
  2941. (force-level (cond (level (prefix-numeric-value level))
  2942. ((string-match
  2943. ^re_ (buffer-substring (point-at-bol) (point)))
  2944. (- (match-end 0) (match-beginning 0)))
  2945. (t nil)))
  2946. (previous-level (save-excursion
  2947. (condition-case nil
  2948. (progn
  2949. (outline-previous-visible-heading 1)
  2950. (if (looking-at re)
  2951. (- (match-end 0) (match-beginning 0))
  2952. 1))
  2953. (error 1))))
  2954. (next-level (save-excursion
  2955. (condition-case nil
  2956. (progn
  2957. (outline-next-visible-heading 1)
  2958. (if (looking-at re)
  2959. (- (match-end 0) (match-beginning 0))
  2960. 1))
  2961. (error 1))))
  2962. (new-level (or force-level (max previous-level next-level)))
  2963. (shift (if (or (= old-level -1)
  2964. (= new-level -1)
  2965. (= old-level new-level))
  2966. 0
  2967. (- new-level old-level)))
  2968. (shift1 shift)
  2969. (delta (if (> shift 0) -1 1))
  2970. (func (if (> shift 0) 'org-demote 'org-promote))
  2971. (org-odd-levels-only nil)
  2972. beg end)
  2973. ;; Remove the forces level indicator
  2974. (if force-level
  2975. (delete-region (point-at-bol) (point)))
  2976. ;; Make sure we start at the beginning of an empty line
  2977. (if (not (bolp)) (insert "\n"))
  2978. (if (not (looking-at "[ \t]*$"))
  2979. (progn (insert "\n") (backward-char 1)))
  2980. ;; Paste
  2981. (setq beg (point))
  2982. (insert txt)
  2983. (setq end (point))
  2984. (goto-char beg)
  2985. ;; Shift if necessary
  2986. (if (= shift 0)
  2987. (message "Pasted at level %d, without shift" new-level)
  2988. (save-restriction
  2989. (narrow-to-region beg end)
  2990. (while (not (= shift 0))
  2991. (org-map-region func (point-min) (point-max))
  2992. (setq shift (+ delta shift)))
  2993. (goto-char (point-min))
  2994. (message "Pasted at level %d, with shift by %d levels"
  2995. new-level shift1)))
  2996. (if (and (eq org-subtree-clip (current-kill 0))
  2997. org-subtree-clip-folded)
  2998. ;; The tree was folded before it was killed/copied
  2999. (hide-subtree))))
  3000. (defun org-kill-is-subtree-p (&optional txt)
  3001. "Check if the current kill is an outline subtree, or a set of trees.
  3002. Returns nil if kill does not start with a headline, or if the first
  3003. headline level is not the largest headline level in the tree.
  3004. So this will actually accept several entries of equal levels as well,
  3005. which is OK for `org-paste-subtree'.
  3006. If optional TXT is given, check this string instead of the current kill."
  3007. (let* ((kill (or txt (current-kill 0) ""))
  3008. (start-level (and (string-match (concat "\\`" outline-regexp) kill)
  3009. (- (match-end 0) (match-beginning 0))))
  3010. (re (concat "^" outline-regexp))
  3011. (start 1))
  3012. (if (not start-level)
  3013. nil ;; does not even start with a heading
  3014. (catch 'exit
  3015. (while (setq start (string-match re kill (1+ start)))
  3016. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  3017. (throw 'exit nil)))
  3018. t))))
  3019. ;;; Plain list items
  3020. (defun org-at-item-p ()
  3021. "Is point in a line starting a hand-formatted item?"
  3022. (let ((llt org-plain-list-ordered-item-terminator))
  3023. (save-excursion
  3024. (goto-char (point-at-bol))
  3025. (looking-at
  3026. (cond
  3027. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3028. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3029. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3030. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  3031. (defun org-get-indentation ()
  3032. "Get the indentation of the current line, interpreting tabs."
  3033. (save-excursion
  3034. (beginning-of-line 1)
  3035. (skip-chars-forward " \t")
  3036. (current-column)))
  3037. (defun org-beginning-of-item ()
  3038. "Go to the beginning of the current hand-formatted item.
  3039. If the cursor is not in an item, throw an error."
  3040. (let ((pos (point))
  3041. (limit (save-excursion (org-back-to-heading)
  3042. (beginning-of-line 2) (point)))
  3043. ind ind1)
  3044. (if (org-at-item-p)
  3045. (beginning-of-line 1)
  3046. (beginning-of-line 1)
  3047. (skip-chars-forward " \t")
  3048. (setq ind (current-column))
  3049. (if (catch 'exit
  3050. (while t
  3051. (beginning-of-line 0)
  3052. (if (< (point) limit) (throw 'exit nil))
  3053. (unless (looking-at " \t]*$")
  3054. (skip-chars-forward " \t")
  3055. (setq ind1 (current-column))
  3056. (if (< ind1 ind)
  3057. (throw 'exit (org-at-item-p))))))
  3058. nil
  3059. (goto-char pos)
  3060. (error "Not in an item")))))
  3061. (defun org-end-of-item ()
  3062. "Go to the end of the current hand-formatted item.
  3063. If the cursor is not in an item, throw an error."
  3064. (let ((pos (point))
  3065. (limit (save-excursion (outline-next-heading) (point)))
  3066. (ind (save-excursion
  3067. (org-beginning-of-item)
  3068. (skip-chars-forward " \t")
  3069. (current-column)))
  3070. ind1)
  3071. (if (catch 'exit
  3072. (while t
  3073. (beginning-of-line 2)
  3074. (if (>= (point) limit) (throw 'exit t))
  3075. (unless (looking-at "[ \t]*$")
  3076. (skip-chars-forward " \t")
  3077. (setq ind1 (current-column))
  3078. (if (<= ind1 ind) (throw 'exit t)))))
  3079. (beginning-of-line 1)
  3080. (goto-char pos)
  3081. (error "Not in an item"))))
  3082. (defun org-move-item-down (arg)
  3083. "Move the plain list item at point down, i.e. swap with following item.
  3084. Subitems (items with larger indentation) are considered part of the item,
  3085. so this really moves item trees."
  3086. (interactive "p")
  3087. (let (beg end ind ind1 (pos (point)) txt)
  3088. (org-beginning-of-item)
  3089. (setq beg (point))
  3090. (setq ind (org-get-indentation))
  3091. (org-end-of-item)
  3092. (setq end (point))
  3093. (setq ind1 (org-get-indentation))
  3094. (if (and (org-at-item-p) (= ind ind1))
  3095. (progn
  3096. (org-end-of-item)
  3097. (setq txt (buffer-substring beg end))
  3098. (save-excursion
  3099. (delete-region beg end))
  3100. (setq pos (point))
  3101. (insert txt)
  3102. (goto-char pos)
  3103. (org-maybe-renumber-ordered-list))
  3104. (goto-char pos)
  3105. (error "Cannot move this item further down"))))
  3106. (defun org-move-item-up (arg)
  3107. "Move the plain list item at point up, i.e. swap with previous item.
  3108. Subitems (items with larger indentation) are considered part of the item,
  3109. so this really moves item trees."
  3110. (interactive "p")
  3111. (let (beg end ind ind1 (pos (point)) txt)
  3112. (org-beginning-of-item)
  3113. (setq beg (point))
  3114. (setq ind (org-get-indentation))
  3115. (org-end-of-item)
  3116. (setq end (point))
  3117. (goto-char beg)
  3118. (catch 'exit
  3119. (while t
  3120. (beginning-of-line 0)
  3121. (if (looking-at "[ \t]*$")
  3122. nil
  3123. (if (<= (setq ind1 (org-get-indentation)) ind)
  3124. (throw 'exit t)))))
  3125. (condition-case nil
  3126. (org-beginning-of-item)
  3127. (error (goto-char beg)
  3128. (error "Cannot move this item further up")))
  3129. (setq ind1 (org-get-indentation))
  3130. (if (and (org-at-item-p) (= ind ind1))
  3131. (progn
  3132. (setq txt (buffer-substring beg end))
  3133. (save-excursion
  3134. (delete-region beg end))
  3135. (setq pos (point))
  3136. (insert txt)
  3137. (goto-char pos)
  3138. (org-maybe-renumber-ordered-list))
  3139. (goto-char pos)
  3140. (error "Cannot move this item further up"))))
  3141. (defun org-maybe-renumber-ordered-list ()
  3142. "Renumber the ordered list at point if setup allows it.
  3143. This tests the user option `org-auto-renumber-ordered-lists' before
  3144. doing the renumbering."
  3145. (and org-auto-renumber-ordered-lists
  3146. (org-at-item-p)
  3147. (match-beginning 3)
  3148. (org-renumber-ordered-list 1)))
  3149. (defun org-get-string-indentation (s)
  3150. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  3151. (let ((n -1) (i 0) (w tab-width) c)
  3152. (catch 'exit
  3153. (while (< (setq n (1+ n)) (length s))
  3154. (setq c (aref s n))
  3155. (cond ((= c ?\ ) (setq i (1+ i)))
  3156. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  3157. (t (throw 'exit t)))))
  3158. i))
  3159. (defun org-renumber-ordered-list (arg)
  3160. "Renumber an ordered plain list.
  3161. Cursor next to be in the first line of an item, the line that starts
  3162. with something like \"1.\" or \"2)\"."
  3163. (interactive "p")
  3164. (unless (and (org-at-item-p)
  3165. (match-beginning 3))
  3166. (error "This is not an ordered list"))
  3167. (let ((line (org-current-line))
  3168. (col (current-column))
  3169. (ind (org-get-string-indentation
  3170. (buffer-substring (point-at-bol) (match-beginning 3))))
  3171. ;; (term (substring (match-string 3) -1))
  3172. ind1 (n (1- arg)))
  3173. ;; find where this list begins
  3174. (catch 'exit
  3175. (while t
  3176. (catch 'next
  3177. (beginning-of-line 0)
  3178. (if (looking-at "[ \t]*$") (throw 'next t))
  3179. (skip-chars-forward " \t") (setq ind1 (current-column))
  3180. (if (or (< ind1 ind)
  3181. (and (= ind1 ind)
  3182. (not (org-at-item-p))))
  3183. (throw 'exit t)))))
  3184. ;; Walk forward and replace these numbers
  3185. (catch 'exit
  3186. (while t
  3187. (catch 'next
  3188. (beginning-of-line 2)
  3189. (if (eobp) (throw 'exit nil))
  3190. (if (looking-at "[ \t]*$") (throw 'next nil))
  3191. (skip-chars-forward " \t") (setq ind1 (current-column))
  3192. (if (> ind1 ind) (throw 'next t))
  3193. (if (< ind1 ind) (throw 'exit t))
  3194. (if (not (org-at-item-p)) (throw 'exit nil))
  3195. (if (not (match-beginning 3))
  3196. (error "unordered bullet in ordered list. Press \\[undo] to recover"))
  3197. (delete-region (match-beginning 3) (1- (match-end 3)))
  3198. (goto-char (match-beginning 3))
  3199. (insert (format "%d" (setq n (1+ n)))))))
  3200. (goto-line line)
  3201. (move-to-column col)))
  3202. (defvar org-last-indent-begin-marker (make-marker))
  3203. (defvar org-last-indent-end-marker (make-marker))
  3204. (defun org-outdent-item (arg)
  3205. "Outdent a local list item."
  3206. (interactive "p")
  3207. (org-indent-item (- arg)))
  3208. (defun org-indent-item (arg)
  3209. "Indent a local list item."
  3210. (interactive "p")
  3211. (unless (org-at-item-p)
  3212. (error "Not on an item"))
  3213. (let (beg end ind ind1)
  3214. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  3215. (setq beg org-last-indent-begin-marker
  3216. end org-last-indent-end-marker)
  3217. (org-beginning-of-item)
  3218. (setq beg (move-marker org-last-indent-begin-marker (point)))
  3219. (org-end-of-item)
  3220. (setq end (move-marker org-last-indent-end-marker (point))))
  3221. (goto-char beg)
  3222. (skip-chars-forward " \t") (setq ind (current-column))
  3223. (if (< (+ arg ind) 0) (error "Cannot outdent beyond margin"))
  3224. (while (< (point) end)
  3225. (beginning-of-line 1)
  3226. (skip-chars-forward " \t") (setq ind1 (current-column))
  3227. (delete-region (point-at-bol) (point))
  3228. (indent-to-column (+ ind1 arg))
  3229. (beginning-of-line 2))
  3230. (goto-char beg)))
  3231. ;;; Archiving
  3232. (defun org-archive-subtree ()
  3233. "Move the current subtree to the archive.
  3234. The archive can be a certain top-level heading in the current file, or in
  3235. a different file. The tree will be moved to that location, the subtree
  3236. heading be marked DONE, and the current time will be added."
  3237. (interactive)
  3238. ;; Save all relevant TODO keyword-relatex variables
  3239. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  3240. (tr-org-todo-keywords org-todo-keywords)
  3241. (tr-org-todo-interpretation org-todo-interpretation)
  3242. (tr-org-done-string org-done-string)
  3243. (tr-org-todo-regexp org-todo-regexp)
  3244. (tr-org-todo-line-regexp org-todo-line-regexp)
  3245. (this-buffer (current-buffer))
  3246. file heading buffer level newfile-p)
  3247. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  3248. (progn
  3249. (setq file (format (match-string 1 org-archive-location)
  3250. (file-name-nondirectory buffer-file-name))
  3251. heading (match-string 2 org-archive-location)))
  3252. (error "Invalid `org-archive-location'"))
  3253. (if (> (length file) 0)
  3254. (setq newfile-p (not (file-exists-p file))
  3255. buffer (find-file-noselect file))
  3256. (setq buffer (current-buffer)))
  3257. (unless buffer
  3258. (error "Cannot access file \"%s\"" file))
  3259. (if (and (> (length heading) 0)
  3260. (string-match "^\\*+" heading))
  3261. (setq level (match-end 0))
  3262. (setq heading nil level 0))
  3263. (save-excursion
  3264. ;; We first only copy, in case something goes wrong
  3265. ;; we need to protect this-command, to avoid kill-region sets it,
  3266. ;; which would lead to duplication of subtrees
  3267. (let (this-command) (org-copy-subtree))
  3268. (set-buffer buffer)
  3269. ;; Enforce org-mode for the archive buffer
  3270. (if (not (eq major-mode 'org-mode))
  3271. ;; Force the mode for future visits.
  3272. (let ((org-insert-mode-line-in-empty-file t))
  3273. (call-interactively 'org-mode)))
  3274. (when newfile-p
  3275. (goto-char (point-max))
  3276. (insert (format "\nArchived entries from file %s\n\n"
  3277. (buffer-file-name this-buffer))))
  3278. ;; Force the TODO keywords of the original buffer
  3279. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  3280. (org-todo-keywords tr-org-todo-keywords)
  3281. (org-todo-interpretation tr-org-todo-interpretation)
  3282. (org-done-string tr-org-done-string)
  3283. (org-todo-regexp tr-org-todo-regexp)
  3284. (org-todo-line-regexp tr-org-todo-line-regexp))
  3285. (goto-char (point-min))
  3286. (if heading
  3287. (progn
  3288. (if (re-search-forward
  3289. (concat "\\(^\\|\r\\)"
  3290. (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
  3291. nil t)
  3292. (goto-char (match-end 0))
  3293. ;; Heading not found, just insert it at the end
  3294. (goto-char (point-max))
  3295. (or (bolp) (insert "\n"))
  3296. (insert "\n" heading "\n")
  3297. (end-of-line 0))
  3298. ;; Make the heading visible, and the following as well
  3299. (let ((org-show-following-heading t)) (org-show-hierarchy-above))
  3300. (if (re-search-forward
  3301. (concat "^" (regexp-quote (make-string level ?*)) "[ \t]")
  3302. nil t)
  3303. (progn (goto-char (match-beginning 0)) (insert "\n")
  3304. (beginning-of-line 0))
  3305. (goto-char (point-max)) (insert "\n")))
  3306. (goto-char (point-max)) (insert "\n"))
  3307. ;; Paste
  3308. (org-paste-subtree (1+ level))
  3309. ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
  3310. (if org-archive-mark-done
  3311. (org-todo (length org-todo-keywords)))
  3312. ;; Move cursor to right after the TODO keyword
  3313. (when org-archive-stamp-time
  3314. (beginning-of-line 1)
  3315. (looking-at org-todo-line-regexp)
  3316. (goto-char (or (match-end 2) (match-beginning 3)))
  3317. (insert "(" (format-time-string (cdr org-time-stamp-formats)
  3318. (org-current-time))
  3319. ")"))
  3320. ;; Save the buffer, if it is not the same buffer.
  3321. (if (not (eq this-buffer buffer)) (save-buffer))))
  3322. ;; Here we are back in the original buffer. Everything seems to have
  3323. ;; worked. So now cut the tree and finish up.
  3324. (let (this-command) (org-cut-subtree))
  3325. (if (looking-at "[ \t]*$") (kill-line))
  3326. (message "Subtree archived %s"
  3327. (if (eq this-buffer buffer)
  3328. (concat "under heading: " heading)
  3329. (concat "in file: " (abbreviate-file-name file))))))
  3330. ;;; Completion
  3331. (defun org-complete (&optional arg)
  3332. "Perform completion on word at point.
  3333. At the beginning of a headline, this completes TODO keywords as given in
  3334. `org-todo-keywords'.
  3335. If the current word is preceded by a backslash, completes the TeX symbols
  3336. that are supported for HTML support.
  3337. If the current word is preceded by \"#+\", completes special words for
  3338. setting file options.
  3339. At all other locations, this simply calls `ispell-complete-word'."
  3340. (interactive "P")
  3341. (catch 'exit
  3342. (let* ((end (point))
  3343. (beg1 (save-excursion
  3344. (if (equal (char-before (point)) ?\ ) (backward-char 1))
  3345. (skip-chars-backward "a-zA-Z_@0-9")
  3346. (point)))
  3347. (beg (save-excursion
  3348. (if (equal (char-before (point)) ?\ ) (backward-char 1))
  3349. (skip-chars-backward "a-zA-Z0-9_:$")
  3350. (point)))
  3351. (camel (equal (char-before beg) ?*))
  3352. (tag (equal (char-before beg1) ?:))
  3353. (texp (equal (char-before beg) ?\\))
  3354. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  3355. beg)
  3356. "#+"))
  3357. (completion-ignore-case opt)
  3358. (type nil)
  3359. (tbl nil)
  3360. (table (cond
  3361. (opt
  3362. (setq type :opt)
  3363. (mapcar (lambda (x)
  3364. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  3365. (cons (match-string 2 x) (match-string 1 x)))
  3366. (org-split-string (org-get-current-options) "\n")))
  3367. (texp
  3368. (setq type :tex)
  3369. org-html-entities)
  3370. ((string-match "\\`\\*+[ \t]*\\'"
  3371. (buffer-substring (point-at-bol) beg))
  3372. (setq type :todo)
  3373. (mapcar 'list org-todo-keywords))
  3374. (camel
  3375. (setq type :camel)
  3376. (save-excursion
  3377. (goto-char (point-min))
  3378. (while (re-search-forward org-todo-line-regexp nil t)
  3379. (push (list
  3380. (if org-file-link-context-use-camel-case
  3381. (org-make-org-heading-camel (match-string 3) t)
  3382. (org-make-org-heading-search-string
  3383. (match-string 3) t)))
  3384. tbl)))
  3385. tbl)
  3386. (tag (setq type :tag beg beg1)
  3387. (org-get-buffer-tags))
  3388. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  3389. (pattern (buffer-substring-no-properties beg end))
  3390. (completion (try-completion pattern table)))
  3391. (cond ((eq completion t)
  3392. (if (equal type :opt)
  3393. (insert (substring (cdr (assoc (upcase pattern) table))
  3394. (length pattern)))))
  3395. ((null completion)
  3396. (message "Can't find completion for \"%s\"" pattern)
  3397. (ding))
  3398. ((not (string= pattern completion))
  3399. (delete-region beg end)
  3400. (if (string-match " +$" completion)
  3401. (setq completion (replace-match "" t t completion)))
  3402. (insert completion)
  3403. (if (get-buffer-window "*Completions*")
  3404. (delete-window (get-buffer-window "*Completions*")))
  3405. (if (assoc completion table)
  3406. (if (eq type :todo) (insert " ")
  3407. (if (eq type :tag) (insert ":"))))
  3408. (if (and (equal type :opt) (assoc completion table))
  3409. (message "%s" (substitute-command-keys
  3410. "Press \\[org-complete] again to insert example settings"))))
  3411. (t
  3412. (message "Making completion list...")
  3413. (let ((list (sort (all-completions pattern table) 'string<)))
  3414. (with-output-to-temp-buffer "*Completions*"
  3415. (condition-case nil
  3416. ;; Protection needed for XEmacs and emacs 21
  3417. (display-completion-list list pattern)
  3418. (error (display-completion-list list)))))
  3419. (message "Making completion list...%s" "done"))))))
  3420. ;;; Comments, TODO and DEADLINE
  3421. (defun org-toggle-comment ()
  3422. "Change the COMMENT state of an entry."
  3423. (interactive)
  3424. (save-excursion
  3425. (org-back-to-heading)
  3426. (if (looking-at (concat outline-regexp
  3427. "\\( +\\<" org-comment-string "\\>\\)"))
  3428. (replace-match "" t t nil 1)
  3429. (if (looking-at outline-regexp)
  3430. (progn
  3431. (goto-char (match-end 0))
  3432. (insert " " org-comment-string))))))
  3433. (defvar org-last-todo-state-is-todo nil
  3434. "This is non-nil when the last TODO state change led to a TODO state.
  3435. If the last change removed the TODO tag or switched to DONE, then
  3436. this is nil.")
  3437. (defun org-todo (&optional arg)
  3438. "Change the TODO state of an item.
  3439. The state of an item is given by a keyword at the start of the heading,
  3440. like
  3441. *** TODO Write paper
  3442. *** DONE Call mom
  3443. The different keywords are specified in the variable `org-todo-keywords'.
  3444. By default the available states are \"TODO\" and \"DONE\".
  3445. So for this example: when the item starts with TODO, it is changed to DONE.
  3446. When it starts with DONE, the DONE is removed. And when neither TODO nor
  3447. DONE are present, add TODO at the beginning of the heading.
  3448. With prefix arg, use completion to determine the new state. With numeric
  3449. prefix arg, switch to that state."
  3450. (interactive "P")
  3451. (save-excursion
  3452. (org-back-to-heading)
  3453. (if (looking-at outline-regexp) (goto-char (match-end 0)))
  3454. (or (looking-at (concat " +" org-todo-regexp " *"))
  3455. (looking-at " *"))
  3456. (let* ((this (match-string 1))
  3457. (completion-ignore-case t)
  3458. (member (member this org-todo-keywords))
  3459. (tail (cdr member))
  3460. (state (cond
  3461. ((equal arg '(4))
  3462. ;; Read a state with completion
  3463. (completing-read "State: " (mapcar (lambda(x) (list x))
  3464. org-todo-keywords)
  3465. nil t))
  3466. ((eq arg 'right)
  3467. (if this
  3468. (if tail (car tail) nil)
  3469. (car org-todo-keywords)))
  3470. ((eq arg 'left)
  3471. (if (equal member org-todo-keywords)
  3472. nil
  3473. (if this
  3474. (nth (- (length org-todo-keywords) (length tail) 2)
  3475. org-todo-keywords)
  3476. org-done-string)))
  3477. (arg
  3478. ;; user requests a specific state
  3479. (nth (1- (prefix-numeric-value arg))
  3480. org-todo-keywords))
  3481. ((null member) (car org-todo-keywords))
  3482. ((null tail) nil) ;; -> first entry
  3483. ((eq org-todo-interpretation 'sequence)
  3484. (car tail))
  3485. ((memq org-todo-interpretation '(type priority))
  3486. (if (eq this-command last-command)
  3487. (car tail)
  3488. (if (> (length tail) 0) org-done-string nil)))
  3489. (t nil)))
  3490. (next (if state (concat " " state " ") " ")))
  3491. (replace-match next t t)
  3492. (setq org-last-todo-state-is-todo
  3493. (not (equal state org-done-string)))
  3494. (when org-log-done
  3495. (if (equal state org-done-string)
  3496. (org-log-done)
  3497. (if (not this)
  3498. (org-log-done t))))
  3499. ;; Fixup tag positioning
  3500. (and org-auto-align-tags (org-set-tags nil t))
  3501. (run-hooks 'org-after-todo-state-change-hook)))
  3502. ;; Fixup cursor location if close to the keyword
  3503. (if (and (outline-on-heading-p)
  3504. (not (bolp))
  3505. (save-excursion (beginning-of-line 1)
  3506. (looking-at org-todo-line-regexp))
  3507. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  3508. (progn
  3509. (goto-char (or (match-end 2) (match-end 1)))
  3510. (just-one-space))))
  3511. (defun org-log-done (&optional undone)
  3512. "Add a time stamp logging that a TODO entry has been closed.
  3513. When UNDONE is non-nil, remove such a time stamp again."
  3514. (interactive)
  3515. (let (beg end col)
  3516. (save-excursion
  3517. (org-back-to-heading t)
  3518. (setq beg (point))
  3519. (looking-at (concat outline-regexp " *"))
  3520. (goto-char (match-end 0))
  3521. (setq col (current-column))
  3522. (outline-next-heading)
  3523. (setq end (point))
  3524. (goto-char beg)
  3525. (when (re-search-forward (concat
  3526. "[\r\n]\\([ \t]*"
  3527. (regexp-quote org-closed-string)
  3528. " *\\[.*?\\][^\n\r]*[\n\r]?\\)") end t)
  3529. (delete-region (match-beginning 1) (match-end 1)))
  3530. (unless undone
  3531. (org-back-to-heading t)
  3532. (skip-chars-forward "^\n\r")
  3533. (goto-char (min (1+ (point)) (point-max)))
  3534. (when (not (member (char-before) '(?\r ?\n)))
  3535. (insert "\n"))
  3536. (indent-to col)
  3537. (insert org-closed-string " "
  3538. (format-time-string
  3539. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  3540. (org-current-time))
  3541. "\n")))))
  3542. (defun org-show-todo-tree (arg)
  3543. "Make a compact tree which shows all headlines marked with TODO.
  3544. The tree will show the lines where the regexp matches, and all higher
  3545. headlines above the match.
  3546. With \\[universal-argument] prefix, also show the DONE entries.
  3547. With a numeric prefix N, construct a sparse tree for the Nth element
  3548. of `org-todo-keywords'."
  3549. (interactive "P")
  3550. (let ((case-fold-search nil)
  3551. (kwd-re
  3552. (cond ((null arg) org-not-done-regexp)
  3553. ((equal arg '(4)) org-todo-regexp)
  3554. ((<= (prefix-numeric-value arg) (length org-todo-keywords))
  3555. (regexp-quote (nth (1- (prefix-numeric-value arg))
  3556. org-todo-keywords)))
  3557. (t (error "Invalid prefix argument: %s" arg)))))
  3558. (message "%d TODO entries found"
  3559. (org-occur (concat "^" outline-regexp " +" kwd-re )))))
  3560. (defun org-deadline ()
  3561. "Insert the DEADLINE: string to make a deadline.
  3562. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  3563. to modify it to the correct date."
  3564. (interactive)
  3565. (insert
  3566. org-deadline-string " "
  3567. (format-time-string (car org-time-stamp-formats)
  3568. (org-read-date nil 'to-time)))
  3569. (message "%s" (substitute-command-keys
  3570. "Use \\[org-timestamp-up-day] and \\[org-timestamp-down-day] to change the date.")))
  3571. (defun org-schedule ()
  3572. "Insert the SCHEDULED: string to schedule a TODO item.
  3573. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  3574. to modify it to the correct date."
  3575. (interactive)
  3576. (insert
  3577. org-scheduled-string " "
  3578. (format-time-string (car org-time-stamp-formats)
  3579. (org-read-date nil 'to-time)))
  3580. (message "%s" (substitute-command-keys
  3581. "Use \\[org-timestamp-up-day] and \\[org-timestamp-down-day] to change the date.")))
  3582. (defun org-occur (regexp &optional callback)
  3583. "Make a compact tree which shows all matches of REGEXP.
  3584. The tree will show the lines where the regexp matches, and all higher
  3585. headlines above the match. It will also show the heading after the match,
  3586. to make sure editing the matching entry is easy.
  3587. If CALLBACK is non-nil, it is a function which is called to confirm
  3588. that the match should indeed be shown."
  3589. (interactive "sRegexp: ")
  3590. (org-remove-occur-highlights nil nil t)
  3591. (setq regexp (org-check-occur-regexp regexp))
  3592. (let ((cnt 0))
  3593. (save-excursion
  3594. (goto-char (point-min))
  3595. (hide-sublevels 1)
  3596. (while (re-search-forward regexp nil t)
  3597. (when (or (not callback)
  3598. (save-match-data (funcall callback)))
  3599. (setq cnt (1+ cnt))
  3600. (org-highlight-new-match (match-beginning 0) (match-end 0))
  3601. (org-show-hierarchy-above))))
  3602. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  3603. nil 'local)
  3604. (run-hooks 'org-occur-hook)
  3605. (if (interactive-p)
  3606. (message "%d match(es) for regexp %s" cnt regexp))
  3607. cnt))
  3608. (defun org-show-hierarchy-above ()
  3609. "Make sure point and the headings hierarchy above is visible."
  3610. (catch 'exit
  3611. (if (org-on-heading-p t)
  3612. (org-flag-heading nil) ; only show the heading
  3613. (and (or (org-invisible-p) (org-invisible-p2))
  3614. (org-show-hidden-entry))) ; show entire entry
  3615. (save-excursion
  3616. (and org-show-following-heading
  3617. (outline-next-heading)
  3618. (org-flag-heading nil))) ; show the next heading
  3619. (when org-show-hierarchy-above
  3620. (save-excursion ; show all higher headings
  3621. (while (and (condition-case nil
  3622. (progn (org-up-heading-all 1) t)
  3623. (error nil))
  3624. (not (bobp)))
  3625. (org-flag-heading nil))))))
  3626. ;; Overlay compatibility functions
  3627. (defun org-make-overlay (beg end &optional buffer)
  3628. (if (featurep 'xemacs)
  3629. (make-extent beg end buffer)
  3630. (make-overlay beg end buffer)))
  3631. (defun org-delete-overlay (ovl)
  3632. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  3633. (defun org-detatch-overlay (ovl)
  3634. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  3635. (defun org-move-overlay (ovl beg end &optional buffer)
  3636. (if (featurep 'xemacs)
  3637. (set-extent-endpoints ovl beg end buffer)
  3638. (move-overlay ovl beg end buffer)))
  3639. (defun org-overlay-put (ovl prop value)
  3640. (if (featurep 'xemacs)
  3641. (set-extent-property ovl prop value)
  3642. (overlay-put ovl prop value)))
  3643. (defvar org-occur-highlights nil)
  3644. (defun org-highlight-new-match (beg end)
  3645. "Highlight from BEG to END and mark the highlight is an occur headline."
  3646. (let ((ov (org-make-overlay beg end)))
  3647. (org-overlay-put ov 'face 'secondary-selection)
  3648. (push ov org-occur-highlights)))
  3649. (defun org-remove-occur-highlights (&optional beg end noremove)
  3650. "Remove the occur highlights from the buffer.
  3651. BEG and END are ignored. If NOREMOVE is nil, remove this function
  3652. from the `before-change-functions' in the current buffer."
  3653. (interactive)
  3654. (mapc 'org-delete-overlay org-occur-highlights)
  3655. (setq org-occur-highlights nil)
  3656. (unless noremove
  3657. (remove-hook 'before-change-functions
  3658. 'org-remove-occur-highlights 'local)))
  3659. ;;; Priorities
  3660. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
  3661. "Regular expression matching the priority indicator.")
  3662. (defvar org-remove-priority-next-time nil)
  3663. (defun org-priority-up ()
  3664. "Increase the priority of the current item."
  3665. (interactive)
  3666. (org-priority 'up))
  3667. (defun org-priority-down ()
  3668. "Decrease the priority of the current item."
  3669. (interactive)
  3670. (org-priority 'down))
  3671. (defun org-priority (&optional action)
  3672. "Change the priority of an item by ARG.
  3673. ACTION can be set, up, or down."
  3674. (interactive)
  3675. (setq action (or action 'set))
  3676. (let (current new news have remove)
  3677. (save-excursion
  3678. (org-back-to-heading)
  3679. (if (looking-at org-priority-regexp)
  3680. (setq current (string-to-char (match-string 2))
  3681. have t)
  3682. (setq current org-default-priority))
  3683. (cond
  3684. ((eq action 'set)
  3685. (message "Priority A-%c, SPC to remove: " org-lowest-priority)
  3686. (setq new (read-char-exclusive))
  3687. (cond ((equal new ?\ ) (setq remove t))
  3688. ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
  3689. (error "Priority must be between `%c' and `%c'"
  3690. ?A org-lowest-priority))))
  3691. ((eq action 'up)
  3692. (setq new (1- current)))
  3693. ((eq action 'down)
  3694. (setq new (1+ current)))
  3695. (t (error "Invalid action")))
  3696. (setq new (min (max ?A (upcase new)) org-lowest-priority))
  3697. (setq news (format "%c" new))
  3698. (if have
  3699. (if remove
  3700. (replace-match "" t t nil 1)
  3701. (replace-match news t t nil 2))
  3702. (if remove
  3703. (error "No priority cookie found in line")
  3704. (looking-at org-todo-line-regexp)
  3705. (if (match-end 2)
  3706. (progn
  3707. (goto-char (match-end 2))
  3708. (insert " [#" news "]"))
  3709. (goto-char (match-beginning 3))
  3710. (insert "[#" news "] ")))))
  3711. (if remove
  3712. (message "Priority removed")
  3713. (message "Priority of current item set to %s" news))))
  3714. (defun org-get-priority (s)
  3715. "Find priority cookie and return priority."
  3716. (save-match-data
  3717. (if (not (string-match org-priority-regexp s))
  3718. (* 1000 (- org-lowest-priority org-default-priority))
  3719. (* 1000 (- org-lowest-priority
  3720. (string-to-char (match-string 2 s)))))))
  3721. ;;; Timestamps
  3722. (defvar org-last-changed-timestamp nil)
  3723. (defun org-time-stamp (arg)
  3724. "Prompt for a date/time and insert a time stamp.
  3725. If the user specifies a time like HH:MM, or if this command is called
  3726. with a prefix argument, the time stamp will contain date and time.
  3727. Otherwise, only the date will be included. All parts of a date not
  3728. specified by the user will be filled in from the current date/time.
  3729. So if you press just return without typing anything, the time stamp
  3730. will represent the current date/time. If there is already a timestamp
  3731. at the cursor, it will be modified."
  3732. (interactive "P")
  3733. (let ((fmt (if arg (cdr org-time-stamp-formats)
  3734. (car org-time-stamp-formats)))
  3735. (org-time-was-given nil)
  3736. time)
  3737. (cond
  3738. ((and (org-at-timestamp-p)
  3739. (eq last-command 'org-time-stamp)
  3740. (eq this-command 'org-time-stamp))
  3741. (insert "--")
  3742. (setq time (let ((this-command this-command))
  3743. (org-read-date arg 'totime)))
  3744. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  3745. (insert (format-time-string fmt time)))
  3746. ((org-at-timestamp-p)
  3747. (setq time (let ((this-command this-command))
  3748. (org-read-date arg 'totime)))
  3749. (and (org-at-timestamp-p) (replace-match
  3750. (setq org-last-changed-timestamp
  3751. (format-time-string fmt time))
  3752. t t))
  3753. (message "Timestamp updated"))
  3754. (t
  3755. (setq time (let ((this-command this-command))
  3756. (org-read-date arg 'totime)))
  3757. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  3758. (insert (format-time-string fmt time))))))
  3759. (defun org-time-stamp-inactive (&optional arg)
  3760. "Insert an inactive time stamp.
  3761. An inactive time stamp is enclosed in square brackets instead of angle
  3762. brackets. It is inactive in the sense that it does not trigger agenda entries,
  3763. does not link to the calendar and cannot be changed with the S-cursor keys.
  3764. So these are more for recording a certain time/date."
  3765. (interactive "P")
  3766. (let ((fmt (if arg (cdr org-time-stamp-formats)
  3767. (car org-time-stamp-formats)))
  3768. (org-time-was-given nil)
  3769. time)
  3770. (setq time (org-read-date arg 'totime))
  3771. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  3772. (setq fmt (concat "[" (substring fmt 1 -1) "]"))
  3773. (insert (format-time-string fmt time))))
  3774. (defvar org-date-ovl (org-make-overlay 1 1))
  3775. (org-overlay-put org-date-ovl 'face 'org-warning)
  3776. (org-detatch-overlay org-date-ovl)
  3777. (defun org-read-date (&optional with-time to-time)
  3778. "Read a date and make things smooth for the user.
  3779. The prompt will suggest to enter an ISO date, but you can also enter anything
  3780. which will at least partially be understood by `parse-time-string'.
  3781. Unrecognized parts of the date will default to the current day, month, year,
  3782. hour and minute. For example,
  3783. 3-2-5 --> 2003-02-05
  3784. feb 15 --> currentyear-02-15
  3785. sep 12 9 --> 2009-09-12
  3786. 12:45 --> today 12:45
  3787. 22 sept 0:34 --> currentyear-09-22 0:34
  3788. 12 --> currentyear-currentmonth-12
  3789. Fri --> nearest Friday (today or later)
  3790. etc.
  3791. The function understands only English month and weekday abbreviations,
  3792. but this can be configured with the variables `parse-time-months' and
  3793. `parse-time-weekdays'.
  3794. While prompting, a calendar is popped up - you can also select the
  3795. date with the mouse (button 1). The calendar shows a period of three
  3796. months. To scroll it to other months, use the keys `>' and `<'.
  3797. If you don't like the calendar, turn it off with
  3798. \(setq org-popup-calendar-for-date-prompt nil)
  3799. With optional argument TO-TIME, the date will immediately be converted
  3800. to an internal time.
  3801. With an optional argument WITH-TIME, the prompt will suggest to also
  3802. insert a time. Note that when WITH-TIME is not set, you can still
  3803. enter a time, and this function will inform the calling routine about
  3804. this change. The calling routine may then choose to change the format
  3805. used to insert the time stamp into the buffer to include the time."
  3806. (require 'parse-time)
  3807. (let* ((org-time-stamp-rounding-minutes
  3808. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  3809. (ct (org-current-time))
  3810. (default-time
  3811. ;; Default time is either today, or, when entering a range,
  3812. ;; the range start.
  3813. (if (save-excursion
  3814. (re-search-backward
  3815. (concat org-ts-regexp "--\\=")
  3816. (- (point) 20) t))
  3817. (apply
  3818. 'encode-time
  3819. (mapcar (lambda(x) (or x 0))
  3820. (parse-time-string (match-string 1))))
  3821. ct))
  3822. (calendar-move-hook nil)
  3823. (view-diary-entries-initially nil)
  3824. (timestr (format-time-string
  3825. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  3826. (prompt (format "YYYY-MM-DD [%s]: " timestr))
  3827. ans ans1 ans2
  3828. second minute hour day month year tl wday wday1)
  3829. (if org-popup-calendar-for-date-prompt
  3830. (save-excursion
  3831. (save-window-excursion
  3832. (calendar)
  3833. (calendar-forward-day (- (time-to-days default-time)
  3834. (calendar-absolute-from-gregorian
  3835. (calendar-current-date))))
  3836. (org-eval-in-calendar nil)
  3837. (let* ((old-map (current-local-map))
  3838. (map (copy-keymap calendar-mode-map))
  3839. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  3840. (define-key map (kbd "RET") 'org-calendar-select)
  3841. (define-key map (if (featurep 'xemacs) [button1] [mouse-1])
  3842. 'org-calendar-select-mouse)
  3843. (define-key map (if (featurep 'xemacs) [button2] [mouse-2])
  3844. 'org-calendar-select-mouse)
  3845. (define-key minibuffer-local-map [(meta shift left)]
  3846. (lambda () (interactive)
  3847. (org-eval-in-calendar '(calendar-backward-month 1))))
  3848. (define-key minibuffer-local-map [(meta shift right)]
  3849. (lambda () (interactive)
  3850. (org-eval-in-calendar '(calendar-forward-month 1))))
  3851. (define-key minibuffer-local-map [(shift up)]
  3852. (lambda () (interactive)
  3853. (org-eval-in-calendar '(calendar-backward-week 1))))
  3854. (define-key minibuffer-local-map [(shift down)]
  3855. (lambda () (interactive)
  3856. (org-eval-in-calendar '(calendar-forward-week 1))))
  3857. (define-key minibuffer-local-map [(shift left)]
  3858. (lambda () (interactive)
  3859. (org-eval-in-calendar '(calendar-backward-day 1))))
  3860. (define-key minibuffer-local-map [(shift right)]
  3861. (lambda () (interactive)
  3862. (org-eval-in-calendar '(calendar-forward-day 1))))
  3863. (define-key minibuffer-local-map ">"
  3864. (lambda () (interactive)
  3865. (org-eval-in-calendar '(scroll-calendar-left 1))))
  3866. (define-key minibuffer-local-map "<"
  3867. (lambda () (interactive)
  3868. (org-eval-in-calendar '(scroll-calendar-right 1))))
  3869. (unwind-protect
  3870. (progn
  3871. (use-local-map map)
  3872. (setq ans (read-string prompt "" nil nil))
  3873. (if (not (string-match "\\S-" ans)) (setq ans nil))
  3874. (setq ans (or ans1 ans ans2)))
  3875. (use-local-map old-map)))))
  3876. ;; Naked prompt only
  3877. (setq ans (read-string prompt "" nil timestr)))
  3878. (org-detatch-overlay org-date-ovl)
  3879. (if (string-match
  3880. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  3881. (progn
  3882. (setq year (if (match-end 2)
  3883. (string-to-number (match-string 2 ans))
  3884. (string-to-number (format-time-string "%Y")))
  3885. month (string-to-number (match-string 3 ans))
  3886. day (string-to-number (match-string 4 ans)))
  3887. (if (< year 100) (setq year (+ 2000 year)))
  3888. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  3889. t nil ans))))
  3890. (setq tl (parse-time-string ans)
  3891. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  3892. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  3893. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  3894. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  3895. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  3896. second (or (nth 0 tl) 0)
  3897. wday (nth 6 tl))
  3898. (when (and wday (not (nth 3 tl)))
  3899. ;; Weekday was given, but no day, so pick that day in the week
  3900. ;; on or after the derived date.
  3901. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  3902. (unless (equal wday wday1)
  3903. (setq day (+ day (% (- wday wday1 -7) 7)))))
  3904. (if (and (boundp 'org-time-was-given)
  3905. (nth 2 tl))
  3906. (setq org-time-was-given t))
  3907. (if (< year 100) (setq year (+ 2000 year)))
  3908. (if to-time
  3909. (encode-time second minute hour day month year)
  3910. (if (or (nth 1 tl) (nth 2 tl))
  3911. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  3912. (format "%04d-%02d-%02d" year month day)))))
  3913. (defun org-eval-in-calendar (form)
  3914. "Eval FORM in the calendar window and return to current window.
  3915. Also, store the cursor date in variable ans2."
  3916. (let ((sw (selected-window)))
  3917. (select-window (get-buffer-window "*Calendar*"))
  3918. (eval form)
  3919. (when (calendar-cursor-to-date)
  3920. (let* ((date (calendar-cursor-to-date))
  3921. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  3922. (setq ans2 (format-time-string "%Y-%m-%d" time))))
  3923. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  3924. (select-window sw)))
  3925. (defun org-calendar-select ()
  3926. "Return to `org-read-date' with the date currently selected.
  3927. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  3928. (interactive)
  3929. (when (calendar-cursor-to-date)
  3930. (let* ((date (calendar-cursor-to-date))
  3931. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  3932. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  3933. (if (active-minibuffer-window) (exit-minibuffer))))
  3934. (defun org-calendar-select-mouse (ev)
  3935. "Return to `org-read-date' with the date currently selected.
  3936. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  3937. (interactive "e")
  3938. (mouse-set-point ev)
  3939. (when (calendar-cursor-to-date)
  3940. (let* ((date (calendar-cursor-to-date))
  3941. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  3942. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  3943. (if (active-minibuffer-window) (exit-minibuffer))))
  3944. (defun org-check-deadlines (ndays)
  3945. "Check if there are any deadlines due or past due.
  3946. A deadline is considered due if it happens within `org-deadline-warning-days'
  3947. days from today's date. If the deadline appears in an entry marked DONE,
  3948. it is not shown. The prefix arg NDAYS can be used to test that many
  3949. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  3950. (interactive "P")
  3951. (let* ((org-warn-days
  3952. (cond
  3953. ((equal ndays '(4)) 100000)
  3954. (ndays (prefix-numeric-value ndays))
  3955. (t org-deadline-warning-days)))
  3956. (case-fold-search nil)
  3957. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  3958. (callback
  3959. (lambda ()
  3960. (and (let ((d1 (time-to-days (current-time)))
  3961. (d2 (time-to-days
  3962. (org-time-string-to-time (match-string 1)))))
  3963. (< (- d2 d1) org-warn-days))
  3964. (not (org-entry-is-done-p))))))
  3965. (message "%d deadlines past-due or due within %d days"
  3966. (org-occur regexp callback)
  3967. org-warn-days)))
  3968. (defun org-evaluate-time-range (&optional to-buffer)
  3969. "Evaluate a time range by computing the difference between start and end.
  3970. Normally the result is just printed in the echo area, but with prefix arg
  3971. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  3972. If the time range is actually in a table, the result is inserted into the
  3973. next column.
  3974. For time difference computation, a year is assumed to be exactly 365
  3975. days in order to avoid rounding problems."
  3976. (interactive "P")
  3977. (save-excursion
  3978. (unless (org-at-date-range-p)
  3979. (goto-char (point-at-bol))
  3980. (re-search-forward org-tr-regexp (point-at-eol) t))
  3981. (if (not (org-at-date-range-p))
  3982. (error "Not at a time-stamp range, and none found in current line")))
  3983. (let* ((ts1 (match-string 1))
  3984. (ts2 (match-string 2))
  3985. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  3986. (match-end (match-end 0))
  3987. (time1 (org-time-string-to-time ts1))
  3988. (time2 (org-time-string-to-time ts2))
  3989. (t1 (time-to-seconds time1))
  3990. (t2 (time-to-seconds time2))
  3991. (diff (abs (- t2 t1)))
  3992. (negative (< (- t2 t1) 0))
  3993. ;; (ys (floor (* 365 24 60 60)))
  3994. (ds (* 24 60 60))
  3995. (hs (* 60 60))
  3996. (fy "%dy %dd %02d:%02d")
  3997. (fy1 "%dy %dd")
  3998. (fd "%dd %02d:%02d")
  3999. (fd1 "%dd")
  4000. (fh "%02d:%02d")
  4001. y d h m align)
  4002. (if havetime
  4003. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  4004. y 0
  4005. d (floor (/ diff ds)) diff (mod diff ds)
  4006. h (floor (/ diff hs)) diff (mod diff hs)
  4007. m (floor (/ diff 60)))
  4008. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  4009. y 0
  4010. d (floor (+ (/ diff ds) 0.5))
  4011. h 0 m 0))
  4012. (if (not to-buffer)
  4013. (message (org-make-tdiff-string y d h m))
  4014. (when (org-at-table-p)
  4015. (goto-char match-end)
  4016. (setq align t)
  4017. (and (looking-at " *|") (goto-char (match-end 0))))
  4018. (if (looking-at
  4019. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  4020. (replace-match ""))
  4021. (if negative (insert " -"))
  4022. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  4023. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  4024. (insert " " (format fh h m))))
  4025. (if align (org-table-align))
  4026. (message "Time difference inserted"))))
  4027. (defun org-make-tdiff-string (y d h m)
  4028. (let ((fmt "")
  4029. (l nil))
  4030. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  4031. l (push y l)))
  4032. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  4033. l (push d l)))
  4034. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  4035. l (push h l)))
  4036. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  4037. l (push m l)))
  4038. (apply 'format fmt (nreverse l))))
  4039. (defun org-time-string-to-time (s)
  4040. (apply 'encode-time (org-parse-time-string s)))
  4041. (defun org-parse-time-string (s &optional nodefault)
  4042. "Parse the standard Org-mode time string.
  4043. This should be a lot faster than the normal `parse-time-string'.
  4044. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  4045. hour and minute fields will be nil if not given."
  4046. (if (string-match org-ts-regexp1 s)
  4047. (list 0
  4048. (if (or (match-beginning 8) (not nodefault))
  4049. (string-to-number (or (match-string 8 s) "0")))
  4050. (if (or (match-beginning 7) (not nodefault))
  4051. (string-to-number (or (match-string 7 s) "0")))
  4052. (string-to-number (match-string 4 s))
  4053. (string-to-number (match-string 3 s))
  4054. (string-to-number (match-string 2 s))
  4055. nil nil nil)
  4056. (make-list 9 0)))
  4057. (defun org-timestamp-up (&optional arg)
  4058. "Increase the date item at the cursor by one.
  4059. If the cursor is on the year, change the year. If it is on the month or
  4060. the day, change that.
  4061. With prefix ARG, change by that many units."
  4062. (interactive "p")
  4063. (org-timestamp-change (prefix-numeric-value arg)))
  4064. (defun org-timestamp-down (&optional arg)
  4065. "Decrease the date item at the cursor by one.
  4066. If the cursor is on the year, change the year. If it is on the month or
  4067. the day, change that.
  4068. With prefix ARG, change by that many units."
  4069. (interactive "p")
  4070. (org-timestamp-change (- (prefix-numeric-value arg))))
  4071. (defun org-timestamp-up-day (&optional arg)
  4072. "Increase the date in the time stamp by one day.
  4073. With prefix ARG, change that many days."
  4074. (interactive "p")
  4075. (if (and (not (org-at-timestamp-p))
  4076. (org-on-heading-p))
  4077. (org-todo 'up)
  4078. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  4079. (defun org-timestamp-down-day (&optional arg)
  4080. "Decrease the date in the time stamp by one day.
  4081. With prefix ARG, change that many days."
  4082. (interactive "p")
  4083. (if (and (not (org-at-timestamp-p))
  4084. (org-on-heading-p))
  4085. (org-todo 'down)
  4086. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  4087. (defsubst org-pos-in-match-range (pos n)
  4088. (and (match-beginning n)
  4089. (<= (match-beginning n) pos)
  4090. (>= (match-end n) pos)))
  4091. (defun org-at-timestamp-p ()
  4092. "Determine if the cursor is in or at a timestamp."
  4093. (interactive)
  4094. (let* ((tsr org-ts-regexp2)
  4095. (pos (point))
  4096. (ans (or (looking-at tsr)
  4097. (save-excursion
  4098. (skip-chars-backward "^<\n\r\t")
  4099. (if (> (point) 1) (backward-char 1))
  4100. (and (looking-at tsr)
  4101. (> (- (match-end 0) pos) -1))))))
  4102. (and (boundp 'org-ts-what)
  4103. (setq org-ts-what
  4104. (cond
  4105. ((org-pos-in-match-range pos 2) 'year)
  4106. ((org-pos-in-match-range pos 3) 'month)
  4107. ((org-pos-in-match-range pos 7) 'hour)
  4108. ((org-pos-in-match-range pos 8) 'minute)
  4109. ((or (org-pos-in-match-range pos 4)
  4110. (org-pos-in-match-range pos 5)) 'day)
  4111. (t 'day))))
  4112. ans))
  4113. (defun org-timestamp-change (n &optional what)
  4114. "Change the date in the time stamp at point.
  4115. The date will be changed by N times WHAT. WHAT can be `day', `month',
  4116. `year', `minute', `second'. If WHAT is not given, the cursor position
  4117. in the timestamp determines what will be changed."
  4118. (let ((fmt (car org-time-stamp-formats))
  4119. org-ts-what
  4120. (pos (point))
  4121. ts time time0)
  4122. (if (not (org-at-timestamp-p))
  4123. (error "Not at a timestamp"))
  4124. (setq org-ts-what (or what org-ts-what))
  4125. (setq fmt (if (<= (abs (- (cdr org-ts-lengths)
  4126. (- (match-end 0) (match-beginning 0))))
  4127. 1)
  4128. (cdr org-time-stamp-formats)
  4129. (car org-time-stamp-formats)))
  4130. (setq ts (match-string 0))
  4131. (replace-match "")
  4132. (setq time0 (org-parse-time-string ts))
  4133. (setq time
  4134. (apply 'encode-time
  4135. (append
  4136. (list (or (car time0) 0))
  4137. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  4138. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  4139. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  4140. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  4141. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  4142. (nthcdr 6 time0))))
  4143. (if (eq what 'calendar)
  4144. (let ((cal-date
  4145. (save-excursion
  4146. (save-match-data
  4147. (set-buffer "*Calendar*")
  4148. (calendar-cursor-to-date)))))
  4149. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  4150. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  4151. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  4152. (setcar time0 (or (car time0) 0))
  4153. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  4154. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  4155. (setq time (apply 'encode-time time0))))
  4156. (insert (setq org-last-changed-timestamp (format-time-string fmt time)))
  4157. (goto-char pos)
  4158. ;; Try to recenter the calendar window, if any
  4159. (if (and org-calendar-follow-timestamp-change
  4160. (get-buffer-window "*Calendar*" t)
  4161. (memq org-ts-what '(day month year)))
  4162. (org-recenter-calendar (time-to-days time)))))
  4163. (defun org-recenter-calendar (date)
  4164. "If the calendar is visible, recenter it to DATE."
  4165. (let* ((win (selected-window))
  4166. (cwin (get-buffer-window "*Calendar*" t))
  4167. (calendar-move-hook nil))
  4168. (when cwin
  4169. (select-window cwin)
  4170. (calendar-goto-date (if (listp date) date
  4171. (calendar-gregorian-from-absolute date)))
  4172. (select-window win))))
  4173. (defun org-goto-calendar (&optional arg)
  4174. "Go to the Emacs calendar at the current date.
  4175. If there is a time stamp in the current line, go to that date.
  4176. A prefix ARG can be used to force the current date."
  4177. (interactive "P")
  4178. (let ((tsr org-ts-regexp) diff
  4179. (calendar-move-hook nil)
  4180. (view-diary-entries-initially nil))
  4181. (if (or (org-at-timestamp-p)
  4182. (save-excursion
  4183. (beginning-of-line 1)
  4184. (looking-at (concat ".*" tsr))))
  4185. (let ((d1 (time-to-days (current-time)))
  4186. (d2 (time-to-days
  4187. (org-time-string-to-time (match-string 1)))))
  4188. (setq diff (- d2 d1))))
  4189. (calendar)
  4190. (calendar-goto-today)
  4191. (if (and diff (not arg)) (calendar-forward-day diff))))
  4192. (defun org-date-from-calendar ()
  4193. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  4194. If there is already a time stamp at the cursor position, update it."
  4195. (interactive)
  4196. (org-timestamp-change 0 'calendar))
  4197. ;;; Agenda, and Diary Integration
  4198. ;;; Define the mode
  4199. (defvar org-agenda-mode-map (make-sparse-keymap)
  4200. "Keymap for `org-agenda-mode'.")
  4201. (defvar org-agenda-menu) ; defined later in this file.
  4202. (defvar org-agenda-follow-mode nil)
  4203. (defvar org-agenda-show-log nil)
  4204. (defvar org-agenda-buffer-name "*Org Agenda*")
  4205. (defvar org-agenda-redo-command nil)
  4206. (defvar org-agenda-mode-hook nil)
  4207. (defvar org-agenda-type nil)
  4208. (defvar org-agenda-force-single-file nil)
  4209. ;;;###autoload
  4210. (defun org-agenda-mode ()
  4211. "Mode for time-sorted view on action items in Org-mode files.
  4212. The following commands are available:
  4213. \\{org-agenda-mode-map}"
  4214. (interactive)
  4215. (kill-all-local-variables)
  4216. (setq major-mode 'org-agenda-mode)
  4217. (setq mode-name "Org-Agenda")
  4218. (use-local-map org-agenda-mode-map)
  4219. (easy-menu-add org-agenda-menu)
  4220. (if org-startup-truncated (setq truncate-lines t))
  4221. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  4222. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  4223. (unless org-agenda-keep-modes
  4224. (setq org-agenda-follow-mode nil
  4225. org-agenda-show-log nil))
  4226. (easy-menu-change
  4227. '("Agenda") "Agenda Files"
  4228. (append
  4229. (list
  4230. (vector
  4231. (if (get 'org-agenda-files 'org-restrict)
  4232. "Restricted to single file"
  4233. "Edit File List")
  4234. '(org-edit-agenda-file-list)
  4235. (not (get 'org-agenda-files 'org-restrict)))
  4236. "--")
  4237. (mapcar 'org-file-menu-entry (org-agenda-files))))
  4238. (org-agenda-set-mode-name)
  4239. (apply
  4240. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  4241. (list 'org-agenda-mode-hook)))
  4242. (define-key org-agenda-mode-map "\C-i" 'org-agenda-goto)
  4243. (define-key org-agenda-mode-map [(tab)] 'org-agenda-goto)
  4244. (define-key org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  4245. (define-key org-agenda-mode-map " " 'org-agenda-show)
  4246. (define-key org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  4247. (define-key org-agenda-mode-map "o" 'delete-other-windows)
  4248. (define-key org-agenda-mode-map "L" 'org-agenda-recenter)
  4249. (define-key org-agenda-mode-map "t" 'org-agenda-todo)
  4250. (define-key org-agenda-mode-map ":" 'org-agenda-set-tags)
  4251. (define-key org-agenda-mode-map "." 'org-agenda-goto-today)
  4252. (define-key org-agenda-mode-map "d" 'org-agenda-day-view)
  4253. (define-key org-agenda-mode-map "w" 'org-agenda-week-view)
  4254. (define-key org-agenda-mode-map (org-key 'S-right) 'org-agenda-date-later)
  4255. (define-key org-agenda-mode-map (org-key 'S-left) 'org-agenda-date-earlier)
  4256. (define-key org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  4257. (define-key org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  4258. (define-key org-agenda-mode-map ">" 'org-agenda-date-prompt)
  4259. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  4260. (while l (define-key org-agenda-mode-map
  4261. (int-to-string (pop l)) 'digit-argument)))
  4262. (define-key org-agenda-mode-map "f" 'org-agenda-follow-mode)
  4263. (define-key org-agenda-mode-map "l" 'org-agenda-log-mode)
  4264. (define-key org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  4265. (define-key org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  4266. (define-key org-agenda-mode-map "r" 'org-agenda-redo)
  4267. (define-key org-agenda-mode-map "q" 'org-agenda-quit)
  4268. (define-key org-agenda-mode-map "x" 'org-agenda-exit)
  4269. (define-key org-agenda-mode-map "P" 'org-agenda-show-priority)
  4270. (define-key org-agenda-mode-map "T" 'org-agenda-show-tags)
  4271. (define-key org-agenda-mode-map "n" 'next-line)
  4272. (define-key org-agenda-mode-map "p" 'previous-line)
  4273. (define-key org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  4274. (define-key org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  4275. (define-key org-agenda-mode-map "," 'org-agenda-priority)
  4276. (define-key org-agenda-mode-map "\C-c," 'org-agenda-priority)
  4277. (define-key org-agenda-mode-map "i" 'org-agenda-diary-entry)
  4278. (define-key org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  4279. (eval-after-load "calendar"
  4280. '(define-key calendar-mode-map org-calendar-to-agenda-key
  4281. 'org-calendar-goto-agenda))
  4282. (define-key org-agenda-mode-map "C" 'org-agenda-convert-date)
  4283. (define-key org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
  4284. (define-key org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  4285. (define-key org-agenda-mode-map "s" 'org-agenda-sunrise-sunset)
  4286. (define-key org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  4287. (define-key org-agenda-mode-map "h" 'org-agenda-holidays)
  4288. (define-key org-agenda-mode-map "H" 'org-agenda-holidays)
  4289. (define-key org-agenda-mode-map "+" 'org-agenda-priority-up)
  4290. (define-key org-agenda-mode-map "-" 'org-agenda-priority-down)
  4291. (define-key org-agenda-mode-map (org-key 'S-up) 'org-agenda-priority-up)
  4292. (define-key org-agenda-mode-map (org-key 'S-down) 'org-agenda-priority-down)
  4293. (define-key org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  4294. (define-key org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  4295. (define-key org-agenda-mode-map [(right)] 'org-agenda-later)
  4296. (define-key org-agenda-mode-map [(left)] 'org-agenda-earlier)
  4297. (define-key org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  4298. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  4299. "Local keymap for agenda entries from Org-mode.")
  4300. (define-key org-agenda-keymap
  4301. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  4302. (define-key org-agenda-keymap
  4303. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  4304. (define-key org-agenda-keymap [follow-link] 'mouse-face)
  4305. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  4306. '("Agenda"
  4307. ("Agenda Files")
  4308. "--"
  4309. ["Show" org-agenda-show t]
  4310. ["Go To (other window)" org-agenda-goto t]
  4311. ["Go To (one window)" org-agenda-switch-to t]
  4312. ["Follow Mode" org-agenda-follow-mode
  4313. :style toggle :selected org-agenda-follow-mode :active t]
  4314. "--"
  4315. ["Cycle TODO" org-agenda-todo t]
  4316. ("Tags"
  4317. ["Show all Tags" org-agenda-show-tags t]
  4318. ["Set Tags" org-agenda-set-tags t])
  4319. ("Reschedule"
  4320. ["Reschedule +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  4321. ["Reschedule -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  4322. "--"
  4323. ["Reschedule to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  4324. ("Priority"
  4325. ["Set Priority" org-agenda-priority t]
  4326. ["Increase Priority" org-agenda-priority-up t]
  4327. ["Decrease Priority" org-agenda-priority-down t]
  4328. ["Show Priority" org-agenda-show-priority t])
  4329. "--"
  4330. ;; ["New agenda command" org-agenda t]
  4331. ["Rebuild buffer" org-agenda-redo t]
  4332. "--"
  4333. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  4334. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  4335. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  4336. "--"
  4337. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  4338. :style radio :selected (equal org-agenda-ndays 1)]
  4339. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  4340. :style radio :selected (equal org-agenda-ndays 7)]
  4341. "--"
  4342. ["Show Logbook entries" org-agenda-log-mode
  4343. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  4344. ["Include Diary" org-agenda-toggle-diary
  4345. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  4346. ["Use Time Grid" org-agenda-toggle-time-grid
  4347. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)]
  4348. "--"
  4349. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  4350. ("Calendar Commands"
  4351. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  4352. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  4353. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  4354. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  4355. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)])
  4356. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t]
  4357. "--"
  4358. ["Quit" org-agenda-quit t]
  4359. ["Exit and Release Buffers" org-agenda-exit t]
  4360. ))
  4361. ;;;###autoload
  4362. (defun org-agenda (arg)
  4363. "Dispatch agenda commands to collect entries to the agenda buffer.
  4364. Prompts for a character to select a command. Any prefix arg will be passed
  4365. on to the selected command. The default selections are:
  4366. a Call `org-agenda' to display the agenda for the current day or week.
  4367. t Call `org-todo-list' to display the global todo list.
  4368. T Call `org-todo-list' to display the global todo list, select only
  4369. entries with a specific TODO keyword (the user gets a prompt).
  4370. m Call `org-tags-view' to display headlines with tags matching
  4371. a condition (the user is prompted for the condition).
  4372. M Like `m', but select only TODO entries, no ordinary headlines.
  4373. More commands can be added by configuring the variable
  4374. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  4375. searches can be pre-defined in this way.
  4376. If the current buffer is in Org-mode and visiting a file, you can also
  4377. first press `1' to indicate that the agenda should be temporarily (until the
  4378. next use of \\[org-agenda]) restricted to the current file."
  4379. (interactive "P")
  4380. (catch 'exit
  4381. (let ((restrict-ok (and buffer-file-name (eq major-mode 'org-mode)))
  4382. (custom org-agenda-custom-commands)
  4383. c entry key type string)
  4384. (put 'org-agenda-files 'org-restrict nil)
  4385. (save-window-excursion
  4386. (delete-other-windows)
  4387. (switch-to-buffer-other-window " *Agenda Commands*")
  4388. (erase-buffer)
  4389. (insert
  4390. "Press key for an agenda command:
  4391. --------------------------------
  4392. a Agenda for current week or day
  4393. t List of all TODO entries T Entries with special TODO kwd
  4394. m Match a TAGS query M Like m, but only TODO entries
  4395. C Configure your own agenda commands")
  4396. (while (setq entry (pop custom))
  4397. (setq key (car entry) type (nth 1 entry) string (nth 2 entry))
  4398. (insert (format "\n%-4s%-14s: %s"
  4399. key
  4400. (cond
  4401. ((eq type 'tags) "Tags query")
  4402. ((eq type 'todo) "TODO keyword")
  4403. ((eq type 'tags-tree) "Tags tree")
  4404. ((eq type 'todo-tree) "TODO kwd tree")
  4405. ((eq type 'occur-tree) "Occur tree")
  4406. (t "???"))
  4407. (org-string-props string 'face 'org-link))))
  4408. (goto-char (point-min))
  4409. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  4410. (message "Press key for agenda command%s"
  4411. (if restrict-ok ", or [1] to restrict to current file" ""))
  4412. (setq c (read-char-exclusive))
  4413. (message "")
  4414. (when (equal c ?1)
  4415. (if restrict-ok
  4416. (put 'org-agenda-files 'org-restrict (list buffer-file-name))
  4417. (error "Cannot restrict agenda to current buffer"))
  4418. (message "Press key for agenda command%s"
  4419. (if restrict-ok " (restricted to current file)" ""))
  4420. (setq c (read-char-exclusive))
  4421. (message "")))
  4422. (require 'calendar) ; FIXME: can we avoid this for some commands?
  4423. ;; For example the todo list should not need it (but does...)
  4424. (cond
  4425. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  4426. ((equal c ?a) (call-interactively 'org-agenda-list))
  4427. ((equal c ?t) (call-interactively 'org-todo-list))
  4428. ((equal c ?T)
  4429. (setq current-prefix-arg (or arg '(4)))
  4430. (call-interactively 'org-todo-list))
  4431. ((equal c ?m) (call-interactively 'org-tags-view))
  4432. ((equal c ?M)
  4433. (setq current-prefix-arg (or arg '(4)))
  4434. (call-interactively 'org-tags-view))
  4435. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  4436. (setq type (nth 1 entry) string (nth 2 entry))
  4437. (cond
  4438. ((eq type 'tags)
  4439. (org-tags-view current-prefix-arg string))
  4440. ((eq type 'todo)
  4441. (org-todo-list string))
  4442. ((eq type 'tags-tree)
  4443. (org-check-for-org-mode)
  4444. (org-tags-sparse-tree current-prefix-arg string))
  4445. ((eq type 'todo-tree)
  4446. (org-check-for-org-mode)
  4447. (org-occur (concat "^" outline-regexp "[ \t]*"
  4448. (regexp-quote string) "\\>")))
  4449. ((eq type 'occur-tree)
  4450. (org-check-for-org-mode)
  4451. (org-occur string))
  4452. (t (error "Invalid custom agenda command type %s" type))))
  4453. (t (error "Invalid key"))))))
  4454. (defun org-check-for-org-mode ()
  4455. "Make sure current buffer is in org-mode. Error if not."
  4456. (or (eq major-mode 'org-mode)
  4457. (error "Cannot execute org-mode agenda command on buffer in %s."
  4458. major-mode)))
  4459. (defun org-fit-agenda-window ()
  4460. "Fit the window to the buffer size."
  4461. (and org-fit-agenda-window
  4462. (fboundp 'fit-window-to-buffer)
  4463. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  4464. (/ (frame-height) 2))))
  4465. (defun org-agenda-files (&optional unrestricted)
  4466. "Get the list of agenda files.
  4467. Optional UNRESTRICTED means return the full list even if a restriction
  4468. is currently in place."
  4469. (cond
  4470. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  4471. ((stringp org-agenda-files) (org-read-agenda-file-list))
  4472. ((listp org-agenda-files) org-agenda-files)
  4473. (t (error "Invalid value of `org-agenda-files'"))))
  4474. (defvar org-window-configuration)
  4475. (defun org-edit-agenda-file-list ()
  4476. "Edit the list of agenda files.
  4477. Depending on setup, this either uses customize to edit the variable
  4478. `org-agenda-files', or it visits the file that is holding the list. In the
  4479. latter case, the buffer is set up in a way that saving it automatically kills
  4480. the buffer and restores the previous window configuration."
  4481. (interactive)
  4482. (if (stringp org-agenda-files)
  4483. (let ((cw (current-window-configuration)))
  4484. (find-file org-agenda-files)
  4485. (set (make-local-variable 'org-window-configuration) cw)
  4486. (org-add-hook 'after-save-hook
  4487. (lambda ()
  4488. (set-window-configuration
  4489. (prog1 org-window-configuration
  4490. (kill-buffer (current-buffer))))
  4491. (org-install-agenda-files-menu)
  4492. (message "New agenda file list installed"))
  4493. nil 'local)
  4494. (message (substitute-command-keys
  4495. "Edit list and finish with \\[save-buffer]")))
  4496. (customize-variable 'org-agenda-files)))
  4497. (defun org-store-new-agenda-file-list (list)
  4498. "Set new value for the agenda file list and save it correcly."
  4499. (if (stringp org-agenda-files)
  4500. (let ((f org-agenda-files) b)
  4501. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  4502. (with-temp-file f
  4503. (insert (mapconcat 'identity list "\n") "\n")))
  4504. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  4505. (setq org-agenda-files list)
  4506. (customize-save-variable 'org-agenda-files org-agenda-files))))
  4507. (defun org-read-agenda-file-list ()
  4508. "Read the list of agenda files from a file."
  4509. (when (stringp org-agenda-files)
  4510. (with-temp-buffer
  4511. (insert-file-contents org-agenda-files)
  4512. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  4513. (defvar org-agenda-markers nil
  4514. "List of all currently active markers created by `org-agenda'.")
  4515. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  4516. "Creation time of the last agenda marker.")
  4517. (defun org-agenda-new-marker (&optional pos)
  4518. "Return a new agenda marker.
  4519. Org-mode keeps a list of these markers and resets them when they are
  4520. no longer in use."
  4521. (let ((m (copy-marker (or pos (point)))))
  4522. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  4523. (push m org-agenda-markers)
  4524. m))
  4525. (defun org-agenda-maybe-reset-markers (&optional force)
  4526. "Reset markers created by `org-agenda'. But only if they are old enough."
  4527. (if (or force
  4528. (> (- (time-to-seconds (current-time))
  4529. org-agenda-last-marker-time)
  4530. 5))
  4531. (while org-agenda-markers
  4532. (move-marker (pop org-agenda-markers) nil))))
  4533. (defvar org-agenda-new-buffers nil
  4534. "Buffers created to visit agenda files.")
  4535. (defun org-get-agenda-file-buffer (file)
  4536. "Get a buffer visiting FILE. If the buffer needs to be created, add
  4537. it to the list of buffers which might be released later."
  4538. (let ((buf (find-buffer-visiting file)))
  4539. (if buf
  4540. buf ; just return it
  4541. ;; Make a new buffer and remember it
  4542. (setq buf (find-file-noselect file))
  4543. (if buf (push buf org-agenda-new-buffers))
  4544. buf)))
  4545. (defun org-release-buffers (blist)
  4546. "Release all buffers in list, asking the user for confirmation when needed.
  4547. When a buffer is unmodified, it is just killed. When modified, it is saved
  4548. \(if the user agrees) and then killed."
  4549. (let (buf file)
  4550. (while (setq buf (pop blist))
  4551. (setq file (buffer-file-name buf))
  4552. (when (and (buffer-modified-p buf)
  4553. file
  4554. (y-or-n-p (format "Save file %s? " file)))
  4555. (with-current-buffer buf (save-buffer)))
  4556. (kill-buffer buf))))
  4557. (defvar org-respect-restriction nil) ; Dynamically-scoped param.
  4558. (defun org-timeline (&optional include-all keep-modes)
  4559. "Show a time-sorted view of the entries in the current org file.
  4560. Only entries with a time stamp of today or later will be listed. With
  4561. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  4562. under the current date.
  4563. If the buffer contains an active region, only check the region for
  4564. dates."
  4565. (interactive "P")
  4566. (require 'calendar)
  4567. (org-agenda-maybe-reset-markers 'force)
  4568. (org-compile-prefix-format org-timeline-prefix-format)
  4569. (let* ((dopast t)
  4570. (dotodo include-all)
  4571. (doclosed org-agenda-show-log)
  4572. (org-agenda-keep-modes keep-modes)
  4573. (entry buffer-file-name)
  4574. (org-agenda-files (list buffer-file-name))
  4575. (date (calendar-current-date))
  4576. (win (selected-window))
  4577. (pos1 (point))
  4578. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  4579. (end (if (org-region-active-p) (region-end) (point-max)))
  4580. (day-numbers (org-get-all-dates beg end 'no-ranges
  4581. t doclosed)) ; always include today
  4582. (today (time-to-days (current-time)))
  4583. (org-respect-restriction t)
  4584. (past t)
  4585. args
  4586. s e rtn d)
  4587. (setq org-agenda-redo-command
  4588. (list 'progn
  4589. (list 'switch-to-buffer-other-window (current-buffer))
  4590. (list 'org-timeline (list 'quote include-all) t)))
  4591. (if (not dopast)
  4592. ;; Remove past dates from the list of dates.
  4593. (setq day-numbers (delq nil (mapcar (lambda(x)
  4594. (if (>= x today) x nil))
  4595. day-numbers))))
  4596. (switch-to-buffer-other-window
  4597. (get-buffer-create org-agenda-buffer-name))
  4598. (setq buffer-read-only nil)
  4599. (erase-buffer)
  4600. (org-agenda-mode) (setq buffer-read-only nil)
  4601. (set (make-local-variable 'org-agenda-type) 'timeline)
  4602. (if doclosed (push :closed args))
  4603. (push :timestamp args)
  4604. (if dotodo (push :todo args))
  4605. (while (setq d (pop day-numbers))
  4606. (if (and (>= d today)
  4607. dopast
  4608. past)
  4609. (progn
  4610. (setq past nil)
  4611. (insert (make-string 79 ?-) "\n")))
  4612. (setq date (calendar-gregorian-from-absolute d))
  4613. (setq s (point))
  4614. (setq rtn (apply 'org-agenda-get-day-entries
  4615. entry date args))
  4616. (if (or rtn (equal d today))
  4617. (progn
  4618. (insert (calendar-day-name date) " "
  4619. (number-to-string (extract-calendar-day date)) " "
  4620. (calendar-month-name (extract-calendar-month date)) " "
  4621. (number-to-string (extract-calendar-year date)) "\n")
  4622. (put-text-property s (1- (point)) 'face
  4623. 'org-link)
  4624. (if (equal d today)
  4625. (put-text-property s (1- (point)) 'org-today t))
  4626. (insert (org-finalize-agenda-entries rtn) "\n")
  4627. (put-text-property s (1- (point)) 'day d))))
  4628. (goto-char (point-min))
  4629. (setq buffer-read-only t)
  4630. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  4631. (point-min)))
  4632. (when (not org-select-timeline-window)
  4633. (select-window win)
  4634. (goto-char pos1))))
  4635. ;;;###autoload
  4636. (defun org-agenda-list (&optional include-all start-day ndays keep-modes)
  4637. "Produce a weekly view from all files in variable `org-agenda-files'.
  4638. The view will be for the current week, but from the overview buffer you
  4639. will be able to go to other weeks.
  4640. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  4641. also be shown, under the current date.
  4642. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  4643. on the days are also shown. See the variable `org-log-done' for how
  4644. to turn on logging.
  4645. START-DAY defaults to TODAY, or to the most recent match for the weekday
  4646. given in `org-agenda-start-on-weekday'.
  4647. NDAYS defaults to `org-agenda-ndays'."
  4648. (interactive "P")
  4649. (org-agenda-maybe-reset-markers 'force)
  4650. (org-compile-prefix-format org-agenda-prefix-format)
  4651. (require 'calendar)
  4652. (let* ((org-agenda-start-on-weekday
  4653. (if (or (equal ndays 1)
  4654. (and (null ndays) (equal 1 org-agenda-ndays)))
  4655. nil org-agenda-start-on-weekday))
  4656. (org-agenda-keep-modes keep-modes)
  4657. (thefiles (org-agenda-files))
  4658. (files thefiles)
  4659. (win (selected-window))
  4660. (today (time-to-days (current-time)))
  4661. (sd (or start-day today))
  4662. (start (if (or (null org-agenda-start-on-weekday)
  4663. (< org-agenda-ndays 7))
  4664. sd
  4665. (let* ((nt (calendar-day-of-week
  4666. (calendar-gregorian-from-absolute sd)))
  4667. (n1 org-agenda-start-on-weekday)
  4668. (d (- nt n1)))
  4669. (- sd (+ (if (< d 0) 7 0) d)))))
  4670. (day-numbers (list start))
  4671. (inhibit-redisplay t)
  4672. s e rtn rtnall file date d start-pos end-pos todayp nd)
  4673. (setq org-agenda-redo-command
  4674. (list 'org-agenda-list (list 'quote include-all) start-day ndays t))
  4675. ;; Make the list of days
  4676. (setq ndays (or ndays org-agenda-ndays)
  4677. nd ndays)
  4678. (while (> ndays 1)
  4679. (push (1+ (car day-numbers)) day-numbers)
  4680. (setq ndays (1- ndays)))
  4681. (setq day-numbers (nreverse day-numbers))
  4682. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  4683. (progn
  4684. (delete-other-windows)
  4685. (switch-to-buffer-other-window
  4686. (get-buffer-create org-agenda-buffer-name))))
  4687. (setq buffer-read-only nil)
  4688. (erase-buffer)
  4689. (org-agenda-mode) (setq buffer-read-only nil)
  4690. (set (make-local-variable 'org-agenda-type) 'agenda)
  4691. (set (make-local-variable 'starting-day) (car day-numbers))
  4692. (set (make-local-variable 'include-all-loc) include-all)
  4693. (when (and (or include-all org-agenda-include-all-todo)
  4694. (member today day-numbers))
  4695. (setq files thefiles
  4696. rtnall nil)
  4697. (while (setq file (pop files))
  4698. (catch 'nextfile
  4699. (org-check-agenda-file file)
  4700. (setq date (calendar-gregorian-from-absolute today)
  4701. rtn (org-agenda-get-day-entries
  4702. file date :todo))
  4703. (setq rtnall (append rtnall rtn))))
  4704. (when rtnall
  4705. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  4706. (add-text-properties (point-min) (1- (point))
  4707. (list 'face 'org-link))
  4708. (insert (org-finalize-agenda-entries rtnall) "\n")))
  4709. (while (setq d (pop day-numbers))
  4710. (setq date (calendar-gregorian-from-absolute d)
  4711. s (point))
  4712. (if (or (setq todayp (= d today))
  4713. (and (not start-pos) (= d sd)))
  4714. (setq start-pos (point))
  4715. (if (and start-pos (not end-pos))
  4716. (setq end-pos (point))))
  4717. (setq files thefiles
  4718. rtnall nil)
  4719. (while (setq file (pop files))
  4720. (catch 'nextfile
  4721. (org-check-agenda-file file)
  4722. (if org-agenda-show-log
  4723. (setq rtn (org-agenda-get-day-entries
  4724. file date
  4725. :deadline :scheduled :timestamp :closed))
  4726. (setq rtn (org-agenda-get-day-entries
  4727. file date
  4728. :deadline :scheduled :timestamp)))
  4729. (setq rtnall (append rtnall rtn))))
  4730. (if org-agenda-include-diary
  4731. (progn
  4732. (require 'diary-lib)
  4733. (setq rtn (org-get-entries-from-diary date))
  4734. (setq rtnall (append rtnall rtn))))
  4735. (if (or rtnall org-agenda-show-all-dates)
  4736. (progn
  4737. (insert (format "%-9s %2d %s %4d\n"
  4738. (calendar-day-name date)
  4739. (extract-calendar-day date)
  4740. (calendar-month-name (extract-calendar-month date))
  4741. (extract-calendar-year date)))
  4742. (put-text-property s (1- (point)) 'face
  4743. 'org-link)
  4744. (if rtnall (insert
  4745. (org-finalize-agenda-entries
  4746. (org-agenda-add-time-grid-maybe
  4747. rtnall nd todayp))
  4748. "\n"))
  4749. (put-text-property s (1- (point)) 'day d))))
  4750. (goto-char (point-min))
  4751. (setq buffer-read-only t)
  4752. (org-fit-agenda-window)
  4753. (unless (and (pos-visible-in-window-p (point-min))
  4754. (pos-visible-in-window-p (point-max)))
  4755. (goto-char (1- (point-max)))
  4756. (recenter -1)
  4757. (if (not (pos-visible-in-window-p (or start-pos 1)))
  4758. (progn
  4759. (goto-char (or start-pos 1))
  4760. (recenter 1))))
  4761. (goto-char (or start-pos 1))
  4762. (if (not org-select-agenda-window) (select-window win))
  4763. (message "")))
  4764. (defvar org-select-this-todo-keyword nil)
  4765. ;;;###autoload
  4766. (defun org-todo-list (arg &optional keep-modes)
  4767. "Show all TODO entries from all agenda file in a single list.
  4768. The prefix arg can be used to select a specific TODO keyword and limit
  4769. the list to these. When using \\[universal-argument], you will be prompted
  4770. for a keyword. A numeric prefix directly selects the Nth keyword in
  4771. `org-todo-keywords'."
  4772. (interactive "P")
  4773. (org-agenda-maybe-reset-markers 'force)
  4774. (org-compile-prefix-format org-agenda-prefix-format)
  4775. (let* ((org-agenda-keep-modes keep-modes)
  4776. (today (time-to-days (current-time)))
  4777. (date (calendar-gregorian-from-absolute today))
  4778. (win (selected-window))
  4779. (kwds org-todo-keywords)
  4780. (completion-ignore-case t)
  4781. (org-select-this-todo-keyword
  4782. (if (stringp arg) arg
  4783. (and arg (integerp arg) (nth (1- arg) org-todo-keywords))))
  4784. rtn rtnall files file pos)
  4785. (when (equal arg '(4))
  4786. (setq org-select-this-todo-keyword
  4787. (completing-read "Keyword: " (mapcar 'list org-todo-keywords)
  4788. nil t)))
  4789. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  4790. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  4791. (progn
  4792. (delete-other-windows)
  4793. (switch-to-buffer-other-window
  4794. (get-buffer-create org-agenda-buffer-name))))
  4795. (setq buffer-read-only nil)
  4796. (erase-buffer)
  4797. (org-agenda-mode) (setq buffer-read-only nil)
  4798. (set (make-local-variable 'org-agenda-type) 'todo)
  4799. (set (make-local-variable 'last-arg) arg)
  4800. (set (make-local-variable 'org-todo-keywords) kwds)
  4801. (set (make-local-variable 'org-agenda-redo-command)
  4802. '(org-todo-list (or current-prefix-arg last-arg) t))
  4803. (setq files (org-agenda-files)
  4804. rtnall nil)
  4805. (while (setq file (pop files))
  4806. (catch 'nextfile
  4807. (org-check-agenda-file file)
  4808. (setq rtn (org-agenda-get-day-entries file date :todo))
  4809. (setq rtnall (append rtnall rtn))))
  4810. (insert "Global list of TODO items of type: ")
  4811. (add-text-properties (point-min) (1- (point))
  4812. (list 'face 'org-link))
  4813. (setq pos (point))
  4814. (insert (or org-select-this-todo-keyword "ALL") "\n")
  4815. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  4816. (setq pos (point))
  4817. (insert
  4818. "Available with `N r': (0)ALL "
  4819. (let ((n 0))
  4820. (mapconcat (lambda (x)
  4821. (format "(%d)%s" (setq n (1+ n)) x))
  4822. org-todo-keywords " "))
  4823. "\n")
  4824. (add-text-properties pos (1- (point)) (list 'face 'org-link))
  4825. (when rtnall
  4826. (insert (org-finalize-agenda-entries rtnall) "\n"))
  4827. (goto-char (point-min))
  4828. (setq buffer-read-only t)
  4829. (org-fit-agenda-window)
  4830. (if (not org-select-agenda-window) (select-window win))))
  4831. (defun org-check-agenda-file (file)
  4832. "Make sure FILE exists. If not, ask user what to do."
  4833. (when (not (file-exists-p file))
  4834. (message "non-existent file %s. [R]emove from list or [A]bort?"
  4835. (abbreviate-file-name file))
  4836. (let ((r (downcase (read-char-exclusive))))
  4837. (cond
  4838. ((equal r ?r)
  4839. (org-remove-file file)
  4840. (throw 'nextfile t))
  4841. (t (error "Abort"))))))
  4842. (defun org-agenda-check-type (error &rest types)
  4843. "Check if agenda buffer is of allowed type.
  4844. If ERROR is non-nil, throw an error, otherwise just return nil."
  4845. (if (memq org-agenda-type types)
  4846. t
  4847. (if error
  4848. (error "Now allowed in %s-type agenda buffers" org-agenda-type)
  4849. nil)))
  4850. (defun org-agenda-quit ()
  4851. "Exit agenda by removing the window or the buffer."
  4852. (interactive)
  4853. (let ((buf (current-buffer)))
  4854. (if (not (one-window-p)) (delete-window))
  4855. (kill-buffer buf)
  4856. (org-agenda-maybe-reset-markers 'force)))
  4857. (defun org-agenda-exit ()
  4858. "Exit agenda by removing the window or the buffer.
  4859. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  4860. Org-mode buffers visited directly by the user will not be touched."
  4861. (interactive)
  4862. (org-release-buffers org-agenda-new-buffers)
  4863. (setq org-agenda-new-buffers nil)
  4864. (org-agenda-quit))
  4865. (defun org-agenda-redo ()
  4866. "Rebuild Agenda.
  4867. When this is the global TODO list, a prefix argument will be interpreted."
  4868. (interactive)
  4869. (message "Rebuilding agenda buffer...")
  4870. (eval org-agenda-redo-command)
  4871. (message "Rebuilding agenda buffer...done"))
  4872. (defun org-agenda-goto-today ()
  4873. "Go to today."
  4874. (interactive)
  4875. (org-agenda-check-type t 'timeline 'agenda)
  4876. (if (boundp 'starting-day)
  4877. (let ((cmd (car org-agenda-redo-command))
  4878. (iall (nth 1 org-agenda-redo-command))
  4879. (nday (nth 3 org-agenda-redo-command))
  4880. (keep (nth 4 org-agenda-redo-command)))
  4881. (eval (list cmd iall nil nday keep)))
  4882. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  4883. (point-min)))))
  4884. (defun org-agenda-later (arg)
  4885. "Go forward in time by `org-agenda-ndays' days.
  4886. With prefix ARG, go forward that many times `org-agenda-ndays'."
  4887. (interactive "p")
  4888. (org-agenda-check-type t 'agenda)
  4889. (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
  4890. (+ starting-day (* arg org-agenda-ndays)) nil t))
  4891. (defun org-agenda-earlier (arg)
  4892. "Go back in time by `org-agenda-ndays' days.
  4893. With prefix ARG, go back that many times `org-agenda-ndays'."
  4894. (interactive "p")
  4895. (org-agenda-check-type t 'agenda)
  4896. (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
  4897. (- starting-day (* arg org-agenda-ndays)) nil t))
  4898. (defun org-agenda-week-view ()
  4899. "Switch to weekly view for agenda."
  4900. (interactive)
  4901. (org-agenda-check-type t 'agenda)
  4902. (setq org-agenda-ndays 7)
  4903. (org-agenda-list include-all-loc
  4904. (or (get-text-property (point) 'day)
  4905. starting-day)
  4906. nil t)
  4907. (org-agenda-set-mode-name)
  4908. (message "Switched to week view"))
  4909. (defun org-agenda-day-view ()
  4910. "Switch to daily view for agenda."
  4911. (interactive)
  4912. (org-agenda-check-type t 'agenda)
  4913. (setq org-agenda-ndays 1)
  4914. (org-agenda-list include-all-loc
  4915. (or (get-text-property (point) 'day)
  4916. starting-day)
  4917. nil t)
  4918. (org-agenda-set-mode-name)
  4919. (message "Switched to day view"))
  4920. (defun org-agenda-next-date-line (&optional arg)
  4921. "Jump to the next line indicating a date in agenda buffer."
  4922. (interactive "p")
  4923. (org-agenda-check-type t 'agenda 'timeline)
  4924. (beginning-of-line 1)
  4925. (if (looking-at "^\\S-") (forward-char 1))
  4926. (if (not (re-search-forward "^\\S-" nil t arg))
  4927. (progn
  4928. (backward-char 1)
  4929. (error "No next date after this line in this buffer")))
  4930. (goto-char (match-beginning 0)))
  4931. (defun org-agenda-previous-date-line (&optional arg)
  4932. "Jump to the previous line indicating a date in agenda buffer."
  4933. (interactive "p")
  4934. (org-agenda-check-type t 'agenda 'timeline)
  4935. (beginning-of-line 1)
  4936. (if (not (re-search-backward "^\\S-" nil t arg))
  4937. (error "No previous date before this line in this buffer")))
  4938. ;; Initialize the highlight
  4939. (defvar org-hl (org-make-overlay 1 1))
  4940. (org-overlay-put org-hl 'face 'highlight)
  4941. (defun org-highlight (begin end &optional buffer)
  4942. "Highlight a region with overlay."
  4943. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  4944. org-hl begin end (or buffer (current-buffer))))
  4945. (defun org-unhighlight ()
  4946. "Detach overlay INDEX."
  4947. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  4948. (defun org-agenda-follow-mode ()
  4949. "Toggle follow mode in an agenda buffer."
  4950. (interactive)
  4951. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  4952. (org-agenda-set-mode-name)
  4953. (message "Follow mode is %s"
  4954. (if org-agenda-follow-mode "on" "off")))
  4955. (defun org-agenda-log-mode ()
  4956. "Toggle log mode in an agenda buffer."
  4957. (interactive)
  4958. (org-agenda-check-type t 'agenda 'timeline)
  4959. (setq org-agenda-show-log (not org-agenda-show-log))
  4960. (org-agenda-set-mode-name)
  4961. (org-agenda-redo)
  4962. (message "Log mode is %s"
  4963. (if org-agenda-show-log "on" "off")))
  4964. (defun org-agenda-toggle-diary ()
  4965. "Toggle diary inclusion in an agenda buffer."
  4966. (interactive)
  4967. (org-agenda-check-type t 'agenda)
  4968. (setq org-agenda-include-diary (not org-agenda-include-diary))
  4969. (org-agenda-redo)
  4970. (org-agenda-set-mode-name)
  4971. (message "Diary inclusion turned %s"
  4972. (if org-agenda-include-diary "on" "off")))
  4973. (defun org-agenda-toggle-time-grid ()
  4974. "Toggle time grid in an agenda buffer."
  4975. (interactive)
  4976. (org-agenda-check-type t 'agenda)
  4977. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  4978. (org-agenda-redo)
  4979. (org-agenda-set-mode-name)
  4980. (message "Time-grid turned %s"
  4981. (if org-agenda-use-time-grid "on" "off")))
  4982. (defun org-agenda-set-mode-name ()
  4983. "Set the mode name to indicate all the small mode settings."
  4984. (setq mode-name
  4985. (concat "Org-Agenda"
  4986. (if (equal org-agenda-ndays 1) " Day" "")
  4987. (if (equal org-agenda-ndays 7) " Week" "")
  4988. (if org-agenda-follow-mode " Follow" "")
  4989. (if org-agenda-include-diary " Diary" "")
  4990. (if org-agenda-use-time-grid " Grid" "")
  4991. (if org-agenda-show-log " Log" "")))
  4992. (force-mode-line-update))
  4993. (defun org-agenda-post-command-hook ()
  4994. (and (eolp) (not (bolp)) (backward-char 1))
  4995. (if (and org-agenda-follow-mode
  4996. (get-text-property (point) 'org-marker))
  4997. (org-agenda-show)))
  4998. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  4999. (defun org-get-entries-from-diary (date)
  5000. "Get the (Emacs Calendar) diary entries for DATE."
  5001. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  5002. (diary-display-hook '(fancy-diary-display))
  5003. (list-diary-entries-hook
  5004. (cons 'org-diary-default-entry list-diary-entries-hook))
  5005. (diary-file-name-prefix-function nil) ; turn this feature off
  5006. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  5007. entries
  5008. (org-disable-agenda-to-diary t))
  5009. (save-excursion
  5010. (save-window-excursion
  5011. (list-diary-entries date 1)))
  5012. (if (not (get-buffer fancy-diary-buffer))
  5013. (setq entries nil)
  5014. (with-current-buffer fancy-diary-buffer
  5015. (setq buffer-read-only nil)
  5016. (if (= (point-max) 1)
  5017. ;; No entries
  5018. (setq entries nil)
  5019. ;; Omit the date and other unnecessary stuff
  5020. (org-agenda-cleanup-fancy-diary)
  5021. ;; Add prefix to each line and extend the text properties
  5022. (if (= (point-max) 1)
  5023. (setq entries nil)
  5024. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  5025. (set-buffer-modified-p nil)
  5026. (kill-buffer fancy-diary-buffer)))
  5027. (when entries
  5028. (setq entries (org-split-string entries "\n"))
  5029. (setq entries
  5030. (mapcar
  5031. (lambda (x)
  5032. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  5033. ;; Extend the text properties to the beginning of the line
  5034. (add-text-properties
  5035. 0 (length x)
  5036. (text-properties-at (1- (length x)) x)
  5037. x)
  5038. x)
  5039. entries)))))
  5040. (defun org-agenda-cleanup-fancy-diary ()
  5041. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  5042. This gets rid of the date, the underline under the date, and
  5043. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  5044. date. It also removes lines that contain only whitespace."
  5045. (goto-char (point-min))
  5046. (if (looking-at ".*?:[ \t]*")
  5047. (progn
  5048. (replace-match "")
  5049. (re-search-forward "\n=+$" nil t)
  5050. (replace-match "")
  5051. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  5052. (re-search-forward "\n=+$" nil t)
  5053. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  5054. (goto-char (point-min))
  5055. (while (re-search-forward "^ +\n" nil t)
  5056. (replace-match ""))
  5057. (goto-char (point-min))
  5058. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  5059. (replace-match "")))
  5060. ;; Make sure entries from the diary have the right text properties.
  5061. (eval-after-load "diary-lib"
  5062. '(if (boundp 'diary-modify-entry-list-string-function)
  5063. ;; We can rely on the hook, nothing to do
  5064. nil
  5065. ;; Hook not avaiable, must use advice to make this work
  5066. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  5067. "Make the position visible."
  5068. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  5069. (stringp string)
  5070. buffer-file-name)
  5071. (setq string (org-modify-diary-entry-string string))))))
  5072. (defun org-modify-diary-entry-string (string)
  5073. "Add text properties to string, allowing org-mode to act on it."
  5074. (add-text-properties
  5075. 0 (length string)
  5076. (list 'mouse-face 'highlight
  5077. 'keymap org-agenda-keymap
  5078. 'help-echo
  5079. (format
  5080. "mouse-2 or RET jump to diary file %s"
  5081. (abbreviate-file-name buffer-file-name))
  5082. 'org-agenda-diary-link t
  5083. 'org-marker (org-agenda-new-marker (point-at-bol)))
  5084. string)
  5085. string)
  5086. (defun org-diary-default-entry ()
  5087. "Add a dummy entry to the diary.
  5088. Needed to avoid empty dates which mess up holiday display."
  5089. ;; Catch the error if dealing with the new add-to-diary-alist
  5090. (when org-disable-agenda-to-diary
  5091. (condition-case nil
  5092. (add-to-diary-list original-date "Org-mode dummy" "")
  5093. (error
  5094. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  5095. (defun org-cycle-agenda-files ()
  5096. "Cycle through the files in `org-agenda-files'.
  5097. If the current buffer visits an agenda file, find the next one in the list.
  5098. If the current buffer does not, find the first agenda file."
  5099. (interactive)
  5100. (let* ((fs (org-agenda-files t))
  5101. (files (append fs (list (car fs))))
  5102. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  5103. file)
  5104. (unless files (error "No agenda files"))
  5105. (catch 'exit
  5106. (while (setq file (pop files))
  5107. (if (equal (file-truename file) tcf)
  5108. (when (car files)
  5109. (find-file (car files))
  5110. (throw 'exit t))))
  5111. (find-file (car fs)))))
  5112. (defun org-agenda-file-to-end ()
  5113. "Move/add the current file to the end of the agenda file list.
  5114. If the file is not present in the list, it is appended to the list. If it is
  5115. present, it is moved there."
  5116. (interactive)
  5117. (org-agenda-file-to-front 'to-end))
  5118. (defun org-agenda-file-to-front (&optional to-end)
  5119. "Move/add the current file to the top of the agenda file list.
  5120. If the file is not present in the list, it is added to the front. If it is
  5121. present, it is moved there. With optional argument TO-END, add/move to the
  5122. end of the list."
  5123. (interactive "P")
  5124. (let ((file-alist (mapcar (lambda (x)
  5125. (cons (file-truename x) x))
  5126. (org-agenda-files t)))
  5127. (ctf (file-truename buffer-file-name))
  5128. x had)
  5129. (setq x (assoc ctf file-alist) had x)
  5130. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  5131. (if to-end
  5132. (setq file-alist (append (delq x file-alist) (list x)))
  5133. (setq file-alist (cons x (delq x file-alist))))
  5134. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  5135. (org-install-agenda-files-menu)
  5136. (message "File %s to %s of agenda file list"
  5137. (if had "moved" "added") (if to-end "end" "front"))))
  5138. (defun org-remove-file (&optional file)
  5139. "Remove current file from the list of files in variable `org-agenda-files'.
  5140. These are the files which are being checked for agenda entries.
  5141. Optional argument FILE means, use this file instead of the current."
  5142. (interactive)
  5143. (let* ((file (or file buffer-file-name))
  5144. (true-file (file-truename file))
  5145. (afile (abbreviate-file-name file))
  5146. (files (delq nil (mapcar
  5147. (lambda (x)
  5148. (if (equal true-file
  5149. (file-truename x))
  5150. nil x))
  5151. (org-agenda-files t)))))
  5152. (if (not (= (length files) (length (org-agenda-files t))))
  5153. (progn
  5154. (org-store-new-agenda-file-list files)
  5155. (org-install-agenda-files-menu)
  5156. (message "Removed file: %s" afile))
  5157. (message "File was not in list: %s" afile))))
  5158. (defun org-file-menu-entry (file)
  5159. (vector file (list 'find-file file) t))
  5160. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive)
  5161. "Return a list of all relevant day numbers from BEG to END buffer positions.
  5162. If NO-RANGES is non-nil, include only the start and end dates of a range,
  5163. not every single day in the range. If FORCE-TODAY is non-nil, make
  5164. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  5165. inactive time stamps (those in square brackets) are included."
  5166. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  5167. dates date day day1 day2 ts1 ts2)
  5168. (if force-today
  5169. (setq dates (list (time-to-days (current-time)))))
  5170. (save-excursion
  5171. (goto-char beg)
  5172. (while (re-search-forward re end t)
  5173. (setq day (time-to-days (org-time-string-to-time
  5174. (substring (match-string 1) 0 10))))
  5175. (or (memq day dates) (push day dates)))
  5176. (unless no-ranges
  5177. (goto-char beg)
  5178. (while (re-search-forward org-tr-regexp end t)
  5179. (setq ts1 (substring (match-string 1) 0 10)
  5180. ts2 (substring (match-string 2) 0 10)
  5181. day1 (time-to-days (org-time-string-to-time ts1))
  5182. day2 (time-to-days (org-time-string-to-time ts2)))
  5183. (while (< (setq day1 (1+ day1)) day2)
  5184. (or (memq day1 dates) (push day1 dates)))))
  5185. (sort dates '<))))
  5186. ;;;###autoload
  5187. (defun org-diary (&rest args)
  5188. "Return diary information from org-files.
  5189. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  5190. It accesses org files and extracts information from those files to be
  5191. listed in the diary. The function accepts arguments specifying what
  5192. items should be listed. The following arguments are allowed:
  5193. :timestamp List the headlines of items containing a date stamp or
  5194. date range matching the selected date. Deadlines will
  5195. also be listed, on the expiration day.
  5196. :deadline List any deadlines past due, or due within
  5197. `org-deadline-warning-days'. The listing occurs only
  5198. in the diary for *today*, not at any other date. If
  5199. an entry is marked DONE, it is no longer listed.
  5200. :scheduled List all items which are scheduled for the given date.
  5201. The diary for *today* also contains items which were
  5202. scheduled earlier and are not yet marked DONE.
  5203. :todo List all TODO items from the org-file. This may be a
  5204. long list - so this is not turned on by default.
  5205. Like deadlines, these entries only show up in the
  5206. diary for *today*, not at any other date.
  5207. The call in the diary file should look like this:
  5208. &%%(org-diary) ~/path/to/some/orgfile.org
  5209. Use a separate line for each org file to check. Or, if you omit the file name,
  5210. all files listed in `org-agenda-files' will be checked automatically:
  5211. &%%(org-diary)
  5212. If you don't give any arguments (as in the example above), the default
  5213. arguments (:deadline :scheduled :timestamp) are used. So the example above may
  5214. also be written as
  5215. &%%(org-diary :deadline :timestamp :scheduled)
  5216. The function expects the lisp variables `entry' and `date' to be provided
  5217. by the caller, because this is how the calendar works. Don't use this
  5218. function from a program - use `org-agenda-get-day-entries' instead."
  5219. (org-agenda-maybe-reset-markers)
  5220. (org-compile-prefix-format org-agenda-prefix-format)
  5221. (setq args (or args '(:deadline :scheduled :timestamp)))
  5222. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  5223. (list entry)
  5224. (org-agenda-files t)))
  5225. file rtn results)
  5226. ;; If this is called during org-agenda, don't return any entries to
  5227. ;; the calendar. Org Agenda will list these entries itself.
  5228. (if org-disable-agenda-to-diary (setq files nil))
  5229. (while (setq file (pop files))
  5230. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  5231. (setq results (append results rtn)))
  5232. (if results
  5233. (concat (org-finalize-agenda-entries results) "\n"))))
  5234. (defvar org-category-table nil)
  5235. (defun org-get-category-table ()
  5236. "Get the table of categories and positions in current buffer."
  5237. (let (tbl)
  5238. (save-excursion
  5239. (goto-char (point-min))
  5240. (while (re-search-forward "\\(^\\|\r\\)#\\+CATEGORY:[ \t]*\\(.*\\)" nil t)
  5241. (push (cons (point) (org-trim (match-string 2))) tbl)))
  5242. tbl))
  5243. (defun org-get-category (&optional pos)
  5244. "Get the category applying to position POS."
  5245. (if (not org-category-table)
  5246. (cond
  5247. ((null org-category)
  5248. (setq org-category
  5249. (if buffer-file-name
  5250. (file-name-sans-extension
  5251. (file-name-nondirectory buffer-file-name))
  5252. "???")))
  5253. ((symbolp org-category) (symbol-name org-category))
  5254. (t org-category))
  5255. (let ((tbl org-category-table)
  5256. (pos (or pos (point))))
  5257. (while (and tbl (> (caar tbl) pos))
  5258. (pop tbl))
  5259. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  5260. org-category-table))))))
  5261. (defun org-agenda-get-day-entries (file date &rest args)
  5262. "Does the work for `org-diary' and `org-agenda'.
  5263. FILE is the path to a file to be checked for entries. DATE is date like
  5264. the one returned by `calendar-current-date'. ARGS are symbols indicating
  5265. which kind of entries should be extracted. For details about these, see
  5266. the documentation of `org-diary'."
  5267. (setq args (or args '(:deadline :scheduled :timestamp)))
  5268. (let* ((org-startup-with-deadline-check nil)
  5269. (org-startup-folded nil)
  5270. (org-startup-align-all-tables nil)
  5271. (buffer (if (file-exists-p file)
  5272. (org-get-agenda-file-buffer file)
  5273. (error "No such file %s" file)))
  5274. arg results rtn)
  5275. (if (not buffer)
  5276. ;; If file does not exist, make sure an error message ends up in diary
  5277. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  5278. (with-current-buffer buffer
  5279. (unless (eq major-mode 'org-mode)
  5280. (error "Agenda file %s is not in `org-mode'" file))
  5281. (setq org-category-table (org-get-category-table))
  5282. (let ((case-fold-search nil))
  5283. (save-excursion
  5284. (save-restriction
  5285. (if org-respect-restriction
  5286. (if (org-region-active-p)
  5287. ;; Respect a region to restrict search
  5288. (narrow-to-region (region-beginning) (region-end)))
  5289. ;; If we work for the calendar or many files,
  5290. ;; get rid of any restriction
  5291. (widen))
  5292. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  5293. (while (setq arg (pop args))
  5294. (cond
  5295. ((and (eq arg :todo)
  5296. (equal date (calendar-current-date)))
  5297. (setq rtn (org-agenda-get-todos))
  5298. (setq results (append results rtn)))
  5299. ((eq arg :timestamp)
  5300. (setq rtn (org-agenda-get-blocks))
  5301. (setq results (append results rtn))
  5302. (setq rtn (org-agenda-get-timestamps))
  5303. (setq results (append results rtn)))
  5304. ((eq arg :scheduled)
  5305. (setq rtn (org-agenda-get-scheduled))
  5306. (setq results (append results rtn)))
  5307. ((eq arg :closed)
  5308. (setq rtn (org-agenda-get-closed))
  5309. (setq results (append results rtn)))
  5310. ((and (eq arg :deadline)
  5311. (equal date (calendar-current-date)))
  5312. (setq rtn (org-agenda-get-deadlines))
  5313. (setq results (append results rtn))))))))
  5314. results))))
  5315. (defun org-entry-is-done-p ()
  5316. "Is the current entry marked DONE?"
  5317. (save-excursion
  5318. (and (re-search-backward "[\r\n]\\*" nil t)
  5319. (looking-at org-nl-done-regexp))))
  5320. (defun org-at-date-range-p ()
  5321. "Is the cursor inside a date range?"
  5322. (interactive)
  5323. (save-excursion
  5324. (catch 'exit
  5325. (let ((pos (point)))
  5326. (skip-chars-backward "^<\r\n")
  5327. (skip-chars-backward "<")
  5328. (and (looking-at org-tr-regexp)
  5329. (>= (match-end 0) pos)
  5330. (throw 'exit t))
  5331. (skip-chars-backward "^<\r\n")
  5332. (skip-chars-backward "<")
  5333. (and (looking-at org-tr-regexp)
  5334. (>= (match-end 0) pos)
  5335. (throw 'exit t)))
  5336. nil)))
  5337. (defun org-agenda-get-todos ()
  5338. "Return the TODO information for agenda display."
  5339. (let* ((props (list 'face nil
  5340. 'done-face 'org-done
  5341. 'mouse-face 'highlight
  5342. 'keymap org-agenda-keymap
  5343. 'help-echo
  5344. (format "mouse-2 or RET jump to org file %s"
  5345. (abbreviate-file-name buffer-file-name))))
  5346. (regexp (concat "[\n\r]\\*+ *\\("
  5347. (if org-select-this-todo-keyword
  5348. (concat "\\<\\(" org-select-this-todo-keyword
  5349. "\\)\\>")
  5350. org-not-done-regexp)
  5351. "[^\n\r]*\\)"))
  5352. marker priority category tags
  5353. ee txt)
  5354. (goto-char (point-min))
  5355. (while (re-search-forward regexp nil t)
  5356. (goto-char (match-beginning 1))
  5357. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  5358. category (org-get-category)
  5359. tags (org-get-tags-at (point))
  5360. txt (org-format-agenda-item "" (match-string 1) category tags)
  5361. priority
  5362. (+ (org-get-priority txt)
  5363. (if org-todo-kwd-priority-p
  5364. (- org-todo-kwd-max-priority -2
  5365. (length
  5366. (member (match-string 2) org-todo-keywords)))
  5367. 1)))
  5368. (add-text-properties
  5369. 0 (length txt) (append (list 'org-marker marker 'org-hd-marker marker
  5370. 'priority priority 'category category)
  5371. props)
  5372. txt)
  5373. (push txt ee)
  5374. (goto-char (match-end 1)))
  5375. (nreverse ee)))
  5376. (defconst org-agenda-no-heading-message
  5377. "No heading for this item in buffer or region.")
  5378. (defun org-agenda-get-timestamps ()
  5379. "Return the date stamp information for agenda display."
  5380. (let* ((props (list 'face nil
  5381. 'mouse-face 'highlight
  5382. 'keymap org-agenda-keymap
  5383. 'help-echo
  5384. (format "mouse-2 or RET jump to org file %s"
  5385. (abbreviate-file-name buffer-file-name))))
  5386. (regexp (regexp-quote
  5387. (substring
  5388. (format-time-string
  5389. (car org-time-stamp-formats)
  5390. (apply 'encode-time ; DATE bound by calendar
  5391. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  5392. 0 11)))
  5393. marker hdmarker deadlinep scheduledp donep tmp priority category
  5394. ee txt timestr tags)
  5395. (goto-char (point-min))
  5396. (while (re-search-forward regexp nil t)
  5397. (if (not (save-match-data (org-at-date-range-p)))
  5398. (progn
  5399. (setq marker (org-agenda-new-marker (match-beginning 0))
  5400. category (org-get-category (match-beginning 0))
  5401. tmp (buffer-substring (max (point-min)
  5402. (- (match-beginning 0)
  5403. org-ds-keyword-length))
  5404. (match-beginning 0))
  5405. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  5406. deadlinep (string-match org-deadline-regexp tmp)
  5407. scheduledp (string-match org-scheduled-regexp tmp)
  5408. donep (org-entry-is-done-p))
  5409. (if (string-match ">" timestr)
  5410. ;; substring should only run to end of time stamp
  5411. (setq timestr (substring timestr 0 (match-end 0))))
  5412. (save-excursion
  5413. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  5414. (progn
  5415. (goto-char (match-end 1))
  5416. (setq hdmarker (org-agenda-new-marker)
  5417. tags (org-get-tags-at))
  5418. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  5419. (setq txt (org-format-agenda-item
  5420. (format "%s%s"
  5421. (if deadlinep "Deadline: " "")
  5422. (if scheduledp "Scheduled: " ""))
  5423. (match-string 1) category tags timestr)))
  5424. (setq txt org-agenda-no-heading-message))
  5425. (setq priority (org-get-priority txt))
  5426. (add-text-properties
  5427. 0 (length txt) (append (list 'org-marker marker
  5428. 'org-hd-marker hdmarker) props)
  5429. txt)
  5430. (if deadlinep
  5431. (add-text-properties
  5432. 0 (length txt)
  5433. (list 'face
  5434. (if donep 'org-done 'org-warning)
  5435. 'undone-face 'org-warning
  5436. 'done-face 'org-done
  5437. 'category category
  5438. 'priority (+ 100 priority))
  5439. txt)
  5440. (if scheduledp
  5441. (add-text-properties
  5442. 0 (length txt)
  5443. (list 'face 'org-scheduled-today
  5444. 'undone-face 'org-scheduled-today
  5445. 'done-face 'org-done
  5446. 'category category
  5447. priority (+ 99 priority))
  5448. txt)
  5449. (add-text-properties
  5450. 0 (length txt)
  5451. (list 'priority priority 'category category) txt)))
  5452. (push txt ee))
  5453. (outline-next-heading))))
  5454. (nreverse ee)))
  5455. (defun org-agenda-get-closed ()
  5456. "Return the logged TODO entries for agenda display."
  5457. (let* ((props (list 'mouse-face 'highlight
  5458. 'keymap org-agenda-keymap
  5459. 'help-echo
  5460. (format "mouse-2 or RET jump to org file %s"
  5461. (abbreviate-file-name buffer-file-name))))
  5462. (regexp (concat
  5463. "\\<" org-closed-string " *\\["
  5464. (regexp-quote
  5465. (substring
  5466. (format-time-string
  5467. (car org-time-stamp-formats)
  5468. (apply 'encode-time ; DATE bound by calendar
  5469. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  5470. 1 11))))
  5471. marker hdmarker priority category tags
  5472. ee txt timestr)
  5473. (goto-char (point-min))
  5474. (while (re-search-forward regexp nil t)
  5475. (if (not (save-match-data (org-at-date-range-p)))
  5476. (progn
  5477. (setq marker (org-agenda-new-marker (match-beginning 0))
  5478. category (org-get-category (match-beginning 0))
  5479. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  5480. ;; donep (org-entry-is-done-p)
  5481. )
  5482. (if (string-match "\\]" timestr)
  5483. ;; substring should only run to end of time stamp
  5484. (setq timestr (substring timestr 0 (match-end 0))))
  5485. (save-excursion
  5486. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  5487. (progn
  5488. (goto-char (match-end 1))
  5489. (setq hdmarker (org-agenda-new-marker)
  5490. tags (org-get-tags-at))
  5491. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  5492. (setq txt (org-format-agenda-item
  5493. "Closed: "
  5494. (match-string 1) category tags timestr)))
  5495. (setq txt org-agenda-no-heading-message))
  5496. (setq priority 100000)
  5497. (add-text-properties
  5498. 0 (length txt) (append (list 'org-marker marker
  5499. 'org-hd-marker hdmarker
  5500. 'face 'org-done
  5501. 'priority priority
  5502. 'category category
  5503. 'undone-face 'org-warning
  5504. 'done-face 'org-done) props)
  5505. txt)
  5506. (push txt ee))
  5507. (outline-next-heading))))
  5508. (nreverse ee)))
  5509. (defun org-agenda-get-deadlines ()
  5510. "Return the deadline information for agenda display."
  5511. (let* ((wdays org-deadline-warning-days)
  5512. (props (list 'mouse-face 'highlight
  5513. 'keymap org-agenda-keymap
  5514. 'help-echo
  5515. (format "mouse-2 or RET jump to org file %s"
  5516. (abbreviate-file-name buffer-file-name))))
  5517. (regexp org-deadline-time-regexp)
  5518. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  5519. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  5520. d2 diff pos pos1 category tags
  5521. ee txt head)
  5522. (goto-char (point-min))
  5523. (while (re-search-forward regexp nil t)
  5524. (setq pos (1- (match-beginning 1))
  5525. d2 (time-to-days
  5526. (org-time-string-to-time (match-string 1)))
  5527. diff (- d2 d1))
  5528. ;; When to show a deadline in the calendar:
  5529. ;; If the expiration is within wdays warning time.
  5530. ;; Past-due deadlines are only shown on the current date
  5531. (if (and (< diff wdays) todayp (not (= diff 0)))
  5532. (save-excursion
  5533. (setq category (org-get-category))
  5534. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  5535. (progn
  5536. (goto-char (match-end 0))
  5537. (setq pos1 (match-end 1))
  5538. (setq tags (org-get-tags-at pos1))
  5539. (setq head (buffer-substring-no-properties
  5540. (point)
  5541. (progn (skip-chars-forward "^\r\n")
  5542. (point))))
  5543. (if (string-match org-looking-at-done-regexp head)
  5544. (setq txt nil)
  5545. (setq txt (org-format-agenda-item
  5546. (format "In %3d d.: " diff) head category tags))))
  5547. (setq txt org-agenda-no-heading-message))
  5548. (when txt
  5549. (add-text-properties
  5550. 0 (length txt)
  5551. (append
  5552. (list 'org-marker (org-agenda-new-marker pos)
  5553. 'org-hd-marker (org-agenda-new-marker pos1)
  5554. 'priority (+ (- 10 diff) (org-get-priority txt))
  5555. 'category category
  5556. 'face (cond ((<= diff 0) 'org-warning)
  5557. ((<= diff 5) 'org-scheduled-previously)
  5558. (t nil))
  5559. 'undone-face (cond
  5560. ((<= diff 0) 'org-warning)
  5561. ((<= diff 5) 'org-scheduled-previously)
  5562. (t nil))
  5563. 'done-face 'org-done)
  5564. props)
  5565. txt)
  5566. (push txt ee)))))
  5567. ee))
  5568. (defun org-agenda-get-scheduled ()
  5569. "Return the scheduled information for agenda display."
  5570. (let* ((props (list 'face 'org-scheduled-previously
  5571. 'undone-face 'org-scheduled-previously
  5572. 'done-face 'org-done
  5573. 'mouse-face 'highlight
  5574. 'keymap org-agenda-keymap
  5575. 'help-echo
  5576. (format "mouse-2 or RET jump to org file %s"
  5577. (abbreviate-file-name buffer-file-name))))
  5578. (regexp org-scheduled-time-regexp)
  5579. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  5580. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  5581. d2 diff pos pos1 category tags
  5582. ee txt head)
  5583. (goto-char (point-min))
  5584. (while (re-search-forward regexp nil t)
  5585. (setq pos (1- (match-beginning 1))
  5586. d2 (time-to-days
  5587. (org-time-string-to-time (match-string 1)))
  5588. diff (- d2 d1))
  5589. ;; When to show a scheduled item in the calendar:
  5590. ;; If it is on or past the date.
  5591. (if (and (< diff 0) todayp)
  5592. (save-excursion
  5593. (setq category (org-get-category))
  5594. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  5595. (progn
  5596. (goto-char (match-end 0))
  5597. (setq pos1 (match-end 1))
  5598. (setq tags (org-get-tags-at))
  5599. (setq head (buffer-substring-no-properties
  5600. (point)
  5601. (progn (skip-chars-forward "^\r\n") (point))))
  5602. (if (string-match org-looking-at-done-regexp head)
  5603. (setq txt nil)
  5604. (setq txt (org-format-agenda-item
  5605. (format "Sched.%2dx: " (- 1 diff)) head
  5606. category tags))))
  5607. (setq txt org-agenda-no-heading-message))
  5608. (when txt
  5609. (add-text-properties
  5610. 0 (length txt)
  5611. (append (list 'org-marker (org-agenda-new-marker pos)
  5612. 'org-hd-marker (org-agenda-new-marker pos1)
  5613. 'priority (+ (- 5 diff) (org-get-priority txt))
  5614. 'category category)
  5615. props) txt)
  5616. (push txt ee)))))
  5617. ee))
  5618. (defun org-agenda-get-blocks ()
  5619. "Return the date-range information for agenda display."
  5620. (let* ((props (list 'face nil
  5621. 'mouse-face 'highlight
  5622. 'keymap org-agenda-keymap
  5623. 'help-echo
  5624. (format "mouse-2 or RET jump to org file %s"
  5625. (abbreviate-file-name buffer-file-name))))
  5626. (regexp org-tr-regexp)
  5627. (d0 (calendar-absolute-from-gregorian date))
  5628. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags)
  5629. (goto-char (point-min))
  5630. (while (re-search-forward regexp nil t)
  5631. (setq timestr (match-string 0)
  5632. s1 (match-string 1)
  5633. s2 (match-string 2)
  5634. d1 (time-to-days (org-time-string-to-time s1))
  5635. d2 (time-to-days (org-time-string-to-time s2)))
  5636. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  5637. ;; Only allow days between the limits, because the normal
  5638. ;; date stamps will catch the limits.
  5639. (save-excursion
  5640. (setq marker (org-agenda-new-marker (point)))
  5641. (setq category (org-get-category))
  5642. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  5643. (progn
  5644. (setq hdmarker (org-agenda-new-marker (match-end 1)))
  5645. (goto-char (match-end 1))
  5646. (setq tags (org-get-tags-at))
  5647. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  5648. (setq txt (org-format-agenda-item
  5649. (format (if (= d1 d2) "" "(%d/%d): ")
  5650. (1+ (- d0 d1)) (1+ (- d2 d1)))
  5651. (match-string 1) category tags
  5652. (if (= d0 d1) timestr))))
  5653. (setq txt org-agenda-no-heading-message))
  5654. (add-text-properties
  5655. 0 (length txt) (append (list 'org-marker marker
  5656. 'org-hd-marker hdmarker
  5657. 'priority (org-get-priority txt)
  5658. 'category category)
  5659. props)
  5660. txt)
  5661. (push txt ee)))
  5662. (outline-next-heading))
  5663. ;; Sort the entries by expiration date.
  5664. (nreverse ee)))
  5665. (defconst org-plain-time-of-day-regexp
  5666. (concat
  5667. "\\(\\<[012]?[0-9]"
  5668. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  5669. "\\(--?"
  5670. "\\(\\<[012]?[0-9]"
  5671. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  5672. "\\)?")
  5673. "Regular expression to match a plain time or time range.
  5674. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  5675. groups carry important information:
  5676. 0 the full match
  5677. 1 the first time, range or not
  5678. 8 the second time, if it is a range.")
  5679. (defconst org-stamp-time-of-day-regexp
  5680. (concat
  5681. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +[a-zA-Z]+ +\\)"
  5682. "\\([012][0-9]:[0-5][0-9]\\)>"
  5683. "\\(--?"
  5684. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  5685. "Regular expression to match a timestamp time or time range.
  5686. After a match, the following groups carry important information:
  5687. 0 the full match
  5688. 1 date plus weekday, for backreferencing to make sure both times on same day
  5689. 2 the first time, range or not
  5690. 4 the second time, if it is a range.")
  5691. (defvar org-prefix-has-time nil
  5692. "A flag, set by `org-compile-prefix-format'.
  5693. The flag is set if the currently compiled format contains a `%t'.")
  5694. (defvar org-prefix-has-tag nil
  5695. "A flag, set by `org-compile-prefix-format'.
  5696. The flag is set if the currently compiled format contains a `%T'.")
  5697. (defun org-format-agenda-item (extra txt &optional category tags dotime noprefix)
  5698. "Format TXT to be inserted into the agenda buffer.
  5699. In particular, it adds the prefix and corresponding text properties. EXTRA
  5700. must be a string and replaces the `%s' specifier in the prefix format.
  5701. CATEGORY (string, symbol or nil) may be used to overrule the default
  5702. category taken from local variable or file name. It will replace the `%c'
  5703. specifier in the format. DOTIME, when non-nil, indicates that a
  5704. time-of-day should be extracted from TXT for sorting of this entry, and for
  5705. the `%t' specifier in the format. When DOTIME is a string, this string is
  5706. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  5707. only the correctly processes TXT should be returned - this is used by
  5708. `org-agenda-change-all-lines'. TAG can be the tag of the headline."
  5709. (save-match-data
  5710. ;; Diary entries sometimes have extra whitespace at the beginning
  5711. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  5712. (let* ((category (or category
  5713. org-category
  5714. (if buffer-file-name
  5715. (file-name-sans-extension
  5716. (file-name-nondirectory buffer-file-name))
  5717. "")))
  5718. (tag (or (nth (1- (or (length tags) 0)) tags) ""))
  5719. time ;; needed for the eval of the prefix format
  5720. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  5721. (time-of-day (and dotime (org-get-time-of-day ts)))
  5722. stamp plain s0 s1 s2 rtn)
  5723. (when (and dotime time-of-day org-prefix-has-time)
  5724. ;; Extract starting and ending time and move them to prefix
  5725. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  5726. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  5727. (setq s0 (match-string 0 ts)
  5728. s1 (match-string (if plain 1 2) ts)
  5729. s2 (match-string (if plain 8 4) ts))
  5730. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  5731. ;; them, we might want to remove them there to avoid duplication.
  5732. ;; The user can turn this off with a variable.
  5733. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  5734. (string-match (concat (regexp-quote s0) " *") txt)
  5735. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  5736. (= (match-beginning 0) 0)
  5737. t))
  5738. (setq txt (replace-match "" nil nil txt))))
  5739. ;; Normalize the time(s) to 24 hour
  5740. (if s1 (setq s1 (org-get-time-of-day s1 'string)))
  5741. (if s2 (setq s2 (org-get-time-of-day s2 'string))))
  5742. (when (and (or (eq org-agenda-remove-tags-when-in-prefix t)
  5743. (and org-agenda-remove-tags-when-in-prefix
  5744. org-prefix-has-tag))
  5745. (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" txt))
  5746. (setq txt (replace-match "" t t txt)))
  5747. ;; Create the final string
  5748. (if noprefix
  5749. (setq rtn txt)
  5750. ;; Prepare the variables needed in the eval of the compiled format
  5751. (setq time (cond (s2 (concat s1 "-" s2))
  5752. (s1 (concat s1 "......"))
  5753. (t ""))
  5754. extra (or extra "")
  5755. category (if (symbolp category) (symbol-name category) category))
  5756. ;; Evaluate the compiled format
  5757. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  5758. ;; And finally add the text properties
  5759. (add-text-properties
  5760. 0 (length rtn) (list 'category (downcase category)
  5761. 'tags tags
  5762. 'prefix-length (- (length rtn) (length txt))
  5763. 'time-of-day time-of-day
  5764. 'dotime dotime)
  5765. rtn)
  5766. rtn)))
  5767. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  5768. (catch 'exit
  5769. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  5770. ((and todayp (member 'today (car org-agenda-time-grid))))
  5771. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  5772. ((member 'weekly (car org-agenda-time-grid)))
  5773. (t (throw 'exit list)))
  5774. (let* ((have (delq nil (mapcar
  5775. (lambda (x) (get-text-property 1 'time-of-day x))
  5776. list)))
  5777. (string (nth 1 org-agenda-time-grid))
  5778. (gridtimes (nth 2 org-agenda-time-grid))
  5779. (req (car org-agenda-time-grid))
  5780. (remove (member 'remove-match req))
  5781. new time)
  5782. (if (and (member 'require-timed req) (not have))
  5783. ;; don't show empty grid
  5784. (throw 'exit list))
  5785. (while (setq time (pop gridtimes))
  5786. (unless (and remove (member time have))
  5787. (setq time (int-to-string time))
  5788. (push (org-format-agenda-item
  5789. nil string "" nil
  5790. (concat (substring time 0 -2) ":" (substring time -2)))
  5791. new)
  5792. (put-text-property
  5793. 1 (length (car new)) 'face 'org-time-grid (car new))))
  5794. (if (member 'time-up org-agenda-sorting-strategy)
  5795. (append new list)
  5796. (append list new)))))
  5797. (defun org-compile-prefix-format (format)
  5798. "Compile the prefix format into a Lisp form that can be evaluated.
  5799. The resulting form is returned and stored in the variable
  5800. `org-prefix-format-compiled'."
  5801. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  5802. (let ((start 0) varform vars var (s format)e c f opt)
  5803. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  5804. s start)
  5805. (setq var (cdr (assoc (match-string 4 s)
  5806. '(("c" . category) ("t" . time) ("s" . extra)
  5807. ("T" . tag))))
  5808. c (or (match-string 3 s) "")
  5809. opt (match-beginning 1)
  5810. start (1+ (match-beginning 0)))
  5811. (if (equal var 'time) (setq org-prefix-has-time t))
  5812. (if (equal var 'tag) (setq org-prefix-has-tag t))
  5813. (setq f (concat "%" (match-string 2 s) "s"))
  5814. (if opt
  5815. (setq varform
  5816. `(if (equal "" ,var)
  5817. ""
  5818. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  5819. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  5820. (setq s (replace-match "%s" t nil s))
  5821. (push varform vars))
  5822. (setq vars (nreverse vars))
  5823. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  5824. (defun org-get-time-of-day (s &optional string)
  5825. "Check string S for a time of day.
  5826. If found, return it as a military time number between 0 and 2400.
  5827. If not found, return nil.
  5828. The optional STRING argument forces conversion into a 5 character wide string
  5829. HH:MM."
  5830. (save-match-data
  5831. (when
  5832. (or
  5833. (string-match
  5834. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  5835. (string-match
  5836. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  5837. (let* ((t0 (+ (* 100
  5838. (+ (string-to-number (match-string 1 s))
  5839. (if (and (match-beginning 4)
  5840. (equal (downcase (match-string 4 s)) "pm"))
  5841. 12 0)))
  5842. (if (match-beginning 3)
  5843. (string-to-number (match-string 3 s))
  5844. 0)))
  5845. (t1 (concat " "
  5846. (if (< t0 100) "0" "") (if (< t0 10) "0" "")
  5847. (int-to-string t0))))
  5848. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  5849. (defun org-finalize-agenda-entries (list)
  5850. "Sort and concatenate the agenda items."
  5851. (mapconcat 'identity (sort list 'org-entries-lessp) "\n"))
  5852. (defsubst org-cmp-priority (a b)
  5853. "Compare the priorities of string A and B."
  5854. (let ((pa (or (get-text-property 1 'priority a) 0))
  5855. (pb (or (get-text-property 1 'priority b) 0)))
  5856. (cond ((> pa pb) +1)
  5857. ((< pa pb) -1)
  5858. (t nil))))
  5859. (defsubst org-cmp-category (a b)
  5860. "Compare the string values of categories of strings A and B."
  5861. (let ((ca (or (get-text-property 1 'category a) ""))
  5862. (cb (or (get-text-property 1 'category b) "")))
  5863. (cond ((string-lessp ca cb) -1)
  5864. ((string-lessp cb ca) +1)
  5865. (t nil))))
  5866. (defsubst org-cmp-time (a b)
  5867. "Compare the time-of-day values of strings A and B."
  5868. (let* ((def (if org-sort-agenda-notime-is-late 2401 -1))
  5869. (ta (or (get-text-property 1 'time-of-day a) def))
  5870. (tb (or (get-text-property 1 'time-of-day b) def)))
  5871. (cond ((< ta tb) -1)
  5872. ((< tb ta) +1)
  5873. (t nil))))
  5874. (defun org-entries-lessp (a b)
  5875. "Predicate for sorting agenda entries."
  5876. ;; The following variables will be used when the form is evaluated.
  5877. (let* ((time-up (org-cmp-time a b))
  5878. (time-down (if time-up (- time-up) nil))
  5879. (priority-up (org-cmp-priority a b))
  5880. (priority-down (if priority-up (- priority-up) nil))
  5881. (category-up (org-cmp-category a b))
  5882. (category-down (if category-up (- category-up) nil))
  5883. (category-keep (if category-up +1 nil)))
  5884. (cdr (assoc
  5885. (eval (cons 'or org-agenda-sorting-strategy))
  5886. '((-1 . t) (1 . nil) (nil . nil))))))
  5887. (defun org-agenda-show-priority ()
  5888. "Show the priority of the current item.
  5889. This priority is composed of the main priority given with the [#A] cookies,
  5890. and by additional input from the age of a schedules or deadline entry."
  5891. (interactive)
  5892. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  5893. (message "Priority is %d" (if pri pri -1000))))
  5894. (defun org-agenda-show-tags ()
  5895. "Show the tags applicable to the current item."
  5896. (interactive)
  5897. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  5898. (if tags
  5899. (message "Tags are :%s:" (mapconcat 'identity tags ":"))
  5900. (message "No tags associated with this line"))))
  5901. (defun org-agenda-goto (&optional highlight)
  5902. "Go to the Org-mode file which contains the item at point."
  5903. (interactive)
  5904. (let* ((marker (or (get-text-property (point) 'org-marker)
  5905. (org-agenda-error)))
  5906. (buffer (marker-buffer marker))
  5907. (pos (marker-position marker)))
  5908. (switch-to-buffer-other-window buffer)
  5909. (widen)
  5910. (goto-char pos)
  5911. (when (eq major-mode 'org-mode)
  5912. (org-show-hidden-entry)
  5913. (save-excursion
  5914. (and (outline-next-heading)
  5915. (org-flag-heading nil)))) ; show the next heading
  5916. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  5917. (defun org-agenda-switch-to ()
  5918. "Go to the Org-mode file which contains the item at point."
  5919. (interactive)
  5920. (let* ((marker (or (get-text-property (point) 'org-marker)
  5921. (org-agenda-error)))
  5922. (buffer (marker-buffer marker))
  5923. (pos (marker-position marker)))
  5924. (switch-to-buffer buffer)
  5925. (delete-other-windows)
  5926. (widen)
  5927. (goto-char pos)
  5928. (when (eq major-mode 'org-mode)
  5929. (org-show-hidden-entry)
  5930. (save-excursion
  5931. (and (outline-next-heading)
  5932. (org-flag-heading nil)))))) ; show the next heading
  5933. (defun org-agenda-goto-mouse (ev)
  5934. "Go to the Org-mode file which contains the item at the mouse click."
  5935. (interactive "e")
  5936. (mouse-set-point ev)
  5937. (org-agenda-goto))
  5938. (defun org-agenda-show ()
  5939. "Display the Org-mode file which contains the item at point."
  5940. (interactive)
  5941. (let ((win (selected-window)))
  5942. (org-agenda-goto t)
  5943. (select-window win)))
  5944. (defun org-agenda-recenter (arg)
  5945. "Display the Org-mode file which contains the item at point and recenter."
  5946. (interactive "P")
  5947. (let ((win (selected-window)))
  5948. (org-agenda-goto t)
  5949. (recenter arg)
  5950. (select-window win)))
  5951. (defun org-agenda-show-mouse (ev)
  5952. "Display the Org-mode file which contains the item at the mouse click."
  5953. (interactive "e")
  5954. (mouse-set-point ev)
  5955. (org-agenda-show))
  5956. (defun org-agenda-check-no-diary ()
  5957. "Check if the entry is a diary link and abort if yes."
  5958. (if (get-text-property (point) 'org-agenda-diary-link)
  5959. (org-agenda-error)))
  5960. (defun org-agenda-error ()
  5961. (error "Command not allowed in this line"))
  5962. (defvar org-last-heading-marker (make-marker)
  5963. "Marker pointing to the headline that last changed its TODO state
  5964. by a remote command from the agenda.")
  5965. (defun org-agenda-todo (&optional arg)
  5966. "Cycle TODO state of line at point, also in Org-mode file.
  5967. This changes the line at point, all other lines in the agenda referring to
  5968. the same tree node, and the headline of the tree node in the Org-mode file."
  5969. (interactive "P")
  5970. (org-agenda-check-no-diary)
  5971. (let* ((col (current-column))
  5972. (marker (or (get-text-property (point) 'org-marker)
  5973. (org-agenda-error)))
  5974. (buffer (marker-buffer marker))
  5975. (pos (marker-position marker))
  5976. (hdmarker (get-text-property (point) 'org-hd-marker))
  5977. (buffer-read-only nil)
  5978. newhead)
  5979. (with-current-buffer buffer
  5980. (widen)
  5981. (goto-char pos)
  5982. (org-show-hidden-entry)
  5983. (save-excursion
  5984. (and (outline-next-heading)
  5985. (org-flag-heading nil))) ; show the next heading
  5986. (org-todo arg)
  5987. (forward-char 1)
  5988. (setq newhead (org-get-heading))
  5989. (save-excursion
  5990. (org-back-to-heading)
  5991. (move-marker org-last-heading-marker (point))))
  5992. (beginning-of-line 1)
  5993. (save-excursion
  5994. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  5995. (move-to-column col)))
  5996. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  5997. "Change all lines in the agenda buffer which match HDMARKER.
  5998. The new content of the line will be NEWHEAD (as modified by
  5999. `org-format-agenda-item'). HDMARKER is checked with
  6000. `equal' against all `org-hd-marker' text properties in the file.
  6001. If FIXFACE is non-nil, the face of each item is modified acording to
  6002. the new TODO state."
  6003. (let* (props m pl undone-face done-face finish new dotime cat tags)
  6004. ; (setq newhead (org-format-agenda-item "x" newhead "x" nil 'noprefix))
  6005. (save-excursion
  6006. (goto-char (point-max))
  6007. (beginning-of-line 1)
  6008. (while (not finish)
  6009. (setq finish (bobp))
  6010. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  6011. (equal m hdmarker))
  6012. (setq props (text-properties-at (point))
  6013. dotime (get-text-property (point) 'dotime)
  6014. cat (get-text-property (point) 'category)
  6015. tags (get-text-property (point) 'tags)
  6016. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  6017. pl (get-text-property (point) 'prefix-length)
  6018. undone-face (get-text-property (point) 'undone-face)
  6019. done-face (get-text-property (point) 'done-face))
  6020. (move-to-column pl)
  6021. (if (looking-at ".*")
  6022. (progn
  6023. (replace-match new t t)
  6024. (beginning-of-line 1)
  6025. (add-text-properties (point-at-bol) (point-at-eol) props)
  6026. (if fixface
  6027. (add-text-properties
  6028. (point-at-bol) (point-at-eol)
  6029. (list 'face
  6030. (if org-last-todo-state-is-todo
  6031. undone-face done-face))))
  6032. (beginning-of-line 1))
  6033. (error "Line update did not work")))
  6034. (beginning-of-line 0)))))
  6035. (defun org-agenda-priority-up ()
  6036. "Increase the priority of line at point, also in Org-mode file."
  6037. (interactive)
  6038. (org-agenda-priority 'up))
  6039. (defun org-agenda-priority-down ()
  6040. "Decrease the priority of line at point, also in Org-mode file."
  6041. (interactive)
  6042. (org-agenda-priority 'down))
  6043. (defun org-agenda-priority (&optional force-direction)
  6044. "Set the priority of line at point, also in Org-mode file.
  6045. This changes the line at point, all other lines in the agenda referring to
  6046. the same tree node, and the headline of the tree node in the Org-mode file."
  6047. (interactive)
  6048. (org-agenda-check-no-diary)
  6049. (let* ((marker (or (get-text-property (point) 'org-marker)
  6050. (org-agenda-error)))
  6051. (buffer (marker-buffer marker))
  6052. (pos (marker-position marker))
  6053. (hdmarker (get-text-property (point) 'org-hd-marker))
  6054. (buffer-read-only nil)
  6055. newhead)
  6056. (with-current-buffer buffer
  6057. (widen)
  6058. (goto-char pos)
  6059. (org-show-hidden-entry)
  6060. (save-excursion
  6061. (and (outline-next-heading)
  6062. (org-flag-heading nil))) ; show the next heading
  6063. (funcall 'org-priority force-direction)
  6064. (end-of-line 1)
  6065. (setq newhead (org-get-heading)))
  6066. (org-agenda-change-all-lines newhead hdmarker)
  6067. (beginning-of-line 1)))
  6068. (defun org-get-tags-at (&optional pos)
  6069. "Get a list of all headline targs applicable at POS.
  6070. POS defaults to point. If tags are inherited, the list contains
  6071. the targets in the same sequence as the headlines appear, i.e.
  6072. the tags of the current headline come last."
  6073. (interactive)
  6074. (let (tags)
  6075. (save-excursion
  6076. (goto-char (or pos (point)))
  6077. (save-match-data
  6078. (org-back-to-heading t)
  6079. (condition-case nil
  6080. (while t
  6081. (if (looking-at "[^\r\n]+?:\\([a-zA-Z_@0-9:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
  6082. (setq tags (append (org-split-string (match-string 1) ":") tags)))
  6083. (or org-use-tag-inheritance (error ""))
  6084. (org-up-heading-all 1))
  6085. (error nil))))
  6086. (message "%s" tags)
  6087. tags))
  6088. (defun org-agenda-set-tags ()
  6089. "Set tags for the current headline."
  6090. (interactive)
  6091. (org-agenda-check-no-diary)
  6092. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  6093. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  6094. (org-agenda-error)))
  6095. (buffer (marker-buffer hdmarker))
  6096. (pos (marker-position hdmarker))
  6097. (buffer-read-only nil)
  6098. newhead)
  6099. (with-current-buffer buffer
  6100. (widen)
  6101. (goto-char pos)
  6102. (org-show-hidden-entry)
  6103. (save-excursion
  6104. (and (outline-next-heading)
  6105. (org-flag-heading nil))) ; show the next heading
  6106. (call-interactively 'org-set-tags)
  6107. (end-of-line 1)
  6108. (setq newhead (org-get-heading)))
  6109. (org-agenda-change-all-lines newhead hdmarker)
  6110. (beginning-of-line 1)))
  6111. (defun org-agenda-date-later (arg &optional what)
  6112. "Change the date of this item to one day later."
  6113. (interactive "p")
  6114. (org-agenda-check-type t 'agenda 'timeline)
  6115. (org-agenda-check-no-diary)
  6116. (let* ((marker (or (get-text-property (point) 'org-marker)
  6117. (org-agenda-error)))
  6118. (buffer (marker-buffer marker))
  6119. (pos (marker-position marker)))
  6120. (with-current-buffer buffer
  6121. (widen)
  6122. (goto-char pos)
  6123. (if (not (org-at-timestamp-p))
  6124. (error "Cannot find time stamp"))
  6125. (org-timestamp-change arg (or what 'day))
  6126. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  6127. (defun org-agenda-date-earlier (arg &optional what)
  6128. "Change the date of this item to one day earlier."
  6129. (interactive "p")
  6130. (org-agenda-date-later (- arg) what))
  6131. (defun org-agenda-date-prompt (arg)
  6132. "Change the date of this item. Date is prompted for, with default today.
  6133. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  6134. be used to request time specification in the time stamp."
  6135. (interactive "P")
  6136. (org-agenda-check-type t 'agenda 'timeline)
  6137. (org-agenda-check-no-diary)
  6138. (let* ((marker (or (get-text-property (point) 'org-marker)
  6139. (org-agenda-error)))
  6140. (buffer (marker-buffer marker))
  6141. (pos (marker-position marker)))
  6142. (with-current-buffer buffer
  6143. (widen)
  6144. (goto-char pos)
  6145. (if (not (org-at-timestamp-p))
  6146. (error "Cannot find time stamp"))
  6147. (org-time-stamp arg)
  6148. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  6149. (defun org-get-heading ()
  6150. "Return the heading of the current entry, without the stars."
  6151. (save-excursion
  6152. (and (memq (char-before) '(?\n ?\r)) (skip-chars-forward "^\n\r"))
  6153. (if (and (re-search-backward "[\r\n]\\*" nil t)
  6154. (looking-at "[\r\n]\\*+[ \t]+\\([^\r\n]*\\)"))
  6155. (match-string 1)
  6156. "")))
  6157. (defun org-agenda-diary-entry ()
  6158. "Make a diary entry, like the `i' command from the calendar.
  6159. All the standard commands work: block, weekly etc."
  6160. (interactive)
  6161. (org-agenda-check-type t 'agenda 'timeline)
  6162. (require 'diary-lib)
  6163. (let* ((char (progn
  6164. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  6165. (read-char-exclusive)))
  6166. (cmd (cdr (assoc char
  6167. '((?d . insert-diary-entry)
  6168. (?w . insert-weekly-diary-entry)
  6169. (?m . insert-monthly-diary-entry)
  6170. (?y . insert-yearly-diary-entry)
  6171. (?a . insert-anniversary-diary-entry)
  6172. (?b . insert-block-diary-entry)
  6173. (?c . insert-cyclic-diary-entry)))))
  6174. (oldf (symbol-function 'calendar-cursor-to-date))
  6175. (point (point))
  6176. (mark (or (mark t) (point))))
  6177. (unless cmd
  6178. (error "No command associated with <%c>" char))
  6179. (unless (and (get-text-property point 'day)
  6180. (or (not (equal ?b char))
  6181. (get-text-property mark 'day)))
  6182. (error "Don't know which date to use for diary entry"))
  6183. ;; We implement this by hacking the `calendar-cursor-to-date' function
  6184. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  6185. (let ((calendar-mark-ring
  6186. (list (calendar-gregorian-from-absolute
  6187. (or (get-text-property mark 'day)
  6188. (get-text-property point 'day))))))
  6189. (unwind-protect
  6190. (progn
  6191. (fset 'calendar-cursor-to-date
  6192. (lambda (&optional error)
  6193. (calendar-gregorian-from-absolute
  6194. (get-text-property point 'day))))
  6195. (call-interactively cmd))
  6196. (fset 'calendar-cursor-to-date oldf)))))
  6197. (defun org-agenda-execute-calendar-command (cmd)
  6198. "Execute a calendar command from the agenda, with the date associated to
  6199. the cursor position."
  6200. (org-agenda-check-type t 'agenda 'timeline)
  6201. (require 'diary-lib)
  6202. (unless (get-text-property (point) 'day)
  6203. (error "Don't know which date to use for calendar command"))
  6204. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  6205. (point (point))
  6206. (date (calendar-gregorian-from-absolute
  6207. (get-text-property point 'day)))
  6208. (displayed-day (extract-calendar-day date))
  6209. (displayed-month (extract-calendar-month date))
  6210. (displayed-year (extract-calendar-year date)))
  6211. (unwind-protect
  6212. (progn
  6213. (fset 'calendar-cursor-to-date
  6214. (lambda (&optional error)
  6215. (calendar-gregorian-from-absolute
  6216. (get-text-property point 'day))))
  6217. (call-interactively cmd))
  6218. (fset 'calendar-cursor-to-date oldf))))
  6219. (defun org-agenda-phases-of-moon ()
  6220. "Display the phases of the moon for the 3 months around the cursor date."
  6221. (interactive)
  6222. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  6223. (defun org-agenda-holidays ()
  6224. "Display the holidays for the 3 months around the cursor date."
  6225. (interactive)
  6226. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  6227. (defun org-agenda-sunrise-sunset (arg)
  6228. "Display sunrise and sunset for the cursor date.
  6229. Latitude and longitude can be specified with the variables
  6230. `calendar-latitude' and `calendar-longitude'. When called with prefix
  6231. argument, latitude and longitude will be prompted for."
  6232. (interactive "P")
  6233. (let ((calendar-longitude (if arg nil calendar-longitude))
  6234. (calendar-latitude (if arg nil calendar-latitude))
  6235. (calendar-location-name
  6236. (if arg "the given coordinates" calendar-location-name)))
  6237. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  6238. (defun org-agenda-goto-calendar ()
  6239. "Open the Emacs calendar with the date at the cursor."
  6240. (interactive)
  6241. (org-agenda-check-type t 'agenda 'timeline)
  6242. (let* ((day (or (get-text-property (point) 'day)
  6243. (error "Don't know which date to open in calendar")))
  6244. (date (calendar-gregorian-from-absolute day))
  6245. (calendar-move-hook nil)
  6246. (view-diary-entries-initially nil))
  6247. (calendar)
  6248. (calendar-goto-date date)))
  6249. (defun org-calendar-goto-agenda ()
  6250. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  6251. This is a command that has to be installed in `calendar-mode-map'."
  6252. (interactive)
  6253. (org-agenda-list nil (calendar-absolute-from-gregorian
  6254. (calendar-cursor-to-date))
  6255. nil t))
  6256. (defun org-agenda-convert-date ()
  6257. (interactive)
  6258. (org-agenda-check-type t 'agenda 'timeline)
  6259. (let ((day (get-text-property (point) 'day))
  6260. date s)
  6261. (unless day
  6262. (error "Don't know which date to convert"))
  6263. (setq date (calendar-gregorian-from-absolute day))
  6264. (setq s (concat
  6265. "Gregorian: " (calendar-date-string date) "\n"
  6266. "ISO: " (calendar-iso-date-string date) "\n"
  6267. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  6268. "Julian: " (calendar-julian-date-string date) "\n"
  6269. "Astron. JD: " (calendar-astro-date-string date)
  6270. " (Julian date number at noon UTC)\n"
  6271. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  6272. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  6273. "French: " (calendar-french-date-string date) "\n"
  6274. "Mayan: " (calendar-mayan-date-string date) "\n"
  6275. "Coptic: " (calendar-coptic-date-string date) "\n"
  6276. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  6277. "Persian: " (calendar-persian-date-string date) "\n"
  6278. "Chinese: " (calendar-chinese-date-string date) "\n"))
  6279. (with-output-to-temp-buffer "*Dates*"
  6280. (princ s))
  6281. (if (fboundp 'fit-window-to-buffer)
  6282. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  6283. ;;; Tags
  6284. (defun org-scan-tags (action matcher &optional todo-only)
  6285. "Scan headline tags with inheritance and produce output ACTION.
  6286. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  6287. evaluated, testing if a given set of tags qualifies a headline for
  6288. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  6289. are included in the output."
  6290. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  6291. (mapconcat 'regexp-quote
  6292. (nreverse (cdr (reverse org-todo-keywords)))
  6293. "\\|")
  6294. "\\>\\)\\)? *\\(.*?\\)\\(:[A-Za-z_@0-9:]+:\\)?[ \t]*[\n\r]"))
  6295. (props (list 'face nil
  6296. 'done-face 'org-done
  6297. 'undone-face nil
  6298. 'mouse-face 'highlight
  6299. 'keymap org-agenda-keymap
  6300. 'help-echo
  6301. (format "mouse-2 or RET jump to org file %s"
  6302. (abbreviate-file-name buffer-file-name))))
  6303. lspos
  6304. tags tags-list tags-alist (llast 0) rtn level category i txt
  6305. todo marker)
  6306. (save-excursion
  6307. (goto-char (point-min))
  6308. (when (eq action 'sparse-tree) (hide-sublevels 1))
  6309. (while (re-search-forward re nil t)
  6310. (setq todo (if (match-end 1) (match-string 2))
  6311. tags (if (match-end 4) (match-string 4)))
  6312. (goto-char (setq lspos (1+ (match-beginning 0))))
  6313. (setq level (funcall outline-level)
  6314. category (org-get-category))
  6315. (setq i llast llast level)
  6316. ;; remove tag lists from same and sublevels
  6317. (while (>= i level)
  6318. (when (setq entry (assoc i tags-alist))
  6319. (setq tags-alist (delete entry tags-alist)))
  6320. (setq i (1- i)))
  6321. ;; add the nex tags
  6322. (when tags
  6323. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  6324. tags-alist
  6325. (cons (cons level tags) tags-alist)))
  6326. ;; compile tags for current headline
  6327. (setq tags-list
  6328. (if org-use-tag-inheritance
  6329. (apply 'append (mapcar 'cdr tags-alist))
  6330. tags))
  6331. (when (and (or (not todo-only) todo)
  6332. (eval matcher))
  6333. ;; list this headline
  6334. (if (eq action 'sparse-tree)
  6335. (progn
  6336. (org-show-hierarchy-above))
  6337. (setq txt (org-format-agenda-item
  6338. ""
  6339. (concat
  6340. (if org-tags-match-list-sublevels
  6341. (make-string (1- level) ?.) "")
  6342. (org-get-heading))
  6343. category tags-list))
  6344. (goto-char lspos)
  6345. (setq marker (org-agenda-new-marker))
  6346. (add-text-properties
  6347. 0 (length txt)
  6348. (append (list 'org-marker marker 'org-hd-marker marker
  6349. 'category category)
  6350. props)
  6351. txt)
  6352. (push txt rtn))
  6353. ;; if we are to skip sublevels, jump to end of subtree
  6354. (point)
  6355. (or org-tags-match-list-sublevels (org-end-of-subtree)))))
  6356. (nreverse rtn)))
  6357. (defun org-tags-sparse-tree (&optional arg match)
  6358. "Create a sparse tree according to tags search string MATCH.
  6359. MATCH can contain positive and negative selection of tags, like
  6360. \"+WORK+URGENT-WITHBOSS\"."
  6361. (interactive "P")
  6362. (let ((org-show-following-heading nil)
  6363. (org-show-hierarchy-above nil))
  6364. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)))))
  6365. (defun org-make-tags-matcher (match)
  6366. "Create the TAGS matcher form for the tags-selecting string MATCH."
  6367. (unless match
  6368. ;; Get a new match request, with completion
  6369. (setq org-last-tags-completion-table
  6370. (or (org-get-buffer-tags)
  6371. org-last-tags-completion-table))
  6372. (setq match (completing-read
  6373. "Tags: " 'org-tags-completion-function nil nil nil
  6374. 'org-tags-history)))
  6375. ;; parse the string and create a lisp form
  6376. (let ((match0 match) minus tag mm matcher orterms term orlist)
  6377. (setq orterms (org-split-string match "|"))
  6378. (while (setq term (pop orterms))
  6379. (while (string-match "^&?\\([-+:]\\)?\\([A-Za-z_@0-9]+\\)" term)
  6380. (setq minus (and (match-end 1)
  6381. (equal (match-string 1 term) "-"))
  6382. tag (match-string 2 term)
  6383. term (substring term (match-end 0))
  6384. mm (list 'member (downcase tag) 'tags-list)
  6385. mm (if minus (list 'not mm) mm))
  6386. (push mm matcher))
  6387. (push (if (> (length matcher) 1) (cons 'and matcher) (car matcher))
  6388. orlist)
  6389. (setq matcher nil))
  6390. (setq matcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  6391. ;; Return the string and lisp forms of the matcher
  6392. (cons match0 matcher)))
  6393. ;;;###autoload
  6394. (defun org-tags-view (&optional todo-only match keep-modes)
  6395. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  6396. The prefix arg TODO-ONLY limits the search to TODO entries."
  6397. (interactive "P")
  6398. (org-agenda-maybe-reset-markers 'force)
  6399. (org-compile-prefix-format org-agenda-prefix-format)
  6400. (let* ((org-agenda-keep-modes keep-modes)
  6401. (org-tags-match-list-sublevels
  6402. (if todo-only t org-tags-match-list-sublevels))
  6403. (win (selected-window))
  6404. (completion-ignore-case t)
  6405. rtn rtnall files file pos matcher
  6406. buffer)
  6407. (setq matcher (org-make-tags-matcher match)
  6408. match (car matcher) matcher (cdr matcher))
  6409. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  6410. (progn
  6411. (delete-other-windows)
  6412. (switch-to-buffer-other-window
  6413. (get-buffer-create org-agenda-buffer-name))))
  6414. (setq buffer-read-only nil)
  6415. (erase-buffer)
  6416. (org-agenda-mode) (setq buffer-read-only nil)
  6417. (set (make-local-variable 'org-agenda-type) 'tags)
  6418. (set (make-local-variable 'org-agenda-redo-command)
  6419. (list 'org-tags-view (list 'quote todo-only)
  6420. (list 'if 'current-prefix-arg nil match) t))
  6421. (setq files (org-agenda-files)
  6422. rtnall nil)
  6423. (while (setq file (pop files))
  6424. (catch 'nextfile
  6425. (org-check-agenda-file file)
  6426. (setq buffer (if (file-exists-p file)
  6427. (org-get-agenda-file-buffer file)
  6428. (error "No such file %s" file)))
  6429. (if (not buffer)
  6430. ;; If file does not exist, merror message to agenda
  6431. (setq rtn (list
  6432. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  6433. rtnall (append rtnall rtn))
  6434. (with-current-buffer buffer
  6435. (unless (eq major-mode 'org-mode)
  6436. (error "Agenda file %s is not in `org-mode'" file))
  6437. (save-excursion
  6438. (save-restriction
  6439. (if org-respect-restriction
  6440. (if (org-region-active-p)
  6441. ;; Respect a region to restrict search
  6442. (narrow-to-region (region-beginning) (region-end)))
  6443. ;; If we work for the calendar or many files,
  6444. ;; get rid of any restriction
  6445. (widen))
  6446. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  6447. (setq rtnall (append rtnall rtn))))))))
  6448. (insert "Headlines with TAGS match: ")
  6449. (add-text-properties (point-min) (1- (point))
  6450. (list 'face 'org-link))
  6451. (setq pos (point))
  6452. (insert match "\n")
  6453. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  6454. (setq pos (point))
  6455. (insert "Press `C-u r' to search again with new search string\n")
  6456. (add-text-properties pos (1- (point)) (list 'face 'org-link))
  6457. (when rtnall
  6458. (insert (mapconcat 'identity rtnall "\n")))
  6459. (goto-char (point-min))
  6460. (setq buffer-read-only t)
  6461. (org-fit-agenda-window)
  6462. (if (not org-select-agenda-window) (select-window win))))
  6463. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  6464. (defun org-set-tags (&optional arg just-align)
  6465. "Set the tags for the current headline.
  6466. With prefix ARG, realign all tags in headings in the current buffer."
  6467. (interactive)
  6468. (let* (;(inherit (org-get-inherited-tags))
  6469. (re (concat "^" outline-regexp))
  6470. (col (current-column))
  6471. (current (org-get-tags))
  6472. tags hd empty invis)
  6473. (if arg
  6474. (save-excursion
  6475. (goto-char (point-min))
  6476. (while (re-search-forward re nil t)
  6477. (org-set-tags nil t))
  6478. (message "All tags realigned to column %d" org-tags-column))
  6479. (if just-align
  6480. (setq tags current)
  6481. (setq org-last-tags-completion-table
  6482. (or (org-get-buffer-tags)
  6483. org-last-tags-completion-table))
  6484. (setq tags
  6485. (let ((org-add-colon-after-tag-completion t))
  6486. (completing-read "Tags: " 'org-tags-completion-function
  6487. nil nil current 'org-tags-history)))
  6488. (while (string-match "[-+&]+" tags)
  6489. (setq tags (replace-match ":" t t tags))))
  6490. ;; FIXME: still optimize this by not checking when JUST-ALIGN?
  6491. (unless (setq empty (string-match "\\`[\t ]*\\'" tags))
  6492. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  6493. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  6494. (if (equal current "")
  6495. (progn
  6496. (end-of-line 1)
  6497. (or empty (insert-before-markers " ")))
  6498. (beginning-of-line 1)
  6499. (setq invis (org-invisible-p))
  6500. (looking-at (concat "\\(.*\\)\\(" (regexp-quote current) "\\)[ \t]*"))
  6501. (setq hd (match-string 1))
  6502. (delete-region (match-beginning 0) (match-end 0))
  6503. (insert-before-markers (org-trim hd) (if empty "" " ")))
  6504. (unless (equal tags "")
  6505. (move-to-column (max (current-column)
  6506. (if (> org-tags-column 0)
  6507. org-tags-column
  6508. (- (- org-tags-column) (length tags))))
  6509. t)
  6510. (insert-before-markers tags)
  6511. (if (and (not invis) (org-invisible-p))
  6512. (outline-flag-region (point-at-bol) (point) nil)))
  6513. (move-to-column col))))
  6514. (defun org-tags-completion-function (string predicate &optional flag)
  6515. (let (s1 s2 rtn (ctable org-last-tags-completion-table))
  6516. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  6517. (setq s1 (match-string 1 string)
  6518. s2 (match-string 2 string))
  6519. (setq s1 "" s2 string))
  6520. (cond
  6521. ((eq flag nil)
  6522. ;; try completion
  6523. (setq rtn (try-completion s2 ctable))
  6524. (if (stringp rtn)
  6525. (concat s1 s2 (substring rtn (length s2))
  6526. (if (and org-add-colon-after-tag-completion
  6527. (assoc rtn ctable))
  6528. ":" "")))
  6529. )
  6530. ((eq flag t)
  6531. ;; all-completions
  6532. (all-completions s2 ctable)
  6533. )
  6534. ((eq flag 'lambda)
  6535. ;; exact match?
  6536. (assoc s2 ctable)))
  6537. ))
  6538. (defun org-get-tags ()
  6539. "Get the TAGS string in the current headline."
  6540. (unless (org-on-heading-p)
  6541. (error "Not on a heading"))
  6542. (save-excursion
  6543. (beginning-of-line 1)
  6544. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  6545. (org-match-string-no-properties 1)
  6546. "")))
  6547. (defun org-get-buffer-tags ()
  6548. "Get a table of all tags used in the buffer, for completion."
  6549. (let (tags)
  6550. (save-excursion
  6551. (goto-char (point-min))
  6552. (while (re-search-forward "[ \t]:\\([A-Za-z_@0-9:]+\\):[ \t\r\n]" nil t)
  6553. (mapc (lambda (x) (add-to-list 'tags x))
  6554. (org-split-string (org-match-string-no-properties 1) ":"))))
  6555. (mapcar 'list tags)))
  6556. ;;; Link Stuff
  6557. (defun org-find-file-at-mouse (ev)
  6558. "Open file link or URL at mouse."
  6559. (interactive "e")
  6560. (mouse-set-point ev)
  6561. (org-open-at-point 'in-emacs))
  6562. (defun org-open-at-mouse (ev)
  6563. "Open file link or URL at mouse."
  6564. (interactive "e")
  6565. (mouse-set-point ev)
  6566. (org-open-at-point))
  6567. (defun org-open-at-point (&optional in-emacs)
  6568. "Open link at or after point.
  6569. If there is no link at point, this function will search forward up to
  6570. the end of the current subtree.
  6571. Normally, files will be opened by an appropriate application. If the
  6572. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6573. (interactive "P")
  6574. (org-remove-occur-highlights nil nil t)
  6575. (if (org-at-timestamp-p)
  6576. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6577. (substring (match-string 1) 0 10)))
  6578. 1)
  6579. (let (type path link line search (pos (point)))
  6580. (catch 'match
  6581. (save-excursion
  6582. (skip-chars-forward "^]\n\r")
  6583. (when (and (re-search-backward "\\[\\[" nil t)
  6584. (looking-at org-bracket-link-regexp)
  6585. (<= (match-beginning 0) pos)
  6586. (>= (match-end 0) pos))
  6587. (setq link (org-link-unescape (match-string 1)))
  6588. (while (string-match " *\n *" link)
  6589. (setq link (replace-match " " t t link)))
  6590. (if (string-match org-link-re-with-space link)
  6591. (setq type (match-string 1 link)
  6592. path (match-string 2 link))
  6593. (setq type "thisfile"
  6594. path link))
  6595. (throw 'match t)))
  6596. (when (get-text-property (point) 'org-linked-text)
  6597. (setq type "thisfile"
  6598. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6599. (1+ (point)) (point))
  6600. path (buffer-substring
  6601. (previous-single-property-change pos 'org-linked-text)
  6602. (next-single-property-change pos 'org-linked-text)))
  6603. (throw 'match t))
  6604. (save-excursion
  6605. (skip-chars-backward
  6606. (concat "^[]" org-non-link-chars
  6607. (if org-allow-space-in-plain-links " " "")))
  6608. (when (or (looking-at org-link-regexp)
  6609. (and (re-search-forward org-link-regexp (point-at-eol) t)
  6610. (<= (match-beginning 0) pos)
  6611. (>= (match-end 0) pos)))
  6612. (setq type (match-string 1)
  6613. path (match-string 2))
  6614. (throw 'match t)))
  6615. (save-excursion
  6616. (skip-chars-backward "^ \t\n\r")
  6617. (when (looking-at "\\(:[A-Za-z_@0-9:]+\\):[ \t\r\n]")
  6618. (setq type "tags"
  6619. path (match-string 1))
  6620. (while (string-match ":" path)
  6621. (setq path (replace-match "+" t t path)))
  6622. (throw 'match t)))
  6623. (save-excursion
  6624. (skip-chars-backward "a-zA-Z_")
  6625. (when (and org-activate-camels
  6626. (looking-at org-camel-regexp))
  6627. (setq type "camel" path (match-string 0))
  6628. (if (equal (char-before) ?*)
  6629. (setq path (concat "*" path))))
  6630. (throw 'match t)))
  6631. (unless path
  6632. (error "No link found"))
  6633. ;; Remove any trailing spaces in path
  6634. (if (string-match " +\\'" path)
  6635. (setq path (replace-match "" t t path)))
  6636. (cond
  6637. ((member type '("http" "https" "ftp" "mailto" "news"))
  6638. (browse-url (concat type ":" path)))
  6639. ((string= type "tags")
  6640. (org-tags-view in-emacs path))
  6641. ((or (string= type "camel")
  6642. (string= type "thisfile"))
  6643. (org-mark-ring-push)
  6644. (org-link-search
  6645. path
  6646. (cond ((equal in-emacs '(4)) 'occur)
  6647. ((equal in-emacs '(16)) 'org-occur)
  6648. (t nil))))
  6649. ((string= type "file")
  6650. (if (string-match "::?\\([0-9]+\\)\\'" path) ;; second : optional
  6651. (setq line (string-to-number (match-string 1 path))
  6652. path (substring path 0 (match-beginning 0)))
  6653. (if (string-match "::\\(.+\\)\\'" path)
  6654. (setq search (match-string 1 path)
  6655. path (substring path 0 (match-beginning 0)))))
  6656. (org-open-file path in-emacs line search))
  6657. ((string= type "news")
  6658. (org-follow-gnus-link path))
  6659. ((string= type "bbdb")
  6660. (org-follow-bbdb-link path))
  6661. ((string= type "gnus")
  6662. (let (group article)
  6663. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  6664. (error "Error in Gnus link"))
  6665. (setq group (match-string 1 path)
  6666. article (match-string 3 path))
  6667. (org-follow-gnus-link group article)))
  6668. ((string= type "vm")
  6669. (let (folder article)
  6670. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  6671. (error "Error in VM link"))
  6672. (setq folder (match-string 1 path)
  6673. article (match-string 3 path))
  6674. ;; in-emacs is the prefix arg, will be interpreted as read-only
  6675. (org-follow-vm-link folder article in-emacs)))
  6676. ((string= type "wl")
  6677. (let (folder article)
  6678. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  6679. (error "Error in Wanderlust link"))
  6680. (setq folder (match-string 1 path)
  6681. article (match-string 3 path))
  6682. (org-follow-wl-link folder article)))
  6683. ((string= type "mhe")
  6684. (let (folder article)
  6685. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  6686. (error "Error in MHE link"))
  6687. (setq folder (match-string 1 path)
  6688. article (match-string 3 path))
  6689. (org-follow-mhe-link folder article)))
  6690. ((string= type "rmail")
  6691. (let (folder article)
  6692. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  6693. (error "Error in RMAIL link"))
  6694. (setq folder (match-string 1 path)
  6695. article (match-string 3 path))
  6696. (org-follow-rmail-link folder article)))
  6697. ((string= type "shell")
  6698. (let ((cmd path))
  6699. (while (string-match "@{" cmd)
  6700. (setq cmd (replace-match "<" t t cmd)))
  6701. (while (string-match "@}" cmd)
  6702. (setq cmd (replace-match ">" t t cmd)))
  6703. (if (or (not org-confirm-shell-links)
  6704. (yes-or-no-p (format "Execute \"%s\" in the shell? " cmd)))
  6705. (shell-command cmd)
  6706. (error "Abort"))))
  6707. (t
  6708. (browse-url-at-point))))))
  6709. (defun org-link-search (s &optional type)
  6710. "Search for a link search option.
  6711. When S is a CamelCaseWord, search for a target, or for a sentence containing
  6712. the words. If S is surrounded by forward slashes, it is interpreted as a
  6713. regular expression. In org-mode files, this will create an `org-occur'
  6714. sparse tree. In ordinary files, `occur' will be used to list matches.
  6715. If the current buffer is in `dired-mode', grep will be used to search
  6716. in all files."
  6717. (let ((case-fold-search t)
  6718. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6719. (pos (point))
  6720. (pre "") (post "")
  6721. words re0 re1 re2 re3 re4 re5 re2a reall camel)
  6722. (cond ((save-excursion
  6723. (goto-char (point-min))
  6724. (and
  6725. (re-search-forward
  6726. (concat "<<" (regexp-quote s0) ">>") nil t)
  6727. (setq pos (match-beginning 0))))
  6728. ;; There is an exact target for this
  6729. (goto-char pos))
  6730. ((string-match "^/\\(.*\\)/$" s)
  6731. ;; A regular expression
  6732. (cond
  6733. ((eq major-mode 'org-mode)
  6734. (org-occur (match-string 1 s)))
  6735. ;;((eq major-mode 'dired-mode)
  6736. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6737. (t (org-do-occur (match-string 1 s)))))
  6738. ((or (setq camel (string-match (concat "^" org-camel-regexp "$") s))
  6739. t)
  6740. ;; A camel or a normal search string
  6741. (when (equal (string-to-char s) ?*)
  6742. ;; Anchor on headlines, post may include tags.
  6743. (setq pre "^\\*+[ \t]*\\(\\sw+\\)?[ \t]*"
  6744. post "[ \t]*\\([ \t]+:[a-zA-Z_@0-9:+]:[ \t]*\\)?$"
  6745. s (substring s 1)))
  6746. (remove-text-properties
  6747. 0 (length s)
  6748. '(face nil mouse-face nil keymap nil fontified nil) s)
  6749. ;; Make a series of regular expressions to find a match
  6750. (setq words
  6751. (if camel
  6752. (org-camel-to-words s)
  6753. (org-split-string s "[ \n\r\t]+"))
  6754. re0 (concat "<<" (regexp-quote s0) ">>")
  6755. re2 (concat "\\<" (mapconcat 'downcase words "[ \t]+") "\\>")
  6756. re2a (concat "\\<" (mapconcat 'downcase words "[ \t\r\n]+") "\\>")
  6757. re4 (concat "\\<" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\>")
  6758. re1 (concat pre re2 post)
  6759. re3 (concat pre re4 post)
  6760. re5 (concat pre ".*" re4)
  6761. re2 (concat pre re2)
  6762. re2a (concat pre re2a)
  6763. re4 (concat pre re4)
  6764. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6765. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6766. re5 "\\)"
  6767. ))
  6768. (cond
  6769. ((eq type 'org-occur) (org-occur reall))
  6770. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6771. (t (goto-char (point-min))
  6772. (if (or (org-search-not-link re0 nil t)
  6773. (org-search-not-link re1 nil t)
  6774. (org-search-not-link re2 nil t)
  6775. (org-search-not-link re2a nil t)
  6776. (org-search-not-link re3 nil t)
  6777. (org-search-not-link re4 nil t)
  6778. (org-search-not-link re5 nil t)
  6779. )
  6780. (goto-char (match-beginning 0))
  6781. (goto-char pos)
  6782. (error "No match")))))
  6783. (t
  6784. ;; Normal string-search
  6785. (goto-char (point-min))
  6786. (if (search-forward s nil t)
  6787. (goto-char (match-beginning 0))
  6788. (error "No match"))))
  6789. (and (eq major-mode 'org-mode) (org-show-hierarchy-above))))
  6790. (defun org-search-not-link (&rest args)
  6791. "Execute `re-search-forward', but only accept matches that are not a link."
  6792. (catch 'exit
  6793. (let ((pos (point)) p1)
  6794. (while (apply 're-search-forward args)
  6795. (setq p1 (point))
  6796. (if (not (save-match-data
  6797. (and (re-search-backward "\\[\\[" nil t)
  6798. (looking-at org-bracket-link-regexp)
  6799. (<= (match-beginning 0) p1)
  6800. (>= (match-end 0) p1))))
  6801. (progn (goto-char (match-end 0))
  6802. (throw 'exit (point)))
  6803. (goto-char (match-end 0)))))))
  6804. (defun org-do-occur (regexp &optional cleanup)
  6805. "Call the Emacs command `occur'.
  6806. If CLEANUP is non-nil, remove the printout of the regular expression
  6807. in the *Occur* buffer. This is useful if the regex is long and not useful
  6808. to read."
  6809. (occur regexp)
  6810. (when cleanup
  6811. (let ((cwin (selected-window)) win beg end)
  6812. (when (setq win (get-buffer-window "*Occur*"))
  6813. (select-window win))
  6814. (goto-char (point-min))
  6815. (when (re-search-forward "match[a-z]+" nil t)
  6816. (setq beg (match-end 0))
  6817. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6818. (setq end (1- (match-beginning 0)))))
  6819. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  6820. (goto-char (point-min))
  6821. (select-window cwin))))
  6822. (defvar org-mark-ring nil
  6823. "Mark ring for positions before jumps in Org-mode.")
  6824. (defvar org-mark-ring-last-goto nil
  6825. "Last position in the mark ring used to go back.")
  6826. ;; Fill and close the ring
  6827. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6828. (loop for i from 1 to org-mark-ring-length do
  6829. (push (make-marker) org-mark-ring))
  6830. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6831. org-mark-ring)
  6832. (defun org-mark-ring-push (&optional pos buffer)
  6833. "Put the current position or POS into the mark ring and rotate it."
  6834. (interactive)
  6835. (setq pos (or pos (point)))
  6836. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6837. (move-marker (car org-mark-ring)
  6838. (or pos (point))
  6839. (or buffer (current-buffer)))
  6840. (message
  6841. (substitute-command-keys
  6842. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6843. (defun org-mark-ring-goto (&optional n)
  6844. "Jump to the previous position in the mark ring.
  6845. With prefix arg N, jump back that many stored positions. When
  6846. called several times in succession, walk through the entire ring.
  6847. Org-mode commands jumping to a different position in the current file,
  6848. or to another Org-mode file, automatically push the old position
  6849. onto the ring."
  6850. (interactive "p")
  6851. (let (p m)
  6852. (if (eq last-command this-command)
  6853. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6854. (setq p org-mark-ring))
  6855. (setq org-mark-ring-last-goto p)
  6856. (setq m (car p))
  6857. (switch-to-buffer (marker-buffer m))
  6858. (goto-char m)
  6859. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-hierarchy-above))))
  6860. (defun org-camel-to-words (s)
  6861. "Split \"CamelCaseWords\" to (\"Camel\" \"Case\" \"Words\")."
  6862. (let ((case-fold-search nil)
  6863. words)
  6864. (while (string-match "[a-z][A-Z]" s)
  6865. (push (substring s 0 (1+ (match-beginning 0))) words)
  6866. (setq s (substring s (1+ (match-beginning 0)))))
  6867. (nreverse (cons s words))))
  6868. (defun org-remove-angle-brackets (s)
  6869. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6870. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6871. s)
  6872. (defun org-add-angle-brackets (s)
  6873. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6874. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6875. s)
  6876. (defun org-follow-bbdb-link (name)
  6877. "Follow a BBDB link to NAME."
  6878. (require 'bbdb)
  6879. (let ((inhibit-redisplay t)
  6880. (bbdb-electric-p nil))
  6881. (catch 'exit
  6882. ;; Exact match on name
  6883. (bbdb-name (concat "\\`" name "\\'") nil)
  6884. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  6885. ;; Exact match on name
  6886. (bbdb-company (concat "\\`" name "\\'") nil)
  6887. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  6888. ;; Partial match on name
  6889. (bbdb-name name nil)
  6890. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  6891. ;; Partial match on company
  6892. (bbdb-company name nil)
  6893. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  6894. ;; General match including network address and notes
  6895. (bbdb name nil)
  6896. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  6897. (delete-window (get-buffer-window "*BBDB*"))
  6898. (error "No matching BBDB record")))))
  6899. (defun org-follow-gnus-link (&optional group article)
  6900. "Follow a Gnus link to GROUP and ARTICLE."
  6901. (require 'gnus)
  6902. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  6903. (if group (gnus-fetch-group group))
  6904. (if article
  6905. (or (gnus-summary-goto-article article nil 'force)
  6906. (if (fboundp 'gnus-summary-insert-cached-articles)
  6907. (progn
  6908. (gnus-summary-insert-cached-articles)
  6909. (gnus-summary-goto-article article nil 'force))
  6910. (message "Message could not be found.")))))
  6911. (defun org-follow-vm-link (&optional folder article readonly)
  6912. "Follow a VM link to FOLDER and ARTICLE."
  6913. (require 'vm)
  6914. (setq article (org-add-angle-brackets article))
  6915. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  6916. ;; ange-ftp or efs or tramp access
  6917. (let ((user (or (match-string 1 folder) (user-login-name)))
  6918. (host (match-string 2 folder))
  6919. (file (match-string 3 folder)))
  6920. (cond
  6921. ((featurep 'tramp)
  6922. ;; use tramp to access the file
  6923. (if (featurep 'xemacs)
  6924. (setq folder (format "[%s@%s]%s" user host file))
  6925. (setq folder (format "/%s@%s:%s" user host file))))
  6926. (t
  6927. ;; use ange-ftp or efs
  6928. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  6929. (setq folder (format "/%s@%s:%s" user host file))))))
  6930. (when folder
  6931. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  6932. (sit-for 0.1)
  6933. (when article
  6934. (vm-select-folder-buffer)
  6935. (widen)
  6936. (let ((case-fold-search t))
  6937. (goto-char (point-min))
  6938. (if (not (re-search-forward
  6939. (concat "^" "message-id: *" (regexp-quote article))))
  6940. (error "Could not find the specified message in this folder"))
  6941. (vm-isearch-update)
  6942. (vm-isearch-narrow)
  6943. (vm-beginning-of-message)
  6944. (vm-summarize)))))
  6945. (defun org-follow-wl-link (folder article)
  6946. "Follow a Wanderlust link to FOLDER and ARTICLE."
  6947. (setq article (org-add-angle-brackets article))
  6948. (wl-summary-goto-folder-subr folder 'no-sync t nil t)
  6949. (if article (wl-summary-jump-to-msg-by-message-id article ">"))
  6950. (wl-summary-redisplay))
  6951. (defun org-follow-rmail-link (folder article)
  6952. "Follow an RMAIL link to FOLDER and ARTICLE."
  6953. (setq article (org-add-angle-brackets article))
  6954. (let (message-number)
  6955. (save-excursion
  6956. (save-window-excursion
  6957. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  6958. (setq message-number
  6959. (save-restriction
  6960. (widen)
  6961. (goto-char (point-max))
  6962. (if (re-search-backward
  6963. (concat "^Message-ID:\\s-+" (regexp-quote
  6964. (or article "")))
  6965. nil t)
  6966. (rmail-what-message))))))
  6967. (if message-number
  6968. (progn
  6969. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  6970. (rmail-show-message message-number)
  6971. message-number)
  6972. (error "Message not found"))))
  6973. ;; mh-e integration based on planner-mode
  6974. (defun org-mhe-get-message-real-folder ()
  6975. "Return the name of the current message real folder, so if you use
  6976. sequences, it will now work."
  6977. (save-excursion
  6978. (let* ((folder
  6979. (if (equal major-mode 'mh-folder-mode)
  6980. mh-current-folder
  6981. ;; Refer to the show buffer
  6982. mh-show-folder-buffer))
  6983. (end-index
  6984. (if (boundp 'mh-index-folder)
  6985. (min (length mh-index-folder) (length folder))))
  6986. )
  6987. ;; a simple test on mh-index-data does not work, because
  6988. ;; mh-index-data is always nil in a show buffer.
  6989. (if (and (boundp 'mh-index-folder)
  6990. (string= mh-index-folder (substring folder 0 end-index)))
  6991. (if (equal major-mode 'mh-show-mode)
  6992. (save-window-excursion
  6993. (when (buffer-live-p (get-buffer folder))
  6994. (progn
  6995. (pop-to-buffer folder)
  6996. (org-mhe-get-message-folder-from-index)
  6997. )
  6998. ))
  6999. (org-mhe-get-message-folder-from-index)
  7000. )
  7001. folder
  7002. )
  7003. )))
  7004. (defun org-mhe-get-message-folder-from-index ()
  7005. "Returns the name of the message folder in a index folder buffer."
  7006. (save-excursion
  7007. (mh-index-previous-folder)
  7008. (re-search-forward "^\\(+.*\\)$" nil t)
  7009. (message (match-string 1))))
  7010. (defun org-mhe-get-message-folder ()
  7011. "Return the name of the current message folder. Be careful if you
  7012. use sequences."
  7013. (save-excursion
  7014. (if (equal major-mode 'mh-folder-mode)
  7015. mh-current-folder
  7016. ;; Refer to the show buffer
  7017. mh-show-folder-buffer)))
  7018. (defun org-mhe-get-message-num ()
  7019. "Return the number of the current message. Be careful if you
  7020. use sequences."
  7021. (save-excursion
  7022. (if (equal major-mode 'mh-folder-mode)
  7023. (mh-get-msg-num nil)
  7024. ;; Refer to the show buffer
  7025. (mh-show-buffer-message-number))))
  7026. (defun org-mhe-get-header (header)
  7027. "Return a header of the message in folder mode. This will create a
  7028. show buffer for the corresponding message. If you have a more clever
  7029. idea..."
  7030. (let* ((folder (org-mhe-get-message-folder))
  7031. (num (org-mhe-get-message-num))
  7032. (buffer (get-buffer-create (concat "show-" folder)))
  7033. (header-field))
  7034. (with-current-buffer buffer
  7035. (mh-display-msg num folder)
  7036. (if (equal major-mode 'mh-folder-mode)
  7037. (mh-header-display)
  7038. (mh-show-header-display))
  7039. (set-buffer buffer)
  7040. (setq header-field (mh-get-header-field header))
  7041. (if (equal major-mode 'mh-folder-mode)
  7042. (mh-show)
  7043. (mh-show-show))
  7044. header-field)))
  7045. (defun org-follow-mhe-link (folder article)
  7046. "Follow an MHE link to FOLDER and ARTICLE.
  7047. If ARTICLE is nil FOLDER is shown. If the configuration variable
  7048. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  7049. ARTICLE is searched in all folders. Indexed searches (swish++,
  7050. namazu, and others supported by MH-E) will always search in all
  7051. folders."
  7052. (require 'mh-e)
  7053. (require 'mh-search)
  7054. (require 'mh-utils)
  7055. (mh-find-path)
  7056. (if (not article)
  7057. (mh-visit-folder (mh-normalize-folder-name folder))
  7058. (setq article (org-add-angle-brackets article))
  7059. (mh-search-choose)
  7060. (if (equal mh-searcher 'pick)
  7061. (progn
  7062. (mh-search folder (list "--message-id" article))
  7063. (when (and org-mhe-search-all-folders
  7064. (not (org-mhe-get-message-real-folder)))
  7065. (kill-this-buffer)
  7066. (mh-search "+" (list "--message-id" article))))
  7067. (mh-search "+" article))
  7068. (if (org-mhe-get-message-real-folder)
  7069. (mh-show-msg 1)
  7070. (kill-this-buffer)
  7071. (error "Message not found"))))
  7072. (defun org-open-file (path &optional in-emacs line search)
  7073. "Open the file at PATH.
  7074. First, this expands any special file name abbreviations. Then the
  7075. configuration variable `org-file-apps' is checked if it contains an
  7076. entry for this file type, and if yes, the corresponding command is launched.
  7077. If no application is found, Emacs simply visits the file.
  7078. With optional argument IN-EMACS, Emacs will visit the file.
  7079. Optional LINE specifies a line to go to, optional SEARCH a string to
  7080. search for. If LINE or SEARCH is given, the file will always be
  7081. opened in Emacs.
  7082. If the file does not exist, an error is thrown."
  7083. (setq in-emacs (or in-emacs line search))
  7084. (let* ((file (if (equal path "")
  7085. buffer-file-name
  7086. (convert-standard-filename (org-expand-file-name path))))
  7087. (dirp (file-directory-p file))
  7088. (dfile (downcase file))
  7089. (old-buffer (current-buffer))
  7090. (old-pos (point))
  7091. (old-mode major-mode)
  7092. ext cmd apps)
  7093. (if (and (not (file-exists-p file))
  7094. (not org-open-non-existing-files))
  7095. (error "No such file: %s" file))
  7096. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7097. (setq ext (match-string 1 dfile))
  7098. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7099. (setq ext (match-string 1 dfile))))
  7100. (setq apps (append org-file-apps (org-default-apps)))
  7101. (if in-emacs
  7102. (setq cmd 'emacs)
  7103. (setq cmd (or (and dirp (cdr (assoc 'directory apps)))
  7104. (cdr (assoc ext apps))
  7105. (cdr (assoc t apps)))))
  7106. (when (eq cmd 'mailcap)
  7107. (require 'mailcap)
  7108. (mailcap-parse-mailcaps)
  7109. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7110. (command (mailcap-mime-info mime-type)))
  7111. (if (stringp command)
  7112. (setq cmd command)
  7113. (setq cmd 'emacs))))
  7114. (cond
  7115. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7116. (setq cmd (format cmd (concat "\"" file "\"")))
  7117. (save-window-excursion
  7118. (shell-command (concat cmd " &"))))
  7119. ((or (stringp cmd)
  7120. (eq cmd 'emacs))
  7121. (unless (equal (file-truename file) (file-truename (or buffer-file-name "")))
  7122. (funcall (cdr (assq 'file org-link-frame-setup)) file))
  7123. (if line (goto-line line)
  7124. (if search (org-link-search search))))
  7125. ((consp cmd)
  7126. (eval cmd))
  7127. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7128. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  7129. (or (not (equal old-buffer (current-buffer)))
  7130. (not (equal old-pos (point))))
  7131. (org-mark-ring-push old-pos old-buffer))))
  7132. (defun org-default-apps ()
  7133. "Return the default applications for this operating system."
  7134. (cond
  7135. ((eq system-type 'darwin)
  7136. org-file-apps-defaults-macosx)
  7137. ((eq system-type 'windows-nt)
  7138. org-file-apps-defaults-windowsnt)
  7139. (t org-file-apps-defaults-gnu)))
  7140. (defun org-expand-file-name (path)
  7141. "Replace special path abbreviations and expand the file name."
  7142. (expand-file-name path))
  7143. (defvar org-insert-link-history nil
  7144. "Minibuffer history for links inserted with `org-insert-link'.")
  7145. (defvar org-stored-links nil
  7146. "Contains the links stored with `org-store-link'.")
  7147. ;;;###autoload
  7148. (defun org-store-link (arg)
  7149. "\\<org-mode-map>Store an org-link to the current location.
  7150. This link can later be inserted into an org-buffer with
  7151. \\[org-insert-link].
  7152. For some link types, a prefix arg is interpreted:
  7153. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  7154. For file links, arg negates `org-context-in-file-links'."
  7155. (interactive "P")
  7156. (let (link cpltxt desc txt (pos (point)))
  7157. (cond
  7158. ((eq major-mode 'bbdb-mode)
  7159. (setq cpltxt (concat
  7160. "bbdb:"
  7161. (or (bbdb-record-name (bbdb-current-record))
  7162. (bbdb-record-company (bbdb-current-record))))
  7163. link (org-make-link cpltxt)))
  7164. ((eq major-mode 'calendar-mode)
  7165. (let ((cd (calendar-cursor-to-date)))
  7166. (setq link
  7167. (format-time-string
  7168. (car org-time-stamp-formats)
  7169. (apply 'encode-time
  7170. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7171. nil nil nil))))))
  7172. ((or (eq major-mode 'vm-summary-mode)
  7173. (eq major-mode 'vm-presentation-mode))
  7174. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  7175. (vm-follow-summary-cursor)
  7176. (save-excursion
  7177. (vm-select-folder-buffer)
  7178. (let* ((message (car vm-message-pointer))
  7179. (folder buffer-file-name)
  7180. (subject (vm-su-subject message))
  7181. (author (vm-su-full-name message))
  7182. (message-id (vm-su-message-id message)))
  7183. (setq message-id (org-remove-angle-brackets message-id))
  7184. (setq folder (abbreviate-file-name folder))
  7185. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  7186. folder)
  7187. (setq folder (replace-match "" t t folder)))
  7188. (setq cpltxt (concat author " on: " subject))
  7189. (setq link (org-make-link "vm:" folder "#" message-id)))))
  7190. ((eq major-mode 'wl-summary-mode)
  7191. (let* ((msgnum (wl-summary-message-number))
  7192. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  7193. msgnum 'message-id))
  7194. (wl-message-entity (elmo-msgdb-overview-get-entity
  7195. msgnum (wl-summary-buffer-msgdb)))
  7196. (author (wl-summary-line-from)) ; FIXME: correct?
  7197. (subject "???")) ; FIXME:
  7198. (setq message-id (org-remove-angle-brackets message-id))
  7199. (setq cpltxt (concat author " on: " subject))
  7200. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  7201. "#" message-id))))
  7202. ((or (equal major-mode 'mh-folder-mode)
  7203. (equal major-mode 'mh-show-mode))
  7204. (let ((from-header (org-mhe-get-header "From:"))
  7205. (to-header (org-mhe-get-header "To:"))
  7206. (subject (org-mhe-get-header "Subject:")))
  7207. (setq cpltxt (concat from-header " on: " subject))
  7208. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  7209. (org-remove-angle-brackets
  7210. (org-mhe-get-header "Message-Id:"))))))
  7211. ((eq major-mode 'rmail-mode)
  7212. (save-excursion
  7213. (save-restriction
  7214. (rmail-narrow-to-non-pruned-header)
  7215. (let ((folder buffer-file-name)
  7216. (message-id (mail-fetch-field "message-id"))
  7217. (author (mail-fetch-field "from"))
  7218. (subject (mail-fetch-field "subject")))
  7219. (setq message-id (org-remove-angle-brackets message-id))
  7220. (setq cpltxt (concat author " on: " subject))
  7221. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  7222. ((eq major-mode 'gnus-group-mode)
  7223. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  7224. (gnus-group-group-name)) ; version
  7225. ((fboundp 'gnus-group-name)
  7226. (gnus-group-name))
  7227. (t "???"))))
  7228. (setq cpltxt (concat
  7229. (if (org-xor arg org-usenet-links-prefer-google)
  7230. "http://groups.google.com/groups?group="
  7231. "gnus:")
  7232. group)
  7233. link (org-make-link cpltxt))))
  7234. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  7235. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  7236. (gnus-summary-beginning-of-article)
  7237. (let* ((group (car gnus-article-current))
  7238. (article (cdr gnus-article-current))
  7239. (header (gnus-summary-article-header article))
  7240. (author (mail-header-from header))
  7241. (message-id (mail-header-id header))
  7242. (date (mail-header-date header))
  7243. (subject (gnus-summary-subject-string)))
  7244. (setq cpltxt (concat author " on: " subject))
  7245. (if (org-xor arg org-usenet-links-prefer-google)
  7246. (setq link
  7247. (concat
  7248. cpltxt "\n "
  7249. (format "http://groups.google.com/groups?as_umsgid=%s"
  7250. (org-fixup-message-id-for-http message-id))))
  7251. (setq link (org-make-link "gnus:" group
  7252. "#" (number-to-string article))))))
  7253. ((eq major-mode 'w3-mode)
  7254. (setq cpltxt (url-view-url t)
  7255. link (org-make-link cpltxt)))
  7256. ((eq major-mode 'w3m-mode)
  7257. (setq cpltxt w3m-current-url
  7258. link (org-make-link cpltxt)))
  7259. ((eq major-mode 'org-mode)
  7260. ;; Just link to current headline
  7261. (setq cpltxt (concat "file:"
  7262. (abbreviate-file-name buffer-file-name)))
  7263. ;; Add a context search string
  7264. (when (org-xor org-context-in-file-links arg)
  7265. ;; Check if we are on a target
  7266. (if (save-excursion
  7267. (skip-chars-forward "^>\n\r")
  7268. (and (re-search-backward "<<" nil t)
  7269. (looking-at "<<\\(.*?\\)>>")
  7270. (<= (match-beginning 0) pos)
  7271. (>= (match-end 0) pos)))
  7272. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  7273. (setq txt (cond
  7274. ((org-on-heading-p) nil)
  7275. ((org-region-active-p)
  7276. (buffer-substring (region-beginning) (region-end)))
  7277. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  7278. (setq cpltxt
  7279. (concat cpltxt "::"
  7280. (if org-file-link-context-use-camel-case
  7281. (org-make-org-heading-camel txt)
  7282. (org-make-org-heading-search-string txt)))
  7283. desc "NONE")))
  7284. (if (string-match "::\\'" cpltxt)
  7285. (setq cpltxt (substring cpltxt 0 -2)))
  7286. (setq link (org-make-link cpltxt)))
  7287. (buffer-file-name
  7288. ;; Just link to this file here.
  7289. (setq cpltxt (concat "file:"
  7290. (abbreviate-file-name buffer-file-name)))
  7291. ;; Add a context string
  7292. (when (org-xor org-context-in-file-links arg)
  7293. (setq txt (if (org-region-active-p)
  7294. (buffer-substring (region-beginning) (region-end))
  7295. (buffer-substring (point-at-bol) (point-at-eol))))
  7296. (setq cpltxt
  7297. (concat cpltxt "::"
  7298. (if org-file-link-context-use-camel-case
  7299. (org-make-org-heading-camel txt)
  7300. (org-make-org-heading-search-string txt)))
  7301. desc "NONE"))
  7302. (setq link (org-make-link cpltxt)))
  7303. ((interactive-p)
  7304. (error "Cannot link to a buffer which is not visiting a file"))
  7305. (t (setq link nil)))
  7306. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7307. (setq link (or link cpltxt)
  7308. desc (or desc cpltxt))
  7309. (if (equal desc "NONE") (setq desc nil))
  7310. (if (and (interactive-p) link)
  7311. (progn
  7312. (setq org-stored-links
  7313. (cons (list cpltxt link desc) org-stored-links))
  7314. (message "Stored: %s" (or cpltxt link)))
  7315. (org-make-link-string link desc))))
  7316. (defun org-make-org-heading-search-string (&optional string heading)
  7317. "Make search string for STRING or current headline."
  7318. (interactive)
  7319. (let ((s (or string (org-get-heading))))
  7320. (unless (and string (not heading))
  7321. ;; We are using a headline, clean up garbage in there.
  7322. (if (string-match org-todo-regexp s)
  7323. (setq s (replace-match "" t t s)))
  7324. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  7325. (setq s (replace-match "" t t s)))
  7326. (setq s (org-trim s))
  7327. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7328. org-comment-string "\\)") s)
  7329. (setq s (replace-match "" t t s)))
  7330. (while (string-match org-ts-regexp s)
  7331. (setq s (replace-match "" t t s))))
  7332. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7333. (setq s (replace-match " " t t s)))
  7334. (or string (setq s (concat "*" s))) ; Add * for headlines
  7335. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7336. (defun org-make-org-heading-camel (&optional string heading)
  7337. "Make a CamelCase string for STRING or the current headline."
  7338. (interactive)
  7339. (let ((s (or string (org-get-heading))))
  7340. (unless (and string (not heading))
  7341. ;; We are using a headline, clean up garbage in there.
  7342. (if (string-match org-todo-regexp s)
  7343. (setq s (replace-match "" t t s)))
  7344. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  7345. (setq s (replace-match "" t t s)))
  7346. (setq s (org-trim s))
  7347. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7348. org-comment-string "\\)") s)
  7349. (setq s (replace-match "" t t s)))
  7350. (while (string-match org-ts-regexp s)
  7351. (setq s (replace-match "" t t s))))
  7352. (while (string-match "[^a-zA-Z_ \t]+" s)
  7353. (setq s (replace-match " " t t s)))
  7354. (or string (setq s (concat "*" s))) ; Add * for headlines
  7355. (mapconcat 'capitalize (org-split-string s "[ \t]+") "")))
  7356. (defun org-make-link (&rest strings)
  7357. "Concatenate STRINGS, format resulting string with `org-link-format'."
  7358. (format org-link-format (apply 'concat strings)))
  7359. (defun org-make-link-string (link &optional description)
  7360. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7361. (if (eq org-link-style 'bracket)
  7362. (concat description "\n" link)
  7363. (if (stringp description)
  7364. ;; Remove brackets from the description, they are fatal.
  7365. (while (string-match "\\[\\|\\]" description)
  7366. (setq description (replace-match "" t t description))))
  7367. (concat "[[" (org-link-escape link) "]"
  7368. (if description (concat "[" description "]") "")
  7369. "]")))
  7370. (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
  7371. "Association list of escapes for some characters problematic in links.")
  7372. (defun org-link-escape (text)
  7373. "Escape charaters in TEXT that are problematic for links."
  7374. (when text
  7375. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  7376. org-link-escape-chars "\\|")))
  7377. (while (string-match re text)
  7378. (setq text
  7379. (replace-match
  7380. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  7381. t t text)))
  7382. text)))
  7383. (defun org-link-unescape (text)
  7384. "Reverse the action of `org-link-escape'."
  7385. (when text
  7386. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  7387. org-link-escape-chars "\\|")))
  7388. (while (string-match re text)
  7389. (setq text
  7390. (replace-match
  7391. (car (rassoc (match-string 0 text) org-link-escape-chars))
  7392. t t text)))
  7393. text)))
  7394. (defun org-xor (a b)
  7395. "Exclusive or."
  7396. (if a (not b) b))
  7397. (defun org-get-header (header)
  7398. "Find a header field in the current buffer."
  7399. (save-excursion
  7400. (goto-char (point-min))
  7401. (let ((case-fold-search t) s)
  7402. (cond
  7403. ((eq header 'from)
  7404. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  7405. (setq s (match-string 1)))
  7406. (while (string-match "\"" s)
  7407. (setq s (replace-match "" t t s)))
  7408. (if (string-match "[<(].*" s)
  7409. (setq s (replace-match "" t t s))))
  7410. ((eq header 'message-id)
  7411. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  7412. (setq s (match-string 1))))
  7413. ((eq header 'subject)
  7414. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  7415. (setq s (match-string 1)))))
  7416. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  7417. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  7418. s)))
  7419. (defun org-fixup-message-id-for-http (s)
  7420. "Replace special characters in a message id, so it can be used in an http query."
  7421. (while (string-match "<" s)
  7422. (setq s (replace-match "%3C" t t s)))
  7423. (while (string-match ">" s)
  7424. (setq s (replace-match "%3E" t t s)))
  7425. (while (string-match "@" s)
  7426. (setq s (replace-match "%40" t t s)))
  7427. s)
  7428. (defun org-insert-link (&optional complete-file)
  7429. "Insert a link. At the prompt, enter the link.
  7430. Completion can be used to select a link previously stored with
  7431. `org-store-link'. When the empty string is entered (i.e. if you just
  7432. press RET at the prompt), the link defaults to the most recently
  7433. stored link. As SPC triggers completion in the minibuffer, you need to
  7434. use M-SPC or C-q SPC to force the insertion of a space character.
  7435. You will also be prompted for a description, and if one is given, it will
  7436. be displayed in the buffer instead of the link.
  7437. If there is already a link at point, this command will allow you to edit link
  7438. and description parts.
  7439. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  7440. selected using completion. The path to the file will be relative to
  7441. the current directory if the file is in the current directory or a
  7442. subdirectory. Otherwise, the link will be the absolute path as
  7443. completed in the minibuffer (i.e. normally ~/path/to/file).
  7444. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  7445. is in the current directory or below."
  7446. (interactive "P")
  7447. (let (link desc entry remove file (pos (point)))
  7448. (cond
  7449. ((save-excursion
  7450. (skip-chars-forward "^]\n\r")
  7451. (and (re-search-backward "\\[\\[" nil t)
  7452. (looking-at org-bracket-link-regexp)
  7453. (<= (match-beginning 0) pos)
  7454. (>= (match-end 0) pos)))
  7455. ;; We do have a link at point, and we are going to edit it.
  7456. (setq remove (list (match-beginning 0) (match-end 0)))
  7457. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7458. (setq link (read-string "Link: " (org-match-string-no-properties 1))))
  7459. (complete-file
  7460. ;; Completing read for file names.
  7461. (setq file (read-file-name "File: "))
  7462. (let ((pwd (file-name-as-directory (expand-file-name "."))))
  7463. (cond
  7464. ((equal complete-file '(16))
  7465. (setq link (org-make-link
  7466. "file:"
  7467. (abbreviate-file-name (expand-file-name file)))))
  7468. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7469. (expand-file-name file))
  7470. (setq link (org-make-link
  7471. "file:" (match-string 1 (expand-file-name file)))))
  7472. (t (setq link (org-make-link "file:" file))))))
  7473. (t
  7474. ;; Read link, with completion for stored links.
  7475. (setq link (completing-read
  7476. "Link: " org-stored-links nil nil nil
  7477. org-insert-link-history
  7478. (or (car (car org-stored-links)))))
  7479. (setq entry (assoc link org-stored-links))
  7480. (if (not org-keep-stored-link-after-insertion)
  7481. (setq org-stored-links (delq (assoc link org-stored-links)
  7482. org-stored-links)))
  7483. (setq link (if entry (nth 1 entry) link)
  7484. desc (or desc (nth 2 entry)))))
  7485. (if (string-match org-link-regexp link)
  7486. ;; URL-like link, normalize the use of angular brackets.
  7487. (setq link (org-make-link (org-remove-angle-brackets link))))
  7488. ;; Check if we are linking to the current file. If yes, simplify the link.
  7489. (when (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link)
  7490. (let* ((path (match-string 1 link))
  7491. (case-fold-search nil)
  7492. (search (match-string 2 link)))
  7493. (when (save-match-data
  7494. (equal (file-truename buffer-file-name)
  7495. (file-truename path)))
  7496. ;; We are linking to this same file, with a search option
  7497. (setq link search))))
  7498. (setq desc (read-string "Description: " desc))
  7499. (unless (string-match "\\S-" desc) (setq desc nil))
  7500. (if remove (apply 'delete-region remove))
  7501. (insert (org-make-link-string link desc))))
  7502. ;;; Hooks for remember.el
  7503. (defvar org-finish-function nil)
  7504. ;;;###autoload
  7505. (defun org-remember-annotation ()
  7506. "Return a link to the current location as an annotation for remember.el.
  7507. If you are using Org-mode files as target for data storage with
  7508. remember.el, then the annotations should include a link compatible with the
  7509. conventions in Org-mode. This function returns such a link."
  7510. (org-store-link nil))
  7511. (defconst org-remember-help
  7512. "Select a destination location for the note.
  7513. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  7514. RET at beg-of-buf -> Append to file as level 2 headline
  7515. RET on headline -> Store as sublevel entry to current headline
  7516. <left>/<right> -> before/after current headline, same headings level")
  7517. ;;;###autoload
  7518. (defun org-remember-apply-template ()
  7519. "Initialize *remember* buffer with template, invode `org-mode'.
  7520. This function should be placed into `remember-mode-hook' and in fact requires
  7521. to be run from that hook to fucntion properly."
  7522. (when org-remember-templates
  7523. (let* ((entry (if (= (length org-remember-templates) 1)
  7524. (cdar org-remember-templates)
  7525. (message "Select template: %s"
  7526. (mapconcat
  7527. (lambda (x) (char-to-string (car x)))
  7528. org-remember-templates " "))
  7529. (cdr (assoc (read-char-exclusive) org-remember-templates))))
  7530. (tpl (if (consp (cdr entry)) (cadr entry) (cdr entry)))
  7531. (file (if (consp (cdr entry)) (nth 2 entry)))
  7532. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  7533. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  7534. (v-u (concat "[" (substring v-t 1 -1) "]"))
  7535. (v-U (concat "[" (substring v-T 1 -1) "]"))
  7536. (v-a annotation) ; defined in `remember-mode'
  7537. (v-i initial) ; defined in `remember-mode'
  7538. (v-n user-full-name)
  7539. )
  7540. (unless tpl (setq tpl "") (message "No template") (ding))
  7541. (insert tpl) (goto-char (point-min))
  7542. (while (re-search-forward "%\\([tTuTai]\\)" nil t)
  7543. (when (and initial (equal (match-string 0) "%i"))
  7544. (save-match-data
  7545. (let* ((lead (buffer-substring
  7546. (point-at-bol) (match-beginning 0))))
  7547. (setq v-i (mapconcat 'identity
  7548. (org-split-string initial "\n")
  7549. (concat "\n" lead))))))
  7550. (replace-match
  7551. (or (eval (intern (concat "v-" (match-string 1)))) "")
  7552. t t))
  7553. (let ((org-startup-folded nil)
  7554. (org-startup-with-deadline-check nil))
  7555. (org-mode))
  7556. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  7557. (set (make-local-variable 'org-default-notes-file) file))
  7558. (goto-char (point-min))
  7559. (if (re-search-forward "%\\?" nil t) (replace-match ""))
  7560. (set (make-local-variable 'org-finish-function) 'remember-buffer))))
  7561. ;;;###autoload
  7562. (defun org-remember-handler ()
  7563. "Store stuff from remember.el into an org file.
  7564. First prompts for an org file. If the user just presses return, the value
  7565. of `org-default-notes-file' is used.
  7566. Then the command offers the headings tree of the selected file in order to
  7567. file the text at a specific location.
  7568. You can either immediately press RET to get the note appended to the
  7569. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  7570. find a better place. Then press RET or <left> or <right> in insert the note.
  7571. Key Cursor position Note gets inserted
  7572. -----------------------------------------------------------------------------
  7573. RET buffer-start as level 2 heading at end of file
  7574. RET on headline as sublevel of the heading at cursor
  7575. RET no heading at cursor position, level taken from context.
  7576. Or use prefix arg to specify level manually.
  7577. <left> on headline as same level, before current heading
  7578. <right> on headline as same level, after current heading
  7579. So the fastest way to store the note is to press RET RET to append it to
  7580. the default file. This way your current train of thought is not
  7581. interrupted, in accordance with the principles of remember.el. But with
  7582. little extra effort, you can push it directly to the correct location.
  7583. Before being stored away, the function ensures that the text has a
  7584. headline, i.e. a first line that starts with a \"*\". If not, a headline
  7585. is constructed from the current date and some additional data.
  7586. If the variable `org-adapt-indentation' is non-nil, the entire text is
  7587. also indented so that it starts in the same column as the headline
  7588. \(i.e. after the stars).
  7589. See also the variable `org-reverse-note-order'."
  7590. (catch 'quit
  7591. (let* ((txt (buffer-substring (point-min) (point-max)))
  7592. (fastp current-prefix-arg)
  7593. (file (if fastp org-default-notes-file (org-get-org-file)))
  7594. (visiting (find-buffer-visiting file))
  7595. (org-startup-with-deadline-check nil)
  7596. (org-startup-folded nil)
  7597. (org-startup-align-all-tables nil)
  7598. spos level indent reversed)
  7599. ;; Modify text so that it becomes a nice subtree which can be inserted
  7600. ;; into an org tree.
  7601. (let* ((lines (split-string txt "\n"))
  7602. first)
  7603. ;; remove empty lines at the beginning
  7604. (while (and lines (string-match "^[ \t]*\n" (car lines)))
  7605. (setq lines (cdr lines)))
  7606. (setq first (car lines) lines (cdr lines))
  7607. (if (string-match "^\\*+" first)
  7608. ;; Is already a headline
  7609. (setq indent nil)
  7610. ;; We need to add a headline: Use time and first buffer line
  7611. (setq lines (cons first lines)
  7612. first (concat "* " (current-time-string)
  7613. " (" (remember-buffer-desc) ")")
  7614. indent " "))
  7615. (if (and org-adapt-indentation indent)
  7616. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  7617. (setq txt (concat first "\n"
  7618. (mapconcat 'identity lines "\n"))))
  7619. ;; Find the file
  7620. (if (not visiting)
  7621. (find-file-noselect file))
  7622. (with-current-buffer (get-file-buffer file)
  7623. (setq reversed (org-notes-order-reversed-p))
  7624. (save-excursion
  7625. (save-restriction
  7626. (widen)
  7627. ;; Ask the User for a location
  7628. (setq spos (if fastp 1 (org-get-location
  7629. (current-buffer)
  7630. org-remember-help)))
  7631. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  7632. ; not handle this note
  7633. (goto-char spos)
  7634. (cond ((bobp)
  7635. ;; Put it at the start or end, as level 2
  7636. (save-restriction
  7637. (widen)
  7638. (goto-char (if reversed (point-min) (point-max)))
  7639. (if (not (bolp)) (newline))
  7640. (org-paste-subtree 2 txt)))
  7641. ((and (org-on-heading-p nil) (not current-prefix-arg))
  7642. ;; Put it below this entry, at the beg/end of the subtree
  7643. (org-back-to-heading)
  7644. (setq level (funcall outline-level))
  7645. (if reversed
  7646. (outline-end-of-heading)
  7647. (outline-end-of-subtree))
  7648. (if (not (bolp)) (newline))
  7649. (beginning-of-line 1)
  7650. (org-paste-subtree (1+ level) txt))
  7651. (t
  7652. ;; Put it right there, with automatic level determined by
  7653. ;; org-paste-subtree or from prefix arg
  7654. (org-paste-subtree current-prefix-arg txt)))
  7655. (when remember-save-after-remembering
  7656. (save-buffer)
  7657. (if (not visiting) (kill-buffer (current-buffer)))))))))
  7658. t) ;; return t to indicate that we took care of this note.
  7659. (defun org-get-org-file ()
  7660. "Read a filename, with default directory `org-directory'."
  7661. (let ((default (or org-default-notes-file remember-data-file)))
  7662. (read-file-name (format "File name [%s]: " default)
  7663. (file-name-as-directory org-directory)
  7664. default)))
  7665. (defun org-notes-order-reversed-p ()
  7666. "Check if the current file should receive notes in reversed order."
  7667. (cond
  7668. ((not org-reverse-note-order) nil)
  7669. ((eq t org-reverse-note-order) t)
  7670. ((not (listp org-reverse-note-order)) nil)
  7671. (t (catch 'exit
  7672. (let ((all org-reverse-note-order)
  7673. entry)
  7674. (while (setq entry (pop all))
  7675. (if (string-match (car entry) buffer-file-name)
  7676. (throw 'exit (cdr entry))))
  7677. nil)))))
  7678. ;;; Tables
  7679. ;; Watch out: Here we are talking about two different kind of tables.
  7680. ;; Most of the code is for the tables created with the Org-mode table editor.
  7681. ;; Sometimes, we talk about tables created and edited with the table.el
  7682. ;; Emacs package. We call the former org-type tables, and the latter
  7683. ;; table.el-type tables.
  7684. (defun org-before-change-function (beg end)
  7685. "Every change indicates that a table might need an update."
  7686. (setq org-table-may-need-update t))
  7687. (defconst org-table-line-regexp "^[ \t]*|"
  7688. "Detects an org-type table line.")
  7689. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7690. "Detects an org-type table line.")
  7691. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7692. "Detects a table line marked for automatic recalculation.")
  7693. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7694. "Detects a table line marked for automatic recalculation.")
  7695. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7696. "Detects a table line marked for automatic recalculation.")
  7697. (defconst org-table-hline-regexp "^[ \t]*|-"
  7698. "Detects an org-type table hline.")
  7699. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7700. "Detects a table-type table hline.")
  7701. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7702. "Detects an org-type or table-type table.")
  7703. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7704. "Searching from within a table (any type) this finds the first line
  7705. outside the table.")
  7706. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7707. "Searching from within a table (any type) this finds the first line
  7708. outside the table.")
  7709. (defun org-table-create-with-table.el ()
  7710. "Use the table.el package to insert a new table.
  7711. If there is already a table at point, convert between Org-mode tables
  7712. and table.el tables."
  7713. (interactive)
  7714. (require 'table)
  7715. (cond
  7716. ((org-at-table.el-p)
  7717. (if (y-or-n-p "Convert table to Org-mode table? ")
  7718. (org-table-convert)))
  7719. ((org-at-table-p)
  7720. (if (y-or-n-p "Convert table to table.el table? ")
  7721. (org-table-convert)))
  7722. (t (call-interactively 'table-insert))))
  7723. (defun org-table-create (&optional size)
  7724. "Query for a size and insert a table skeleton.
  7725. SIZE is a string Columns x Rows like for example \"3x2\"."
  7726. (interactive "P")
  7727. (unless size
  7728. (setq size (read-string
  7729. (concat "Table size Columns x Rows [e.g. "
  7730. org-table-default-size "]: ")
  7731. "" nil org-table-default-size)))
  7732. (let* ((pos (point))
  7733. (indent (make-string (current-column) ?\ ))
  7734. (split (org-split-string size " *x *"))
  7735. (rows (string-to-number (nth 1 split)))
  7736. (columns (string-to-number (car split)))
  7737. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7738. "\n")))
  7739. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7740. (point-at-bol) (point)))
  7741. (beginning-of-line 1)
  7742. (newline))
  7743. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7744. (dotimes (i rows) (insert line))
  7745. (goto-char pos)
  7746. (if (> rows 1)
  7747. ;; Insert a hline after the first row.
  7748. (progn
  7749. (end-of-line 1)
  7750. (insert "\n|-")
  7751. (goto-char pos)))
  7752. (org-table-align)))
  7753. (defun org-table-convert-region (beg0 end0 nspace)
  7754. "Convert region to a table.
  7755. The region goes from BEG0 to END0, but these borders will be moved
  7756. slightly, to make sure a beginning of line in the first line is included.
  7757. When NSPACE is non-nil, it indicates the minimum number of spaces that
  7758. separate columns (default: just one space)."
  7759. (let* ((beg (min beg0 end0))
  7760. (end (max beg0 end0))
  7761. (tabsep t)
  7762. re)
  7763. (goto-char beg)
  7764. (beginning-of-line 1)
  7765. (setq beg (move-marker (make-marker) (point)))
  7766. (goto-char end)
  7767. (if (bolp) (backward-char 1) (end-of-line 1))
  7768. (setq end (move-marker (make-marker) (point)))
  7769. ;; Lets see if this is tab-separated material. If every nonempty line
  7770. ;; contains a tab, we will assume that it is tab-separated material
  7771. (if nspace
  7772. (setq tabsep nil)
  7773. (goto-char beg)
  7774. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  7775. (if nspace (setq tabsep nil))
  7776. (if tabsep
  7777. (setq re "^\\|\t")
  7778. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  7779. (max 1 (prefix-numeric-value nspace)))))
  7780. (goto-char beg)
  7781. (while (re-search-forward re end t)
  7782. (replace-match "|" t t))
  7783. (goto-char beg)
  7784. (insert " ")
  7785. (org-table-align)))
  7786. (defun org-table-import (file arg)
  7787. "Import FILE as a table.
  7788. The file is assumed to be tab-separated. Such files can be produced by most
  7789. spreadsheet and database applications. If no tabs (at least one per line)
  7790. are found, lines will be split on whitespace into fields."
  7791. (interactive "f\nP")
  7792. (or (bolp) (newline))
  7793. (let ((beg (point))
  7794. (pm (point-max)))
  7795. (insert-file-contents file)
  7796. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7797. (defun org-table-export ()
  7798. "Export table as a tab-separated file.
  7799. Such a file can be imported into a spreadsheet program like Excel."
  7800. (interactive)
  7801. (let* ((beg (org-table-begin))
  7802. (end (org-table-end))
  7803. (table (buffer-substring beg end))
  7804. (file (read-file-name "Export table to: "))
  7805. buf)
  7806. (unless (or (not (file-exists-p file))
  7807. (y-or-n-p (format "Overwrite file %s? " file)))
  7808. (error "Abort"))
  7809. (with-current-buffer (find-file-noselect file)
  7810. (setq buf (current-buffer))
  7811. (erase-buffer)
  7812. (fundamental-mode)
  7813. (insert table)
  7814. (goto-char (point-min))
  7815. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7816. (replace-match "" t t)
  7817. (end-of-line 1))
  7818. (goto-char (point-min))
  7819. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7820. (replace-match "" t t)
  7821. (goto-char (min (1+ (point)) (point-max))))
  7822. (goto-char (point-min))
  7823. (while (re-search-forward "^-[-+]*$" nil t)
  7824. (replace-match "")
  7825. (if (looking-at "\n")
  7826. (delete-char 1)))
  7827. (goto-char (point-min))
  7828. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7829. (replace-match "\t" t t))
  7830. (save-buffer))
  7831. (kill-buffer buf)))
  7832. (defvar org-table-aligned-begin-marker (make-marker)
  7833. "Marker at the beginning of the table last aligned.
  7834. Used to check if cursor still is in that table, to minimize realignment.")
  7835. (defvar org-table-aligned-end-marker (make-marker)
  7836. "Marker at the end of the table last aligned.
  7837. Used to check if cursor still is in that table, to minimize realignment.")
  7838. (defvar org-table-last-alignment nil
  7839. "List of flags for flushright alignment, from the last re-alignment.
  7840. This is being used to correctly align a single field after TAB or RET.")
  7841. (defvar org-table-last-column-widths nil
  7842. "List of max width of fields in each column.
  7843. This is being used to correctly align a single field after TAB or RET.")
  7844. (defvar org-last-recalc-line nil)
  7845. (defconst org-narrow-column-arrow "=>"
  7846. "Used as display property in narrowed table columns.")
  7847. (defun org-table-align ()
  7848. "Align the table at point by aligning all vertical bars."
  7849. (interactive)
  7850. (let* (
  7851. ;; Limits of table
  7852. (beg (org-table-begin))
  7853. (end (org-table-end))
  7854. ;; Current cursor position
  7855. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  7856. (colpos (org-table-current-column))
  7857. (winstart (window-start))
  7858. text lines (new "") lengths l typenums ty fields maxfields i
  7859. column
  7860. (indent "") cnt frac
  7861. rfmt hfmt
  7862. (spaces (if (org-in-invisibility-spec-p '(org-table))
  7863. org-table-spaces-around-invisible-separators
  7864. org-table-spaces-around-separators))
  7865. (sp1 (car spaces))
  7866. (sp2 (cdr spaces))
  7867. (rfmt1 (concat
  7868. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7869. (hfmt1 (concat
  7870. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7871. emptystrings xx links narrow fmax fmin f1 len c e)
  7872. (untabify beg end)
  7873. (remove-text-properties beg end '(org-cwidth t display t))
  7874. ;; Check if we have links
  7875. (goto-char beg)
  7876. (setq links (re-search-forward org-bracket-link-regexp end t))
  7877. ;; Make sure the link properties are right FIXME: Can this be optimized????
  7878. (when links (goto-char beg) (while (org-activate-links2 end)))
  7879. ;; Check if we are narrowing any columns
  7880. (goto-char beg)
  7881. (setq narrow (and org-format-transports-properties-p
  7882. (re-search-forward "<[0-9]+>" end t)))
  7883. ;; Get the rows
  7884. (setq lines (org-split-string
  7885. (buffer-substring beg end) "\n"))
  7886. ;; Store the indentation of the first line
  7887. (if (string-match "^ *" (car lines))
  7888. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7889. ;; Mark the hlines by setting the corresponding element to nil
  7890. ;; At the same time, we remove trailing space.
  7891. (setq lines (mapcar (lambda (l)
  7892. (if (string-match "^ *|-" l)
  7893. nil
  7894. (if (string-match "[ \t]+$" l)
  7895. (substring l 0 (match-beginning 0))
  7896. l)))
  7897. lines))
  7898. ;; Get the data fields by splitting the lines.
  7899. (setq fields (mapcar
  7900. (lambda (l)
  7901. (org-split-string l " *| *"))
  7902. (delq nil (copy-sequence lines))))
  7903. ;; How many fields in the longest line?
  7904. (condition-case nil
  7905. (setq maxfields (apply 'max (mapcar 'length fields)))
  7906. (error
  7907. (kill-region beg end)
  7908. (org-table-create org-table-default-size)
  7909. (error "Empty table - created default table")))
  7910. ;; A list of empty string to fill any short rows on output
  7911. (setq emptystrings (make-list maxfields ""))
  7912. ;; Check for special formatting.
  7913. (setq i -1)
  7914. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7915. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7916. ;; Check if there is an explicit width specified
  7917. (when (and org-table-limit-column-width narrow)
  7918. (setq c column fmax nil)
  7919. (while c
  7920. (setq e (pop c))
  7921. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7922. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7923. ;; Find fields that are wider than fmax, and shorten them
  7924. (when fmax
  7925. (loop for xx in column do
  7926. (when (and (stringp xx)
  7927. (> (org-string-width xx) fmax))
  7928. (add-text-properties
  7929. 0 (length xx)
  7930. (list 'help-echo
  7931. (concat "Clipped table field, use C-c ` to edit. Full value is:\n"
  7932. (copy-sequence xx)))
  7933. xx)
  7934. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7935. (unless (> f1 1)
  7936. (error "Cannot narrow field starting with wide link \"%s\""
  7937. (match-string 0 xx)))
  7938. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7939. (add-text-properties (- f1 2) f1
  7940. (list 'display org-narrow-column-arrow)
  7941. xx)))))
  7942. ;; Get the maximum width for each column
  7943. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7944. ;; Get the fraction of numbers, to decide about alignment of the column
  7945. (setq cnt 0 frac 0.0)
  7946. (loop for x in column do
  7947. (if (equal x "")
  7948. nil
  7949. (setq frac ( / (+ (* frac cnt)
  7950. (if (string-match org-table-number-regexp x) 1 0))
  7951. (setq cnt (1+ cnt))))))
  7952. (push (>= frac org-table-number-fraction) typenums))
  7953. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7954. ;; Store the alignment of this table, for later editing of single fields
  7955. (setq org-table-last-alignment typenums
  7956. org-table-last-column-widths lengths)
  7957. ;; With invisible characters, `format' does not get the field width right
  7958. ;; So we need to make these fields wide by hand.
  7959. (when links
  7960. (loop for i from 0 upto (1- maxfields) do
  7961. (setq len (nth i lengths))
  7962. (loop for j from 0 upto (1- (length fields)) do
  7963. (setq c (nthcdr i (car (nthcdr j fields))))
  7964. (if (and (stringp (car c))
  7965. (string-match org-bracket-link-regexp (car c))
  7966. (< (org-string-width (car c)) len))
  7967. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7968. ;; Compute the formats needed for output of the table
  7969. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7970. (while (setq l (pop lengths))
  7971. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7972. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7973. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7974. (setq rfmt (concat rfmt "\n")
  7975. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7976. (setq new (mapconcat
  7977. (lambda (l)
  7978. (if l (apply 'format rfmt
  7979. (append (pop fields) emptystrings))
  7980. hfmt))
  7981. lines ""))
  7982. ;; Replace the old one
  7983. (delete-region beg end)
  7984. (move-marker end nil)
  7985. (move-marker org-table-aligned-begin-marker (point))
  7986. (insert new)
  7987. (move-marker org-table-aligned-end-marker (point))
  7988. (when (and orgtbl-mode (not (eq major-mode 'org-mode)))
  7989. (goto-char org-table-aligned-begin-marker)
  7990. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7991. ;; Try to move to the old location (approximately)
  7992. (goto-line linepos)
  7993. (set-window-start (selected-window) winstart 'noforce)
  7994. (org-table-goto-column colpos)
  7995. (setq org-table-may-need-update nil)
  7996. (if (org-in-invisibility-spec-p '(org-table))
  7997. (org-table-add-invisible-to-vertical-lines))
  7998. ))
  7999. (defun org-string-width (s)
  8000. "Compute width of string, ignoring invisible characters.
  8001. This ignores character with invisibility property `org-link', and also
  8002. characters with property `org-cwidth', because these will become invisible
  8003. upon the next fontification round."
  8004. (let (b)
  8005. (when (or (eq t buffer-invisibility-spec)
  8006. (assq 'org-link buffer-invisibility-spec))
  8007. (while (setq b (text-property-any 0 (length s)
  8008. 'invisible 'org-link s))
  8009. (setq s (concat (substring s 0 b)
  8010. (substring s (or (next-single-property-change
  8011. b 'invisible s) (length s)))))))
  8012. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  8013. (setq s (concat (substring s 0 b)
  8014. (substring s (or (next-single-property-change
  8015. b 'org-cwidth s) (length s))))))
  8016. (string-width s)))
  8017. (defun org-table-begin (&optional table-type)
  8018. "Find the beginning of the table and return its position.
  8019. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  8020. (save-excursion
  8021. (if (not (re-search-backward
  8022. (if table-type org-table-any-border-regexp
  8023. org-table-border-regexp)
  8024. nil t))
  8025. (error "Can't find beginning of table")
  8026. (goto-char (match-beginning 0))
  8027. (beginning-of-line 2)
  8028. (point))))
  8029. (defun org-table-end (&optional table-type)
  8030. "Find the end of the table and return its position.
  8031. With argument TABLE-TYPE, go to the end of a table.el-type table."
  8032. (save-excursion
  8033. (if (not (re-search-forward
  8034. (if table-type org-table-any-border-regexp
  8035. org-table-border-regexp)
  8036. nil t))
  8037. (goto-char (point-max))
  8038. (goto-char (match-beginning 0)))
  8039. (point-marker)))
  8040. (defun org-table-justify-field-maybe (&optional new)
  8041. "Justify the current field, text to left, number to right.
  8042. Optional argument NEW may specify text to replace the current field content."
  8043. (cond
  8044. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  8045. ((org-at-table-hline-p))
  8046. ((and (not new)
  8047. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  8048. (current-buffer)))
  8049. (< (point) org-table-aligned-begin-marker)
  8050. (>= (point) org-table-aligned-end-marker)))
  8051. ;; This is not the same table, force a full re-align
  8052. (setq org-table-may-need-update t))
  8053. (t ;; realign the current field, based on previous full realign
  8054. (let* ((pos (point)) s
  8055. (col (org-table-current-column))
  8056. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  8057. l f n o e)
  8058. (when (> col 0)
  8059. (skip-chars-backward "^|\n")
  8060. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  8061. (progn
  8062. (setq s (match-string 1)
  8063. o (match-string 0)
  8064. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  8065. e (not (= (match-beginning 2) (match-end 2))))
  8066. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  8067. l (if e "|" (setq org-table-may-need-update t) ""))
  8068. n (format f s t t))
  8069. (if new
  8070. (if (<= (length new) l)
  8071. (setq n (format f new t t)) ;; FIXME: why t t?????
  8072. (setq n (concat new "|") org-table-may-need-update t)))
  8073. (or (equal n o)
  8074. (let (org-table-may-need-update)
  8075. (replace-match n))))
  8076. (setq org-table-may-need-update t))
  8077. (goto-char pos))))))
  8078. (defun org-table-next-field ()
  8079. "Go to the next field in the current table, creating new lines as needed.
  8080. Before doing so, re-align the table if necessary."
  8081. (interactive)
  8082. (org-table-maybe-eval-formula)
  8083. (org-table-maybe-recalculate-line)
  8084. (if (and org-table-automatic-realign
  8085. org-table-may-need-update)
  8086. (org-table-align))
  8087. (let ((end (org-table-end)))
  8088. (if (org-at-table-hline-p)
  8089. (end-of-line 1))
  8090. (condition-case nil
  8091. (progn
  8092. (re-search-forward "|" end)
  8093. (if (looking-at "[ \t]*$")
  8094. (re-search-forward "|" end))
  8095. (if (and (looking-at "-")
  8096. org-table-tab-jumps-over-hlines
  8097. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  8098. (goto-char (match-beginning 1)))
  8099. (if (looking-at "-")
  8100. (progn
  8101. (beginning-of-line 0)
  8102. (org-table-insert-row 'below))
  8103. (if (looking-at " ") (forward-char 1))))
  8104. (error
  8105. (org-table-insert-row 'below)))))
  8106. (defun org-table-previous-field ()
  8107. "Go to the previous field in the table.
  8108. Before doing so, re-align the table if necessary."
  8109. (interactive)
  8110. (org-table-justify-field-maybe)
  8111. (org-table-maybe-recalculate-line)
  8112. (if (and org-table-automatic-realign
  8113. org-table-may-need-update)
  8114. (org-table-align))
  8115. (if (org-at-table-hline-p)
  8116. (end-of-line 1))
  8117. (re-search-backward "|" (org-table-begin))
  8118. (re-search-backward "|" (org-table-begin))
  8119. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  8120. (re-search-backward "|" (org-table-begin)))
  8121. (if (looking-at "| ?")
  8122. (goto-char (match-end 0))))
  8123. (defun org-table-next-row ()
  8124. "Go to the next row (same column) in the current table.
  8125. Before doing so, re-align the table if necessary."
  8126. (interactive)
  8127. (org-table-maybe-eval-formula)
  8128. (org-table-maybe-recalculate-line)
  8129. (if (or (looking-at "[ \t]*$")
  8130. (save-excursion (skip-chars-backward " \t") (bolp)))
  8131. (newline)
  8132. (if (and org-table-automatic-realign
  8133. org-table-may-need-update)
  8134. (org-table-align))
  8135. (let ((col (org-table-current-column)))
  8136. (beginning-of-line 2)
  8137. (if (or (not (org-at-table-p))
  8138. (org-at-table-hline-p))
  8139. (progn
  8140. (beginning-of-line 0)
  8141. (org-table-insert-row 'below)))
  8142. (org-table-goto-column col)
  8143. (skip-chars-backward "^|\n\r")
  8144. (if (looking-at " ") (forward-char 1)))))
  8145. (defun org-table-copy-down (n)
  8146. "Copy a field down in the current column.
  8147. If the field at the cursor is empty, copy into it the content of the nearest
  8148. non-empty field above. With argument N, use the Nth non-empty field.
  8149. If the current field is not empty, it is copied down to the next row, and
  8150. the cursor is moved with it. Therefore, repeating this command causes the
  8151. column to be filled row-by-row.
  8152. If the variable `org-table-copy-increment' is non-nil and the field is an
  8153. integer, it will be incremented while copying."
  8154. (interactive "p")
  8155. (let* ((colpos (org-table-current-column))
  8156. (field (org-table-get-field))
  8157. (non-empty (string-match "[^ \t]" field))
  8158. (beg (org-table-begin))
  8159. txt)
  8160. (org-table-check-inside-data-field)
  8161. (if non-empty
  8162. (progn
  8163. (setq txt (org-trim field))
  8164. (org-table-next-row)
  8165. (org-table-blank-field))
  8166. (save-excursion
  8167. (setq txt
  8168. (catch 'exit
  8169. (while (progn (beginning-of-line 1)
  8170. (re-search-backward org-table-dataline-regexp
  8171. beg t))
  8172. (org-table-goto-column colpos t)
  8173. (if (and (looking-at
  8174. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  8175. (= (setq n (1- n)) 0))
  8176. (throw 'exit (match-string 1))))))))
  8177. (if txt
  8178. (progn
  8179. (if (and org-table-copy-increment
  8180. (string-match "^[0-9]+$" txt))
  8181. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  8182. (insert txt)
  8183. (org-table-maybe-recalculate-line)
  8184. (org-table-align))
  8185. (error "No non-empty field found"))))
  8186. (defun org-table-check-inside-data-field ()
  8187. "Is point inside a table data field?
  8188. I.e. not on a hline or before the first or after the last column?
  8189. This actually throws an error, so it aborts the current command."
  8190. (if (or (not (org-at-table-p))
  8191. (= (org-table-current-column) 0)
  8192. (org-at-table-hline-p)
  8193. (looking-at "[ \t]*$"))
  8194. (error "Not in table data field")))
  8195. (defvar org-table-clip nil
  8196. "Clipboard for table regions.")
  8197. (defun org-table-blank-field ()
  8198. "Blank the current table field or active region."
  8199. (interactive)
  8200. (org-table-check-inside-data-field)
  8201. (if (and (interactive-p) (org-region-active-p))
  8202. (let (org-table-clip)
  8203. (org-table-cut-region (region-beginning) (region-end)))
  8204. (skip-chars-backward "^|")
  8205. (backward-char 1)
  8206. (if (looking-at "|[^|\n]+")
  8207. (let* ((pos (match-beginning 0))
  8208. (match (match-string 0))
  8209. (len (org-string-width match)))
  8210. (replace-match (concat "|" (make-string (1- len) ?\ )))
  8211. (goto-char (+ 2 pos))
  8212. (substring match 1)))))
  8213. (defun org-table-get-field (&optional n replace)
  8214. "Return the value of the field in column N of current row.
  8215. N defaults to current field.
  8216. If REPLACE is a string, replace field with this value. The return value
  8217. is always the old value."
  8218. (and n (org-table-goto-column n))
  8219. (skip-chars-backward "^|\n")
  8220. (backward-char 1)
  8221. (if (looking-at "|[^|\r\n]*")
  8222. (let* ((pos (match-beginning 0))
  8223. (val (buffer-substring (1+ pos) (match-end 0))))
  8224. (if replace
  8225. (replace-match (concat "|" replace)))
  8226. (goto-char (min (point-at-eol) (+ 2 pos)))
  8227. val)
  8228. (forward-char 1) ""))
  8229. (defun org-table-current-column ()
  8230. "Find out which column we are in.
  8231. When called interactively, column is also displayed in echo area."
  8232. (interactive)
  8233. (if (interactive-p) (org-table-check-inside-data-field))
  8234. (save-excursion
  8235. (let ((cnt 0) (pos (point)))
  8236. (beginning-of-line 1)
  8237. (while (search-forward "|" pos t)
  8238. (setq cnt (1+ cnt)))
  8239. (if (interactive-p) (message "This is table column %d" cnt))
  8240. cnt)))
  8241. (defun org-table-goto-column (n &optional on-delim force)
  8242. "Move the cursor to the Nth column in the current table line.
  8243. With optional argument ON-DELIM, stop with point before the left delimiter
  8244. of the field.
  8245. If there are less than N fields, just go to after the last delimiter.
  8246. However, when FORCE is non-nil, create new columns if necessary."
  8247. (interactive "p")
  8248. (let ((pos (point-at-eol)))
  8249. (beginning-of-line 1)
  8250. (when (> n 0)
  8251. (while (and (> (setq n (1- n)) -1)
  8252. (or (search-forward "|" pos t)
  8253. (and force
  8254. (progn (end-of-line 1)
  8255. (skip-chars-backward "^|")
  8256. (insert " | "))))))
  8257. ; (backward-char 2) t)))))
  8258. (when (and force (not (looking-at ".*|")))
  8259. (save-excursion (end-of-line 1) (insert " | ")))
  8260. (if on-delim
  8261. (backward-char 1)
  8262. (if (looking-at " ") (forward-char 1))))))
  8263. (defun org-at-table-p (&optional table-type)
  8264. "Return t if the cursor is inside an org-type table.
  8265. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8266. (if org-enable-table-editor
  8267. (save-excursion
  8268. (beginning-of-line 1)
  8269. (looking-at (if table-type org-table-any-line-regexp
  8270. org-table-line-regexp)))
  8271. nil))
  8272. (defun org-at-table.el-p ()
  8273. "Return t if and only if we are at a table.el table."
  8274. (and (org-at-table-p 'any)
  8275. (save-excursion
  8276. (goto-char (org-table-begin 'any))
  8277. (looking-at org-table1-hline-regexp))))
  8278. (defun org-table-recognize-table.el ()
  8279. "If there is a table.el table nearby, recognize it and move into it."
  8280. (if org-table-tab-recognizes-table.el
  8281. (if (org-at-table.el-p)
  8282. (progn
  8283. (beginning-of-line 1)
  8284. (if (looking-at org-table-dataline-regexp)
  8285. nil
  8286. (if (looking-at org-table1-hline-regexp)
  8287. (progn
  8288. (beginning-of-line 2)
  8289. (if (looking-at org-table-any-border-regexp)
  8290. (beginning-of-line -1)))))
  8291. (if (re-search-forward "|" (org-table-end t) t)
  8292. (progn
  8293. (require 'table)
  8294. (if (table--at-cell-p (point))
  8295. t
  8296. (message "recognizing table.el table...")
  8297. (table-recognize-table)
  8298. (message "recognizing table.el table...done")))
  8299. (error "This should not happen..."))
  8300. t)
  8301. nil)
  8302. nil))
  8303. (defun org-at-table-hline-p ()
  8304. "Return t if the cursor is inside a hline in a table."
  8305. (if org-enable-table-editor
  8306. (save-excursion
  8307. (beginning-of-line 1)
  8308. (looking-at org-table-hline-regexp))
  8309. nil))
  8310. (defun org-table-insert-column ()
  8311. "Insert a new column into the table."
  8312. (interactive)
  8313. (if (not (org-at-table-p))
  8314. (error "Not at a table"))
  8315. (org-table-find-dataline)
  8316. (let* ((col (max 1 (org-table-current-column)))
  8317. (beg (org-table-begin))
  8318. (end (org-table-end))
  8319. ;; Current cursor position
  8320. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  8321. (colpos col))
  8322. (goto-char beg)
  8323. (while (< (point) end)
  8324. (if (org-at-table-hline-p)
  8325. nil
  8326. (org-table-goto-column col t)
  8327. (insert "| "))
  8328. (beginning-of-line 2))
  8329. (move-marker end nil)
  8330. (goto-line linepos)
  8331. (org-table-goto-column colpos)
  8332. (org-table-align)
  8333. (org-table-modify-formulas 'insert col)))
  8334. (defun org-table-find-dataline ()
  8335. "Find a dataline in the current table, which is needed for column commands."
  8336. (if (and (org-at-table-p)
  8337. (not (org-at-table-hline-p)))
  8338. t
  8339. (let ((col (current-column))
  8340. (end (org-table-end)))
  8341. (move-to-column col)
  8342. (while (and (< (point) end)
  8343. (or (not (= (current-column) col))
  8344. (org-at-table-hline-p)))
  8345. (beginning-of-line 2)
  8346. (move-to-column col))
  8347. (if (and (org-at-table-p)
  8348. (not (org-at-table-hline-p)))
  8349. t
  8350. (error
  8351. "Please position cursor in a data line for column operations")))))
  8352. (defun org-table-delete-column ()
  8353. "Delete a column into the table."
  8354. (interactive)
  8355. (if (not (org-at-table-p))
  8356. (error "Not at a table"))
  8357. (org-table-find-dataline)
  8358. (org-table-check-inside-data-field)
  8359. (let* ((col (org-table-current-column))
  8360. (beg (org-table-begin))
  8361. (end (org-table-end))
  8362. ;; Current cursor position
  8363. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  8364. (colpos col))
  8365. (goto-char beg)
  8366. (while (< (point) end)
  8367. (if (org-at-table-hline-p)
  8368. nil
  8369. (org-table-goto-column col t)
  8370. (and (looking-at "|[^|\n]+|")
  8371. (replace-match "|")))
  8372. (beginning-of-line 2))
  8373. (move-marker end nil)
  8374. (goto-line linepos)
  8375. (org-table-goto-column colpos)
  8376. (org-table-align)
  8377. (org-table-modify-formulas 'remove col)))
  8378. (defun org-table-move-column-right ()
  8379. "Move column to the right."
  8380. (interactive)
  8381. (org-table-move-column nil))
  8382. (defun org-table-move-column-left ()
  8383. "Move column to the left."
  8384. (interactive)
  8385. (org-table-move-column 'left))
  8386. (defun org-table-move-column (&optional left)
  8387. "Move the current column to the right. With arg LEFT, move to the left."
  8388. (interactive "P")
  8389. (if (not (org-at-table-p))
  8390. (error "Not at a table"))
  8391. (org-table-find-dataline)
  8392. (org-table-check-inside-data-field)
  8393. (let* ((col (org-table-current-column))
  8394. (col1 (if left (1- col) col))
  8395. (beg (org-table-begin))
  8396. (end (org-table-end))
  8397. ;; Current cursor position
  8398. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  8399. (colpos (if left (1- col) (1+ col))))
  8400. (if (and left (= col 1))
  8401. (error "Cannot move column further left"))
  8402. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8403. (error "Cannot move column further right"))
  8404. (goto-char beg)
  8405. (while (< (point) end)
  8406. (if (org-at-table-hline-p)
  8407. nil
  8408. (org-table-goto-column col1 t)
  8409. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8410. (replace-match "|\\2|\\1|")))
  8411. (beginning-of-line 2))
  8412. (move-marker end nil)
  8413. (goto-line linepos)
  8414. (org-table-goto-column colpos)
  8415. (org-table-align)
  8416. (org-table-modify-formulas 'swap col (if left (1- col) (1+ col)))))
  8417. (defun org-table-move-row-down ()
  8418. "Move table row down."
  8419. (interactive)
  8420. (org-table-move-row nil))
  8421. (defun org-table-move-row-up ()
  8422. "Move table row up."
  8423. (interactive)
  8424. (org-table-move-row 'up))
  8425. (defun org-table-move-row (&optional up)
  8426. "Move the current table line down. With arg UP, move it up."
  8427. (interactive "P")
  8428. (let ((col (current-column))
  8429. (pos (point))
  8430. (tonew (if up 0 2))
  8431. txt)
  8432. (beginning-of-line tonew)
  8433. (if (not (org-at-table-p))
  8434. (progn
  8435. (goto-char pos)
  8436. (error "Cannot move row further")))
  8437. (goto-char pos)
  8438. (beginning-of-line 1)
  8439. (setq pos (point))
  8440. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8441. (delete-region (point) (1+ (point-at-eol)))
  8442. (beginning-of-line tonew)
  8443. (insert txt)
  8444. (beginning-of-line 0)
  8445. (move-to-column col)))
  8446. (defun org-table-insert-row (&optional arg)
  8447. "Insert a new row above the current line into the table.
  8448. With prefix ARG, insert below the current line."
  8449. (interactive "P")
  8450. (if (not (org-at-table-p))
  8451. (error "Not at a table"))
  8452. (let* ((line
  8453. (org-expand-wide-chars
  8454. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  8455. new)
  8456. (if (string-match "^[ \t]*|-" line)
  8457. (setq new (mapcar (lambda (x) (if (member x '(?| ?+)) ?| ?\ )) line))
  8458. (setq new (mapcar (lambda (x) (if (equal x ?|) ?| ?\ )) line)))
  8459. ;; Fix the first field if necessary
  8460. (setq new (concat new))
  8461. (if (string-match "^[ \t]*| *[#$] *|" line)
  8462. (setq new (replace-match (match-string 0 line) t t new)))
  8463. (beginning-of-line (if arg 2 1))
  8464. (let (org-table-may-need-update)
  8465. (insert-before-markers new)
  8466. (insert-before-markers "\n"))
  8467. (beginning-of-line 0)
  8468. (re-search-forward "| ?" (point-at-eol) t)
  8469. (and org-table-may-need-update (org-table-align))))
  8470. (defun org-table-insert-hline (&optional arg)
  8471. "Insert a horizontal-line below the current line into the table.
  8472. With prefix ARG, insert above the current line."
  8473. (interactive "P")
  8474. (if (not (org-at-table-p))
  8475. (error "Not at a table"))
  8476. (let ((line
  8477. (org-expand-wide-chars
  8478. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  8479. (col (current-column))
  8480. start)
  8481. (if (string-match "^[ \t]*|-" line)
  8482. (setq line
  8483. (mapcar (lambda (x) (if (member x '(?| ?+))
  8484. (prog1 (if start ?+ ?|) (setq start t))
  8485. (if start ?- ?\ )))
  8486. line))
  8487. (setq line
  8488. (mapcar (lambda (x) (if (equal x ?|)
  8489. (prog1 (if start ?+ ?|) (setq start t))
  8490. (if start ?- ?\ )))
  8491. line)))
  8492. (beginning-of-line (if arg 1 2))
  8493. (apply 'insert line)
  8494. (if (equal (char-before (point)) ?+)
  8495. (progn (backward-delete-char 1) (insert "|")))
  8496. (insert "\n")
  8497. (beginning-of-line (if arg 1 -1))
  8498. (move-to-column col)))
  8499. (defun org-expand-wide-chars (s)
  8500. "Expand wide characters to spaces."
  8501. (let (w a)
  8502. (mapconcat
  8503. (lambda (x)
  8504. (if (> (setq w (string-width (setq a (char-to-string x)))) 1)
  8505. (make-string w ?\ )
  8506. a))
  8507. s "")))
  8508. (defun org-table-kill-row ()
  8509. "Delete the current row or horizontal line from the table."
  8510. (interactive)
  8511. (if (not (org-at-table-p))
  8512. (error "Not at a table"))
  8513. (let ((col (current-column)))
  8514. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8515. (if (not (org-at-table-p)) (beginning-of-line 0))
  8516. (move-to-column col)))
  8517. (defun org-table-sort-lines (beg end numericp)
  8518. "Sort table lines in region.
  8519. Point and mark define the first and last line to include. Both point and
  8520. mark should be in the column that is used for sorting. For example, to
  8521. sort according to column 3, put the mark in the first line to sort, in
  8522. table column 3. Put point into the last line to be included in the sorting,
  8523. also in table column 3. The command will prompt for the sorting method
  8524. \(n for numerical, a for alphanumeric)."
  8525. (interactive "r\nsSorting method: [n]=numeric [a]=alpha: ")
  8526. (setq numericp (string-match "[nN]" numericp))
  8527. (org-table-align) ;; Just to be safe
  8528. (let* (bcol ecol cmp column lns)
  8529. (goto-char beg)
  8530. (org-table-check-inside-data-field)
  8531. (setq column (org-table-current-column)
  8532. beg (move-marker (make-marker) (point-at-bol)))
  8533. (goto-char end)
  8534. (org-table-check-inside-data-field)
  8535. (setq end (move-marker (make-marker) (1+ (point-at-eol))))
  8536. (untabify beg end)
  8537. (goto-char beg)
  8538. (org-table-goto-column column)
  8539. (skip-chars-backward "^|")
  8540. (setq bcol (current-column))
  8541. (org-table-goto-column (1+ column))
  8542. (skip-chars-backward "^|")
  8543. (setq ecol (1- (current-column)))
  8544. (setq cmp (if numericp
  8545. (lambda (a b) (< (car a) (car b)))
  8546. (lambda (a b) (string< (car a) (car b)))))
  8547. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  8548. (split-string (buffer-substring beg end) "\n")))
  8549. (if numericp
  8550. (setq lns (mapcar (lambda(x)
  8551. (cons (string-to-number (car x)) (cdr x)))
  8552. lns)))
  8553. (delete-region beg end)
  8554. (move-marker beg nil)
  8555. (move-marker end nil)
  8556. (insert (mapconcat 'cdr (setq lns (sort lns cmp)) "\n") "\n")
  8557. (message "%d lines sorted %s based on column %d"
  8558. (length lns)
  8559. (if numericp "numerically" "alphabetically") column)))
  8560. (defun org-table-cut-region (beg end)
  8561. "Copy region in table to the clipboard and blank all relevant fields."
  8562. (interactive "r")
  8563. (org-table-copy-region beg end 'cut))
  8564. (defun org-table-copy-region (beg end &optional cut)
  8565. "Copy rectangular region in table to clipboard.
  8566. A special clipboard is used which can only be accessed
  8567. with `org-table-paste-rectangle'."
  8568. (interactive "rP")
  8569. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8570. region cols
  8571. (rpl (if cut " " nil)))
  8572. (goto-char beg)
  8573. (org-table-check-inside-data-field)
  8574. (setq l01 (count-lines (point-min) (point))
  8575. c01 (org-table-current-column))
  8576. (goto-char end)
  8577. (org-table-check-inside-data-field)
  8578. (setq l02 (count-lines (point-min) (point))
  8579. c02 (org-table-current-column))
  8580. (setq l1 (min l01 l02) l2 (max l01 l02)
  8581. c1 (min c01 c02) c2 (max c01 c02))
  8582. (catch 'exit
  8583. (while t
  8584. (catch 'nextline
  8585. (if (> l1 l2) (throw 'exit t))
  8586. (goto-line l1)
  8587. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8588. (setq cols nil ic1 c1 ic2 c2)
  8589. (while (< ic1 (1+ ic2))
  8590. (push (org-table-get-field ic1 rpl) cols)
  8591. (setq ic1 (1+ ic1)))
  8592. (push (nreverse cols) region)
  8593. (setq l1 (1+ l1)))))
  8594. (setq org-table-clip (nreverse region))
  8595. (if cut (org-table-align))
  8596. org-table-clip))
  8597. (defun org-table-paste-rectangle ()
  8598. "Paste a rectangular region into a table.
  8599. The upper right corner ends up in the current field. All involved fields
  8600. will be overwritten. If the rectangle does not fit into the present table,
  8601. the table is enlarged as needed. The process ignores horizontal separator
  8602. lines."
  8603. (interactive)
  8604. (unless (and org-table-clip (listp org-table-clip))
  8605. (error "First cut/copy a region to paste!"))
  8606. (org-table-check-inside-data-field)
  8607. (let* ((clip org-table-clip)
  8608. (line (count-lines (point-min) (point)))
  8609. (col (org-table-current-column))
  8610. (org-enable-table-editor t)
  8611. (org-table-automatic-realign nil)
  8612. c cols field)
  8613. (while (setq cols (pop clip))
  8614. (while (org-at-table-hline-p) (beginning-of-line 2))
  8615. (if (not (org-at-table-p))
  8616. (progn (end-of-line 0) (org-table-next-field)))
  8617. (setq c col)
  8618. (while (setq field (pop cols))
  8619. (org-table-goto-column c nil 'force)
  8620. (org-table-get-field nil field)
  8621. (setq c (1+ c)))
  8622. (beginning-of-line 2))
  8623. (goto-line line)
  8624. (org-table-goto-column col)
  8625. (org-table-align)))
  8626. (defun org-table-convert ()
  8627. "Convert from `org-mode' table to table.el and back.
  8628. Obviously, this only works within limits. When an Org-mode table is
  8629. converted to table.el, all horizontal separator lines get lost, because
  8630. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8631. A table.el table can be converted to an Org-mode table only if it does not
  8632. do row or column spanning. Multiline cells will become multiple cells.
  8633. Beware, Org-mode does not test if the table can be successfully converted - it
  8634. blindly applies a recipe that works for simple tables."
  8635. (interactive)
  8636. (require 'table)
  8637. (if (org-at-table.el-p)
  8638. ;; convert to Org-mode table
  8639. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8640. (end (move-marker (make-marker) (org-table-end t))))
  8641. (table-unrecognize-region beg end)
  8642. (goto-char beg)
  8643. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8644. (replace-match ""))
  8645. (goto-char beg))
  8646. (if (org-at-table-p)
  8647. ;; convert to table.el table
  8648. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8649. (end (move-marker (make-marker) (org-table-end))))
  8650. ;; first, get rid of all horizontal lines
  8651. (goto-char beg)
  8652. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8653. (replace-match ""))
  8654. ;; insert a hline before first
  8655. (goto-char beg)
  8656. (org-table-insert-hline 'above)
  8657. (beginning-of-line -1)
  8658. ;; insert a hline after each line
  8659. (while (progn (beginning-of-line 3) (< (point) end))
  8660. (org-table-insert-hline))
  8661. (goto-char beg)
  8662. (setq end (move-marker end (org-table-end)))
  8663. ;; replace "+" at beginning and ending of hlines
  8664. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8665. (replace-match "\\1+-"))
  8666. (goto-char beg)
  8667. (while (re-search-forward "-|[ \t]*$" end t)
  8668. (replace-match "-+"))
  8669. (goto-char beg)))))
  8670. (defun org-table-wrap-region (arg)
  8671. "Wrap several fields in a column like a paragraph.
  8672. This is useful if you'd like to spread the contents of a field over several
  8673. lines, in order to keep the table compact.
  8674. If there is an active region, and both point and mark are in the same column,
  8675. the text in the column is wrapped to minimum width for the given number of
  8676. lines. Generally, this makes the table more compact. A prefix ARG may be
  8677. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8678. formats the selected text to two lines. If the region was longer than two
  8679. lines, the remaining lines remain empty. A negative prefix argument reduces
  8680. the current number of lines by that amount. The wrapped text is pasted back
  8681. into the table. If you formatted it to more lines than it was before, fields
  8682. further down in the table get overwritten - so you might need to make space in
  8683. the table first.
  8684. If there is no region, the current field is split at the cursor position and
  8685. the text fragment to the right of the cursor is prepended to the field one
  8686. line down.
  8687. If there is no region, but you specify a prefix ARG, the current field gets
  8688. blank, and the content is appended to the field above."
  8689. (interactive "P")
  8690. (org-table-check-inside-data-field)
  8691. (if (org-region-active-p)
  8692. ;; There is a region: fill as a paragraph
  8693. (let ((beg (region-beginning))
  8694. nlines)
  8695. (org-table-cut-region (region-beginning) (region-end))
  8696. (if (> (length (car org-table-clip)) 1)
  8697. (error "Region must be limited to single column"))
  8698. (setq nlines (if arg
  8699. (if (< arg 1)
  8700. (+ (length org-table-clip) arg)
  8701. arg)
  8702. (length org-table-clip)))
  8703. (setq org-table-clip
  8704. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8705. nil nlines)))
  8706. (goto-char beg)
  8707. (org-table-paste-rectangle))
  8708. ;; No region, split the current field at point
  8709. (if arg
  8710. ;; combine with field above
  8711. (let ((s (org-table-blank-field))
  8712. (col (org-table-current-column)))
  8713. (beginning-of-line 0)
  8714. (while (org-at-table-hline-p) (beginning-of-line 0))
  8715. (org-table-goto-column col)
  8716. (skip-chars-forward "^|")
  8717. (skip-chars-backward " ")
  8718. (insert " " (org-trim s))
  8719. (org-table-align))
  8720. ;; split field
  8721. (when (looking-at "\\([^|]+\\)+|")
  8722. (let ((s (match-string 1)))
  8723. (replace-match " |")
  8724. (goto-char (match-beginning 0))
  8725. (org-table-next-row)
  8726. (insert (org-trim s) " ")
  8727. (org-table-align))))))
  8728. (defvar org-field-marker nil)
  8729. (defun org-table-edit-field (arg)
  8730. "Edit table field in a different window.
  8731. This is mainly useful for fields that contain hidden parts.
  8732. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8733. it can be edited in place."
  8734. (interactive "P")
  8735. (if arg
  8736. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8737. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8738. (remove-text-properties b e '(org-cwidth t invisible t
  8739. display t intangible t))
  8740. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8741. (font-lock-fontify-block)))
  8742. (let ((pos (move-marker (make-marker) (point)))
  8743. (field (org-table-get-field))
  8744. (cw (current-window-configuration))
  8745. p)
  8746. (switch-to-buffer-other-window "*Org tmp*")
  8747. (erase-buffer)
  8748. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8749. (org-mode)
  8750. (goto-char (setq p (point-max)))
  8751. (insert (org-trim field))
  8752. (remove-text-properties p (point-max)
  8753. '(invisible t org-cwidth t display t
  8754. intangible t))
  8755. (goto-char p)
  8756. (set (make-local-variable 'org-finish-function)
  8757. 'org-table-finish-edit-field)
  8758. (set (make-local-variable 'org-window-configuration) cw)
  8759. (set (make-local-variable 'org-field-marker) pos)
  8760. (message "Edit and finish with C-c C-c"))))
  8761. (defun org-table-finish-edit-field ()
  8762. "Finish editing a table data field.
  8763. Remove all newline characters, insert the result into the table, realign
  8764. the table and kill the editing buffer."
  8765. (let ((pos org-field-marker)
  8766. (cw org-window-configuration)
  8767. (cb (current-buffer))
  8768. text)
  8769. (goto-char (point-min))
  8770. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8771. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8772. (replace-match " "))
  8773. (setq text (org-trim (buffer-string)))
  8774. (set-window-configuration cw)
  8775. (kill-buffer cb)
  8776. (select-window (get-buffer-window (marker-buffer pos)))
  8777. (goto-char pos)
  8778. (move-marker pos nil)
  8779. (org-table-check-inside-data-field)
  8780. (org-table-get-field nil text)
  8781. (org-table-align)
  8782. (message "New field value inserted")))
  8783. (defun org-trim (s)
  8784. "Remove whitespace at beginning and end of string."
  8785. (if (string-match "^[ \t]+" s) (setq s (replace-match "" t t s)))
  8786. (if (string-match "[ \t]+$" s) (setq s (replace-match "" t t s)))
  8787. s)
  8788. (defun org-wrap (string &optional width lines)
  8789. "Wrap string to either a number of lines, or a width in characters.
  8790. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8791. that costs. If there is a word longer than WIDTH, the text is actually
  8792. wrapped to the length of that word.
  8793. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8794. many lines, whatever width that takes.
  8795. The return value is a list of lines, without newlines at the end."
  8796. (let* ((words (org-split-string string "[ \t\n]+"))
  8797. (maxword (apply 'max (mapcar 'org-string-width words)))
  8798. w ll)
  8799. (cond (width
  8800. (org-do-wrap words (max maxword width)))
  8801. (lines
  8802. (setq w maxword)
  8803. (setq ll (org-do-wrap words maxword))
  8804. (if (<= (length ll) lines)
  8805. ll
  8806. (setq ll words)
  8807. (while (> (length ll) lines)
  8808. (setq w (1+ w))
  8809. (setq ll (org-do-wrap words w)))
  8810. ll))
  8811. (t (error "Cannot wrap this")))))
  8812. (defun org-do-wrap (words width)
  8813. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8814. (let (lines line)
  8815. (while words
  8816. (setq line (pop words))
  8817. (while (and words (< (+ (length line) (length (car words))) width))
  8818. (setq line (concat line " " (pop words))))
  8819. (setq lines (push line lines)))
  8820. (nreverse lines)))
  8821. (defun org-split-string (string &optional separators)
  8822. "Splits STRING into substrings at SEPARATORS.
  8823. No empty strings are returned if there are matches at the beginning
  8824. and end of string."
  8825. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8826. (start 0)
  8827. notfirst
  8828. (list nil))
  8829. (while (and (string-match rexp string
  8830. (if (and notfirst
  8831. (= start (match-beginning 0))
  8832. (< start (length string)))
  8833. (1+ start) start))
  8834. (< (match-beginning 0) (length string)))
  8835. (setq notfirst t)
  8836. (or (eq (match-beginning 0) 0)
  8837. (and (eq (match-beginning 0) (match-end 0))
  8838. (eq (match-beginning 0) start))
  8839. (setq list
  8840. (cons (substring string start (match-beginning 0))
  8841. list)))
  8842. (setq start (match-end 0)))
  8843. (or (eq start (length string))
  8844. (setq list
  8845. (cons (substring string start)
  8846. list)))
  8847. (nreverse list)))
  8848. (defun org-table-add-invisible-to-vertical-lines ()
  8849. "Add an `invisible' property to vertical lines of current table."
  8850. (interactive)
  8851. (let* ((beg (org-table-begin))
  8852. (end (org-table-end))
  8853. (end1))
  8854. (save-excursion
  8855. (goto-char beg)
  8856. (while (< (point) end)
  8857. (setq end1 (point-at-eol))
  8858. (if (looking-at org-table-dataline-regexp)
  8859. (while (re-search-forward "|" end1 t)
  8860. (add-text-properties (1- (point)) (point)
  8861. '(invisible org-table)))
  8862. (while (re-search-forward "[+|]" end1 t)
  8863. (add-text-properties (1- (point)) (point)
  8864. '(invisible org-table))))
  8865. (beginning-of-line 2)))))
  8866. (defun org-table-toggle-vline-visibility (&optional arg)
  8867. "Toggle the visibility of table vertical lines.
  8868. The effect is immediate and on all tables in the file.
  8869. With prefix ARG, make lines invisible when ARG is positive, make lines
  8870. visible when ARG is not positive."
  8871. (interactive "P")
  8872. (let ((action (cond
  8873. ((and arg (> (prefix-numeric-value arg) 0)) 'on)
  8874. ((and arg (< (prefix-numeric-value arg) 1)) 'off)
  8875. (t (if (org-in-invisibility-spec-p '(org-table))
  8876. 'off
  8877. 'on)))))
  8878. (if (eq action 'off)
  8879. (progn
  8880. (org-remove-from-invisibility-spec '(org-table))
  8881. (org-table-map-tables 'org-table-align)
  8882. (message "Vertical table lines visible")
  8883. (if (org-at-table-p)
  8884. (org-table-align)))
  8885. (org-add-to-invisibility-spec '(org-table))
  8886. (org-table-map-tables 'org-table-align)
  8887. (message "Vertical table lines invisible"))
  8888. (redraw-frame (selected-frame))))
  8889. (defun org-table-map-tables (function)
  8890. "Apply FUNCTION to the start of all tables in the buffer."
  8891. (save-excursion
  8892. (save-restriction
  8893. (widen)
  8894. (goto-char (point-min))
  8895. (while (re-search-forward org-table-any-line-regexp nil t)
  8896. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8897. (beginning-of-line 1)
  8898. (if (looking-at org-table-line-regexp)
  8899. (save-excursion (funcall function)))
  8900. (re-search-forward org-table-any-border-regexp nil 1))))
  8901. (message "Mapping tables: done"))
  8902. (defun org-table-sum (&optional beg end nlast)
  8903. "Sum numbers in region of current table column.
  8904. The result will be displayed in the echo area, and will be available
  8905. as kill to be inserted with \\[yank].
  8906. If there is an active region, it is interpreted as a rectangle and all
  8907. numbers in that rectangle will be summed. If there is no active
  8908. region and point is located in a table column, sum all numbers in that
  8909. column.
  8910. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8911. numbers are assumed to be times as well (in decimal hours) and the
  8912. numbers are added as such.
  8913. If NLAST is a number, only the NLAST fields will actually be summed."
  8914. (interactive)
  8915. (save-excursion
  8916. (let (col (timecnt 0) diff h m s org-table-clip)
  8917. (cond
  8918. ((and beg end)) ; beg and end given explicitly
  8919. ((org-region-active-p)
  8920. (setq beg (region-beginning) end (region-end)))
  8921. (t
  8922. (setq col (org-table-current-column))
  8923. (goto-char (org-table-begin))
  8924. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8925. (error "No table data"))
  8926. (org-table-goto-column col)
  8927. ;not needed? (skip-chars-backward "^|")
  8928. (setq beg (point))
  8929. (goto-char (org-table-end))
  8930. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8931. (error "No table data"))
  8932. (org-table-goto-column col)
  8933. ;not needed? (skip-chars-forward "^|")
  8934. (setq end (point))))
  8935. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8936. (items1 (cond ((not nlast) items)
  8937. ((>= nlast (length items)) items)
  8938. (t (setq items (reverse items))
  8939. (setcdr (nthcdr (1- nlast) items) nil)
  8940. (nreverse items))))
  8941. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8942. items1)))
  8943. (res (apply '+ numbers))
  8944. (sres (if (= timecnt 0)
  8945. (format "%g" res)
  8946. (setq diff (* 3600 res)
  8947. h (floor (/ diff 3600)) diff (mod diff 3600)
  8948. m (floor (/ diff 60)) diff (mod diff 60)
  8949. s diff)
  8950. (format "%d:%02d:%02d" h m s))))
  8951. (kill-new sres)
  8952. (if (interactive-p)
  8953. (message "%s"
  8954. (substitute-command-keys
  8955. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8956. (length numbers) sres))))
  8957. sres))))
  8958. (defun org-table-get-number-for-summing (s)
  8959. (let (n)
  8960. (if (string-match "^ *|? *" s)
  8961. (setq s (replace-match "" nil nil s)))
  8962. (if (string-match " *|? *$" s)
  8963. (setq s (replace-match "" nil nil s)))
  8964. (setq n (string-to-number s))
  8965. (cond
  8966. ((and (string-match "0" s)
  8967. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8968. ((string-match "\\`[ \t]+\\'" s) nil)
  8969. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8970. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8971. (m (string-to-number (or (match-string 2 s) "0")))
  8972. (s (string-to-number (or (match-string 4 s) "0"))))
  8973. (if (boundp 'timecnt) (setq timecnt (1+ timecnt)))
  8974. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8975. ((equal n 0) nil)
  8976. (t n))))
  8977. (defun org-table-get-vertical-vector (desc &optional tbeg col)
  8978. "Get a calc vector from a column, accorting to descriptor DESC.
  8979. Optional arguments TBEG and COL can give the beginning of the table and
  8980. the current column, to avoid unnecessary parsing."
  8981. (save-excursion
  8982. (or tbeg (setq tbeg (org-table-begin)))
  8983. (or col (setq col (org-table-current-column)))
  8984. (let (beg end nn n n1 n2 l (thisline (org-current-line)) hline-list)
  8985. (cond
  8986. ((string-match "\\(I+\\)\\(-\\(I+\\)\\)?" desc)
  8987. (setq n1 (- (match-end 1) (match-beginning 1)))
  8988. (if (match-beginning 3)
  8989. (setq n2 (- (match-end 2) (match-beginning 3))))
  8990. (setq n (if n2 (max n1 n2) n1))
  8991. (setq n1 (if n2 (min n1 n2)))
  8992. (setq nn n)
  8993. (while (and (> nn 0)
  8994. (re-search-backward org-table-hline-regexp tbeg t))
  8995. (push (org-current-line) hline-list)
  8996. (setq nn (1- nn)))
  8997. (setq hline-list (nreverse hline-list))
  8998. (goto-line (nth (1- n) hline-list))
  8999. (when (re-search-forward org-table-dataline-regexp)
  9000. (org-table-goto-column col)
  9001. (setq beg (point)))
  9002. (goto-line (if n1 (nth (1- n1) hline-list) thisline))
  9003. (when (re-search-backward org-table-dataline-regexp)
  9004. (org-table-goto-column col)
  9005. (setq end (point)))
  9006. (setq l (apply 'append (org-table-copy-region beg end)))
  9007. (concat "[" (mapconcat (lambda (x) (setq x (org-trim x))
  9008. (if (equal x "") "0" x))
  9009. l ",") "]"))
  9010. ((string-match "\\([0-9]+\\)-\\([0-9]+\\)" desc)
  9011. (setq n1 (string-to-number (match-string 1 desc))
  9012. n2 (string-to-number (match-string 2 desc)))
  9013. (beginning-of-line 1)
  9014. (save-excursion
  9015. (when (re-search-backward org-table-dataline-regexp tbeg t n1)
  9016. (org-table-goto-column col)
  9017. (setq beg (point))))
  9018. (when (re-search-backward org-table-dataline-regexp tbeg t n2)
  9019. (org-table-goto-column col)
  9020. (setq end (point)))
  9021. (setq l (apply 'append (org-table-copy-region beg end)))
  9022. (concat "[" (mapconcat
  9023. (lambda (x) (setq x (org-trim x))
  9024. (if (equal x "") "0" x))
  9025. l ",") "]"))
  9026. ((string-match "\\([0-9]+\\)" desc)
  9027. (beginning-of-line 1)
  9028. (when (re-search-backward org-table-dataline-regexp tbeg t
  9029. (string-to-number (match-string 0 desc)))
  9030. (org-table-goto-column col)
  9031. (org-trim (org-table-get-field))))))))
  9032. (defvar org-table-formula-history nil)
  9033. (defvar org-table-column-names nil
  9034. "Alist with column names, derived from the `!' line.")
  9035. (defvar org-table-column-name-regexp nil
  9036. "Regular expression matching the current column names.")
  9037. (defvar org-table-local-parameters nil
  9038. "Alist with parameter names, derived from the `$' line.")
  9039. (defvar org-table-named-field-locations nil
  9040. "Alist with locations of named fields.")
  9041. (defun org-table-get-formula (&optional equation named)
  9042. "Read a formula from the minibuffer, offer stored formula as default."
  9043. (let* ((name (car (rassoc (list (org-current-line)
  9044. (org-table-current-column))
  9045. org-table-named-field-locations)))
  9046. (scol (if named
  9047. (if name name
  9048. (error "Not in a named field"))
  9049. (int-to-string (org-table-current-column))))
  9050. (dummy (and name (not named)
  9051. (not (y-or-n-p "Replace named-field formula with column equation? " ))
  9052. (error "Abort")))
  9053. (org-table-may-need-update nil)
  9054. (stored-list (org-table-get-stored-formulas))
  9055. (stored (cdr (assoc scol stored-list)))
  9056. (eq (cond
  9057. ((and stored equation (string-match "^ *=? *$" equation))
  9058. stored)
  9059. ((stringp equation)
  9060. equation)
  9061. (t (read-string
  9062. (format "%s formula $%s=" (if named "Field" "Column") scol)
  9063. (or stored "") 'org-table-formula-history
  9064. ;stored
  9065. ))))
  9066. mustsave)
  9067. (when (not (string-match "\\S-" eq))
  9068. ;; remove formula
  9069. (setq stored-list (delq (assoc scol stored-list) stored-list))
  9070. (org-table-store-formulas stored-list)
  9071. (error "Formula removed"))
  9072. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  9073. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  9074. (if (and name (not named))
  9075. ;; We set the column equation, delete the named one.
  9076. (setq stored-list (delq (assoc name stored-list) stored-list)
  9077. mustsave t))
  9078. (if stored
  9079. (setcdr (assoc scol stored-list) eq)
  9080. (setq stored-list (cons (cons scol eq) stored-list)))
  9081. (if (or mustsave (not (equal stored eq)))
  9082. (org-table-store-formulas stored-list))
  9083. eq))
  9084. (defun org-table-store-formulas (alist)
  9085. "Store the list of formulas below the current table."
  9086. (setq alist (sort alist (lambda (a b) (string< (car a) (car b)))))
  9087. (save-excursion
  9088. (goto-char (org-table-end))
  9089. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:.*\n?")
  9090. (delete-region (point) (match-end 0)))
  9091. (insert "#+TBLFM: "
  9092. (mapconcat (lambda (x)
  9093. (concat "$" (car x) "=" (cdr x)))
  9094. alist "::")
  9095. "\n")))
  9096. (defun org-table-get-stored-formulas ()
  9097. "Return an alist with the t=stored formulas directly after current table."
  9098. (interactive)
  9099. (let (scol eq eq-alist strings string seen)
  9100. (save-excursion
  9101. (goto-char (org-table-end))
  9102. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  9103. (setq strings (org-split-string (match-string 2) " *:: *"))
  9104. (while (setq string (pop strings))
  9105. (when (string-match "\\$\\([a-zA-Z0-9]+\\) *= *\\(.*[^ \t]\\)" string)
  9106. (setq scol (match-string 1 string)
  9107. eq (match-string 2 string)
  9108. eq-alist (cons (cons scol eq) eq-alist))
  9109. (if (member scol seen)
  9110. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  9111. (push scol seen))))))
  9112. (nreverse eq-alist)))
  9113. (defun org-table-modify-formulas (action &rest columns)
  9114. "Modify the formulas stored below the current table.
  9115. ACTION can be `remove', `insert', `swap'. For `swap', two column numbers are
  9116. expected, for the other actions only a single column number is needed."
  9117. (let ((list (org-table-get-stored-formulas))
  9118. (nmax (length (org-split-string
  9119. (buffer-substring (point-at-bol) (point-at-eol))
  9120. "|")))
  9121. col col1 col2 scol si sc1 sc2)
  9122. (cond
  9123. ((null list)) ; No action needed if there are no stored formulas
  9124. ((eq action 'remove)
  9125. (setq col (car columns)
  9126. scol (int-to-string col))
  9127. (org-table-replace-in-formulas list scol "INVALID")
  9128. (if (assoc scol list) (setq list (delq (assoc scol list) list)))
  9129. (loop for i from (1+ col) upto nmax by 1 do
  9130. (setq si (int-to-string i))
  9131. (org-table-replace-in-formulas list si (int-to-string (1- i)))
  9132. (if (assoc si list) (setcar (assoc si list)
  9133. (int-to-string (1- i))))))
  9134. ((eq action 'insert)
  9135. (setq col (car columns))
  9136. (loop for i from nmax downto col by 1 do
  9137. (setq si (int-to-string i))
  9138. (org-table-replace-in-formulas list si (int-to-string (1+ i)))
  9139. (if (assoc si list) (setcar (assoc si list)
  9140. (int-to-string (1+ i))))))
  9141. ((eq action 'swap)
  9142. (setq col1 (car columns) col2 (nth 1 columns)
  9143. sc1 (int-to-string col1) sc2 (int-to-string col2))
  9144. ;; Hopefully, ZqZtZ will never be a name in a table
  9145. (org-table-replace-in-formulas list sc1 "ZqZtZ")
  9146. (org-table-replace-in-formulas list sc2 sc1)
  9147. (org-table-replace-in-formulas list "ZqZtZ" sc2)
  9148. (if (assoc sc1 list) (setcar (assoc sc1 list) "ZqZtZ"))
  9149. (if (assoc sc2 list) (setcar (assoc sc2 list) sc1))
  9150. (if (assoc "ZqZtZ" list) (setcar (assoc "ZqZtZ" list) sc2)))
  9151. (t (error "Invalid action in `org-table-modify-formulas'")))
  9152. (if list (org-table-store-formulas list))))
  9153. (defun org-table-replace-in-formulas (list s1 s2)
  9154. (let (elt re s)
  9155. (setq s1 (concat "$" (if (integerp s1) (int-to-string s1) s1))
  9156. s2 (concat "$" (if (integerp s2) (int-to-string s2) s2))
  9157. re (concat (regexp-quote s1) "\\>"))
  9158. (while (setq elt (pop list))
  9159. (setq s (cdr elt))
  9160. (while (string-match re s)
  9161. (setq s (replace-match s2 t t s)))
  9162. (setcdr elt s))))
  9163. (defun org-table-get-specials ()
  9164. "Get the column names and local parameters for this table."
  9165. (save-excursion
  9166. (let ((beg (org-table-begin)) (end (org-table-end))
  9167. names name fields fields1 field cnt c v line col)
  9168. (setq org-table-column-names nil
  9169. org-table-local-parameters nil
  9170. org-table-named-field-locations nil)
  9171. (goto-char beg)
  9172. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  9173. (setq names (org-split-string (match-string 1) " *| *")
  9174. cnt 1)
  9175. (while (setq name (pop names))
  9176. (setq cnt (1+ cnt))
  9177. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  9178. (push (cons name (int-to-string cnt)) org-table-column-names))))
  9179. (setq org-table-column-names (nreverse org-table-column-names))
  9180. (setq org-table-column-name-regexp
  9181. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  9182. (goto-char beg)
  9183. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  9184. (setq fields (org-split-string (match-string 1) " *| *"))
  9185. (while (setq field (pop fields))
  9186. (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  9187. (push (cons (match-string 1 field) (match-string 2 field))
  9188. org-table-local-parameters))))
  9189. (goto-char beg)
  9190. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  9191. (setq c (match-string 1)
  9192. fields (org-split-string (match-string 2) " *| *"))
  9193. (save-excursion
  9194. (beginning-of-line (if (equal c "_") 2 0))
  9195. (setq line (org-current-line) col 1)
  9196. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  9197. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  9198. (while (and fields1 (setq field (pop fields)))
  9199. (setq v (pop fields1) col (1+ col))
  9200. (when (and (stringp field) (stringp v)
  9201. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  9202. (push (cons field v) org-table-local-parameters)
  9203. (push (list field line col) org-table-named-field-locations)))))))
  9204. (defun org-this-word ()
  9205. ;; Get the current word
  9206. (save-excursion
  9207. (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
  9208. (end (progn (skip-chars-forward "^ \t\n") (point))))
  9209. (buffer-substring-no-properties beg end))))
  9210. (defun org-table-maybe-eval-formula ()
  9211. "Check if the current field starts with \"=\" or \":=\".
  9212. If yes, store the formula and apply it."
  9213. ;; We already know we are in a table. Get field will only return a formula
  9214. ;; when appropriate. It might return a separator line, but no problem.
  9215. (when org-table-formula-evaluate-inline
  9216. (let* ((field (org-trim (or (org-table-get-field) "")))
  9217. named eq)
  9218. (when (string-match "^:?=\\(.*\\)" field)
  9219. (setq named (equal (string-to-char field) ?:)
  9220. eq (match-string 1 field))
  9221. (if (fboundp 'calc-eval)
  9222. (org-table-eval-formula (if named '(4) nil) eq))))))
  9223. (defvar org-recalc-commands nil
  9224. "List of commands triggering the recalculation of a line.
  9225. Will be filled automatically during use.")
  9226. (defvar org-recalc-marks
  9227. '((" " . "Unmarked: no special line, no automatic recalculation")
  9228. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9229. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9230. ("!" . "Column name definition line. Reference in formula as $name.")
  9231. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9232. ("_" . "Names for values in row below this one.")
  9233. ("^" . "Names for values in row above this one.")))
  9234. (defun org-table-rotate-recalc-marks (&optional newchar)
  9235. "Rotate the recalculation mark in the first column.
  9236. If in any row, the first field is not consistent with a mark,
  9237. insert a new column for the markers.
  9238. When there is an active region, change all the lines in the region,
  9239. after prompting for the marking character.
  9240. After each change, a message will be displayed indicating the meaning
  9241. of the new mark."
  9242. (interactive)
  9243. (unless (org-at-table-p) (error "Not at a table"))
  9244. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9245. (beg (org-table-begin))
  9246. (end (org-table-end))
  9247. (l (org-current-line))
  9248. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9249. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9250. (have-col
  9251. (save-excursion
  9252. (goto-char beg)
  9253. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9254. (col (org-table-current-column))
  9255. (forcenew (car (assoc newchar org-recalc-marks)))
  9256. epos new)
  9257. (when l1
  9258. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9259. (setq newchar (char-to-string (read-char-exclusive))
  9260. forcenew (car (assoc newchar org-recalc-marks))))
  9261. (if (and newchar (not forcenew))
  9262. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9263. newchar))
  9264. (if l1 (goto-line l1))
  9265. (save-excursion
  9266. (beginning-of-line 1)
  9267. (unless (looking-at org-table-dataline-regexp)
  9268. (error "Not at a table data line")))
  9269. (unless have-col
  9270. (org-table-goto-column 1)
  9271. (org-table-insert-column)
  9272. (org-table-goto-column (1+ col)))
  9273. (setq epos (point-at-eol))
  9274. (save-excursion
  9275. (beginning-of-line 1)
  9276. (org-table-get-field
  9277. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9278. (concat " "
  9279. (setq new (or forcenew
  9280. (cadr (member (match-string 1) marks))))
  9281. " ")
  9282. " # ")))
  9283. (if (and l1 l2)
  9284. (progn
  9285. (goto-line l1)
  9286. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9287. (and (looking-at org-table-dataline-regexp)
  9288. (org-table-get-field 1 (concat " " new " "))))
  9289. (goto-line l1)))
  9290. (if (not (= epos (point-at-eol))) (org-table-align))
  9291. (goto-line l)
  9292. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  9293. (defun org-table-maybe-recalculate-line ()
  9294. "Recompute the current line if marked for it, and if we haven't just done it."
  9295. (interactive)
  9296. (and org-table-allow-automatic-line-recalculation
  9297. (not (and (memq last-command org-recalc-commands)
  9298. (equal org-last-recalc-line (org-current-line))))
  9299. (save-excursion (beginning-of-line 1)
  9300. (looking-at org-table-auto-recalculate-regexp))
  9301. (fboundp 'calc-eval)
  9302. (org-table-recalculate) t))
  9303. (defvar org-table-formula-debug nil
  9304. "Non-nil means, debug table formulas.
  9305. When nil, simply write \"#ERROR\" in corrupted fields.")
  9306. (defvar modes)
  9307. (defsubst org-set-calc-mode (var &optional value)
  9308. (if (stringp var)
  9309. (setq var (assoc var '(("D" calc-angle-mode deg)
  9310. ("R" calc-angle-mode rad)
  9311. ("F" calc-prefer-frac t)
  9312. ("S" calc-symbolic-mode t)))
  9313. value (nth 2 var) var (nth 1 var)))
  9314. (if (memq var modes)
  9315. (setcar (cdr (memq var modes)) value)
  9316. (cons var (cons value modes)))
  9317. modes)
  9318. (defun org-table-eval-formula (&optional arg equation
  9319. suppress-align suppress-const
  9320. suppress-store)
  9321. "Replace the table field value at the cursor by the result of a calculation.
  9322. This function makes use of Dave Gillespie's Calc package, in my view the
  9323. most exciting program ever written for GNU Emacs. So you need to have Calc
  9324. installed in order to use this function.
  9325. In a table, this command replaces the value in the current field with the
  9326. result of a formula. It also installs the formula as the \"current\" column
  9327. formula, by storing it in a special line below the table. When called
  9328. with a `C-u' prefix, the current field must ba a named field, and the
  9329. formula is installed as valid in only this specific field.
  9330. When called, the command first prompts for a formula, which is read in
  9331. the minibuffer. Previously entered formulas are available through the
  9332. history list, and the last used formula is offered as a default.
  9333. These stored formulas are adapted correctly when moving, inserting, or
  9334. deleting columns with the corresponding commands.
  9335. The formula can be any algebraic expression understood by the Calc package.
  9336. For details, see the Org-mode manual.
  9337. This function can also be called from Lisp programs and offers
  9338. additional arguments: EQUATION can be the formula to apply. If this
  9339. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9340. used to speed-up recursive calls by by-passing unnecessary aligns.
  9341. SUPPRESS-CONST suppresses the interpretation of constants in the
  9342. formula, assuming that this has been done already outside the function.
  9343. SUPPRESS-STORE means the formula should not be stored, either because
  9344. it is already stored, or because it is a modified equation that should
  9345. not overwrite the stored one."
  9346. (interactive "P")
  9347. (require 'calc)
  9348. (org-table-check-inside-data-field)
  9349. (org-table-get-specials)
  9350. (let* (fields
  9351. (ndown (if (integerp arg) arg 1))
  9352. (org-table-automatic-realign nil)
  9353. (case-fold-search nil)
  9354. (down (> ndown 1))
  9355. (formula (if (and equation suppress-store)
  9356. equation
  9357. (org-table-get-formula equation (equal arg '(4)))))
  9358. (n0 (org-table-current-column))
  9359. (modes (copy-sequence org-calc-default-modes))
  9360. n form fmt x ev orig c)
  9361. ;; Parse the format string. Since we have a lot of modes, this is
  9362. ;; a lot of work. However, I think calc still uses most of the time.
  9363. (if (string-match ";" formula)
  9364. (let ((tmp (org-split-string formula ";")))
  9365. (setq formula (car tmp)
  9366. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9367. (nth 1 tmp)))
  9368. (while (string-match "[pnfse]\\(-?[0-9]+\\)" fmt)
  9369. (setq c (string-to-char (match-string 1 fmt))
  9370. n (string-to-number (or (match-string 1 fmt) "")))
  9371. (if (= c ?p) (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9372. (setq modes (org-set-calc-mode
  9373. 'calc-float-format
  9374. (list (cdr (assoc c '((?n . float) (?f . fix)
  9375. (?s . sci) (?e . eng))))
  9376. n))))
  9377. (setq fmt (replace-match "" t t fmt)))
  9378. (while (string-match "[DRFS]" fmt)
  9379. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9380. (setq fmt (replace-match "" t t fmt)))
  9381. (unless (string-match "\\S-" fmt)
  9382. (setq fmt nil))))
  9383. (if (and (not suppress-const) org-table-formula-use-constants)
  9384. (setq formula (org-table-formula-substitute-names formula)))
  9385. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9386. (while (> ndown 0)
  9387. (setq fields (org-split-string
  9388. (buffer-substring
  9389. (point-at-bol) (point-at-eol)) " *| *"))
  9390. (if org-table-formula-numbers-only
  9391. (setq fields (mapcar
  9392. (lambda (x) (number-to-string (string-to-number x)))
  9393. fields)))
  9394. (setq ndown (1- ndown))
  9395. (setq form (copy-sequence formula))
  9396. ;; Insert the references to fields in same row
  9397. (while (string-match "\\$\\([0-9]+\\)?" form)
  9398. (setq n (if (match-beginning 1)
  9399. (string-to-number (match-string 1 form))
  9400. n0)
  9401. x (nth (1- n) fields))
  9402. (unless x (error "Invalid field specifier \"%s\""
  9403. (match-string 0 form)))
  9404. (if (equal x "") (setq x "0"))
  9405. (setq form (replace-match (concat "(" x ")") t t form)))
  9406. ;; Insert ranges in current column
  9407. (while (string-match "\\&[-I0-9]+" form)
  9408. (setq form (replace-match
  9409. (save-match-data
  9410. (org-table-get-vertical-vector (match-string 0 form)
  9411. nil n0))
  9412. t t form)))
  9413. (setq ev (calc-eval (cons form modes)
  9414. (if org-table-formula-numbers-only 'num)))
  9415. (when org-table-formula-debug
  9416. (with-output-to-temp-buffer "*Help*"
  9417. (princ (format "Substitution history of formula
  9418. Orig: %s
  9419. $xyz-> %s
  9420. $1-> %s\n" orig formula form))
  9421. (if (listp ev)
  9422. (princ (format " %s^\nError: %s"
  9423. (make-string (car ev) ?\-) (nth 1 ev)))
  9424. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9425. ev (or fmt "NONE")
  9426. (if fmt (format fmt (string-to-number ev)) ev)))))
  9427. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  9428. (unless (and (interactive-p) (not ndown))
  9429. (unless (let (inhibit-redisplay)
  9430. (y-or-n-p "Debugging Formula. Continue to next? "))
  9431. (org-table-align)
  9432. (error "Abort"))
  9433. (delete-window (get-buffer-window "*Help*"))
  9434. (message "")))
  9435. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9436. (org-table-justify-field-maybe
  9437. (if fmt (format fmt (string-to-number ev)) ev))
  9438. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9439. (call-interactively 'org-return)
  9440. (setq ndown 0)))
  9441. (and down (org-table-maybe-recalculate-line))
  9442. (or suppress-align (and org-table-may-need-update
  9443. (org-table-align)))))
  9444. (defun org-table-recalculate (&optional all noalign)
  9445. "Recalculate the current table line by applying all stored formulas."
  9446. (interactive "P")
  9447. (or (memq this-command org-recalc-commands)
  9448. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9449. (unless (org-at-table-p) (error "Not at a table"))
  9450. (org-table-get-specials)
  9451. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9452. (lambda (a b) (string< (car a) (car b)))))
  9453. (inhibit-redisplay t)
  9454. (line-re org-table-dataline-regexp)
  9455. (thisline (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9456. (thiscol (org-table-current-column))
  9457. beg end entry eqlnum eqlname eql (cnt 0) eq a name)
  9458. ;; Insert constants in all formulas
  9459. (setq eqlist
  9460. (mapcar (lambda (x)
  9461. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9462. x)
  9463. eqlist))
  9464. ;; Split the equation list
  9465. (while (setq eq (pop eqlist))
  9466. (if (<= (string-to-char (car eq)) ?9)
  9467. (push eq eqlnum)
  9468. (push eq eqlname)))
  9469. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9470. (if all
  9471. (progn
  9472. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9473. (goto-char (setq beg (org-table-begin)))
  9474. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9475. ;; This is a table with marked lines, only compute selected lines
  9476. (setq line-re org-table-recalculate-regexp)
  9477. ;; Move forward to the first non-header line
  9478. (if (and (re-search-forward org-table-dataline-regexp end t)
  9479. (re-search-forward org-table-hline-regexp end t)
  9480. (re-search-forward org-table-dataline-regexp end t))
  9481. (setq beg (match-beginning 0))
  9482. nil))) ;; just leave beg where it is
  9483. (setq beg (point-at-bol)
  9484. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9485. (goto-char beg)
  9486. (and all (message "Re-applying formulas to full table..."))
  9487. (while (re-search-forward line-re end t)
  9488. (unless (string-match "^ *[_^!$] *$" (org-table-get-field 1))
  9489. ;; Unprotected line, recalculate
  9490. (and all (message "Re-applying formulas to full table...(line %d)"
  9491. (setq cnt (1+ cnt))))
  9492. (setq org-last-recalc-line (org-current-line))
  9493. (setq eql eqlnum)
  9494. (while (setq entry (pop eql))
  9495. (goto-line org-last-recalc-line)
  9496. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9497. (org-table-eval-formula nil (cdr entry) 'noalign 'nocst 'nostore))))
  9498. (goto-line thisline)
  9499. (org-table-goto-column thiscol)
  9500. (or noalign (and org-table-may-need-update (org-table-align))
  9501. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9502. ;; Now do the names fields
  9503. (while (setq eq (pop eqlname))
  9504. (setq name (car eq)
  9505. a (assoc name org-table-named-field-locations))
  9506. (when a
  9507. (message "Re-applying formula to named field: %s" name)
  9508. (goto-line (nth 1 a))
  9509. (org-table-goto-column (nth 2 a))
  9510. (org-table-eval-formula nil (cdr eq) 'noalign 'nocst 'nostore)))
  9511. ;; back to initial position
  9512. (goto-line thisline)
  9513. (org-table-goto-column thiscol)
  9514. (or noalign (and org-table-may-need-update (org-table-align))
  9515. (and all (message "Re-applying formulas...done")))))
  9516. (defun org-table-formula-substitute-names (f)
  9517. "Replace $const with values in string F."
  9518. (let ((start 0) a n1 n2 nn1 nn2 s (f1 f))
  9519. ;; First, check for column names
  9520. (while (setq start (string-match org-table-column-name-regexp f start))
  9521. (setq start (1+ start))
  9522. (setq a (assoc (match-string 1 f) org-table-column-names))
  9523. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9524. ;; Expand ranges to vectors
  9525. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\.?\\$\\([0-9]+\\)" f)
  9526. (setq n1 (string-to-number (match-string 1 f))
  9527. n2 (string-to-number (match-string 2 f))
  9528. nn1 (1+ (min n1 n2)) nn2 (max n1 n2)
  9529. s (concat "[($" (number-to-string (1- nn1)) ")"))
  9530. (loop for i from nn1 upto nn2 do
  9531. (setq s (concat s ",($" (int-to-string i) ")")))
  9532. (setq s (concat s "]"))
  9533. (if (< n2 n1) (setq s (concat "rev(" s ")")))
  9534. (setq f (replace-match s t t f)))
  9535. ;; Parameters and constants
  9536. (setq start 0)
  9537. (while (setq start (string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start))
  9538. (setq start (1+ start))
  9539. (if (setq a (save-match-data
  9540. (org-table-get-constant (match-string 1 f))))
  9541. (setq f (replace-match (concat "(" a ")") t t f))))
  9542. (if org-table-formula-debug
  9543. (put-text-property 0 (length f) :orig-formula f1 f))
  9544. f))
  9545. (defun org-table-get-constant (const)
  9546. "Find the value for a parameter or constant in a formula.
  9547. Parameters get priority."
  9548. (or (cdr (assoc const org-table-local-parameters))
  9549. (cdr (assoc const org-table-formula-constants))
  9550. (and (fboundp 'constants-get) (constants-get const))
  9551. "#UNDEFINED_NAME"))
  9552. (defvar org-edit-formulas-map (make-sparse-keymap))
  9553. (define-key org-edit-formulas-map "\C-c\C-c" 'org-finish-edit-formulas)
  9554. (define-key org-edit-formulas-map "\C-c\C-q" 'org-abort-edit-formulas)
  9555. (define-key org-edit-formulas-map "\C-c?" 'org-show-variable)
  9556. (defvar org-pos)
  9557. (defun org-table-edit-formulas ()
  9558. "Edit the formulas of the current table in a separate buffer."
  9559. (interactive)
  9560. (unless (org-at-table-p)
  9561. (error "Not at a table"))
  9562. (org-table-get-specials)
  9563. (let ((eql (org-table-get-stored-formulas))
  9564. (pos (move-marker (make-marker) (point)))
  9565. (wc (current-window-configuration))
  9566. entry loc s)
  9567. (switch-to-buffer-other-window "*Edit Formulas*")
  9568. (erase-buffer)
  9569. (fundamental-mode)
  9570. (set (make-local-variable 'org-pos) pos)
  9571. (set (make-local-variable 'org-window-configuration) wc)
  9572. (use-local-map org-edit-formulas-map)
  9573. (setq s "# Edit formulas and finish with `C-c C-c'.
  9574. # Use `C-u C-c C-c' to also appy them immediately to the entire table.
  9575. # Use `C-c ?' to get information about $name at point.
  9576. # To cancel editing, press `C-c C-q'.\n")
  9577. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  9578. (insert s)
  9579. (while (setq entry (pop eql))
  9580. (when (setq loc (assoc (car entry) org-table-named-field-locations))
  9581. (setq s (format "# Named formula, referring to column %d in line %d\n"
  9582. (nth 2 loc) (nth 1 loc)))
  9583. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  9584. (insert s))
  9585. (setq s (concat "$" (car entry) "=" (cdr entry) "\n"))
  9586. (remove-text-properties 0 (length s) '(face nil) s)
  9587. (insert s))
  9588. (goto-char (point-min))
  9589. (message "Edit formulas and finish with `C-c C-c'.")))
  9590. (defun org-show-variable ()
  9591. "Show the location/value of the $ expression at point."
  9592. (interactive)
  9593. (let (var (pos org-pos) (win (selected-window)) e)
  9594. (save-excursion
  9595. (or (looking-at "\\$") (skip-chars-backward "$a-zA-Z0-9"))
  9596. (if (looking-at "\\$\\([a-zA-Z0-9]+\\)")
  9597. (setq var (match-string 1))
  9598. (error "No variable at point")))
  9599. (cond
  9600. ((setq e (assoc var org-table-named-field-locations))
  9601. (switch-to-buffer-other-window (marker-buffer pos))
  9602. (goto-line (nth 1 e))
  9603. (org-table-goto-column (nth 2 e))
  9604. (select-window win)
  9605. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  9606. ((setq e (assoc var org-table-column-names))
  9607. (switch-to-buffer-other-window (marker-buffer pos))
  9608. (goto-char pos)
  9609. (goto-char (org-table-begin))
  9610. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  9611. (org-table-end) t)
  9612. (progn
  9613. (goto-char (match-beginning 1))
  9614. (message "Named column (column %s)" (cdr e)))
  9615. (error "Column name not found"))
  9616. (select-window win))
  9617. ((string-match "^[0-9]$" var)
  9618. ;; column number
  9619. (switch-to-buffer-other-window (marker-buffer pos))
  9620. (goto-char pos)
  9621. (goto-char (org-table-begin))
  9622. (recenter 1)
  9623. (if (re-search-forward org-table-dataline-regexp
  9624. (org-table-end) t)
  9625. (progn
  9626. (goto-char (match-beginning 0))
  9627. (org-table-goto-column (string-to-number var))
  9628. (message "Column %s" var))
  9629. (error "Column name not found"))
  9630. (select-window win))
  9631. ((setq e (assoc var org-table-local-parameters))
  9632. (switch-to-buffer-other-window (marker-buffer pos))
  9633. (goto-char pos)
  9634. (goto-char (org-table-begin))
  9635. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  9636. (progn
  9637. (goto-char (match-beginning 1))
  9638. (message "Local parameter."))
  9639. (error "Parameter not found"))
  9640. (select-window win))
  9641. (t
  9642. (cond
  9643. ((setq e (assoc var org-table-formula-constants))
  9644. (message "Constant: $%s=%s in `org-table-formula-constants'." var (cdr e)))
  9645. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  9646. (message "Constant: $%s=%s, retrieved from `constants.el'." var e))
  9647. (t (error "Undefined name $%s" var)))))))
  9648. (defun org-finish-edit-formulas (&optional arg)
  9649. "Parse the buffer for formula definitions and install them.
  9650. With prefix ARG, apply the new formulas to the table."
  9651. (interactive "P")
  9652. (let ((pos org-pos) eql)
  9653. (goto-char (point-min))
  9654. (while (re-search-forward "^\\$\\([a-zA-Z0-9]+\\) *= *\\(.*?\\) *$" nil t)
  9655. (push (cons (match-string 1) (match-string 2)) eql))
  9656. (set-window-configuration org-window-configuration)
  9657. (select-window (get-buffer-window (marker-buffer pos)))
  9658. (goto-char pos)
  9659. (unless (org-at-table-p)
  9660. (error "Lost table position - cannot install formulae"))
  9661. (org-table-store-formulas eql)
  9662. (move-marker pos nil)
  9663. (kill-buffer "*Edit Formulas*")
  9664. (if arg
  9665. (org-table-recalculate 'all)
  9666. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9667. (defun org-abort-edit-formulas ()
  9668. "Abort editing formulas, without installing the changes."
  9669. (interactive)
  9670. (let ((pos org-pos))
  9671. (set-window-configuration org-window-configuration)
  9672. (select-window (get-buffer-window (marker-buffer pos)))
  9673. (goto-char pos)
  9674. (message "Formula editing aborted without installing changes")))
  9675. ;;; The orgtbl minor mode
  9676. ;; Define a minor mode which can be used in other modes in order to
  9677. ;; integrate the org-mode table editor.
  9678. ;; This is really a hack, because the org-mode table editor uses several
  9679. ;; keys which normally belong to the major mode, for example the TAB and
  9680. ;; RET keys. Here is how it works: The minor mode defines all the keys
  9681. ;; necessary to operate the table editor, but wraps the commands into a
  9682. ;; function which tests if the cursor is currently inside a table. If that
  9683. ;; is the case, the table editor command is executed. However, when any of
  9684. ;; those keys is used outside a table, the function uses `key-binding' to
  9685. ;; look up if the key has an associated command in another currently active
  9686. ;; keymap (minor modes, major mode, global), and executes that command.
  9687. ;; There might be problems if any of the keys used by the table editor is
  9688. ;; otherwise used as a prefix key.
  9689. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  9690. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  9691. ;; addresses this by checking explicitly for both bindings.
  9692. ;; The optimized version (see variable `orgtbl-optimized') takes over
  9693. ;; all keys which are bound to `self-insert-command' in the *global map*.
  9694. ;; Some modes bind other commands to simple characters, for example
  9695. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  9696. ;; active, this binding is ignored inside tables and replaced with a
  9697. ;; modified self-insert.
  9698. (defvar orgtbl-mode nil
  9699. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  9700. table editor in arbitrary modes.")
  9701. (make-variable-buffer-local 'orgtbl-mode)
  9702. (defvar orgtbl-mode-map (make-keymap)
  9703. "Keymap for `orgtbl-mode'.")
  9704. ;;;###autoload
  9705. (defun turn-on-orgtbl ()
  9706. "Unconditionally turn on `orgtbl-mode'."
  9707. (orgtbl-mode 1))
  9708. ;;;###autoload
  9709. (defun orgtbl-mode (&optional arg)
  9710. "The `org-mode' table editor as a minor mode for use in other modes."
  9711. (interactive)
  9712. (if (eq major-mode 'org-mode)
  9713. ;; Exit without error, in case some hook functions calls this
  9714. ;; by accident in org-mode.
  9715. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  9716. (setq orgtbl-mode
  9717. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  9718. (if orgtbl-mode
  9719. (progn
  9720. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  9721. ;; Make sure we are first in minor-mode-map-alist
  9722. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  9723. (and c (setq minor-mode-map-alist
  9724. (cons c (delq c minor-mode-map-alist)))))
  9725. (set (make-local-variable (quote org-table-may-need-update)) t)
  9726. (org-add-hook 'before-change-functions 'org-before-change-function
  9727. nil 'local)
  9728. (set (make-local-variable 'org-old-auto-fill-inhibit-regexp)
  9729. auto-fill-inhibit-regexp)
  9730. (set (make-local-variable 'auto-fill-inhibit-regexp)
  9731. (if auto-fill-inhibit-regexp
  9732. (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp)
  9733. "[ \t]*|"))
  9734. (org-add-to-invisibility-spec '(org-cwidth . nil))
  9735. (easy-menu-add orgtbl-mode-menu)
  9736. (run-hooks 'orgtbl-mode-hook))
  9737. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  9738. (org-cleanup-narrow-column-properties)
  9739. (org-remove-from-invisibility-spec '(org-cwidth . nil))
  9740. (remove-hook 'before-change-functions 'org-before-change-function t)
  9741. (easy-menu-remove orgtbl-mode-menu)
  9742. (force-mode-line-update 'all))))
  9743. (defun org-cleanup-narrow-column-properties ()
  9744. "Remove all properties related to narrow-column invisibility."
  9745. (let ((s 1))
  9746. (while (setq s (text-property-any s (point-max)
  9747. 'display org-narrow-column-arrow))
  9748. (remove-text-properties s (1+ s) '(display t)))
  9749. (setq s 1)
  9750. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  9751. (remove-text-properties s (1+ s) '(org-cwidth t)))
  9752. (setq s 1)
  9753. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  9754. (remove-text-properties s (1+ s) '(invisible t)))))
  9755. ;; Install it as a minor mode.
  9756. (put 'orgtbl-mode :included t)
  9757. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  9758. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  9759. (defun orgtbl-make-binding (fun n &rest keys)
  9760. "Create a function for binding in the table minor mode.
  9761. FUN is the command to call inside a table. N is used to create a unique
  9762. command name. KEYS are keys that should be checked in for a command
  9763. to execute outside of tables."
  9764. (eval
  9765. (list 'defun
  9766. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  9767. '(arg)
  9768. (concat "In tables, run `" (symbol-name fun) "'.\n"
  9769. "Outside of tables, run the binding of `"
  9770. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  9771. "'.")
  9772. '(interactive "p")
  9773. (list 'if
  9774. '(org-at-table-p)
  9775. (list 'call-interactively (list 'quote fun))
  9776. (list 'let '(orgtbl-mode)
  9777. (list 'call-interactively
  9778. (append '(or)
  9779. (mapcar (lambda (k)
  9780. (list 'key-binding k))
  9781. keys)
  9782. '('orgtbl-error))))))))
  9783. (defun orgtbl-error ()
  9784. "Error when there is no default binding for a table key."
  9785. (interactive)
  9786. (error "This key is has no function outside tables"))
  9787. (defun orgtbl-setup ()
  9788. "Setup orgtbl keymaps."
  9789. (let ((nfunc 0)
  9790. (bindings
  9791. (list
  9792. '([(meta shift left)] org-table-delete-column)
  9793. '([(meta left)] org-table-move-column-left)
  9794. '([(meta right)] org-table-move-column-right)
  9795. '([(meta shift right)] org-table-insert-column)
  9796. '([(meta shift up)] org-table-kill-row)
  9797. '([(meta shift down)] org-table-insert-row)
  9798. '([(meta up)] org-table-move-row-up)
  9799. '([(meta down)] org-table-move-row-down)
  9800. '("\C-c\C-w" org-table-cut-region)
  9801. '("\C-c\M-w" org-table-copy-region)
  9802. '("\C-c\C-y" org-table-paste-rectangle)
  9803. '("\C-c-" org-table-insert-hline)
  9804. ; '([(shift tab)] org-table-previous-field)
  9805. '("\C-m" org-table-next-row)
  9806. (list (org-key 'S-return) 'org-table-copy-down)
  9807. '([(meta return)] org-table-wrap-region)
  9808. '("\C-c\C-q" org-table-wrap-region)
  9809. '("\C-c?" org-table-current-column)
  9810. '("\C-c " org-table-blank-field)
  9811. '("\C-c+" org-table-sum)
  9812. '("\C-c|" org-table-toggle-vline-visibility)
  9813. '("\C-c=" org-table-eval-formula)
  9814. '("\C-c'" org-table-edit-formulas)
  9815. '("\C-c`" org-table-edit-field)
  9816. '("\C-c*" org-table-recalculate)
  9817. '("\C-c^" org-table-sort-lines)
  9818. '([(control ?#)] org-table-rotate-recalc-marks)))
  9819. elt key fun cmd)
  9820. (while (setq elt (pop bindings))
  9821. (setq nfunc (1+ nfunc))
  9822. (setq key (car elt)
  9823. fun (nth 1 elt)
  9824. cmd (orgtbl-make-binding fun nfunc key))
  9825. (define-key orgtbl-mode-map key cmd))
  9826. ;; Special treatment needed for TAB and RET
  9827. (define-key orgtbl-mode-map [(return)]
  9828. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  9829. (define-key orgtbl-mode-map "\C-m"
  9830. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  9831. (define-key orgtbl-mode-map [(tab)]
  9832. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  9833. (define-key orgtbl-mode-map "\C-i"
  9834. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)])))
  9835. (define-key orgtbl-mode-map "\C-i"
  9836. (orgtbl-make-binding 'orgtbl-tab 104 [(shift tab)]))
  9837. (define-key orgtbl-mode-map "\C-c\C-c"
  9838. (orgtbl-make-binding 'org-ctrl-c-ctrl-c 105 "\C-c\C-c"))
  9839. (when orgtbl-optimized
  9840. ;; If the user wants maximum table support, we need to hijack
  9841. ;; some standard editing functions
  9842. (org-remap orgtbl-mode-map
  9843. 'self-insert-command 'orgtbl-self-insert-command
  9844. 'delete-char 'org-delete-char
  9845. 'delete-backward-char 'org-delete-backward-char)
  9846. (define-key orgtbl-mode-map "|" 'org-force-self-insert))
  9847. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  9848. '("OrgTbl"
  9849. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  9850. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  9851. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  9852. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  9853. "--"
  9854. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  9855. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  9856. ["Copy Field from Above"
  9857. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  9858. "--"
  9859. ("Column"
  9860. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  9861. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  9862. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  9863. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"]
  9864. "--"
  9865. ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :active (org-at-table-p) :selected org-table-limit-column-width :style toggle])
  9866. ("Row"
  9867. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  9868. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  9869. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  9870. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  9871. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  9872. "--"
  9873. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  9874. ("Rectangle"
  9875. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  9876. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  9877. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  9878. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  9879. "--"
  9880. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  9881. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  9882. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  9883. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  9884. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  9885. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  9886. ["Sum Column/Rectangle" org-table-sum
  9887. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  9888. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  9889. ["Debug Formulas"
  9890. (setq org-table-formula-debug (not org-table-formula-debug))
  9891. :style toggle :selected org-table-formula-debug]
  9892. ))
  9893. t)
  9894. (defun orgtbl-tab (arg)
  9895. "Justification and field motion for `orgtbl-mode'."
  9896. (interactive "P")
  9897. (if arg (org-table-edit-field t)
  9898. (org-table-justify-field-maybe)
  9899. (org-table-next-field)))
  9900. (defun orgtbl-ret ()
  9901. "Justification and field motion for `orgtbl-mode'."
  9902. (interactive)
  9903. (org-table-justify-field-maybe)
  9904. (org-table-next-row))
  9905. (defun orgtbl-self-insert-command (N)
  9906. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  9907. If the cursor is in a table looking at whitespace, the whitespace is
  9908. overwritten, and the table is not marked as requiring realignment."
  9909. (interactive "p")
  9910. (if (and (org-at-table-p)
  9911. (or
  9912. (and org-table-auto-blank-field
  9913. (member last-command
  9914. '(orgtbl-hijacker-command-100
  9915. orgtbl-hijacker-command-101
  9916. orgtbl-hijacker-command-102
  9917. orgtbl-hijacker-command-103
  9918. orgtbl-hijacker-command-104
  9919. orgtbl-hijacker-command-105))
  9920. (org-table-blank-field))
  9921. t)
  9922. (eq N 1)
  9923. (looking-at "[^|\n]* +|"))
  9924. (let (org-table-may-need-update)
  9925. (goto-char (1- (match-end 0)))
  9926. (delete-backward-char 1)
  9927. (goto-char (match-beginning 0))
  9928. (self-insert-command N))
  9929. (setq org-table-may-need-update t)
  9930. (let (orgtbl-mode)
  9931. (call-interactively (key-binding (vector last-input-event))))))
  9932. (defun org-force-self-insert (N)
  9933. "Needed to enforce self-insert under remapping."
  9934. (interactive "p")
  9935. (self-insert-command N))
  9936. ;;; Exporting
  9937. (defconst org-level-max 20)
  9938. (defun org-export-find-first-heading-line (list)
  9939. "Remove all lines from LIST which are before the first headline."
  9940. (let ((orig-list list)
  9941. (re (concat "^" outline-regexp)))
  9942. (while (and list
  9943. (not (string-match re (car list))))
  9944. (pop list))
  9945. (or list orig-list)))
  9946. (defun org-skip-comments (lines)
  9947. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  9948. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  9949. (re2 "^\\(\\*+\\)[ \t\n\r]")
  9950. rtn line level)
  9951. (while (setq line (pop lines))
  9952. (cond
  9953. ((and (string-match re1 line)
  9954. (setq level (- (match-end 1) (match-beginning 1))))
  9955. ;; Beginning of a COMMENT subtree. Skip it.
  9956. (while (and (setq line (pop lines))
  9957. (or (not (string-match re2 line))
  9958. (> (- (match-end 1) (match-beginning 1)) level))))
  9959. (setq lines (cons line lines)))
  9960. ((string-match "^#" line)
  9961. ;; an ordinary comment line
  9962. )
  9963. ((and org-export-table-remove-special-lines
  9964. (string-match "^[ \t]*| *[!_^] *|" line))
  9965. ;; a special table line that should be removed
  9966. )
  9967. (t (setq rtn (cons line rtn)))))
  9968. (nreverse rtn)))
  9969. ;; ASCII
  9970. (defconst org-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  9971. "Characters for underlining headings in ASCII export.")
  9972. (defconst org-html-entities
  9973. '(("nbsp")
  9974. ("iexcl")
  9975. ("cent")
  9976. ("pound")
  9977. ("curren")
  9978. ("yen")
  9979. ("brvbar")
  9980. ("sect")
  9981. ("uml")
  9982. ("copy")
  9983. ("ordf")
  9984. ("laquo")
  9985. ("not")
  9986. ("shy")
  9987. ("reg")
  9988. ("macr")
  9989. ("deg")
  9990. ("plusmn")
  9991. ("sup2")
  9992. ("sup3")
  9993. ("acute")
  9994. ("micro")
  9995. ("para")
  9996. ("middot")
  9997. ("odot"."o")
  9998. ("star"."*")
  9999. ("cedil")
  10000. ("sup1")
  10001. ("ordm")
  10002. ("raquo")
  10003. ("frac14")
  10004. ("frac12")
  10005. ("frac34")
  10006. ("iquest")
  10007. ("Agrave")
  10008. ("Aacute")
  10009. ("Acirc")
  10010. ("Atilde")
  10011. ("Auml")
  10012. ("Aring") ("AA"."&Aring;")
  10013. ("AElig")
  10014. ("Ccedil")
  10015. ("Egrave")
  10016. ("Eacute")
  10017. ("Ecirc")
  10018. ("Euml")
  10019. ("Igrave")
  10020. ("Iacute")
  10021. ("Icirc")
  10022. ("Iuml")
  10023. ("ETH")
  10024. ("Ntilde")
  10025. ("Ograve")
  10026. ("Oacute")
  10027. ("Ocirc")
  10028. ("Otilde")
  10029. ("Ouml")
  10030. ("times")
  10031. ("Oslash")
  10032. ("Ugrave")
  10033. ("Uacute")
  10034. ("Ucirc")
  10035. ("Uuml")
  10036. ("Yacute")
  10037. ("THORN")
  10038. ("szlig")
  10039. ("agrave")
  10040. ("aacute")
  10041. ("acirc")
  10042. ("atilde")
  10043. ("auml")
  10044. ("aring")
  10045. ("aelig")
  10046. ("ccedil")
  10047. ("egrave")
  10048. ("eacute")
  10049. ("ecirc")
  10050. ("euml")
  10051. ("igrave")
  10052. ("iacute")
  10053. ("icirc")
  10054. ("iuml")
  10055. ("eth")
  10056. ("ntilde")
  10057. ("ograve")
  10058. ("oacute")
  10059. ("ocirc")
  10060. ("otilde")
  10061. ("ouml")
  10062. ("divide")
  10063. ("oslash")
  10064. ("ugrave")
  10065. ("uacute")
  10066. ("ucirc")
  10067. ("uuml")
  10068. ("yacute")
  10069. ("thorn")
  10070. ("yuml")
  10071. ("fnof")
  10072. ("Alpha")
  10073. ("Beta")
  10074. ("Gamma")
  10075. ("Delta")
  10076. ("Epsilon")
  10077. ("Zeta")
  10078. ("Eta")
  10079. ("Theta")
  10080. ("Iota")
  10081. ("Kappa")
  10082. ("Lambda")
  10083. ("Mu")
  10084. ("Nu")
  10085. ("Xi")
  10086. ("Omicron")
  10087. ("Pi")
  10088. ("Rho")
  10089. ("Sigma")
  10090. ("Tau")
  10091. ("Upsilon")
  10092. ("Phi")
  10093. ("Chi")
  10094. ("Psi")
  10095. ("Omega")
  10096. ("alpha")
  10097. ("beta")
  10098. ("gamma")
  10099. ("delta")
  10100. ("epsilon")
  10101. ("varepsilon"."&epsilon;")
  10102. ("zeta")
  10103. ("eta")
  10104. ("theta")
  10105. ("iota")
  10106. ("kappa")
  10107. ("lambda")
  10108. ("mu")
  10109. ("nu")
  10110. ("xi")
  10111. ("omicron")
  10112. ("pi")
  10113. ("rho")
  10114. ("sigmaf") ("varsigma"."&sigmaf;")
  10115. ("sigma")
  10116. ("tau")
  10117. ("upsilon")
  10118. ("phi")
  10119. ("chi")
  10120. ("psi")
  10121. ("omega")
  10122. ("thetasym") ("vartheta"."&thetasym;")
  10123. ("upsih")
  10124. ("piv")
  10125. ("bull") ("bullet"."&bull;")
  10126. ("hellip") ("dots"."&hellip;")
  10127. ("prime")
  10128. ("Prime")
  10129. ("oline")
  10130. ("frasl")
  10131. ("weierp")
  10132. ("image")
  10133. ("real")
  10134. ("trade")
  10135. ("alefsym")
  10136. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  10137. ("uarr") ("uparrow"."&uarr;")
  10138. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  10139. ("darr")("downarrow"."&darr;")
  10140. ("harr") ("leftrightarrow"."&harr;")
  10141. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  10142. ("lArr") ("Leftarrow"."&lArr;")
  10143. ("uArr") ("Uparrow"."&uArr;")
  10144. ("rArr") ("Rightarrow"."&rArr;")
  10145. ("dArr") ("Downarrow"."&dArr;")
  10146. ("hArr") ("Leftrightarrow"."&hArr;")
  10147. ("forall")
  10148. ("part") ("partial"."&part;")
  10149. ("exist") ("exists"."&exist;")
  10150. ("empty") ("emptyset"."&empty;")
  10151. ("nabla")
  10152. ("isin") ("in"."&isin;")
  10153. ("notin")
  10154. ("ni")
  10155. ("prod")
  10156. ("sum")
  10157. ("minus")
  10158. ("lowast") ("ast"."&lowast;")
  10159. ("radic")
  10160. ("prop") ("proptp"."&prop;")
  10161. ("infin") ("infty"."&infin;")
  10162. ("ang") ("angle"."&ang;")
  10163. ("and") ("vee"."&and;")
  10164. ("or") ("wedge"."&or;")
  10165. ("cap")
  10166. ("cup")
  10167. ("int")
  10168. ("there4")
  10169. ("sim")
  10170. ("cong") ("simeq"."&cong;")
  10171. ("asymp")("approx"."&asymp;")
  10172. ("ne") ("neq"."&ne;")
  10173. ("equiv")
  10174. ("le")
  10175. ("ge")
  10176. ("sub") ("subset"."&sub;")
  10177. ("sup") ("supset"."&sup;")
  10178. ("nsub")
  10179. ("sube")
  10180. ("supe")
  10181. ("oplus")
  10182. ("otimes")
  10183. ("perp")
  10184. ("sdot") ("cdot"."&sdot;")
  10185. ("lceil")
  10186. ("rceil")
  10187. ("lfloor")
  10188. ("rfloor")
  10189. ("lang")
  10190. ("rang")
  10191. ("loz") ("Diamond"."&loz;")
  10192. ("spades") ("spadesuit"."&spades;")
  10193. ("clubs") ("clubsuit"."&clubs;")
  10194. ("hearts") ("diamondsuit"."&hearts;")
  10195. ("diams") ("diamondsuit"."&diams;")
  10196. ("quot")
  10197. ("amp")
  10198. ("lt")
  10199. ("gt")
  10200. ("OElig")
  10201. ("oelig")
  10202. ("Scaron")
  10203. ("scaron")
  10204. ("Yuml")
  10205. ("circ")
  10206. ("tilde")
  10207. ("ensp")
  10208. ("emsp")
  10209. ("thinsp")
  10210. ("zwnj")
  10211. ("zwj")
  10212. ("lrm")
  10213. ("rlm")
  10214. ("ndash")
  10215. ("mdash")
  10216. ("lsquo")
  10217. ("rsquo")
  10218. ("sbquo")
  10219. ("ldquo")
  10220. ("rdquo")
  10221. ("bdquo")
  10222. ("dagger")
  10223. ("Dagger")
  10224. ("permil")
  10225. ("lsaquo")
  10226. ("rsaquo")
  10227. ("euro")
  10228. ("arccos"."arccos")
  10229. ("arcsin"."arcsin")
  10230. ("arctan"."arctan")
  10231. ("arg"."arg")
  10232. ("cos"."cos")
  10233. ("cosh"."cosh")
  10234. ("cot"."cot")
  10235. ("coth"."coth")
  10236. ("csc"."csc")
  10237. ("deg"."deg")
  10238. ("det"."det")
  10239. ("dim"."dim")
  10240. ("exp"."exp")
  10241. ("gcd"."gcd")
  10242. ("hom"."hom")
  10243. ("inf"."inf")
  10244. ("ker"."ker")
  10245. ("lg"."lg")
  10246. ("lim"."lim")
  10247. ("liminf"."liminf")
  10248. ("limsup"."limsup")
  10249. ("ln"."ln")
  10250. ("log"."log")
  10251. ("max"."max")
  10252. ("min"."min")
  10253. ("Pr"."Pr")
  10254. ("sec"."sec")
  10255. ("sin"."sin")
  10256. ("sinh"."sinh")
  10257. ("sup"."sup")
  10258. ("tan"."tan")
  10259. ("tanh"."tanh")
  10260. )
  10261. "Entities for TeX->HTML translation.
  10262. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  10263. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  10264. In that case, \"\\ent\" will be translated to \"&other;\".
  10265. The list contains HTML entities for Latin-1, Greek and other symbols.
  10266. It is supplemented by a number of commonly used TeX macros with appropriate
  10267. translations. There is currently no way for users to extend this.")
  10268. (defun org-cleaned-string-for-export (string)
  10269. "Cleanup a buffer substring so that links can be created safely."
  10270. (interactive)
  10271. (let* ((cb (current-buffer))
  10272. (re-radio (and org-target-link-regexp
  10273. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  10274. rtn)
  10275. (save-excursion
  10276. (set-buffer (get-buffer-create " org-mode-tmp"))
  10277. (erase-buffer)
  10278. (insert string)
  10279. (org-mode)
  10280. ;; Find targets in comments and move them out of comments,
  10281. ;; but mark them as targets that should be invisible
  10282. (goto-char (point-min))
  10283. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  10284. (replace-match "\\1(INVISIBLE)"))
  10285. ;; Find matches for radio targets and turn them into internal links
  10286. (goto-char (point-min))
  10287. (when re-radio
  10288. (while (re-search-forward re-radio nil t)
  10289. (replace-match "\\1[[\\2]]")))
  10290. ;; Find all links that contain a newline and put them into a single line
  10291. (goto-char (point-min))
  10292. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  10293. (replace-match "\\1 \\3")
  10294. (goto-char (match-beginning 0)))
  10295. ;; Normalize links: Convert angle and plain links into bracket links
  10296. (goto-char (point-min))
  10297. (while (re-search-forward org-external-link-maybe-brackets nil t)
  10298. (replace-match
  10299. (concat (if (match-end 1) (match-string 1) "[[")
  10300. (match-string 3) ":" (match-string 4)
  10301. (if (match-end 5) (match-string 5) "]]")) t t))
  10302. ;; Remove comments
  10303. (goto-char (point-min))
  10304. (while (re-search-forward "^#.*\n?" nil t)
  10305. (replace-match ""))
  10306. (setq rtn (buffer-string)))
  10307. (kill-buffer " org-mode-tmp")
  10308. rtn))
  10309. (defun org-solidify-link-text (s &optional alist)
  10310. "Take link text and make a safe target out of it."
  10311. (save-match-data
  10312. (let* ((rtn
  10313. (mapconcat
  10314. 'identity
  10315. (org-split-string s "[ \t\r\n]+") "--"))
  10316. (a (assoc rtn alist)))
  10317. (or (cdr a) rtn))))
  10318. (defun org-convert-to-odd-levels ()
  10319. "Convert an org-mode file with all levels allowed to one with odd levels.
  10320. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  10321. level 5 etc."
  10322. (interactive)
  10323. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  10324. (let ((org-odd-levels-only nil) n)
  10325. (save-excursion
  10326. (goto-char (point-min))
  10327. (while (re-search-forward "^\\*\\*+" nil t)
  10328. (setq n (1- (length (match-string 0))))
  10329. (while (>= (setq n (1- n)) 0)
  10330. (org-demote))
  10331. (end-of-line 1))))))
  10332. (defun org-convert-to-oddeven-levels ()
  10333. "Convert an org-mode file with only odd levels to one with odd and even levels.
  10334. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  10335. section with an even level, conversion would destroy the structure of the file. An error
  10336. is signaled in this case."
  10337. (interactive)
  10338. (goto-char (point-min))
  10339. ;; First check if there are no even levels
  10340. (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
  10341. (org-show-hierarchy-above)
  10342. (error "Not all levels are odd in this file. Conversion not possible."))
  10343. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  10344. (let ((org-odd-levels-only nil) n)
  10345. (save-excursion
  10346. (goto-char (point-min))
  10347. (while (re-search-forward "^\\*\\*+" nil t)
  10348. (setq n (/ (length (match-string 0)) 2))
  10349. (while (>= (setq n (1- n)) 0)
  10350. (org-promote))
  10351. (end-of-line 1))))))
  10352. (defun org-tr-level (n)
  10353. "Make N odd if required."
  10354. (if org-odd-levels-only (1+ (/ n 2)) n))
  10355. (defvar org-last-level nil) ; dynamically scoped variable
  10356. (defun org-export-as-ascii (arg)
  10357. "Export the outline as a pretty ASCII file.
  10358. If there is an active region, export only the region.
  10359. The prefix ARG specifies how many levels of the outline should become
  10360. underlined headlines. The default is 3."
  10361. (interactive "P")
  10362. (setq-default org-todo-line-regexp org-todo-line-regexp)
  10363. (let* ((region
  10364. (buffer-substring
  10365. (if (org-region-active-p) (region-beginning) (point-min))
  10366. (if (org-region-active-p) (region-end) (point-max))))
  10367. (lines (org-export-find-first-heading-line
  10368. (org-skip-comments
  10369. (org-split-string
  10370. (org-cleaned-string-for-export region)
  10371. "[\r\n]"))))
  10372. (org-startup-with-deadline-check nil)
  10373. (level 0) line txt
  10374. (umax nil)
  10375. (case-fold-search nil)
  10376. (filename (concat (file-name-sans-extension buffer-file-name)
  10377. ".txt"))
  10378. (buffer (find-file-noselect filename))
  10379. (levels-open (make-vector org-level-max nil))
  10380. (date (format-time-string "%Y/%m/%d" (current-time)))
  10381. (time (format-time-string "%X" (org-current-time)))
  10382. (author user-full-name)
  10383. (title (buffer-name))
  10384. (options nil)
  10385. (email user-mail-address)
  10386. (language org-export-default-language)
  10387. (text nil)
  10388. (todo nil)
  10389. (lang-words nil))
  10390. (setq org-last-level 1)
  10391. (org-init-section-numbers)
  10392. (find-file-noselect filename)
  10393. ;; Search for the export key lines
  10394. (org-parse-key-lines)
  10395. (setq lang-words (or (assoc language org-export-language-setup)
  10396. (assoc "en" org-export-language-setup)))
  10397. (if org-export-ascii-show-new-buffer
  10398. (switch-to-buffer-other-window buffer)
  10399. (set-buffer buffer))
  10400. (erase-buffer)
  10401. (fundamental-mode)
  10402. (if options (org-parse-export-options options))
  10403. (setq umax (if arg (prefix-numeric-value arg)
  10404. org-export-headline-levels))
  10405. ;; File header
  10406. (if title (org-insert-centered title ?=))
  10407. (insert "\n")
  10408. (if (or author email)
  10409. (insert (concat (nth 1 lang-words) ": " (or author "")
  10410. (if email (concat " <" email ">") "")
  10411. "\n")))
  10412. (if (and date time)
  10413. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  10414. (if text (insert (concat (org-html-expand-for-ascii text) "\n\n")))
  10415. (insert "\n\n")
  10416. (if org-export-with-toc
  10417. (progn
  10418. (insert (nth 3 lang-words) "\n"
  10419. (make-string (length (nth 3 lang-words)) ?=) "\n")
  10420. (mapcar '(lambda (line)
  10421. (if (string-match org-todo-line-regexp
  10422. line)
  10423. ;; This is a headline
  10424. (progn
  10425. (setq level (- (match-end 1) (match-beginning 1))
  10426. level (org-tr-level level)
  10427. txt (match-string 3 line)
  10428. todo
  10429. (or (and (match-beginning 2)
  10430. (not (equal (match-string 2 line)
  10431. org-done-string)))
  10432. ; TODO, not DONE
  10433. (and (= level umax)
  10434. (org-search-todo-below
  10435. line lines level))))
  10436. (setq txt (org-html-expand-for-ascii txt))
  10437. (if org-export-with-section-numbers
  10438. (setq txt (concat (org-section-number level)
  10439. " " txt)))
  10440. (if (<= level umax)
  10441. (progn
  10442. (insert
  10443. (make-string (* (1- level) 4) ?\ )
  10444. (format (if todo "%s (*)\n" "%s\n") txt))
  10445. (setq org-last-level level))
  10446. ))))
  10447. lines)))
  10448. (org-init-section-numbers)
  10449. (while (setq line (pop lines))
  10450. ;; Remove the quoted HTML tags.
  10451. (setq line (org-html-expand-for-ascii line))
  10452. ;; Remove targets
  10453. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  10454. (setq line (replace-match "" t t line)))
  10455. ;; Replace internal links
  10456. (while (string-match org-bracket-link-regexp line)
  10457. (setq line (replace-match
  10458. (if (match-end 3) "[\\3]" "[\\1]")
  10459. t nil line)))
  10460. (cond
  10461. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  10462. ;; a Headline
  10463. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  10464. txt (match-string 2 line))
  10465. (org-ascii-level-start level txt umax))
  10466. (t (insert line "\n"))))
  10467. (normal-mode)
  10468. (save-buffer)
  10469. (goto-char (point-min))))
  10470. (defun org-search-todo-below (line lines level)
  10471. "Search the subtree below LINE for any TODO entries."
  10472. (let ((rest (cdr (memq line lines)))
  10473. (re org-todo-line-regexp)
  10474. line lv todo)
  10475. (catch 'exit
  10476. (while (setq line (pop rest))
  10477. (if (string-match re line)
  10478. (progn
  10479. (setq lv (- (match-end 1) (match-beginning 1))
  10480. todo (and (match-beginning 2)
  10481. (not (equal (match-string 2 line)
  10482. org-done-string))))
  10483. ; TODO, not DONE
  10484. (if (<= lv level) (throw 'exit nil))
  10485. (if todo (throw 'exit t))))))))
  10486. ;; FIXME: Try to handle <b> and <i> as faces via text properties.
  10487. ;; We could also implement *bold*,/italic/ and _underline_ for ASCII export
  10488. (defun org-html-expand-for-ascii (line)
  10489. "Handle quoted HTML for ASCII export."
  10490. (if org-export-html-expand
  10491. (while (string-match "@<[^<>\n]*>" line)
  10492. ;; We just remove the tags for now.
  10493. (setq line (replace-match "" nil nil line))))
  10494. line)
  10495. (defun org-insert-centered (s &optional underline)
  10496. "Insert the string S centered and underline it with character UNDERLINE."
  10497. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  10498. (insert (make-string ind ?\ ) s "\n")
  10499. (if underline
  10500. (insert (make-string ind ?\ )
  10501. (make-string (string-width s) underline)
  10502. "\n"))))
  10503. (defun org-ascii-level-start (level title umax)
  10504. "Insert a new level in ASCII export."
  10505. (let (char)
  10506. (if (> level umax)
  10507. (insert (make-string (* 2 (- level umax 1)) ?\ ) "* " title "\n")
  10508. (if (or (not (equal (char-before) ?\n))
  10509. (not (equal (char-before (1- (point))) ?\n)))
  10510. (insert "\n"))
  10511. (setq char (nth (- umax level) (reverse org-ascii-underline)))
  10512. (if org-export-with-section-numbers
  10513. (setq title (concat (org-section-number level) " " title)))
  10514. (insert title "\n" (make-string (string-width title) char) "\n"))))
  10515. (defun org-export-copy-visible ()
  10516. "Copy the visible part of the buffer to another buffer, for printing.
  10517. Also removes the first line of the buffer if it specifies a mode,
  10518. and all options lines."
  10519. (interactive)
  10520. (let* ((filename (concat (file-name-sans-extension buffer-file-name)
  10521. ".txt"))
  10522. (buffer (find-file-noselect filename))
  10523. (ore (concat
  10524. (org-make-options-regexp
  10525. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  10526. "STARTUP" "ARCHIVE"
  10527. "TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"))
  10528. (if org-noutline-p "\\(\n\\|$\\)" "")))
  10529. s e)
  10530. (with-current-buffer buffer
  10531. (erase-buffer)
  10532. (text-mode))
  10533. (save-excursion
  10534. (setq s (goto-char (point-min)))
  10535. (while (not (= (point) (point-max)))
  10536. (goto-char (org-find-invisible))
  10537. (append-to-buffer buffer s (point))
  10538. (setq s (goto-char (org-find-visible)))))
  10539. (switch-to-buffer-other-window buffer)
  10540. (newline)
  10541. (goto-char (point-min))
  10542. (if (looking-at ".*-\\*- mode:.*\n")
  10543. (replace-match ""))
  10544. (while (re-search-forward ore nil t)
  10545. (replace-match ""))
  10546. (goto-char (point-min))))
  10547. (defun org-find-visible ()
  10548. (if (featurep 'noutline)
  10549. (let ((s (point)))
  10550. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  10551. (get-char-property s 'invisible)))
  10552. s)
  10553. (skip-chars-forward "^\n")
  10554. (point)))
  10555. (defun org-find-invisible ()
  10556. (if (featurep 'noutline)
  10557. (let ((s (point)))
  10558. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  10559. (not (get-char-property s 'invisible))))
  10560. s)
  10561. (skip-chars-forward "^\r")
  10562. (point)))
  10563. ;; HTML
  10564. (defun org-get-current-options ()
  10565. "Return a string with current options as keyword options.
  10566. Does include HTML export options as well as TODO and CATEGORY stuff."
  10567. (format
  10568. "#+TITLE: %s
  10569. #+AUTHOR: %s
  10570. #+EMAIL: %s
  10571. #+LANGUAGE: %s
  10572. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  10573. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s
  10574. #+CATEGORY: %s
  10575. #+SEQ_TODO: %s
  10576. #+TYP_TODO: %s
  10577. #+STARTUP: %s %s %s %s %s
  10578. #+ARCHIVE: %s
  10579. "
  10580. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  10581. org-export-headline-levels
  10582. org-export-with-section-numbers
  10583. org-export-with-toc
  10584. org-export-preserve-breaks
  10585. org-export-html-expand
  10586. org-export-with-fixed-width
  10587. org-export-with-tables
  10588. org-export-with-sub-superscripts
  10589. org-export-with-emphasize
  10590. org-export-with-TeX-macros
  10591. (file-name-nondirectory buffer-file-name)
  10592. (if (equal org-todo-interpretation 'sequence)
  10593. (mapconcat 'identity org-todo-keywords " ")
  10594. "TODO FEEDBACK VERIFY DONE")
  10595. (if (equal org-todo-interpretation 'type)
  10596. (mapconcat 'identity org-todo-keywords " ")
  10597. "Me Jason Marie DONE")
  10598. (cdr (assoc org-startup-folded
  10599. '((nil . "nofold")(t . "fold")(content . "content"))))
  10600. (if org-startup-with-deadline-check "dlcheck" "nodlcheck")
  10601. (if org-odd-levels-only "odd" "oddeven")
  10602. (if org-hide-leading-stars "hidestars" "showstars")
  10603. (if org-startup-align-all-tables "align" "noalign")
  10604. org-archive-location
  10605. ))
  10606. (defun org-insert-export-options-template ()
  10607. "Insert into the buffer a template with information for exporting."
  10608. (interactive)
  10609. (if (not (bolp)) (newline))
  10610. (let ((s (org-get-current-options)))
  10611. (and (string-match "#\\+CATEGORY" s)
  10612. (setq s (substring s 0 (match-beginning 0))))
  10613. (insert s)))
  10614. (defun org-toggle-fixed-width-section (arg)
  10615. "Toggle the fixed-width export.
  10616. If there is no active region, the QUOTE keyword at the current headline is
  10617. inserted or removed. When present, it causes the text between this headline
  10618. and the next to be exported as fixed-width text, and unmodified.
  10619. If there is an active region, this command adds or removes a colon as the
  10620. first character of this line. If the first character of a line is a colon,
  10621. this line is also exported in fixed-width font."
  10622. (interactive "P")
  10623. (let* ((cc 0)
  10624. (regionp (org-region-active-p))
  10625. (beg (if regionp (region-beginning) (point)))
  10626. (end (if regionp (region-end)))
  10627. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  10628. (re "[ \t]*\\(:\\)")
  10629. off)
  10630. (if regionp
  10631. (save-excursion
  10632. (goto-char beg)
  10633. (setq cc (current-column))
  10634. (beginning-of-line 1)
  10635. (setq off (looking-at re))
  10636. (while (> nlines 0)
  10637. (setq nlines (1- nlines))
  10638. (beginning-of-line 1)
  10639. (cond
  10640. (arg
  10641. (move-to-column cc t)
  10642. (insert ":\n")
  10643. (forward-line -1))
  10644. ((and off (looking-at re))
  10645. (replace-match "" t t nil 1))
  10646. ((not off) (move-to-column cc t) (insert ":")))
  10647. (forward-line 1)))
  10648. (save-excursion
  10649. (org-back-to-heading)
  10650. (if (looking-at (concat outline-regexp
  10651. "\\( +\\<" org-quote-string "\\>\\)"))
  10652. (replace-match "" t t nil 1)
  10653. (if (looking-at outline-regexp)
  10654. (progn
  10655. (goto-char (match-end 0))
  10656. (insert " " org-quote-string))))))))
  10657. (defun org-export-as-html-and-open (arg)
  10658. "Export the outline as HTML and immediately open it with a browser.
  10659. If there is an active region, export only the region.
  10660. The prefix ARG specifies how many levels of the outline should become
  10661. headlines. The default is 3. Lower levels will become bulleted lists."
  10662. (interactive "P")
  10663. (org-export-as-html arg 'hidden)
  10664. (org-open-file buffer-file-name))
  10665. (defun org-export-as-html-batch ()
  10666. "Call `org-export-as-html', may be used in batch processing as
  10667. emacs --batch
  10668. --load=$HOME/lib/emacs/org.el
  10669. --eval \"(setq org-export-headline-levels 2)\"
  10670. --visit=MyFile --funcall org-export-as-html-batch"
  10671. (org-export-as-html org-export-headline-levels 'hidden))
  10672. (defun org-export-as-html (arg &optional hidden)
  10673. "Export the outline as a pretty HTML file.
  10674. If there is an active region, export only the region.
  10675. The prefix ARG specifies how many levels of the outline should become
  10676. headlines. The default is 3. Lower levels will become bulleted lists."
  10677. (interactive "P")
  10678. (setq-default org-todo-line-regexp org-todo-line-regexp)
  10679. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  10680. (setq-default org-done-string org-done-string)
  10681. (let* ((style org-export-html-style)
  10682. (odd org-odd-levels-only)
  10683. (region-p (org-region-active-p))
  10684. (region
  10685. (buffer-substring
  10686. (if region-p (region-beginning) (point-min))
  10687. (if region-p (region-end) (point-max))))
  10688. (all_lines
  10689. (org-skip-comments (org-split-string
  10690. (org-cleaned-string-for-export region)
  10691. "[\r\n]")))
  10692. (lines (org-export-find-first-heading-line all_lines))
  10693. (level 0) (line "") (origline "") txt todo
  10694. (umax nil)
  10695. (filename (concat (file-name-sans-extension buffer-file-name)
  10696. ".html"))
  10697. (buffer (find-file-noselect filename))
  10698. (levels-open (make-vector org-level-max nil))
  10699. (date (format-time-string "%Y/%m/%d" (current-time)))
  10700. (time (format-time-string "%X" (org-current-time)))
  10701. (author user-full-name)
  10702. (title (buffer-name))
  10703. (options nil)
  10704. (quote-re (concat "^\\*+[ \t]*" org-quote-string "\\>"))
  10705. (inquote nil)
  10706. (infixed nil)
  10707. (in-local-list nil)
  10708. (local-list-num nil)
  10709. (local-list-indent nil)
  10710. (llt org-plain-list-ordered-item-terminator)
  10711. (email user-mail-address)
  10712. (language org-export-default-language)
  10713. (text nil)
  10714. (lang-words nil)
  10715. (target-alist nil) tg
  10716. (head-count 0) cnt
  10717. (start 0)
  10718. ;; FIXME: The following returns always nil under XEmacs
  10719. (coding-system (and (fboundp 'coding-system-get)
  10720. (boundp 'buffer-file-coding-system)
  10721. buffer-file-coding-system))
  10722. (coding-system-for-write (or coding-system coding-system-for-write))
  10723. (save-buffer-coding-system (or coding-system save-buffer-coding-system))
  10724. (charset (and coding-system
  10725. (coding-system-get coding-system 'mime-charset)))
  10726. table-open type
  10727. table-buffer table-orig-buffer
  10728. ind start-is-num starter
  10729. rpl path desc desc1 desc2 link
  10730. )
  10731. (message "Exporting...")
  10732. (setq org-last-level 1)
  10733. (org-init-section-numbers)
  10734. ;; Search for the export key lines
  10735. (org-parse-key-lines)
  10736. (setq lang-words (or (assoc language org-export-language-setup)
  10737. (assoc "en" org-export-language-setup)))
  10738. ;; Switch to the output buffer
  10739. (if (or hidden (not org-export-html-show-new-buffer))
  10740. (set-buffer buffer)
  10741. (switch-to-buffer-other-window buffer))
  10742. (erase-buffer)
  10743. (fundamental-mode)
  10744. (let ((case-fold-search nil)
  10745. (org-odd-levels-only odd))
  10746. (if options (org-parse-export-options options))
  10747. (setq umax (if arg (prefix-numeric-value arg)
  10748. org-export-headline-levels))
  10749. ;; File header
  10750. (insert (format
  10751. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"
  10752. \"http://www.w3.org/TR/REC-html40/loose.dtd\">
  10753. <html lang=\"%s\"><head>
  10754. <title>%s</title>
  10755. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\">
  10756. <meta name=generator content=\"Org-mode\">
  10757. <meta name=generated content=\"%s %s\">
  10758. <meta name=author content=\"%s\">
  10759. %s
  10760. </head><body>
  10761. "
  10762. language (org-html-expand title) (or charset "iso-8859-1")
  10763. date time author style))
  10764. (if title (insert (concat "<H1 class=\"title\">"
  10765. (org-html-expand title) "</H1>\n")))
  10766. (if author (insert (concat (nth 1 lang-words) ": " author "\n")))
  10767. (if email (insert (concat "<a href=\"mailto:" email "\">&lt;"
  10768. email "&gt;</a>\n")))
  10769. (if (or author email) (insert "<br>\n"))
  10770. (if (and date time) (insert (concat (nth 2 lang-words) ": "
  10771. date " " time "<br>\n")))
  10772. (if text (insert (concat "<p>\n" (org-html-expand text))))
  10773. (if org-export-with-toc
  10774. (progn
  10775. (insert (format "<H2>%s</H2>\n" (nth 3 lang-words)))
  10776. (insert "<ul>\n")
  10777. (setq lines
  10778. (mapcar '(lambda (line)
  10779. (if (string-match org-todo-line-regexp line)
  10780. ;; This is a headline
  10781. (progn
  10782. (setq level (- (match-end 1) (match-beginning 1))
  10783. level (org-tr-level level)
  10784. txt (save-match-data
  10785. (org-html-expand
  10786. (match-string 3 line)))
  10787. todo
  10788. (or (and (match-beginning 2)
  10789. (not (equal (match-string 2 line)
  10790. org-done-string)))
  10791. ; TODO, not DONE
  10792. (and (= level umax)
  10793. (org-search-todo-below
  10794. line lines level))))
  10795. (if org-export-with-section-numbers
  10796. (setq txt (concat (org-section-number level)
  10797. " " txt)))
  10798. (if (<= level umax)
  10799. (progn
  10800. (setq head-count (+ head-count 1))
  10801. (if (> level org-last-level)
  10802. (progn
  10803. (setq cnt (- level org-last-level))
  10804. (while (>= (setq cnt (1- cnt)) 0)
  10805. (insert "<ul>"))
  10806. (insert "\n")))
  10807. (if (< level org-last-level)
  10808. (progn
  10809. (setq cnt (- org-last-level level))
  10810. (while (>= (setq cnt (1- cnt)) 0)
  10811. (insert "</ul>"))
  10812. (insert "\n")))
  10813. ;; Check for targets
  10814. (while (string-match org-target-regexp line)
  10815. (setq tg (match-string 1 line)
  10816. line (replace-match
  10817. (concat "@<span class=\"target\">" tg "@</span> ")
  10818. t t line))
  10819. (push (cons (org-solidify-link-text tg)
  10820. (format "sec-%d" head-count))
  10821. target-alist))
  10822. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  10823. (setq txt (replace-match "" t t txt)))
  10824. (insert
  10825. (format
  10826. (if todo
  10827. "<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>\n"
  10828. "<li><a href=\"#sec-%d\">%s</a>\n")
  10829. head-count txt))
  10830. (setq org-last-level level))
  10831. )))
  10832. line)
  10833. lines))
  10834. (while (> org-last-level 0)
  10835. (setq org-last-level (1- org-last-level))
  10836. (insert "</ul>\n"))
  10837. ))
  10838. (setq head-count 0)
  10839. (org-init-section-numbers)
  10840. (while (setq line (pop lines) origline line)
  10841. (catch 'nextline
  10842. ;; end of quote section?
  10843. (when (and inquote (string-match "^\\*+" line))
  10844. (insert "</pre>\n")
  10845. (setq inquote nil))
  10846. ;; inside a quote section?
  10847. (when inquote
  10848. (insert (org-html-protect line) "\n")
  10849. (throw 'nextline nil))
  10850. ;; verbatim lines
  10851. (when (and org-export-with-fixed-width
  10852. (string-match "^[ \t]*:\\(.*\\)" line))
  10853. (when (not infixed)
  10854. (setq infixed t)
  10855. (insert "<pre>\n"))
  10856. (insert (org-html-protect (match-string 1 line)) "\n")
  10857. (when (and lines
  10858. (not (string-match "^[ \t]*\\(:.*\\)"
  10859. (car lines))))
  10860. (setq infixed nil)
  10861. (insert "</pre>\n"))
  10862. (throw 'nextline nil))
  10863. ;; make targets to anchors
  10864. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  10865. (cond
  10866. ((match-end 2)
  10867. (setq line (replace-match
  10868. (concat "@<a name=\""
  10869. (org-solidify-link-text (match-string 1 line))
  10870. "\">\\nbsp@</a>")
  10871. t t line)))
  10872. ((and org-export-with-toc (equal (string-to-char line) ?*))
  10873. (setq line (replace-match
  10874. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  10875. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  10876. t t line)))
  10877. (t
  10878. (setq line (replace-match
  10879. (concat "@<a name=\""
  10880. (org-solidify-link-text (match-string 1 line))
  10881. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  10882. t t line)))))
  10883. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  10884. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  10885. (setq line (org-html-expand line))
  10886. ;; Format the links
  10887. (setq start 0)
  10888. (while (string-match org-bracket-link-analytic-regexp line start)
  10889. (setq start (match-beginning 0))
  10890. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  10891. (setq path (match-string 3 line))
  10892. (setq desc1 (if (match-end 5) (match-string 5 line))
  10893. desc2 (if (match-end 2) (concat type ":" path) path)
  10894. desc (or desc1 desc2))
  10895. (cond
  10896. ((equal type "internal")
  10897. (setq rpl
  10898. (concat
  10899. "<a href=\"#"
  10900. (org-solidify-link-text path target-alist)
  10901. "\">" desc "</a>")))
  10902. ((member type '("http" "https" "ftp" "mailto" "news"))
  10903. ;; standard URL
  10904. (setq link (concat type ":" path))
  10905. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  10906. ((string= type "file")
  10907. ;; FILE link
  10908. (let* ((filename path)
  10909. (abs-p (file-name-absolute-p filename))
  10910. (thefile (if abs-p (expand-file-name filename) filename))
  10911. (thefile (save-match-data
  10912. (if (string-match ":[0-9]+$" thefile)
  10913. (replace-match "" t t thefile)
  10914. thefile)))
  10915. (file-is-image-p
  10916. (save-match-data
  10917. (string-match (org-image-file-name-regexp) thefile))))
  10918. (setq rpl (if (and org-export-html-inline-images
  10919. file-is-image-p)
  10920. (concat "<img src=\"" thefile "\"/>")
  10921. (concat "<a href=\"" thefile "\">" desc "</a>")))))
  10922. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell"))
  10923. (setq rpl (concat "<i>&lt;" type ":" path "&gt;</i>"))))
  10924. (setq line (replace-match rpl t t line)
  10925. start (+ start (length rpl))))
  10926. ;; TODO items
  10927. (if (and (string-match org-todo-line-regexp line)
  10928. (match-beginning 2))
  10929. (if (equal (match-string 2 line) org-done-string)
  10930. (setq line (replace-match
  10931. "<span class=\"done\">\\2</span>"
  10932. nil nil line 2))
  10933. (setq line (replace-match "<span class=\"todo\">\\2</span>"
  10934. nil nil line 2))))
  10935. ;; DEADLINES
  10936. (if (string-match org-deadline-line-regexp line)
  10937. (progn
  10938. (if (save-match-data
  10939. (string-match "<a href"
  10940. (substring line 0 (match-beginning 0))))
  10941. nil ; Don't do the replacement - it is inside a link
  10942. (setq line (replace-match "<span class=\"deadline\">\\&</span>"
  10943. nil nil line 1)))))
  10944. (cond
  10945. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  10946. ;; This is a headline
  10947. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  10948. txt (match-string 2 line))
  10949. (if (<= level umax) (setq head-count (+ head-count 1)))
  10950. (when in-local-list
  10951. ;; Close any local lists before inserting a new header line
  10952. (while local-list-num
  10953. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  10954. (pop local-list-num))
  10955. (setq local-list-indent nil
  10956. in-local-list nil))
  10957. (org-html-level-start level txt umax
  10958. (and org-export-with-toc (<= level umax))
  10959. head-count)
  10960. ;; QUOTES
  10961. (when (string-match quote-re line)
  10962. (insert "<pre>")
  10963. (setq inquote t)))
  10964. ((and org-export-with-tables
  10965. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  10966. (if (not table-open)
  10967. ;; New table starts
  10968. (setq table-open t table-buffer nil table-orig-buffer nil))
  10969. ;; Accumulate lines
  10970. (setq table-buffer (cons line table-buffer)
  10971. table-orig-buffer (cons origline table-orig-buffer))
  10972. (when (or (not lines)
  10973. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  10974. (car lines))))
  10975. (setq table-open nil
  10976. table-buffer (nreverse table-buffer)
  10977. table-orig-buffer (nreverse table-orig-buffer))
  10978. (insert (org-format-table-html table-buffer table-orig-buffer))))
  10979. (t
  10980. ;; Normal lines
  10981. (when (and (> org-export-plain-list-max-depth 0)
  10982. (string-match
  10983. (cond
  10984. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+[.)]\\)\\)?\\( +[^ \t\n\r]\\|[ \t]*$\\)")
  10985. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+\\.\\)\\)?\\( +[^ \t\n\r]\\|[ \t]*$\\)")
  10986. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+)\\)\\)?\\( +[^ \t\n\r]\\|[ \t]*$\\)")
  10987. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  10988. line))
  10989. (setq ind (org-get-string-indentation line)
  10990. start-is-num (match-beginning 4)
  10991. starter (if (match-beginning 2) (match-string 2 line))
  10992. line (substring line (match-beginning 5)))
  10993. (unless (string-match "[^ \t]" line)
  10994. ;; empty line. Pretend indentation is large.
  10995. (setq ind (1+ (or (car local-list-indent) 1))))
  10996. (while (and in-local-list
  10997. (or (and (= ind (car local-list-indent))
  10998. (not starter))
  10999. (< ind (car local-list-indent))))
  11000. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  11001. (pop local-list-num) (pop local-list-indent)
  11002. (setq in-local-list local-list-indent))
  11003. (cond
  11004. ((and starter
  11005. (or (not in-local-list)
  11006. (> ind (car local-list-indent)))
  11007. (< (length local-list-indent)
  11008. org-export-plain-list-max-depth))
  11009. ;; Start new (level of ) list
  11010. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  11011. (push start-is-num local-list-num)
  11012. (push ind local-list-indent)
  11013. (setq in-local-list t))
  11014. (starter
  11015. ;; continue current list
  11016. (insert "<li>\n"))))
  11017. ;; Empty lines start a new paragraph. If hand-formatted lists
  11018. ;; are not fully interpreted, lines starting with "-", "+", "*"
  11019. ;; also start a new paragraph.
  11020. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (insert "<p>"))
  11021. (insert line (if org-export-preserve-breaks "<br>\n" "\n"))))
  11022. ))
  11023. (if org-export-html-with-timestamp
  11024. (insert org-export-html-html-helper-timestamp))
  11025. (insert "</body>\n</html>\n")
  11026. (normal-mode)
  11027. (save-buffer)
  11028. (goto-char (point-min)))))
  11029. (defun org-format-table-html (lines olines)
  11030. "Find out which HTML converter to use and return the HTML code."
  11031. (if (string-match "^[ \t]*|" (car lines))
  11032. ;; A normal org table
  11033. (org-format-org-table-html lines)
  11034. ;; Table made by table.el - test for spanning
  11035. (let* ((hlines (delq nil (mapcar
  11036. (lambda (x)
  11037. (if (string-match "^[ \t]*\\+-" x) x
  11038. nil))
  11039. lines)))
  11040. (first (car hlines))
  11041. (ll (and (string-match "\\S-+" first)
  11042. (match-string 0 first)))
  11043. (re (concat "^[ \t]*" (regexp-quote ll)))
  11044. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  11045. hlines))))
  11046. (if (and (not spanning)
  11047. (not org-export-prefer-native-exporter-for-tables))
  11048. ;; We can use my own converter with HTML conversions
  11049. (org-format-table-table-html lines)
  11050. ;; Need to use the code generator in table.el, with the original text.
  11051. (org-format-table-table-html-using-table-generate-source olines)))))
  11052. (defun org-format-org-table-html (lines)
  11053. "Format a table into HTML."
  11054. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  11055. (setq lines (nreverse lines))
  11056. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  11057. (setq lines (nreverse lines))
  11058. (when org-export-table-remove-special-lines
  11059. ;; Check if the table has a marking column. If yes remove the
  11060. ;; column and the special lines
  11061. (let* ((special
  11062. (not
  11063. (memq nil
  11064. (mapcar
  11065. (lambda (x)
  11066. (or (string-match "^[ \t]*|-" x)
  11067. (string-match "^[ \t]*| *\\([#!$*_^ ]\\) *|" x)))
  11068. lines)))))
  11069. (if special
  11070. (setq lines
  11071. (delq nil
  11072. (mapcar
  11073. (lambda (x)
  11074. (if (string-match "^[ \t]*| *[!_^] *|" x)
  11075. nil ; ignore this line
  11076. (and (or (string-match "^[ \t]*|-+\\+" x)
  11077. (string-match "^[ \t]*|[^|]*|" x))
  11078. (replace-match "|" t t x))))
  11079. lines))))))
  11080. (let ((head (and org-export-highlight-first-table-line
  11081. (delq nil (mapcar
  11082. (lambda (x) (string-match "^[ \t]*|-" x))
  11083. (cdr lines)))))
  11084. line fields html)
  11085. (setq html (concat org-export-html-table-tag "\n"))
  11086. (while (setq line (pop lines))
  11087. (catch 'next-line
  11088. (if (string-match "^[ \t]*|-" line)
  11089. (progn
  11090. (setq head nil) ;; head ends here, first time around
  11091. ;; ignore this line
  11092. (throw 'next-line t)))
  11093. ;; Break the line into fields
  11094. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  11095. (setq html (concat
  11096. html
  11097. "<tr>"
  11098. (mapconcat (lambda (x)
  11099. (if head
  11100. (concat "<th>" x "</th>")
  11101. (concat "<td>" x "</td>")))
  11102. fields "")
  11103. "</tr>\n"))))
  11104. (setq html (concat html "</table>\n"))
  11105. html))
  11106. (defun org-fake-empty-table-line (line)
  11107. "Replace everything except \"|\" with spaces."
  11108. (let ((i (length line))
  11109. (newstr (copy-sequence line)))
  11110. (while (> i 0)
  11111. (setq i (1- i))
  11112. (if (not (eq (aref newstr i) ?|))
  11113. (aset newstr i ?\ )))
  11114. newstr))
  11115. (defun org-format-table-table-html (lines)
  11116. "Format a table generated by table.el into HTML.
  11117. This conversion does *not* use `table-generate-source' from table.el.
  11118. This has the advantage that Org-mode's HTML conversions can be used.
  11119. But it has the disadvantage, that no cell- or row-spanning is allowed."
  11120. (let (line field-buffer
  11121. (head org-export-highlight-first-table-line)
  11122. fields html empty)
  11123. (setq html (concat org-export-html-table-tag "\n"))
  11124. (while (setq line (pop lines))
  11125. (setq empty "&nbsp")
  11126. (catch 'next-line
  11127. (if (string-match "^[ \t]*\\+-" line)
  11128. (progn
  11129. (if field-buffer
  11130. (progn
  11131. (setq html (concat
  11132. html
  11133. "<tr>"
  11134. (mapconcat
  11135. (lambda (x)
  11136. (if (equal x "") (setq x empty))
  11137. (if head
  11138. (concat "<th>" x "</th>\n")
  11139. (concat "<td>" x "</td>\n")))
  11140. field-buffer "\n")
  11141. "</tr>\n"))
  11142. (setq head nil)
  11143. (setq field-buffer nil)))
  11144. ;; Ignore this line
  11145. (throw 'next-line t)))
  11146. ;; Break the line into fields and store the fields
  11147. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  11148. (if field-buffer
  11149. (setq field-buffer (mapcar
  11150. (lambda (x)
  11151. (concat x "<br>" (pop fields)))
  11152. field-buffer))
  11153. (setq field-buffer fields))))
  11154. (setq html (concat html "</table>\n"))
  11155. html))
  11156. (defun org-format-table-table-html-using-table-generate-source (lines)
  11157. "Format a table into html, using `table-generate-source' from table.el.
  11158. This has the advantage that cell- or row-spanning is allowed.
  11159. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  11160. (require 'table)
  11161. (with-current-buffer (get-buffer-create " org-tmp1 ")
  11162. (erase-buffer)
  11163. (insert (mapconcat 'identity lines "\n"))
  11164. (goto-char (point-min))
  11165. (if (not (re-search-forward "|[^+]" nil t))
  11166. (error "Error processing table"))
  11167. (table-recognize-table)
  11168. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  11169. (table-generate-source 'html " org-tmp2 ")
  11170. (set-buffer " org-tmp2 ")
  11171. (buffer-substring (point-min) (point-max))))
  11172. (defun org-html-protect (s)
  11173. ;; convert & to &amp;, < to &lt; and > to &gt;
  11174. (let ((start 0))
  11175. (while (string-match "&" s start)
  11176. (setq s (replace-match "&amp;" t t s)
  11177. start (1+ (match-beginning 0))))
  11178. (while (string-match "<" s)
  11179. (setq s (replace-match "&lt;" t t s)))
  11180. (while (string-match ">" s)
  11181. (setq s (replace-match "&gt;" t t s))))
  11182. s)
  11183. (defun org-html-expand (string)
  11184. "Prepare STRING for HTML export. Applies all active conversions."
  11185. ;; First check if there is a link in the line - if yes, apply conversions
  11186. ;; only before the start of the link.
  11187. ;; FIXME: This is no longer correct, because links now have an end.
  11188. (let* ((m (string-match org-link-regexp string))
  11189. (s (if m (substring string 0 m) string))
  11190. (r (if m (substring string m) "")))
  11191. ;; convert & to &amp;, < to &lt; and > to &gt;
  11192. (setq s (org-html-protect s))
  11193. (if org-export-html-expand
  11194. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  11195. (setq s (replace-match "<\\1>" nil nil s))))
  11196. (if org-export-with-emphasize
  11197. (setq s (org-export-html-convert-emphasize s)))
  11198. (if org-export-with-sub-superscripts
  11199. (setq s (org-export-html-convert-sub-super s)))
  11200. (if org-export-with-TeX-macros
  11201. (let ((start 0) wd ass)
  11202. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  11203. (setq wd (match-string 1 s))
  11204. (if (setq ass (assoc wd org-html-entities))
  11205. (setq s (replace-match (or (cdr ass)
  11206. (concat "&" (car ass) ";"))
  11207. t t s))
  11208. (setq start (+ start (length wd)))))))
  11209. (concat s r)))
  11210. (defun org-create-multibrace-regexp (left right n)
  11211. "Create a regular expression which will match a balanced sexp.
  11212. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  11213. as single character strings.
  11214. The regexp returned will match the entire expression including the
  11215. delimiters. It will also define a single group which contains the
  11216. match except for the outermost delimiters. The maximum depth of
  11217. stacked delimiters is N. Escaping delimiters is not possible."
  11218. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  11219. (or "\\|")
  11220. (re nothing)
  11221. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  11222. (while (> n 1)
  11223. (setq n (1- n)
  11224. re (concat re or next)
  11225. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  11226. (concat left "\\(" re "\\)" right)))
  11227. (defvar org-match-substring-regexp
  11228. (concat
  11229. "\\([^\\]\\)\\([_^]\\)\\("
  11230. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  11231. "\\|"
  11232. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  11233. "\\|"
  11234. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  11235. "The regular expression matching a sub- or superscript.")
  11236. (defun org-export-html-convert-sub-super (string)
  11237. "Convert sub- and superscripts in STRING to HTML."
  11238. (let (key c)
  11239. (while (string-match org-match-substring-regexp string)
  11240. (setq key (if (string= (match-string 2 string) "_") "sub" "sup"))
  11241. (setq c (or (match-string 8 string)
  11242. (match-string 6 string)
  11243. (match-string 5 string)))
  11244. (setq string (replace-match
  11245. (concat (match-string 1 string)
  11246. "<" key ">" c "</" key ">")
  11247. t t string)))
  11248. (while (string-match "\\\\\\([_^]\\)" string)
  11249. (setq string (replace-match (match-string 1 string) t t string))))
  11250. string)
  11251. (defun org-export-html-convert-emphasize (string)
  11252. (let (c (s 0))
  11253. (while (string-match "\\(\\W\\|^\\)\\([*/_]\\)\\(\\w+\\)\\2\\(\\W\\|$\\)" string s)
  11254. (setq c (cdr (assoc (match-string 2 string)
  11255. '(("*" . "b") ("/" . "i") ("_" . "u"))))
  11256. s (+ (match-end 0) 3)
  11257. string (replace-match
  11258. (concat "\\1<" c ">\\3</" c ">\\4") t nil string)))
  11259. string))
  11260. (defun org-parse-key-lines ()
  11261. "Find the special key lines with the information for exporters."
  11262. (save-excursion
  11263. (goto-char 0)
  11264. (let ((re (org-make-options-regexp
  11265. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  11266. key)
  11267. (while (re-search-forward re nil t)
  11268. (setq key (match-string 1))
  11269. (cond ((string-equal key "TITLE")
  11270. (setq title (match-string 2)))
  11271. ((string-equal key "AUTHOR")
  11272. (setq author (match-string 2)))
  11273. ((string-equal key "EMAIL")
  11274. (setq email (match-string 2)))
  11275. ((string-equal key "LANGUAGE")
  11276. (setq language (match-string 2)))
  11277. ((string-equal key "TEXT")
  11278. (setq text (concat text "\n" (match-string 2))))
  11279. ((string-equal key "OPTIONS")
  11280. (setq options (match-string 2))))))))
  11281. (defun org-parse-export-options (s)
  11282. "Parse the export options line."
  11283. (let ((op '(("H" . org-export-headline-levels)
  11284. ("num" . org-export-with-section-numbers)
  11285. ("toc" . org-export-with-toc)
  11286. ("\\n" . org-export-preserve-breaks)
  11287. ("@" . org-export-html-expand)
  11288. (":" . org-export-with-fixed-width)
  11289. ("|" . org-export-with-tables)
  11290. ("^" . org-export-with-sub-superscripts)
  11291. ("*" . org-export-with-emphasize)
  11292. ("TeX" . org-export-with-TeX-macros)))
  11293. o)
  11294. (while (setq o (pop op))
  11295. (if (string-match (concat (regexp-quote (car o)) ":\\([^ \t\n\r;,.]*\\)")
  11296. s)
  11297. (set (make-local-variable (cdr o))
  11298. (car (read-from-string (match-string 1 s))))))))
  11299. (defun org-html-level-start (level title umax with-toc head-count)
  11300. "Insert a new level in HTML export."
  11301. (let ((l (1+ (max level umax))))
  11302. (while (<= l org-level-max)
  11303. (if (aref levels-open (1- l))
  11304. (progn
  11305. (org-html-level-close l)
  11306. (aset levels-open (1- l) nil)))
  11307. (setq l (1+ l)))
  11308. (if (> level umax)
  11309. (progn
  11310. (if (aref levels-open (1- level))
  11311. (insert "<li>" title "<p>\n")
  11312. (aset levels-open (1- level) t)
  11313. (insert "<ul><li>" title "<p>\n")))
  11314. (if org-export-with-section-numbers
  11315. (setq title (concat (org-section-number level) " " title)))
  11316. (setq level (+ level 1))
  11317. (if with-toc
  11318. (insert (format "\n<H%d><a name=\"sec-%d\">%s</a></H%d>\n"
  11319. level head-count title level))
  11320. (insert (format "\n<H%d>%s</H%d>\n" level title level))))))
  11321. (defun org-html-level-close (&rest args)
  11322. "Terminate one level in HTML export."
  11323. (insert "</ul>"))
  11324. ;; Variable holding the vector with section numbers
  11325. (defvar org-section-numbers (make-vector org-level-max 0))
  11326. (defun org-init-section-numbers ()
  11327. "Initialize the vector for the section numbers."
  11328. (let* ((level -1)
  11329. (numbers (nreverse (org-split-string "" "\\.")))
  11330. (depth (1- (length org-section-numbers)))
  11331. (i depth) number-string)
  11332. (while (>= i 0)
  11333. (if (> i level)
  11334. (aset org-section-numbers i 0)
  11335. (setq number-string (or (car numbers) "0"))
  11336. (if (string-match "\\`[A-Z]\\'" number-string)
  11337. (aset org-section-numbers i
  11338. (- (string-to-char number-string) ?A -1))
  11339. (aset org-section-numbers i (string-to-number number-string)))
  11340. (pop numbers))
  11341. (setq i (1- i)))))
  11342. (defun org-section-number (&optional level)
  11343. "Return a string with the current section number.
  11344. When LEVEL is non-nil, increase section numbers on that level."
  11345. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  11346. (when level
  11347. (when (> level -1)
  11348. (aset org-section-numbers
  11349. level (1+ (aref org-section-numbers level))))
  11350. (setq idx (1+ level))
  11351. (while (<= idx depth)
  11352. (if (not (= idx 1))
  11353. (aset org-section-numbers idx 0))
  11354. (setq idx (1+ idx))))
  11355. (setq idx 0)
  11356. (while (<= idx depth)
  11357. (setq n (aref org-section-numbers idx))
  11358. (setq string (concat string (if (not (string= string "")) "." "")
  11359. (int-to-string n)))
  11360. (setq idx (1+ idx)))
  11361. (save-match-data
  11362. (if (string-match "\\`\\([@0]\\.\\)+" string)
  11363. (setq string (replace-match "" nil nil string)))
  11364. (if (string-match "\\(\\.0\\)+\\'" string)
  11365. (setq string (replace-match "" nil nil string))))
  11366. string))
  11367. (defun org-export-icalendar-this-file ()
  11368. "Export current file as an iCalendar file.
  11369. The iCalendar file will be located in the same directory as the Org-mode
  11370. file, but with extension `.ics'."
  11371. (interactive)
  11372. (org-export-icalendar nil buffer-file-name))
  11373. ;;;###autoload
  11374. (defun org-export-icalendar-all-agenda-files ()
  11375. "Export all files in `org-agenda-files' to iCalendar .ics files.
  11376. Each iCalendar file will be located in the same directory as the Org-mode
  11377. file, but with extension `.ics'."
  11378. (interactive)
  11379. (apply 'org-export-icalendar nil (org-agenda-files t)))
  11380. ;;;###autoload
  11381. (defun org-export-icalendar-combine-agenda-files ()
  11382. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  11383. The file is stored under the name `org-combined-agenda-icalendar-file'."
  11384. (interactive)
  11385. (apply 'org-export-icalendar t (org-agenda-files t)))
  11386. (defun org-export-icalendar (combine &rest files)
  11387. "Create iCalendar files for all elements of FILES.
  11388. If COMBINE is non-nil, combine all calendar entries into a single large
  11389. file and store it under the name `org-combined-agenda-icalendar-file'."
  11390. (save-excursion
  11391. (let* (file ical-file ical-buffer category started org-agenda-new-buffers)
  11392. (when combine
  11393. (setq ical-file org-combined-agenda-icalendar-file
  11394. ical-buffer (org-get-agenda-file-buffer ical-file))
  11395. (set-buffer ical-buffer) (erase-buffer))
  11396. (while (setq file (pop files))
  11397. (catch 'nextfile
  11398. (org-check-agenda-file file)
  11399. (unless combine
  11400. (setq ical-file (concat (file-name-sans-extension file) ".ics"))
  11401. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  11402. (set-buffer ical-buffer) (erase-buffer))
  11403. (set-buffer (org-get-agenda-file-buffer file))
  11404. (setq category (or org-category
  11405. (file-name-sans-extension
  11406. (file-name-nondirectory buffer-file-name))))
  11407. (if (symbolp category) (setq category (symbol-name category)))
  11408. (let ((standard-output ical-buffer))
  11409. (if combine
  11410. (and (not started) (setq started t)
  11411. (org-start-icalendar-file org-icalendar-combined-name))
  11412. (org-start-icalendar-file category))
  11413. (org-print-icalendar-entries combine category)
  11414. (when (or (and combine (not files)) (not combine))
  11415. (org-finish-icalendar-file)
  11416. (set-buffer ical-buffer)
  11417. (save-buffer)
  11418. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  11419. (org-release-buffers org-agenda-new-buffers))))
  11420. (defvar org-after-save-iCalendar-file-hook nil
  11421. "Hook run after an iCalendar file has been saved.
  11422. The iCalendar buffer is still current when this hook is run.
  11423. A good way to use this is to tell a desktop calenndar application to re-read
  11424. the iCalendar file.")
  11425. (defun org-print-icalendar-entries (&optional combine category)
  11426. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  11427. When COMBINE is non nil, add the category to each line."
  11428. (let ((re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  11429. (dts (org-ical-ts-to-string
  11430. (format-time-string (cdr org-time-stamp-formats) (current-time))
  11431. "DTSTART"))
  11432. hd ts ts2 state (inc t) pos scheduledp deadlinep tmp pri)
  11433. (save-excursion
  11434. (goto-char (point-min))
  11435. (while (re-search-forward org-ts-regexp nil t)
  11436. (setq pos (match-beginning 0)
  11437. ts (match-string 0)
  11438. inc t
  11439. hd (org-get-heading))
  11440. (if (looking-at re2)
  11441. (progn
  11442. (goto-char (match-end 0))
  11443. (setq ts2 (match-string 1) inc nil))
  11444. (setq ts2 ts
  11445. tmp (buffer-substring (max (point-min)
  11446. (- pos org-ds-keyword-length))
  11447. pos)
  11448. deadlinep (string-match org-deadline-regexp tmp)
  11449. scheduledp (string-match org-scheduled-regexp tmp)
  11450. ;; donep (org-entry-is-done-p)
  11451. ))
  11452. (if (or (string-match org-tr-regexp hd)
  11453. (string-match org-ts-regexp hd))
  11454. (setq hd (replace-match "" t t hd)))
  11455. (if combine
  11456. (setq hd (concat hd " (category " category ")")))
  11457. (if deadlinep (setq hd (concat "DL: " hd " This is a deadline")))
  11458. (if scheduledp (setq hd (concat "S: " hd " Scheduled for this date")))
  11459. (princ (format "BEGIN:VEVENT
  11460. %s
  11461. %s
  11462. SUMMARY:%s
  11463. END:VEVENT\n"
  11464. (org-ical-ts-to-string ts "DTSTART")
  11465. (org-ical-ts-to-string ts2 "DTEND" inc)
  11466. hd)))
  11467. (when org-icalendar-include-todo
  11468. (goto-char (point-min))
  11469. (while (re-search-forward org-todo-line-regexp nil t)
  11470. (setq state (match-string 1))
  11471. (unless (equal state org-done-string)
  11472. (setq hd (match-string 3))
  11473. (if (string-match org-priority-regexp hd)
  11474. (setq pri (string-to-char (match-string 2 hd))
  11475. hd (concat (substring hd 0 (match-beginning 1))
  11476. (substring hd (- (match-end 1)))))
  11477. (setq pri org-default-priority))
  11478. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  11479. (- org-lowest-priority ?A))))))
  11480. (princ (format "BEGIN:VTODO
  11481. %s
  11482. SUMMARY:%s
  11483. SEQUENCE:1
  11484. PRIORITY:%d
  11485. END:VTODO\n"
  11486. dts hd pri))))))))
  11487. (defun org-start-icalendar-file (name)
  11488. "Start an iCalendar file by inserting the header."
  11489. (let ((user user-full-name)
  11490. (name (or name "unknown"))
  11491. (timezone (cadr (current-time-zone))))
  11492. (princ
  11493. ;; FIXME: Should we use VERSION 1? MS Outlook would then work, it seems
  11494. (format "BEGIN:VCALENDAR
  11495. VERSION:2.0
  11496. X-WR-CALNAME:%s
  11497. PRODID:-//%s//Emacs with Org-mode//EN
  11498. X-WR-TIMEZONE:%s
  11499. CALSCALE:GREGORIAN\n" name user timezone))))
  11500. (defun org-finish-icalendar-file ()
  11501. "Finish an iCalendar file by inserting the END statement."
  11502. (princ "END:VCALENDAR\n"))
  11503. (defun org-ical-ts-to-string (s keyword &optional inc)
  11504. "Take a time string S and convert it to iCalendar format.
  11505. KEYWORD is added in front, to make a complete line like DTSTART....
  11506. When INC is non-nil, increase the hour by two (if time string contains
  11507. a time), or the day by one (if it does not contain a time)."
  11508. (let ((t1 (org-parse-time-string s 'nodefault))
  11509. t2 fmt have-time time)
  11510. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  11511. (setq t2 t1 have-time t)
  11512. (setq t2 (org-parse-time-string s)))
  11513. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  11514. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  11515. (when inc
  11516. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  11517. (setq time (encode-time s mi h d m y)))
  11518. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  11519. (concat keyword (format-time-string fmt time))))
  11520. ;;; Key bindings
  11521. ;; - Bindings in Org-mode map are currently
  11522. ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
  11523. ;; abcd fgh j lmnopqrstuvwxyz!? #$ ^ -+*/= [] ; |,.<>~ '\t necessary bindings
  11524. ;; e (?) useful from outline-mode
  11525. ;; i k @ expendable from outline-mode
  11526. ;; 0123456789 % & ()_{} " ` free
  11527. ;; Make `C-c C-x' a prefix key
  11528. (define-key org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11529. ;; TAB key with modifiers
  11530. (define-key org-mode-map "\C-i" 'org-cycle)
  11531. (define-key org-mode-map [(tab)] 'org-cycle)
  11532. (define-key org-mode-map [(meta tab)] 'org-complete)
  11533. (define-key org-mode-map "\M-\C-i" 'org-complete) ; for tty emacs
  11534. ;; The following line is necessary under Suse GNU/Linux
  11535. (unless (featurep 'xemacs)
  11536. (define-key org-mode-map [S-iso-lefttab] 'org-shifttab))
  11537. (define-key org-mode-map [(shift tab)] 'org-shifttab)
  11538. (define-key org-mode-map (org-key 'S-return) 'org-table-copy-down)
  11539. (define-key org-mode-map "\C-c\C-xc" 'org-table-copy-down) ; tty
  11540. (define-key org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11541. (define-key org-mode-map "\C-c\C-xM" 'org-insert-todo-heading) ; tty
  11542. (define-key org-mode-map [(meta return)] 'org-meta-return)
  11543. (define-key org-mode-map "\C-c\C-xm" 'org-meta-return) ; tty emacs
  11544. (define-key org-mode-map [?\e (return)] 'org-meta-return) ; tty emacs
  11545. ;; Cursor keys with modifiers
  11546. (define-key org-mode-map [(meta left)] 'org-metaleft)
  11547. (define-key org-mode-map [?\e (left)] 'org-metaleft) ; for tty emacs
  11548. (define-key org-mode-map "\C-c\C-xl" 'org-metaleft) ; for tty emacs
  11549. (define-key org-mode-map [(meta right)] 'org-metaright)
  11550. (define-key org-mode-map [?\e (right)] 'org-metaright) ; for tty emacs
  11551. (define-key org-mode-map "\C-c\C-xr" 'org-metaright) ; for tty emacs
  11552. (define-key org-mode-map [(meta up)] 'org-metaup)
  11553. (define-key org-mode-map [?\e (up)] 'org-metaup) ; for tty emacs
  11554. (define-key org-mode-map "\C-c\C-xu" 'org-metaup) ; for tty emacs
  11555. (define-key org-mode-map [(meta down)] 'org-metadown)
  11556. (define-key org-mode-map [?\e (down)] 'org-metadown) ; for tty emacs
  11557. (define-key org-mode-map "\C-c\C-xd" 'org-metadown) ; for tty emacs
  11558. (define-key org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11559. (define-key org-mode-map "\C-c\C-xL" 'org-shiftmetaleft) ; tty
  11560. (define-key org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11561. (define-key org-mode-map "\C-c\C-xR" 'org-shiftmetaright) ; tty
  11562. (define-key org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11563. (define-key org-mode-map "\C-c\C-xU" 'org-shiftmetaup) ; tty
  11564. (define-key org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11565. (define-key org-mode-map "\C-c\C-xD" 'org-shiftmetadown) ; tty
  11566. (define-key org-mode-map (org-key 'S-up) 'org-shiftup)
  11567. (define-key org-mode-map [?\C-c ?\C-x (up)] 'org-shiftup)
  11568. (define-key org-mode-map (org-key 'S-down) 'org-shiftdown)
  11569. (define-key org-mode-map [?\C-c ?\C-x (down)] 'org-shiftdown)
  11570. (define-key org-mode-map (org-key 'S-left) 'org-shiftleft)
  11571. (define-key org-mode-map [?\C-c ?\C-x (left)] 'org-shiftleft)
  11572. (define-key org-mode-map (org-key 'S-right) 'org-shiftright)
  11573. (define-key org-mode-map [?\C-c ?\C-x (right)] 'org-shiftright)
  11574. ;; All the other keys
  11575. (define-key org-mode-map "\C-c$" 'org-archive-subtree)
  11576. (define-key org-mode-map "\C-c\C-j" 'org-goto)
  11577. (define-key org-mode-map "\C-c\C-t" 'org-todo)
  11578. (define-key org-mode-map "\C-c\C-s" 'org-schedule)
  11579. (define-key org-mode-map "\C-c\C-d" 'org-deadline)
  11580. (define-key org-mode-map "\C-c;" 'org-toggle-comment)
  11581. (define-key org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11582. (define-key org-mode-map "\C-c\C-w" 'org-check-deadlines)
  11583. (define-key org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  11584. (define-key org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11585. (define-key org-mode-map "\C-c\C-m" 'org-insert-heading)
  11586. (define-key org-mode-map "\M-\C-m" 'org-insert-heading)
  11587. (define-key org-mode-map "\C-c\C-l" 'org-insert-link)
  11588. (define-key org-mode-map "\C-c\C-o" 'org-open-at-point)
  11589. (define-key org-mode-map "\C-c%" 'org-mark-ring-push)
  11590. (define-key org-mode-map "\C-c&" 'org-mark-ring-goto)
  11591. (define-key org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  11592. (define-key org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11593. (define-key org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11594. (define-key org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11595. (define-key org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11596. (define-key org-mode-map "\C-c>" 'org-goto-calendar)
  11597. (define-key org-mode-map "\C-c<" 'org-date-from-calendar)
  11598. (define-key org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11599. (define-key org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11600. (define-key org-mode-map "\C-c]" 'org-remove-file)
  11601. (define-key org-mode-map "\C-c\C-r" 'org-timeline)
  11602. (define-key org-mode-map "\C-c-" 'org-table-insert-hline)
  11603. (define-key org-mode-map "\C-c^" 'org-table-sort-lines)
  11604. (define-key org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11605. (define-key org-mode-map "\C-m" 'org-return)
  11606. (define-key org-mode-map "\C-c?" 'org-table-current-column)
  11607. (define-key org-mode-map "\C-c " 'org-table-blank-field)
  11608. (define-key org-mode-map "\C-c+" 'org-table-sum)
  11609. (define-key org-mode-map "\C-c|" 'org-table-toggle-vline-visibility)
  11610. (define-key org-mode-map "\C-c=" 'org-table-eval-formula)
  11611. (define-key org-mode-map "\C-c'" 'org-table-edit-formulas)
  11612. (define-key org-mode-map "\C-c`" 'org-table-edit-field)
  11613. (define-key org-mode-map "\C-c*" 'org-table-recalculate)
  11614. (define-key org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11615. (define-key org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11616. (define-key org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  11617. (define-key org-mode-map "\C-c\C-xa" 'org-export-as-ascii)
  11618. (define-key org-mode-map "\C-c\C-x\C-a" 'org-export-as-ascii)
  11619. (define-key org-mode-map "\C-c\C-xv" 'org-export-copy-visible)
  11620. (define-key org-mode-map "\C-c\C-x\C-v" 'org-export-copy-visible)
  11621. ;; OPML support is only an option for the future
  11622. ;(define-key org-mode-map "\C-c\C-xo" 'org-export-as-opml)
  11623. ;(define-key org-mode-map "\C-c\C-x\C-o" 'org-export-as-opml)
  11624. (define-key org-mode-map "\C-c\C-xi" 'org-export-icalendar-this-file)
  11625. (define-key org-mode-map "\C-c\C-x\C-i" 'org-export-icalendar-all-agenda-files)
  11626. (define-key org-mode-map "\C-c\C-xc" 'org-export-icalendar-combine-agenda-files)
  11627. (define-key org-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  11628. (define-key org-mode-map "\C-c\C-xt" 'org-insert-export-options-template)
  11629. (define-key org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11630. (define-key org-mode-map "\C-c\C-xh" 'org-export-as-html)
  11631. (define-key org-mode-map "\C-c\C-xx" 'org-export-as-xml)
  11632. (define-key org-mode-map "\C-c\C-x\C-x" 'org-export-as-xml)
  11633. (define-key org-mode-map "\C-c\C-xb" 'org-export-as-html-and-open)
  11634. (define-key org-mode-map "\C-c\C-x\C-b" 'org-export-as-html-and-open)
  11635. (define-key org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  11636. (define-key org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11637. (define-key org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11638. (define-key org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11639. (defsubst org-table-p () (org-at-table-p))
  11640. (defun org-self-insert-command (N)
  11641. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11642. If the cursor is in a table looking at whitespace, the whitespace is
  11643. overwritten, and the table is not marked as requiring realignment."
  11644. (interactive "p")
  11645. (if (and (org-table-p)
  11646. (progn
  11647. ;; check if we blank the field, and if that triggers align
  11648. (and org-table-auto-blank-field
  11649. (member last-command
  11650. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11651. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11652. ;; got extra space, this field does not determine column width
  11653. (let (org-table-may-need-update) (org-table-blank-field))
  11654. ;; no extra space, this field may determine column width
  11655. (org-table-blank-field)))
  11656. t)
  11657. (eq N 1)
  11658. (looking-at "[^|\n]* |"))
  11659. (let (org-table-may-need-update)
  11660. (goto-char (1- (match-end 0)))
  11661. (delete-backward-char 1)
  11662. (goto-char (match-beginning 0))
  11663. (self-insert-command N))
  11664. (setq org-table-may-need-update t)
  11665. (self-insert-command N)))
  11666. (defun org-delete-backward-char (N)
  11667. "Like `delete-backward-char', insert whitespace at field end in tables.
  11668. When deleting backwards, in tables this function will insert whitespace in
  11669. front of the next \"|\" separator, to keep the table aligned. The table will
  11670. still be marked for re-alignment if the field did fill the entire column,
  11671. because, in this case the deletion might narrow the column."
  11672. (interactive "p")
  11673. (if (and (org-table-p)
  11674. (eq N 1)
  11675. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11676. (looking-at ".*?|"))
  11677. (let ((pos (point))
  11678. (noalign (looking-at "[^|\n\r]* |"))
  11679. (c org-table-may-need-update))
  11680. (backward-delete-char N)
  11681. (skip-chars-forward "^|")
  11682. (insert " ")
  11683. (goto-char (1- pos))
  11684. ;; noalign: if there were two spaces at the end, this field
  11685. ;; does not determine the width of the column.
  11686. (if noalign (setq org-table-may-need-update c)))
  11687. (backward-delete-char N)))
  11688. (defun org-delete-char (N)
  11689. "Like `delete-char', but insert whitespace at field end in tables.
  11690. When deleting characters, in tables this function will insert whitespace in
  11691. front of the next \"|\" separator, to keep the table aligned. The table will
  11692. still be marked for re-alignment if the field did fill the entire column,
  11693. because, in this case the deletion might narrow the column."
  11694. (interactive "p")
  11695. (if (and (org-table-p)
  11696. (not (bolp))
  11697. (not (= (char-after) ?|))
  11698. (eq N 1))
  11699. (if (looking-at ".*?|")
  11700. (let ((pos (point))
  11701. (noalign (looking-at "[^|\n\r]* |"))
  11702. (c org-table-may-need-update))
  11703. (replace-match (concat
  11704. (substring (match-string 0) 1 -1)
  11705. " |"))
  11706. (goto-char pos)
  11707. ;; noalign: if there were two spaces at the end, this field
  11708. ;; does not determine the width of the column.
  11709. (if noalign (setq org-table-may-need-update c))))
  11710. (delete-char N)))
  11711. ;; How to do this: Measure non-white length of current string
  11712. ;; If equal to column width, we should realign.
  11713. (defun org-remap (map &rest commands)
  11714. "In MAP, remap the functions given in COMMANDS.
  11715. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11716. (let (new old)
  11717. (while commands
  11718. (setq old (pop commands) new (pop commands))
  11719. (if (fboundp 'command-remapping)
  11720. (define-key map (vector 'remap old) new)
  11721. (substitute-key-definition old new map global-map)))))
  11722. (when (eq org-enable-table-editor 'optimized)
  11723. ;; If the user wants maximum table support, we need to hijack
  11724. ;; some standard editing functions
  11725. (org-remap org-mode-map
  11726. 'self-insert-command 'org-self-insert-command
  11727. 'delete-char 'org-delete-char
  11728. 'delete-backward-char 'org-delete-backward-char)
  11729. (define-key org-mode-map "|" 'org-force-self-insert))
  11730. (defun org-shiftcursor-error ()
  11731. "Throw an error because Shift-Cursor command was applied in wrong context."
  11732. (error "This command is active in special context like tables, headlines or timestamps"))
  11733. (defun org-shifttab ()
  11734. "Global visibility cycling or move to previous table field.
  11735. Calls `(org-cycle t)' or `org-table-previous-field', depending on context.
  11736. See the individual commands for more information."
  11737. (interactive)
  11738. (cond
  11739. ((org-at-table-p) (org-table-previous-field))
  11740. (t (org-cycle '(4)))))
  11741. (defun org-shiftmetaleft ()
  11742. "Promote subtree or delete table column.
  11743. Calls `org-promote-subtree' or `org-table-delete-column', depending on context.
  11744. See the individual commands for more information."
  11745. (interactive)
  11746. (cond
  11747. ((org-at-table-p) (org-table-delete-column))
  11748. ((org-on-heading-p) (org-promote-subtree))
  11749. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11750. (t (org-shiftcursor-error))))
  11751. (defun org-shiftmetaright ()
  11752. "Demote subtree or insert table column.
  11753. Calls `org-demote-subtree' or `org-table-insert-column', depending on context.
  11754. See the individual commands for more information."
  11755. (interactive)
  11756. (cond
  11757. ((org-at-table-p) (org-table-insert-column))
  11758. ((org-on-heading-p) (org-demote-subtree))
  11759. ((org-at-item-p) (call-interactively 'org-indent-item))
  11760. (t (org-shiftcursor-error))))
  11761. (defun org-shiftmetaup (&optional arg)
  11762. "Move subtree up or kill table row.
  11763. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11764. `org-move-item-up' depending on context. See the individual commands
  11765. for more information."
  11766. (interactive "P")
  11767. (cond
  11768. ((org-at-table-p) (org-table-kill-row))
  11769. ((org-on-heading-p) (org-move-subtree-up arg))
  11770. ((org-at-item-p) (org-move-item-up arg))
  11771. (t (org-shiftcursor-error))))
  11772. (defun org-shiftmetadown (&optional arg)
  11773. "Move subtree down or insert table row.
  11774. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11775. `org-move-item-down', depending on context. See the individual
  11776. commands for more information."
  11777. (interactive "P")
  11778. (cond
  11779. ((org-at-table-p) (org-table-insert-row arg))
  11780. ((org-on-heading-p) (org-move-subtree-down arg))
  11781. ((org-at-item-p) (org-move-item-down arg))
  11782. (t (org-shiftcursor-error))))
  11783. (defun org-metaleft (&optional arg)
  11784. "Promote heading or move table column to left.
  11785. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11786. With no specific context, calls the Emacs default `backward-word'.
  11787. See the individual commands for more information."
  11788. (interactive "P")
  11789. (cond
  11790. ((org-at-table-p) (org-table-move-column 'left))
  11791. ((or (org-on-heading-p) (org-region-active-p)) (org-do-promote))
  11792. (t (backward-word (prefix-numeric-value arg)))))
  11793. (defun org-metaright (&optional arg)
  11794. "Demote subtree or move table column to right.
  11795. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11796. With no specific context, calls the Emacs default `forward-word'.
  11797. See the individual commands for more information."
  11798. (interactive "P")
  11799. (cond
  11800. ((org-at-table-p) (org-table-move-column nil))
  11801. ((or (org-on-heading-p) (org-region-active-p)) (org-do-demote))
  11802. (t (forward-word (prefix-numeric-value arg)))))
  11803. (defun org-metaup (&optional arg)
  11804. "Move subtree up or move table row up.
  11805. Calls `org-move-subtree-up' or `org-table-move-row' or
  11806. `org-move-item-up', depending on context. See the individual commands
  11807. for more information."
  11808. (interactive "P")
  11809. (cond
  11810. ((org-at-table-p) (org-table-move-row 'up))
  11811. ((org-on-heading-p) (org-move-subtree-up arg))
  11812. ((org-at-item-p) (org-move-item-up arg))
  11813. (t (org-shiftcursor-error))))
  11814. (defun org-metadown (&optional arg)
  11815. "Move subtree down or move table row down.
  11816. Calls `org-move-subtree-down' or `org-table-move-row' or
  11817. `org-move-item-down', depending on context. See the individual
  11818. commands for more information."
  11819. (interactive "P")
  11820. (cond
  11821. ((org-at-table-p) (org-table-move-row nil))
  11822. ((org-on-heading-p) (org-move-subtree-down arg))
  11823. ((org-at-item-p) (org-move-item-down arg))
  11824. (t (org-shiftcursor-error))))
  11825. (defun org-shiftup (&optional arg)
  11826. "Increase item in timestamp or increase priority of current item.
  11827. Calls `org-timestamp-up' or `org-priority-up', depending on context.
  11828. See the individual commands for more information."
  11829. (interactive "P")
  11830. (cond
  11831. ((org-at-timestamp-p) (org-timestamp-up arg))
  11832. (t (org-priority-up))))
  11833. (defun org-shiftdown (&optional arg)
  11834. "Decrease item in timestamp or decrease priority of current item.
  11835. Calls `org-timestamp-down' or `org-priority-down', depending on context.
  11836. See the individual commands for more information."
  11837. (interactive "P")
  11838. (cond
  11839. ((org-at-timestamp-p) (org-timestamp-down arg))
  11840. (t (org-priority-down))))
  11841. (defun org-shiftright ()
  11842. "Next TODO keyword or timestamp one day later, depending on context."
  11843. (interactive)
  11844. (cond
  11845. ((org-at-timestamp-p) (org-timestamp-up-day))
  11846. ((org-on-heading-p) (org-todo 'right))
  11847. (t (org-shiftcursor-error))))
  11848. (defun org-shiftleft ()
  11849. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11850. (interactive)
  11851. (cond
  11852. ((org-at-timestamp-p) (org-timestamp-down-day))
  11853. ((org-on-heading-p) (org-todo 'left))
  11854. (t (org-shiftcursor-error))))
  11855. (defun org-copy-special ()
  11856. "Copy region in table or copy current subtree.
  11857. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11858. See the individual commands for more information."
  11859. (interactive)
  11860. (call-interactively
  11861. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11862. (defun org-cut-special ()
  11863. "Cut region in table or cut current subtree.
  11864. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11865. See the individual commands for more information."
  11866. (interactive)
  11867. (call-interactively
  11868. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11869. (defun org-paste-special (arg)
  11870. "Paste rectangular region into table, or past subtree relative to level.
  11871. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11872. See the individual commands for more information."
  11873. (interactive "P")
  11874. (if (org-at-table-p)
  11875. (org-table-paste-rectangle)
  11876. (org-paste-subtree arg)))
  11877. (defun org-ctrl-c-ctrl-c (&optional arg)
  11878. "Call realign table, or recognize a table.el table, or update keywords.
  11879. When the cursor is inside a table created by the table.el package,
  11880. activate that table. Otherwise, if the cursor is at a normal table
  11881. created with org.el, re-align that table. This command works even if
  11882. the automatic table editor has been turned off.
  11883. If the cursor is in a headline, prompt for tags and insert them into
  11884. the current line, aligned to `org-tags-column'. When in a headline and
  11885. called with prefix arg, realign all tags in the current buffer.
  11886. If the cursor is in one of the special #+KEYWORD lines, this triggers
  11887. scanning the buffer for these lines and updating the information.
  11888. If the cursor is on a #+TBLFM line, re-apply the formulae to the table."
  11889. (interactive "P")
  11890. (let ((org-enable-table-editor t))
  11891. (cond
  11892. ((and (local-variable-p 'org-finish-function (current-buffer))
  11893. (fboundp org-finish-function))
  11894. (funcall org-finish-function))
  11895. ; ((and (boundp 'remember-buffer) (equal (buffer-name) remember-buffer))
  11896. ; (remember-buffer)) ;; FIXME: can we remove this??????
  11897. ((org-on-target-p) (org-update-radio-target-regexp))
  11898. ((org-on-heading-p) (org-set-tags arg))
  11899. ((org-at-table.el-p)
  11900. (require 'table)
  11901. (beginning-of-line 1)
  11902. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11903. (table-recognize-table))
  11904. ((org-at-table-p)
  11905. (org-table-maybe-eval-formula)
  11906. (if arg
  11907. (org-table-recalculate t)
  11908. (org-table-maybe-recalculate-line))
  11909. (org-table-align))
  11910. ((org-at-item-p)
  11911. (org-renumber-ordered-list (prefix-numeric-value arg)))
  11912. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11913. (cond
  11914. ((equal (match-string 1) "TBLFM")
  11915. ;; Recalculate the table before this line
  11916. (save-excursion
  11917. (beginning-of-line 1)
  11918. (skip-chars-backward " \r\n\t")
  11919. (if (org-at-table-p) (org-table-recalculate t))))
  11920. (t
  11921. (org-mode-restart))))
  11922. ((org-region-active-p)
  11923. (org-table-convert-region (region-beginning) (region-end) arg))
  11924. ((condition-case nil
  11925. (and (region-beginning) (region-end))
  11926. (error nil))
  11927. (if (y-or-n-p "Convert inactive region to table? ")
  11928. (org-table-convert-region (region-beginning) (region-end) arg)
  11929. (error "Abort")))
  11930. (t (error "C-c C-c can do nothing useful at this location.")))))
  11931. (defun org-mode-restart ()
  11932. "Restart Org-mode, to scan again for special lines.
  11933. Also updates the keyword regular expressions."
  11934. (interactive)
  11935. (let ((org-inhibit-startup t)) (org-mode))
  11936. (message "Org-mode restarted to refresh keyword and special line setup"))
  11937. (defun org-return ()
  11938. "Goto next table row or insert a newline.
  11939. Calls `org-table-next-row' or `newline', depending on context.
  11940. See the individual commands for more information."
  11941. (interactive)
  11942. (cond
  11943. ((org-at-table-p)
  11944. (org-table-justify-field-maybe)
  11945. (org-table-next-row))
  11946. (t (newline))))
  11947. (defun org-meta-return (&optional arg)
  11948. "Insert a new heading or wrap a region in a table.
  11949. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11950. See the individual commands for more information."
  11951. (interactive "P")
  11952. (cond
  11953. ((org-at-table-p)
  11954. (org-table-wrap-region arg))
  11955. (t (org-insert-heading arg))))
  11956. ;;; Menu entries
  11957. ;; Define the Org-mode menus
  11958. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11959. '("Tbl"
  11960. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  11961. ["Next Field" org-cycle (org-at-table-p)]
  11962. ["Previous Field" org-shifttab (org-at-table-p)]
  11963. ["Next Row" org-return (org-at-table-p)]
  11964. "--"
  11965. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11966. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11967. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11968. "--"
  11969. ("Column"
  11970. ["Move Column Left" org-metaleft (org-at-table-p)]
  11971. ["Move Column Right" org-metaright (org-at-table-p)]
  11972. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11973. ["Insert Column" org-shiftmetaright (org-at-table-p)]
  11974. "--"
  11975. ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :active (org-at-table-p) :selected org-table-limit-column-width :style toggle])
  11976. ("Row"
  11977. ["Move Row Up" org-metaup (org-at-table-p)]
  11978. ["Move Row Down" org-metadown (org-at-table-p)]
  11979. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11980. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11981. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11982. "--"
  11983. ["Insert Hline" org-table-insert-hline (org-at-table-p)])
  11984. ("Rectangle"
  11985. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11986. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11987. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11988. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11989. "--"
  11990. ("Calculate"
  11991. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11992. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11993. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  11994. "--"
  11995. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11996. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11997. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11998. "--"
  11999. ["Sum Column/Rectangle" org-table-sum
  12000. (or (org-at-table-p) (org-region-active-p))]
  12001. ["Which Column?" org-table-current-column (org-at-table-p)])
  12002. ["Debug Formulas"
  12003. (setq org-table-formula-debug (not org-table-formula-debug))
  12004. :style toggle :selected org-table-formula-debug]
  12005. "--"
  12006. ["Invisible Vlines" org-table-toggle-vline-visibility
  12007. :style toggle :selected (org-in-invisibility-spec-p '(org-table))]
  12008. "--"
  12009. ["Create" org-table-create (and (not (org-at-table-p))
  12010. org-enable-table-editor)]
  12011. ["Convert Region" org-ctrl-c-ctrl-c (not (org-at-table-p 'any))]
  12012. ["Import from File" org-table-import (not (org-at-table-p))]
  12013. ["Export to File" org-table-export (org-at-table-p)]
  12014. "--"
  12015. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12016. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12017. '("Org"
  12018. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  12019. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  12020. ["Sparse Tree" org-occur t]
  12021. ["Show All" show-all t]
  12022. "--"
  12023. ["New Heading" org-insert-heading t]
  12024. ("Navigate Headings"
  12025. ["Up" outline-up-heading t]
  12026. ["Next" outline-next-visible-heading t]
  12027. ["Previous" outline-previous-visible-heading t]
  12028. ["Next Same Level" outline-forward-same-level t]
  12029. ["Previous Same Level" outline-backward-same-level t]
  12030. "--"
  12031. ["Jump" org-goto t])
  12032. ("Edit Structure"
  12033. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12034. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12035. "--"
  12036. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12037. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12038. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12039. "--"
  12040. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12041. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12042. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12043. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12044. "--"
  12045. ["Archive Subtree" org-archive-subtree t]
  12046. "--"
  12047. ["Convert to odd levels" org-convert-to-odd-levels t]
  12048. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12049. "--"
  12050. ("TODO Lists"
  12051. ["TODO/DONE/-" org-todo t]
  12052. ["Show TODO Tree" org-show-todo-tree t]
  12053. ["Global TODO list" org-todo-list t]
  12054. "--"
  12055. ["Set Priority" org-priority t]
  12056. ["Priority Up" org-shiftup t]
  12057. ["Priority Down" org-shiftdown t])
  12058. ("Dates and Scheduling"
  12059. ["Timestamp" org-time-stamp t]
  12060. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12061. ("Change Date"
  12062. ["1 Day Later" org-timestamp-up-day t]
  12063. ["1 Day Earlier" org-timestamp-down-day t]
  12064. ["1 ... Later" org-shiftup t]
  12065. ["1 ... Earlier" org-shiftdown t])
  12066. ["Compute Time Range" org-evaluate-time-range t]
  12067. ["Schedule Item" org-schedule t]
  12068. ["Deadline" org-deadline t]
  12069. "--"
  12070. ["Goto Calendar" org-goto-calendar t]
  12071. ["Date from Calendar" org-date-from-calendar t])
  12072. "--"
  12073. ["Agenda Command" org-agenda t]
  12074. ("File List for Agenda")
  12075. ("Special views current file"
  12076. ["TODO Tree" org-show-todo-tree t]
  12077. ["Check Deadlines" org-check-deadlines t]
  12078. ["Timeline" org-timeline t]
  12079. ["Tags Tree" org-tags-sparse-tree t])
  12080. "--"
  12081. ("Hyperlinks"
  12082. ["Store Link (Global)" org-store-link t]
  12083. ["Insert Link" org-insert-link t]
  12084. ["Follow Link" org-open-at-point t]
  12085. "--"
  12086. ["Descriptive Links"
  12087. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12088. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  12089. ["Literal Links"
  12090. (progn
  12091. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12092. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))]
  12093. )
  12094. "--"
  12095. ("Export"
  12096. ["ASCII" org-export-as-ascii t]
  12097. ["Extract Visible Text" org-export-copy-visible t]
  12098. ["HTML" org-export-as-html t]
  12099. ["HTML and Open" org-export-as-html-and-open t]
  12100. ["XML (XOXO)" org-export-as-xml t]
  12101. "--"
  12102. ["iCalendar this file" org-export-icalendar-this-file t]
  12103. ["iCalendar all agenda files" org-export-icalendar-all-agenda-files
  12104. :active t :keys "C-c C-x C-i"]
  12105. ["iCalendar combined" org-export-icalendar-combine-agenda-files t]
  12106. "--"
  12107. ["Option Template" org-insert-export-options-template t]
  12108. ["Toggle Fixed Width" org-toggle-fixed-width-section t])
  12109. "--"
  12110. ("Documentation"
  12111. ["Show Version" org-version t]
  12112. ["Info Documentation" org-info t])
  12113. ("Customize"
  12114. ["Browse Org Group" org-customize t]
  12115. "--"
  12116. ["Build Full Customize Menu" org-create-customize-menu
  12117. (fboundp 'customize-menu-create)])
  12118. "--"
  12119. ["Refresh setup" org-mode-restart t]
  12120. ))
  12121. (defun org-info (&optional node)
  12122. "Read documentation for Org-mode in the info system.
  12123. With optional NODE, go directly to that node."
  12124. (interactive)
  12125. (require 'info)
  12126. (Info-goto-node (format "(org)%s" (or node ""))))
  12127. (defun org-install-agenda-files-menu ()
  12128. (let ((bl (buffer-list)))
  12129. (save-excursion
  12130. (while bl
  12131. (set-buffer (pop bl))
  12132. (if (eq major-mode 'org-mode) (setq bl nil)))
  12133. (when (eq major-mode 'org-mode)
  12134. (easy-menu-change
  12135. '("Org") "File List for Agenda"
  12136. (append
  12137. (list
  12138. ["Edit File List" (org-edit-agenda-file-list) t]
  12139. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12140. ["Remove Current File from List" org-remove-file t]
  12141. ["Cycle through agenda files" org-cycle-agenda-files t]
  12142. "--")
  12143. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12144. ;;; Documentation
  12145. (defun org-customize ()
  12146. "Call the customize function with org as argument."
  12147. (interactive)
  12148. (customize-browse 'org))
  12149. (defun org-create-customize-menu ()
  12150. "Create a full customization menu for Org-mode, insert it into the menu."
  12151. (interactive)
  12152. (if (fboundp 'customize-menu-create)
  12153. (progn
  12154. (easy-menu-change
  12155. '("Org") "Customize"
  12156. `(["Browse Org group" org-customize t]
  12157. "--"
  12158. ,(customize-menu-create 'org)
  12159. ["Set" Custom-set t]
  12160. ["Save" Custom-save t]
  12161. ["Reset to Current" Custom-reset-current t]
  12162. ["Reset to Saved" Custom-reset-saved t]
  12163. ["Reset to Standard Settings" Custom-reset-standard t]))
  12164. (message "\"Org\"-menu now contains full customization menu"))
  12165. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12166. ;;; Miscellaneous stuff
  12167. (defun org-move-line-down (arg)
  12168. "Move the current line down. With prefix argument, move it past ARG lines."
  12169. (interactive "p")
  12170. (let ((col (current-column))
  12171. beg end pos)
  12172. (beginning-of-line 1) (setq beg (point))
  12173. (beginning-of-line 2) (setq end (point))
  12174. (beginning-of-line (+ 1 arg))
  12175. (setq pos (move-marker (make-marker) (point)))
  12176. (insert (delete-and-extract-region beg end))
  12177. (goto-char pos)
  12178. (move-to-column col)))
  12179. (defun org-move-line-up (arg)
  12180. "Move the current line up. With prefix argument, move it past ARG lines."
  12181. (interactive "p")
  12182. (let ((col (current-column))
  12183. beg end pos)
  12184. (beginning-of-line 1) (setq beg (point))
  12185. (beginning-of-line 2) (setq end (point))
  12186. (beginning-of-line (- arg))
  12187. (setq pos (move-marker (make-marker) (point)))
  12188. (insert (delete-and-extract-region beg end))
  12189. (goto-char pos)
  12190. (move-to-column col)))
  12191. ;; Paragraph filling stuff.
  12192. ;; We want this to be just right, so use the full arsenal.
  12193. ;; FIXME: This very likely does not work correctly for XEmacs, because the
  12194. ;; filladapt package works slightly differently.
  12195. (defun org-set-autofill-regexps ()
  12196. (interactive)
  12197. ;; In the paragraph separator we include headlines, because filling
  12198. ;; text in a line directly attached to a headline would otherwise
  12199. ;; fill the headline as well.
  12200. (set (make-local-variable 'paragraph-separate) "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
  12201. ;; The paragraph starter includes hand-formatted lists.
  12202. (set (make-local-variable 'paragraph-start)
  12203. "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*]\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12204. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12205. ;; But only if the user has not turned off tables or fixed-width regions
  12206. (set (make-local-variable 'auto-fill-inhibit-regexp)
  12207. (concat "\\*\\|#"
  12208. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12209. (concat
  12210. "\\|[ \t]*["
  12211. (if org-enable-table-editor "|" "")
  12212. (if org-enable-fixed-width-editor ":" "")
  12213. "]"))))
  12214. ;; We use our own fill-paragraph function, to make sure that tables
  12215. ;; and fixed-width regions are not wrapped. That function will pass
  12216. ;; through to `fill-paragraph' when appropriate.
  12217. (set (make-local-variable 'fill-paragraph-function) 'org-fill-paragraph)
  12218. ;; Adaptive filling: To get full control, first make sure that
  12219. ;; `adaptive-fill-regexp' never matches. Then install our won matcher.
  12220. (setq adaptive-fill-regexp "\000")
  12221. (setq adaptive-fill-function 'org-adaptive-fill-function))
  12222. (defun org-fill-paragraph (&optional justify)
  12223. "Re-align a table, pass through to fill-paragraph if no table."
  12224. (let ((table-p (org-at-table-p))
  12225. (table.el-p (org-at-table.el-p)))
  12226. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  12227. (table.el-p t) ; skip table.el tables
  12228. (table-p (org-table-align) t) ; align org-mode tables
  12229. (t nil)))) ; call paragraph-fill
  12230. ;; For reference, this is the default value of adaptive-fill-regexp
  12231. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12232. (defun org-adaptive-fill-function ()
  12233. "Return a fill prefix for org-mode files.
  12234. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12235. work correctly."
  12236. (if (looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?")
  12237. (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  12238. ;; Functions needed for Emacs/XEmacs region compatibility
  12239. (defun org-add-hook (hook function &optional append local)
  12240. "Add-hook, compatible with both Emacsen."
  12241. (if (and local (featurep 'xemacs))
  12242. (add-local-hook hook function append)
  12243. (add-hook hook function append local)))
  12244. (defun org-region-active-p ()
  12245. "Is `transient-mark-mode' on and the region active?
  12246. Works on both Emacs and XEmacs."
  12247. (if org-ignore-region
  12248. nil
  12249. (if (featurep 'xemacs)
  12250. (and zmacs-regions (region-active-p))
  12251. (and transient-mark-mode mark-active))))
  12252. (defun org-add-to-invisibility-spec (arg)
  12253. "Add elements to `buffer-invisibility-spec'.
  12254. See documentation for `buffer-invisibility-spec' for the kind of elements
  12255. that can be added."
  12256. (cond
  12257. ((fboundp 'add-to-invisibility-spec)
  12258. (add-to-invisibility-spec arg))
  12259. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  12260. (setq buffer-invisibility-spec (list arg)))
  12261. (t
  12262. (setq buffer-invisibility-spec
  12263. (cons arg buffer-invisibility-spec)))))
  12264. (defun org-remove-from-invisibility-spec (arg)
  12265. "Remove elements from `buffer-invisibility-spec'."
  12266. (if (fboundp 'remove-from-invisibility-spec)
  12267. (remove-from-invisibility-spec arg)
  12268. (if (consp buffer-invisibility-spec)
  12269. (setq buffer-invisibility-spec
  12270. (delete arg buffer-invisibility-spec)))))
  12271. (defun org-in-invisibility-spec-p (arg)
  12272. "Is ARG a member of `buffer-invisibility-spec'?"
  12273. (if (consp buffer-invisibility-spec)
  12274. (member arg buffer-invisibility-spec)
  12275. nil))
  12276. (defun org-image-file-name-regexp ()
  12277. "Return regexp matching the file names of images."
  12278. (if (fboundp 'image-file-name-regexp)
  12279. (image-file-name-regexp)
  12280. (let ((image-file-name-extensions
  12281. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12282. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12283. (concat "\\."
  12284. (regexp-opt (nconc (mapcar 'upcase
  12285. image-file-name-extensions)
  12286. image-file-name-extensions)
  12287. t)
  12288. "\\'"))))
  12289. ;; Functions needed for compatibility with old outline.el.
  12290. ;; Programming for the old outline.el (that uses selective display
  12291. ;; instead of `invisible' text properties) is a nightmare, mostly
  12292. ;; because regular expressions can no longer be anchored at
  12293. ;; beginning/end of line. Therefore a number of function need special
  12294. ;; treatment when the old outline.el is being used.
  12295. ;; The following functions capture almost the entire compatibility code
  12296. ;; between the different versions of outline-mode. The only other
  12297. ;; places where this is important are the font-lock-keywords, and in
  12298. ;; `org-export-copy-visible'. Search for `org-noutline-p' to find them.
  12299. ;; C-a should go to the beginning of a *visible* line, also in the
  12300. ;; new outline.el. I guess this should be patched into Emacs?
  12301. (defun org-beginning-of-line ()
  12302. "Go to the beginning of the current line. If that is invisible, continue
  12303. to a visible line beginning. This makes the function of C-a more intuitive."
  12304. (interactive)
  12305. (beginning-of-line 1)
  12306. (if (bobp)
  12307. nil
  12308. (backward-char 1)
  12309. (if (org-invisible-p)
  12310. (while (and (not (bobp)) (org-invisible-p))
  12311. (backward-char 1)
  12312. (beginning-of-line 1))
  12313. (forward-char 1))))
  12314. (when org-noutline-p
  12315. (define-key org-mode-map "\C-a" 'org-beginning-of-line))
  12316. ;; FIXME: should I use substitute-key-definition to reach other bindings
  12317. ;; of beginning-of-line?
  12318. (defun org-invisible-p ()
  12319. "Check if point is at a character currently not visible."
  12320. (if org-noutline-p
  12321. ;; Early versions of noutline don't have `outline-invisible-p'.
  12322. (if (fboundp 'outline-invisible-p)
  12323. (outline-invisible-p)
  12324. (get-char-property (point) 'invisible))
  12325. (save-excursion
  12326. (skip-chars-backward "^\r\n")
  12327. (equal (char-before) ?\r))))
  12328. (defun org-invisible-p2 ()
  12329. "Check if point is at a character currently not visible."
  12330. (save-excursion
  12331. (if org-noutline-p
  12332. (progn
  12333. (if (and (eolp) (not (bobp))) (backward-char 1))
  12334. ;; Early versions of noutline don't have `outline-invisible-p'.
  12335. (if (fboundp 'outline-invisible-p)
  12336. (outline-invisible-p)
  12337. (get-char-property (point) 'invisible)))
  12338. (skip-chars-backward "^\r\n")
  12339. (equal (char-before) ?\r))))
  12340. (defun org-back-to-heading (&optional invisible-ok)
  12341. "Move to previous heading line, or beg of this line if it's a heading.
  12342. Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
  12343. (if org-noutline-p
  12344. (outline-back-to-heading invisible-ok)
  12345. (if (and (memq (char-before) '(?\n ?\r))
  12346. (looking-at outline-regexp))
  12347. t
  12348. (if (re-search-backward (concat (if invisible-ok "\\([\r\n]\\|^\\)" "^")
  12349. outline-regexp)
  12350. nil t)
  12351. (if invisible-ok
  12352. (progn (goto-char (match-end 1))
  12353. (looking-at outline-regexp)))
  12354. (error "Before first heading")))))
  12355. (defun org-on-heading-p (&optional invisible-ok)
  12356. "Return t if point is on a (visible) heading line.
  12357. If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
  12358. (if org-noutline-p
  12359. (outline-on-heading-p 'invisible-ok)
  12360. (save-excursion
  12361. (skip-chars-backward "^\n\r")
  12362. (and (looking-at outline-regexp)
  12363. (or invisible-ok
  12364. (bobp)
  12365. (equal (char-before) ?\n))))))
  12366. (defun org-on-target-p ()
  12367. (let ((pos (point)))
  12368. (save-excursion
  12369. (skip-chars-forward "<")
  12370. (and (re-search-backward "<<" nil t)
  12371. (or (looking-at org-radio-target-regexp)
  12372. (looking-at org-target-regexp))
  12373. (<= (match-beginning 0) pos)
  12374. (>= (1+ (match-end 0)) pos)))))
  12375. (defun org-up-heading-all (arg)
  12376. "Move to the heading line of which the present line is a subheading.
  12377. This function considers both visible and invisible heading lines.
  12378. With argument, move up ARG levels."
  12379. (if org-noutline-p
  12380. (if (fboundp 'outline-up-heading-all)
  12381. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12382. (outline-up-heading arg t)) ; emacs 22 version of outline.el
  12383. (org-back-to-heading t)
  12384. (looking-at outline-regexp)
  12385. (if (<= (- (match-end 0) (match-beginning 0)) arg)
  12386. (error "Cannot move up %d levels" arg)
  12387. (re-search-backward
  12388. (concat "[\n\r]" (regexp-quote
  12389. (make-string (- (match-end 0) (match-beginning 0) arg)
  12390. ?*))
  12391. "[^*]"))
  12392. (forward-char 1))))
  12393. (defun org-show-hidden-entry ()
  12394. "Show an entry where even the heading is hidden."
  12395. (save-excursion
  12396. (if (not org-noutline-p)
  12397. (progn
  12398. (org-back-to-heading t)
  12399. (org-flag-heading nil)))
  12400. (org-show-entry)))
  12401. (defun org-check-occur-regexp (regexp)
  12402. "If REGEXP starts with \"^\", modify it to check for \\r as well.
  12403. Of course, only for the old outline mode."
  12404. (if org-noutline-p
  12405. regexp
  12406. (if (string-match "^\\^" regexp)
  12407. (concat "[\n\r]" (substring regexp 1))
  12408. regexp)))
  12409. (defun org-flag-heading (flag &optional entry)
  12410. "Flag the current heading. FLAG non-nil means make invisible.
  12411. When ENTRY is non-nil, show the entire entry."
  12412. (save-excursion
  12413. (org-back-to-heading t)
  12414. (if (not org-noutline-p)
  12415. ;; Make the current headline visible
  12416. (outline-flag-region (max 1 (1- (point))) (point) (if flag ?\r ?\n)))
  12417. ;; Check if we should show the entire entry
  12418. (if entry
  12419. (progn
  12420. (org-show-entry)
  12421. (save-excursion ;; FIXME: Is this the fix for points in the -|
  12422. ;; middle of text? |
  12423. (and (outline-next-heading) ;; |
  12424. (org-flag-heading nil)))) ; show the next heading _|
  12425. (outline-flag-region (max 1 (1- (point)))
  12426. (save-excursion (outline-end-of-heading) (point))
  12427. (if org-noutline-p
  12428. flag
  12429. (if flag ?\r ?\n))))))
  12430. (defun org-end-of-subtree (&optional invisible-OK)
  12431. ;; This is an exact copy of the original function, but it uses
  12432. ;; `org-back-to-heading', to make it work also in invisible
  12433. ;; trees. And is uses an invisible-OK argument.
  12434. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12435. (org-back-to-heading invisible-OK)
  12436. (let ((first t)
  12437. (level (funcall outline-level)))
  12438. (while (and (not (eobp))
  12439. (or first (> (funcall outline-level) level)))
  12440. (setq first nil)
  12441. (outline-next-heading))
  12442. (if (memq (preceding-char) '(?\n ?\^M))
  12443. (progn
  12444. ;; Go to end of line before heading
  12445. (forward-char -1)
  12446. (if (memq (preceding-char) '(?\n ?\^M))
  12447. ;; leave blank line before heading
  12448. (forward-char -1))))))
  12449. (defun org-show-subtree ()
  12450. "Show everything after this heading at deeper levels."
  12451. (outline-flag-region
  12452. (point)
  12453. (save-excursion
  12454. (outline-end-of-subtree) (outline-next-heading) (point))
  12455. (if org-noutline-p nil ?\n)))
  12456. (defun org-show-entry ()
  12457. "Show the body directly following this heading.
  12458. Show the heading too, if it is currently invisible."
  12459. (interactive)
  12460. (save-excursion
  12461. (org-back-to-heading t)
  12462. (outline-flag-region
  12463. (1- (point))
  12464. (save-excursion
  12465. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12466. (or (match-beginning 1) (point-max)))
  12467. (if org-noutline-p nil ?\n))))
  12468. (defun org-make-options-regexp (kwds)
  12469. "Make a regular expression for keyword lines."
  12470. (concat
  12471. (if org-noutline-p "^" "[\n\r]")
  12472. "#?[ \t]*\\+\\("
  12473. (mapconcat 'regexp-quote kwds "\\|")
  12474. "\\):[ \t]*"
  12475. (if org-noutline-p "\\(.+\\)" "\\([^\n\r]+\\)")))
  12476. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12477. (eval-after-load "bookmark"
  12478. '(if (boundp 'bookmark-after-jump-hook)
  12479. ;; We can use the hook
  12480. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12481. ;; Hook not available, use advice
  12482. (defadvice bookmark-jump (after org-make-visible activate)
  12483. "Make the position visible."
  12484. (org-bookmark-jump-unhide))))
  12485. (defun org-bookmark-jump-unhide ()
  12486. "Unhide the current position, to show the bookmark location."
  12487. (and (eq major-mode 'org-mode)
  12488. (or (org-invisible-p)
  12489. (save-excursion (goto-char (max (point-min) (1- (point))))
  12490. (org-invisible-p)))
  12491. (org-show-hierarchy-above)))
  12492. ;;; Finish up
  12493. (provide 'org)
  12494. (run-hooks 'org-load-hook)
  12495. ;; Experimental code
  12496. (defun org-upgrade-old-links (&optional broken-only)
  12497. "Transfer old <...> style links to new [[...]] style links.
  12498. With arg BROKEN-ONLY, fix only the links that contain space characters
  12499. and therefore have stopped working in this new version."
  12500. (interactive "P")
  12501. (save-excursion
  12502. (goto-char (point-min))
  12503. (let ((re (concat "\\([^[]\\)<\\("
  12504. "\\(" (mapconcat 'identity org-link-types "\\|")
  12505. "\\):"
  12506. "[^" org-non-link-chars "]+\\)>"))
  12507. l1 l2)
  12508. (while (re-search-forward re nil t)
  12509. (setq l1 (match-string 2)
  12510. l2 (save-match-data
  12511. (org-link-escape (match-string 2))))
  12512. (when (or (not broken-only)
  12513. (save-match-data (string-match " " l1)))
  12514. (if (equal l1 l2)
  12515. (replace-match (concat (match-string 1) "[[" l1 "]]") t t)
  12516. (replace-match
  12517. (concat (match-string 1) "[[" l2 "][" l1 "]]")
  12518. t t)))))))
  12519. (defun org-export-as-xml ()
  12520. "Export current buffer as XOXO XML buffer."
  12521. (interactive)
  12522. (cond ((eq org-export-xml-type 'xoxo)
  12523. (org-export-as-xoxo (current-buffer)))))
  12524. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  12525. (with-current-buffer buffer
  12526. (apply 'insert output)))
  12527. (defun org-export-as-xoxo (&optional buffer)
  12528. "Export the org buffer as XOXO.
  12529. The XOXO buffer is named *xoxo-<source buffer name>*"
  12530. (interactive (list (current-buffer)))
  12531. ;; A quickie abstraction
  12532. ;; Output everything as XOXO
  12533. (with-current-buffer (get-buffer buffer)
  12534. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  12535. (let* ((filename (concat (file-name-sans-extension buffer-file-name)
  12536. ".xml"))
  12537. (out (find-file-noselect filename))
  12538. (last-level 1)
  12539. (hanging-li nil))
  12540. ;; Check the output buffer is empty.
  12541. (with-current-buffer out (erase-buffer))
  12542. ;; Kick off the output
  12543. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  12544. (while (re-search-forward "^\\(\\*+\\) \\(.+\\)" (point-max) 't)
  12545. (let* ((hd (match-string-no-properties 1))
  12546. (level (length hd))
  12547. (text (concat
  12548. (match-string-no-properties 2)
  12549. (save-excursion
  12550. (goto-char (match-end 0))
  12551. (let ((str ""))
  12552. (catch 'loop
  12553. (while 't
  12554. (forward-line)
  12555. (if (looking-at "^[ \t]\\(.*\\)")
  12556. (setq str (concat str (match-string-no-properties 1)))
  12557. (throw 'loop str)))))))))
  12558. ;; Handle level rendering
  12559. (cond
  12560. ((> level last-level)
  12561. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  12562. ((< level last-level)
  12563. (dotimes (- (- last-level level) 1)
  12564. (if hanging-li
  12565. (org-export-as-xoxo-insert-into out "</li>\n"))
  12566. (org-export-as-xoxo-insert-into out "</ol>\n"))
  12567. (when hanging-li
  12568. (org-export-as-xoxo-insert-into out "</li>\n")
  12569. (setq hanging-li nil)))
  12570. ((equal level last-level)
  12571. (if hanging-li
  12572. (org-export-as-xoxo-insert-into out "</li>\n")))
  12573. )
  12574. (setq last-level level)
  12575. ;; And output the new li
  12576. (setq hanging-li 't)
  12577. (if (equal ?+ (elt text 0))
  12578. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  12579. (org-export-as-xoxo-insert-into out "<li>" text))))
  12580. ;; Finally finish off the ol
  12581. (dotimes (- last-level 1)
  12582. (if hanging-li
  12583. (org-export-as-xoxo-insert-into out "</li>\n"))
  12584. (org-export-as-xoxo-insert-into out "</ol>\n"))
  12585. ;; Finish the buffer off and clean it up.
  12586. (switch-to-buffer-other-window out)
  12587. (indent-region (point-min) (point-max))
  12588. (save-buffer)
  12589. (goto-char (point-min))
  12590. )))
  12591. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  12592. ;;; org.el ends here