org.el 540 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652
  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.34
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;;
  47. ;; Installation
  48. ;; ------------
  49. ;; If Org-mode is part of the Emacs distribution or an XEmacs package, you
  50. ;; only need to copy the following lines to your .emacs file. The last two
  51. ;; lines define *global* keys for the commands `org-store-link' and
  52. ;; `org-agenda' - please choose suitable keys yourself.
  53. ;;
  54. ;; (add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
  55. ;; (define-key global-map "\C-cl" 'org-store-link)
  56. ;; (define-key global-map "\C-ca" 'org-agenda)
  57. ;;
  58. ;; Furthermore you need to activate font-lock-mode in org-mode buffers.
  59. ;; either of the following two lins will do the trick:
  60. ;;
  61. ;; (global-font-lock-mode 1) ; for all buffers
  62. ;; (add-hook 'org-mode-hook 'turn-on-font-lock) ; org-mode buffers only
  63. ;;
  64. ;; If you have downloaded Org-mode from the Web, you have to take additional
  65. ;; action: Byte-compile org.el and org-publish.el and put them together with
  66. ;; org-install.el on your load path. Then also add to your .emacs file:
  67. ;;
  68. ;; (require 'org-install)
  69. ;;
  70. ;;
  71. ;; Activation
  72. ;; ----------
  73. ;; The setup above will put all files with extension ".org" into Org-mode.
  74. ;; As an alternative, make the first line of a file look like this:
  75. ;;
  76. ;; MY PROJECTS -*- mode: org; -*-
  77. ;;
  78. ;; which will select Org-mode for this buffer no matter what the file's
  79. ;; name is.
  80. ;;
  81. ;; Documentation
  82. ;; -------------
  83. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  84. ;; distribution also contains a PDF version of it. At the homepage of
  85. ;; Org-mode, you can read the same text online as HTML. There is also an
  86. ;; excellent reference card made by Philip Rooke. This card can be found
  87. ;; in the etc/ directory of Emacs 22.
  88. ;;
  89. ;; Recent changes
  90. ;; --------------
  91. ;; Version 4.34
  92. ;; - Bug fixes.
  93. ;;
  94. ;; Version 4.33
  95. ;; - New commands to move through plain lists: S-up and S-down.
  96. ;; - Bug fixes and documentation update.
  97. ;;
  98. ;; Version 4.32
  99. ;; - Fast (single-key-per-tag) interface for setting TAGS.
  100. ;; - The list of legal tags can be configured globally and locally.
  101. ;; - Elisp and Info links (thanks to Todd Neal).
  102. ;; - `org-export-publishing-directory' can be an alist, with different
  103. ;; directories for different export types.
  104. ;; - All context-sensitive commands use `call-interactively' to dispatch.
  105. ;; - `org-confirm-shell-links' renamed to `org-confirm-shell-link-function'.
  106. ;; - Bug fixes.
  107. ;;
  108. ;; Version 4.31
  109. ;; - Bug fixes.
  110. ;;
  111. ;; Version 4.30
  112. ;; - Modified installation: Autoloads have been collected in org-install.el.
  113. ;; - Logging (org-log-done) is now a #+STARTUP option.
  114. ;; - Checkboxes in plain list items, following up on Frank Ruell's idea.
  115. ;; - File links inserted with C-c C-l will use relative paths if the linked
  116. ;; file is in the current directory or a subdirectory of it.
  117. ;; - New variable `org-link-file-path-type' to specify preference for
  118. ;; relative and absolute paths.
  119. ;; - New CSS classes for tags, timestamps, timestamp keywords.
  120. ;; - Bug and typo fixes.
  121. ;;
  122. ;; Version 4.29
  123. ;; - Inlining images in HTML export now depends on wheather the link
  124. ;; contains a description or not.
  125. ;; - TODO items can be scheduled from the global TODO list using C-c C-s.
  126. ;; - TODO items already scheduled can be made to disappear from the global
  127. ;; todo list, see `org-agenda-todo-ignore-scheduled'.
  128. ;; - In Tables, formulas may also be Lisp forms.
  129. ;; - Exporting the visible part of an outline with `C-c C-x v' works now
  130. ;; for all available exporters.
  131. ;; - Bug fixes, lots of them :-(
  132. ;;
  133. ;; Version 4.28
  134. ;; - Bug fixes.
  135. ;;
  136. ;; Version 4.27
  137. ;; - HTML exporter generalized to receive external options.
  138. ;; As part of the process, author, email and date have been moved to the
  139. ;; end of the HTML file.
  140. ;; - Support for customizable file search in file links.
  141. ;; - BibTeX database links as first application of the above.
  142. ;; - New option `org-agenda-todo-list-sublevels' to turn off listing TODO
  143. ;; entries that are sublevels of another TODO entry.
  144. ;;
  145. ;;
  146. ;;; Code:
  147. (eval-when-compile
  148. (require 'cl)
  149. (require 'calendar))
  150. (require 'outline)
  151. (require 'time-date)
  152. (require 'easymenu)
  153. ;;; Customization variables
  154. (defvar org-version "4.34"
  155. "The version number of the file org.el.")
  156. (defun org-version ()
  157. (interactive)
  158. (message "Org-mode version %s" org-version))
  159. ;; The following constant is for compatibility with different versions
  160. ;; of outline.el.
  161. (defconst org-noutline-p (featurep 'noutline)
  162. "Are we using the new outline mode?")
  163. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  164. (defconst org-format-transports-properties-p
  165. (let ((x "a"))
  166. (add-text-properties 0 1 '(test t) x)
  167. (get-text-property 0 'test (format "%s" x)))
  168. "Does format transport text properties?")
  169. (defgroup org nil
  170. "Outline-based notes management and organizer."
  171. :tag "Org"
  172. :group 'outlines
  173. :group 'hypermedia
  174. :group 'calendar)
  175. (defgroup org-startup nil
  176. "Options concerning startup of Org-mode."
  177. :tag "Org Startup"
  178. :group 'org)
  179. (defcustom org-startup-folded t
  180. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  181. This can also be configured on a per-file basis by adding one of
  182. the following lines anywhere in the buffer:
  183. #+STARTUP: fold
  184. #+STARTUP: nofold
  185. #+STARTUP: content"
  186. :group 'org-startup
  187. :type '(choice
  188. (const :tag "nofold: show all" nil)
  189. (const :tag "fold: overview" t)
  190. (const :tag "content: all headlines" content)))
  191. (defcustom org-startup-truncated t
  192. "Non-nil means, entering Org-mode will set `truncate-lines'.
  193. This is useful since some lines containing links can be very long and
  194. uninteresting. Also tables look terrible when wrapped."
  195. :group 'org-startup
  196. :type 'boolean)
  197. (defcustom org-startup-align-all-tables nil
  198. "Non-nil means, align all tables when visiting a file.
  199. This is useful when the column width in tables is forced with <N> cookies
  200. in table fields. Such tables will look correct only after the first re-align.
  201. This can also be configured on a per-file basis by adding one of
  202. the following lines anywhere in the buffer:
  203. #+STARTUP: align
  204. #+STARTUP: noalign"
  205. :group 'org-startup
  206. :type 'boolean)
  207. (defcustom org-startup-with-deadline-check nil
  208. "Non-nil means, entering Org-mode will run the deadline check.
  209. This means, if you start editing an org file, you will get an
  210. immediate reminder of any due deadlines.
  211. This can also be configured on a per-file basis by adding one of
  212. the following lines anywhere in the buffer:
  213. #+STARTUP: dlcheck
  214. #+STARTUP: nodlcheck"
  215. :group 'org-startup
  216. :type 'boolean)
  217. (defcustom org-insert-mode-line-in-empty-file nil
  218. "Non-nil means insert the first line setting Org-mode in empty files.
  219. When the function `org-mode' is called interactively in an empty file, this
  220. normally means that the file name does not automatically trigger Org-mode.
  221. To ensure that the file will always be in Org-mode in the future, a
  222. line enforcing Org-mode will be inserted into the buffer, if this option
  223. has been set."
  224. :group 'org-startup
  225. :type 'boolean)
  226. (defcustom org-CUA-compatible nil
  227. "Non-nil means use alternative key bindings for S-<cursor movement>.
  228. Org-mode used S-<cursor movement> for changing timestamps and priorities.
  229. S-<cursor movement> is also used for example by `CUA-mode' to select text.
  230. If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
  231. alternative bindings. Setting this variable to t will replace the following
  232. keys both in Org-mode and in the Org-agenda buffer.
  233. S-RET -> C-S-RET
  234. S-up -> M-p
  235. S-down -> M-n
  236. S-left -> M--
  237. S-right -> M-+
  238. If you do not like the alternative keys, take a look at the variable
  239. `org-disputed-keys'.
  240. This option is only relevant at load-time of Org-mode. Changing it requires
  241. a restart of Emacs to become effective."
  242. :group 'org-startup
  243. :type 'boolean)
  244. (defvar org-disputed-keys
  245. '((S-up [(shift up)] [(meta ?p)])
  246. (S-down [(shift down)] [(meta ?n)])
  247. (S-left [(shift left)] [(meta ?-)])
  248. (S-right [(shift right)] [(meta ?+)])
  249. (S-return [(shift return)] [(control shift return)]))
  250. "Keys for which Org-mode and other modes compete.
  251. This is an alist, cars are symbols for lookup, 1st element is the default key,
  252. second element will be used when `org-CUA-compatible' is t.")
  253. (defun org-key (key)
  254. "Select a key according to `org-CUA-compatible'."
  255. (nth (if org-CUA-compatible 2 1)
  256. (or (assq key org-disputed-keys)
  257. (error "Invalid Key %s in `org-key'" key))))
  258. (defcustom org-ellipsis nil
  259. "The ellipsis to use in the Org-mode outline.
  260. When nil, just use the standard three dots. When a string, use that instead,
  261. and just in Org-mode (which will then use its own display table).
  262. Changing this requires executing `M-x org-mode' in a buffer to become
  263. effective."
  264. :group 'org-startup
  265. :type '(choice (const :tag "Default" nil)
  266. (string :tag "String" :value "...#")))
  267. (defvar org-display-table nil
  268. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  269. (defgroup org-keywords nil
  270. "Keywords in Org-mode."
  271. :tag "Org Keywords"
  272. :group 'org)
  273. (defcustom org-deadline-string "DEADLINE:"
  274. "String to mark deadline entries.
  275. A deadline is this string, followed by a time stamp. Should be a word,
  276. terminated by a colon. You can insert a schedule keyword and
  277. a timestamp with \\[org-deadline].
  278. Changes become only effective after restarting Emacs."
  279. :group 'org-keywords
  280. :type 'string)
  281. (defcustom org-scheduled-string "SCHEDULED:"
  282. "String to mark scheduled TODO entries.
  283. A schedule 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-schedule].
  286. Changes become only effective after restarting Emacs."
  287. :group 'org-keywords
  288. :type 'string)
  289. (defcustom org-closed-string "CLOSED:"
  290. "String used as the prefix for timestamps logging closing a TODO entry."
  291. :group 'org-keywords
  292. :type 'string)
  293. (defcustom org-comment-string "COMMENT"
  294. "Entries starting with this keyword will never be exported.
  295. An entry can be toggled between COMMENT and normal with
  296. \\[org-toggle-comment].
  297. Changes become only effective after restarting Emacs."
  298. :group 'org-keywords
  299. :type 'string)
  300. (defcustom org-quote-string "QUOTE"
  301. "Entries starting with this keyword will be exported in fixed-width font.
  302. Quoting applies only to the text in the entry following the headline, and does
  303. not extend beyond the next headline, even if that is lower level.
  304. An entry can be toggled between QUOTE and normal with
  305. \\[org-toggle-fixed-width-section]."
  306. :group 'org-keywords
  307. :type 'string)
  308. (defgroup org-structure nil
  309. "Options concerning the general structure of Org-mode files."
  310. :tag "Org Structure"
  311. :group 'org)
  312. (defgroup org-cycle nil
  313. "Options concerning visibility cycling in Org-mode."
  314. :tag "Org Cycle"
  315. :group 'org-structure)
  316. (defcustom org-cycle-global-at-bob t
  317. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  318. This makes it possible to do global cycling without having to use S-TAB or
  319. C-u TAB. For this special case to work, the first line of the buffer
  320. must not be a headline - it may be empty ot some other text. When used in
  321. this way, `org-cycle-hook' is disables temporarily, to make sure the
  322. cursor stays at the beginning of the buffer.
  323. When this option is nil, don't do anything special at the beginning
  324. of the buffer."
  325. :group 'org-cycle
  326. :type 'boolean)
  327. (defcustom org-cycle-emulate-tab t
  328. "Where should `org-cycle' emulate TAB.
  329. nil Never
  330. white Only in completely white lines
  331. t Everywhere except in headlines"
  332. :group 'org-cycle
  333. :type '(choice (const :tag "Never" nil)
  334. (const :tag "Only in completely white lines" white)
  335. (const :tag "Everywhere except in headlines" t)
  336. ))
  337. (defcustom org-cycle-hook '(org-optimize-window-after-visibility-change)
  338. "Hook that is run after `org-cycle' has changed the buffer visibility.
  339. The function(s) in this hook must accept a single argument which indicates
  340. the new state that was set by the most recent `org-cycle' command. The
  341. argument is a symbol. After a global state change, it can have the values
  342. `overview', `content', or `all'. After a local state change, it can have
  343. the values `folded', `children', or `subtree'."
  344. :group 'org-cycle
  345. :type 'hook)
  346. (defgroup org-edit-structure nil
  347. "Options concerning structure editing in Org-mode."
  348. :tag "Org Edit Structure"
  349. :group 'org-structure)
  350. (defcustom org-odd-levels-only nil
  351. "Non-nil means, skip even levels and only use odd levels for the outline.
  352. This has the effect that two stars are being added/taken away in
  353. promotion/demotion commands. It also influences how levels are
  354. handled by the exporters.
  355. Changing it requires restart of `font-lock-mode' to become effective
  356. for fontification also in regions already fontified.
  357. You may also set this on a per-file basis by adding one of the following
  358. lines to the buffer:
  359. #+STARTUP: odd
  360. #+STARTUP: oddeven"
  361. :group 'org-edit-structure
  362. :group 'org-font-lock
  363. :type 'boolean)
  364. (defcustom org-adapt-indentation t
  365. "Non-nil means, adapt indentation when promoting and demoting.
  366. When this is set and the *entire* text in an entry is indented, the
  367. indentation is increased by one space in a demotion command, and
  368. decreased by one in a promotion command. If any line in the entry
  369. body starts at column 0, indentation is not changed at all."
  370. :group 'org-edit-structure
  371. :type 'boolean)
  372. (defcustom org-insert-heading-hook nil
  373. "Hook being run after inserting a new heading."
  374. :group 'org-edit-structure
  375. :type 'boolean)
  376. (defcustom org-enable-fixed-width-editor t
  377. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  378. This currently only means, they are never auto-wrapped.
  379. When nil, such lines will be treated like ordinary lines.
  380. See also the QUOTE keyword."
  381. :group 'org-edit-structure
  382. :type 'boolean)
  383. (defgroup org-sparse-trees nil
  384. "Options concerning sparse trees in Org-mode."
  385. :tag "Org Sparse Trees"
  386. :group 'org-structure)
  387. (defcustom org-highlight-sparse-tree-matches t
  388. "Non-nil means, highlight all matches that define a sparse tree.
  389. The highlights will automatically disappear the next time the buffer is
  390. changed by an edit command."
  391. :group 'org-sparse-trees
  392. :type 'boolean)
  393. (defcustom org-show-hierarchy-above t
  394. "Non-nil means, show full hierarchy when showing a spot in the tree.
  395. Turning this off makes sparse trees more compact, but also less clear."
  396. :group 'org-sparse-trees
  397. :type 'boolean)
  398. (defcustom org-show-following-heading t
  399. "Non-nil means, show heading following match in `org-occur'.
  400. When doing an `org-occur' it is useful to show the headline which
  401. follows the match, even if they do not match the regexp. This makes it
  402. easier to edit directly inside the sparse tree. However, if you use
  403. `org-occur' mainly as an overview, the following headlines are
  404. unnecessary clutter."
  405. :group 'org-sparse-trees
  406. :type 'boolean)
  407. (defcustom org-occur-hook '(org-first-headline-recenter)
  408. "Hook that is run after `org-occur' has constructed a sparse tree.
  409. This can be used to recenter the window to show as much of the structure
  410. as possible."
  411. :group 'org-sparse-trees
  412. :type 'hook)
  413. (defgroup org-plain-lists nil
  414. "Options concerning plain lists in Org-mode."
  415. :tag "Org Plain lists"
  416. :group 'org-structure)
  417. (defcustom org-cycle-include-plain-lists nil
  418. "Non-nil means, include plain lists into visibility cycling.
  419. This means that during cycling, plain list items will *temporarily* be
  420. interpreted as outline headlines with a level given by 1000+i where i is the
  421. indentation of the bullet. In all other operations, plain list items are
  422. not seen as headlines. For example, you cannot assign a TODO keyword to
  423. such an item."
  424. :group 'org-plain-lists
  425. :type 'boolean)
  426. (defcustom org-plain-list-ordered-item-terminator t
  427. "The character that makes a line with leading number an ordered list item.
  428. Valid values are ?. and ?\). To get both terminators, use t. While
  429. ?. may look nicer, it creates the danger that a line with leading
  430. number may be incorrectly interpreted as an item. ?\) therefore is
  431. the safe choice."
  432. :group 'org-plain-lists
  433. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  434. (const :tag "paren like in \"2)\"" ?\))
  435. (const :tab "both" t)))
  436. (defcustom org-auto-renumber-ordered-lists t
  437. "Non-nil means, automatically renumber ordered plain lists.
  438. Renumbering happens when the sequence have been changed with
  439. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  440. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  441. :group 'org-plain-lists
  442. :type 'boolean)
  443. (defgroup org-archive nil
  444. "Options concerning archiving in Org-mode."
  445. :tag "Org Archive"
  446. :group 'org-structure)
  447. (defcustom org-archive-location "%s_archive::"
  448. "The location where subtrees should be archived.
  449. This string consists of two parts, separated by a double-colon.
  450. The first part is a file name - when omitted, archiving happens in the same
  451. file. %s will be replaced by the current file name (without directory part).
  452. Archiving to a different file is useful to keep archived entries from
  453. contributing to the Org-mode Agenda.
  454. The part after the double colon is a headline. The archived entries will be
  455. filed under that headline. When omitted, the subtrees are simply filed away
  456. at the end of the file, as top-level entries.
  457. Here are a few examples:
  458. \"%s_archive::\"
  459. If the current file is Projects.org, archive in file
  460. Projects.org_archive, as top-level trees. This is the default.
  461. \"::* Archived Tasks\"
  462. Archive in the current file, under the top-level headline
  463. \"* Archived Tasks\".
  464. \"~/org/archive.org::\"
  465. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  466. \"basement::** Finished Tasks\"
  467. Archive in file ./basement (relative path), as level 3 trees
  468. below the level 2 heading \"** Finished Tasks\".
  469. You may set this option on a per-file basis by adding to the buffer a
  470. line like
  471. #+ARCHIVE: basement::** Finished Tasks"
  472. :group 'org-archive
  473. :type 'string)
  474. (defcustom org-archive-mark-done t
  475. "Non-nil means, mark archived entries as DONE."
  476. :group 'org-archive
  477. :type 'boolean)
  478. (defcustom org-archive-stamp-time t
  479. "Non-nil means, add a time stamp to archived entries.
  480. The time stamp will be added directly after the TODO state keyword in the
  481. first line, so it is probably best to use this in combinations with
  482. `org-archive-mark-done'."
  483. :group 'org-archive
  484. :type 'boolean)
  485. (defgroup org-table nil
  486. "Options concerning tables in Org-mode."
  487. :tag "Org Table"
  488. :group 'org)
  489. (defcustom org-enable-table-editor 'optimized
  490. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  491. When nil, such lines will be treated like ordinary lines.
  492. When equal to the symbol `optimized', the table editor will be optimized to
  493. do the following:
  494. - Use automatic overwrite mode in front of whitespace in table fields.
  495. This make the structure of the table stay in tact as long as the edited
  496. field does not exceed the column width.
  497. - Minimize the number of realigns. Normally, the table is aligned each time
  498. TAB or RET are pressed to move to another field. With optimization this
  499. happens only if changes to a field might have changed the column width.
  500. Optimization requires replacing the functions `self-insert-command',
  501. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  502. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  503. very good at guessing when a re-align will be necessary, but you can always
  504. force one with \\[org-ctrl-c-ctrl-c].
  505. If you would like to use the optimized version in Org-mode, but the
  506. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  507. This variable can be used to turn on and off the table editor during a session,
  508. but in order to toggle optimization, a restart is required.
  509. See also the variable `org-table-auto-blank-field'."
  510. :group 'org-table
  511. :type '(choice
  512. (const :tag "off" nil)
  513. (const :tag "on" t)
  514. (const :tag "on, optimized" optimized)))
  515. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  516. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  517. In the optimized version, the table editor takes over all simple keys that
  518. normally just insert a character. In tables, the characters are inserted
  519. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  520. for empty fields). Outside tables, the correct binding of the keys is
  521. restored.
  522. The default for this option is t if the optimized version is also used in
  523. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  524. this variable requires a restart of Emacs to become effective."
  525. :group 'org-table
  526. :type 'boolean)
  527. (defgroup org-table-settings nil
  528. "Settings for tables in Org-mode."
  529. :tag "Org Table Settings"
  530. :group 'org-table)
  531. (defcustom org-table-default-size "5x2"
  532. "The default size for newly created tables, Columns x Rows."
  533. :group 'org-table-settings
  534. :type 'string)
  535. (defcustom org-table-number-regexp "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$"
  536. "Regular expression for recognizing numbers in table columns.
  537. If a table column contains mostly numbers, it will be aligned to the
  538. right. If not, it will be aligned to the left.
  539. The default value of this option is a regular expression which allows
  540. anything which looks remotely like a number as used in scientific
  541. context. For example, all of the following will be considered a
  542. number:
  543. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  544. Other options offered by the customize interface are more restrictive."
  545. :group 'org-table-settings
  546. :type '(choice
  547. (const :tag "Positive Integers"
  548. "^[0-9]+$")
  549. (const :tag "Integers"
  550. "^[-+]?[0-9]+$")
  551. (const :tag "Floating Point Numbers"
  552. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  553. (const :tag "Floating Point Number or Integer"
  554. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  555. (const :tag "Exponential, Floating point, Integer"
  556. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  557. (const :tag "Very General Number-Like"
  558. "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$")
  559. (string :tag "Regexp:")))
  560. (defcustom org-table-number-fraction 0.5
  561. "Fraction of numbers in a column required to make the column align right.
  562. In a column all non-white fields are considered. If at least this
  563. fraction of fields is matched by `org-table-number-fraction',
  564. alignment to the right border applies."
  565. :group 'org-table-settings
  566. :type 'number)
  567. (defgroup org-table-editing nil
  568. "Bahavior of tables during editing in Org-mode."
  569. :tag "Org Table Editing"
  570. :group 'org-table)
  571. (defcustom org-table-automatic-realign t
  572. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  573. When nil, aligning is only done with \\[org-table-align], or after column
  574. removal/insertion."
  575. :group 'org-table-editing
  576. :type 'boolean)
  577. (defcustom org-table-limit-column-width t ;kw
  578. "Non-nil means, allow to limit the width of table columns with <N> fields."
  579. :group 'org-table-editing
  580. :type 'boolean)
  581. (defcustom org-table-auto-blank-field t
  582. "Non-nil means, automatically blank table field when starting to type into it.
  583. This only happens when typing immediately after a field motion
  584. command (TAB, S-TAB or RET).
  585. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  586. :group 'org-table-editing
  587. :type 'boolean)
  588. (defcustom org-table-tab-jumps-over-hlines t
  589. "Non-nil means, tab in the last column of a table with jump over a hline.
  590. If a horizontal separator line is following the current line,
  591. `org-table-next-field' can either create a new row before that line, or jump
  592. over the line. When this option is nil, a new line will be created before
  593. this line."
  594. :group 'org-table-editing
  595. :type 'boolean)
  596. (defcustom org-table-tab-recognizes-table.el t
  597. "Non-nil means, TAB will automatically notice a table.el table.
  598. When it sees such a table, it moves point into it and - if necessary -
  599. calls `table-recognize-table'."
  600. :group 'org-table-editing
  601. :type 'boolean)
  602. (defgroup org-table-calculation nil
  603. "Options concerning tables in Org-mode."
  604. :tag "Org Table Calculation"
  605. :group 'org-table)
  606. (defcustom org-table-copy-increment t
  607. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  608. :group 'org-table-calculation
  609. :type 'boolean)
  610. (defcustom org-calc-default-modes
  611. '(calc-internal-prec 12
  612. calc-float-format (float 5)
  613. calc-angle-mode deg
  614. calc-prefer-frac nil
  615. calc-symbolic-mode nil
  616. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  617. calc-display-working-message t
  618. )
  619. "List with Calc mode settings for use in calc-eval for table formulas.
  620. The list must contain alternating symbols (Calc modes variables and values).
  621. Don't remove any of the default settings, just change the values. Org-mode
  622. relies on the variables to be present in the list."
  623. :group 'org-table-calculation
  624. :type 'plist)
  625. (defcustom org-table-formula-evaluate-inline t
  626. "Non-nil means, TAB and RET evaluate a formula in current table field.
  627. If the current field starts with an equal sign, it is assumed to be a formula
  628. which should be evaluated as described in the manual and in the documentation
  629. string of the command `org-table-eval-formula'. This feature requires the
  630. Emacs calc package.
  631. When this variable is nil, formula calculation is only available through
  632. the command \\[org-table-eval-formula]."
  633. :group 'org-table-calculation
  634. :type 'boolean)
  635. (defcustom org-table-formula-use-constants t
  636. "Non-nil means, interpret constants in formulas in tables.
  637. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  638. by the value given in `org-table-formula-constants', or by a value obtained
  639. from the `constants.el' package."
  640. :group 'org-table-calculation
  641. :type 'boolean)
  642. (defcustom org-table-formula-constants nil
  643. "Alist with constant names and values, for use in table formulas.
  644. The car of each element is a name of a constant, without the `$' before it.
  645. The cdr is the value as a string. For example, if you'd like to use the
  646. speed of light in a formula, you would configure
  647. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  648. and then use it in an equation like `$1*$c'."
  649. :group 'org-table-calculation
  650. :type '(repeat
  651. (cons (string :tag "name")
  652. (string :tag "value"))))
  653. (defcustom org-table-formula-numbers-only nil
  654. "Non-nil means, calculate only with numbers in table formulas.
  655. Then all input fields will be converted to a number, and the result
  656. must also be a number. When nil, calc's full potential is available
  657. in table calculations, including symbolics etc."
  658. :group 'org-table-calculation
  659. :type 'boolean)
  660. (defcustom org-table-allow-automatic-line-recalculation t
  661. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  662. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  663. :group 'org-table-calculation
  664. :type 'boolean)
  665. (defgroup org-link nil
  666. "Options concerning links in Org-mode."
  667. :tag "Org Link"
  668. :group 'org)
  669. (defcustom org-descriptive-links t
  670. "Non-nil means, hide link part and only show description of bracket links.
  671. Bracket links are like [[link][descritpion]]. This variable sets the initial
  672. state in new org-mode buffers. The setting can then be toggled on a
  673. per-buffer basis from the Org->Hyperlinks menu."
  674. :group 'org-link
  675. :type 'boolean)
  676. (defcustom org-link-style 'bracket
  677. "The style of links to be inserted with \\[org-insert-link].
  678. Possible values are:
  679. bracket [[link][description]]. This is recommended
  680. plain Description \\n link. The old way, no longer recommended."
  681. :group 'org-link
  682. :type '(choice
  683. (const :tag "Bracket (recommended)" bracket)
  684. (const :tag "Plain (no longer recommended)" plain)))
  685. (defcustom org-link-format "%s"
  686. "Default format for external, URL-like linkes in the buffer.
  687. This is a format string for printf, %s will be replaced by the link text.
  688. The recommended value is just \"%s\", since links will be protected by
  689. enclosing them in double brackets. If you prefer plain links (see variable
  690. `org-link-style'), \"<%s>\" is useful. Some people also recommend an
  691. additional URL: prefix, so the format would be \"<URL:%s>\"."
  692. :group 'org-link
  693. :type '(choice
  694. (const :tag "\"%s\" (e.g. http://www.there.com)" "%s")
  695. (const :tag "\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
  696. (const :tag "\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
  697. (string :tag "Other" :value "<%s>")))
  698. (defcustom org-link-file-path-type 'adaptive
  699. "How the path name in file links should be stored.
  700. Valid values are:
  701. relative relative to the current directory, i.e. the directory of the file
  702. into which the link is being inserted.
  703. absolute absolute path, if possible with ~ for home directory.
  704. noabbrev absolute path, no abbreviation of home directory.
  705. adaptive Use relative path for files in the current directory and sub-
  706. directories of it. For other files, use an absolute path."
  707. :group 'org-link
  708. :type '(choice
  709. (const relative)
  710. (const absolute)
  711. (const noabbrev)
  712. (const adaptive)))
  713. (defcustom org-activate-links '(bracket angle plain radio tag date)
  714. "Types of links that should be activated in Org-mode files.
  715. This is a list of symbols, each leading to the activation of a certain link
  716. type. In principle, it does not hurt to turn on most link types - there may
  717. be a small gain when turning off unused link types. The types are:
  718. bracket The recommended [[link][description]] or [[link]] links with hiding.
  719. angular Links in angular brackes that may contain whitespace like
  720. <bbdb:Carsten Dominik>.
  721. plain Plain links in normal text, no whitespace, like http://google.com.
  722. radio Text that is matched by a radio target, see manual for details.
  723. tag Tag settings in a headline (link to tag search).
  724. date Time stamps (link to calendar).
  725. camel CamelCase words defining text searches.
  726. Changing this variable requires a restart of Emacs to become effective."
  727. :group 'org-link
  728. :type '(set (const :tag "Double bracket links (new style)" bracket)
  729. (const :tag "Angular bracket links (old style)" angular)
  730. (const :tag "plain text links" plain)
  731. (const :tag "Radio target matches" radio)
  732. (const :tag "Tags" tag)
  733. (const :tag "Timestamps" date)
  734. (const :tag "CamelCase words" camel)))
  735. (defgroup org-link-store nil
  736. "Options concerning storing links in Org-mode"
  737. :tag "Org Store Link"
  738. :group 'org-link)
  739. (defcustom org-context-in-file-links t
  740. "Non-nil means, file links from `org-store-link' contain context.
  741. A search string will be added to the file name with :: as separator and
  742. used to find the context when the link is activated by the command
  743. `org-open-at-point'.
  744. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  745. negates this setting for the duration of the command."
  746. :group 'org-link-store
  747. :type 'boolean)
  748. (defcustom org-file-link-context-use-camel-case nil
  749. "Non-nil means, use CamelCase to store a search context in a file link.
  750. When nil, the search string simply consists of the words of the string.
  751. CamelCase is deprecated, and support for it may be dropped in the future."
  752. :group 'org-link-store
  753. :type 'boolean)
  754. (defcustom org-keep-stored-link-after-insertion nil
  755. "Non-nil means, keep link in list for entire session.
  756. The command `org-store-link' adds a link pointing to the current
  757. location to an internal list. These links accumulate during a session.
  758. The command `org-insert-link' can be used to insert links into any
  759. Org-mode file (offering completion for all stored links). When this
  760. option is nil, every link which has been inserted once using \\[org-insert-link]
  761. will be removed from the list, to make completing the unused links
  762. more efficient."
  763. :group 'org-link-store
  764. :type 'boolean)
  765. (defcustom org-usenet-links-prefer-google nil
  766. "Non-nil means, `org-store-link' will create web links to Google groups.
  767. When nil, Gnus will be used for such links.
  768. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  769. negates this setting for the duration of the command."
  770. :group 'org-link-store
  771. :type 'boolean)
  772. (defgroup org-link-follow nil
  773. "Options concerning following links in Org-mode"
  774. :tag "Org Follow Link"
  775. :group 'org-link)
  776. (defcustom org-tab-follows-link nil
  777. "Non-nil means, on links TAB will follow the link.
  778. Needs to be set before org.el is loaded."
  779. :group 'org-link-follow
  780. :type 'boolean)
  781. (defcustom org-return-follows-link nil
  782. "Non-nil means, on links RET will follow the link.
  783. Needs to be set before org.el is loaded."
  784. :group 'org-link-follow
  785. :type 'boolean)
  786. (defcustom org-mouse-1-follows-link t
  787. "Non-nil means, mouse-1 on a link will follow the link.
  788. A longer mouse click will still set point. Does not wortk on XEmacs.
  789. Needs to be set before org.el is loaded."
  790. :group 'org-link-follow
  791. :type 'boolean)
  792. (defcustom org-mark-ring-length 4
  793. "Number of different positions to be recorded in the ring
  794. Changing this requires a restart of Emacs to work correctly."
  795. :group 'org-link-follow
  796. :type 'interger)
  797. (defcustom org-link-frame-setup
  798. '((vm . vm-visit-folder-other-frame)
  799. (gnus . gnus-other-frame)
  800. (file . find-file-other-window))
  801. "Setup the frame configuration for following links.
  802. When following a link with Emacs, it may often be useful to display
  803. this link in another window or frame. This variable can be used to
  804. set this up for the different types of links.
  805. For VM, use any of
  806. `vm-visit-folder'
  807. `vm-visit-folder-other-frame'
  808. For Gnus, use any of
  809. `gnus'
  810. `gnus-other-frame'
  811. For FILE, use any of
  812. `find-file'
  813. `find-file-other-window'
  814. `find-file-other-frame'
  815. For the calendar, use the variable `calendar-setup'.
  816. For BBDB, it is currently only possible to display the matches in
  817. another window."
  818. :group 'org-link-follow
  819. :type '(list
  820. (cons (const vm)
  821. (choice
  822. (const vm-visit-folder)
  823. (const vm-visit-folder-other-window)
  824. (const vm-visit-folder-other-frame)))
  825. (cons (const gnus)
  826. (choice
  827. (const gnus)
  828. (const gnus-other-frame)))
  829. (cons (const file)
  830. (choice
  831. (const find-file)
  832. (const find-file-other-window)
  833. (const find-file-other-frame)))))
  834. (defcustom org-open-non-existing-files nil
  835. "Non-nil means, `org-open-file' will open non-existing file.
  836. When nil, an error will be generated."
  837. :group 'org-link-follow
  838. :type 'boolean)
  839. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  840. "Non-nil means, ask for confirmation before executing shell links.
  841. Shell links can be dangerous, just thing about a link
  842. [[shell:rm -rf ~/*][Google Search]]
  843. This link would show up in your Org-mode document as \"Google Search\"
  844. but really it would remove your entire home directory.
  845. Therefore I *definitely* advise against setting this variable to nil.
  846. Just change it to `y-or-n-p' of you want to confirm with a single key press
  847. rather than having to type \"yes\"."
  848. :group 'org-link-follow
  849. :type '(choice
  850. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  851. (const :tag "with y-or-n (faster)" y-or-n-p)
  852. (const :tag "no confirmation (dangerous)" nil)))
  853. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  854. "Non-nil means, ask for confirmation before executing elisp links.
  855. Elisp links can be dangerous, just thing about a link
  856. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  857. This link would show up in your Org-mode document as \"Google Search\"
  858. but really it would remove your entire home directory.
  859. Therefore I *definitely* advise against setting this variable to nil.
  860. Just change it to `y-or-n-p' of you want to confirm with a single key press
  861. rather than having to type \"yes\"."
  862. :group 'org-link-follow
  863. :type '(choice
  864. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  865. (const :tag "with y-or-n (faster)" y-or-n-p)
  866. (const :tag "no confirmation (dangerous)" nil)))
  867. (defconst org-file-apps-defaults-gnu
  868. '((t . mailcap))
  869. "Default file applications on a UNIX or GNU/Linux system.
  870. See `org-file-apps'.")
  871. (defconst org-file-apps-defaults-macosx
  872. '((t . "open %s")
  873. ("ps" . "gv %s")
  874. ("ps.gz" . "gv %s")
  875. ("eps" . "gv %s")
  876. ("eps.gz" . "gv %s")
  877. ("dvi" . "xdvi %s")
  878. ("fig" . "xfig %s"))
  879. "Default file applications on a MacOS X system.
  880. The system \"open\" is known as a default, but we use X11 applications
  881. for some files for which the OS does not have a good default.
  882. See `org-file-apps'.")
  883. (defconst org-file-apps-defaults-windowsnt
  884. (list (cons t
  885. (list (if (featurep 'xemacs)
  886. 'mswindows-shell-execute
  887. 'w32-shell-execute)
  888. "open" 'file)))
  889. "Default file applications on a Windows NT system.
  890. The system \"open\" is used for most files.
  891. See `org-file-apps'.")
  892. (defcustom org-file-apps
  893. '(
  894. ("txt" . emacs)
  895. ("tex" . emacs)
  896. ("ltx" . emacs)
  897. ("org" . emacs)
  898. ("el" . emacs)
  899. ("bib" . emacs)
  900. )
  901. "External applications for opening `file:path' items in a document.
  902. Org-mode uses system defaults for different file types, but
  903. you can use this variable to set the application for a given file
  904. extension. The entries in this list are cons cells where the car identifies
  905. files and the cdr the corresponding command. Possible values for the
  906. file identifier are
  907. \"ext\" A string identifying an extension
  908. `directory' Matches a directory
  909. t Default for all remaining files
  910. Possible values for the command are:
  911. `emacs' The file will be visited by the current Emacs process.
  912. `default' Use the default application for this file type.
  913. string A command to be executed by a shell; %s will be replaced
  914. by the path to the file.
  915. sexp A Lisp form which will be evaluated. The file path will
  916. be available in the Lisp variable `file'.
  917. For more examples, see the system specific constants
  918. `org-file-apps-defaults-macosx'
  919. `org-file-apps-defaults-windowsnt'
  920. `org-file-apps-defaults-gnu'."
  921. :group 'org-link-follow
  922. :type '(repeat
  923. (cons (choice :value ""
  924. (string :tag "Extension")
  925. (const :tag "Default for unrecognized files" t)
  926. (const :tag "Links to a directory" directory))
  927. (choice :value ""
  928. (const :tag "Visit with Emacs" emacs)
  929. (const :tag "Use system default" default)
  930. (string :tag "Command")
  931. (sexp :tag "Lisp form")))))
  932. (defcustom org-mhe-search-all-folders nil
  933. "Non-nil means, that the search for the mh-message will be extended to
  934. all folders if the message cannot be found in the folder given in the link.
  935. Searching all folders is very effective with one of the search engines
  936. supported by MH-E, but will be slow with pick."
  937. :group 'org-link-follow
  938. :type 'boolean)
  939. (defgroup org-remember nil
  940. "Options concerning interaction with remember.el."
  941. :tag "Org Remember"
  942. :group 'org)
  943. (defcustom org-directory "~/org"
  944. "Directory with org files.
  945. This directory will be used as default to prompt for org files.
  946. Used by the hooks for remember.el."
  947. :group 'org-remember
  948. :type 'directory)
  949. (defcustom org-default-notes-file "~/.notes"
  950. "Default target for storing notes.
  951. Used by the hooks for remember.el. This can be a string, or nil to mean
  952. the value of `remember-data-file'."
  953. :group 'org-remember
  954. :type '(choice
  955. (const :tag "Default from remember-data-file" nil)
  956. file))
  957. (defcustom org-remember-templates nil
  958. "Templates for the creation of remember buffers.
  959. When nil, just let remember make the buffer.
  960. When not nil, this is a list of 3-element lists. In each entry, the first
  961. element is a character, a unique key to select this template.
  962. The second element is the template. The third element is optional and can
  963. specify a destination file for remember items created with this template.
  964. The default file is given by `org-default-notes-file'.
  965. The template specifies the structure of the remember buffer. It should have
  966. a first line starting with a star, to act as the org-mode headline.
  967. Furthermore, the following %-escapes will be replaced with content:
  968. %t time stamp, date only
  969. %T time stamp with date and time
  970. %u inactive time stamp, date only
  971. %U inactive time stamp with date and time
  972. %n user name
  973. %a annotation, normally the link created with org-store-link
  974. %i initial content, the region when remember is called with C-u.
  975. If %i is indented, the entire inserted text will be indented as well.
  976. %? This will be removed, and the cursor placed at this position."
  977. :group 'org-remember
  978. :type '(repeat :tag "enabled"
  979. (list :value (?a "\n" nil)
  980. (character :tag "Selection Key")
  981. (string :tag "Template")
  982. (file :tag "Destination file (optional)"))))
  983. (defcustom org-reverse-note-order nil
  984. "Non-nil means, store new notes at the beginning of a file or entry.
  985. When nil, new notes will be filed to the end of a file or entry."
  986. :group 'org-remember
  987. :type '(choice
  988. (const :tag "Reverse always" t)
  989. (const :tag "Reverse never" nil)
  990. (repeat :tag "By file name regexp"
  991. (cons regexp boolean))))
  992. (defgroup org-todo nil
  993. "Options concerning TODO items in Org-mode."
  994. :tag "Org TODO"
  995. :group 'org)
  996. (defcustom org-todo-keywords '("TODO" "DONE")
  997. "List of TODO entry keywords.
  998. \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
  999. considered to mean that the entry is \"done\". All the other mean that
  1000. action is required, and will make the entry show up in todo lists, diaries
  1001. etc.
  1002. The command \\[org-todo] cycles an entry through these states, and an
  1003. additional state where no keyword is present. For details about this
  1004. cycling, see also the variable `org-todo-interpretation'
  1005. Changes become only effective after restarting Emacs."
  1006. :group 'org-todo
  1007. :group 'org-keywords
  1008. :type '(repeat (string :tag "Keyword")))
  1009. (defcustom org-todo-interpretation 'sequence
  1010. "Controls how TODO keywords are interpreted.
  1011. This variable is only relevant if `org-todo-keywords' contains more than two
  1012. states. \\<org-mode-map>Possible values are `sequence' and `type'.
  1013. When `sequence', \\[org-todo] will always switch to the next state in the
  1014. `org-todo-keywords' list. When `type', \\[org-todo] only cycles from state
  1015. to state when executed several times in direct succession. Otherwise, it
  1016. switches directly to DONE from any state.
  1017. See the manual for more information."
  1018. :group 'org-todo
  1019. :group 'org-keywords
  1020. :type '(choice (const sequence)
  1021. (const type)))
  1022. (defcustom org-after-todo-state-change-hook nil
  1023. "Hook which is run after the state of a TODO item was changed.
  1024. The new state (a string with a TODO keyword, or nil) is available in the
  1025. Lisp variable `state'."
  1026. :group 'org-todo
  1027. :type 'hook)
  1028. (defcustom org-log-done nil
  1029. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1030. When the state of an entry is changed from nothing to TODO, remove a previous
  1031. closing date.
  1032. This can also be configured on a per-file basis by adding one of
  1033. the following lines anywhere in the buffer:
  1034. #+STARTUP: logging
  1035. #+STARTUP: nologging"
  1036. :group 'org-todo
  1037. :type 'boolean)
  1038. (defgroup org-priorities nil
  1039. "Priorities in Org-mode."
  1040. :tag "Org Priorities"
  1041. :group 'org-todo)
  1042. (defcustom org-default-priority ?B
  1043. "The default priority of TODO items.
  1044. This is the priority an item get if no explicit priority is given."
  1045. :group 'org-priorities
  1046. :type 'character)
  1047. (defcustom org-lowest-priority ?C
  1048. "The lowest priority of TODO items. A character like ?A, ?B etc."
  1049. :group 'org-priorities
  1050. :type 'character)
  1051. (defgroup org-time nil
  1052. "Options concerning time stamps and deadlines in Org-mode."
  1053. :tag "Org Time"
  1054. :group 'org)
  1055. (defcustom org-insert-labeled-timestamps-at-point nil
  1056. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1057. When nil, these labeled time stamps are forces into the second line of an
  1058. entry, just after the headline. When scheduling from the global TODO list,
  1059. the time stamp will always be forced into the second line."
  1060. :group 'org-time
  1061. :type 'boolean)
  1062. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1063. "Formats for `format-time-string' which are used for time stamps.
  1064. It is not recommended to change this constant.")
  1065. (defcustom org-time-stamp-rounding-minutes 0
  1066. "Number of minutes to round time stamps to upon insertion.
  1067. When zero, insert the time unmodified. Useful rounding numbers
  1068. should be factors of 60, so for example 5, 10, 15.
  1069. When this is not zero, you can still force an exact time-stamp by using
  1070. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1071. :group 'org-time
  1072. :type 'integer)
  1073. (defcustom org-deadline-warning-days 30
  1074. "No. of days before expiration during which a deadline becomes active.
  1075. This variable governs the display in the org file."
  1076. :group 'org-time
  1077. :type 'number)
  1078. (defcustom org-popup-calendar-for-date-prompt t
  1079. "Non-nil means, pop up a calendar when prompting for a date.
  1080. In the calendar, the date can be selected with mouse-1. However, the
  1081. minibuffer will also be active, and you can simply enter the date as well.
  1082. When nil, only the minibuffer will be available."
  1083. :group 'org-time
  1084. :type 'boolean)
  1085. (defcustom org-calendar-follow-timestamp-change t
  1086. "Non-nil means, make the calendar window follow timestamp changes.
  1087. When a timestamp is modified and the calendar window is visible, it will be
  1088. moved to the new date."
  1089. :group 'org-time
  1090. :type 'boolean)
  1091. (defgroup org-tags nil
  1092. "Options concerning tags in Org-mode."
  1093. :tag "Org Tags"
  1094. :group 'org)
  1095. (defcustom org-tag-alist nil
  1096. "List of tags allowed in Org-mode files.
  1097. When this list is nil, Org-mode will base TAG input on what is already in the
  1098. buffer.
  1099. The value of this variable is an alist, the car may be (and should) be a
  1100. character that is used to select that tag through the fast-tag-selection
  1101. interface. See the manual for details."
  1102. :group 'org-tags
  1103. :type '(repeat
  1104. (cons (character) (string :tag "Tag"))))
  1105. (defcustom org-use-fast-tag-selection 'auto
  1106. "Non-nil means, use fast tag selection scheme.
  1107. This is a special interface to select and deselect tags with single keys.
  1108. When nil, fast selection is never used.
  1109. When the symbol `auto', fast selection is used if and only if selection
  1110. characters for tags have been configured, either through the variable
  1111. `org-tag-alist' or through a #+TAGS line in the buffer.
  1112. When t, fast selection is always used and selection keys are assigned
  1113. automatically if necessary."
  1114. :group 'org-tags
  1115. :type '(choice
  1116. (const :tag "Always" t)
  1117. (const :tag "Never" nil)
  1118. (const :tag "When selection characters are configured" 'auto)))
  1119. (defcustom org-tags-column 48
  1120. "The column to which tags should be indented in a headline.
  1121. If this number is positive, it specifies the column. If it is negative,
  1122. it means that the tags should be flushright to that column. For example,
  1123. -79 works well for a normal 80 character screen."
  1124. :group 'org-tags
  1125. :type 'integer)
  1126. (defcustom org-auto-align-tags t
  1127. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1128. These operations change the length of a headline and therefore shift
  1129. the tags around. With this options turned on, after each such operation
  1130. the tags are again aligned to `org-tags-column'."
  1131. :group 'org-tags
  1132. :type 'boolean)
  1133. (defcustom org-use-tag-inheritance t
  1134. "Non-nil means, tags in levels apply also for sublevels.
  1135. When nil, only the tags directly given in a specific line apply there.
  1136. If you turn off this option, you very likely want to turn on the
  1137. companion option `org-tags-match-list-sublevels'."
  1138. :group 'org-tags
  1139. :type 'boolean)
  1140. (defcustom org-tags-match-list-sublevels nil
  1141. "Non-nil means list also sublevels of headlines matching tag search.
  1142. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1143. the sublevels of a headline matching a tag search often also match
  1144. the same search. Listing all of them can create very long lists.
  1145. Setting this variable to nil causes subtrees of a match to be skipped.
  1146. This option is off by default, because inheritance in on. If you turn
  1147. inheritance off, you very likely want to turn this option on.
  1148. As a special case, if the tag search is restricted to TODO items, the
  1149. value of this variable is ignored and sublevels are always checked, to
  1150. make sure all corresponding TODO items find their way into the list."
  1151. :group 'org-tags
  1152. :type 'boolean)
  1153. (defvar org-tags-history nil
  1154. "History of minibuffer reads for tags.")
  1155. (defvar org-last-tags-completion-table nil
  1156. "The last used completion table for tags.")
  1157. (defgroup org-agenda nil
  1158. "Options concerning agenda display Org-mode."
  1159. :tag "Org Agenda"
  1160. :group 'org)
  1161. (defvar org-category nil
  1162. "Variable used by org files to set a category for agenda display.
  1163. Such files should use a file variable to set it, for example
  1164. -*- mode: org; org-category: \"ELisp\"
  1165. or contain a special line
  1166. #+CATEGORY: ELisp
  1167. If the file does not specify a category, then file's base name
  1168. is used instead.")
  1169. (make-variable-buffer-local 'org-category)
  1170. (defcustom org-agenda-files nil
  1171. "The files to be used for agenda display.
  1172. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1173. \\[org-remove-file]. You can also use customize to edit the list.
  1174. If the value of the variable is not a list but a single file name, then
  1175. the list of agenda files is actually stored and maintained in that file, one
  1176. agenda file per line."
  1177. :group 'org-agenda
  1178. :type '(choice
  1179. (repeat :tag "List of files" file)
  1180. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1181. (defcustom org-agenda-custom-commands '(("w" todo "WAITING"))
  1182. "Custom commands for the agenda.
  1183. These commands will be offered on the splash screen displayed by the
  1184. agenda dispatcher \\[org-agenda]. Each entry is a list of 3 items:
  1185. key The key (a single char as a string) to be associated with the command.
  1186. type The command type, any of the following symbols:
  1187. todo Entries with a specific TODO keyword, in all agenda files.
  1188. tags Tags match in all agenda files.
  1189. tags-todo Tags match in all agenda files, TODO entries only.
  1190. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1191. tags-tree Sparse tree with all tags matches in *current* file.
  1192. occur-tree Occur sparse tree for current file.
  1193. match What to search for:
  1194. - a single keyword for TODO keyword searches
  1195. - a tags match expression for tags searches
  1196. - a regular expression for occur searches"
  1197. :group 'org-agenda
  1198. :type '(repeat
  1199. (list (string :tag "Key")
  1200. (choice :tag "Type"
  1201. (const :tag "Tags search in all agenda files" tags)
  1202. (const :tag "Tags search of TODO entries, all agenda files" tags-todo)
  1203. (const :tag "TODO keyword search in all agenda files" todo)
  1204. (const :tag "Tags sparse tree in current buffer" tags-tree)
  1205. (const :tag "TODO keyword tree in current buffer" todo-tree)
  1206. (const :tag "Occur tree in current buffer" occur-tree))
  1207. (string :tag "Match"))))
  1208. ;; FIXME: Need a toggle for this variable, maybe a mode in the agenda buffer?
  1209. (defcustom org-agenda-todo-list-sublevels t
  1210. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1211. When nil, the sublevels of a TODO entry are not checked, resulting in
  1212. potentially much shorter TODO lists."
  1213. :group 'org-agenda
  1214. :group 'org-todo
  1215. :type 'boolean)
  1216. (defcustom org-agenda-todo-ignore-scheduled nil
  1217. "Non-nil means, don't show scheduled entries in the global todo list.
  1218. The idea behind this is that by scheduling it, you have already taken care
  1219. of this item."
  1220. :group 'org-agenda
  1221. :group 'org-todo
  1222. :type 'boolean)
  1223. (defcustom org-agenda-include-all-todo nil
  1224. "Non-nil means, the agenda will always contain all TODO entries.
  1225. When nil, date-less entries will only be shown if `org-agenda' is called
  1226. with a prefix argument.
  1227. When non-nil, the TODO entries will be listed at the top of the agenda, before
  1228. the entries for specific days."
  1229. :group 'org-agenda
  1230. :type 'boolean)
  1231. (defcustom org-agenda-include-diary nil
  1232. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1233. :group 'org-agenda
  1234. :type 'boolean)
  1235. (defcustom org-calendar-to-agenda-key [?c]
  1236. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1237. The command `org-calendar-goto-agenda' will be bound to this key. The
  1238. default is the character `c' because then `c' can be used to switch back and
  1239. forth between agenda and calendar."
  1240. :group 'org-agenda
  1241. :type 'sexp)
  1242. (defgroup org-agenda-setup nil
  1243. "Options concerning setting up the Agenda window in Org Mode."
  1244. :tag "Org Agenda Window Setup"
  1245. :group 'org-agenda)
  1246. (defcustom org-agenda-mouse-1-follows-link nil
  1247. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1248. A longer mouse click will still set point. Does not wortk on XEmacs.
  1249. Needs to be set before org.el is loaded."
  1250. :group 'org-agenda-setup
  1251. :type 'boolean)
  1252. (defcustom org-agenda-start-with-follow-mode nil
  1253. "The initial value of follwo-mode in a newly created agenda window."
  1254. :group 'org-agenda-setup
  1255. :type 'boolean)
  1256. (defcustom org-select-timeline-window t
  1257. "Non-nil means, after creating a timeline, move cursor into Timeline window.
  1258. When nil, cursor will remain in the current window."
  1259. :group 'org-agenda-setup
  1260. :type 'boolean)
  1261. (defcustom org-select-agenda-window t
  1262. "Non-nil means, after creating an agenda, move cursor into Agenda window.
  1263. When nil, cursor will remain in the current window."
  1264. :group 'org-agenda-setup
  1265. :type 'boolean)
  1266. (defcustom org-fit-agenda-window t
  1267. "Non-nil means, change window size of agenda to fit content."
  1268. :group 'org-agenda-setup
  1269. :type 'boolean)
  1270. (defgroup org-agenda-display nil
  1271. "Options concerning what to display initially in Agenda."
  1272. :tag "Org Agenda Display"
  1273. :group 'org-agenda)
  1274. (defcustom org-agenda-show-all-dates t
  1275. "Non-nil means, `org-agenda' shows every day in the selected range.
  1276. When nil, only the days which actually have entries are shown."
  1277. :group 'org-agenda-display
  1278. :type 'boolean)
  1279. (defcustom org-agenda-start-on-weekday 1
  1280. "Non-nil means, start the overview always on the specified weekday.
  1281. 0 denotes Sunday, 1 denotes Monday etc.
  1282. When nil, always start on the current day."
  1283. :group 'org-agenda-display
  1284. :type '(choice (const :tag "Today" nil)
  1285. (number :tag "Weekday No.")))
  1286. (defcustom org-agenda-ndays 7
  1287. "Number of days to include in overview display.
  1288. Should be 1 or 7."
  1289. :group 'org-agenda-display
  1290. :type 'number)
  1291. (defcustom org-agenda-use-time-grid t
  1292. "Non-nil means, show a time grid in the agenda schedule.
  1293. A time grid is a set of lines for specific times (like every two hours between
  1294. 8:00 and 20:00). The items scheduled for a day at specific times are
  1295. sorted in between these lines.
  1296. For details about when the grid will be shown, and what it will look like, see
  1297. the variable `org-agenda-time-grid'."
  1298. :group 'org-agenda-display
  1299. :type 'boolean)
  1300. (defcustom org-agenda-time-grid
  1301. '((daily today require-timed)
  1302. "----------------"
  1303. (800 1000 1200 1400 1600 1800 2000))
  1304. "The settings for time grid for agenda display.
  1305. This is a list of three items. The first item is again a list. It contains
  1306. symbols specifying conditions when the grid should be displayed:
  1307. daily if the agenda shows a single day
  1308. weekly if the agenda shows an entire week
  1309. today show grid on current date, independent of daily/weekly display
  1310. require-timed show grid only if at least on item has a time specification
  1311. The second item is a string which will be places behing the grid time.
  1312. The third item is a list of integers, indicating the times that should have
  1313. a grid line."
  1314. :group 'org-agenda-display
  1315. :type
  1316. '(list
  1317. (set :greedy t :tag "Grid Display Options"
  1318. (const :tag "Show grid in single day agenda display" daily)
  1319. (const :tag "Show grid in weekly agenda display" weekly)
  1320. (const :tag "Always show grid for today" today)
  1321. (const :tag "Show grid only if any timed entries are present"
  1322. require-timed)
  1323. (const :tag "Skip grid times already present in an entry"
  1324. remove-match))
  1325. (string :tag "Grid String")
  1326. (repeat :tag "Grid Times" (integer :tag "Time"))))
  1327. (defcustom org-agenda-sorting-strategy '(time-up category-keep priority-down)
  1328. "Sorting structure for the agenda items of a single day.
  1329. This is a list of symbols which will be used in sequence to determine
  1330. if an entry should be listed before another entry. The following
  1331. symbols are recognized:
  1332. time-up Put entries with time-of-day indications first, early first
  1333. time-down Put entries with time-of-day indications first, late first
  1334. category-keep Keep the default order of categories, corresponding to the
  1335. sequence in `org-agenda-files'.
  1336. category-up Sort alphabetically by category, A-Z.
  1337. category-down Sort alphabetically by category, Z-A.
  1338. priority-up Sort numerically by priority, high priority last.
  1339. priority-down Sort numerically by priority, high priority first.
  1340. The different possibilities will be tried in sequence, and testing stops
  1341. if one comparison returns a \"not-equal\". For example, the default
  1342. '(time-up category-keep priority-down)
  1343. means: Pull out all entries having a specified time of day and sort them,
  1344. in order to make a time schedule for the current day the first thing in the
  1345. agenda listing for the day. Of the entries without a time indication, keep
  1346. the grouped in categories, don't sort the categories, but keep them in
  1347. the sequence given in `org-agenda-files'. Within each category sort by
  1348. priority.
  1349. Leaving out `category-keep' would mean that items will be sorted across
  1350. categories by priority."
  1351. :group 'org-agenda-display
  1352. :type '(repeat
  1353. (choice
  1354. (const time-up)
  1355. (const time-down)
  1356. (const category-keep)
  1357. (const category-up)
  1358. (const category-down)
  1359. (const priority-up)
  1360. (const priority-down))))
  1361. (defcustom org-sort-agenda-notime-is-late t
  1362. "Non-nil means, items without time are considered late.
  1363. This is only relevant for sorting. When t, items which have no explicit
  1364. time like 15:30 will be considered as 24:01, i.e. later than any items which
  1365. do have a time. When nil, the default time is before 0:00. You can use this
  1366. option to decide if the schedule for today should come before or after timeless
  1367. agenda entries."
  1368. :group 'org-agenda-display
  1369. :type 'boolean)
  1370. (defgroup org-agenda-prefix nil
  1371. "Options concerning the entry prefix in the Org-mode agenda display."
  1372. :tag "Org Agenda Prefix"
  1373. :group 'org-agenda)
  1374. (defcustom org-agenda-prefix-format " %-12:c%?-12t% s"
  1375. "Format specification for the prefix of items in the agenda buffer.
  1376. This format works similar to a printf format, with the following meaning:
  1377. %c the category of the item, \"Diary\" for entries from the diary, or
  1378. as given by the CATEGORY keyword or derived from the file name.
  1379. %T the first tag of the item.
  1380. %t the time-of-day specification if one applies to the entry, in the
  1381. format HH:MM
  1382. %s Scheduling/Deadline information, a short string
  1383. All specifiers work basically like the standard `%s' of printf, but may
  1384. contain two additional characters: A question mark just after the `%' and
  1385. a whitespace/punctuation character just before the final letter.
  1386. If the first character after `%' is a question mark, the entire field
  1387. will only be included if the corresponding value applies to the
  1388. current entry. This is useful for fields which should have fixed
  1389. width when present, but zero width when absent. For example,
  1390. \"%?-12t\" will result in a 12 character time field if a time of the
  1391. day is specified, but will completely disappear in entries which do
  1392. not contain a time.
  1393. If there is punctuation or whitespace character just before the final
  1394. format letter, this character will be appended to the field value if
  1395. the value is not empty. For example, the format \"%-12:c\" leads to
  1396. \"Diary: \" if the category is \"Diary\". If the category were be
  1397. empty, no additional colon would be interted.
  1398. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  1399. - Indent the line with two space characters
  1400. - Give the category in a 12 chars wide field, padded with whitespace on
  1401. the right (because of `-'). Append a colon if there is a category
  1402. (because of `:').
  1403. - If there is a time-of-day, put it into a 12 chars wide field. If no
  1404. time, don't put in an empty field, just skip it (because of '?').
  1405. - Finally, put the scheduling information and append a whitespace.
  1406. As another example, if you don't want the time-of-day of entries in
  1407. the prefix, you could use:
  1408. (setq org-agenda-prefix-format \" %-11:c% s\")
  1409. See also the variables `org-agenda-remove-times-when-in-prefix' and
  1410. `org-agenda-remove-tags-when-in-prefix'."
  1411. :type 'string
  1412. :group 'org-agenda-prefix)
  1413. (defcustom org-timeline-prefix-format " % s"
  1414. "Like `org-agenda-prefix-format', but for the timeline of a single file."
  1415. :type 'string
  1416. :group 'org-agenda-prefix)
  1417. (defvar org-prefix-format-compiled nil
  1418. "The compiled version of the most recently used prefix format.
  1419. Depending on which command was used last, this may be the compiled version
  1420. of `org-agenda-prefix-format' or `org-timeline-prefix-format'.")
  1421. ;; FIXME: There seem to be situations where this does not work.
  1422. (defcustom org-agenda-remove-times-when-in-prefix t
  1423. "Non-nil means, remove duplicate time specifications in agenda items.
  1424. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  1425. time-of-day specification in a headline or diary entry is extracted and
  1426. placed into the prefix. If this option is non-nil, the original specification
  1427. \(a timestamp or -range, or just a plain time(range) specification like
  1428. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  1429. cluttered.
  1430. The option can be t or nil. It may also be the symbol `beg', indicating
  1431. that the time should only be removed what it is located at the beginning of
  1432. the headline/diary entry."
  1433. :group 'org-agenda-prefix
  1434. :type '(choice
  1435. (const :tag "Always" t)
  1436. (const :tag "Never" nil)
  1437. (const :tag "When at beginning of entry" beg)))
  1438. (defcustom org-agenda-remove-tags-when-in-prefix nil
  1439. "Non-nil means, remove the tags from the headline copy in the agenda.
  1440. When this is the symbol `prefix', only remove tags when
  1441. `org-agenda-prefix-format' contains a `%T' specifier."
  1442. :group 'org-agenda-prefix
  1443. :type '(choice
  1444. (const :tag "Always" t)
  1445. (const :tag "Never" nil)
  1446. (const :tag "When prefix format contains %T" prefix)))
  1447. (defgroup org-export nil
  1448. "Options for exporting org-listings."
  1449. :tag "Org Export"
  1450. :group 'org)
  1451. (defgroup org-export-general nil
  1452. "General options for exporting Org-mode files."
  1453. :tag "Org Export General"
  1454. :group 'org-export)
  1455. (defcustom org-export-publishing-directory "."
  1456. "Path to the location where exported files should be located.
  1457. This path may be relative to the directory where the Org-mode file lives.
  1458. The default is to put them into the same directory as the Org-mode file.
  1459. The variable may also be an alist with export types `:html', `:ascii',
  1460. `:ical', or `:xoxo' and the corresponding directories. If a direcoty path
  1461. is relative, it is interpreted relative to the directory where the exported
  1462. Org-mode files lives."
  1463. :group 'org-export-general
  1464. :type '(choice
  1465. (directory)
  1466. (repeat
  1467. (cons
  1468. (choice :tag "Type"
  1469. (const :html) (const :ascii) (const :ical) (const :xoxo))
  1470. (directory)))))
  1471. (defcustom org-export-language-setup
  1472. '(("en" "Author" "Date" "Table of Contents")
  1473. ("da" "Ophavsmand" "Dato" "Indhold")
  1474. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  1475. ("es" "Autor" "Fecha" "\xccndice")
  1476. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  1477. ("it" "Autore" "Data" "Indice")
  1478. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  1479. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  1480. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  1481. "Terms used in export text, translated to different languages.
  1482. Use the variable `org-export-default-language' to set the language,
  1483. or use the +OPTION lines for a per-file setting."
  1484. :group 'org-export-general
  1485. :type '(repeat
  1486. (list
  1487. (string :tag "HTML language tag")
  1488. (string :tag "Author")
  1489. (string :tag "Date")
  1490. (string :tag "Table of Contents"))))
  1491. (defcustom org-export-default-language "en"
  1492. "The default language of HTML export, as a string.
  1493. This should have an association in `org-export-language-setup'."
  1494. :group 'org-export-general
  1495. :type 'string)
  1496. (defcustom org-export-headline-levels 3
  1497. "The last level which is still exported as a headline.
  1498. Inferior levels will produce itemize lists when exported.
  1499. Note that a numeric prefix argument to an exporter function overrides
  1500. this setting.
  1501. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  1502. :group 'org-export-general
  1503. :type 'number)
  1504. (defcustom org-export-with-section-numbers t
  1505. "Non-nil means, add section numbers to headlines when exporting.
  1506. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  1507. :group 'org-export-general
  1508. :type 'boolean)
  1509. (defcustom org-export-with-toc t
  1510. "Non-nil means, create a table of contents in exported files.
  1511. The TOC contains headlines with levels up to`org-export-headline-levels'.
  1512. Headlines which contain any TODO items will be marked with \"(*)\" in
  1513. ASCII export, and with red color in HTML output.
  1514. In HTML output, the TOC will be clickable.
  1515. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
  1516. :group 'org-export-general
  1517. :type 'boolean)
  1518. (defcustom org-export-mark-todo-in-toc nil
  1519. "Non-nil means, mark TOC lines that contain any open TODO items."
  1520. :group 'org-export-general
  1521. :type 'boolean)
  1522. (defcustom org-export-preserve-breaks nil
  1523. "Non-nil means, preserve all line breaks when exporting.
  1524. Normally, in HTML output paragraphs will be reformatted. In ASCII
  1525. export, line breaks will always be preserved, regardless of this variable.
  1526. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  1527. :group 'org-export-general
  1528. :type 'boolean)
  1529. (defcustom org-export-with-timestamps t
  1530. "Nil means, do not export time stamps and associated keywords."
  1531. :group 'org-export
  1532. :type 'boolean)
  1533. (defcustom org-export-with-tags t
  1534. "Nil means, do not export tags, just remove them from headlines."
  1535. :group 'org-export-general
  1536. :type 'boolean)
  1537. (defcustom org-export-with-timestamps t
  1538. "Nil means, do not export timestamps and associated keywords."
  1539. :group 'org-export-general
  1540. :type 'boolean)
  1541. (defgroup org-export-translation nil
  1542. "Options for translating special ascii sequences for the export backends."
  1543. :tag "Org Export Translation"
  1544. :group 'org-export)
  1545. (defcustom org-export-with-emphasize t
  1546. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  1547. If the export target supports emphasizing text, the word will be
  1548. typeset in bold, italic, or underlined, respectively. Works only for
  1549. single words, but you can say: I *really* *mean* *this*.
  1550. Not all export backends support this.
  1551. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  1552. :group 'org-export-translation
  1553. :type 'boolean)
  1554. (defcustom org-export-with-sub-superscripts t
  1555. "Non-nil means, interpret \"_\" and \"^\" for export.
  1556. When this option is turned on, you can use TeX-like syntax for sub- and
  1557. superscripts. Several characters after \"_\" or \"^\" will be
  1558. considered as a single item - so grouping with {} is normally not
  1559. needed. For example, the following things will be parsed as single
  1560. sub- or superscripts.
  1561. 10^24 or 10^tau several digits will be considered 1 item.
  1562. 10^-12 or 10^-tau a leading sign with digits or a word
  1563. x^2-y^3 will be read as x^2 - y^3, because items are
  1564. terminated by almost any nonword/nondigit char.
  1565. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  1566. Still, ambiguity is possible - so when in doubt use {} to enclose the
  1567. sub/superscript.
  1568. Not all export backends support this, but HTML does.
  1569. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  1570. :group 'org-export-translation
  1571. :type 'boolean)
  1572. (defcustom org-export-with-TeX-macros t
  1573. "Non-nil means, interpret simple TeX-like macros when exporting.
  1574. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  1575. No only real TeX macros will work here, but the standard HTML entities
  1576. for math can be used as macro names as well. For a list of supported
  1577. names in HTML export, see the constant `org-html-entities'.
  1578. Not all export backends support this.
  1579. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  1580. :group 'org-export-translation
  1581. :type 'boolean)
  1582. (defcustom org-export-with-fixed-width t
  1583. "Non-nil means, lines starting with \":\" will be in fixed width font.
  1584. This can be used to have pre-formatted text, fragments of code etc. For
  1585. example:
  1586. : ;; Some Lisp examples
  1587. : (while (defc cnt)
  1588. : (ding))
  1589. will be looking just like this in also HTML. See also the QUOTE keyword.
  1590. Not all export backends support this.
  1591. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  1592. :group 'org-export-translation
  1593. :type 'boolean)
  1594. (defcustom org-match-sexp-depth 3
  1595. "Number of stacked braces for sub/superscript matching.
  1596. This has to be set before loading org.el to be effective."
  1597. :group 'org-export-translation
  1598. :type 'integer)
  1599. (defgroup org-export-tables nil
  1600. "Options for exporting tables in Org-mode."
  1601. :tag "Org Export Tables"
  1602. :group 'org-export)
  1603. (defcustom org-export-with-tables t
  1604. "If non-nil, lines starting with \"|\" define a table.
  1605. For example:
  1606. | Name | Address | Birthday |
  1607. |-------------+----------+-----------|
  1608. | Arthur Dent | England | 29.2.2100 |
  1609. Not all export backends support this.
  1610. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  1611. :group 'org-export-tables
  1612. :type 'boolean)
  1613. (defcustom org-export-highlight-first-table-line t
  1614. "Non-nil means, highlight the first table line.
  1615. In HTML export, this means use <th> instead of <td>.
  1616. In tables created with table.el, this applies to the first table line.
  1617. In Org-mode tables, all lines before the first horizontal separator
  1618. line will be formatted with <th> tags."
  1619. :group 'org-export-tables
  1620. :type 'boolean)
  1621. (defcustom org-export-table-remove-special-lines t
  1622. "Remove special lines and marking characters in calculating tables.
  1623. This removes the special marking character column from tables that are set
  1624. up for spreadsheet calculations. It also removes the entire lines
  1625. marked with `!', `_', or `^'. The lines with `$' are kept, because
  1626. the values of constants may be useful to have."
  1627. :group 'org-export-tables
  1628. :type 'boolean)
  1629. (defcustom org-export-prefer-native-exporter-for-tables nil
  1630. "Non-nil means, always export tables created with table.el natively.
  1631. Natively means, use the HTML code generator in table.el.
  1632. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  1633. the table does not use row- or column-spanning). This has the
  1634. advantage, that the automatic HTML conversions for math symbols and
  1635. sub/superscripts can be applied. Org-mode's HTML generator is also
  1636. much faster."
  1637. :group 'org-export-tables
  1638. :type 'boolean)
  1639. (defgroup org-export-ascii nil
  1640. "Options specific for ASCII export of Org-mode files."
  1641. :tag "Org Export ASCII"
  1642. :group 'org-export)
  1643. (defcustom org-export-ascii-show-new-buffer t
  1644. "Non-nil means, popup buffer containing the exported ASCII text.
  1645. Otherwise the buffer will just be saved to a file and stay hidden."
  1646. :group 'org-export-ascii
  1647. :type 'boolean)
  1648. (defgroup org-export-xml nil
  1649. "Options specific for XML export of Org-mode files."
  1650. :tag "Org Export XML"
  1651. :group 'org-export)
  1652. ;; FIXME: I am told XOXO is not XML, it is semantic-only HTML.
  1653. (defcustom org-export-xml-type 'xoxo
  1654. "The kind of XML to be produced by the XML exporter.
  1655. Allowed values are:
  1656. xoxo The XOXO exporter."
  1657. :group 'org-export-xml
  1658. :type '(choice
  1659. (const :tag "XOXO" xoxo)))
  1660. (defgroup org-export-html nil
  1661. "Options specific for HTML export of Org-mode files."
  1662. :tag "Org Export HTML"
  1663. :group 'org-export)
  1664. (defcustom org-export-html-style
  1665. "<style type=\"text/css\">
  1666. html {
  1667. font-family: Times, serif;
  1668. font-size: 12pt;
  1669. }
  1670. .title { text-align: center; }
  1671. .todo { color: red; }
  1672. .done { color: green; }
  1673. .timestamp { color: grey }
  1674. .timestamp-kwd { color: CadetBlue }
  1675. .tag { background-color:lightblue; font-weight:normal }
  1676. .target { background-color: lavender; }
  1677. pre {
  1678. border: 1pt solid #AEBDCC;
  1679. background-color: #F3F5F7;
  1680. padding: 5pt;
  1681. font-family: courier, monospace;
  1682. }
  1683. table { border-collapse: collapse; }
  1684. td, th {
  1685. vertical-align: top;
  1686. border: 1pt solid #ADB9CC;
  1687. }
  1688. </style>"
  1689. "The default style specification for exported HTML files.
  1690. Since there are different ways of setting style information, this variable
  1691. needs to contain the full HTML structure to provide a style, including the
  1692. surrounding HTML tags. The style specifications should include definitions
  1693. for new classes todo, done, title, and deadline. For example, legal values
  1694. would be:
  1695. <style type=\"text/css\">
  1696. p { font-weight: normal; color: gray; }
  1697. h1 { color: black; }
  1698. .title { text-align: center; }
  1699. .todo, .deadline { color: red; }
  1700. .done { color: green; }
  1701. </style>
  1702. or, if you want to keep the style in a file,
  1703. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1704. As the value of this option simply gets inserted into the HTML <head> header,
  1705. you can \"misuse\" it to add arbitrary text to the header."
  1706. :group 'org-export-html
  1707. :type 'string)
  1708. (defcustom org-export-html-link-org-files-as-html t
  1709. "Non-nil means, make file links to `file.org' point to `file.html'.
  1710. When org-mode is exporting an org-mode file to HTML, links to
  1711. non-html files are directly put into a href tag in HTML.
  1712. However, links to other Org-mode files (recognized by the
  1713. extension `.org.) should become links to the corresponding html
  1714. file, assuming that the linked org-mode file will also be
  1715. converted to HTML.
  1716. When nil, the links still point to the plain `.org' file."
  1717. :group 'org-export-html
  1718. :type 'boolean)
  1719. (defcustom org-export-html-inline-images 'maybe
  1720. "Non-nil means, inline images into exported HTML pages.
  1721. This is done using an <img> tag. When nil, an anchor with href is used to
  1722. link to the image. If this option is `maybe', then images in links with
  1723. an empty description will be inlined, while images with a description will
  1724. be linked only."
  1725. :group 'org-export-html
  1726. :type '(choice (const :tag "Never" nil)
  1727. (const :tag "Always" t)
  1728. (const :tag "When there is no description" maybe)))
  1729. (defcustom org-export-html-expand t
  1730. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  1731. When nil, these tags will be exported as plain text and therefore
  1732. not be interpreted by a browser.
  1733. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  1734. :group 'org-export-html
  1735. :type 'boolean)
  1736. (defcustom org-export-html-table-tag
  1737. "<table border=1 cellspacing=0 cellpadding=6>"
  1738. "The HTML tag used to start a table.
  1739. This must be a <table> tag, but you may change the options like
  1740. borders and spacing."
  1741. :group 'org-export-html
  1742. :type 'string)
  1743. (defcustom org-export-html-with-timestamp nil
  1744. "If non-nil, write `org-export-html-html-helper-timestamp'
  1745. into the exported HTML text. Otherwise, the buffer will just be saved
  1746. to a file."
  1747. :group 'org-export-html
  1748. :type 'boolean)
  1749. (defcustom org-export-html-html-helper-timestamp
  1750. "<br><br><hr><p><!-- hhmts start --> <!-- hhmts end -->\n"
  1751. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  1752. :group 'org-export-html
  1753. :type 'string)
  1754. (defcustom org-export-html-show-new-buffer nil
  1755. "Non-nil means, popup buffer containing the exported html text.
  1756. Otherwise, the buffer will just be saved to a file and stay hidden."
  1757. :group 'org-export-html
  1758. :type 'boolean)
  1759. (defgroup org-export-icalendar nil
  1760. "Options specific for iCalendar export of Org-mode files."
  1761. :tag "Org Export iCalendar"
  1762. :group 'org-export)
  1763. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  1764. "The file name for the iCalendar file covering all agenda files.
  1765. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  1766. The file name should be absolute."
  1767. :group 'org-export-icalendar
  1768. :type 'file)
  1769. (defcustom org-icalendar-include-todo nil
  1770. "Non-nil means, export to iCalendar files should also cover TODO items."
  1771. :group 'org-export-icalendar
  1772. :type 'boolean)
  1773. (defcustom org-icalendar-combined-name "OrgMode"
  1774. "Calendar name for the combined iCalendar representing all agenda files."
  1775. :group 'org-export-icalendar
  1776. :type 'string)
  1777. (defgroup org-font-lock nil
  1778. "Font-lock settings for highlighting in Org-mode."
  1779. :tag "Org Font Lock"
  1780. :group 'org)
  1781. (defcustom org-level-color-stars-only nil
  1782. "Non-nil means fontify only the stars in each headline.
  1783. When nil, the entire headline is fontified.
  1784. Changing it requires restart of `font-lock-mode' to become effective
  1785. also in regions already fontified."
  1786. :group 'org-font-lock
  1787. :type 'boolean)
  1788. (defcustom org-hide-leading-stars nil
  1789. "Non-nil means, hide the first N-1 stars in a headline.
  1790. This works by using the face `org-hide' for these stars. This
  1791. face is white for a light background, and black for a dark
  1792. background. You may have to customize the face `org-hide' to
  1793. make this work.
  1794. Changing it requires restart of `font-lock-mode' to become effective
  1795. also in regions already fontified.
  1796. You may also set this on a per-file basis by adding one of the following
  1797. lines to the buffer:
  1798. #+STARTUP: hidestars
  1799. #+STARTUP: showstars"
  1800. :group 'org-font-lock
  1801. :type 'boolean)
  1802. (defcustom org-fontify-done-headline nil
  1803. "Non-nil means, change the face of a headline if it is marked DONE.
  1804. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1805. When this is non-nil, the headline after the keyword is set to the
  1806. `org-headline-done' as an additional indication."
  1807. :group 'org-font-lock
  1808. :type 'boolean)
  1809. (defcustom org-fontify-emphasized-text t
  1810. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1811. Changing this variable requires a restart of Emacs to take effect."
  1812. :group 'org-font-lock
  1813. :type 'boolean)
  1814. (defgroup org-faces nil
  1815. "Faces in Org-mode."
  1816. :tag "Org Faces"
  1817. :group 'org-font-lock)
  1818. (defun org-compatible-face (specs)
  1819. "Make a compatible face specification.
  1820. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  1821. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  1822. to the top of the list. The `min-colors' attribute will be removed from
  1823. any other entries, and any resulting duplicates will be removed entirely."
  1824. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  1825. (let (r e a)
  1826. (while (setq e (pop specs))
  1827. (cond
  1828. ((memq (car e) '(t default)) (push e r))
  1829. ((setq a (member '(min-colors 8) (car e)))
  1830. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  1831. (cdr e)))))
  1832. ((setq a (assq 'min-colors (car e)))
  1833. (setq e (cons (delq a (car e)) (cdr e)))
  1834. (or (assoc (car e) r) (push e r)))
  1835. (t (or (assoc (car e) r) (push e r)))))
  1836. (nreverse r))
  1837. specs))
  1838. (defface org-hide
  1839. '((((background light)) (:foreground "white"))
  1840. (((background dark)) (:foreground "black")))
  1841. "Face used to hide leading stars in headlines.
  1842. The forground color of this face should be equal to the background
  1843. color of the frame."
  1844. :group 'org-faces)
  1845. (defface org-level-1 ;; font-lock-function-name-face
  1846. (org-compatible-face
  1847. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  1848. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  1849. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  1850. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  1851. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  1852. (t (:bold t))))
  1853. "Face used for level 1 headlines."
  1854. :group 'org-faces)
  1855. (defface org-level-2 ;; font-lock-variable-name-face
  1856. (org-compatible-face
  1857. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  1858. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  1859. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  1860. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  1861. (t (:bold t))))
  1862. "Face used for level 2 headlines."
  1863. :group 'org-faces)
  1864. (defface org-level-3 ;; font-lock-keyword-face
  1865. (org-compatible-face
  1866. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  1867. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  1868. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  1869. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  1870. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  1871. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  1872. (t (:bold t))))
  1873. "Face used for level 3 headlines."
  1874. :group 'org-faces)
  1875. (defface org-level-4 ;; font-lock-comment-face
  1876. (org-compatible-face
  1877. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  1878. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  1879. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  1880. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  1881. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  1882. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  1883. (t (:bold t))))
  1884. "Face used for level 4 headlines."
  1885. :group 'org-faces)
  1886. (defface org-level-5 ;; font-lock-type-face
  1887. (org-compatible-face
  1888. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  1889. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  1890. (((class color) (min-colors 8)) (:foreground "green"))))
  1891. "Face used for level 5 headlines."
  1892. :group 'org-faces)
  1893. (defface org-level-6 ;; font-lock-constant-face
  1894. (org-compatible-face
  1895. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  1896. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  1897. (((class color) (min-colors 8)) (:foreground "magenta"))))
  1898. "Face used for level 6 headlines."
  1899. :group 'org-faces)
  1900. (defface org-level-7 ;; font-lock-builtin-face
  1901. (org-compatible-face
  1902. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  1903. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  1904. (((class color) (min-colors 8)) (:foreground "blue"))))
  1905. "Face used for level 7 headlines."
  1906. :group 'org-faces)
  1907. (defface org-level-8 ;; font-lock-string-face
  1908. (org-compatible-face
  1909. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  1910. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  1911. (((class color) (min-colors 8)) (:foreground "green"))))
  1912. "Face used for level 8 headlines."
  1913. :group 'org-faces)
  1914. (defface org-special-keyword ;; font-lock-string-face
  1915. (org-compatible-face
  1916. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  1917. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  1918. (t (:italic t))))
  1919. "Face used for special keywords."
  1920. :group 'org-faces)
  1921. (defface org-warning ;; font-lock-warning-face
  1922. (org-compatible-face
  1923. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  1924. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  1925. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  1926. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  1927. (t (:bold t))))
  1928. "Face for deadlines and TODO keywords."
  1929. :group 'org-faces)
  1930. (defface org-headline-done ;; font-lock-string-face
  1931. (org-compatible-face
  1932. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  1933. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  1934. (((class color) (min-colors 8) (background light)) (:bold nil))))
  1935. "Face used to indicate that a headline is DONE.
  1936. This face is only used if `org-fontify-done-headline' is set."
  1937. :group 'org-faces)
  1938. (defface org-link
  1939. '((((class color) (background light)) (:foreground "Purple" :underline t))
  1940. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  1941. (t (:underline t)))
  1942. "Face for links."
  1943. :group 'org-faces)
  1944. (defface org-date
  1945. '((((class color) (background light)) (:foreground "Purple" :underline t))
  1946. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  1947. (t (:underline t)))
  1948. "Face for links."
  1949. :group 'org-faces)
  1950. (defface org-tag
  1951. '((t (:bold t)))
  1952. "Face for tags."
  1953. :group 'org-faces)
  1954. (defface org-todo ;; font-lock-warning-face
  1955. (org-compatible-face
  1956. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  1957. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  1958. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  1959. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  1960. (t (:inverse-video t :bold t))))
  1961. "Face for TODO keywords."
  1962. :group 'org-faces)
  1963. (defface org-done ;; font-lock-type-face
  1964. (org-compatible-face
  1965. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  1966. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  1967. (((class color) (min-colors 8)) (:foreground "green"))
  1968. (t (:bold t))))
  1969. "Face used for DONE."
  1970. :group 'org-faces)
  1971. (defface org-table ;; font-lock-function-name-face
  1972. (org-compatible-face
  1973. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  1974. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  1975. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  1976. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  1977. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  1978. (((class color) (min-colors 8) (background dark)))))
  1979. "Face used for tables."
  1980. :group 'org-faces)
  1981. (defface org-formula
  1982. (org-compatible-face
  1983. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  1984. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  1985. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  1986. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  1987. (t (:bold t :italic t))))
  1988. "Face for formulas."
  1989. :group 'org-faces)
  1990. (defface org-scheduled-today
  1991. (org-compatible-face
  1992. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  1993. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  1994. (((class color) (min-colors 8)) (:foreground "green"))
  1995. (t (:bold t :italic t))))
  1996. "Face for items scheduled for a certain day."
  1997. :group 'org-faces)
  1998. (defface org-scheduled-previously
  1999. (org-compatible-face
  2000. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2001. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2002. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2003. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2004. (t (:bold t))))
  2005. "Face for items scheduled previously, and not yet done."
  2006. :group 'org-faces)
  2007. (defface org-time-grid ;; font-lock-variable-name-face
  2008. (org-compatible-face
  2009. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2010. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2011. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2012. "Face used for time grids."
  2013. :group 'org-faces)
  2014. (defconst org-level-faces
  2015. '(org-level-1 org-level-2 org-level-3 org-level-4
  2016. org-level-5 org-level-6 org-level-7 org-level-8
  2017. ))
  2018. (defconst org-n-levels (length org-level-faces))
  2019. (defconst org-bold-re
  2020. (if (featurep 'xemacs)
  2021. "\\([ ]\\|^\\)\\(\\*\\(\\w[a-zA-Z0-9-_ ]*?\\w\\)\\*\\)\\([ ,.]\\|$\\)"
  2022. "\\([ ]\\|^\\)\\(\\*\\(\\w[[:word:] -_]*?\\w\\)\\*\\)\\([ ,.]\\|$\\)")
  2023. "Regular expression for bold emphasis.")
  2024. (defconst org-italic-re
  2025. (if (featurep 'xemacs)
  2026. "\\([ ]\\|^\\)\\(/\\(\\w[a-zA-Z0-9-_ ]*?\\w\\)/\\)\\([ ,.]\\|$\\)"
  2027. "\\([ ]\\|^\\)\\(/\\(\\w[[:word:] -_]*?\\w\\)/\\)\\([ ,.]\\|$\\)")
  2028. "Regular expression for italic emphasis.")
  2029. (defconst org-underline-re
  2030. (if (featurep 'xemacs)
  2031. "\\([ ]\\|^\\)\\(_\\(\\w[a-zA-Z0-9-_ ]*?\\w\\)_\\)\\([ ,.]\\|$\\)"
  2032. "\\([ ]\\|^\\)\\(_\\(\\w[[:word:] -_]*?\\w\\)_\\)\\([ ,.]\\|$\\)")
  2033. "Regular expression for underline emphasis.")
  2034. ;; Variables for pre-computed regular expressions, all buffer local
  2035. (defvar org-done-string nil
  2036. "The last string in `org-todo-keywords', indicating an item is DONE.")
  2037. (make-variable-buffer-local 'org-done-string)
  2038. (defvar org-todo-regexp nil
  2039. "Matches any of the TODO state keywords.")
  2040. (make-variable-buffer-local 'org-todo-regexp)
  2041. (defvar org-not-done-regexp nil
  2042. "Matches any of the TODO state keywords except the last one.")
  2043. (make-variable-buffer-local 'org-not-done-regexp)
  2044. (defvar org-todo-line-regexp nil
  2045. "Matches a headline and puts TODO state into group 2 if present.")
  2046. (make-variable-buffer-local 'org-todo-line-regexp)
  2047. (defvar org-nl-done-regexp nil
  2048. "Matches newline followed by a headline with the DONE keyword.")
  2049. (make-variable-buffer-local 'org-nl-done-regexp)
  2050. (defvar org-looking-at-done-regexp nil
  2051. "Matches the DONE keyword a point.")
  2052. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2053. (defvar org-todo-kwd-priority-p nil
  2054. "Do TODO items have priorities?")
  2055. (make-variable-buffer-local 'org-todo-kwd-priority-p)
  2056. (defvar org-todo-kwd-max-priority nil
  2057. "Maximum priority of TODO items.")
  2058. (make-variable-buffer-local 'org-todo-kwd-max-priority)
  2059. (defvar org-ds-keyword-length 12
  2060. "Maximum length of the Deadline and SCHEDULED keywords.")
  2061. (make-variable-buffer-local 'org-ds-keyword-length)
  2062. (defvar org-deadline-regexp nil
  2063. "Matches the DEADLINE keyword.")
  2064. (make-variable-buffer-local 'org-deadline-regexp)
  2065. (defvar org-deadline-time-regexp nil
  2066. "Matches the DEADLINE keyword together with a time stamp.")
  2067. (make-variable-buffer-local 'org-deadline-time-regexp)
  2068. (defvar org-deadline-line-regexp nil
  2069. "Matches the DEADLINE keyword and the rest of the line.")
  2070. (make-variable-buffer-local 'org-deadline-line-regexp)
  2071. (defvar org-scheduled-regexp nil
  2072. "Matches the SCHEDULED keyword.")
  2073. (make-variable-buffer-local 'org-scheduled-regexp)
  2074. (defvar org-scheduled-time-regexp nil
  2075. "Matches the SCHEDULED keyword together with a time stamp.")
  2076. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2077. (defvar org-closed-time-regexp nil
  2078. "Matches the CLOSED keyword together with a time stamp.")
  2079. (make-variable-buffer-local 'org-closed-time-regexp)
  2080. (defvar org-keyword-time-regexp nil
  2081. "Matches any of the 3 keywords, together with the time stamp.")
  2082. (make-variable-buffer-local 'org-keyword-time-regexp)
  2083. (defvar org-maybe-keyword-time-regexp nil
  2084. "Matches a timestamp, possibly preceeded by a keyword.")
  2085. (make-variable-buffer-local 'org-keyword-time-regexp)
  2086. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  2087. mouse-map t)
  2088. "Properties to remove when a string without properties is wanted.")
  2089. (defsubst org-match-string-no-properties (num &optional string)
  2090. (if (featurep 'xemacs)
  2091. (let ((s (match-string num string)))
  2092. (remove-text-properties 0 (length s) org-rm-props s)
  2093. s)
  2094. (match-string-no-properties num string)))
  2095. (defsubst org-no-properties (s)
  2096. (remove-text-properties 0 (length s) org-rm-props s)
  2097. s)
  2098. (defun org-set-regexps-and-options ()
  2099. "Precompute regular expressions for current buffer."
  2100. (when (eq major-mode 'org-mode)
  2101. (let ((re (org-make-options-regexp
  2102. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  2103. "STARTUP" "ARCHIVE" "TAGS")))
  2104. (splitre "[ \t]+")
  2105. kwds int key value cat arch tags)
  2106. (save-excursion
  2107. (save-restriction
  2108. (widen)
  2109. (goto-char (point-min))
  2110. (while (re-search-forward re nil t)
  2111. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2112. (cond
  2113. ((equal key "CATEGORY")
  2114. (if (string-match "[ \t]+$" value)
  2115. (setq value (replace-match "" t t value)))
  2116. (setq cat (intern value)))
  2117. ((equal key "SEQ_TODO")
  2118. (setq int 'sequence
  2119. kwds (append kwds (org-split-string value splitre))))
  2120. ((equal key "PRI_TODO")
  2121. (setq int 'priority
  2122. kwds (append kwds (org-split-string value splitre))))
  2123. ((equal key "TYP_TODO")
  2124. (setq int 'type
  2125. kwds (append kwds (org-split-string value splitre))))
  2126. ((equal key "TAGS")
  2127. (setq tags (append tags (org-split-string value splitre))))
  2128. ((equal key "STARTUP")
  2129. (let ((opts (org-split-string value splitre))
  2130. (set '(("fold" org-startup-folded t)
  2131. ("overview" org-startup-folded t)
  2132. ("nofold" org-startup-folded nil)
  2133. ("showall" org-startup-folded nil)
  2134. ("content" org-startup-folded content)
  2135. ("hidestars" org-hide-leading-stars t)
  2136. ("showstars" org-hide-leading-stars nil)
  2137. ("odd" org-odd-levels-only t)
  2138. ("oddeven" org-odd-levels-only nil)
  2139. ("align" org-startup-align-all-tables t)
  2140. ("noalign" org-startup-align-all-tables nil)
  2141. ("logging" org-log-done t)
  2142. ("nologging" org-log-done nil)
  2143. ("dlcheck" org-startup-with-deadline-check t)
  2144. ("nodlcheck" org-startup-with-deadline-check nil)))
  2145. l var val)
  2146. (while (setq l (assoc (pop opts) set))
  2147. (setq var (nth 1 l) val (nth 2 l))
  2148. (set (make-local-variable var) val))))
  2149. ((equal key "ARCHIVE")
  2150. (string-match " *$" value)
  2151. (setq arch (replace-match "" t t value))
  2152. (remove-text-properties 0 (length arch)
  2153. '(face t fontified t) arch)))
  2154. )))
  2155. (and cat (set (make-local-variable 'org-category) cat))
  2156. (and kwds (set (make-local-variable 'org-todo-keywords) kwds))
  2157. (and arch (set (make-local-variable 'org-archive-location) arch))
  2158. (and int (set (make-local-variable 'org-todo-interpretation) int))
  2159. (when tags
  2160. (let (e tg c tgs)
  2161. (while (setq e (pop tags))
  2162. (if (string-match "^\\([0-9a-zA-Z_@]+\\)(\\(.\\))$" e)
  2163. (push (cons (match-string 1 e)
  2164. (string-to-char (match-string 2 e)))
  2165. tgs)
  2166. (push (list e) tgs)))
  2167. (set (make-local-variable 'org-tag-alist) nil)
  2168. (while (setq e (pop tgs))
  2169. (or (assoc (car e) org-tag-alist)
  2170. (push e org-tag-alist))))))
  2171. ;; Compute the regular expressions and other local variables
  2172. (setq org-todo-kwd-priority-p (equal org-todo-interpretation 'priority)
  2173. org-todo-kwd-max-priority (1- (length org-todo-keywords))
  2174. org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2175. (length org-scheduled-string)))
  2176. org-done-string
  2177. (nth (1- (length org-todo-keywords)) org-todo-keywords)
  2178. org-todo-regexp
  2179. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
  2180. "\\|") "\\)\\>")
  2181. org-not-done-regexp
  2182. (concat "\\<\\("
  2183. (mapconcat 'regexp-quote
  2184. (nreverse (cdr (reverse org-todo-keywords)))
  2185. "\\|")
  2186. "\\)\\>")
  2187. org-todo-line-regexp
  2188. (concat "^\\(\\*+\\)[ \t]*\\("
  2189. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2190. "\\)? *\\(.*\\)")
  2191. org-nl-done-regexp
  2192. (concat "[\r\n]\\*+[ \t]+" org-done-string "\\>")
  2193. org-looking-at-done-regexp (concat "^" org-done-string "\\>")
  2194. org-deadline-regexp (concat "\\<" org-deadline-string)
  2195. org-deadline-time-regexp
  2196. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2197. org-deadline-line-regexp
  2198. (concat "\\<\\(" org-deadline-string "\\).*")
  2199. org-scheduled-regexp
  2200. (concat "\\<" org-scheduled-string)
  2201. org-scheduled-time-regexp
  2202. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2203. org-closed-time-regexp
  2204. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2205. org-keyword-time-regexp
  2206. (concat "\\<\\(" org-scheduled-string
  2207. "\\|" org-deadline-string
  2208. "\\|" org-closed-string "\\)"
  2209. " *[[<]\\([^]>]+\\)[]>]")
  2210. org-maybe-keyword-time-regexp
  2211. (concat "\\(\\<\\(" org-scheduled-string
  2212. "\\|" org-deadline-string
  2213. "\\|" org-closed-string "\\)\\)?"
  2214. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^]\r\n>]*?[]>]\\)"))
  2215. (org-set-font-lock-defaults)))
  2216. ;; Tell the compiler about dynamically scoped variables,
  2217. ;; and variables from other packages
  2218. (defvar calc-embedded-close-formula) ; defined by the calc package
  2219. (defvar calc-embedded-open-formula) ; defined by the calc package
  2220. (defvar font-lock-unfontify-region-function) ; defined by font-lock.el
  2221. (defvar zmacs-regions) ; XEmacs regions
  2222. (defvar original-date) ; dynamically scoped in calendar
  2223. (defvar org-old-auto-fill-inhibit-regexp) ; local variable used by `orgtbl-mode'
  2224. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  2225. (defvar org-html-entities) ; defined later in this file
  2226. (defvar org-goto-start-pos) ; dynamically scoped parameter
  2227. (defvar org-time-was-given) ; dynamically scoped parameter
  2228. (defvar org-ts-what) ; dynamically scoped parameter
  2229. (defvar mark-active) ; Emacs only, not available in XEmacs.
  2230. (defvar timecnt) ; dynamically scoped parameter
  2231. (defvar levels-open) ; dynamically scoped parameter
  2232. (defvar entry) ; dynamically scoped parameter
  2233. (defvar date) ; dynamically scoped parameter
  2234. (defvar description) ; dynamically scoped parameter
  2235. (defvar ans1) ; dynamically scoped parameter
  2236. (defvar ans2) ; dynamically scoped parameter
  2237. (defvar starting-day) ; local variable
  2238. (defvar include-all-loc) ; local variable
  2239. (defvar vm-message-pointer) ; from vm
  2240. (defvar vm-folder-directory) ; from vm
  2241. (defvar wl-summary-buffer-elmo-folder) ; from wanderlust
  2242. (defvar wl-summary-buffer-folder-name) ; from wanderlust
  2243. (defvar gnus-group-name) ; from gnus
  2244. (defvar gnus-article-current) ; from gnus
  2245. (defvar w3m-current-url) ; from w3m
  2246. (defvar mh-progs) ; from MH-E
  2247. (defvar mh-current-folder) ; from MH-E
  2248. (defvar mh-show-folder-buffer) ; from MH-E
  2249. (defvar mh-index-folder) ; from MH-E
  2250. (defvar mh-searcher) ; from MH-E
  2251. (defvar org-selected-point) ; dynamically scoped parameter
  2252. (defvar calendar-mode-map) ; from calendar.el
  2253. (defvar last-arg) ; local variable
  2254. (defvar remember-save-after-remembering) ; from remember.el
  2255. (defvar remember-data-file) ; from remember.el
  2256. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  2257. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  2258. (defvar orgtbl-mode) ; defined later in this file
  2259. (defvar Info-current-file) ; from info.el
  2260. (defvar Info-current-node) ; from info.el
  2261. ;;; Define the mode
  2262. (defvar org-mode-map
  2263. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2264. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or ugrade to newer allout, for example by switching to Emacs 22.")
  2265. (copy-keymap outline-mode-map))
  2266. "Keymap for Org-mode.")
  2267. (defvar org-struct-menu) ; defined later in this file
  2268. (defvar org-org-menu) ; defined later in this file
  2269. (defvar org-tbl-menu) ; defined later in this file
  2270. ;; We use a before-change function to check if a table might need
  2271. ;; an update.
  2272. (defvar org-table-may-need-update t
  2273. "Indicates that a table might need an update.
  2274. This variable is set by `org-before-change-function'.
  2275. `org-table-align' sets it back to nil.")
  2276. (defvar org-mode-hook nil)
  2277. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2278. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2279. ;;;###autoload
  2280. (define-derived-mode org-mode outline-mode "Org"
  2281. "Outline-based notes management and organizer, alias
  2282. \"Carsten's outline-mode for keeping track of everything.\"
  2283. Org-mode develops organizational tasks around a NOTES file which
  2284. contains information about projects as plain text. Org-mode is
  2285. implemented on top of outline-mode, which is ideal to keep the content
  2286. of large files well structured. It supports ToDo items, deadlines and
  2287. time stamps, which magically appear in the diary listing of the Emacs
  2288. calendar. Tables are easily created with a built-in table editor.
  2289. Plain text URL-like links connect to websites, emails (VM), Usenet
  2290. messages (Gnus), BBDB entries, and any files related to the project.
  2291. For printing and sharing of notes, an Org-mode file (or a part of it)
  2292. can be exported as a structured ASCII or HTML file.
  2293. The following commands are available:
  2294. \\{org-mode-map}"
  2295. ;; Get rid of Outline menus, they are not needed
  2296. ;; Need to do this here because define-derived-mode sets up
  2297. ;; the keymap so late.
  2298. (if (featurep 'xemacs)
  2299. (if org-noutline-p
  2300. (progn
  2301. (easy-menu-remove outline-mode-menu-heading)
  2302. (easy-menu-remove outline-mode-menu-show)
  2303. (easy-menu-remove outline-mode-menu-hide))
  2304. (delete-menu-item '("Headings"))
  2305. (delete-menu-item '("Show"))
  2306. (delete-menu-item '("Hide"))
  2307. (set-menubar-dirty-flag))
  2308. (define-key org-mode-map [menu-bar headings] 'undefined)
  2309. (define-key org-mode-map [menu-bar hide] 'undefined)
  2310. (define-key org-mode-map [menu-bar show] 'undefined))
  2311. (easy-menu-add org-org-menu)
  2312. (easy-menu-add org-tbl-menu)
  2313. (org-install-agenda-files-menu)
  2314. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2315. (org-add-to-invisibility-spec '(org-cwidth))
  2316. (when (featurep 'xemacs)
  2317. (set (make-local-variable 'line-move-ignore-invisible) t))
  2318. (setq outline-regexp "\\*+")
  2319. ;;(setq outline-regexp "\\(?:\\*+\\|[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\) \\)")
  2320. (setq outline-level 'org-outline-level)
  2321. (when (and org-ellipsis (stringp org-ellipsis))
  2322. (unless org-display-table
  2323. (setq org-display-table (make-display-table)))
  2324. (set-display-table-slot org-display-table
  2325. 4 (string-to-vector org-ellipsis))
  2326. (setq buffer-display-table org-display-table))
  2327. (org-set-regexps-and-options)
  2328. (if org-startup-truncated (setq truncate-lines t))
  2329. (set (make-local-variable 'font-lock-unfontify-region-function)
  2330. 'org-unfontify-region)
  2331. ;; Activate before-change-function
  2332. (set (make-local-variable 'org-table-may-need-update) t)
  2333. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2334. 'local)
  2335. ;; Paragraphs and auto-filling
  2336. (org-set-autofill-regexps)
  2337. (org-update-radio-target-regexp)
  2338. ;; Settings for Calc embedded mode
  2339. (set (make-local-variable 'calc-embedded-open-formula) "|\\|\n")
  2340. (set (make-local-variable 'calc-embedded-close-formula) "|\\|\n")
  2341. (if (and org-insert-mode-line-in-empty-file
  2342. (interactive-p)
  2343. (= (point-min) (point-max)))
  2344. (insert " -*- mode: org -*-\n\n"))
  2345. (unless org-inhibit-startup
  2346. (if org-startup-align-all-tables
  2347. (org-table-map-tables 'org-table-align))
  2348. (if org-startup-with-deadline-check
  2349. (call-interactively 'org-check-deadlines)
  2350. (cond
  2351. ((eq org-startup-folded t)
  2352. (org-cycle '(4)))
  2353. ((eq org-startup-folded 'content)
  2354. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2355. (org-cycle '(4)) (org-cycle '(4))))))))
  2356. (defsubst org-call-with-arg (command arg)
  2357. "Call COMMAND interactively, but pretend prefix are was ARG."
  2358. (let ((current-prefix-arg arg)) (call-interactively command)))
  2359. (defsubst org-current-line (&optional pos)
  2360. (+ (if (bolp) 1 0) (count-lines (point-min) (or pos (point)))))
  2361. (defun org-current-time ()
  2362. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2363. (if (> org-time-stamp-rounding-minutes 0)
  2364. (let ((r org-time-stamp-rounding-minutes)
  2365. (time (decode-time)))
  2366. (apply 'encode-time
  2367. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2368. (nthcdr 2 time))))
  2369. (current-time)))
  2370. (defun org-add-props (string plist &rest props)
  2371. "Add text properties to entire string, from beginning to end.
  2372. PLIST may be a list of properties, PROPS are individual properties and values
  2373. that will be added to PLIST. Returns the string that was modified."
  2374. (add-text-properties
  2375. 0 (length string) (if props (append plist props) plist) string)
  2376. string)
  2377. (put 'org-add-props 'lisp-indent-function 2)
  2378. ;;; Font-Lock stuff
  2379. (defvar org-mouse-map (make-sparse-keymap))
  2380. (define-key org-mouse-map
  2381. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2382. (define-key org-mouse-map
  2383. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2384. (when org-mouse-1-follows-link
  2385. (define-key org-mouse-map [follow-link] 'mouse-face))
  2386. (when org-tab-follows-link
  2387. (define-key org-mouse-map [(tab)] 'org-open-at-point)
  2388. (define-key org-mouse-map "\C-i" 'org-open-at-point))
  2389. (when org-return-follows-link
  2390. (define-key org-mouse-map [(return)] 'org-open-at-point)
  2391. (define-key org-mouse-map "\C-m" 'org-open-at-point))
  2392. (require 'font-lock)
  2393. (defconst org-non-link-chars "]\t\n\r<>")
  2394. (defconst org-link-types '("https?" "ftp" "mailto" "file" "news" "bbdb" "vm"
  2395. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  2396. (defconst org-link-re-with-space
  2397. (concat
  2398. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2399. "\\([^" org-non-link-chars " ]"
  2400. "[^" org-non-link-chars "]*"
  2401. "[^" org-non-link-chars " ]\\)>?")
  2402. "Matches a link with spaces, optional angular brackets around it.")
  2403. (defconst org-link-re-with-space2
  2404. (concat
  2405. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2406. "\\([^" org-non-link-chars " ]"
  2407. "[^]\t\n\r]*"
  2408. "[^" org-non-link-chars " ]\\)>?")
  2409. "Matches a link with spaces, optional angular brackets around it.")
  2410. (defconst org-angle-link-re
  2411. (concat
  2412. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2413. "\\([^" org-non-link-chars " ]"
  2414. "[^" org-non-link-chars "]*"
  2415. "\\)>")
  2416. "Matches link with angular brackets, spaces are allowed.")
  2417. (defconst org-plain-link-re
  2418. (concat
  2419. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2420. "\\([^]\t\n\r<>,;() ]+\\)")
  2421. "Matches plain link, without spaces.")
  2422. (defconst org-bracket-link-regexp
  2423. "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
  2424. "Matches a link in double brackets.")
  2425. (defconst org-bracket-link-analytic-regexp
  2426. (concat
  2427. "\\[\\["
  2428. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2429. "\\([^]]+\\)"
  2430. "\\]"
  2431. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2432. "\\]"))
  2433. ; 1: http:
  2434. ; 2: http
  2435. ; 3: path
  2436. ; 4: [desc]
  2437. ; 5: desc
  2438. (defconst org-ts-lengths
  2439. (cons (length (format-time-string (car org-time-stamp-formats)))
  2440. (length (format-time-string (cdr org-time-stamp-formats))))
  2441. "This holds the lengths of the two different time formats.")
  2442. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)>"
  2443. "Regular expression for fast time stamp matching.")
  2444. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)[]>]"
  2445. "Regular expression for fast time stamp matching.")
  2446. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2447. "Regular expression matching time strings for analysis.")
  2448. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
  2449. "Regular expression matching time stamps, with groups.")
  2450. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2451. "Regular expression matching a time stamp range.")
  2452. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2453. org-ts-regexp "\\)?")
  2454. "Regular expression matching a time stamp or time stamp range.")
  2455. (defun org-activate-plain-links (limit)
  2456. "Run through the buffer and add overlays to links."
  2457. (if (re-search-forward org-plain-link-re limit t)
  2458. (progn
  2459. (add-text-properties (match-beginning 0) (match-end 0)
  2460. (list 'mouse-face 'highlight
  2461. 'keymap org-mouse-map
  2462. ))
  2463. t)))
  2464. (defun org-activate-angle-links (limit)
  2465. "Run through the buffer and add overlays to links."
  2466. (if (re-search-forward org-angle-link-re limit t)
  2467. (progn
  2468. (add-text-properties (match-beginning 0) (match-end 0)
  2469. (list 'mouse-face 'highlight
  2470. 'keymap org-mouse-map
  2471. ))
  2472. t)))
  2473. (defun org-activate-bracket-links (limit)
  2474. "Run through the buffer and add overlays to bracketed links."
  2475. (if (re-search-forward org-bracket-link-regexp limit t)
  2476. (let* ((help (concat "LINK: "
  2477. (org-match-string-no-properties 1)))
  2478. ;; FIXME: above we should remove the escapes.
  2479. (ip (list 'invisible 'org-link 'intangible t 'rear-nonsticky t
  2480. 'keymap org-mouse-map 'mouse-face 'highlight
  2481. 'help-echo help))
  2482. (vp (list 'rear-nonsticky t
  2483. 'keymap org-mouse-map 'mouse-face 'highlight
  2484. 'help-echo help)))
  2485. ;; We need to remove the invisible property here. Table narrowing
  2486. ;; may have made some of this invisible.
  2487. (remove-text-properties (match-beginning 0) (match-end 0)
  2488. '(invisible nil))
  2489. (if (match-end 3)
  2490. (progn
  2491. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  2492. (add-text-properties (match-beginning 3) (match-end 3) vp)
  2493. (add-text-properties (match-end 3) (match-end 0) ip))
  2494. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  2495. (add-text-properties (match-beginning 1) (match-end 1) vp)
  2496. (add-text-properties (match-end 1) (match-end 0) ip))
  2497. t)))
  2498. (defun org-activate-dates (limit)
  2499. "Run through the buffer and add overlays to dates."
  2500. (if (re-search-forward org-tsr-regexp limit t)
  2501. (progn
  2502. (add-text-properties (match-beginning 0) (match-end 0)
  2503. (list 'mouse-face 'highlight
  2504. 'keymap org-mouse-map))
  2505. t)))
  2506. (defvar org-target-link-regexp nil
  2507. "Regular expression matching radio targets in plain text.")
  2508. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  2509. "Regular expression matching a link target.")
  2510. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  2511. "Regular expression matching a link target.")
  2512. (defun org-activate-target-links (limit)
  2513. "Run through the buffer and add overlays to target matches."
  2514. (when org-target-link-regexp
  2515. (let ((case-fold-search t))
  2516. (if (re-search-forward org-target-link-regexp limit t)
  2517. (progn
  2518. (add-text-properties (match-beginning 0) (match-end 0)
  2519. (list 'mouse-face 'highlight
  2520. 'keymap org-mouse-map
  2521. 'help-echo "Radio target link"
  2522. 'org-linked-text t))
  2523. t)))))
  2524. (defun org-update-radio-target-regexp ()
  2525. "Find all radio targets in this file and update the regular expression."
  2526. (interactive)
  2527. (when (memq 'radio org-activate-links)
  2528. (setq org-target-link-regexp
  2529. (org-make-target-link-regexp (org-all-targets 'radio)))
  2530. (org-restart-font-lock)))
  2531. (defun org-hide-wide-columns (limit)
  2532. (let (s e)
  2533. (setq s (text-property-any (point) (or limit (point-max))
  2534. 'org-cwidth t))
  2535. (when s
  2536. (setq e (next-single-property-change s 'org-cwidth))
  2537. (add-text-properties s e '(invisible org-cwidth intangible t))
  2538. (goto-char e)
  2539. t)))
  2540. (defun org-restart-font-lock ()
  2541. "Restart font-lock-mode, to force refontification."
  2542. (when (and (boundp 'font-lock-mode) font-lock-mode)
  2543. (font-lock-mode -1)
  2544. (font-lock-mode 1)))
  2545. (defun org-all-targets (&optional radio)
  2546. "Return a list of all targets in this file.
  2547. With optional argument RADIO, only find radio targets."
  2548. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  2549. rtn)
  2550. (save-excursion
  2551. (goto-char (point-min))
  2552. (while (re-search-forward re nil t)
  2553. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  2554. rtn)))
  2555. (defun org-make-target-link-regexp (targets)
  2556. "Make regular expression matching all strings in TARGETS.
  2557. The regular expression finds the targets also if there is a line break
  2558. between words."
  2559. (and targets
  2560. (concat
  2561. "\\<\\("
  2562. (mapconcat
  2563. (lambda (x)
  2564. (while (string-match " +" x)
  2565. (setq x (replace-match "\\s-+" t t x)))
  2566. x)
  2567. targets
  2568. "\\|")
  2569. "\\)\\>")))
  2570. (defvar org-camel-regexp "\\*?\\<[A-Z]+[a-z]+[A-Z][a-zA-Z]*\\>"
  2571. "Matches CamelCase words, possibly with a star before it.")
  2572. (defun org-activate-camels (limit)
  2573. "Run through the buffer and add overlays to dates."
  2574. (if (re-search-forward org-camel-regexp limit t)
  2575. (progn
  2576. (add-text-properties (match-beginning 0) (match-end 0)
  2577. (list 'mouse-face 'highlight
  2578. 'keymap org-mouse-map))
  2579. t)))
  2580. (defun org-activate-tags (limit)
  2581. (if (re-search-forward "[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \r\n]" limit t)
  2582. (progn
  2583. (add-text-properties (match-beginning 1) (match-end 1)
  2584. (list 'mouse-face 'highlight
  2585. 'keymap org-mouse-map))
  2586. t)))
  2587. (defun org-font-lock-level ()
  2588. (save-excursion
  2589. (org-back-to-heading t)
  2590. (- (match-end 0) (match-beginning 0))))
  2591. (defun org-outline-level ()
  2592. (save-excursion
  2593. (looking-at outline-regexp)
  2594. (if (match-beginning 1)
  2595. (+ (org-get-string-indentation (match-string 1)) 1000)
  2596. (- (match-end 0) (match-beginning 0)))))
  2597. (defvar org-font-lock-keywords nil)
  2598. (defun org-set-font-lock-defaults ()
  2599. (let* ((em org-fontify-emphasized-text)
  2600. (lk org-activate-links)
  2601. (org-font-lock-extra-keywords
  2602. ;; Headlines
  2603. (list
  2604. '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
  2605. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  2606. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  2607. (1 'org-table))
  2608. ;; Links
  2609. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  2610. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  2611. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  2612. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  2613. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  2614. (if (memq 'camel lk) '(org-activate-camels (0 'org-link t)))
  2615. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  2616. (if org-table-limit-column-width
  2617. '(org-hide-wide-columns (0 nil append)))
  2618. ;; TODO lines
  2619. (list (concat "^\\*+[ \t]*" org-not-done-regexp)
  2620. '(1 'org-todo t))
  2621. ;; Priorities
  2622. (list (concat "\\[#[A-Z]\\]") '(0 'org-special-keyword t))
  2623. ;; Special keywords
  2624. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  2625. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  2626. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  2627. ;; Emphasis
  2628. (if em (list org-bold-re 2 ''bold 'prepend))
  2629. (if em (list org-italic-re 2 ''italic 'prepend))
  2630. (if em (list org-underline-re 2 ''underline 'prepend))
  2631. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  2632. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
  2633. 2 'bold prepend)
  2634. ;; COMMENT
  2635. (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
  2636. "\\|" org-quote-string "\\)\\>")
  2637. '(1 'org-special-keyword t))
  2638. '("^#.*" (0 'font-lock-comment-face t))
  2639. ;; DONE
  2640. (if org-fontify-done-headline
  2641. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
  2642. '(1 'org-done t) '(2 'org-headline-done t))
  2643. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
  2644. '(1 'org-done t)))
  2645. ;; Table stuff
  2646. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  2647. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  2648. '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
  2649. (if org-format-transports-properties-p
  2650. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  2651. )))
  2652. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  2653. ;; Now set the full font-lock-keywords
  2654. (set (make-local-variable 'org-font-lock-keywords)
  2655. org-font-lock-extra-keywords)
  2656. (set (make-local-variable 'font-lock-defaults)
  2657. '(org-font-lock-keywords t nil nil backward-paragraph))
  2658. (kill-local-variable 'font-lock-keywords) nil))
  2659. (defvar org-m nil)
  2660. (defvar org-l nil)
  2661. (defvar org-f nil)
  2662. (defun org-get-level-face (n)
  2663. "Get the right face for match N in font-lock matching of healdines."
  2664. (setq org-l (- (match-end 2) (match-beginning 1)))
  2665. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  2666. ; (setq org-f (nth (1- (% org-l org-n-levels)) org-level-faces))
  2667. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  2668. (cond
  2669. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  2670. ((eq n 2) org-f)
  2671. (t (if org-level-color-stars-only nil org-f))))
  2672. (defun org-unfontify-region (beg end &optional maybe_loudly)
  2673. "Remove fontification and activation overlays from links."
  2674. (font-lock-default-unfontify-region beg end)
  2675. (let* ((buffer-undo-list t)
  2676. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  2677. (inhibit-modification-hooks t)
  2678. deactivate-mark buffer-file-name buffer-file-truename)
  2679. (remove-text-properties beg end
  2680. '(mouse-face nil keymap nil org-linked-text nil
  2681. invisible nil intangible nil))))
  2682. ;;; Visibility cycling
  2683. (defvar org-cycle-global-status nil)
  2684. (make-variable-buffer-local 'org-cycle-global-status)
  2685. (defvar org-cycle-subtree-status nil)
  2686. (make-variable-buffer-local 'org-cycle-subtree-status)
  2687. ;;;###autoload
  2688. (defun org-cycle (&optional arg)
  2689. "Visibility cycling for Org-mode.
  2690. - When this function is called with a prefix argument, rotate the entire
  2691. buffer through 3 states (global cycling)
  2692. 1. OVERVIEW: Show only top-level headlines.
  2693. 2. CONTENTS: Show all headlines of all levels, but no body text.
  2694. 3. SHOW ALL: Show everything.
  2695. - When point is at the beginning of a headline, rotate the subtree started
  2696. by this line through 3 different states (local cycling)
  2697. 1. FOLDED: Only the main headline is shown.
  2698. 2. CHILDREN: The main headline and the direct children are shown.
  2699. From this state, you can move to one of the children
  2700. and zoom in further.
  2701. 3. SUBTREE: Show the entire subtree, including body text.
  2702. - When there is a numeric prefix, go up to a heading with level ARG, do
  2703. a `show-subtree' and return to the previous cursor position. If ARG
  2704. is negative, go up that many levels.
  2705. - When point is not at the beginning of a headline, execute
  2706. `indent-relative', like TAB normally does. See the option
  2707. `org-cycle-emulate-tab' for details.
  2708. - Special case: if point is the the beginning of the buffer and there is
  2709. no headline in line 1, this function will act as if called with prefix arg."
  2710. (interactive "P")
  2711. (let* ((outline-regexp
  2712. (if org-cycle-include-plain-lists
  2713. "\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) "
  2714. outline-regexp))
  2715. (bob-special (and org-cycle-global-at-bob (bobp)
  2716. (not (looking-at outline-regexp))))
  2717. (org-cycle-hook (if bob-special nil org-cycle-hook)))
  2718. (if (or bob-special (equal arg '(4)))
  2719. ;; special case: use global cycling
  2720. (setq arg t))
  2721. (cond
  2722. ((org-at-table-p 'any)
  2723. ;; Enter the table or move to the next field in the table
  2724. (or (org-table-recognize-table.el)
  2725. (progn
  2726. (if arg (org-table-edit-field t)
  2727. (org-table-justify-field-maybe)
  2728. (call-interactively 'org-table-next-field)))))
  2729. ((eq arg t) ;; Global cycling
  2730. (cond
  2731. ((and (eq last-command this-command)
  2732. (eq org-cycle-global-status 'overview))
  2733. ;; We just created the overview - now do table of contents
  2734. ;; This can be slow in very large buffers, so indicate action
  2735. (message "CONTENTS...")
  2736. (org-content)
  2737. (message "CONTENTS...done")
  2738. (setq org-cycle-global-status 'contents)
  2739. (run-hook-with-args 'org-cycle-hook 'contents))
  2740. ((and (eq last-command this-command)
  2741. (eq org-cycle-global-status 'contents))
  2742. ;; We just showed the table of contents - now show everything
  2743. (show-all)
  2744. (message "SHOW ALL")
  2745. (setq org-cycle-global-status 'all)
  2746. (run-hook-with-args 'org-cycle-hook 'all))
  2747. (t
  2748. ;; Default action: go to overview
  2749. (org-overview)
  2750. (message "OVERVIEW")
  2751. (setq org-cycle-global-status 'overview)
  2752. (run-hook-with-args 'org-cycle-hook 'overview))))
  2753. ((integerp arg)
  2754. ;; Show-subtree, ARG levels up from here.
  2755. (save-excursion
  2756. (org-back-to-heading)
  2757. (outline-up-heading (if (< arg 0) (- arg)
  2758. (- (funcall outline-level) arg)))
  2759. (org-show-subtree)))
  2760. ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  2761. ;; At a heading: rotate between three different views
  2762. (org-back-to-heading)
  2763. (let ((goal-column 0) eoh eol eos)
  2764. ;; First, some boundaries
  2765. (save-excursion
  2766. (org-back-to-heading)
  2767. (save-excursion
  2768. (beginning-of-line 2)
  2769. (while (and (not (eobp)) ;; this is like `next-line'
  2770. (get-char-property (1- (point)) 'invisible))
  2771. (beginning-of-line 2)) (setq eol (point)))
  2772. (outline-end-of-heading) (setq eoh (point))
  2773. (org-end-of-subtree t) (setq eos (point))
  2774. (outline-next-heading))
  2775. ;; Find out what to do next and set `this-command'
  2776. (cond
  2777. ((and (= eos eoh)
  2778. ;; Nothing is hidden behind this heading
  2779. (message "EMPTY ENTRY")
  2780. (setq org-cycle-subtree-status nil)))
  2781. ((>= eol eos)
  2782. ;; Entire subtree is hidden in one line: open it
  2783. (org-show-entry)
  2784. (show-children)
  2785. (message "CHILDREN")
  2786. (setq org-cycle-subtree-status 'children)
  2787. (run-hook-with-args 'org-cycle-hook 'children))
  2788. ((and (eq last-command this-command)
  2789. (eq org-cycle-subtree-status 'children))
  2790. ;; We just showed the children, now show everything.
  2791. (org-show-subtree)
  2792. (message "SUBTREE")
  2793. (setq org-cycle-subtree-status 'subtree)
  2794. (run-hook-with-args 'org-cycle-hook 'subtree))
  2795. (t
  2796. ;; Default action: hide the subtree.
  2797. (hide-subtree)
  2798. (message "FOLDED")
  2799. (setq org-cycle-subtree-status 'folded)
  2800. (run-hook-with-args 'org-cycle-hook 'folded)))))
  2801. ;; TAB emulation
  2802. (buffer-read-only (org-back-to-heading))
  2803. ((if (and (eq org-cycle-emulate-tab 'white)
  2804. (save-excursion (beginning-of-line 1) (looking-at "[ \t]+$")))
  2805. t
  2806. (eq org-cycle-emulate-tab t))
  2807. (if (and (looking-at "[ \n\r\t]")
  2808. (string-match "^[ \t]*$" (buffer-substring
  2809. (point-at-bol) (point))))
  2810. (progn
  2811. (beginning-of-line 1)
  2812. (and (looking-at "[ \t]+") (replace-match ""))))
  2813. (indent-relative))
  2814. (t (save-excursion
  2815. (org-back-to-heading)
  2816. (org-cycle))))))
  2817. ;;;###autoload
  2818. (defun org-global-cycle ()
  2819. "Cycle the global visibility. For details see `org-cycle'."
  2820. (interactive)
  2821. (org-cycle '(4)))
  2822. (defun org-overview ()
  2823. "Switch to overview mode, shoing only top-level headlines.
  2824. Really, this shows all headlines with level equal or greater than the level
  2825. of the first headline in the buffer. This is important, because if the
  2826. first headline is not level one, then (hide-sublevels 1) gives confusing
  2827. results."
  2828. (interactive)
  2829. (hide-sublevels (save-excursion
  2830. (goto-char (point-min))
  2831. (if (re-search-forward (concat "^" outline-regexp) nil t)
  2832. (progn
  2833. (goto-char (match-beginning 0))
  2834. (funcall outline-level))
  2835. 1))))
  2836. ;; FIXME: allow an argument to give a limiting level for this.
  2837. (defun org-content ()
  2838. "Show all headlines in the buffer, like a table of contents"
  2839. (interactive)
  2840. (save-excursion
  2841. ;; Visit all headings and show their offspring
  2842. (goto-char (point-max))
  2843. (catch 'exit
  2844. (while (and (progn (condition-case nil
  2845. (outline-previous-visible-heading 1)
  2846. (error (goto-char (point-min))))
  2847. t)
  2848. (looking-at outline-regexp))
  2849. (show-branches)
  2850. (if (bobp) (throw 'exit nil))))))
  2851. (defun org-optimize-window-after-visibility-change (state)
  2852. "Adjust the window after a change in outline visibility.
  2853. This function is the default value of the hook `org-cycle-hook'."
  2854. (when (get-buffer-window (current-buffer))
  2855. (cond
  2856. ((eq state 'overview) (org-first-headline-recenter 1))
  2857. ((eq state 'content) nil)
  2858. ((eq state 'all) nil)
  2859. ((eq state 'folded) nil)
  2860. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  2861. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  2862. (defun org-subtree-end-visible-p ()
  2863. "Is the end of the current subtree visible?"
  2864. (pos-visible-in-window-p
  2865. (save-excursion (org-end-of-subtree t) (point))))
  2866. (defun org-first-headline-recenter (&optional N)
  2867. "Move cursor to the first headline and recenter the headline.
  2868. Optional argument N means, put the headline into the Nth line of the window."
  2869. (goto-char (point-min))
  2870. (when (re-search-forward (concat "^" outline-regexp) nil t)
  2871. (beginning-of-line)
  2872. (recenter (prefix-numeric-value N))))
  2873. (defvar org-goto-window-configuration nil)
  2874. (defvar org-goto-marker nil)
  2875. (defvar org-goto-map (make-sparse-keymap))
  2876. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  2877. (while (setq cmd (pop cmds))
  2878. (substitute-key-definition cmd cmd org-goto-map global-map)))
  2879. (define-key org-goto-map "\C-m" 'org-goto-ret)
  2880. (define-key org-goto-map [(left)] 'org-goto-left)
  2881. (define-key org-goto-map [(right)] 'org-goto-right)
  2882. (define-key org-goto-map [(?q)] 'org-goto-quit)
  2883. (define-key org-goto-map [(control ?g)] 'org-goto-quit)
  2884. (define-key org-goto-map "\C-i" 'org-cycle)
  2885. (define-key org-goto-map [(tab)] 'org-cycle)
  2886. (define-key org-goto-map [(down)] 'outline-next-visible-heading)
  2887. (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
  2888. (define-key org-goto-map "n" 'outline-next-visible-heading)
  2889. (define-key org-goto-map "p" 'outline-previous-visible-heading)
  2890. (define-key org-goto-map "f" 'outline-forward-same-level)
  2891. (define-key org-goto-map "b" 'outline-backward-same-level)
  2892. (define-key org-goto-map "u" 'outline-up-heading)
  2893. (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  2894. (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  2895. (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  2896. (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  2897. (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
  2898. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  2899. (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
  2900. (defconst org-goto-help
  2901. "Select a location to jump to, press RET
  2902. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  2903. (defun org-goto ()
  2904. "Go to a different location of the document, keeping current visibility.
  2905. When you want to go to a different location in a document, the fastest way
  2906. is often to fold the entire buffer and then dive into the tree. This
  2907. method has the disadvantage, that the previous location will be folded,
  2908. which may not be what you want.
  2909. This command works around this by showing a copy of the current buffer in
  2910. overview mode. You can dive into the tree in that copy, to find the
  2911. location you want to reach. When pressing RET, the command returns to the
  2912. original buffer in which the visibility is still unchanged. It then jumps
  2913. to the new location, making it and the headline hierarchy above it visible."
  2914. (interactive)
  2915. (let* ((org-goto-start-pos (point))
  2916. (selected-point
  2917. (org-get-location (current-buffer) org-goto-help)))
  2918. (if selected-point
  2919. (progn
  2920. (org-mark-ring-push org-goto-start-pos)
  2921. (goto-char selected-point)
  2922. (if (or (org-invisible-p) (org-invisible-p2))
  2923. (org-show-hierarchy-above)))
  2924. (error "Quit"))))
  2925. (defun org-get-location (buf help)
  2926. "Let the user select a location in the Org-mode buffer BUF.
  2927. This function uses a recursive edit. It returns the selected position
  2928. or nil."
  2929. (let (org-selected-point)
  2930. (save-excursion
  2931. (save-window-excursion
  2932. (delete-other-windows)
  2933. (switch-to-buffer (get-buffer-create "*org-goto*"))
  2934. (with-output-to-temp-buffer "*Help*"
  2935. (princ help))
  2936. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  2937. (setq buffer-read-only nil)
  2938. (erase-buffer)
  2939. (insert-buffer-substring buf)
  2940. (let ((org-startup-truncated t)
  2941. (org-startup-folded t)
  2942. (org-startup-align-all-tables nil)
  2943. (org-startup-with-deadline-check nil))
  2944. (org-mode))
  2945. (setq buffer-read-only t)
  2946. (if (boundp 'org-goto-start-pos)
  2947. (goto-char org-goto-start-pos)
  2948. (goto-char (point-min)))
  2949. (org-beginning-of-line)
  2950. (message "Select location and press RET")
  2951. ;; now we make sure that during selection, ony very few keys work
  2952. ;; and that it is impossible to switch to another window.
  2953. (let ((gm (current-global-map))
  2954. (overriding-local-map org-goto-map))
  2955. (unwind-protect
  2956. (progn
  2957. (use-global-map org-goto-map)
  2958. (recursive-edit))
  2959. (use-global-map gm)))))
  2960. (kill-buffer "*org-goto*")
  2961. org-selected-point))
  2962. (defun org-goto-ret (&optional arg)
  2963. "Finish `org-goto' by going to the new location."
  2964. (interactive "P")
  2965. (setq org-selected-point (point)
  2966. current-prefix-arg arg)
  2967. (throw 'exit nil))
  2968. (defun org-goto-left ()
  2969. "Finish `org-goto' by going to the new location."
  2970. (interactive)
  2971. (if (org-on-heading-p)
  2972. (progn
  2973. (beginning-of-line 1)
  2974. (setq org-selected-point (point)
  2975. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  2976. (throw 'exit nil))
  2977. (error "Not on a heading")))
  2978. (defun org-goto-right ()
  2979. "Finish `org-goto' by going to the new location."
  2980. (interactive)
  2981. (if (org-on-heading-p)
  2982. (progn
  2983. (outline-end-of-subtree)
  2984. (or (eobp) (forward-char 1))
  2985. (setq org-selected-point (point)
  2986. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  2987. (throw 'exit nil))
  2988. (error "Not on a heading")))
  2989. (defun org-goto-quit ()
  2990. "Finish `org-goto' without cursor motion."
  2991. (interactive)
  2992. (setq org-selected-point nil)
  2993. (throw 'exit nil))
  2994. ;;; Promotion, Demotion, Inserting new headlines
  2995. (defvar org-ignore-region nil
  2996. "To temporarily disable the active region.")
  2997. (defun org-insert-heading (&optional force-heading)
  2998. "Insert a new heading or item with same depth at point.
  2999. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3000. If point is at the beginning of a headline, insert a sibling before the
  3001. current headline. If point is in the middle of a headline, split the headline
  3002. at that position and make the rest of the headline part of the sibling below
  3003. the current headline."
  3004. (interactive "P")
  3005. (if (= (buffer-size) 0)
  3006. (insert "\n* ")
  3007. (when (or force-heading (not (org-insert-item)))
  3008. (let* ((head (save-excursion
  3009. (condition-case nil
  3010. (progn
  3011. (org-back-to-heading)
  3012. (match-string 0))
  3013. (error "*"))))
  3014. pos)
  3015. (cond
  3016. ((and (org-on-heading-p) (bolp)
  3017. (save-excursion (backward-char 1) (not (org-invisible-p))))
  3018. (open-line 1))
  3019. ((bolp) nil)
  3020. (t (newline)))
  3021. (insert head) (just-one-space)
  3022. (setq pos (point))
  3023. (end-of-line 1)
  3024. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3025. (run-hooks 'org-insert-heading-hook)))))
  3026. (defun org-insert-item (&optional checkbox)
  3027. "Insert a new item at the current level.
  3028. Return t when things worked, nil when we are not in an item."
  3029. (when (save-excursion
  3030. (condition-case nil
  3031. (progn
  3032. (org-beginning-of-item)
  3033. (org-at-item-p)
  3034. t)
  3035. (error nil)))
  3036. (let* ((bul (match-string 0))
  3037. (end (match-end 0))
  3038. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  3039. (match-end 0)))
  3040. (eowcol (save-excursion (goto-char eow) (current-column)))
  3041. pos)
  3042. (cond
  3043. ((and (org-at-item-p) (<= (point) eow))
  3044. ;; before the bullet
  3045. (beginning-of-line 1)
  3046. (open-line 1))
  3047. ((<= (point) eow)
  3048. (beginning-of-line 1))
  3049. (t (newline)))
  3050. (insert bul (if checkbox "[ ]" ""))
  3051. (just-one-space)
  3052. (setq pos (point))
  3053. (end-of-line 1)
  3054. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  3055. (org-maybe-renumber-ordered-list)
  3056. t))
  3057. (defun org-insert-todo-heading (arg)
  3058. "Insert a new heading with the same level and TODO state as current heading.
  3059. If the heading has no TODO state, or if the state is DONE, use the first
  3060. state (TODO by default). Also with prefix arg, force first state."
  3061. (interactive "P")
  3062. (when (not (org-insert-item 'checkbox))
  3063. (org-insert-heading)
  3064. (save-excursion
  3065. (org-back-to-heading)
  3066. (if org-noutline-p
  3067. (outline-previous-heading)
  3068. (outline-previous-visible-heading t))
  3069. (looking-at org-todo-line-regexp))
  3070. (if (or arg
  3071. (not (match-beginning 2))
  3072. (equal (match-string 2) org-done-string))
  3073. (insert (car org-todo-keywords) " ")
  3074. (insert (match-string 2) " "))))
  3075. (defun org-promote-subtree ()
  3076. "Promote the entire subtree.
  3077. See also `org-promote'."
  3078. (interactive)
  3079. (save-excursion
  3080. (org-map-tree 'org-promote)))
  3081. (defun org-demote-subtree ()
  3082. "Demote the entire subtree. See `org-demote'.
  3083. See also `org-promote'."
  3084. (interactive)
  3085. (save-excursion
  3086. (org-map-tree 'org-demote)))
  3087. (defun org-do-promote ()
  3088. "Promote the current heading higher up the tree.
  3089. If the region is active in `transient-mark-mode', promote all headings
  3090. in the region."
  3091. (interactive)
  3092. (save-excursion
  3093. (if (org-region-active-p)
  3094. (org-map-region 'org-promote (region-beginning) (region-end))
  3095. (org-promote)))
  3096. (org-fix-position-after-promote))
  3097. (defun org-do-demote ()
  3098. "Demote the current heading lower down the tree.
  3099. If the region is active in `transient-mark-mode', demote all headings
  3100. in the region."
  3101. (interactive)
  3102. (save-excursion
  3103. (if (org-region-active-p)
  3104. (org-map-region 'org-demote (region-beginning) (region-end))
  3105. (org-demote)))
  3106. (org-fix-position-after-promote))
  3107. (defun org-fix-position-after-promote ()
  3108. "Make sure that after pro/demotion cursor position is right."
  3109. (and (equal (char-after) ?\ )
  3110. (equal (char-before) ?*)
  3111. (forward-char 1)))
  3112. (defun org-get-legal-level (level change)
  3113. "Rectify a level change under the influence of `org-odd-levels-only'
  3114. LEVEL is a current level, CHANGE is by how much the level should be
  3115. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  3116. even level numbers will become the next higher odd number."
  3117. (if org-odd-levels-only
  3118. (cond ((not change) (1+ (* 2 (/ level 2))))
  3119. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  3120. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  3121. (max 1 (+ level change))))
  3122. (defun org-promote ()
  3123. "Promote the current heading higher up the tree.
  3124. If the region is active in `transient-mark-mode', promote all headings
  3125. in the region."
  3126. (org-back-to-heading t)
  3127. (let* ((level (save-match-data (funcall outline-level)))
  3128. (up-head (make-string (org-get-legal-level level -1) ?*))
  3129. (diff (abs (- level (length up-head)))))
  3130. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover"))
  3131. (replace-match up-head nil t)
  3132. ;; Fixup tag positioning
  3133. (and org-auto-align-tags (org-set-tags nil t))
  3134. (if org-adapt-indentation
  3135. (org-fixup-indentation (if (> diff 1) "^ " "^ ") ""
  3136. (if (> diff 1) "^ ? ?\\S-" "^ ?\\S-")))))
  3137. (defun org-demote ()
  3138. "Demote the current heading lower down the tree.
  3139. If the region is active in `transient-mark-mode', demote all headings
  3140. in the region."
  3141. (org-back-to-heading t)
  3142. (let* ((level (save-match-data (funcall outline-level)))
  3143. (down-head (make-string (org-get-legal-level level 1) ?*))
  3144. (diff (abs (- level (length down-head)))))
  3145. (replace-match down-head nil t)
  3146. ;; Fixup tag positioning
  3147. (and org-auto-align-tags (org-set-tags nil t))
  3148. (if org-adapt-indentation
  3149. (org-fixup-indentation "^ " (if (> diff 1) " " " ") "^\\S-"))))
  3150. (defun org-map-tree (fun)
  3151. "Call FUN for every heading underneath the current one."
  3152. (org-back-to-heading)
  3153. (let ((level (funcall outline-level)))
  3154. (save-excursion
  3155. (funcall fun)
  3156. (while (and (progn
  3157. (outline-next-heading)
  3158. (> (funcall outline-level) level))
  3159. (not (eobp)))
  3160. (funcall fun)))))
  3161. (defun org-map-region (fun beg end)
  3162. "Call FUN for every heading between BEG and END."
  3163. (let ((org-ignore-region t))
  3164. (save-excursion
  3165. (setq end (copy-marker end))
  3166. (goto-char beg)
  3167. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  3168. (< (point) end))
  3169. (funcall fun))
  3170. (while (and (progn
  3171. (outline-next-heading)
  3172. (< (point) end))
  3173. (not (eobp)))
  3174. (funcall fun)))))
  3175. (defun org-fixup-indentation (from to prohibit)
  3176. "Change the indentation in the current entry by re-replacing FROM with TO.
  3177. However, if the regexp PROHIBIT matches at all, don't do anything.
  3178. This is being used to change indentation along with the length of the
  3179. heading marker. But if there are any lines which are not indented, nothing
  3180. is changed at all."
  3181. (save-excursion
  3182. (let ((end (save-excursion (outline-next-heading)
  3183. (point-marker))))
  3184. (unless (save-excursion (re-search-forward prohibit end t))
  3185. (while (re-search-forward from end t)
  3186. (replace-match to)
  3187. (beginning-of-line 2)))
  3188. (move-marker end nil))))
  3189. ;;; Vertical tree motion, cutting and pasting of subtrees
  3190. (defun org-move-subtree-up (&optional arg)
  3191. "Move the current subtree up past ARG headlines of the same level."
  3192. (interactive "p")
  3193. (org-move-subtree-down (- (prefix-numeric-value arg))))
  3194. (defun org-move-subtree-down (&optional arg)
  3195. "Move the current subtree down past ARG headlines of the same level."
  3196. (interactive "p")
  3197. (setq arg (prefix-numeric-value arg))
  3198. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  3199. 'outline-get-last-sibling))
  3200. (ins-point (make-marker))
  3201. (cnt (abs arg))
  3202. beg end txt folded)
  3203. ;; Select the tree
  3204. (org-back-to-heading)
  3205. (setq beg (point))
  3206. (save-match-data
  3207. (save-excursion (outline-end-of-heading)
  3208. (setq folded (org-invisible-p)))
  3209. (outline-end-of-subtree))
  3210. (outline-next-heading)
  3211. (setq end (point))
  3212. ;; Find insertion point, with error handling
  3213. (goto-char beg)
  3214. (while (> cnt 0)
  3215. (or (and (funcall movfunc) (looking-at outline-regexp))
  3216. (progn (goto-char beg)
  3217. (error "Cannot move past superior level or buffer limit")))
  3218. (setq cnt (1- cnt)))
  3219. (if (> arg 0)
  3220. ;; Moving forward - still need to move over subtree
  3221. (progn (outline-end-of-subtree)
  3222. (outline-next-heading)
  3223. (if (not (or (looking-at (concat "^" outline-regexp))
  3224. (bolp)))
  3225. (newline))))
  3226. (move-marker ins-point (point))
  3227. (setq txt (buffer-substring beg end))
  3228. (delete-region beg end)
  3229. (insert txt)
  3230. (goto-char ins-point)
  3231. (if folded (hide-subtree))
  3232. (move-marker ins-point nil)))
  3233. (defvar org-subtree-clip ""
  3234. "Clipboard for cut and paste of subtrees.
  3235. This is actually only a copy of the kill, because we use the normal kill
  3236. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  3237. (defvar org-subtree-clip-folded nil
  3238. "Was the last copied subtree folded?
  3239. This is used to fold the tree back after pasting.")
  3240. (defun org-cut-subtree ()
  3241. "Cut the current subtree into the clipboard.
  3242. This is a short-hand for marking the subtree and then cutting it."
  3243. (interactive)
  3244. (org-copy-subtree 'cut))
  3245. (defun org-copy-subtree (&optional cut)
  3246. "Cut the current subtree into the clipboard.
  3247. This is a short-hand for marking the subtree and then copying it.
  3248. If CUT is non nil, actually cut the subtree."
  3249. (interactive)
  3250. (let (beg end folded)
  3251. (org-back-to-heading)
  3252. (setq beg (point))
  3253. (save-match-data
  3254. (save-excursion (outline-end-of-heading)
  3255. (setq folded (org-invisible-p)))
  3256. (outline-end-of-subtree))
  3257. (if (equal (char-after) ?\n) (forward-char 1))
  3258. (setq end (point))
  3259. (goto-char beg)
  3260. (when (> end beg)
  3261. (setq org-subtree-clip-folded folded)
  3262. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  3263. (setq org-subtree-clip (current-kill 0))
  3264. (message "%s: Subtree with %d characters"
  3265. (if cut "Cut" "Copied")
  3266. (length org-subtree-clip)))))
  3267. (defun org-paste-subtree (&optional level tree)
  3268. "Paste the clipboard as a subtree, with modification of headline level.
  3269. The entire subtree is promoted or demoted in order to match a new headline
  3270. level. By default, the new level is derived from the visible headings
  3271. before and after the insertion point, and taken to be the inferior headline
  3272. level of the two. So if the previous visible heading is level 3 and the
  3273. next is level 4 (or vice versa), level 4 will be used for insertion.
  3274. This makes sure that the subtree remains an independent subtree and does
  3275. not swallow low level entries.
  3276. You can also force a different level, either by using a numeric prefix
  3277. argument, or by inserting the heading marker by hand. For example, if the
  3278. cursor is after \"*****\", then the tree will be shifted to level 5.
  3279. If you want to insert the tree as is, just use \\[yank].
  3280. If optional TREE is given, use this text instead of the kill ring."
  3281. (interactive "P")
  3282. (unless (org-kill-is-subtree-p tree)
  3283. (error
  3284. (substitute-command-keys
  3285. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  3286. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  3287. (^re (concat "^\\(" outline-regexp "\\)"))
  3288. (re (concat "\\(" outline-regexp "\\)"))
  3289. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  3290. (old-level (if (string-match ^re txt)
  3291. (- (match-end 0) (match-beginning 0))
  3292. -1))
  3293. (force-level (cond (level (prefix-numeric-value level))
  3294. ((string-match
  3295. ^re_ (buffer-substring (point-at-bol) (point)))
  3296. (- (match-end 0) (match-beginning 0)))
  3297. (t nil)))
  3298. (previous-level (save-excursion
  3299. (condition-case nil
  3300. (progn
  3301. (outline-previous-visible-heading 1)
  3302. (if (looking-at re)
  3303. (- (match-end 0) (match-beginning 0))
  3304. 1))
  3305. (error 1))))
  3306. (next-level (save-excursion
  3307. (condition-case nil
  3308. (progn
  3309. (outline-next-visible-heading 1)
  3310. (if (looking-at re)
  3311. (- (match-end 0) (match-beginning 0))
  3312. 1))
  3313. (error 1))))
  3314. (new-level (or force-level (max previous-level next-level)))
  3315. (shift (if (or (= old-level -1)
  3316. (= new-level -1)
  3317. (= old-level new-level))
  3318. 0
  3319. (- new-level old-level)))
  3320. (shift1 shift)
  3321. (delta (if (> shift 0) -1 1))
  3322. (func (if (> shift 0) 'org-demote 'org-promote))
  3323. (org-odd-levels-only nil)
  3324. beg end)
  3325. ;; Remove the forces level indicator
  3326. (if force-level
  3327. (delete-region (point-at-bol) (point)))
  3328. ;; Make sure we start at the beginning of an empty line
  3329. (if (not (bolp)) (insert "\n"))
  3330. (if (not (looking-at "[ \t]*$"))
  3331. (progn (insert "\n") (backward-char 1)))
  3332. ;; Paste
  3333. (setq beg (point))
  3334. (insert txt)
  3335. (setq end (point))
  3336. (goto-char beg)
  3337. ;; Shift if necessary
  3338. (if (= shift 0)
  3339. (message "Pasted at level %d, without shift" new-level)
  3340. (save-restriction
  3341. (narrow-to-region beg end)
  3342. (while (not (= shift 0))
  3343. (org-map-region func (point-min) (point-max))
  3344. (setq shift (+ delta shift)))
  3345. (goto-char (point-min))
  3346. (message "Pasted at level %d, with shift by %d levels"
  3347. new-level shift1)))
  3348. (if (and kill-ring
  3349. (eq org-subtree-clip (current-kill 0))
  3350. org-subtree-clip-folded)
  3351. ;; The tree was folded before it was killed/copied
  3352. (hide-subtree))))
  3353. (defun org-kill-is-subtree-p (&optional txt)
  3354. "Check if the current kill is an outline subtree, or a set of trees.
  3355. Returns nil if kill does not start with a headline, or if the first
  3356. headline level is not the largest headline level in the tree.
  3357. So this will actually accept several entries of equal levels as well,
  3358. which is OK for `org-paste-subtree'.
  3359. If optional TXT is given, check this string instead of the current kill."
  3360. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  3361. (start-level (and kill
  3362. (string-match (concat "\\`" outline-regexp) kill)
  3363. (- (match-end 0) (match-beginning 0))))
  3364. (re (concat "^" outline-regexp))
  3365. (start 1))
  3366. (if (not start-level)
  3367. nil ;; does not even start with a heading
  3368. (catch 'exit
  3369. (while (setq start (string-match re kill (1+ start)))
  3370. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  3371. (throw 'exit nil)))
  3372. t))))
  3373. ;;; Plain list items
  3374. (defun org-at-item-p ()
  3375. "Is point in a line starting a hand-formatted item?"
  3376. (let ((llt org-plain-list-ordered-item-terminator))
  3377. (save-excursion
  3378. (goto-char (point-at-bol))
  3379. (looking-at
  3380. (cond
  3381. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3382. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3383. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3384. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  3385. (defun org-at-item-checkbox-p ()
  3386. "Is point at a line starting a plain-list item with a checklet?"
  3387. (and (org-at-item-p)
  3388. (save-excursion
  3389. (goto-char (match-end 0))
  3390. (skip-chars-forward " \t")
  3391. (looking-at "\\[[ X]\\]"))))
  3392. (defun org-toggle-checkbox ()
  3393. "Toggle the checkbox in the current line."
  3394. (interactive)
  3395. (save-excursion
  3396. (if (org-at-item-checkbox-p)
  3397. (replace-match (if (equal (match-string 0) "[ ]") "[X]" "[ ]") t t))))
  3398. (defun org-get-indentation ()
  3399. "Get the indentation of the current line, interpreting tabs."
  3400. (save-excursion
  3401. (beginning-of-line 1)
  3402. (skip-chars-forward " \t")
  3403. (current-column)))
  3404. (defun org-beginning-of-item ()
  3405. "Go to the beginning of the current hand-formatted item.
  3406. If the cursor is not in an item, throw an error."
  3407. (interactive)
  3408. (let ((pos (point))
  3409. (limit (save-excursion (org-back-to-heading)
  3410. (beginning-of-line 2) (point)))
  3411. ind ind1)
  3412. (if (org-at-item-p)
  3413. (beginning-of-line 1)
  3414. (beginning-of-line 1)
  3415. (skip-chars-forward " \t")
  3416. (setq ind (current-column))
  3417. (if (catch 'exit
  3418. (while t
  3419. (beginning-of-line 0)
  3420. (if (< (point) limit) (throw 'exit nil))
  3421. (unless (looking-at " \t]*$")
  3422. (skip-chars-forward " \t")
  3423. (setq ind1 (current-column))
  3424. (if (< ind1 ind)
  3425. (throw 'exit (org-at-item-p))))))
  3426. nil
  3427. (goto-char pos)
  3428. (error "Not in an item")))))
  3429. (defun org-end-of-item ()
  3430. "Go to the end of the current hand-formatted item.
  3431. If the cursor is not in an item, throw an error."
  3432. (interactive)
  3433. (let ((pos (point))
  3434. (limit (save-excursion (outline-next-heading) (point)))
  3435. (ind (save-excursion
  3436. (org-beginning-of-item)
  3437. (skip-chars-forward " \t")
  3438. (current-column)))
  3439. ind1)
  3440. (if (catch 'exit
  3441. (while t
  3442. (beginning-of-line 2)
  3443. (if (>= (point) limit) (throw 'exit t))
  3444. (unless (looking-at "[ \t]*$")
  3445. (skip-chars-forward " \t")
  3446. (setq ind1 (current-column))
  3447. (if (<= ind1 ind) (throw 'exit t)))))
  3448. (beginning-of-line 1)
  3449. (goto-char pos)
  3450. (error "Not in an item"))))
  3451. (defun org-next-item ()
  3452. "Move to the beginning of the next item in the current plain list.
  3453. Error if not at a plain list, or if this is the last item in the list."
  3454. (interactive)
  3455. (let (beg end ind ind1 (pos (point)) txt)
  3456. (org-beginning-of-item)
  3457. (setq beg (point))
  3458. (setq ind (org-get-indentation))
  3459. (org-end-of-item)
  3460. (setq end (point))
  3461. (setq ind1 (org-get-indentation))
  3462. (unless (and (org-at-item-p) (= ind ind1))
  3463. (goto-char pos)
  3464. (error "On last item"))))
  3465. (defun org-previous-item ()
  3466. "Move to the beginning of the previous item in the current plain list.
  3467. Error if not at a plain list, or if this is the last item in the list."
  3468. (interactive)
  3469. (let (beg end ind ind1 (pos (point)) txt)
  3470. (org-beginning-of-item)
  3471. (setq beg (point))
  3472. (setq ind (org-get-indentation))
  3473. (goto-char beg)
  3474. (catch 'exit
  3475. (while t
  3476. (beginning-of-line 0)
  3477. (if (looking-at "[ \t]*$")
  3478. nil
  3479. (if (<= (setq ind1 (org-get-indentation)) ind)
  3480. (throw 'exit t)))))
  3481. (condition-case nil
  3482. (org-beginning-of-item)
  3483. (error (goto-char pos)
  3484. (error "On first item")))))
  3485. (defun org-move-item-down ()
  3486. "Move the plain list item at point down, i.e. swap with following item.
  3487. Subitems (items with larger indentation) are considered part of the item,
  3488. so this really moves item trees."
  3489. (interactive)
  3490. (let (beg end ind ind1 (pos (point)) txt)
  3491. (org-beginning-of-item)
  3492. (setq beg (point))
  3493. (setq ind (org-get-indentation))
  3494. (org-end-of-item)
  3495. (setq end (point))
  3496. (setq ind1 (org-get-indentation))
  3497. (if (and (org-at-item-p) (= ind ind1))
  3498. (progn
  3499. (org-end-of-item)
  3500. (setq txt (buffer-substring beg end))
  3501. (save-excursion
  3502. (delete-region beg end))
  3503. (setq pos (point))
  3504. (insert txt)
  3505. (goto-char pos)
  3506. (org-maybe-renumber-ordered-list))
  3507. (goto-char pos)
  3508. (error "Cannot move this item further down"))))
  3509. (defun org-move-item-up (arg)
  3510. "Move the plain list item at point up, i.e. swap with previous item.
  3511. Subitems (items with larger indentation) are considered part of the item,
  3512. so this really moves item trees."
  3513. (interactive "p")
  3514. (let (beg end ind ind1 (pos (point)) txt)
  3515. (org-beginning-of-item)
  3516. (setq beg (point))
  3517. (setq ind (org-get-indentation))
  3518. (org-end-of-item)
  3519. (setq end (point))
  3520. (goto-char beg)
  3521. (catch 'exit
  3522. (while t
  3523. (beginning-of-line 0)
  3524. (if (looking-at "[ \t]*$")
  3525. nil
  3526. (if (<= (setq ind1 (org-get-indentation)) ind)
  3527. (throw 'exit t)))))
  3528. (condition-case nil
  3529. (org-beginning-of-item)
  3530. (error (goto-char beg)
  3531. (error "Cannot move this item further up")))
  3532. (setq ind1 (org-get-indentation))
  3533. (if (and (org-at-item-p) (= ind ind1))
  3534. (progn
  3535. (setq txt (buffer-substring beg end))
  3536. (save-excursion
  3537. (delete-region beg end))
  3538. (setq pos (point))
  3539. (insert txt)
  3540. (goto-char pos)
  3541. (org-maybe-renumber-ordered-list))
  3542. (goto-char pos)
  3543. (error "Cannot move this item further up"))))
  3544. (defun org-maybe-renumber-ordered-list ()
  3545. "Renumber the ordered list at point if setup allows it.
  3546. This tests the user option `org-auto-renumber-ordered-lists' before
  3547. doing the renumbering."
  3548. (and org-auto-renumber-ordered-lists
  3549. (org-at-item-p)
  3550. (match-beginning 3)
  3551. (org-renumber-ordered-list 1)))
  3552. (defun org-get-string-indentation (s)
  3553. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  3554. (let ((n -1) (i 0) (w tab-width) c)
  3555. (catch 'exit
  3556. (while (< (setq n (1+ n)) (length s))
  3557. (setq c (aref s n))
  3558. (cond ((= c ?\ ) (setq i (1+ i)))
  3559. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  3560. (t (throw 'exit t)))))
  3561. i))
  3562. (defun org-renumber-ordered-list (arg)
  3563. "Renumber an ordered plain list.
  3564. Cursor needs to be in the first line of an item, the line that starts
  3565. with something like \"1.\" or \"2)\"."
  3566. (interactive "p")
  3567. (unless (and (org-at-item-p)
  3568. (match-beginning 3))
  3569. (error "This is not an ordered list"))
  3570. (let ((line (org-current-line))
  3571. (col (current-column))
  3572. (ind (org-get-string-indentation
  3573. (buffer-substring (point-at-bol) (match-beginning 3))))
  3574. ;; (term (substring (match-string 3) -1))
  3575. ind1 (n (1- arg)))
  3576. ;; find where this list begins
  3577. (catch 'exit
  3578. (while t
  3579. (catch 'next
  3580. (beginning-of-line 0)
  3581. (if (looking-at "[ \t]*$") (throw 'next t))
  3582. (skip-chars-forward " \t") (setq ind1 (current-column))
  3583. (if (or (< ind1 ind)
  3584. (and (= ind1 ind)
  3585. (not (org-at-item-p))))
  3586. (throw 'exit t)))))
  3587. ;; Walk forward and replace these numbers
  3588. (catch 'exit
  3589. (while t
  3590. (catch 'next
  3591. (beginning-of-line 2)
  3592. (if (eobp) (throw 'exit nil))
  3593. (if (looking-at "[ \t]*$") (throw 'next nil))
  3594. (skip-chars-forward " \t") (setq ind1 (current-column))
  3595. (if (> ind1 ind) (throw 'next t))
  3596. (if (< ind1 ind) (throw 'exit t))
  3597. (if (not (org-at-item-p)) (throw 'exit nil))
  3598. (if (not (match-beginning 3))
  3599. (error "unordered bullet in ordered list. Press \\[undo] to recover"))
  3600. (delete-region (match-beginning 3) (1- (match-end 3)))
  3601. (goto-char (match-beginning 3))
  3602. (insert (format "%d" (setq n (1+ n)))))))
  3603. (goto-line line)
  3604. (move-to-column col)))
  3605. (defvar org-last-indent-begin-marker (make-marker))
  3606. (defvar org-last-indent-end-marker (make-marker))
  3607. (defun org-outdent-item (arg)
  3608. "Outdent a local list item."
  3609. (interactive "p")
  3610. (org-indent-item (- arg)))
  3611. (defun org-indent-item (arg)
  3612. "Indent a local list item."
  3613. (interactive "p")
  3614. (unless (org-at-item-p)
  3615. (error "Not on an item"))
  3616. (save-excursion
  3617. (let (beg end ind ind1)
  3618. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  3619. (setq beg org-last-indent-begin-marker
  3620. end org-last-indent-end-marker)
  3621. (org-beginning-of-item)
  3622. (setq beg (move-marker org-last-indent-begin-marker (point)))
  3623. (org-end-of-item)
  3624. (setq end (move-marker org-last-indent-end-marker (point))))
  3625. (goto-char beg)
  3626. (skip-chars-forward " \t") (setq ind (current-column))
  3627. (if (< (+ arg ind) 0) (error "Cannot outdent beyond margin"))
  3628. (while (< (point) end)
  3629. (beginning-of-line 1)
  3630. (skip-chars-forward " \t") (setq ind1 (current-column))
  3631. (delete-region (point-at-bol) (point))
  3632. (indent-to-column (+ ind1 arg))
  3633. (beginning-of-line 2)))))
  3634. ;;; Archiving
  3635. (defun org-archive-subtree ()
  3636. "Move the current subtree to the archive.
  3637. The archive can be a certain top-level heading in the current file, or in
  3638. a different file. The tree will be moved to that location, the subtree
  3639. heading be marked DONE, and the current time will be added."
  3640. (interactive)
  3641. ;; Save all relevant TODO keyword-relatex variables
  3642. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  3643. (tr-org-todo-keywords org-todo-keywords)
  3644. (tr-org-todo-interpretation org-todo-interpretation)
  3645. (tr-org-done-string org-done-string)
  3646. (tr-org-todo-regexp org-todo-regexp)
  3647. (tr-org-todo-line-regexp org-todo-line-regexp)
  3648. (this-buffer (current-buffer))
  3649. file heading buffer level newfile-p)
  3650. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  3651. (progn
  3652. (setq file (format (match-string 1 org-archive-location)
  3653. (file-name-nondirectory buffer-file-name))
  3654. heading (match-string 2 org-archive-location)))
  3655. (error "Invalid `org-archive-location'"))
  3656. (if (> (length file) 0)
  3657. (setq newfile-p (not (file-exists-p file))
  3658. buffer (find-file-noselect file))
  3659. (setq buffer (current-buffer)))
  3660. (unless buffer
  3661. (error "Cannot access file \"%s\"" file))
  3662. (if (and (> (length heading) 0)
  3663. (string-match "^\\*+" heading))
  3664. (setq level (match-end 0))
  3665. (setq heading nil level 0))
  3666. (save-excursion
  3667. ;; We first only copy, in case something goes wrong
  3668. ;; we need to protect this-command, to avoid kill-region sets it,
  3669. ;; which would lead to duplication of subtrees
  3670. (let (this-command) (org-copy-subtree))
  3671. (set-buffer buffer)
  3672. ;; Enforce org-mode for the archive buffer
  3673. (if (not (eq major-mode 'org-mode))
  3674. ;; Force the mode for future visits.
  3675. (let ((org-insert-mode-line-in-empty-file t))
  3676. (call-interactively 'org-mode)))
  3677. (when newfile-p
  3678. (goto-char (point-max))
  3679. (insert (format "\nArchived entries from file %s\n\n"
  3680. (buffer-file-name this-buffer))))
  3681. ;; Force the TODO keywords of the original buffer
  3682. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  3683. (org-todo-keywords tr-org-todo-keywords)
  3684. (org-todo-interpretation tr-org-todo-interpretation)
  3685. (org-done-string tr-org-done-string)
  3686. (org-todo-regexp tr-org-todo-regexp)
  3687. (org-todo-line-regexp tr-org-todo-line-regexp))
  3688. (goto-char (point-min))
  3689. (if heading
  3690. (progn
  3691. (if (re-search-forward
  3692. (concat "\\(^\\|\r\\)"
  3693. (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
  3694. nil t)
  3695. (goto-char (match-end 0))
  3696. ;; Heading not found, just insert it at the end
  3697. (goto-char (point-max))
  3698. (or (bolp) (insert "\n"))
  3699. (insert "\n" heading "\n")
  3700. (end-of-line 0))
  3701. ;; Make the heading visible, and the following as well
  3702. (let ((org-show-following-heading t)) (org-show-hierarchy-above))
  3703. (if (re-search-forward
  3704. (concat "^" (regexp-quote (make-string level ?*)) "[ \t]")
  3705. nil t)
  3706. (progn (goto-char (match-beginning 0)) (insert "\n")
  3707. (beginning-of-line 0))
  3708. (goto-char (point-max)) (insert "\n")))
  3709. (goto-char (point-max)) (insert "\n"))
  3710. ;; Paste
  3711. (org-paste-subtree (1+ level))
  3712. ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
  3713. (if org-archive-mark-done
  3714. (org-todo (length org-todo-keywords)))
  3715. ;; Move cursor to right after the TODO keyword
  3716. (when org-archive-stamp-time
  3717. (beginning-of-line 1)
  3718. (looking-at org-todo-line-regexp)
  3719. (goto-char (or (match-end 2) (match-beginning 3)))
  3720. (insert "(" (format-time-string (cdr org-time-stamp-formats)
  3721. (org-current-time))
  3722. ")"))
  3723. ;; Save the buffer, if it is not the same buffer.
  3724. (if (not (eq this-buffer buffer)) (save-buffer))))
  3725. ;; Here we are back in the original buffer. Everything seems to have
  3726. ;; worked. So now cut the tree and finish up.
  3727. (let (this-command) (org-cut-subtree))
  3728. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  3729. (message "Subtree archived %s"
  3730. (if (eq this-buffer buffer)
  3731. (concat "under heading: " heading)
  3732. (concat "in file: " (abbreviate-file-name file))))))
  3733. ;;; Completion
  3734. (defun org-complete (&optional arg)
  3735. "Perform completion on word at point.
  3736. At the beginning of a headline, this completes TODO keywords as given in
  3737. `org-todo-keywords'.
  3738. If the current word is preceded by a backslash, completes the TeX symbols
  3739. that are supported for HTML support.
  3740. If the current word is preceded by \"#+\", completes special words for
  3741. setting file options.
  3742. At all other locations, this simply calls `ispell-complete-word'."
  3743. (interactive "P")
  3744. (catch 'exit
  3745. (let* ((end (point))
  3746. (beg1 (save-excursion
  3747. (if (equal (char-before (point)) ?\ ) (backward-char 1))
  3748. (skip-chars-backward "a-zA-Z_@0-9")
  3749. (point)))
  3750. (beg (save-excursion
  3751. (if (equal (char-before (point)) ?\ ) (backward-char 1))
  3752. (skip-chars-backward "a-zA-Z0-9_:$")
  3753. (point)))
  3754. (camel (equal (char-before beg) ?*))
  3755. (tag (equal (char-before beg1) ?:))
  3756. (texp (equal (char-before beg) ?\\))
  3757. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  3758. beg)
  3759. "#+"))
  3760. (completion-ignore-case opt)
  3761. (type nil)
  3762. (tbl nil)
  3763. (table (cond
  3764. (opt
  3765. (setq type :opt)
  3766. (mapcar (lambda (x)
  3767. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  3768. (cons (match-string 2 x) (match-string 1 x)))
  3769. (org-split-string (org-get-current-options) "\n")))
  3770. (texp
  3771. (setq type :tex)
  3772. org-html-entities)
  3773. ((string-match "\\`\\*+[ \t]*\\'"
  3774. (buffer-substring (point-at-bol) beg))
  3775. (setq type :todo)
  3776. (mapcar 'list org-todo-keywords))
  3777. (camel
  3778. (setq type :camel)
  3779. (save-excursion
  3780. (goto-char (point-min))
  3781. (while (re-search-forward org-todo-line-regexp nil t)
  3782. (push (list
  3783. (if org-file-link-context-use-camel-case
  3784. (org-make-org-heading-camel (match-string 3) t)
  3785. (org-make-org-heading-search-string
  3786. (match-string 3) t)))
  3787. tbl)))
  3788. tbl)
  3789. (tag (setq type :tag beg beg1)
  3790. (or org-tag-alist (org-get-buffer-tags)))
  3791. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  3792. (pattern (buffer-substring-no-properties beg end))
  3793. (completion (try-completion pattern table)))
  3794. (cond ((eq completion t)
  3795. (if (equal type :opt)
  3796. (insert (substring (cdr (assoc (upcase pattern) table))
  3797. (length pattern)))))
  3798. ((null completion)
  3799. (message "Can't find completion for \"%s\"" pattern)
  3800. (ding))
  3801. ((not (string= pattern completion))
  3802. (delete-region beg end)
  3803. (if (string-match " +$" completion)
  3804. (setq completion (replace-match "" t t completion)))
  3805. (insert completion)
  3806. (if (get-buffer-window "*Completions*")
  3807. (delete-window (get-buffer-window "*Completions*")))
  3808. (if (assoc completion table)
  3809. (if (eq type :todo) (insert " ")
  3810. (if (eq type :tag) (insert ":"))))
  3811. (if (and (equal type :opt) (assoc completion table))
  3812. (message "%s" (substitute-command-keys
  3813. "Press \\[org-complete] again to insert example settings"))))
  3814. (t
  3815. (message "Making completion list...")
  3816. (let ((list (sort (all-completions pattern table) 'string<)))
  3817. (with-output-to-temp-buffer "*Completions*"
  3818. (condition-case nil
  3819. ;; Protection needed for XEmacs and emacs 21
  3820. (display-completion-list list pattern)
  3821. (error (display-completion-list list)))))
  3822. (message "Making completion list...%s" "done"))))))
  3823. ;;; Comments, TODO and DEADLINE
  3824. (defun org-toggle-comment ()
  3825. "Change the COMMENT state of an entry."
  3826. (interactive)
  3827. (save-excursion
  3828. (org-back-to-heading)
  3829. (if (looking-at (concat outline-regexp
  3830. "\\( +\\<" org-comment-string "\\>\\)"))
  3831. (replace-match "" t t nil 1)
  3832. (if (looking-at outline-regexp)
  3833. (progn
  3834. (goto-char (match-end 0))
  3835. (insert " " org-comment-string))))))
  3836. (defvar org-last-todo-state-is-todo nil
  3837. "This is non-nil when the last TODO state change led to a TODO state.
  3838. If the last change removed the TODO tag or switched to DONE, then
  3839. this is nil.")
  3840. (defun org-todo (&optional arg)
  3841. "Change the TODO state of an item.
  3842. The state of an item is given by a keyword at the start of the heading,
  3843. like
  3844. *** TODO Write paper
  3845. *** DONE Call mom
  3846. The different keywords are specified in the variable `org-todo-keywords'.
  3847. By default the available states are \"TODO\" and \"DONE\".
  3848. So for this example: when the item starts with TODO, it is changed to DONE.
  3849. When it starts with DONE, the DONE is removed. And when neither TODO nor
  3850. DONE are present, add TODO at the beginning of the heading.
  3851. With prefix arg, use completion to determine the new state. With numeric
  3852. prefix arg, switch to that state."
  3853. (interactive "P")
  3854. (save-excursion
  3855. (org-back-to-heading)
  3856. (if (looking-at outline-regexp) (goto-char (match-end 0)))
  3857. (or (looking-at (concat " +" org-todo-regexp " *"))
  3858. (looking-at " *"))
  3859. (let* ((this (match-string 1))
  3860. (completion-ignore-case t)
  3861. (member (member this org-todo-keywords))
  3862. (tail (cdr member))
  3863. (state (cond
  3864. ((equal arg '(4))
  3865. ;; Read a state with completion
  3866. (completing-read "State: " (mapcar (lambda(x) (list x))
  3867. org-todo-keywords)
  3868. nil t))
  3869. ((eq arg 'right)
  3870. (if this
  3871. (if tail (car tail) nil)
  3872. (car org-todo-keywords)))
  3873. ((eq arg 'left)
  3874. (if (equal member org-todo-keywords)
  3875. nil
  3876. (if this
  3877. (nth (- (length org-todo-keywords) (length tail) 2)
  3878. org-todo-keywords)
  3879. org-done-string)))
  3880. (arg
  3881. ;; user requests a specific state
  3882. (nth (1- (prefix-numeric-value arg))
  3883. org-todo-keywords))
  3884. ((null member) (car org-todo-keywords))
  3885. ((null tail) nil) ;; -> first entry
  3886. ((eq org-todo-interpretation 'sequence)
  3887. (car tail))
  3888. ((memq org-todo-interpretation '(type priority))
  3889. (if (eq this-command last-command)
  3890. (car tail)
  3891. (if (> (length tail) 0) org-done-string nil)))
  3892. (t nil)))
  3893. (next (if state (concat " " state " ") " ")))
  3894. (replace-match next t t)
  3895. (setq org-last-todo-state-is-todo
  3896. (not (equal state org-done-string)))
  3897. (when org-log-done
  3898. (if (equal state org-done-string)
  3899. (org-add-planning-info 'closed (current-time) 'scheduled)
  3900. (if (not this)
  3901. (org-add-planning-info nil nil 'closed))))
  3902. ;; Fixup tag positioning
  3903. (and org-auto-align-tags (org-set-tags nil t))
  3904. (run-hooks 'org-after-todo-state-change-hook)))
  3905. ;; Fixup cursor location if close to the keyword
  3906. (if (and (outline-on-heading-p)
  3907. (not (bolp))
  3908. (save-excursion (beginning-of-line 1)
  3909. (looking-at org-todo-line-regexp))
  3910. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  3911. (progn
  3912. (goto-char (or (match-end 2) (match-end 1)))
  3913. (just-one-space))))
  3914. (defun org-log-done (&optional undone)
  3915. "Add a time stamp logging that a TODO entry has been closed.
  3916. When UNDONE is non-nil, remove such a time stamp again."
  3917. (interactive)
  3918. (let (beg end col)
  3919. (save-excursion
  3920. (org-back-to-heading t)
  3921. (setq beg (point))
  3922. (looking-at (concat outline-regexp " *"))
  3923. (goto-char (match-end 0))
  3924. (setq col (current-column))
  3925. (outline-next-heading)
  3926. (setq end (point))
  3927. (goto-char beg)
  3928. (when (re-search-forward (concat
  3929. "[\r\n]\\([ \t]*"
  3930. (regexp-quote org-closed-string)
  3931. " *\\[.*?\\][^\n\r]*[\n\r]?\\)") end t)
  3932. (delete-region (match-beginning 1) (match-end 1)))
  3933. (unless undone
  3934. (org-back-to-heading t)
  3935. (skip-chars-forward "^\n\r")
  3936. (goto-char (min (1+ (point)) (point-max)))
  3937. (when (not (member (char-before) '(?\r ?\n)))
  3938. (insert "\n"))
  3939. (indent-to col)
  3940. (insert org-closed-string " "
  3941. (format-time-string
  3942. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  3943. (org-current-time))
  3944. "\n")))))
  3945. (defun org-show-todo-tree (arg)
  3946. "Make a compact tree which shows all headlines marked with TODO.
  3947. The tree will show the lines where the regexp matches, and all higher
  3948. headlines above the match.
  3949. With \\[universal-argument] prefix, also show the DONE entries.
  3950. With a numeric prefix N, construct a sparse tree for the Nth element
  3951. of `org-todo-keywords'."
  3952. (interactive "P")
  3953. (let ((case-fold-search nil)
  3954. (kwd-re
  3955. (cond ((null arg) org-not-done-regexp)
  3956. ((equal arg '(4)) org-todo-regexp)
  3957. ((<= (prefix-numeric-value arg) (length org-todo-keywords))
  3958. (regexp-quote (nth (1- (prefix-numeric-value arg))
  3959. org-todo-keywords)))
  3960. (t (error "Invalid prefix argument: %s" arg)))))
  3961. (message "%d TODO entries found"
  3962. (org-occur (concat "^" outline-regexp " +" kwd-re )))))
  3963. (defun org-deadline ()
  3964. "Insert the DEADLINE: string to make a deadline.
  3965. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  3966. to modify it to the correct date."
  3967. (interactive)
  3968. (org-add-planning-info 'deadline nil 'closed))
  3969. (defun org-schedule ()
  3970. "Insert the SCHEDULED: string to schedule a TODO item.
  3971. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  3972. to modify it to the correct date."
  3973. (interactive)
  3974. (org-add-planning-info 'scheduled nil 'closed))
  3975. (defun org-add-planning-info (what &optional time &rest remove)
  3976. "Insert new timestamp with keyword in the line directly after the headline.
  3977. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  3978. If non is given, the user is prompted for a date.
  3979. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  3980. be removed."
  3981. (interactive)
  3982. (when what (setq time (or time (org-read-date nil 'to-time))))
  3983. (when (and org-insert-labeled-timestamps-at-point
  3984. (member what '(scheduled deadline)))
  3985. (insert
  3986. (if (eq what 'scheduled) org-scheduled-string org-deadline-string)
  3987. " "
  3988. (format-time-string (car org-time-stamp-formats) time))
  3989. (setq what nil))
  3990. (save-excursion
  3991. (let (beg end col list elt (buffer-invisibility-spec nil) ts)
  3992. (org-back-to-heading t)
  3993. (setq beg (point))
  3994. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  3995. (goto-char (match-end 1))
  3996. (setq col (current-column))
  3997. (goto-char (1+ (match-end 0)))
  3998. (if (and (not (looking-at outline-regexp))
  3999. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  4000. "[^\r\n]*")))
  4001. (narrow-to-region (match-beginning 0) (match-end 0))
  4002. (insert "\n")
  4003. (backward-char 1)
  4004. (narrow-to-region (point) (point))
  4005. (indent-to-column col))
  4006. ;; Check if we have to remove something.
  4007. (setq list (cons what remove))
  4008. (while list
  4009. (setq elt (pop list))
  4010. (goto-char (point-min))
  4011. (when (or (and (eq elt 'scheduled)
  4012. (re-search-forward org-scheduled-time-regexp nil t))
  4013. (and (eq elt 'deadline)
  4014. (re-search-forward org-deadline-time-regexp nil t))
  4015. (and (eq elt 'closed)
  4016. (re-search-forward org-closed-time-regexp nil t)))
  4017. (replace-match "")
  4018. (if (looking-at " +") (replace-match ""))))
  4019. (goto-char (point-max))
  4020. (when what
  4021. (insert
  4022. (if (not (equal (char-before) ?\ )) " " "")
  4023. (cond ((eq what 'scheduled) org-scheduled-string)
  4024. ((eq what 'deadline) org-deadline-string)
  4025. ((eq what 'closed) org-closed-string))
  4026. " ")
  4027. (insert
  4028. (setq ts
  4029. (format-time-string
  4030. (if (eq what 'closed)
  4031. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  4032. (car org-time-stamp-formats))
  4033. time))))
  4034. (goto-char (point-min))
  4035. (widen)
  4036. (if (looking-at "[ \t]+\r?\n")
  4037. (replace-match ""))
  4038. ts)))
  4039. (defun org-occur (regexp &optional callback)
  4040. "Make a compact tree which shows all matches of REGEXP.
  4041. The tree will show the lines where the regexp matches, and all higher
  4042. headlines above the match. It will also show the heading after the match,
  4043. to make sure editing the matching entry is easy.
  4044. If CALLBACK is non-nil, it is a function which is called to confirm
  4045. that the match should indeed be shown."
  4046. (interactive "sRegexp: ")
  4047. (org-remove-occur-highlights nil nil t)
  4048. (setq regexp (org-check-occur-regexp regexp))
  4049. (let ((cnt 0))
  4050. (save-excursion
  4051. (goto-char (point-min))
  4052. (org-overview)
  4053. (while (re-search-forward regexp nil t)
  4054. (when (or (not callback)
  4055. (save-match-data (funcall callback)))
  4056. (setq cnt (1+ cnt))
  4057. (org-highlight-new-match (match-beginning 0) (match-end 0))
  4058. (org-show-hierarchy-above))))
  4059. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  4060. nil 'local)
  4061. (run-hooks 'org-occur-hook)
  4062. (if (interactive-p)
  4063. (message "%d match(es) for regexp %s" cnt regexp))
  4064. cnt))
  4065. (defun org-show-hierarchy-above ()
  4066. "Make sure point and the headings hierarchy above is visible."
  4067. (catch 'exit
  4068. (if (org-on-heading-p t)
  4069. (org-flag-heading nil) ; only show the heading
  4070. (and (or (org-invisible-p) (org-invisible-p2))
  4071. (org-show-hidden-entry))) ; show entire entry
  4072. (save-excursion
  4073. (and org-show-following-heading
  4074. (outline-next-heading)
  4075. (org-flag-heading nil))) ; show the next heading
  4076. (when org-show-hierarchy-above
  4077. (save-excursion ; show all higher headings
  4078. (while (and (condition-case nil
  4079. (progn (org-up-heading-all 1) t)
  4080. (error nil))
  4081. (not (bobp)))
  4082. (org-flag-heading nil))))))
  4083. ;; Overlay compatibility functions
  4084. (defun org-make-overlay (beg end &optional buffer)
  4085. (if (featurep 'xemacs)
  4086. (make-extent beg end buffer)
  4087. (make-overlay beg end buffer)))
  4088. (defun org-delete-overlay (ovl)
  4089. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4090. (defun org-detatch-overlay (ovl)
  4091. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4092. (defun org-move-overlay (ovl beg end &optional buffer)
  4093. (if (featurep 'xemacs)
  4094. (set-extent-endpoints ovl beg end buffer)
  4095. (move-overlay ovl beg end buffer)))
  4096. (defun org-overlay-put (ovl prop value)
  4097. (if (featurep 'xemacs)
  4098. (set-extent-property ovl prop value)
  4099. (overlay-put ovl prop value)))
  4100. (defvar org-occur-highlights nil)
  4101. (defun org-highlight-new-match (beg end)
  4102. "Highlight from BEG to END and mark the highlight is an occur headline."
  4103. (let ((ov (org-make-overlay beg end)))
  4104. (org-overlay-put ov 'face 'secondary-selection)
  4105. (push ov org-occur-highlights)))
  4106. (defun org-remove-occur-highlights (&optional beg end noremove)
  4107. "Remove the occur highlights from the buffer.
  4108. BEG and END are ignored. If NOREMOVE is nil, remove this function
  4109. from the `before-change-functions' in the current buffer."
  4110. (interactive)
  4111. (mapc 'org-delete-overlay org-occur-highlights)
  4112. (setq org-occur-highlights nil)
  4113. (unless noremove
  4114. (remove-hook 'before-change-functions
  4115. 'org-remove-occur-highlights 'local)))
  4116. ;;; Priorities
  4117. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
  4118. "Regular expression matching the priority indicator.")
  4119. (defvar org-remove-priority-next-time nil)
  4120. (defun org-priority-up ()
  4121. "Increase the priority of the current item."
  4122. (interactive)
  4123. (org-priority 'up))
  4124. (defun org-priority-down ()
  4125. "Decrease the priority of the current item."
  4126. (interactive)
  4127. (org-priority 'down))
  4128. (defun org-priority (&optional action)
  4129. "Change the priority of an item by ARG.
  4130. ACTION can be set, up, or down."
  4131. (interactive)
  4132. (setq action (or action 'set))
  4133. (let (current new news have remove)
  4134. (save-excursion
  4135. (org-back-to-heading)
  4136. (if (looking-at org-priority-regexp)
  4137. (setq current (string-to-char (match-string 2))
  4138. have t)
  4139. (setq current org-default-priority))
  4140. (cond
  4141. ((eq action 'set)
  4142. (message "Priority A-%c, SPC to remove: " org-lowest-priority)
  4143. (setq new (read-char-exclusive))
  4144. (cond ((equal new ?\ ) (setq remove t))
  4145. ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
  4146. (error "Priority must be between `%c' and `%c'"
  4147. ?A org-lowest-priority))))
  4148. ((eq action 'up)
  4149. (setq new (1- current)))
  4150. ((eq action 'down)
  4151. (setq new (1+ current)))
  4152. (t (error "Invalid action")))
  4153. (setq new (min (max ?A (upcase new)) org-lowest-priority))
  4154. (setq news (format "%c" new))
  4155. (if have
  4156. (if remove
  4157. (replace-match "" t t nil 1)
  4158. (replace-match news t t nil 2))
  4159. (if remove
  4160. (error "No priority cookie found in line")
  4161. (looking-at org-todo-line-regexp)
  4162. (if (match-end 2)
  4163. (progn
  4164. (goto-char (match-end 2))
  4165. (insert " [#" news "]"))
  4166. (goto-char (match-beginning 3))
  4167. (insert "[#" news "] ")))))
  4168. (if remove
  4169. (message "Priority removed")
  4170. (message "Priority of current item set to %s" news))))
  4171. (defun org-get-priority (s)
  4172. "Find priority cookie and return priority."
  4173. (save-match-data
  4174. (if (not (string-match org-priority-regexp s))
  4175. (* 1000 (- org-lowest-priority org-default-priority))
  4176. (* 1000 (- org-lowest-priority
  4177. (string-to-char (match-string 2 s)))))))
  4178. ;;; Timestamps
  4179. (defvar org-last-changed-timestamp nil)
  4180. (defun org-time-stamp (arg)
  4181. "Prompt for a date/time and insert a time stamp.
  4182. If the user specifies a time like HH:MM, or if this command is called
  4183. with a prefix argument, the time stamp will contain date and time.
  4184. Otherwise, only the date will be included. All parts of a date not
  4185. specified by the user will be filled in from the current date/time.
  4186. So if you press just return without typing anything, the time stamp
  4187. will represent the current date/time. If there is already a timestamp
  4188. at the cursor, it will be modified."
  4189. (interactive "P")
  4190. (let ((fmt (if arg (cdr org-time-stamp-formats)
  4191. (car org-time-stamp-formats)))
  4192. (org-time-was-given nil)
  4193. time)
  4194. (cond
  4195. ((and (org-at-timestamp-p)
  4196. (eq last-command 'org-time-stamp)
  4197. (eq this-command 'org-time-stamp))
  4198. (insert "--")
  4199. (setq time (let ((this-command this-command))
  4200. (org-read-date arg 'totime)))
  4201. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4202. (insert (format-time-string fmt time)))
  4203. ((org-at-timestamp-p)
  4204. (setq time (let ((this-command this-command))
  4205. (org-read-date arg 'totime)))
  4206. (and (org-at-timestamp-p) (replace-match
  4207. (setq org-last-changed-timestamp
  4208. (format-time-string fmt time))
  4209. t t))
  4210. (message "Timestamp updated"))
  4211. (t
  4212. (setq time (let ((this-command this-command))
  4213. (org-read-date arg 'totime)))
  4214. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4215. (insert (format-time-string fmt time))))))
  4216. (defun org-time-stamp-inactive (&optional arg)
  4217. "Insert an inactive time stamp.
  4218. An inactive time stamp is enclosed in square brackets instead of angle
  4219. brackets. It is inactive in the sense that it does not trigger agenda entries,
  4220. does not link to the calendar and cannot be changed with the S-cursor keys.
  4221. So these are more for recording a certain time/date."
  4222. (interactive "P")
  4223. (let ((fmt (if arg (cdr org-time-stamp-formats)
  4224. (car org-time-stamp-formats)))
  4225. (org-time-was-given nil)
  4226. time)
  4227. (setq time (org-read-date arg 'totime))
  4228. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4229. (setq fmt (concat "[" (substring fmt 1 -1) "]"))
  4230. (insert (format-time-string fmt time))))
  4231. (defvar org-date-ovl (org-make-overlay 1 1))
  4232. (org-overlay-put org-date-ovl 'face 'org-warning)
  4233. (org-detatch-overlay org-date-ovl)
  4234. (defun org-read-date (&optional with-time to-time)
  4235. "Read a date and make things smooth for the user.
  4236. The prompt will suggest to enter an ISO date, but you can also enter anything
  4237. which will at least partially be understood by `parse-time-string'.
  4238. Unrecognized parts of the date will default to the current day, month, year,
  4239. hour and minute. For example,
  4240. 3-2-5 --> 2003-02-05
  4241. feb 15 --> currentyear-02-15
  4242. sep 12 9 --> 2009-09-12
  4243. 12:45 --> today 12:45
  4244. 22 sept 0:34 --> currentyear-09-22 0:34
  4245. 12 --> currentyear-currentmonth-12
  4246. Fri --> nearest Friday (today or later)
  4247. etc.
  4248. The function understands only English month and weekday abbreviations,
  4249. but this can be configured with the variables `parse-time-months' and
  4250. `parse-time-weekdays'.
  4251. While prompting, a calendar is popped up - you can also select the
  4252. date with the mouse (button 1). The calendar shows a period of three
  4253. months. To scroll it to other months, use the keys `>' and `<'.
  4254. If you don't like the calendar, turn it off with
  4255. \(setq org-popup-calendar-for-date-prompt nil)
  4256. With optional argument TO-TIME, the date will immediately be converted
  4257. to an internal time.
  4258. With an optional argument WITH-TIME, the prompt will suggest to also
  4259. insert a time. Note that when WITH-TIME is not set, you can still
  4260. enter a time, and this function will inform the calling routine about
  4261. this change. The calling routine may then choose to change the format
  4262. used to insert the time stamp into the buffer to include the time."
  4263. (require 'parse-time)
  4264. (let* ((org-time-stamp-rounding-minutes
  4265. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  4266. (ct (org-current-time))
  4267. (default-time
  4268. ;; Default time is either today, or, when entering a range,
  4269. ;; the range start.
  4270. (if (save-excursion
  4271. (re-search-backward
  4272. (concat org-ts-regexp "--\\=") ; FIXME: exactly two minuses?
  4273. (- (point) 20) t))
  4274. (apply
  4275. 'encode-time
  4276. (mapcar (lambda(x) (or x 0))
  4277. (parse-time-string (match-string 1))))
  4278. ct))
  4279. (calendar-move-hook nil)
  4280. (view-diary-entries-initially nil)
  4281. (view-calendar-holidays-initially nil)
  4282. (timestr (format-time-string
  4283. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  4284. (prompt (format "YYYY-MM-DD [%s]: " timestr))
  4285. ans ans1 ans2
  4286. second minute hour day month year tl wday wday1)
  4287. (if org-popup-calendar-for-date-prompt
  4288. (save-excursion
  4289. (save-window-excursion
  4290. (calendar)
  4291. (calendar-forward-day (- (time-to-days default-time)
  4292. (calendar-absolute-from-gregorian
  4293. (calendar-current-date))))
  4294. (org-eval-in-calendar nil)
  4295. (let* ((old-map (current-local-map))
  4296. (map (copy-keymap calendar-mode-map))
  4297. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  4298. (define-key map (kbd "RET") 'org-calendar-select)
  4299. (define-key map (if (featurep 'xemacs) [button1] [mouse-1])
  4300. 'org-calendar-select-mouse)
  4301. (define-key map (if (featurep 'xemacs) [button2] [mouse-2])
  4302. 'org-calendar-select-mouse)
  4303. (define-key minibuffer-local-map [(meta shift left)]
  4304. (lambda () (interactive)
  4305. (org-eval-in-calendar '(calendar-backward-month 1))))
  4306. (define-key minibuffer-local-map [(meta shift right)]
  4307. (lambda () (interactive)
  4308. (org-eval-in-calendar '(calendar-forward-month 1))))
  4309. (define-key minibuffer-local-map [(shift up)]
  4310. (lambda () (interactive)
  4311. (org-eval-in-calendar '(calendar-backward-week 1))))
  4312. (define-key minibuffer-local-map [(shift down)]
  4313. (lambda () (interactive)
  4314. (org-eval-in-calendar '(calendar-forward-week 1))))
  4315. (define-key minibuffer-local-map [(shift left)]
  4316. (lambda () (interactive)
  4317. (org-eval-in-calendar '(calendar-backward-day 1))))
  4318. (define-key minibuffer-local-map [(shift right)]
  4319. (lambda () (interactive)
  4320. (org-eval-in-calendar '(calendar-forward-day 1))))
  4321. (define-key minibuffer-local-map ">"
  4322. (lambda () (interactive)
  4323. (org-eval-in-calendar '(scroll-calendar-left 1))))
  4324. (define-key minibuffer-local-map "<"
  4325. (lambda () (interactive)
  4326. (org-eval-in-calendar '(scroll-calendar-right 1))))
  4327. (unwind-protect
  4328. (progn
  4329. (use-local-map map)
  4330. (setq ans (read-string prompt "" nil nil))
  4331. (if (not (string-match "\\S-" ans)) (setq ans nil))
  4332. (setq ans (or ans1 ans ans2)))
  4333. (use-local-map old-map)))))
  4334. ;; Naked prompt only
  4335. (setq ans (read-string prompt "" nil timestr)))
  4336. (org-detatch-overlay org-date-ovl)
  4337. (if (string-match
  4338. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  4339. (progn
  4340. (setq year (if (match-end 2)
  4341. (string-to-number (match-string 2 ans))
  4342. (string-to-number (format-time-string "%Y")))
  4343. month (string-to-number (match-string 3 ans))
  4344. day (string-to-number (match-string 4 ans)))
  4345. (if (< year 100) (setq year (+ 2000 year)))
  4346. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  4347. t nil ans))))
  4348. (setq tl (parse-time-string ans)
  4349. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  4350. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  4351. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  4352. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  4353. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  4354. second (or (nth 0 tl) 0)
  4355. wday (nth 6 tl))
  4356. (when (and wday (not (nth 3 tl)))
  4357. ;; Weekday was given, but no day, so pick that day in the week
  4358. ;; on or after the derived date.
  4359. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  4360. (unless (equal wday wday1)
  4361. (setq day (+ day (% (- wday wday1 -7) 7)))))
  4362. (if (and (boundp 'org-time-was-given)
  4363. (nth 2 tl))
  4364. (setq org-time-was-given t))
  4365. (if (< year 100) (setq year (+ 2000 year)))
  4366. (if to-time
  4367. (encode-time second minute hour day month year)
  4368. (if (or (nth 1 tl) (nth 2 tl))
  4369. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  4370. (format "%04d-%02d-%02d" year month day)))))
  4371. (defun org-eval-in-calendar (form)
  4372. "Eval FORM in the calendar window and return to current window.
  4373. Also, store the cursor date in variable ans2."
  4374. (let ((sw (selected-window)))
  4375. (select-window (get-buffer-window "*Calendar*"))
  4376. (eval form)
  4377. (when (calendar-cursor-to-date)
  4378. (let* ((date (calendar-cursor-to-date))
  4379. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  4380. (setq ans2 (format-time-string "%Y-%m-%d" time))))
  4381. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  4382. (select-window sw)))
  4383. (defun org-calendar-select ()
  4384. "Return to `org-read-date' with the date currently selected.
  4385. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  4386. (interactive)
  4387. (when (calendar-cursor-to-date)
  4388. (let* ((date (calendar-cursor-to-date))
  4389. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  4390. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  4391. (if (active-minibuffer-window) (exit-minibuffer))))
  4392. (defun org-calendar-select-mouse (ev)
  4393. "Return to `org-read-date' with the date currently selected.
  4394. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  4395. (interactive "e")
  4396. (mouse-set-point ev)
  4397. (when (calendar-cursor-to-date)
  4398. (let* ((date (calendar-cursor-to-date))
  4399. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  4400. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  4401. (if (active-minibuffer-window) (exit-minibuffer))))
  4402. (defun org-check-deadlines (ndays)
  4403. "Check if there are any deadlines due or past due.
  4404. A deadline is considered due if it happens within `org-deadline-warning-days'
  4405. days from today's date. If the deadline appears in an entry marked DONE,
  4406. it is not shown. The prefix arg NDAYS can be used to test that many
  4407. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  4408. (interactive "P")
  4409. (let* ((org-warn-days
  4410. (cond
  4411. ((equal ndays '(4)) 100000)
  4412. (ndays (prefix-numeric-value ndays))
  4413. (t org-deadline-warning-days)))
  4414. (case-fold-search nil)
  4415. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  4416. (callback
  4417. (lambda ()
  4418. (and (let ((d1 (time-to-days (current-time)))
  4419. (d2 (time-to-days
  4420. (org-time-string-to-time (match-string 1)))))
  4421. (< (- d2 d1) org-warn-days))
  4422. (not (org-entry-is-done-p))))))
  4423. (message "%d deadlines past-due or due within %d days"
  4424. (org-occur regexp callback)
  4425. org-warn-days)))
  4426. (defun org-evaluate-time-range (&optional to-buffer)
  4427. "Evaluate a time range by computing the difference between start and end.
  4428. Normally the result is just printed in the echo area, but with prefix arg
  4429. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  4430. If the time range is actually in a table, the result is inserted into the
  4431. next column.
  4432. For time difference computation, a year is assumed to be exactly 365
  4433. days in order to avoid rounding problems."
  4434. (interactive "P")
  4435. (save-excursion
  4436. (unless (org-at-date-range-p)
  4437. (goto-char (point-at-bol))
  4438. (re-search-forward org-tr-regexp (point-at-eol) t))
  4439. (if (not (org-at-date-range-p))
  4440. (error "Not at a time-stamp range, and none found in current line")))
  4441. (let* ((ts1 (match-string 1))
  4442. (ts2 (match-string 2))
  4443. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  4444. (match-end (match-end 0))
  4445. (time1 (org-time-string-to-time ts1))
  4446. (time2 (org-time-string-to-time ts2))
  4447. (t1 (time-to-seconds time1))
  4448. (t2 (time-to-seconds time2))
  4449. (diff (abs (- t2 t1)))
  4450. (negative (< (- t2 t1) 0))
  4451. ;; (ys (floor (* 365 24 60 60)))
  4452. (ds (* 24 60 60))
  4453. (hs (* 60 60))
  4454. (fy "%dy %dd %02d:%02d")
  4455. (fy1 "%dy %dd")
  4456. (fd "%dd %02d:%02d")
  4457. (fd1 "%dd")
  4458. (fh "%02d:%02d")
  4459. y d h m align)
  4460. (if havetime
  4461. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  4462. y 0
  4463. d (floor (/ diff ds)) diff (mod diff ds)
  4464. h (floor (/ diff hs)) diff (mod diff hs)
  4465. m (floor (/ diff 60)))
  4466. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  4467. y 0
  4468. d (floor (+ (/ diff ds) 0.5))
  4469. h 0 m 0))
  4470. (if (not to-buffer)
  4471. (message (org-make-tdiff-string y d h m))
  4472. (when (org-at-table-p)
  4473. (goto-char match-end)
  4474. (setq align t)
  4475. (and (looking-at " *|") (goto-char (match-end 0))))
  4476. (if (looking-at
  4477. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  4478. (replace-match ""))
  4479. (if negative (insert " -"))
  4480. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  4481. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  4482. (insert " " (format fh h m))))
  4483. (if align (org-table-align))
  4484. (message "Time difference inserted"))))
  4485. (defun org-make-tdiff-string (y d h m)
  4486. (let ((fmt "")
  4487. (l nil))
  4488. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  4489. l (push y l)))
  4490. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  4491. l (push d l)))
  4492. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  4493. l (push h l)))
  4494. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  4495. l (push m l)))
  4496. (apply 'format fmt (nreverse l))))
  4497. (defun org-time-string-to-time (s)
  4498. (apply 'encode-time (org-parse-time-string s)))
  4499. (defun org-parse-time-string (s &optional nodefault)
  4500. "Parse the standard Org-mode time string.
  4501. This should be a lot faster than the normal `parse-time-string'.
  4502. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  4503. hour and minute fields will be nil if not given."
  4504. (if (string-match org-ts-regexp1 s)
  4505. (list 0
  4506. (if (or (match-beginning 8) (not nodefault))
  4507. (string-to-number (or (match-string 8 s) "0")))
  4508. (if (or (match-beginning 7) (not nodefault))
  4509. (string-to-number (or (match-string 7 s) "0")))
  4510. (string-to-number (match-string 4 s))
  4511. (string-to-number (match-string 3 s))
  4512. (string-to-number (match-string 2 s))
  4513. nil nil nil)
  4514. (make-list 9 0)))
  4515. (defun org-timestamp-up (&optional arg)
  4516. "Increase the date item at the cursor by one.
  4517. If the cursor is on the year, change the year. If it is on the month or
  4518. the day, change that.
  4519. With prefix ARG, change by that many units."
  4520. (interactive "p")
  4521. (org-timestamp-change (prefix-numeric-value arg)))
  4522. (defun org-timestamp-down (&optional arg)
  4523. "Decrease the date item at the cursor by one.
  4524. If the cursor is on the year, change the year. If it is on the month or
  4525. the day, change that.
  4526. With prefix ARG, change by that many units."
  4527. (interactive "p")
  4528. (org-timestamp-change (- (prefix-numeric-value arg))))
  4529. (defun org-timestamp-up-day (&optional arg)
  4530. "Increase the date in the time stamp by one day.
  4531. With prefix ARG, change that many days."
  4532. (interactive "p")
  4533. (if (and (not (org-at-timestamp-p))
  4534. (org-on-heading-p))
  4535. (org-todo 'up)
  4536. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  4537. (defun org-timestamp-down-day (&optional arg)
  4538. "Decrease the date in the time stamp by one day.
  4539. With prefix ARG, change that many days."
  4540. (interactive "p")
  4541. (if (and (not (org-at-timestamp-p))
  4542. (org-on-heading-p))
  4543. (org-todo 'down)
  4544. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  4545. (defsubst org-pos-in-match-range (pos n)
  4546. (and (match-beginning n)
  4547. (<= (match-beginning n) pos)
  4548. (>= (match-end n) pos)))
  4549. (defun org-at-timestamp-p ()
  4550. "Determine if the cursor is in or at a timestamp."
  4551. (interactive)
  4552. (let* ((tsr org-ts-regexp2)
  4553. (pos (point))
  4554. (ans (or (looking-at tsr)
  4555. (save-excursion
  4556. (skip-chars-backward "^<\n\r\t")
  4557. (if (> (point) 1) (backward-char 1))
  4558. (and (looking-at tsr)
  4559. (> (- (match-end 0) pos) -1))))))
  4560. (and (boundp 'org-ts-what)
  4561. (setq org-ts-what
  4562. (cond
  4563. ((org-pos-in-match-range pos 2) 'year)
  4564. ((org-pos-in-match-range pos 3) 'month)
  4565. ((org-pos-in-match-range pos 7) 'hour)
  4566. ((org-pos-in-match-range pos 8) 'minute)
  4567. ((or (org-pos-in-match-range pos 4)
  4568. (org-pos-in-match-range pos 5)) 'day)
  4569. (t 'day))))
  4570. ans))
  4571. (defun org-timestamp-change (n &optional what)
  4572. "Change the date in the time stamp at point.
  4573. The date will be changed by N times WHAT. WHAT can be `day', `month',
  4574. `year', `minute', `second'. If WHAT is not given, the cursor position
  4575. in the timestamp determines what will be changed."
  4576. (let ((fmt (car org-time-stamp-formats))
  4577. org-ts-what
  4578. (pos (point))
  4579. ts time time0)
  4580. (if (not (org-at-timestamp-p))
  4581. (error "Not at a timestamp"))
  4582. (setq org-ts-what (or what org-ts-what))
  4583. (setq fmt (if (<= (abs (- (cdr org-ts-lengths)
  4584. (- (match-end 0) (match-beginning 0))))
  4585. 1)
  4586. (cdr org-time-stamp-formats)
  4587. (car org-time-stamp-formats)))
  4588. (setq ts (match-string 0))
  4589. (replace-match "")
  4590. (setq time0 (org-parse-time-string ts))
  4591. (setq time
  4592. (apply 'encode-time
  4593. (append
  4594. (list (or (car time0) 0))
  4595. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  4596. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  4597. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  4598. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  4599. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  4600. (nthcdr 6 time0))))
  4601. (if (eq what 'calendar)
  4602. (let ((cal-date
  4603. (save-excursion
  4604. (save-match-data
  4605. (set-buffer "*Calendar*")
  4606. (calendar-cursor-to-date)))))
  4607. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  4608. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  4609. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  4610. (setcar time0 (or (car time0) 0))
  4611. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  4612. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  4613. (setq time (apply 'encode-time time0))))
  4614. (insert (setq org-last-changed-timestamp (format-time-string fmt time)))
  4615. (goto-char pos)
  4616. ;; Try to recenter the calendar window, if any
  4617. (if (and org-calendar-follow-timestamp-change
  4618. (get-buffer-window "*Calendar*" t)
  4619. (memq org-ts-what '(day month year)))
  4620. (org-recenter-calendar (time-to-days time)))))
  4621. (defun org-recenter-calendar (date)
  4622. "If the calendar is visible, recenter it to DATE."
  4623. (let* ((win (selected-window))
  4624. (cwin (get-buffer-window "*Calendar*" t))
  4625. (calendar-move-hook nil))
  4626. (when cwin
  4627. (select-window cwin)
  4628. (calendar-goto-date (if (listp date) date
  4629. (calendar-gregorian-from-absolute date)))
  4630. (select-window win))))
  4631. (defun org-goto-calendar (&optional arg)
  4632. "Go to the Emacs calendar at the current date.
  4633. If there is a time stamp in the current line, go to that date.
  4634. A prefix ARG can be used to force the current date."
  4635. (interactive "P")
  4636. (let ((tsr org-ts-regexp) diff
  4637. (calendar-move-hook nil)
  4638. (view-calendar-holidays-initially nil)
  4639. (view-diary-entries-initially nil))
  4640. (if (or (org-at-timestamp-p)
  4641. (save-excursion
  4642. (beginning-of-line 1)
  4643. (looking-at (concat ".*" tsr))))
  4644. (let ((d1 (time-to-days (current-time)))
  4645. (d2 (time-to-days
  4646. (org-time-string-to-time (match-string 1)))))
  4647. (setq diff (- d2 d1))))
  4648. (calendar)
  4649. (calendar-goto-today)
  4650. (if (and diff (not arg)) (calendar-forward-day diff))))
  4651. (defun org-date-from-calendar ()
  4652. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  4653. If there is already a time stamp at the cursor position, update it."
  4654. (interactive)
  4655. (org-timestamp-change 0 'calendar))
  4656. ;;; Agenda, and Diary Integration
  4657. ;;; Define the mode
  4658. (defvar org-agenda-mode-map (make-sparse-keymap)
  4659. "Keymap for `org-agenda-mode'.")
  4660. (defvar org-agenda-menu) ; defined later in this file.
  4661. (defvar org-agenda-follow-mode nil)
  4662. (defvar org-agenda-show-log nil)
  4663. (defvar org-agenda-buffer-name "*Org Agenda*")
  4664. (defvar org-agenda-redo-command nil)
  4665. (defvar org-agenda-mode-hook nil)
  4666. (defvar org-agenda-type nil)
  4667. (defvar org-agenda-force-single-file nil)
  4668. (defun org-agenda-mode ()
  4669. "Mode for time-sorted view on action items in Org-mode files.
  4670. The following commands are available:
  4671. \\{org-agenda-mode-map}"
  4672. (interactive)
  4673. (kill-all-local-variables)
  4674. (setq major-mode 'org-agenda-mode)
  4675. (setq mode-name "Org-Agenda")
  4676. (use-local-map org-agenda-mode-map)
  4677. (easy-menu-add org-agenda-menu)
  4678. (if org-startup-truncated (setq truncate-lines t))
  4679. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  4680. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  4681. (unless org-agenda-keep-modes
  4682. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  4683. org-agenda-show-log nil))
  4684. (easy-menu-change
  4685. '("Agenda") "Agenda Files"
  4686. (append
  4687. (list
  4688. (vector
  4689. (if (get 'org-agenda-files 'org-restrict)
  4690. "Restricted to single file"
  4691. "Edit File List")
  4692. '(org-edit-agenda-file-list)
  4693. (not (get 'org-agenda-files 'org-restrict)))
  4694. "--")
  4695. (mapcar 'org-file-menu-entry (org-agenda-files))))
  4696. (org-agenda-set-mode-name)
  4697. (apply
  4698. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  4699. (list 'org-agenda-mode-hook)))
  4700. (define-key org-agenda-mode-map "\C-i" 'org-agenda-goto)
  4701. (define-key org-agenda-mode-map [(tab)] 'org-agenda-goto)
  4702. (define-key org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  4703. (define-key org-agenda-mode-map " " 'org-agenda-show)
  4704. (define-key org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  4705. (define-key org-agenda-mode-map "o" 'delete-other-windows)
  4706. (define-key org-agenda-mode-map "L" 'org-agenda-recenter)
  4707. (define-key org-agenda-mode-map "t" 'org-agenda-todo)
  4708. (define-key org-agenda-mode-map ":" 'org-agenda-set-tags)
  4709. (define-key org-agenda-mode-map "." 'org-agenda-goto-today)
  4710. (define-key org-agenda-mode-map "d" 'org-agenda-day-view)
  4711. (define-key org-agenda-mode-map "w" 'org-agenda-week-view)
  4712. (define-key org-agenda-mode-map (org-key 'S-right) 'org-agenda-date-later)
  4713. (define-key org-agenda-mode-map (org-key 'S-left) 'org-agenda-date-earlier)
  4714. (define-key org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  4715. (define-key org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  4716. (define-key org-agenda-mode-map ">" 'org-agenda-date-prompt)
  4717. (define-key org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  4718. (define-key org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  4719. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  4720. (while l (define-key org-agenda-mode-map
  4721. (int-to-string (pop l)) 'digit-argument)))
  4722. (define-key org-agenda-mode-map "f" 'org-agenda-follow-mode)
  4723. (define-key org-agenda-mode-map "l" 'org-agenda-log-mode)
  4724. (define-key org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  4725. (define-key org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  4726. (define-key org-agenda-mode-map "r" 'org-agenda-redo)
  4727. (define-key org-agenda-mode-map "q" 'org-agenda-quit)
  4728. (define-key org-agenda-mode-map "x" 'org-agenda-exit)
  4729. (define-key org-agenda-mode-map "P" 'org-agenda-show-priority)
  4730. (define-key org-agenda-mode-map "T" 'org-agenda-show-tags)
  4731. (define-key org-agenda-mode-map "n" 'next-line)
  4732. (define-key org-agenda-mode-map "p" 'previous-line)
  4733. (define-key org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  4734. (define-key org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  4735. (define-key org-agenda-mode-map "," 'org-agenda-priority)
  4736. (define-key org-agenda-mode-map "\C-c," 'org-agenda-priority)
  4737. (define-key org-agenda-mode-map "i" 'org-agenda-diary-entry)
  4738. (define-key org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  4739. (eval-after-load "calendar"
  4740. '(define-key calendar-mode-map org-calendar-to-agenda-key
  4741. 'org-calendar-goto-agenda))
  4742. (define-key org-agenda-mode-map "C" 'org-agenda-convert-date)
  4743. (define-key org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
  4744. (define-key org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  4745. (define-key org-agenda-mode-map "s" 'org-agenda-sunrise-sunset)
  4746. (define-key org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  4747. (define-key org-agenda-mode-map "h" 'org-agenda-holidays)
  4748. (define-key org-agenda-mode-map "H" 'org-agenda-holidays)
  4749. (define-key org-agenda-mode-map "+" 'org-agenda-priority-up)
  4750. (define-key org-agenda-mode-map "-" 'org-agenda-priority-down)
  4751. (define-key org-agenda-mode-map (org-key 'S-up) 'org-agenda-priority-up)
  4752. (define-key org-agenda-mode-map (org-key 'S-down) 'org-agenda-priority-down)
  4753. (define-key org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  4754. (define-key org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  4755. (define-key org-agenda-mode-map [(right)] 'org-agenda-later)
  4756. (define-key org-agenda-mode-map [(left)] 'org-agenda-earlier)
  4757. (define-key org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  4758. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  4759. "Local keymap for agenda entries from Org-mode.")
  4760. (define-key org-agenda-keymap
  4761. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  4762. (define-key org-agenda-keymap
  4763. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  4764. (when org-agenda-mouse-1-follows-link
  4765. (define-key org-agenda-keymap [follow-link] 'mouse-face))
  4766. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  4767. '("Agenda"
  4768. ("Agenda Files")
  4769. "--"
  4770. ["Show" org-agenda-show t]
  4771. ["Go To (other window)" org-agenda-goto t]
  4772. ["Go To (one window)" org-agenda-switch-to t]
  4773. ["Follow Mode" org-agenda-follow-mode
  4774. :style toggle :selected org-agenda-follow-mode :active t]
  4775. "--"
  4776. ["Cycle TODO" org-agenda-todo t]
  4777. ("Tags"
  4778. ["Show all Tags" org-agenda-show-tags t]
  4779. ["Set Tags" org-agenda-set-tags t])
  4780. ("Schedule"
  4781. ["Schedule" org-agenda-schedule t]
  4782. ["Set Deadline" org-agenda-deadline t]
  4783. "--"
  4784. ["Reschedule +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  4785. ["Reschedule -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  4786. ["Reschedule to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  4787. ("Priority"
  4788. ["Set Priority" org-agenda-priority t]
  4789. ["Increase Priority" org-agenda-priority-up t]
  4790. ["Decrease Priority" org-agenda-priority-down t]
  4791. ["Show Priority" org-agenda-show-priority t])
  4792. "--"
  4793. ;; ["New agenda command" org-agenda t]
  4794. ["Rebuild buffer" org-agenda-redo t]
  4795. "--"
  4796. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  4797. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  4798. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  4799. "--"
  4800. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  4801. :style radio :selected (equal org-agenda-ndays 1)]
  4802. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  4803. :style radio :selected (equal org-agenda-ndays 7)]
  4804. "--"
  4805. ["Show Logbook entries" org-agenda-log-mode
  4806. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  4807. ["Include Diary" org-agenda-toggle-diary
  4808. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  4809. ["Use Time Grid" org-agenda-toggle-time-grid
  4810. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)]
  4811. "--"
  4812. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  4813. ("Calendar Commands"
  4814. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  4815. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  4816. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  4817. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  4818. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)])
  4819. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t]
  4820. "--"
  4821. ["Quit" org-agenda-quit t]
  4822. ["Exit and Release Buffers" org-agenda-exit t]
  4823. ))
  4824. ;;;###autoload
  4825. (defun org-agenda (arg)
  4826. "Dispatch agenda commands to collect entries to the agenda buffer.
  4827. Prompts for a character to select a command. Any prefix arg will be passed
  4828. on to the selected command. The default selections are:
  4829. a Call `org-agenda' to display the agenda for the current day or week.
  4830. t Call `org-todo-list' to display the global todo list.
  4831. T Call `org-todo-list' to display the global todo list, select only
  4832. entries with a specific TODO keyword (the user gets a prompt).
  4833. m Call `org-tags-view' to display headlines with tags matching
  4834. a condition (the user is prompted for the condition).
  4835. M Like `m', but select only TODO entries, no ordinary headlines.
  4836. More commands can be added by configuring the variable
  4837. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  4838. searches can be pre-defined in this way.
  4839. If the current buffer is in Org-mode and visiting a file, you can also
  4840. first press `1' to indicate that the agenda should be temporarily (until the
  4841. next use of \\[org-agenda]) restricted to the current file."
  4842. (interactive "P")
  4843. (catch 'exit
  4844. (let ((restrict-ok (and buffer-file-name (eq major-mode 'org-mode)))
  4845. (custom org-agenda-custom-commands)
  4846. c entry key type string)
  4847. (put 'org-agenda-files 'org-restrict nil)
  4848. (save-window-excursion
  4849. (delete-other-windows)
  4850. (switch-to-buffer-other-window " *Agenda Commands*")
  4851. (erase-buffer)
  4852. (insert
  4853. "Press key for an agenda command:
  4854. --------------------------------
  4855. a Agenda for current week or day
  4856. t List of all TODO entries T Entries with special TODO kwd
  4857. m Match a TAGS query M Like m, but only TODO entries
  4858. C Configure your own agenda commands")
  4859. (while (setq entry (pop custom))
  4860. (setq key (car entry) type (nth 1 entry) string (nth 2 entry))
  4861. (insert (format "\n%-4s%-14s: %s"
  4862. key
  4863. (cond
  4864. ((eq type 'tags) "Tags query")
  4865. ((eq type 'todo) "TODO keyword")
  4866. ((eq type 'tags-tree) "Tags tree")
  4867. ((eq type 'todo-tree) "TODO kwd tree")
  4868. ((eq type 'occur-tree) "Occur tree")
  4869. (t "???"))
  4870. (org-add-props string nil 'face 'org-warning))))
  4871. (goto-char (point-min))
  4872. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  4873. (message "Press key for agenda command%s"
  4874. (if restrict-ok ", or [1] to restrict to current file" ""))
  4875. (setq c (read-char-exclusive))
  4876. (message "")
  4877. (when (equal c ?1)
  4878. (if restrict-ok
  4879. (put 'org-agenda-files 'org-restrict (list buffer-file-name))
  4880. (error "Cannot restrict agenda to current buffer"))
  4881. (message "Press key for agenda command%s"
  4882. (if restrict-ok " (restricted to current file)" ""))
  4883. (setq c (read-char-exclusive))
  4884. (message "")))
  4885. (require 'calendar) ; FIXME: can we avoid this for some commands?
  4886. ;; For example the todo list should not need it (but does...)
  4887. (cond
  4888. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  4889. ((equal c ?a) (call-interactively 'org-agenda-list))
  4890. ((equal c ?t) (call-interactively 'org-todo-list))
  4891. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  4892. ((equal c ?m) (call-interactively 'org-tags-view))
  4893. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  4894. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  4895. (setq type (nth 1 entry) string (nth 2 entry))
  4896. (cond
  4897. ((eq type 'tags)
  4898. (org-tags-view current-prefix-arg string))
  4899. ((eq type 'tags-todo)
  4900. (org-tags-view '(4) string))
  4901. ((eq type 'todo)
  4902. (org-todo-list string))
  4903. ((eq type 'tags-tree)
  4904. (org-check-for-org-mode)
  4905. (org-tags-sparse-tree current-prefix-arg string))
  4906. ((eq type 'todo-tree)
  4907. (org-check-for-org-mode)
  4908. (org-occur (concat "^" outline-regexp "[ \t]*"
  4909. (regexp-quote string) "\\>")))
  4910. ((eq type 'occur-tree)
  4911. (org-check-for-org-mode)
  4912. (org-occur string))
  4913. (t (error "Invalid custom agenda command type %s" type))))
  4914. (t (error "Invalid key"))))))
  4915. (defun org-check-for-org-mode ()
  4916. "Make sure current buffer is in org-mode. Error if not."
  4917. (or (eq major-mode 'org-mode)
  4918. (error "Cannot execute org-mode agenda command on buffer in %s."
  4919. major-mode)))
  4920. (defun org-fit-agenda-window ()
  4921. "Fit the window to the buffer size."
  4922. (and org-fit-agenda-window
  4923. (fboundp 'fit-window-to-buffer)
  4924. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  4925. (/ (frame-height) 2))))
  4926. (defun org-agenda-files (&optional unrestricted)
  4927. "Get the list of agenda files.
  4928. Optional UNRESTRICTED means return the full list even if a restriction
  4929. is currently in place."
  4930. (cond
  4931. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  4932. ((stringp org-agenda-files) (org-read-agenda-file-list))
  4933. ((listp org-agenda-files) org-agenda-files)
  4934. (t (error "Invalid value of `org-agenda-files'"))))
  4935. (defvar org-window-configuration)
  4936. (defun org-edit-agenda-file-list ()
  4937. "Edit the list of agenda files.
  4938. Depending on setup, this either uses customize to edit the variable
  4939. `org-agenda-files', or it visits the file that is holding the list. In the
  4940. latter case, the buffer is set up in a way that saving it automatically kills
  4941. the buffer and restores the previous window configuration."
  4942. (interactive)
  4943. (if (stringp org-agenda-files)
  4944. (let ((cw (current-window-configuration)))
  4945. (find-file org-agenda-files)
  4946. (set (make-local-variable 'org-window-configuration) cw)
  4947. (org-add-hook 'after-save-hook
  4948. (lambda ()
  4949. (set-window-configuration
  4950. (prog1 org-window-configuration
  4951. (kill-buffer (current-buffer))))
  4952. (org-install-agenda-files-menu)
  4953. (message "New agenda file list installed"))
  4954. nil 'local)
  4955. (message (substitute-command-keys
  4956. "Edit list and finish with \\[save-buffer]")))
  4957. (customize-variable 'org-agenda-files)))
  4958. (defun org-store-new-agenda-file-list (list)
  4959. "Set new value for the agenda file list and save it correcly."
  4960. (if (stringp org-agenda-files)
  4961. (let ((f org-agenda-files) b)
  4962. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  4963. (with-temp-file f
  4964. (insert (mapconcat 'identity list "\n") "\n")))
  4965. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  4966. (setq org-agenda-files list)
  4967. (customize-save-variable 'org-agenda-files org-agenda-files))))
  4968. (defun org-read-agenda-file-list ()
  4969. "Read the list of agenda files from a file."
  4970. (when (stringp org-agenda-files)
  4971. (with-temp-buffer
  4972. (insert-file-contents org-agenda-files)
  4973. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  4974. (defvar org-agenda-markers nil
  4975. "List of all currently active markers created by `org-agenda'.")
  4976. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  4977. "Creation time of the last agenda marker.")
  4978. (defun org-agenda-new-marker (&optional pos)
  4979. "Return a new agenda marker.
  4980. Org-mode keeps a list of these markers and resets them when they are
  4981. no longer in use."
  4982. (let ((m (copy-marker (or pos (point)))))
  4983. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  4984. (push m org-agenda-markers)
  4985. m))
  4986. (defun org-agenda-maybe-reset-markers (&optional force)
  4987. "Reset markers created by `org-agenda'. But only if they are old enough."
  4988. (if (or force
  4989. (> (- (time-to-seconds (current-time))
  4990. org-agenda-last-marker-time)
  4991. 5))
  4992. (while org-agenda-markers
  4993. (move-marker (pop org-agenda-markers) nil))))
  4994. (defvar org-agenda-new-buffers nil
  4995. "Buffers created to visit agenda files.")
  4996. (defun org-get-agenda-file-buffer (file)
  4997. "Get a buffer visiting FILE. If the buffer needs to be created, add
  4998. it to the list of buffers which might be released later."
  4999. (let ((buf (find-buffer-visiting file)))
  5000. (if buf
  5001. buf ; just return it
  5002. ;; Make a new buffer and remember it
  5003. (setq buf (find-file-noselect file))
  5004. (if buf (push buf org-agenda-new-buffers))
  5005. buf)))
  5006. (defun org-release-buffers (blist)
  5007. "Release all buffers in list, asking the user for confirmation when needed.
  5008. When a buffer is unmodified, it is just killed. When modified, it is saved
  5009. \(if the user agrees) and then killed."
  5010. (let (buf file)
  5011. (while (setq buf (pop blist))
  5012. (setq file (buffer-file-name buf))
  5013. (when (and (buffer-modified-p buf)
  5014. file
  5015. (y-or-n-p (format "Save file %s? " file)))
  5016. (with-current-buffer buf (save-buffer)))
  5017. (kill-buffer buf))))
  5018. (defvar org-respect-restriction nil) ; Dynamically-scoped param.
  5019. (defun org-timeline (&optional include-all keep-modes)
  5020. "Show a time-sorted view of the entries in the current org file.
  5021. Only entries with a time stamp of today or later will be listed. With
  5022. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  5023. under the current date.
  5024. If the buffer contains an active region, only check the region for
  5025. dates."
  5026. (interactive "P")
  5027. (require 'calendar)
  5028. (org-agenda-maybe-reset-markers 'force)
  5029. (org-compile-prefix-format org-timeline-prefix-format)
  5030. (let* ((dopast t)
  5031. (dotodo include-all)
  5032. (doclosed org-agenda-show-log)
  5033. (org-agenda-keep-modes keep-modes)
  5034. (entry buffer-file-name)
  5035. (org-agenda-files (list buffer-file-name))
  5036. (date (calendar-current-date))
  5037. (win (selected-window))
  5038. (pos1 (point))
  5039. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  5040. (end (if (org-region-active-p) (region-end) (point-max)))
  5041. (day-numbers (org-get-all-dates beg end 'no-ranges
  5042. t doclosed)) ; always include today
  5043. (today (time-to-days (current-time)))
  5044. (org-respect-restriction t)
  5045. (past t)
  5046. args
  5047. s e rtn d)
  5048. (setq org-agenda-redo-command
  5049. (list 'progn
  5050. (list 'switch-to-buffer-other-window (current-buffer))
  5051. (list 'org-timeline (list 'quote include-all) t)))
  5052. (if (not dopast)
  5053. ;; Remove past dates from the list of dates.
  5054. (setq day-numbers (delq nil (mapcar (lambda(x)
  5055. (if (>= x today) x nil))
  5056. day-numbers))))
  5057. (switch-to-buffer-other-window
  5058. (get-buffer-create org-agenda-buffer-name))
  5059. (setq buffer-read-only nil)
  5060. (erase-buffer)
  5061. (org-agenda-mode) (setq buffer-read-only nil)
  5062. (set (make-local-variable 'org-agenda-type) 'timeline)
  5063. (if doclosed (push :closed args))
  5064. (push :timestamp args)
  5065. (if dotodo (push :todo args))
  5066. (while (setq d (pop day-numbers))
  5067. (if (and (>= d today)
  5068. dopast
  5069. past)
  5070. (progn
  5071. (setq past nil)
  5072. (insert (make-string 79 ?-) "\n")))
  5073. (setq date (calendar-gregorian-from-absolute d))
  5074. (setq s (point))
  5075. (setq rtn (apply 'org-agenda-get-day-entries
  5076. entry date args))
  5077. (if (or rtn (equal d today))
  5078. (progn
  5079. (insert (calendar-day-name date) " "
  5080. (number-to-string (extract-calendar-day date)) " "
  5081. (calendar-month-name (extract-calendar-month date)) " "
  5082. (number-to-string (extract-calendar-year date)) "\n")
  5083. (put-text-property s (1- (point)) 'face
  5084. 'org-level-3)
  5085. (if (equal d today)
  5086. (put-text-property s (1- (point)) 'org-today t))
  5087. (insert (org-finalize-agenda-entries rtn) "\n")
  5088. (put-text-property s (1- (point)) 'day d))))
  5089. (goto-char (point-min))
  5090. (setq buffer-read-only t)
  5091. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  5092. (point-min)))
  5093. (when (not org-select-timeline-window)
  5094. (select-window win)
  5095. (goto-char pos1))))
  5096. ;;;###autoload
  5097. (defun org-agenda-list (&optional include-all start-day ndays keep-modes)
  5098. "Produce a weekly view from all files in variable `org-agenda-files'.
  5099. The view will be for the current week, but from the overview buffer you
  5100. will be able to go to other weeks.
  5101. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  5102. also be shown, under the current date.
  5103. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  5104. on the days are also shown. See the variable `org-log-done' for how
  5105. to turn on logging.
  5106. START-DAY defaults to TODAY, or to the most recent match for the weekday
  5107. given in `org-agenda-start-on-weekday'.
  5108. NDAYS defaults to `org-agenda-ndays'."
  5109. (interactive "P")
  5110. (org-agenda-maybe-reset-markers 'force)
  5111. (org-compile-prefix-format org-agenda-prefix-format)
  5112. (require 'calendar)
  5113. (let* ((org-agenda-start-on-weekday
  5114. (if (or (equal ndays 1)
  5115. (and (null ndays) (equal 1 org-agenda-ndays)))
  5116. nil org-agenda-start-on-weekday))
  5117. (org-agenda-keep-modes keep-modes)
  5118. (thefiles (org-agenda-files))
  5119. (files thefiles)
  5120. (win (selected-window))
  5121. (today (time-to-days (current-time)))
  5122. (sd (or start-day today))
  5123. (start (if (or (null org-agenda-start-on-weekday)
  5124. (< org-agenda-ndays 7))
  5125. sd
  5126. (let* ((nt (calendar-day-of-week
  5127. (calendar-gregorian-from-absolute sd)))
  5128. (n1 org-agenda-start-on-weekday)
  5129. (d (- nt n1)))
  5130. (- sd (+ (if (< d 0) 7 0) d)))))
  5131. (day-numbers (list start))
  5132. (inhibit-redisplay t)
  5133. s e rtn rtnall file date d start-pos end-pos todayp nd)
  5134. (setq org-agenda-redo-command
  5135. (list 'org-agenda-list (list 'quote include-all) start-day ndays t))
  5136. ;; Make the list of days
  5137. (setq ndays (or ndays org-agenda-ndays)
  5138. nd ndays)
  5139. (while (> ndays 1)
  5140. (push (1+ (car day-numbers)) day-numbers)
  5141. (setq ndays (1- ndays)))
  5142. (setq day-numbers (nreverse day-numbers))
  5143. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  5144. (progn
  5145. (delete-other-windows)
  5146. (switch-to-buffer-other-window
  5147. (get-buffer-create org-agenda-buffer-name))))
  5148. (setq buffer-read-only nil)
  5149. (erase-buffer)
  5150. (org-agenda-mode) (setq buffer-read-only nil)
  5151. (set (make-local-variable 'org-agenda-type) 'agenda)
  5152. (set (make-local-variable 'starting-day) (car day-numbers))
  5153. (set (make-local-variable 'include-all-loc) include-all)
  5154. (when (and (or include-all org-agenda-include-all-todo)
  5155. (member today day-numbers))
  5156. (setq files thefiles
  5157. rtnall nil)
  5158. (while (setq file (pop files))
  5159. (catch 'nextfile
  5160. (org-check-agenda-file file)
  5161. (setq date (calendar-gregorian-from-absolute today)
  5162. rtn (org-agenda-get-day-entries
  5163. file date :todo))
  5164. (setq rtnall (append rtnall rtn))))
  5165. (when rtnall
  5166. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  5167. (add-text-properties (point-min) (1- (point))
  5168. (list 'face 'org-level-3))
  5169. (insert (org-finalize-agenda-entries rtnall) "\n")))
  5170. (while (setq d (pop day-numbers))
  5171. (setq date (calendar-gregorian-from-absolute d)
  5172. s (point))
  5173. (if (or (setq todayp (= d today))
  5174. (and (not start-pos) (= d sd)))
  5175. (setq start-pos (point))
  5176. (if (and start-pos (not end-pos))
  5177. (setq end-pos (point))))
  5178. (setq files thefiles
  5179. rtnall nil)
  5180. (while (setq file (pop files))
  5181. (catch 'nextfile
  5182. (org-check-agenda-file file)
  5183. (if org-agenda-show-log
  5184. (setq rtn (org-agenda-get-day-entries
  5185. file date
  5186. :deadline :scheduled :timestamp :closed))
  5187. (setq rtn (org-agenda-get-day-entries
  5188. file date
  5189. :deadline :scheduled :timestamp)))
  5190. (setq rtnall (append rtnall rtn))))
  5191. (if org-agenda-include-diary
  5192. (progn
  5193. (require 'diary-lib)
  5194. (setq rtn (org-get-entries-from-diary date))
  5195. (setq rtnall (append rtnall rtn))))
  5196. (if (or rtnall org-agenda-show-all-dates)
  5197. (progn
  5198. (insert (format "%-9s %2d %s %4d\n"
  5199. (calendar-day-name date)
  5200. (extract-calendar-day date)
  5201. (calendar-month-name (extract-calendar-month date))
  5202. (extract-calendar-year date)))
  5203. (put-text-property s (1- (point)) 'face
  5204. 'org-level-3)
  5205. (if rtnall (insert
  5206. (org-finalize-agenda-entries
  5207. (org-agenda-add-time-grid-maybe
  5208. rtnall nd todayp))
  5209. "\n"))
  5210. (put-text-property s (1- (point)) 'day d))))
  5211. (goto-char (point-min))
  5212. (setq buffer-read-only t)
  5213. (org-fit-agenda-window)
  5214. (unless (and (pos-visible-in-window-p (point-min))
  5215. (pos-visible-in-window-p (point-max)))
  5216. (goto-char (1- (point-max)))
  5217. (recenter -1)
  5218. (if (not (pos-visible-in-window-p (or start-pos 1)))
  5219. (progn
  5220. (goto-char (or start-pos 1))
  5221. (recenter 1))))
  5222. (goto-char (or start-pos 1))
  5223. (if (not org-select-agenda-window) (select-window win))
  5224. (message "")))
  5225. (defvar org-select-this-todo-keyword nil)
  5226. ;;;###autoload
  5227. (defun org-todo-list (arg &optional keep-modes)
  5228. "Show all TODO entries from all agenda file in a single list.
  5229. The prefix arg can be used to select a specific TODO keyword and limit
  5230. the list to these. When using \\[universal-argument], you will be prompted
  5231. for a keyword. A numeric prefix directly selects the Nth keyword in
  5232. `org-todo-keywords'."
  5233. (interactive "P")
  5234. (org-agenda-maybe-reset-markers 'force)
  5235. (org-compile-prefix-format org-agenda-prefix-format)
  5236. (let* ((org-agenda-keep-modes keep-modes)
  5237. (today (time-to-days (current-time)))
  5238. (date (calendar-gregorian-from-absolute today))
  5239. (win (selected-window))
  5240. (kwds org-todo-keywords)
  5241. (completion-ignore-case t)
  5242. (org-select-this-todo-keyword
  5243. (if (stringp arg) arg
  5244. (and arg (integerp arg) (> arg 0)
  5245. (nth (1- arg) org-todo-keywords))))
  5246. rtn rtnall files file pos)
  5247. (when (equal arg '(4))
  5248. (setq org-select-this-todo-keyword
  5249. (completing-read "Keyword: " (mapcar 'list org-todo-keywords)
  5250. nil t)))
  5251. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  5252. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  5253. (progn
  5254. (delete-other-windows)
  5255. (switch-to-buffer-other-window
  5256. (get-buffer-create org-agenda-buffer-name))))
  5257. (setq buffer-read-only nil)
  5258. (erase-buffer)
  5259. (org-agenda-mode) (setq buffer-read-only nil)
  5260. (set (make-local-variable 'org-agenda-type) 'todo)
  5261. (set (make-local-variable 'last-arg) arg)
  5262. (set (make-local-variable 'org-todo-keywords) kwds)
  5263. (set (make-local-variable 'org-agenda-redo-command)
  5264. '(org-todo-list (or current-prefix-arg last-arg) t))
  5265. (setq files (org-agenda-files)
  5266. rtnall nil)
  5267. (while (setq file (pop files))
  5268. (catch 'nextfile
  5269. (org-check-agenda-file file)
  5270. (setq rtn (org-agenda-get-day-entries file date :todo))
  5271. (setq rtnall (append rtnall rtn))))
  5272. (insert "Global list of TODO items of type: ")
  5273. (add-text-properties (point-min) (1- (point))
  5274. (list 'face 'org-level-3))
  5275. (setq pos (point))
  5276. (insert (or org-select-this-todo-keyword "ALL") "\n")
  5277. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  5278. (setq pos (point))
  5279. (insert
  5280. "Available with `N r': (0)ALL "
  5281. (let ((n 0))
  5282. (mapconcat (lambda (x)
  5283. (format "(%d)%s" (setq n (1+ n)) x))
  5284. org-todo-keywords " "))
  5285. "\n")
  5286. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  5287. (when rtnall
  5288. (insert (org-finalize-agenda-entries rtnall) "\n"))
  5289. (goto-char (point-min))
  5290. (setq buffer-read-only t)
  5291. (org-fit-agenda-window)
  5292. (if (not org-select-agenda-window) (select-window win))))
  5293. (defun org-check-agenda-file (file)
  5294. "Make sure FILE exists. If not, ask user what to do."
  5295. (when (not (file-exists-p file))
  5296. (message "non-existent file %s. [R]emove from list or [A]bort?"
  5297. (abbreviate-file-name file))
  5298. (let ((r (downcase (read-char-exclusive))))
  5299. (cond
  5300. ((equal r ?r)
  5301. (org-remove-file file)
  5302. (throw 'nextfile t))
  5303. (t (error "Abort"))))))
  5304. (defun org-agenda-check-type (error &rest types)
  5305. "Check if agenda buffer is of allowed type.
  5306. If ERROR is non-nil, throw an error, otherwise just return nil."
  5307. (if (memq org-agenda-type types)
  5308. t
  5309. (if error
  5310. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  5311. nil)))
  5312. (defun org-agenda-quit ()
  5313. "Exit agenda by removing the window or the buffer."
  5314. (interactive)
  5315. (let ((buf (current-buffer)))
  5316. (if (not (one-window-p)) (delete-window))
  5317. (kill-buffer buf)
  5318. (org-agenda-maybe-reset-markers 'force)))
  5319. (defun org-agenda-exit ()
  5320. "Exit agenda by removing the window or the buffer.
  5321. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  5322. Org-mode buffers visited directly by the user will not be touched."
  5323. (interactive)
  5324. (org-release-buffers org-agenda-new-buffers)
  5325. (setq org-agenda-new-buffers nil)
  5326. (org-agenda-quit))
  5327. (defun org-agenda-redo ()
  5328. "Rebuild Agenda.
  5329. When this is the global TODO list, a prefix argument will be interpreted."
  5330. (interactive)
  5331. (message "Rebuilding agenda buffer...")
  5332. (eval org-agenda-redo-command)
  5333. (message "Rebuilding agenda buffer...done"))
  5334. (defun org-agenda-goto-today ()
  5335. "Go to today."
  5336. (interactive)
  5337. (org-agenda-check-type t 'timeline 'agenda)
  5338. (if (boundp 'starting-day)
  5339. (let ((cmd (car org-agenda-redo-command))
  5340. (iall (nth 1 org-agenda-redo-command))
  5341. (nday (nth 3 org-agenda-redo-command))
  5342. (keep (nth 4 org-agenda-redo-command)))
  5343. (eval (list cmd iall nil nday keep)))
  5344. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  5345. (point-min)))))
  5346. (defun org-agenda-later (arg)
  5347. "Go forward in time by `org-agenda-ndays' days.
  5348. With prefix ARG, go forward that many times `org-agenda-ndays'."
  5349. (interactive "p")
  5350. (org-agenda-check-type t 'agenda)
  5351. (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
  5352. (+ starting-day (* arg org-agenda-ndays)) nil t))
  5353. (defun org-agenda-earlier (arg)
  5354. "Go back in time by `org-agenda-ndays' days.
  5355. With prefix ARG, go back that many times `org-agenda-ndays'."
  5356. (interactive "p")
  5357. (org-agenda-check-type t 'agenda)
  5358. (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
  5359. (- starting-day (* arg org-agenda-ndays)) nil t))
  5360. (defun org-agenda-week-view ()
  5361. "Switch to weekly view for agenda."
  5362. (interactive)
  5363. (org-agenda-check-type t 'agenda)
  5364. (setq org-agenda-ndays 7)
  5365. (org-agenda-list include-all-loc
  5366. (or (get-text-property (point) 'day)
  5367. starting-day)
  5368. nil t)
  5369. (org-agenda-set-mode-name)
  5370. (message "Switched to week view"))
  5371. (defun org-agenda-day-view ()
  5372. "Switch to daily view for agenda."
  5373. (interactive)
  5374. (org-agenda-check-type t 'agenda)
  5375. (setq org-agenda-ndays 1)
  5376. (org-agenda-list include-all-loc
  5377. (or (get-text-property (point) 'day)
  5378. starting-day)
  5379. nil t)
  5380. (org-agenda-set-mode-name)
  5381. (message "Switched to day view"))
  5382. (defun org-agenda-next-date-line (&optional arg)
  5383. "Jump to the next line indicating a date in agenda buffer."
  5384. (interactive "p")
  5385. (org-agenda-check-type t 'agenda 'timeline)
  5386. (beginning-of-line 1)
  5387. (if (looking-at "^\\S-") (forward-char 1))
  5388. (if (not (re-search-forward "^\\S-" nil t arg))
  5389. (progn
  5390. (backward-char 1)
  5391. (error "No next date after this line in this buffer")))
  5392. (goto-char (match-beginning 0)))
  5393. (defun org-agenda-previous-date-line (&optional arg)
  5394. "Jump to the previous line indicating a date in agenda buffer."
  5395. (interactive "p")
  5396. (org-agenda-check-type t 'agenda 'timeline)
  5397. (beginning-of-line 1)
  5398. (if (not (re-search-backward "^\\S-" nil t arg))
  5399. (error "No previous date before this line in this buffer")))
  5400. ;; Initialize the highlight
  5401. (defvar org-hl (org-make-overlay 1 1))
  5402. (org-overlay-put org-hl 'face 'highlight)
  5403. (defun org-highlight (begin end &optional buffer)
  5404. "Highlight a region with overlay."
  5405. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  5406. org-hl begin end (or buffer (current-buffer))))
  5407. (defun org-unhighlight ()
  5408. "Detach overlay INDEX."
  5409. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  5410. (defun org-agenda-follow-mode ()
  5411. "Toggle follow mode in an agenda buffer."
  5412. (interactive)
  5413. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  5414. (org-agenda-set-mode-name)
  5415. (message "Follow mode is %s"
  5416. (if org-agenda-follow-mode "on" "off")))
  5417. (defun org-agenda-log-mode ()
  5418. "Toggle log mode in an agenda buffer."
  5419. (interactive)
  5420. (org-agenda-check-type t 'agenda 'timeline)
  5421. (setq org-agenda-show-log (not org-agenda-show-log))
  5422. (org-agenda-set-mode-name)
  5423. (org-agenda-redo)
  5424. (message "Log mode is %s"
  5425. (if org-agenda-show-log "on" "off")))
  5426. (defun org-agenda-toggle-diary ()
  5427. "Toggle diary inclusion in an agenda buffer."
  5428. (interactive)
  5429. (org-agenda-check-type t 'agenda)
  5430. (setq org-agenda-include-diary (not org-agenda-include-diary))
  5431. (org-agenda-redo)
  5432. (org-agenda-set-mode-name)
  5433. (message "Diary inclusion turned %s"
  5434. (if org-agenda-include-diary "on" "off")))
  5435. (defun org-agenda-toggle-time-grid ()
  5436. "Toggle time grid in an agenda buffer."
  5437. (interactive)
  5438. (org-agenda-check-type t 'agenda)
  5439. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  5440. (org-agenda-redo)
  5441. (org-agenda-set-mode-name)
  5442. (message "Time-grid turned %s"
  5443. (if org-agenda-use-time-grid "on" "off")))
  5444. (defun org-agenda-set-mode-name ()
  5445. "Set the mode name to indicate all the small mode settings."
  5446. (setq mode-name
  5447. (concat "Org-Agenda"
  5448. (if (equal org-agenda-ndays 1) " Day" "")
  5449. (if (equal org-agenda-ndays 7) " Week" "")
  5450. (if org-agenda-follow-mode " Follow" "")
  5451. (if org-agenda-include-diary " Diary" "")
  5452. (if org-agenda-use-time-grid " Grid" "")
  5453. (if org-agenda-show-log " Log" "")))
  5454. (force-mode-line-update))
  5455. (defun org-agenda-post-command-hook ()
  5456. (and (eolp) (not (bolp)) (backward-char 1))
  5457. (if (and org-agenda-follow-mode
  5458. (get-text-property (point) 'org-marker))
  5459. (org-agenda-show)))
  5460. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  5461. (defun org-get-entries-from-diary (date)
  5462. "Get the (Emacs Calendar) diary entries for DATE."
  5463. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  5464. (diary-display-hook '(fancy-diary-display))
  5465. (list-diary-entries-hook
  5466. (cons 'org-diary-default-entry list-diary-entries-hook))
  5467. (diary-file-name-prefix-function nil) ; turn this feature off
  5468. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  5469. entries
  5470. (org-disable-agenda-to-diary t))
  5471. (save-excursion
  5472. (save-window-excursion
  5473. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  5474. (if (not (get-buffer fancy-diary-buffer))
  5475. (setq entries nil)
  5476. (with-current-buffer fancy-diary-buffer
  5477. (setq buffer-read-only nil)
  5478. (if (= (point-max) 1)
  5479. ;; No entries
  5480. (setq entries nil)
  5481. ;; Omit the date and other unnecessary stuff
  5482. (org-agenda-cleanup-fancy-diary)
  5483. ;; Add prefix to each line and extend the text properties
  5484. (if (= (point-max) 1)
  5485. (setq entries nil)
  5486. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  5487. (set-buffer-modified-p nil)
  5488. (kill-buffer fancy-diary-buffer)))
  5489. (when entries
  5490. (setq entries (org-split-string entries "\n"))
  5491. (setq entries
  5492. (mapcar
  5493. (lambda (x)
  5494. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  5495. ;; Extend the text properties to the beginning of the line
  5496. (org-add-props x (text-properties-at (1- (length x)) x)))
  5497. entries)))))
  5498. (defun org-agenda-cleanup-fancy-diary ()
  5499. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  5500. This gets rid of the date, the underline under the date, and
  5501. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  5502. date. It also removes lines that contain only whitespace."
  5503. (goto-char (point-min))
  5504. (if (looking-at ".*?:[ \t]*")
  5505. (progn
  5506. (replace-match "")
  5507. (re-search-forward "\n=+$" nil t)
  5508. (replace-match "")
  5509. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  5510. (re-search-forward "\n=+$" nil t)
  5511. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  5512. (goto-char (point-min))
  5513. (while (re-search-forward "^ +\n" nil t)
  5514. (replace-match ""))
  5515. (goto-char (point-min))
  5516. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  5517. (replace-match "")))
  5518. ;; Make sure entries from the diary have the right text properties.
  5519. (eval-after-load "diary-lib"
  5520. '(if (boundp 'diary-modify-entry-list-string-function)
  5521. ;; We can rely on the hook, nothing to do
  5522. nil
  5523. ;; Hook not avaiable, must use advice to make this work
  5524. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  5525. "Make the position visible."
  5526. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  5527. (stringp string)
  5528. buffer-file-name)
  5529. (setq string (org-modify-diary-entry-string string))))))
  5530. (defun org-modify-diary-entry-string (string)
  5531. "Add text properties to string, allowing org-mode to act on it."
  5532. (org-add-props string nil
  5533. 'mouse-face 'highlight
  5534. 'keymap org-agenda-keymap
  5535. 'help-echo (format "mouse-2 or RET jump to diary file %s"
  5536. (abbreviate-file-name buffer-file-name))
  5537. 'org-agenda-diary-link t
  5538. 'org-marker (org-agenda-new-marker (point-at-bol))))
  5539. (defun org-diary-default-entry ()
  5540. "Add a dummy entry to the diary.
  5541. Needed to avoid empty dates which mess up holiday display."
  5542. ;; Catch the error if dealing with the new add-to-diary-alist
  5543. (when org-disable-agenda-to-diary
  5544. (condition-case nil
  5545. (add-to-diary-list original-date "Org-mode dummy" "")
  5546. (error
  5547. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  5548. (defun org-cycle-agenda-files ()
  5549. "Cycle through the files in `org-agenda-files'.
  5550. If the current buffer visits an agenda file, find the next one in the list.
  5551. If the current buffer does not, find the first agenda file."
  5552. (interactive)
  5553. (let* ((fs (org-agenda-files t))
  5554. (files (append fs (list (car fs))))
  5555. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  5556. file)
  5557. (unless files (error "No agenda files"))
  5558. (catch 'exit
  5559. (while (setq file (pop files))
  5560. (if (equal (file-truename file) tcf)
  5561. (when (car files)
  5562. (find-file (car files))
  5563. (throw 'exit t))))
  5564. (find-file (car fs)))))
  5565. (defun org-agenda-file-to-end ()
  5566. "Move/add the current file to the end of the agenda file list.
  5567. If the file is not present in the list, it is appended to the list. If it is
  5568. present, it is moved there."
  5569. (interactive)
  5570. (org-agenda-file-to-front 'to-end))
  5571. (defun org-agenda-file-to-front (&optional to-end)
  5572. "Move/add the current file to the top of the agenda file list.
  5573. If the file is not present in the list, it is added to the front. If it is
  5574. present, it is moved there. With optional argument TO-END, add/move to the
  5575. end of the list."
  5576. (interactive "P")
  5577. (let ((file-alist (mapcar (lambda (x)
  5578. (cons (file-truename x) x))
  5579. (org-agenda-files t)))
  5580. (ctf (file-truename buffer-file-name))
  5581. x had)
  5582. (setq x (assoc ctf file-alist) had x)
  5583. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  5584. (if to-end
  5585. (setq file-alist (append (delq x file-alist) (list x)))
  5586. (setq file-alist (cons x (delq x file-alist))))
  5587. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  5588. (org-install-agenda-files-menu)
  5589. (message "File %s to %s of agenda file list"
  5590. (if had "moved" "added") (if to-end "end" "front"))))
  5591. (defun org-remove-file (&optional file)
  5592. "Remove current file from the list of files in variable `org-agenda-files'.
  5593. These are the files which are being checked for agenda entries.
  5594. Optional argument FILE means, use this file instead of the current."
  5595. (interactive)
  5596. (let* ((file (or file buffer-file-name))
  5597. (true-file (file-truename file))
  5598. (afile (abbreviate-file-name file))
  5599. (files (delq nil (mapcar
  5600. (lambda (x)
  5601. (if (equal true-file
  5602. (file-truename x))
  5603. nil x))
  5604. (org-agenda-files t)))))
  5605. (if (not (= (length files) (length (org-agenda-files t))))
  5606. (progn
  5607. (org-store-new-agenda-file-list files)
  5608. (org-install-agenda-files-menu)
  5609. (message "Removed file: %s" afile))
  5610. (message "File was not in list: %s" afile))))
  5611. (defun org-file-menu-entry (file)
  5612. (vector file (list 'find-file file) t))
  5613. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive)
  5614. "Return a list of all relevant day numbers from BEG to END buffer positions.
  5615. If NO-RANGES is non-nil, include only the start and end dates of a range,
  5616. not every single day in the range. If FORCE-TODAY is non-nil, make
  5617. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  5618. inactive time stamps (those in square brackets) are included."
  5619. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  5620. dates date day day1 day2 ts1 ts2)
  5621. (if force-today
  5622. (setq dates (list (time-to-days (current-time)))))
  5623. (save-excursion
  5624. (goto-char beg)
  5625. (while (re-search-forward re end t)
  5626. (setq day (time-to-days (org-time-string-to-time
  5627. (substring (match-string 1) 0 10))))
  5628. (or (memq day dates) (push day dates)))
  5629. (unless no-ranges
  5630. (goto-char beg)
  5631. (while (re-search-forward org-tr-regexp end t)
  5632. (setq ts1 (substring (match-string 1) 0 10)
  5633. ts2 (substring (match-string 2) 0 10)
  5634. day1 (time-to-days (org-time-string-to-time ts1))
  5635. day2 (time-to-days (org-time-string-to-time ts2)))
  5636. (while (< (setq day1 (1+ day1)) day2)
  5637. (or (memq day1 dates) (push day1 dates)))))
  5638. (sort dates '<))))
  5639. ;;;###autoload
  5640. (defun org-diary (&rest args)
  5641. "Return diary information from org-files.
  5642. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  5643. It accesses org files and extracts information from those files to be
  5644. listed in the diary. The function accepts arguments specifying what
  5645. items should be listed. The following arguments are allowed:
  5646. :timestamp List the headlines of items containing a date stamp or
  5647. date range matching the selected date. Deadlines will
  5648. also be listed, on the expiration day.
  5649. :deadline List any deadlines past due, or due within
  5650. `org-deadline-warning-days'. The listing occurs only
  5651. in the diary for *today*, not at any other date. If
  5652. an entry is marked DONE, it is no longer listed.
  5653. :scheduled List all items which are scheduled for the given date.
  5654. The diary for *today* also contains items which were
  5655. scheduled earlier and are not yet marked DONE.
  5656. :todo List all TODO items from the org-file. This may be a
  5657. long list - so this is not turned on by default.
  5658. Like deadlines, these entries only show up in the
  5659. diary for *today*, not at any other date.
  5660. The call in the diary file should look like this:
  5661. &%%(org-diary) ~/path/to/some/orgfile.org
  5662. Use a separate line for each org file to check. Or, if you omit the file name,
  5663. all files listed in `org-agenda-files' will be checked automatically:
  5664. &%%(org-diary)
  5665. If you don't give any arguments (as in the example above), the default
  5666. arguments (:deadline :scheduled :timestamp) are used. So the example above may
  5667. also be written as
  5668. &%%(org-diary :deadline :timestamp :scheduled)
  5669. The function expects the lisp variables `entry' and `date' to be provided
  5670. by the caller, because this is how the calendar works. Don't use this
  5671. function from a program - use `org-agenda-get-day-entries' instead."
  5672. (org-agenda-maybe-reset-markers)
  5673. (org-compile-prefix-format org-agenda-prefix-format)
  5674. (setq args (or args '(:deadline :scheduled :timestamp)))
  5675. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  5676. (list entry)
  5677. (org-agenda-files t)))
  5678. file rtn results)
  5679. ;; If this is called during org-agenda, don't return any entries to
  5680. ;; the calendar. Org Agenda will list these entries itself.
  5681. (if org-disable-agenda-to-diary (setq files nil))
  5682. (while (setq file (pop files))
  5683. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  5684. (setq results (append results rtn)))
  5685. (if results
  5686. (concat (org-finalize-agenda-entries results) "\n"))))
  5687. (defvar org-category-table nil)
  5688. (defun org-get-category-table ()
  5689. "Get the table of categories and positions in current buffer."
  5690. (let (tbl)
  5691. (save-excursion
  5692. (goto-char (point-min))
  5693. (while (re-search-forward "\\(^\\|\r\\)#\\+CATEGORY:[ \t]*\\(.*\\)" nil t)
  5694. (push (cons (point) (org-trim (match-string 2))) tbl)))
  5695. tbl))
  5696. (defun org-get-category (&optional pos)
  5697. "Get the category applying to position POS."
  5698. (if (not org-category-table)
  5699. (cond
  5700. ((null org-category)
  5701. (setq org-category
  5702. (if buffer-file-name
  5703. (file-name-sans-extension
  5704. (file-name-nondirectory buffer-file-name))
  5705. "???")))
  5706. ((symbolp org-category) (symbol-name org-category))
  5707. (t org-category))
  5708. (let ((tbl org-category-table)
  5709. (pos (or pos (point))))
  5710. (while (and tbl (> (caar tbl) pos))
  5711. (pop tbl))
  5712. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  5713. org-category-table))))))
  5714. (defun org-agenda-get-day-entries (file date &rest args)
  5715. "Does the work for `org-diary' and `org-agenda'.
  5716. FILE is the path to a file to be checked for entries. DATE is date like
  5717. the one returned by `calendar-current-date'. ARGS are symbols indicating
  5718. which kind of entries should be extracted. For details about these, see
  5719. the documentation of `org-diary'."
  5720. (setq args (or args '(:deadline :scheduled :timestamp)))
  5721. (let* ((org-startup-with-deadline-check nil)
  5722. (org-startup-folded nil)
  5723. (org-startup-align-all-tables nil)
  5724. (buffer (if (file-exists-p file)
  5725. (org-get-agenda-file-buffer file)
  5726. (error "No such file %s" file)))
  5727. arg results rtn)
  5728. (if (not buffer)
  5729. ;; If file does not exist, make sure an error message ends up in diary
  5730. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  5731. (with-current-buffer buffer
  5732. (unless (eq major-mode 'org-mode)
  5733. (error "Agenda file %s is not in `org-mode'" file))
  5734. (setq org-category-table (org-get-category-table))
  5735. (let ((case-fold-search nil))
  5736. (save-excursion
  5737. (save-restriction
  5738. (if org-respect-restriction
  5739. (if (org-region-active-p)
  5740. ;; Respect a region to restrict search
  5741. (narrow-to-region (region-beginning) (region-end)))
  5742. ;; If we work for the calendar or many files,
  5743. ;; get rid of any restriction
  5744. (widen))
  5745. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  5746. (while (setq arg (pop args))
  5747. (cond
  5748. ((and (eq arg :todo)
  5749. (equal date (calendar-current-date)))
  5750. (setq rtn (org-agenda-get-todos))
  5751. (setq results (append results rtn)))
  5752. ((eq arg :timestamp)
  5753. (setq rtn (org-agenda-get-blocks))
  5754. (setq results (append results rtn))
  5755. (setq rtn (org-agenda-get-timestamps))
  5756. (setq results (append results rtn)))
  5757. ((eq arg :scheduled)
  5758. (setq rtn (org-agenda-get-scheduled))
  5759. (setq results (append results rtn)))
  5760. ((eq arg :closed)
  5761. (setq rtn (org-agenda-get-closed))
  5762. (setq results (append results rtn)))
  5763. ((and (eq arg :deadline)
  5764. (equal date (calendar-current-date)))
  5765. (setq rtn (org-agenda-get-deadlines))
  5766. (setq results (append results rtn))))))))
  5767. results))))
  5768. (defun org-entry-is-done-p ()
  5769. "Is the current entry marked DONE?"
  5770. (save-excursion
  5771. (and (re-search-backward "[\r\n]\\*" nil t)
  5772. (looking-at org-nl-done-regexp))))
  5773. (defun org-at-date-range-p ()
  5774. "Is the cursor inside a date range?"
  5775. (interactive)
  5776. (save-excursion
  5777. (catch 'exit
  5778. (let ((pos (point)))
  5779. (skip-chars-backward "^<\r\n")
  5780. (skip-chars-backward "<")
  5781. (and (looking-at org-tr-regexp)
  5782. (>= (match-end 0) pos)
  5783. (throw 'exit t))
  5784. (skip-chars-backward "^<\r\n")
  5785. (skip-chars-backward "<")
  5786. (and (looking-at org-tr-regexp)
  5787. (>= (match-end 0) pos)
  5788. (throw 'exit t)))
  5789. nil)))
  5790. (defun org-agenda-get-todos ()
  5791. "Return the TODO information for agenda display."
  5792. (let* ((props (list 'face nil
  5793. 'done-face 'org-done
  5794. 'org-not-done-regexp org-not-done-regexp
  5795. 'mouse-face 'highlight
  5796. 'keymap org-agenda-keymap
  5797. 'help-echo
  5798. (format "mouse-2 or RET jump to org file %s"
  5799. (abbreviate-file-name buffer-file-name))))
  5800. (regexp (concat "[\n\r]\\*+ *\\("
  5801. (if org-select-this-todo-keyword
  5802. (concat "\\<\\(" org-select-this-todo-keyword
  5803. "\\)\\>")
  5804. org-not-done-regexp)
  5805. "[^\n\r]*\\)"))
  5806. (sched-re (concat ".*\n.*?" org-scheduled-time-regexp))
  5807. marker priority category tags
  5808. ee txt)
  5809. (goto-char (point-min))
  5810. (while (re-search-forward regexp nil t)
  5811. (when (not (and org-agenda-todo-ignore-scheduled
  5812. (save-match-data (looking-at sched-re))))
  5813. (goto-char (match-beginning 1))
  5814. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  5815. category (org-get-category)
  5816. tags (org-get-tags-at (point))
  5817. txt (org-format-agenda-item "" (match-string 1) category tags)
  5818. priority
  5819. (+ (org-get-priority txt)
  5820. (if org-todo-kwd-priority-p
  5821. (- org-todo-kwd-max-priority -2
  5822. (length
  5823. (member (match-string 2) org-todo-keywords)))
  5824. 1)))
  5825. (org-add-props txt props
  5826. 'org-marker marker 'org-hd-marker marker
  5827. 'priority priority 'category category)
  5828. (push txt ee)
  5829. (if org-agenda-todo-list-sublevels
  5830. (goto-char (match-end 1))
  5831. (org-end-of-subtree 'invisible))))
  5832. (nreverse ee)))
  5833. (defconst org-agenda-no-heading-message
  5834. "No heading for this item in buffer or region.")
  5835. (defun org-agenda-get-timestamps ()
  5836. "Return the date stamp information for agenda display."
  5837. (let* ((props (list 'face nil
  5838. 'org-not-done-regexp org-not-done-regexp
  5839. 'mouse-face 'highlight
  5840. 'keymap org-agenda-keymap
  5841. 'help-echo
  5842. (format "mouse-2 or RET jump to org file %s"
  5843. (abbreviate-file-name buffer-file-name))))
  5844. (regexp (regexp-quote
  5845. (substring
  5846. (format-time-string
  5847. (car org-time-stamp-formats)
  5848. (apply 'encode-time ; DATE bound by calendar
  5849. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  5850. 0 11)))
  5851. marker hdmarker deadlinep scheduledp donep tmp priority category
  5852. ee txt timestr tags)
  5853. (goto-char (point-min))
  5854. (while (re-search-forward regexp nil t)
  5855. (if (not (save-match-data (org-at-date-range-p)))
  5856. (progn
  5857. (setq marker (org-agenda-new-marker (match-beginning 0))
  5858. category (org-get-category (match-beginning 0))
  5859. tmp (buffer-substring (max (point-min)
  5860. (- (match-beginning 0)
  5861. org-ds-keyword-length))
  5862. (match-beginning 0))
  5863. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  5864. deadlinep (string-match org-deadline-regexp tmp)
  5865. scheduledp (string-match org-scheduled-regexp tmp)
  5866. donep (org-entry-is-done-p))
  5867. (if (string-match ">" timestr)
  5868. ;; substring should only run to end of time stamp
  5869. (setq timestr (substring timestr 0 (match-end 0))))
  5870. (save-excursion
  5871. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  5872. (progn
  5873. (goto-char (match-end 1))
  5874. (setq hdmarker (org-agenda-new-marker)
  5875. tags (org-get-tags-at))
  5876. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  5877. (setq txt (org-format-agenda-item
  5878. (format "%s%s"
  5879. (if deadlinep "Deadline: " "")
  5880. (if scheduledp "Scheduled: " ""))
  5881. (match-string 1) category tags timestr)))
  5882. (setq txt org-agenda-no-heading-message))
  5883. (setq priority (org-get-priority txt))
  5884. (org-add-props txt props
  5885. 'org-marker marker 'org-hd-marker hdmarker)
  5886. (if deadlinep
  5887. (org-add-props txt nil
  5888. 'face (if donep 'org-done 'org-warning)
  5889. 'undone-face 'org-warning 'done-face 'org-done
  5890. 'category category 'priority (+ 100 priority))
  5891. (if scheduledp
  5892. (org-add-props txt nil
  5893. 'face 'org-scheduled-today
  5894. 'undone-face 'org-scheduled-today 'done-face 'org-done
  5895. 'category category 'priority (+ 99 priority))
  5896. (org-add-props txt nil 'priority priority 'category category)))
  5897. (push txt ee))
  5898. (outline-next-heading))))
  5899. (nreverse ee)))
  5900. (defun org-agenda-get-closed ()
  5901. "Return the logged TODO entries for agenda display."
  5902. (let* ((props (list 'mouse-face 'highlight
  5903. 'org-not-done-regexp org-not-done-regexp
  5904. 'keymap org-agenda-keymap
  5905. 'help-echo
  5906. (format "mouse-2 or RET jump to org file %s"
  5907. (abbreviate-file-name buffer-file-name))))
  5908. (regexp (concat
  5909. "\\<" org-closed-string " *\\["
  5910. (regexp-quote
  5911. (substring
  5912. (format-time-string
  5913. (car org-time-stamp-formats)
  5914. (apply 'encode-time ; DATE bound by calendar
  5915. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  5916. 1 11))))
  5917. marker hdmarker priority category tags
  5918. ee txt timestr)
  5919. (goto-char (point-min))
  5920. (while (re-search-forward regexp nil t)
  5921. (if (not (save-match-data (org-at-date-range-p)))
  5922. (progn
  5923. (setq marker (org-agenda-new-marker (match-beginning 0))
  5924. category (org-get-category (match-beginning 0))
  5925. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  5926. ;; donep (org-entry-is-done-p)
  5927. )
  5928. (if (string-match "\\]" timestr)
  5929. ;; substring should only run to end of time stamp
  5930. (setq timestr (substring timestr 0 (match-end 0))))
  5931. (save-excursion
  5932. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  5933. (progn
  5934. (goto-char (match-end 1))
  5935. (setq hdmarker (org-agenda-new-marker)
  5936. tags (org-get-tags-at))
  5937. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  5938. (setq txt (org-format-agenda-item
  5939. "Closed: "
  5940. (match-string 1) category tags timestr)))
  5941. (setq txt org-agenda-no-heading-message))
  5942. (setq priority 100000)
  5943. (org-add-props txt props
  5944. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  5945. 'priority priority 'category category
  5946. 'undone-face 'org-warning 'done-face 'org-done)
  5947. (push txt ee))
  5948. (outline-next-heading))))
  5949. (nreverse ee)))
  5950. (defun org-agenda-get-deadlines ()
  5951. "Return the deadline information for agenda display."
  5952. (let* ((wdays org-deadline-warning-days)
  5953. (props (list 'mouse-face 'highlight
  5954. 'org-not-done-regexp org-not-done-regexp
  5955. 'keymap org-agenda-keymap
  5956. 'help-echo
  5957. (format "mouse-2 or RET jump to org file %s"
  5958. (abbreviate-file-name buffer-file-name))))
  5959. (regexp org-deadline-time-regexp)
  5960. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  5961. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  5962. d2 diff pos pos1 category tags
  5963. ee txt head)
  5964. (goto-char (point-min))
  5965. (while (re-search-forward regexp nil t)
  5966. (setq pos (1- (match-beginning 1))
  5967. d2 (time-to-days
  5968. (org-time-string-to-time (match-string 1)))
  5969. diff (- d2 d1))
  5970. ;; When to show a deadline in the calendar:
  5971. ;; If the expiration is within wdays warning time.
  5972. ;; Past-due deadlines are only shown on the current date
  5973. (if (and (< diff wdays) todayp (not (= diff 0)))
  5974. (save-excursion
  5975. (setq category (org-get-category))
  5976. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  5977. (progn
  5978. (goto-char (match-end 0))
  5979. (setq pos1 (match-end 1))
  5980. (setq tags (org-get-tags-at pos1))
  5981. (setq head (buffer-substring-no-properties
  5982. (point)
  5983. (progn (skip-chars-forward "^\r\n")
  5984. (point))))
  5985. (if (string-match org-looking-at-done-regexp head)
  5986. (setq txt nil)
  5987. (setq txt (org-format-agenda-item
  5988. (format "In %3d d.: " diff) head category tags))))
  5989. (setq txt org-agenda-no-heading-message))
  5990. (when txt
  5991. (org-add-props txt props
  5992. 'org-marker (org-agenda-new-marker pos)
  5993. 'org-hd-marker (org-agenda-new-marker pos1)
  5994. 'priority (+ (- 10 diff) (org-get-priority txt))
  5995. 'category category
  5996. 'face (cond ((<= diff 0) 'org-warning)
  5997. ((<= diff 5) 'org-scheduled-previously)
  5998. (t nil))
  5999. 'undone-face (cond
  6000. ((<= diff 0) 'org-warning)
  6001. ((<= diff 5) 'org-scheduled-previously)
  6002. (t nil))
  6003. 'done-face 'org-done)
  6004. (push txt ee)))))
  6005. ee))
  6006. (defun org-agenda-get-scheduled ()
  6007. "Return the scheduled information for agenda display."
  6008. (let* ((props (list 'face 'org-scheduled-previously
  6009. 'org-not-done-regexp org-not-done-regexp
  6010. 'undone-face 'org-scheduled-previously
  6011. 'done-face 'org-done
  6012. 'mouse-face 'highlight
  6013. 'keymap org-agenda-keymap
  6014. 'help-echo
  6015. (format "mouse-2 or RET jump to org file %s"
  6016. (abbreviate-file-name buffer-file-name))))
  6017. (regexp org-scheduled-time-regexp)
  6018. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  6019. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  6020. d2 diff pos pos1 category tags
  6021. ee txt head)
  6022. (goto-char (point-min))
  6023. (while (re-search-forward regexp nil t)
  6024. (setq pos (1- (match-beginning 1))
  6025. d2 (time-to-days
  6026. (org-time-string-to-time (match-string 1)))
  6027. diff (- d2 d1))
  6028. ;; When to show a scheduled item in the calendar:
  6029. ;; If it is on or past the date.
  6030. (if (and (< diff 0) todayp)
  6031. (save-excursion
  6032. (setq category (org-get-category))
  6033. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  6034. (progn
  6035. (goto-char (match-end 0))
  6036. (setq pos1 (match-end 1))
  6037. (setq tags (org-get-tags-at))
  6038. (setq head (buffer-substring-no-properties
  6039. (point)
  6040. (progn (skip-chars-forward "^\r\n") (point))))
  6041. (if (string-match org-looking-at-done-regexp head)
  6042. (setq txt nil)
  6043. (setq txt (org-format-agenda-item
  6044. (format "Sched.%2dx: " (- 1 diff)) head
  6045. category tags))))
  6046. (setq txt org-agenda-no-heading-message))
  6047. (when txt
  6048. (org-add-props txt props
  6049. 'org-marker (org-agenda-new-marker pos)
  6050. 'org-hd-marker (org-agenda-new-marker pos1)
  6051. 'priority (+ (- 5 diff) (org-get-priority txt))
  6052. 'category category)
  6053. (push txt ee)))))
  6054. ee))
  6055. (defun org-agenda-get-blocks ()
  6056. "Return the date-range information for agenda display."
  6057. (let* ((props (list 'face nil
  6058. 'org-not-done-regexp org-not-done-regexp
  6059. 'mouse-face 'highlight
  6060. 'keymap org-agenda-keymap
  6061. 'help-echo
  6062. (format "mouse-2 or RET jump to org file %s"
  6063. (abbreviate-file-name buffer-file-name))))
  6064. (regexp org-tr-regexp)
  6065. (d0 (calendar-absolute-from-gregorian date))
  6066. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags)
  6067. (goto-char (point-min))
  6068. (while (re-search-forward regexp nil t)
  6069. (setq timestr (match-string 0)
  6070. s1 (match-string 1)
  6071. s2 (match-string 2)
  6072. d1 (time-to-days (org-time-string-to-time s1))
  6073. d2 (time-to-days (org-time-string-to-time s2)))
  6074. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  6075. ;; Only allow days between the limits, because the normal
  6076. ;; date stamps will catch the limits.
  6077. (save-excursion
  6078. (setq marker (org-agenda-new-marker (point)))
  6079. (setq category (org-get-category))
  6080. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  6081. (progn
  6082. (setq hdmarker (org-agenda-new-marker (match-end 1)))
  6083. (goto-char (match-end 1))
  6084. (setq tags (org-get-tags-at))
  6085. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  6086. (setq txt (org-format-agenda-item
  6087. (format (if (= d1 d2) "" "(%d/%d): ")
  6088. (1+ (- d0 d1)) (1+ (- d2 d1)))
  6089. (match-string 1) category tags
  6090. (if (= d0 d1) timestr))))
  6091. (setq txt org-agenda-no-heading-message))
  6092. (org-add-props txt props
  6093. 'org-marker marker 'org-hd-marker hdmarker
  6094. 'priority (org-get-priority txt) 'category category)
  6095. (push txt ee)))
  6096. (outline-next-heading))
  6097. ;; Sort the entries by expiration date.
  6098. (nreverse ee)))
  6099. (defconst org-plain-time-of-day-regexp
  6100. (concat
  6101. "\\(\\<[012]?[0-9]"
  6102. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  6103. "\\(--?"
  6104. "\\(\\<[012]?[0-9]"
  6105. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  6106. "\\)?")
  6107. "Regular expression to match a plain time or time range.
  6108. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  6109. groups carry important information:
  6110. 0 the full match
  6111. 1 the first time, range or not
  6112. 8 the second time, if it is a range.")
  6113. (defconst org-stamp-time-of-day-regexp
  6114. (concat
  6115. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +[a-zA-Z]+ +\\)"
  6116. "\\([012][0-9]:[0-5][0-9]\\)>"
  6117. "\\(--?"
  6118. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  6119. "Regular expression to match a timestamp time or time range.
  6120. After a match, the following groups carry important information:
  6121. 0 the full match
  6122. 1 date plus weekday, for backreferencing to make sure both times on same day
  6123. 2 the first time, range or not
  6124. 4 the second time, if it is a range.")
  6125. (defvar org-prefix-has-time nil
  6126. "A flag, set by `org-compile-prefix-format'.
  6127. The flag is set if the currently compiled format contains a `%t'.")
  6128. (defvar org-prefix-has-tag nil
  6129. "A flag, set by `org-compile-prefix-format'.
  6130. The flag is set if the currently compiled format contains a `%T'.")
  6131. (defun org-format-agenda-item (extra txt &optional category tags dotime noprefix)
  6132. "Format TXT to be inserted into the agenda buffer.
  6133. In particular, it adds the prefix and corresponding text properties. EXTRA
  6134. must be a string and replaces the `%s' specifier in the prefix format.
  6135. CATEGORY (string, symbol or nil) may be used to overrule the default
  6136. category taken from local variable or file name. It will replace the `%c'
  6137. specifier in the format. DOTIME, when non-nil, indicates that a
  6138. time-of-day should be extracted from TXT for sorting of this entry, and for
  6139. the `%t' specifier in the format. When DOTIME is a string, this string is
  6140. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  6141. only the correctly processes TXT should be returned - this is used by
  6142. `org-agenda-change-all-lines'. TAG can be the tag of the headline."
  6143. (save-match-data
  6144. ;; Diary entries sometimes have extra whitespace at the beginning
  6145. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  6146. (let* ((category (or category
  6147. org-category
  6148. (if buffer-file-name
  6149. (file-name-sans-extension
  6150. (file-name-nondirectory buffer-file-name))
  6151. "")))
  6152. (tag (if tags (nth (1- (length tags)) tags) ""))
  6153. time ;; needed for the eval of the prefix format
  6154. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  6155. (time-of-day (and dotime (org-get-time-of-day ts)))
  6156. stamp plain s0 s1 s2 rtn)
  6157. (when (and dotime time-of-day org-prefix-has-time)
  6158. ;; Extract starting and ending time and move them to prefix
  6159. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  6160. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  6161. (setq s0 (match-string 0 ts)
  6162. s1 (match-string (if plain 1 2) ts)
  6163. s2 (match-string (if plain 8 4) ts))
  6164. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  6165. ;; them, we might want to remove them there to avoid duplication.
  6166. ;; The user can turn this off with a variable.
  6167. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  6168. (string-match (concat (regexp-quote s0) " *") txt)
  6169. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  6170. (= (match-beginning 0) 0)
  6171. t))
  6172. (setq txt (replace-match "" nil nil txt))))
  6173. ;; Normalize the time(s) to 24 hour
  6174. (if s1 (setq s1 (org-get-time-of-day s1 'string)))
  6175. (if s2 (setq s2 (org-get-time-of-day s2 'string))))
  6176. (when (string-match "\\([ \t]+\\)\\(:[a-zA-Z_@0-9:]+:\\)[ \t]*$" txt)
  6177. ;; Tags are in the string
  6178. (if (or (eq org-agenda-remove-tags-when-in-prefix t)
  6179. (and org-agenda-remove-tags-when-in-prefix
  6180. org-prefix-has-tag))
  6181. (setq txt (replace-match "" t t txt))
  6182. (setq txt (replace-match
  6183. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  6184. (match-string 2 txt))
  6185. t t txt))))
  6186. ;; Create the final string
  6187. (if noprefix
  6188. (setq rtn txt)
  6189. ;; Prepare the variables needed in the eval of the compiled format
  6190. (setq time (cond (s2 (concat s1 "-" s2))
  6191. (s1 (concat s1 "......"))
  6192. (t ""))
  6193. extra (or extra "")
  6194. category (if (symbolp category) (symbol-name category) category))
  6195. ;; Evaluate the compiled format
  6196. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  6197. ;; And finally add the text properties
  6198. (org-add-props rtn nil
  6199. 'category (downcase category) 'tags tags
  6200. 'prefix-length (- (length rtn) (length txt))
  6201. 'time-of-day time-of-day
  6202. 'dotime dotime))))
  6203. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  6204. (catch 'exit
  6205. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  6206. ((and todayp (member 'today (car org-agenda-time-grid))))
  6207. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  6208. ((member 'weekly (car org-agenda-time-grid)))
  6209. (t (throw 'exit list)))
  6210. (let* ((have (delq nil (mapcar
  6211. (lambda (x) (get-text-property 1 'time-of-day x))
  6212. list)))
  6213. (string (nth 1 org-agenda-time-grid))
  6214. (gridtimes (nth 2 org-agenda-time-grid))
  6215. (req (car org-agenda-time-grid))
  6216. (remove (member 'remove-match req))
  6217. new time)
  6218. (if (and (member 'require-timed req) (not have))
  6219. ;; don't show empty grid
  6220. (throw 'exit list))
  6221. (while (setq time (pop gridtimes))
  6222. (unless (and remove (member time have))
  6223. (setq time (int-to-string time))
  6224. (push (org-format-agenda-item
  6225. nil string "" nil
  6226. (concat (substring time 0 -2) ":" (substring time -2)))
  6227. new)
  6228. (put-text-property
  6229. 1 (length (car new)) 'face 'org-time-grid (car new))))
  6230. (if (member 'time-up org-agenda-sorting-strategy)
  6231. (append new list)
  6232. (append list new)))))
  6233. (defun org-compile-prefix-format (format)
  6234. "Compile the prefix format into a Lisp form that can be evaluated.
  6235. The resulting form is returned and stored in the variable
  6236. `org-prefix-format-compiled'."
  6237. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  6238. (let ((start 0) varform vars var (s format)e c f opt)
  6239. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  6240. s start)
  6241. (setq var (cdr (assoc (match-string 4 s)
  6242. '(("c" . category) ("t" . time) ("s" . extra)
  6243. ("T" . tag))))
  6244. c (or (match-string 3 s) "")
  6245. opt (match-beginning 1)
  6246. start (1+ (match-beginning 0)))
  6247. (if (equal var 'time) (setq org-prefix-has-time t))
  6248. (if (equal var 'tag) (setq org-prefix-has-tag t))
  6249. (setq f (concat "%" (match-string 2 s) "s"))
  6250. (if opt
  6251. (setq varform
  6252. `(if (equal "" ,var)
  6253. ""
  6254. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  6255. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  6256. (setq s (replace-match "%s" t nil s))
  6257. (push varform vars))
  6258. (setq vars (nreverse vars))
  6259. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  6260. (defun org-get-time-of-day (s &optional string)
  6261. "Check string S for a time of day.
  6262. If found, return it as a military time number between 0 and 2400.
  6263. If not found, return nil.
  6264. The optional STRING argument forces conversion into a 5 character wide string
  6265. HH:MM."
  6266. (save-match-data
  6267. (when
  6268. (or
  6269. (string-match
  6270. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  6271. (string-match
  6272. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  6273. (let* ((t0 (+ (* 100
  6274. (+ (string-to-number (match-string 1 s))
  6275. (if (and (match-beginning 4)
  6276. (equal (downcase (match-string 4 s)) "pm"))
  6277. 12 0)))
  6278. (if (match-beginning 3)
  6279. (string-to-number (match-string 3 s))
  6280. 0)))
  6281. (t1 (concat " "
  6282. (if (< t0 100) "0" "") (if (< t0 10) "0" "")
  6283. (int-to-string t0))))
  6284. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  6285. (defun org-finalize-agenda-entries (list)
  6286. "Sort and concatenate the agenda items."
  6287. (setq list (mapcar 'org-agenda-highlight-todo list))
  6288. (mapconcat 'identity (sort list 'org-entries-lessp) "\n"))
  6289. (defun org-agenda-highlight-todo (x)
  6290. (let (re)
  6291. (if (eq x 'line)
  6292. (save-excursion
  6293. (beginning-of-line 1)
  6294. (setq re (get-text-property (point) 'org-not-done-regexp))
  6295. (goto-char (+ (point) (get-text-property (point) 'prefix-length)))
  6296. (and (looking-at (concat "[ \t]*" re))
  6297. (add-text-properties (match-beginning 0) (match-end 0)
  6298. '(face org-todo))))
  6299. (setq re (get-text-property 0 'org-not-done-regexp x))
  6300. (and re (string-match re x)
  6301. (add-text-properties (match-beginning 0) (match-end 0)
  6302. '(face org-todo) x))
  6303. x)))
  6304. (defsubst org-cmp-priority (a b)
  6305. "Compare the priorities of string A and B."
  6306. (let ((pa (or (get-text-property 1 'priority a) 0))
  6307. (pb (or (get-text-property 1 'priority b) 0)))
  6308. (cond ((> pa pb) +1)
  6309. ((< pa pb) -1)
  6310. (t nil))))
  6311. (defsubst org-cmp-category (a b)
  6312. "Compare the string values of categories of strings A and B."
  6313. (let ((ca (or (get-text-property 1 'category a) ""))
  6314. (cb (or (get-text-property 1 'category b) "")))
  6315. (cond ((string-lessp ca cb) -1)
  6316. ((string-lessp cb ca) +1)
  6317. (t nil))))
  6318. (defsubst org-cmp-time (a b)
  6319. "Compare the time-of-day values of strings A and B."
  6320. (let* ((def (if org-sort-agenda-notime-is-late 2401 -1))
  6321. (ta (or (get-text-property 1 'time-of-day a) def))
  6322. (tb (or (get-text-property 1 'time-of-day b) def)))
  6323. (cond ((< ta tb) -1)
  6324. ((< tb ta) +1)
  6325. (t nil))))
  6326. (defun org-entries-lessp (a b)
  6327. "Predicate for sorting agenda entries."
  6328. ;; The following variables will be used when the form is evaluated.
  6329. (let* ((time-up (org-cmp-time a b))
  6330. (time-down (if time-up (- time-up) nil))
  6331. (priority-up (org-cmp-priority a b))
  6332. (priority-down (if priority-up (- priority-up) nil))
  6333. (category-up (org-cmp-category a b))
  6334. (category-down (if category-up (- category-up) nil))
  6335. (category-keep (if category-up +1 nil)))
  6336. (cdr (assoc
  6337. (eval (cons 'or org-agenda-sorting-strategy))
  6338. '((-1 . t) (1 . nil) (nil . nil))))))
  6339. (defun org-agenda-show-priority ()
  6340. "Show the priority of the current item.
  6341. This priority is composed of the main priority given with the [#A] cookies,
  6342. and by additional input from the age of a schedules or deadline entry."
  6343. (interactive)
  6344. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  6345. (message "Priority is %d" (if pri pri -1000))))
  6346. (defun org-agenda-show-tags ()
  6347. "Show the tags applicable to the current item."
  6348. (interactive)
  6349. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  6350. (if tags
  6351. (message "Tags are :%s:"
  6352. (org-no-properties (mapconcat 'identity tags ":")))
  6353. (message "No tags associated with this line"))))
  6354. (defun org-agenda-goto (&optional highlight)
  6355. "Go to the Org-mode file which contains the item at point."
  6356. (interactive)
  6357. (let* ((marker (or (get-text-property (point) 'org-marker)
  6358. (org-agenda-error)))
  6359. (buffer (marker-buffer marker))
  6360. (pos (marker-position marker)))
  6361. (switch-to-buffer-other-window buffer)
  6362. (widen)
  6363. (goto-char pos)
  6364. (when (eq major-mode 'org-mode)
  6365. (org-show-hidden-entry)
  6366. (save-excursion
  6367. (and (outline-next-heading)
  6368. (org-flag-heading nil)))) ; show the next heading
  6369. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  6370. (defun org-agenda-switch-to ()
  6371. "Go to the Org-mode file which contains the item at point."
  6372. (interactive)
  6373. (let* ((marker (or (get-text-property (point) 'org-marker)
  6374. (org-agenda-error)))
  6375. (buffer (marker-buffer marker))
  6376. (pos (marker-position marker)))
  6377. (switch-to-buffer buffer)
  6378. (delete-other-windows)
  6379. (widen)
  6380. (goto-char pos)
  6381. (when (eq major-mode 'org-mode)
  6382. (org-show-hidden-entry)
  6383. (save-excursion
  6384. (and (outline-next-heading)
  6385. (org-flag-heading nil)))))) ; show the next heading
  6386. (defun org-agenda-goto-mouse (ev)
  6387. "Go to the Org-mode file which contains the item at the mouse click."
  6388. (interactive "e")
  6389. (mouse-set-point ev)
  6390. (org-agenda-goto))
  6391. (defun org-agenda-show ()
  6392. "Display the Org-mode file which contains the item at point."
  6393. (interactive)
  6394. (let ((win (selected-window)))
  6395. (org-agenda-goto t)
  6396. (select-window win)))
  6397. (defun org-agenda-recenter (arg)
  6398. "Display the Org-mode file which contains the item at point and recenter."
  6399. (interactive "P")
  6400. (let ((win (selected-window)))
  6401. (org-agenda-goto t)
  6402. (recenter arg)
  6403. (select-window win)))
  6404. (defun org-agenda-show-mouse (ev)
  6405. "Display the Org-mode file which contains the item at the mouse click."
  6406. (interactive "e")
  6407. (mouse-set-point ev)
  6408. (org-agenda-show))
  6409. (defun org-agenda-check-no-diary ()
  6410. "Check if the entry is a diary link and abort if yes."
  6411. (if (get-text-property (point) 'org-agenda-diary-link)
  6412. (org-agenda-error)))
  6413. (defun org-agenda-error ()
  6414. (error "Command not allowed in this line"))
  6415. (defvar org-last-heading-marker (make-marker)
  6416. "Marker pointing to the headline that last changed its TODO state
  6417. by a remote command from the agenda.")
  6418. (defun org-agenda-todo (&optional arg)
  6419. "Cycle TODO state of line at point, also in Org-mode file.
  6420. This changes the line at point, all other lines in the agenda referring to
  6421. the same tree node, and the headline of the tree node in the Org-mode file."
  6422. (interactive "P")
  6423. (org-agenda-check-no-diary)
  6424. (let* ((col (current-column))
  6425. (marker (or (get-text-property (point) 'org-marker)
  6426. (org-agenda-error)))
  6427. (buffer (marker-buffer marker))
  6428. (pos (marker-position marker))
  6429. (hdmarker (get-text-property (point) 'org-hd-marker))
  6430. (buffer-read-only nil)
  6431. newhead)
  6432. (with-current-buffer buffer
  6433. (widen)
  6434. (goto-char pos)
  6435. (org-show-hidden-entry)
  6436. (save-excursion
  6437. (and (outline-next-heading)
  6438. (org-flag-heading nil))) ; show the next heading
  6439. (org-todo arg)
  6440. (and (bolp) (forward-char 1))
  6441. (setq newhead (org-get-heading))
  6442. (save-excursion
  6443. (org-back-to-heading)
  6444. (move-marker org-last-heading-marker (point))))
  6445. (beginning-of-line 1)
  6446. (save-excursion
  6447. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  6448. (move-to-column col)))
  6449. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  6450. "Change all lines in the agenda buffer which match HDMARKER.
  6451. The new content of the line will be NEWHEAD (as modified by
  6452. `org-format-agenda-item'). HDMARKER is checked with
  6453. `equal' against all `org-hd-marker' text properties in the file.
  6454. If FIXFACE is non-nil, the face of each item is modified acording to
  6455. the new TODO state."
  6456. (let* (props m pl undone-face done-face finish new dotime cat tags)
  6457. ; (setq newhead (org-format-agenda-item "x" newhead "x" nil 'noprefix))
  6458. (save-excursion
  6459. (goto-char (point-max))
  6460. (beginning-of-line 1)
  6461. (while (not finish)
  6462. (setq finish (bobp))
  6463. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  6464. (equal m hdmarker))
  6465. (setq props (text-properties-at (point))
  6466. dotime (get-text-property (point) 'dotime)
  6467. cat (get-text-property (point) 'category)
  6468. tags (get-text-property (point) 'tags)
  6469. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  6470. pl (get-text-property (point) 'prefix-length)
  6471. undone-face (get-text-property (point) 'undone-face)
  6472. done-face (get-text-property (point) 'done-face))
  6473. (move-to-column pl)
  6474. (if (looking-at ".*")
  6475. (progn
  6476. (replace-match new t t)
  6477. (beginning-of-line 1)
  6478. (add-text-properties (point-at-bol) (point-at-eol) props)
  6479. (when fixface
  6480. (add-text-properties
  6481. (point-at-bol) (point-at-eol)
  6482. (list 'face
  6483. (if org-last-todo-state-is-todo
  6484. undone-face done-face)))
  6485. (org-agenda-highlight-todo 'line))
  6486. (beginning-of-line 1))
  6487. (error "Line update did not work")))
  6488. (beginning-of-line 0)))))
  6489. (defun org-agenda-priority-up ()
  6490. "Increase the priority of line at point, also in Org-mode file."
  6491. (interactive)
  6492. (org-agenda-priority 'up))
  6493. (defun org-agenda-priority-down ()
  6494. "Decrease the priority of line at point, also in Org-mode file."
  6495. (interactive)
  6496. (org-agenda-priority 'down))
  6497. (defun org-agenda-priority (&optional force-direction)
  6498. "Set the priority of line at point, also in Org-mode file.
  6499. This changes the line at point, all other lines in the agenda referring to
  6500. the same tree node, and the headline of the tree node in the Org-mode file."
  6501. (interactive)
  6502. (org-agenda-check-no-diary)
  6503. (let* ((marker (or (get-text-property (point) 'org-marker)
  6504. (org-agenda-error)))
  6505. (buffer (marker-buffer marker))
  6506. (pos (marker-position marker))
  6507. (hdmarker (get-text-property (point) 'org-hd-marker))
  6508. (buffer-read-only nil)
  6509. newhead)
  6510. (with-current-buffer buffer
  6511. (widen)
  6512. (goto-char pos)
  6513. (org-show-hidden-entry)
  6514. (save-excursion
  6515. (and (outline-next-heading)
  6516. (org-flag-heading nil))) ; show the next heading
  6517. (funcall 'org-priority force-direction)
  6518. (end-of-line 1)
  6519. (setq newhead (org-get-heading)))
  6520. (org-agenda-change-all-lines newhead hdmarker)
  6521. (beginning-of-line 1)))
  6522. (defun org-get-tags-at (&optional pos)
  6523. "Get a list of all headline tags applicable at POS.
  6524. POS defaults to point. If tags are inherited, the list contains
  6525. the targets in the same sequence as the headlines appear, i.e.
  6526. the tags of the current headline come last."
  6527. (interactive)
  6528. (let (tags)
  6529. (save-excursion
  6530. (goto-char (or pos (point)))
  6531. (save-match-data
  6532. (org-back-to-heading t)
  6533. (condition-case nil
  6534. (while t
  6535. (if (looking-at "[^\r\n]+?:\\([a-zA-Z_@0-9:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
  6536. (setq tags (append (org-split-string
  6537. (org-match-string-no-properties 1) ":")
  6538. tags)))
  6539. (or org-use-tag-inheritance (error ""))
  6540. (org-up-heading-all 1))
  6541. (error nil))))
  6542. (message "%s" tags)
  6543. tags))
  6544. (defun org-agenda-set-tags ()
  6545. "Set tags for the current headline."
  6546. (interactive)
  6547. (org-agenda-check-no-diary)
  6548. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  6549. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  6550. (org-agenda-error)))
  6551. (buffer (marker-buffer hdmarker))
  6552. (pos (marker-position hdmarker))
  6553. (buffer-read-only nil)
  6554. newhead)
  6555. (with-current-buffer buffer
  6556. (widen)
  6557. (goto-char pos)
  6558. (org-show-hidden-entry)
  6559. (save-excursion
  6560. (and (outline-next-heading)
  6561. (org-flag-heading nil))) ; show the next heading
  6562. (call-interactively 'org-set-tags)
  6563. (end-of-line 1)
  6564. (setq newhead (org-get-heading)))
  6565. (org-agenda-change-all-lines newhead hdmarker)
  6566. (beginning-of-line 1)))
  6567. (defun org-agenda-date-later (arg &optional what)
  6568. "Change the date of this item to one day later."
  6569. (interactive "p")
  6570. (org-agenda-check-type t 'agenda 'timeline)
  6571. (org-agenda-check-no-diary)
  6572. (let* ((marker (or (get-text-property (point) 'org-marker)
  6573. (org-agenda-error)))
  6574. (buffer (marker-buffer marker))
  6575. (pos (marker-position marker)))
  6576. (with-current-buffer buffer
  6577. (widen)
  6578. (goto-char pos)
  6579. (if (not (org-at-timestamp-p))
  6580. (error "Cannot find time stamp"))
  6581. (org-timestamp-change arg (or what 'day))
  6582. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  6583. (defun org-agenda-date-earlier (arg &optional what)
  6584. "Change the date of this item to one day earlier."
  6585. (interactive "p")
  6586. (org-agenda-date-later (- arg) what))
  6587. (defun org-agenda-date-prompt (arg)
  6588. "Change the date of this item. Date is prompted for, with default today.
  6589. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  6590. be used to request time specification in the time stamp."
  6591. (interactive "P")
  6592. (org-agenda-check-type t 'agenda 'timeline)
  6593. (org-agenda-check-no-diary)
  6594. (let* ((marker (or (get-text-property (point) 'org-marker)
  6595. (org-agenda-error)))
  6596. (buffer (marker-buffer marker))
  6597. (pos (marker-position marker)))
  6598. (with-current-buffer buffer
  6599. (widen)
  6600. (goto-char pos)
  6601. (if (not (org-at-timestamp-p))
  6602. (error "Cannot find time stamp"))
  6603. (org-time-stamp arg)
  6604. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  6605. (defun org-agenda-schedule (arg)
  6606. "Schedule the item at point."
  6607. (interactive "P")
  6608. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  6609. (org-agenda-check-no-diary)
  6610. (let* ((marker (or (get-text-property (point) 'org-marker)
  6611. (org-agenda-error)))
  6612. (buffer (marker-buffer marker))
  6613. (pos (marker-position marker))
  6614. (org-insert-labeled-timestamps-at-point nil)
  6615. ts)
  6616. (with-current-buffer buffer
  6617. (widen)
  6618. (goto-char pos)
  6619. (setq ts (org-schedule))
  6620. (message "Item scheduled for %s" ts))))
  6621. (defun org-agenda-deadline (arg)
  6622. "Schedule the item at point."
  6623. (interactive "P")
  6624. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  6625. (org-agenda-check-no-diary)
  6626. (let* ((marker (or (get-text-property (point) 'org-marker)
  6627. (org-agenda-error)))
  6628. (buffer (marker-buffer marker))
  6629. (pos (marker-position marker))
  6630. (org-insert-labeled-timestamps-at-point nil)
  6631. ts)
  6632. (with-current-buffer buffer
  6633. (widen)
  6634. (goto-char pos)
  6635. (setq ts (org-deadline))
  6636. (message "Deadline for this item set to %s" ts))))
  6637. (defun org-get-heading ()
  6638. "Return the heading of the current entry, without the stars."
  6639. (save-excursion
  6640. (and (memq (char-before) '(?\n ?\r)) (skip-chars-forward "^\n\r"))
  6641. (if (and (re-search-backward "[\r\n]\\*" nil t)
  6642. (looking-at "[\r\n]\\*+[ \t]+\\([^\r\n]*\\)"))
  6643. (match-string 1)
  6644. "")))
  6645. (defun org-agenda-diary-entry ()
  6646. "Make a diary entry, like the `i' command from the calendar.
  6647. All the standard commands work: block, weekly etc."
  6648. (interactive)
  6649. (org-agenda-check-type t 'agenda 'timeline)
  6650. (require 'diary-lib)
  6651. (let* ((char (progn
  6652. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  6653. (read-char-exclusive)))
  6654. (cmd (cdr (assoc char
  6655. '((?d . insert-diary-entry)
  6656. (?w . insert-weekly-diary-entry)
  6657. (?m . insert-monthly-diary-entry)
  6658. (?y . insert-yearly-diary-entry)
  6659. (?a . insert-anniversary-diary-entry)
  6660. (?b . insert-block-diary-entry)
  6661. (?c . insert-cyclic-diary-entry)))))
  6662. (oldf (symbol-function 'calendar-cursor-to-date))
  6663. (point (point))
  6664. (mark (or (mark t) (point))))
  6665. (unless cmd
  6666. (error "No command associated with <%c>" char))
  6667. (unless (and (get-text-property point 'day)
  6668. (or (not (equal ?b char))
  6669. (get-text-property mark 'day)))
  6670. (error "Don't know which date to use for diary entry"))
  6671. ;; We implement this by hacking the `calendar-cursor-to-date' function
  6672. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  6673. (let ((calendar-mark-ring
  6674. (list (calendar-gregorian-from-absolute
  6675. (or (get-text-property mark 'day)
  6676. (get-text-property point 'day))))))
  6677. (unwind-protect
  6678. (progn
  6679. (fset 'calendar-cursor-to-date
  6680. (lambda (&optional error)
  6681. (calendar-gregorian-from-absolute
  6682. (get-text-property point 'day))))
  6683. (call-interactively cmd))
  6684. (fset 'calendar-cursor-to-date oldf)))))
  6685. (defun org-agenda-execute-calendar-command (cmd)
  6686. "Execute a calendar command from the agenda, with the date associated to
  6687. the cursor position."
  6688. (org-agenda-check-type t 'agenda 'timeline)
  6689. (require 'diary-lib)
  6690. (unless (get-text-property (point) 'day)
  6691. (error "Don't know which date to use for calendar command"))
  6692. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  6693. (point (point))
  6694. (date (calendar-gregorian-from-absolute
  6695. (get-text-property point 'day)))
  6696. (displayed-day (extract-calendar-day date))
  6697. (displayed-month (extract-calendar-month date))
  6698. (displayed-year (extract-calendar-year date)))
  6699. (unwind-protect
  6700. (progn
  6701. (fset 'calendar-cursor-to-date
  6702. (lambda (&optional error)
  6703. (calendar-gregorian-from-absolute
  6704. (get-text-property point 'day))))
  6705. (call-interactively cmd))
  6706. (fset 'calendar-cursor-to-date oldf))))
  6707. (defun org-agenda-phases-of-moon ()
  6708. "Display the phases of the moon for the 3 months around the cursor date."
  6709. (interactive)
  6710. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  6711. (defun org-agenda-holidays ()
  6712. "Display the holidays for the 3 months around the cursor date."
  6713. (interactive)
  6714. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  6715. (defun org-agenda-sunrise-sunset (arg)
  6716. "Display sunrise and sunset for the cursor date.
  6717. Latitude and longitude can be specified with the variables
  6718. `calendar-latitude' and `calendar-longitude'. When called with prefix
  6719. argument, latitude and longitude will be prompted for."
  6720. (interactive "P")
  6721. (let ((calendar-longitude (if arg nil calendar-longitude))
  6722. (calendar-latitude (if arg nil calendar-latitude))
  6723. (calendar-location-name
  6724. (if arg "the given coordinates" calendar-location-name)))
  6725. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  6726. (defun org-agenda-goto-calendar ()
  6727. "Open the Emacs calendar with the date at the cursor."
  6728. (interactive)
  6729. (org-agenda-check-type t 'agenda 'timeline)
  6730. (let* ((day (or (get-text-property (point) 'day)
  6731. (error "Don't know which date to open in calendar")))
  6732. (date (calendar-gregorian-from-absolute day))
  6733. (calendar-move-hook nil)
  6734. (view-calendar-holidays-initially nil)
  6735. (view-diary-entries-initially nil))
  6736. (calendar)
  6737. (calendar-goto-date date)))
  6738. (defun org-calendar-goto-agenda ()
  6739. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  6740. This is a command that has to be installed in `calendar-mode-map'."
  6741. (interactive)
  6742. (org-agenda-list nil (calendar-absolute-from-gregorian
  6743. (calendar-cursor-to-date))
  6744. nil t))
  6745. (defun org-agenda-convert-date ()
  6746. (interactive)
  6747. (org-agenda-check-type t 'agenda 'timeline)
  6748. (let ((day (get-text-property (point) 'day))
  6749. date s)
  6750. (unless day
  6751. (error "Don't know which date to convert"))
  6752. (setq date (calendar-gregorian-from-absolute day))
  6753. (setq s (concat
  6754. "Gregorian: " (calendar-date-string date) "\n"
  6755. "ISO: " (calendar-iso-date-string date) "\n"
  6756. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  6757. "Julian: " (calendar-julian-date-string date) "\n"
  6758. "Astron. JD: " (calendar-astro-date-string date)
  6759. " (Julian date number at noon UTC)\n"
  6760. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  6761. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  6762. "French: " (calendar-french-date-string date) "\n"
  6763. "Mayan: " (calendar-mayan-date-string date) "\n"
  6764. "Coptic: " (calendar-coptic-date-string date) "\n"
  6765. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  6766. "Persian: " (calendar-persian-date-string date) "\n"
  6767. "Chinese: " (calendar-chinese-date-string date) "\n"))
  6768. (with-output-to-temp-buffer "*Dates*"
  6769. (princ s))
  6770. (if (fboundp 'fit-window-to-buffer)
  6771. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  6772. ;;; Tags
  6773. (defun org-scan-tags (action matcher &optional todo-only)
  6774. "Scan headline tags with inheritance and produce output ACTION.
  6775. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  6776. evaluated, testing if a given set of tags qualifies a headline for
  6777. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  6778. are included in the output."
  6779. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  6780. (mapconcat 'regexp-quote
  6781. (nreverse (cdr (reverse org-todo-keywords)))
  6782. "\\|")
  6783. "\\>\\)\\)? *\\(.*?\\)\\(:[A-Za-z_@0-9:]+:\\)?[ \t]*[\n\r]"))
  6784. (props (list 'face nil
  6785. 'done-face 'org-done
  6786. 'undone-face nil
  6787. 'mouse-face 'highlight
  6788. 'keymap org-agenda-keymap
  6789. 'help-echo
  6790. (format "mouse-2 or RET jump to org file %s"
  6791. (abbreviate-file-name buffer-file-name))))
  6792. lspos
  6793. tags tags-list tags-alist (llast 0) rtn level category i txt
  6794. todo marker)
  6795. (save-excursion
  6796. (goto-char (point-min))
  6797. (when (eq action 'sparse-tree) (org-overview))
  6798. (while (re-search-forward re nil t)
  6799. (setq todo (if (match-end 1) (match-string 2))
  6800. tags (if (match-end 4) (match-string 4)))
  6801. (goto-char (setq lspos (1+ (match-beginning 0))))
  6802. (setq level (funcall outline-level)
  6803. category (org-get-category))
  6804. (setq i llast llast level)
  6805. ;; remove tag lists from same and sublevels
  6806. (while (>= i level)
  6807. (when (setq entry (assoc i tags-alist))
  6808. (setq tags-alist (delete entry tags-alist)))
  6809. (setq i (1- i)))
  6810. ;; add the nex tags
  6811. (when tags
  6812. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  6813. tags-alist
  6814. (cons (cons level tags) tags-alist)))
  6815. ;; compile tags for current headline
  6816. (setq tags-list
  6817. (if org-use-tag-inheritance
  6818. (apply 'append (mapcar 'cdr tags-alist))
  6819. tags))
  6820. (when (and (or (not todo-only) todo)
  6821. (eval matcher))
  6822. ;; list this headline
  6823. (if (eq action 'sparse-tree)
  6824. (progn
  6825. (org-show-hierarchy-above))
  6826. (setq txt (org-format-agenda-item
  6827. ""
  6828. (concat
  6829. (if org-tags-match-list-sublevels
  6830. (make-string (1- level) ?.) "")
  6831. (org-get-heading))
  6832. category tags-list))
  6833. (goto-char lspos)
  6834. (setq marker (org-agenda-new-marker))
  6835. (org-add-props txt props
  6836. 'org-marker marker 'org-hd-marker marker 'category category)
  6837. (push txt rtn))
  6838. ;; if we are to skip sublevels, jump to end of subtree
  6839. (point)
  6840. (or org-tags-match-list-sublevels (org-end-of-subtree)))))
  6841. (nreverse rtn)))
  6842. (defun org-tags-sparse-tree (&optional arg match)
  6843. "Create a sparse tree according to tags search string MATCH.
  6844. MATCH can contain positive and negative selection of tags, like
  6845. \"+WORK+URGENT-WITHBOSS\"."
  6846. (interactive "P")
  6847. (let ((org-show-following-heading nil)
  6848. (org-show-hierarchy-above nil))
  6849. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)))))
  6850. (defun org-make-tags-matcher (match)
  6851. "Create the TAGS matcher form for the tags-selecting string MATCH."
  6852. (unless match
  6853. ;; Get a new match request, with completion
  6854. (setq org-last-tags-completion-table
  6855. (or (org-get-buffer-tags)
  6856. org-last-tags-completion-table))
  6857. (setq match (completing-read
  6858. "Tags: " 'org-tags-completion-function nil nil nil
  6859. 'org-tags-history)))
  6860. ;; parse the string and create a lisp form
  6861. (let ((match0 match) minus tag mm matcher orterms term orlist)
  6862. (setq orterms (org-split-string match "|"))
  6863. (while (setq term (pop orterms))
  6864. (while (string-match "^&?\\([-+:]\\)?\\([A-Za-z_@0-9]+\\)" term)
  6865. (setq minus (and (match-end 1)
  6866. (equal (match-string 1 term) "-"))
  6867. tag (match-string 2 term)
  6868. term (substring term (match-end 0))
  6869. mm (list 'member (downcase tag) 'tags-list)
  6870. mm (if minus (list 'not mm) mm))
  6871. (push mm matcher))
  6872. (push (if (> (length matcher) 1) (cons 'and matcher) (car matcher))
  6873. orlist)
  6874. (setq matcher nil))
  6875. (setq matcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  6876. ;; Return the string and lisp forms of the matcher
  6877. (cons match0 matcher)))
  6878. ;;;###autoload
  6879. (defun org-tags-view (&optional todo-only match keep-modes)
  6880. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  6881. The prefix arg TODO-ONLY limits the search to TODO entries."
  6882. (interactive "P")
  6883. (org-agenda-maybe-reset-markers 'force)
  6884. (org-compile-prefix-format org-agenda-prefix-format)
  6885. (let* ((org-agenda-keep-modes keep-modes)
  6886. (org-tags-match-list-sublevels
  6887. (if todo-only t org-tags-match-list-sublevels))
  6888. (win (selected-window))
  6889. (completion-ignore-case t)
  6890. rtn rtnall files file pos matcher
  6891. buffer)
  6892. (setq matcher (org-make-tags-matcher match)
  6893. match (car matcher) matcher (cdr matcher))
  6894. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  6895. (progn
  6896. (delete-other-windows)
  6897. (switch-to-buffer-other-window
  6898. (get-buffer-create org-agenda-buffer-name))))
  6899. (setq buffer-read-only nil)
  6900. (erase-buffer)
  6901. (org-agenda-mode) (setq buffer-read-only nil)
  6902. (set (make-local-variable 'org-agenda-type) 'tags)
  6903. (set (make-local-variable 'org-agenda-redo-command)
  6904. (list 'org-tags-view (list 'quote todo-only)
  6905. (list 'if 'current-prefix-arg nil match) t))
  6906. (setq files (org-agenda-files)
  6907. rtnall nil)
  6908. (while (setq file (pop files))
  6909. (catch 'nextfile
  6910. (org-check-agenda-file file)
  6911. (setq buffer (if (file-exists-p file)
  6912. (org-get-agenda-file-buffer file)
  6913. (error "No such file %s" file)))
  6914. (if (not buffer)
  6915. ;; If file does not exist, merror message to agenda
  6916. (setq rtn (list
  6917. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  6918. rtnall (append rtnall rtn))
  6919. (with-current-buffer buffer
  6920. (unless (eq major-mode 'org-mode)
  6921. (error "Agenda file %s is not in `org-mode'" file))
  6922. (setq org-category-table (org-get-category-table))
  6923. (save-excursion
  6924. (save-restriction
  6925. (if org-respect-restriction
  6926. (if (org-region-active-p)
  6927. ;; Respect a region to restrict search
  6928. (narrow-to-region (region-beginning) (region-end)))
  6929. ;; If we work for the calendar or many files,
  6930. ;; get rid of any restriction
  6931. (widen))
  6932. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  6933. (setq rtnall (append rtnall rtn))))))))
  6934. (insert "Headlines with TAGS match: ")
  6935. (add-text-properties (point-min) (1- (point))
  6936. (list 'face 'org-level-3))
  6937. (setq pos (point))
  6938. (insert match "\n")
  6939. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  6940. (setq pos (point))
  6941. (insert "Press `C-u r' to search again with new search string\n")
  6942. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  6943. (when rtnall
  6944. (insert (mapconcat 'identity rtnall "\n")))
  6945. (goto-char (point-min))
  6946. (setq buffer-read-only t)
  6947. (org-fit-agenda-window)
  6948. (if (not org-select-agenda-window) (select-window win))))
  6949. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  6950. (defun org-set-tags (&optional arg just-align)
  6951. "Set the tags for the current headline.
  6952. With prefix ARG, realign all tags in headings in the current buffer."
  6953. (interactive "P")
  6954. (let* ((re (concat "^" outline-regexp))
  6955. (col (current-column))
  6956. (current (org-get-tags))
  6957. table current-tags inherited-tags ; computed below when needed
  6958. tags hd empty invis)
  6959. (if arg
  6960. (save-excursion
  6961. (goto-char (point-min))
  6962. (while (re-search-forward re nil t)
  6963. (org-set-tags nil t))
  6964. (message "All tags realigned to column %d" org-tags-column))
  6965. (if just-align
  6966. (setq tags current)
  6967. (setq table (or org-tag-alist (org-get-buffer-tags))
  6968. org-last-tags-completion-table table
  6969. current-tags (org-split-string current ":")
  6970. inherited-tags (nreverse
  6971. (nthcdr (length current-tags)
  6972. (nreverse (org-get-tags-at))))
  6973. tags
  6974. (if (or (eq t org-use-fast-tag-selection)
  6975. (and org-use-fast-tag-selection (cdar table)))
  6976. (org-fast-tag-selection current-tags inherited-tags table)
  6977. (let ((org-add-colon-after-tag-completion t))
  6978. (completing-read "Tags: " 'org-tags-completion-function
  6979. nil nil current 'org-tags-history))))
  6980. (while (string-match "[-+&]+" tags)
  6981. (setq tags (replace-match ":" t t tags))))
  6982. ;; FIXME: still optimize this by not checking when JUST-ALIGN?
  6983. (unless (setq empty (string-match "\\`[\t ]*\\'" tags))
  6984. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  6985. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  6986. (if (equal current "")
  6987. (progn
  6988. (end-of-line 1)
  6989. (or empty (insert-before-markers " ")))
  6990. (beginning-of-line 1)
  6991. (setq invis (org-invisible-p))
  6992. (looking-at (concat "\\(.*\\)\\(" (regexp-quote current) "\\)[ \t]*"))
  6993. (setq hd (match-string 1))
  6994. (delete-region (match-beginning 0) (match-end 0))
  6995. (insert-before-markers (org-trim hd) (if empty "" " ")))
  6996. (unless (equal tags "")
  6997. (move-to-column (max (current-column)
  6998. (if (> org-tags-column 0)
  6999. org-tags-column
  7000. (- (- org-tags-column) (length tags))))
  7001. t)
  7002. (insert-before-markers tags)
  7003. (if (and (not invis) (org-invisible-p))
  7004. (outline-flag-region (point-at-bol) (point) nil)))
  7005. (move-to-column col))))
  7006. (defun org-tags-completion-function (string predicate &optional flag)
  7007. (let (s1 s2 rtn (ctable org-last-tags-completion-table))
  7008. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  7009. (setq s1 (match-string 1 string)
  7010. s2 (match-string 2 string))
  7011. (setq s1 "" s2 string))
  7012. (cond
  7013. ((eq flag nil)
  7014. ;; try completion
  7015. (setq rtn (try-completion s2 ctable))
  7016. (if (stringp rtn)
  7017. (concat s1 s2 (substring rtn (length s2))
  7018. (if (and org-add-colon-after-tag-completion
  7019. (assoc rtn ctable))
  7020. ":" "")))
  7021. )
  7022. ((eq flag t)
  7023. ;; all-completions
  7024. (all-completions s2 ctable)
  7025. )
  7026. ((eq flag 'lambda)
  7027. ;; exact match?
  7028. (assoc s2 ctable)))
  7029. ))
  7030. (defun org-fast-tag-insert (kwd tags face &optional end)
  7031. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  7032. (insert (format "%-12s" (concat kwd ":"))
  7033. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  7034. (or end "")))
  7035. (defun org-fast-tag-selection (current inherited table)
  7036. "Fast tag selection with single keys.
  7037. CURRENT is the current list of tags in the headline, INHERITED is the
  7038. list of inherited tags, and TABLE is an alist of tags and corresponding keys.
  7039. If the keys are nil, a-z are automatically assigned.
  7040. Returns the new tags string, or nil to not change the current settings."
  7041. (let* ((maxlen (apply 'max (mapcar (lambda (x)
  7042. (string-width (car x))) table)))
  7043. (fwidth (+ maxlen 3 1 3))
  7044. (ncol (/ (window-width) fwidth))
  7045. (i-face 'org-done)
  7046. (c-face 'org-tag)
  7047. tg cnt e c char ntable tbl rtn)
  7048. (save-window-excursion
  7049. (delete-other-windows)
  7050. (split-window-vertically)
  7051. (switch-to-buffer-other-window (get-buffer-create " *Org tags*"))
  7052. (erase-buffer)
  7053. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  7054. (org-fast-tag-insert "Current" current c-face "\n\n")
  7055. (setq tbl table char (1- ?a) cnt 0)
  7056. (while (setq e (pop tbl))
  7057. (setq tg (car e) c (or (cdr e) (setq char (1+ char))))
  7058. (setq tg (org-add-props tg nil 'face
  7059. (cond
  7060. ((member tg current) c-face)
  7061. ((member tg inherited) i-face)
  7062. (t nil))))
  7063. (insert "[" c "] " tg (make-string
  7064. (- fwidth 4 (length tg)) ?\ ))
  7065. (push (cons tg c) ntable)
  7066. (when (= (setq cnt (1+ cnt)) ncol)
  7067. (insert "\n")
  7068. (setq cnt 0)))
  7069. (insert "\n")
  7070. (goto-char (point-min))
  7071. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  7072. (setq rtn
  7073. (catch 'exit
  7074. (while t
  7075. (message "[key]:Toggle SPC: clear current RET accept")
  7076. (setq c (read-char-exclusive))
  7077. (cond
  7078. ((= c ?\r) (throw 'exit t))
  7079. ((= c ?\C-g) (throw 'exit nil))
  7080. ((= c ?\ ) (setq current nil))
  7081. (t (setq e (rassoc c ntable) tg (car e))
  7082. (if (member tg current)
  7083. (setq current (delete tg current))
  7084. (setq current (append current (list tg))))))
  7085. (goto-char (point-min))
  7086. (beginning-of-line 2)
  7087. (delete-region (point) (point-at-eol))
  7088. (org-fast-tag-insert "Current" current c-face)
  7089. (while (re-search-forward "\\[.\\] \\([a-zA-Z0-9_@]+\\)" nil t)
  7090. (setq tg (match-string 1))
  7091. (add-text-properties (match-beginning 1) (match-end 1)
  7092. (list 'face
  7093. (cond
  7094. ((member tg current) c-face)
  7095. ((member tg inherited) i-face)
  7096. (t nil)))))
  7097. (goto-char (point-min)))))
  7098. (if rtn
  7099. (mapconcat 'identity current ":")
  7100. nil))))
  7101. (defun org-get-tags ()
  7102. "Get the TAGS string in the current headline."
  7103. (unless (org-on-heading-p)
  7104. (error "Not on a heading"))
  7105. (save-excursion
  7106. (beginning-of-line 1)
  7107. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  7108. (org-match-string-no-properties 1)
  7109. "")))
  7110. (defun org-get-buffer-tags ()
  7111. "Get a table of all tags used in the buffer, for completion."
  7112. (let (tags)
  7113. (save-excursion
  7114. (goto-char (point-min))
  7115. (while (re-search-forward "[ \t]:\\([A-Za-z_@0-9:]+\\):[ \t\r\n]" nil t)
  7116. (mapc (lambda (x) (add-to-list 'tags x))
  7117. (org-split-string (org-match-string-no-properties 1) ":"))))
  7118. (mapcar 'list tags)))
  7119. ;;; Link Stuff
  7120. (defvar org-create-file-search-functions nil
  7121. "List of functions to construct the right search string for a file link.
  7122. These functions are called in turn with point at the location to
  7123. which the link should point.
  7124. A function in the hook should first test if it would like to
  7125. handle this file type, for example by checking the major-mode or
  7126. the file extension. If it decides not to handle this file, it
  7127. should just return nil to give other functions a chance. If it
  7128. does handle the file, it must return the search string to be used
  7129. when following the link. The search string will be part of the
  7130. file link, given after a double colon, and `org-open-at-point'
  7131. will automatically search for it. If special measures must be
  7132. taken to make the search successful, another function should be
  7133. added to the companion hook `org-execute-file-search-functions',
  7134. which see.
  7135. A function in this hook may also use `setq' to set the variable
  7136. `description' to provide a suggestion for the descriptive text to
  7137. be used for this link when it gets inserted into an Org-mode
  7138. buffer with \\[org-insert-link].")
  7139. (defvar org-execute-file-search-functions nil
  7140. "List of functions to execute a file search triggered by a link.
  7141. Functions added to this hook must accept a single argument, the
  7142. search string that was part of the file link, the part after the
  7143. double colon. The function must first check if it would like to
  7144. handle this search, for example by checking the major-mode or the
  7145. file extension. If it decides not to handle this search, it
  7146. should just return nil to give other functions a chance. If it
  7147. does handle the search, it must return a non-nil value to keep
  7148. other functions from trying.
  7149. Each function can access the current prefix argument through the
  7150. variable `current-prefix-argument'. Note that a single prefix is
  7151. used to force opening a link in Emacs, so it may be good to only
  7152. use a numeric or double prefix to guide the search function.
  7153. In case this is needed, a function in this hook can also restore
  7154. the window configuration before `org-open-at-point' was called using:
  7155. (set-window-configuration org-window-config-before-follow-link)")
  7156. (defun org-find-file-at-mouse (ev)
  7157. "Open file link or URL at mouse."
  7158. (interactive "e")
  7159. (mouse-set-point ev)
  7160. (org-open-at-point 'in-emacs))
  7161. (defun org-open-at-mouse (ev)
  7162. "Open file link or URL at mouse."
  7163. (interactive "e")
  7164. (mouse-set-point ev)
  7165. (org-open-at-point))
  7166. (defvar org-window-config-before-follow-link nil
  7167. "The window configuration before following a link.
  7168. This is saved in case the need arises to restore it.")
  7169. (defun org-open-at-point (&optional in-emacs)
  7170. "Open link at or after point.
  7171. If there is no link at point, this function will search forward up to
  7172. the end of the current subtree.
  7173. Normally, files will be opened by an appropriate application. If the
  7174. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  7175. (interactive "P")
  7176. (setq org-window-config-before-follow-link (current-window-configuration))
  7177. (org-remove-occur-highlights nil nil t)
  7178. (if (org-at-timestamp-p)
  7179. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7180. (substring (match-string 1) 0 10)))
  7181. 1)
  7182. (let (type path link line search (pos (point)))
  7183. (catch 'match
  7184. (save-excursion
  7185. (skip-chars-forward "^]\n\r")
  7186. (when (and (re-search-backward "\\[\\[" nil t)
  7187. (looking-at org-bracket-link-regexp)
  7188. (<= (match-beginning 0) pos)
  7189. (>= (match-end 0) pos))
  7190. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  7191. (while (string-match " *\n *" link)
  7192. (setq link (replace-match " " t t link)))
  7193. (if (string-match org-link-re-with-space2 link)
  7194. (setq type (match-string 1 link)
  7195. path (match-string 2 link))
  7196. (setq type "thisfile"
  7197. path link))
  7198. (throw 'match t)))
  7199. (when (get-text-property (point) 'org-linked-text)
  7200. (setq type "thisfile"
  7201. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7202. (1+ (point)) (point))
  7203. path (buffer-substring
  7204. (previous-single-property-change pos 'org-linked-text)
  7205. (next-single-property-change pos 'org-linked-text)))
  7206. (throw 'match t))
  7207. (save-excursion
  7208. (skip-chars-backward (concat "^[]" org-non-link-chars " "))
  7209. (if (equal (char-before) ?<) (backward-char 1))
  7210. (when (or (looking-at org-angle-link-re)
  7211. (looking-at org-plain-link-re)
  7212. (and (or (re-search-forward org-angle-link-re (point-at-eol) t)
  7213. (re-search-forward org-plain-link-re (point-at-eol) t))
  7214. (<= (match-beginning 0) pos)
  7215. (>= (match-end 0) pos)))
  7216. (setq type (match-string 1)
  7217. path (match-string 2))
  7218. (throw 'match t)))
  7219. (save-excursion
  7220. (skip-chars-backward "^ \t\n\r")
  7221. (when (looking-at "\\(:[A-Za-z_@0-9:]+\\):[ \t\r\n]")
  7222. (setq type "tags"
  7223. path (match-string 1))
  7224. (while (string-match ":" path)
  7225. (setq path (replace-match "+" t t path)))
  7226. (throw 'match t)))
  7227. (save-excursion
  7228. (skip-chars-backward "a-zA-Z_")
  7229. (when (and (memq 'camel org-activate-links)
  7230. (looking-at org-camel-regexp))
  7231. (setq type "camel" path (match-string 0))
  7232. (if (equal (char-before) ?*)
  7233. (setq path (concat "*" path))))
  7234. (throw 'match t)))
  7235. (unless path
  7236. (error "No link found"))
  7237. ;; Remove any trailing spaces in path
  7238. (if (string-match " +\\'" path)
  7239. (setq path (replace-match "" t t path)))
  7240. (cond
  7241. ((member type '("http" "https" "ftp" "mailto" "news"))
  7242. (browse-url (concat type ":" path)))
  7243. ((string= type "tags")
  7244. (org-tags-view in-emacs path))
  7245. ((or (string= type "camel")
  7246. (string= type "thisfile"))
  7247. (org-mark-ring-push)
  7248. (org-link-search
  7249. path
  7250. (cond ((equal in-emacs '(4)) 'occur)
  7251. ((equal in-emacs '(16)) 'org-occur)
  7252. (t nil))))
  7253. ((string= type "file")
  7254. (if (string-match "::?\\([0-9]+\\)\\'" path) ;; second : optional
  7255. ;; FIXME: It is unsafe to allow a single colon.
  7256. (setq line (string-to-number (match-string 1 path))
  7257. path (substring path 0 (match-beginning 0)))
  7258. (if (string-match "::\\(.+\\)\\'" path)
  7259. (setq search (match-string 1 path)
  7260. path (substring path 0 (match-beginning 0)))))
  7261. (org-open-file path in-emacs line search))
  7262. ((string= type "news")
  7263. (org-follow-gnus-link path))
  7264. ((string= type "bbdb")
  7265. (org-follow-bbdb-link path))
  7266. ((string= type "info")
  7267. (org-follow-info-link path))
  7268. ((string= type "gnus")
  7269. (let (group article)
  7270. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7271. (error "Error in Gnus link"))
  7272. (setq group (match-string 1 path)
  7273. article (match-string 3 path))
  7274. (org-follow-gnus-link group article)))
  7275. ((string= type "vm")
  7276. (let (folder article)
  7277. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7278. (error "Error in VM link"))
  7279. (setq folder (match-string 1 path)
  7280. article (match-string 3 path))
  7281. ;; in-emacs is the prefix arg, will be interpreted as read-only
  7282. (org-follow-vm-link folder article in-emacs)))
  7283. ((string= type "wl")
  7284. (let (folder article)
  7285. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7286. (error "Error in Wanderlust link"))
  7287. (setq folder (match-string 1 path)
  7288. article (match-string 3 path))
  7289. (org-follow-wl-link folder article)))
  7290. ((string= type "mhe")
  7291. (let (folder article)
  7292. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7293. (error "Error in MHE link"))
  7294. (setq folder (match-string 1 path)
  7295. article (match-string 3 path))
  7296. (org-follow-mhe-link folder article)))
  7297. ((string= type "rmail")
  7298. (let (folder article)
  7299. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7300. (error "Error in RMAIL link"))
  7301. (setq folder (match-string 1 path)
  7302. article (match-string 3 path))
  7303. (org-follow-rmail-link folder article)))
  7304. ((string= type "shell")
  7305. (let ((cmd path))
  7306. (while (string-match "@{" cmd)
  7307. (setq cmd (replace-match "<" t t cmd)))
  7308. (while (string-match "@}" cmd)
  7309. (setq cmd (replace-match ">" t t cmd)))
  7310. (if (or (not org-confirm-shell-link-function)
  7311. (funcall org-confirm-shell-link-function
  7312. (format "Execute \"%s\" in shell? "
  7313. (org-add-props cmd nil
  7314. 'face 'org-warning))))
  7315. (progn
  7316. (message "Executing %s" cmd)
  7317. (shell-command cmd))
  7318. (error "Abort"))))
  7319. ((string= type "elisp")
  7320. (let ((cmd path))
  7321. (if (or (not org-confirm-elisp-link-function)
  7322. (funcall org-confirm-elisp-link-function
  7323. (format "Execute \"%s\" as elisp? "
  7324. (org-add-props cmd nil
  7325. 'face 'org-warning))))
  7326. (message "%s => %s" cmd (eval (read cmd)))
  7327. (error "Abort"))))
  7328. (t
  7329. (browse-url-at-point))))))
  7330. (defun org-link-search (s &optional type)
  7331. "Search for a link search option.
  7332. When S is a CamelCaseWord, search for a target, or for a sentence containing
  7333. the words. If S is surrounded by forward slashes, it is interpreted as a
  7334. regular expression. In org-mode files, this will create an `org-occur'
  7335. sparse tree. In ordinary files, `occur' will be used to list matches.
  7336. If the current buffer is in `dired-mode', grep will be used to search
  7337. in all files."
  7338. (let ((case-fold-search t)
  7339. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7340. (pos (point))
  7341. (pre "") (post "")
  7342. words re0 re1 re2 re3 re4 re5 re2a reall camel)
  7343. (cond
  7344. ;; First check if there are any special
  7345. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7346. ;; Now try the builtin stuff
  7347. ((save-excursion
  7348. (goto-char (point-min))
  7349. (and
  7350. (re-search-forward
  7351. (concat "<<" (regexp-quote s0) ">>") nil t)
  7352. (setq pos (match-beginning 0))))
  7353. ;; There is an exact target for this
  7354. (goto-char pos))
  7355. ((string-match "^/\\(.*\\)/$" s)
  7356. ;; A regular expression
  7357. (cond
  7358. ((eq major-mode 'org-mode)
  7359. (org-occur (match-string 1 s)))
  7360. ;;((eq major-mode 'dired-mode)
  7361. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7362. (t (org-do-occur (match-string 1 s)))))
  7363. ((or (setq camel (string-match (concat "^" org-camel-regexp "$") s))
  7364. t)
  7365. ;; A camel or a normal search string
  7366. (when (equal (string-to-char s) ?*)
  7367. ;; Anchor on headlines, post may include tags.
  7368. (setq pre "^\\*+[ \t]*\\(?:\\sw+\\)?[ \t]*"
  7369. post "[ \t]*\\(?:[ \t]+:[a-zA-Z_@0-9:+]:[ \t]*\\)?$"
  7370. s (substring s 1)))
  7371. (remove-text-properties
  7372. 0 (length s)
  7373. '(face nil mouse-face nil keymap nil fontified nil) s)
  7374. ;; Make a series of regular expressions to find a match
  7375. (setq words
  7376. (if camel
  7377. (org-camel-to-words s)
  7378. (org-split-string s "[ \n\r\t]+"))
  7379. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7380. re2 (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t]+") "\\)[ \t\r\n]")
  7381. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7382. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7383. re1 (concat pre re2 post)
  7384. re3 (concat pre re4 post)
  7385. re5 (concat pre ".*" re4)
  7386. re2 (concat pre re2)
  7387. re2a (concat pre re2a)
  7388. re4 (concat pre re4)
  7389. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7390. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7391. re5 "\\)"
  7392. ))
  7393. (cond
  7394. ((eq type 'org-occur) (org-occur reall))
  7395. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7396. (t (goto-char (point-min))
  7397. (if (or (org-search-not-link re0 nil t)
  7398. (org-search-not-link re1 nil t)
  7399. (org-search-not-link re2 nil t)
  7400. (org-search-not-link re2a nil t)
  7401. (org-search-not-link re3 nil t)
  7402. (org-search-not-link re4 nil t)
  7403. (org-search-not-link re5 nil t)
  7404. )
  7405. (goto-char (match-beginning 1))
  7406. (goto-char pos)
  7407. (error "No match")))))
  7408. (t
  7409. ;; Normal string-search
  7410. (goto-char (point-min))
  7411. (if (search-forward s nil t)
  7412. (goto-char (match-beginning 0))
  7413. (error "No match"))))
  7414. (and (eq major-mode 'org-mode) (org-show-hierarchy-above))))
  7415. (defun org-search-not-link (&rest args)
  7416. "Execute `re-search-forward', but only accept matches that are not a link."
  7417. (catch 'exit
  7418. (let ((pos (point)) p1)
  7419. (while (apply 're-search-forward args)
  7420. (setq p1 (point))
  7421. (if (not (save-match-data
  7422. (and (re-search-backward "\\[\\[" nil t)
  7423. (looking-at org-bracket-link-regexp)
  7424. (<= (match-beginning 0) p1)
  7425. (>= (match-end 0) p1))))
  7426. (progn (goto-char (match-end 0))
  7427. (throw 'exit (point)))
  7428. (goto-char (match-end 0)))))))
  7429. (defun org-do-occur (regexp &optional cleanup)
  7430. "Call the Emacs command `occur'.
  7431. If CLEANUP is non-nil, remove the printout of the regular expression
  7432. in the *Occur* buffer. This is useful if the regex is long and not useful
  7433. to read."
  7434. (occur regexp)
  7435. (when cleanup
  7436. (let ((cwin (selected-window)) win beg end)
  7437. (when (setq win (get-buffer-window "*Occur*"))
  7438. (select-window win))
  7439. (goto-char (point-min))
  7440. (when (re-search-forward "match[a-z]+" nil t)
  7441. (setq beg (match-end 0))
  7442. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7443. (setq end (1- (match-beginning 0)))))
  7444. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  7445. (goto-char (point-min))
  7446. (select-window cwin))))
  7447. (defvar org-mark-ring nil
  7448. "Mark ring for positions before jumps in Org-mode.")
  7449. (defvar org-mark-ring-last-goto nil
  7450. "Last position in the mark ring used to go back.")
  7451. ;; Fill and close the ring
  7452. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7453. (loop for i from 1 to org-mark-ring-length do
  7454. (push (make-marker) org-mark-ring))
  7455. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7456. org-mark-ring)
  7457. (defun org-mark-ring-push (&optional pos buffer)
  7458. "Put the current position or POS into the mark ring and rotate it."
  7459. (interactive)
  7460. (setq pos (or pos (point)))
  7461. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7462. (move-marker (car org-mark-ring)
  7463. (or pos (point))
  7464. (or buffer (current-buffer)))
  7465. (message
  7466. (substitute-command-keys
  7467. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7468. (defun org-mark-ring-goto (&optional n)
  7469. "Jump to the previous position in the mark ring.
  7470. With prefix arg N, jump back that many stored positions. When
  7471. called several times in succession, walk through the entire ring.
  7472. Org-mode commands jumping to a different position in the current file,
  7473. or to another Org-mode file, automatically push the old position
  7474. onto the ring."
  7475. (interactive "p")
  7476. (let (p m)
  7477. (if (eq last-command this-command)
  7478. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7479. (setq p org-mark-ring))
  7480. (setq org-mark-ring-last-goto p)
  7481. (setq m (car p))
  7482. (switch-to-buffer (marker-buffer m))
  7483. (goto-char m)
  7484. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-hierarchy-above))))
  7485. (defun org-camel-to-words (s)
  7486. "Split \"CamelCaseWords\" to (\"Camel\" \"Case\" \"Words\")."
  7487. (let ((case-fold-search nil)
  7488. words)
  7489. (while (string-match "[a-z][A-Z]" s)
  7490. (push (substring s 0 (1+ (match-beginning 0))) words)
  7491. (setq s (substring s (1+ (match-beginning 0)))))
  7492. (nreverse (cons s words))))
  7493. (defun org-remove-angle-brackets (s)
  7494. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7495. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7496. s)
  7497. (defun org-add-angle-brackets (s)
  7498. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7499. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7500. s)
  7501. (defun org-follow-bbdb-link (name)
  7502. "Follow a BBDB link to NAME."
  7503. (require 'bbdb)
  7504. (let ((inhibit-redisplay t)
  7505. (bbdb-electric-p nil))
  7506. (catch 'exit
  7507. ;; Exact match on name
  7508. (bbdb-name (concat "\\`" name "\\'") nil)
  7509. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  7510. ;; Exact match on name
  7511. (bbdb-company (concat "\\`" name "\\'") nil)
  7512. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  7513. ;; Partial match on name
  7514. (bbdb-name name nil)
  7515. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  7516. ;; Partial match on company
  7517. (bbdb-company name nil)
  7518. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  7519. ;; General match including network address and notes
  7520. (bbdb name nil)
  7521. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  7522. (delete-window (get-buffer-window "*BBDB*"))
  7523. (error "No matching BBDB record")))))
  7524. (defun org-follow-info-link (name)
  7525. "Follow an info file & node link to NAME."
  7526. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  7527. (string-match "\\(.*\\)" name))
  7528. (progn
  7529. (require 'info)
  7530. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  7531. (Info-find-node (match-string 1 name) (match-string 2 name))
  7532. (Info-find-node (match-string 1 name) "Top")))
  7533. (message (concat "Could not open: " name))))
  7534. (defun org-follow-gnus-link (&optional group article)
  7535. "Follow a Gnus link to GROUP and ARTICLE."
  7536. (require 'gnus)
  7537. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  7538. (if group (gnus-fetch-group group))
  7539. (if article
  7540. (or (gnus-summary-goto-article article nil 'force)
  7541. (if (fboundp 'gnus-summary-insert-cached-articles)
  7542. (progn
  7543. (gnus-summary-insert-cached-articles)
  7544. (gnus-summary-goto-article article nil 'force))
  7545. (message "Message could not be found.")))))
  7546. (defun org-follow-vm-link (&optional folder article readonly)
  7547. "Follow a VM link to FOLDER and ARTICLE."
  7548. (require 'vm)
  7549. (setq article (org-add-angle-brackets article))
  7550. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  7551. ;; ange-ftp or efs or tramp access
  7552. (let ((user (or (match-string 1 folder) (user-login-name)))
  7553. (host (match-string 2 folder))
  7554. (file (match-string 3 folder)))
  7555. (cond
  7556. ((featurep 'tramp)
  7557. ;; use tramp to access the file
  7558. (if (featurep 'xemacs)
  7559. (setq folder (format "[%s@%s]%s" user host file))
  7560. (setq folder (format "/%s@%s:%s" user host file))))
  7561. (t
  7562. ;; use ange-ftp or efs
  7563. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  7564. (setq folder (format "/%s@%s:%s" user host file))))))
  7565. (when folder
  7566. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  7567. (sit-for 0.1)
  7568. (when article
  7569. (vm-select-folder-buffer)
  7570. (widen)
  7571. (let ((case-fold-search t))
  7572. (goto-char (point-min))
  7573. (if (not (re-search-forward
  7574. (concat "^" "message-id: *" (regexp-quote article))))
  7575. (error "Could not find the specified message in this folder"))
  7576. (vm-isearch-update)
  7577. (vm-isearch-narrow)
  7578. (vm-beginning-of-message)
  7579. (vm-summarize)))))
  7580. (defun org-follow-wl-link (folder article)
  7581. "Follow a Wanderlust link to FOLDER and ARTICLE."
  7582. (setq article (org-add-angle-brackets article))
  7583. (wl-summary-goto-folder-subr folder 'no-sync t nil t)
  7584. (if article (wl-summary-jump-to-msg-by-message-id article ">"))
  7585. (wl-summary-redisplay))
  7586. (defun org-follow-rmail-link (folder article)
  7587. "Follow an RMAIL link to FOLDER and ARTICLE."
  7588. (setq article (org-add-angle-brackets article))
  7589. (let (message-number)
  7590. (save-excursion
  7591. (save-window-excursion
  7592. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  7593. (setq message-number
  7594. (save-restriction
  7595. (widen)
  7596. (goto-char (point-max))
  7597. (if (re-search-backward
  7598. (concat "^Message-ID:\\s-+" (regexp-quote
  7599. (or article "")))
  7600. nil t)
  7601. (rmail-what-message))))))
  7602. (if message-number
  7603. (progn
  7604. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  7605. (rmail-show-message message-number)
  7606. message-number)
  7607. (error "Message not found"))))
  7608. ;; mh-e integration based on planner-mode
  7609. (defun org-mhe-get-message-real-folder ()
  7610. "Return the name of the current message real folder, so if you use
  7611. sequences, it will now work."
  7612. (save-excursion
  7613. (let* ((folder
  7614. (if (equal major-mode 'mh-folder-mode)
  7615. mh-current-folder
  7616. ;; Refer to the show buffer
  7617. mh-show-folder-buffer))
  7618. (end-index
  7619. (if (boundp 'mh-index-folder)
  7620. (min (length mh-index-folder) (length folder))))
  7621. )
  7622. ;; a simple test on mh-index-data does not work, because
  7623. ;; mh-index-data is always nil in a show buffer.
  7624. (if (and (boundp 'mh-index-folder)
  7625. (string= mh-index-folder (substring folder 0 end-index)))
  7626. (if (equal major-mode 'mh-show-mode)
  7627. (save-window-excursion
  7628. (when (buffer-live-p (get-buffer folder))
  7629. (progn
  7630. (pop-to-buffer folder)
  7631. (org-mhe-get-message-folder-from-index)
  7632. )
  7633. ))
  7634. (org-mhe-get-message-folder-from-index)
  7635. )
  7636. folder
  7637. )
  7638. )))
  7639. (defun org-mhe-get-message-folder-from-index ()
  7640. "Returns the name of the message folder in a index folder buffer."
  7641. (save-excursion
  7642. (mh-index-previous-folder)
  7643. (re-search-forward "^\\(+.*\\)$" nil t)
  7644. (message (match-string 1))))
  7645. (defun org-mhe-get-message-folder ()
  7646. "Return the name of the current message folder. Be careful if you
  7647. use sequences."
  7648. (save-excursion
  7649. (if (equal major-mode 'mh-folder-mode)
  7650. mh-current-folder
  7651. ;; Refer to the show buffer
  7652. mh-show-folder-buffer)))
  7653. (defun org-mhe-get-message-num ()
  7654. "Return the number of the current message. Be careful if you
  7655. use sequences."
  7656. (save-excursion
  7657. (if (equal major-mode 'mh-folder-mode)
  7658. (mh-get-msg-num nil)
  7659. ;; Refer to the show buffer
  7660. (mh-show-buffer-message-number))))
  7661. (defun org-mhe-get-header (header)
  7662. "Return a header of the message in folder mode. This will create a
  7663. show buffer for the corresponding message. If you have a more clever
  7664. idea..."
  7665. (let* ((folder (org-mhe-get-message-folder))
  7666. (num (org-mhe-get-message-num))
  7667. (buffer (get-buffer-create (concat "show-" folder)))
  7668. (header-field))
  7669. (with-current-buffer buffer
  7670. (mh-display-msg num folder)
  7671. (if (equal major-mode 'mh-folder-mode)
  7672. (mh-header-display)
  7673. (mh-show-header-display))
  7674. (set-buffer buffer)
  7675. (setq header-field (mh-get-header-field header))
  7676. (if (equal major-mode 'mh-folder-mode)
  7677. (mh-show)
  7678. (mh-show-show))
  7679. header-field)))
  7680. (defun org-follow-mhe-link (folder article)
  7681. "Follow an MHE link to FOLDER and ARTICLE.
  7682. If ARTICLE is nil FOLDER is shown. If the configuration variable
  7683. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  7684. ARTICLE is searched in all folders. Indexed searches (swish++,
  7685. namazu, and others supported by MH-E) will always search in all
  7686. folders."
  7687. (require 'mh-e)
  7688. (require 'mh-search)
  7689. (require 'mh-utils)
  7690. (mh-find-path)
  7691. (if (not article)
  7692. (mh-visit-folder (mh-normalize-folder-name folder))
  7693. (setq article (org-add-angle-brackets article))
  7694. (mh-search-choose)
  7695. (if (equal mh-searcher 'pick)
  7696. (progn
  7697. (mh-search folder (list "--message-id" article))
  7698. (when (and org-mhe-search-all-folders
  7699. (not (org-mhe-get-message-real-folder)))
  7700. (kill-this-buffer)
  7701. (mh-search "+" (list "--message-id" article))))
  7702. (mh-search "+" article))
  7703. (if (org-mhe-get-message-real-folder)
  7704. (mh-show-msg 1)
  7705. (kill-this-buffer)
  7706. (error "Message not found"))))
  7707. ;; BibTeX links
  7708. ;; Use the custom search meachnism to construct and use search strings for
  7709. ;; file links to BibTeX database entries.
  7710. (defun org-create-file-search-in-bibtex ()
  7711. "Create the search string and description for a BibTeX database entry."
  7712. (when (eq major-mode 'bibtex-mode)
  7713. ;; yes, we want to construct this search string.
  7714. ;; Make a good description for this entry, using names, year and the title
  7715. ;; Put it into the `description' variable which is dynamically scoped.
  7716. (let ((bibtex-autokey-names 1)
  7717. (bibtex-autokey-names-stretch 1)
  7718. (bibtex-autokey-name-case-convert-function 'identity)
  7719. (bibtex-autokey-name-separator " & ")
  7720. (bibtex-autokey-additional-names " et al.")
  7721. (bibtex-autokey-year-length 4)
  7722. (bibtex-autokey-name-year-separator " ")
  7723. (bibtex-autokey-titlewords 3)
  7724. (bibtex-autokey-titleword-separator " ")
  7725. (bibtex-autokey-titleword-case-convert-function 'identity)
  7726. (bibtex-autokey-titleword-length 'infty)
  7727. (bibtex-autokey-year-title-separator ": "))
  7728. (setq description (bibtex-generate-autokey)))
  7729. ;; Now parse the entry, get the key and return it.
  7730. (save-excursion
  7731. (bibtex-beginning-of-entry)
  7732. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  7733. (defun org-execute-file-search-in-bibtex (s)
  7734. "Find the link search string S as a key for a database entry."
  7735. (when (eq major-mode 'bibtex-mode)
  7736. ;; Yes, we want to do the search in this file.
  7737. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  7738. (goto-char (point-min))
  7739. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  7740. (regexp-quote s) "[ \t\n]*,") nil t)
  7741. (goto-char (match-beginning 0)))
  7742. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  7743. ;; Use double prefix to indicate that any web link should be browsed
  7744. (let ((b (current-buffer)) (p (point)))
  7745. ;; Restore the window configuration because we just use the web link
  7746. (set-window-configuration org-window-config-before-follow-link)
  7747. (save-excursion (set-buffer b) (goto-char p)
  7748. (bibtex-url)))
  7749. (recenter 0)) ; Move entry start to beginning of window
  7750. ;; return t to indicate that the search is done.
  7751. t))
  7752. ;; Finally add the functions to the right hooks.
  7753. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  7754. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  7755. ;; end of Bibtex link setup
  7756. (defun org-upgrade-old-links (&optional query-description)
  7757. "Transfer old <...> style links to new [[...]] style links.
  7758. With arg query-description, ask at each match for a description text to use
  7759. for this link."
  7760. (interactive (list (y-or-n-p "Would you like to be queried for a description at each link?")))
  7761. (save-excursion
  7762. (goto-char (point-min))
  7763. (let ((re (concat "\\([^[]\\)<\\("
  7764. "\\(" (mapconcat 'identity org-link-types "\\|")
  7765. "\\):"
  7766. "[^" org-non-link-chars "]+\\)>"))
  7767. l1 l2 (cnt 0))
  7768. (while (re-search-forward re nil t)
  7769. (setq cnt (1+ cnt)
  7770. l1 (org-match-string-no-properties 2)
  7771. l2 (save-match-data (org-link-escape l1)))
  7772. (when query-description (setq l1 (read-string "Desc: " l1)))
  7773. (if (equal l1 l2)
  7774. (replace-match (concat (match-string 1) "[[" l1 "]]") t t)
  7775. (replace-match (concat (match-string 1) "[[" l2 "][" l1 "]]") t t)))
  7776. (message "%d matches have beed treated" cnt))))
  7777. (defun org-open-file (path &optional in-emacs line search)
  7778. "Open the file at PATH.
  7779. First, this expands any special file name abbreviations. Then the
  7780. configuration variable `org-file-apps' is checked if it contains an
  7781. entry for this file type, and if yes, the corresponding command is launched.
  7782. If no application is found, Emacs simply visits the file.
  7783. With optional argument IN-EMACS, Emacs will visit the file.
  7784. Optional LINE specifies a line to go to, optional SEARCH a string to
  7785. search for. If LINE or SEARCH is given, the file will always be
  7786. opened in Emacs.
  7787. If the file does not exist, an error is thrown."
  7788. (setq in-emacs (or in-emacs line search))
  7789. (let* ((file (if (equal path "")
  7790. buffer-file-name
  7791. (convert-standard-filename (org-expand-file-name path))))
  7792. (dirp (file-directory-p file))
  7793. (dfile (downcase file))
  7794. (old-buffer (current-buffer))
  7795. (old-pos (point))
  7796. (old-mode major-mode)
  7797. ext cmd apps)
  7798. (if (and (not (file-exists-p file))
  7799. (not org-open-non-existing-files))
  7800. (error "No such file: %s" file))
  7801. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7802. (setq ext (match-string 1 dfile))
  7803. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7804. (setq ext (match-string 1 dfile))))
  7805. (setq apps (append org-file-apps (org-default-apps)))
  7806. (if in-emacs
  7807. (setq cmd 'emacs)
  7808. (setq cmd (or (and dirp (cdr (assoc 'directory apps)))
  7809. (cdr (assoc ext apps))
  7810. (cdr (assoc t apps)))))
  7811. (when (eq cmd 'mailcap)
  7812. (require 'mailcap)
  7813. (mailcap-parse-mailcaps)
  7814. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7815. (command (mailcap-mime-info mime-type)))
  7816. (if (stringp command)
  7817. (setq cmd command)
  7818. (setq cmd 'emacs))))
  7819. (cond
  7820. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7821. ;; Normalize use of quote, this can vary.
  7822. (if (string-match "['\"]%s['\"]" cmd)
  7823. (setq cmd (replace-match "'%s'" t t cmd)))
  7824. (setq cmd (format cmd file))
  7825. (save-window-excursion
  7826. (shell-command (concat cmd " &"))))
  7827. ((or (stringp cmd)
  7828. (eq cmd 'emacs))
  7829. (unless (equal (file-truename file) (file-truename (or buffer-file-name "")))
  7830. (funcall (cdr (assq 'file org-link-frame-setup)) file))
  7831. (if line (goto-line line)
  7832. (if search (org-link-search search))))
  7833. ((consp cmd)
  7834. (eval cmd))
  7835. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7836. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  7837. (or (not (equal old-buffer (current-buffer)))
  7838. (not (equal old-pos (point))))
  7839. (org-mark-ring-push old-pos old-buffer))))
  7840. (defun org-default-apps ()
  7841. "Return the default applications for this operating system."
  7842. (cond
  7843. ((eq system-type 'darwin)
  7844. org-file-apps-defaults-macosx)
  7845. ((eq system-type 'windows-nt)
  7846. org-file-apps-defaults-windowsnt)
  7847. (t org-file-apps-defaults-gnu)))
  7848. (defun org-expand-file-name (path)
  7849. "Replace special path abbreviations and expand the file name."
  7850. (expand-file-name path))
  7851. (defvar org-insert-link-history nil
  7852. "Minibuffer history for links inserted with `org-insert-link'.")
  7853. (defvar org-stored-links nil
  7854. "Contains the links stored with `org-store-link'.")
  7855. ;;;###autoload
  7856. (defun org-store-link (arg)
  7857. "\\<org-mode-map>Store an org-link to the current location.
  7858. This link can later be inserted into an org-buffer with
  7859. \\[org-insert-link].
  7860. For some link types, a prefix arg is interpreted:
  7861. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  7862. For file links, arg negates `org-context-in-file-links'."
  7863. (interactive "P")
  7864. (let (link cpltxt desc description search txt (pos (point)))
  7865. (cond
  7866. ((eq major-mode 'bbdb-mode)
  7867. (setq cpltxt (concat
  7868. "bbdb:"
  7869. (or (bbdb-record-name (bbdb-current-record))
  7870. (bbdb-record-company (bbdb-current-record))))
  7871. link (org-make-link cpltxt)))
  7872. ((eq major-mode 'Info-mode)
  7873. (setq link (org-make-link "info:"
  7874. (file-name-nondirectory Info-current-file)
  7875. ":" Info-current-node))
  7876. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  7877. ":" Info-current-node)))
  7878. ((eq major-mode 'calendar-mode)
  7879. (let ((cd (calendar-cursor-to-date)))
  7880. (setq link
  7881. (format-time-string
  7882. (car org-time-stamp-formats)
  7883. (apply 'encode-time
  7884. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7885. nil nil nil))))))
  7886. ((or (eq major-mode 'vm-summary-mode)
  7887. (eq major-mode 'vm-presentation-mode))
  7888. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  7889. (vm-follow-summary-cursor)
  7890. (save-excursion
  7891. (vm-select-folder-buffer)
  7892. (let* ((message (car vm-message-pointer))
  7893. (folder buffer-file-name)
  7894. (subject (vm-su-subject message))
  7895. (author (vm-su-full-name message))
  7896. (message-id (vm-su-message-id message)))
  7897. (setq message-id (org-remove-angle-brackets message-id))
  7898. (setq folder (abbreviate-file-name folder))
  7899. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  7900. folder)
  7901. (setq folder (replace-match "" t t folder)))
  7902. (setq cpltxt (concat author " on: " subject))
  7903. (setq link (org-make-link "vm:" folder "#" message-id)))))
  7904. ((eq major-mode 'wl-summary-mode)
  7905. (let* ((msgnum (wl-summary-message-number))
  7906. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  7907. msgnum 'message-id))
  7908. (wl-message-entity (elmo-msgdb-overview-get-entity
  7909. msgnum (wl-summary-buffer-msgdb)))
  7910. (author (wl-summary-line-from)) ; FIXME: correct?
  7911. (subject "???")) ; FIXME:
  7912. (setq message-id (org-remove-angle-brackets message-id))
  7913. (setq cpltxt (concat author " on: " subject))
  7914. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  7915. "#" message-id))))
  7916. ((or (equal major-mode 'mh-folder-mode)
  7917. (equal major-mode 'mh-show-mode))
  7918. (let ((from-header (org-mhe-get-header "From:"))
  7919. (to-header (org-mhe-get-header "To:"))
  7920. (subject (org-mhe-get-header "Subject:")))
  7921. (setq cpltxt (concat from-header " on: " subject))
  7922. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  7923. (org-remove-angle-brackets
  7924. (org-mhe-get-header "Message-Id:"))))))
  7925. ((eq major-mode 'rmail-mode)
  7926. (save-excursion
  7927. (save-restriction
  7928. (rmail-narrow-to-non-pruned-header)
  7929. (let ((folder buffer-file-name)
  7930. (message-id (mail-fetch-field "message-id"))
  7931. (author (mail-fetch-field "from"))
  7932. (subject (mail-fetch-field "subject")))
  7933. (setq message-id (org-remove-angle-brackets message-id))
  7934. (setq cpltxt (concat author " on: " subject))
  7935. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  7936. ((eq major-mode 'gnus-group-mode)
  7937. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  7938. (gnus-group-group-name)) ; version
  7939. ((fboundp 'gnus-group-name)
  7940. (gnus-group-name))
  7941. (t "???"))))
  7942. (setq cpltxt (concat
  7943. (if (org-xor arg org-usenet-links-prefer-google)
  7944. "http://groups.google.com/groups?group="
  7945. "gnus:")
  7946. group)
  7947. link (org-make-link cpltxt))))
  7948. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  7949. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  7950. (gnus-summary-beginning-of-article)
  7951. (let* ((group (car gnus-article-current))
  7952. (article (cdr gnus-article-current))
  7953. (header (gnus-summary-article-header article))
  7954. (author (mail-header-from header))
  7955. (message-id (mail-header-id header))
  7956. (date (mail-header-date header))
  7957. (subject (gnus-summary-subject-string)))
  7958. (setq cpltxt (concat author " on: " subject))
  7959. (if (org-xor arg org-usenet-links-prefer-google)
  7960. (setq link
  7961. (concat
  7962. cpltxt "\n "
  7963. (format "http://groups.google.com/groups?as_umsgid=%s"
  7964. (org-fixup-message-id-for-http message-id))))
  7965. (setq link (org-make-link "gnus:" group
  7966. "#" (number-to-string article))))))
  7967. ((eq major-mode 'w3-mode)
  7968. (setq cpltxt (url-view-url t)
  7969. link (org-make-link cpltxt)))
  7970. ((eq major-mode 'w3m-mode)
  7971. (setq cpltxt w3m-current-url
  7972. link (org-make-link cpltxt)))
  7973. ((setq search (run-hook-with-args-until-success
  7974. 'org-create-file-search-functions))
  7975. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7976. "::" search))
  7977. (setq cpltxt (or description link))) ;; FIXME: is this the best way?
  7978. ((eq major-mode 'org-mode)
  7979. ;; Just link to current headline
  7980. (setq cpltxt (concat "file:"
  7981. (abbreviate-file-name buffer-file-name)))
  7982. ;; Add a context search string
  7983. (when (org-xor org-context-in-file-links arg)
  7984. ;; Check if we are on a target
  7985. (if (save-excursion
  7986. (skip-chars-forward "^>\n\r")
  7987. (and (re-search-backward "<<" nil t)
  7988. (looking-at "<<\\(.*?\\)>>")
  7989. (<= (match-beginning 0) pos)
  7990. (>= (match-end 0) pos)))
  7991. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  7992. (setq txt (cond
  7993. ((org-on-heading-p) nil)
  7994. ((org-region-active-p)
  7995. (buffer-substring (region-beginning) (region-end)))
  7996. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  7997. (when (string-match "\\S-" txt)
  7998. (setq cpltxt
  7999. (concat cpltxt "::"
  8000. (if org-file-link-context-use-camel-case
  8001. (org-make-org-heading-camel txt)
  8002. (org-make-org-heading-search-string txt)))
  8003. desc "NONE"))))
  8004. (if (string-match "::\\'" cpltxt)
  8005. (setq cpltxt (substring cpltxt 0 -2)))
  8006. (setq link (org-make-link cpltxt)))
  8007. (buffer-file-name
  8008. ;; Just link to this file here.
  8009. (setq cpltxt (concat "file:"
  8010. (abbreviate-file-name buffer-file-name)))
  8011. ;; Add a context string
  8012. (when (org-xor org-context-in-file-links arg)
  8013. (setq txt (if (org-region-active-p)
  8014. (buffer-substring (region-beginning) (region-end))
  8015. (buffer-substring (point-at-bol) (point-at-eol))))
  8016. ;; Only use search option if there is some text.
  8017. (when (string-match "\\S-" txt)
  8018. (setq cpltxt
  8019. (concat cpltxt "::"
  8020. (if org-file-link-context-use-camel-case
  8021. (org-make-org-heading-camel txt)
  8022. (org-make-org-heading-search-string txt)))
  8023. desc "NONE")))
  8024. (setq link (org-make-link cpltxt)))
  8025. ((interactive-p)
  8026. (error "Cannot link to a buffer which is not visiting a file"))
  8027. (t (setq link nil)))
  8028. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8029. (setq link (or link cpltxt)
  8030. desc (or desc cpltxt))
  8031. (if (equal desc "NONE") (setq desc nil))
  8032. (if (and (interactive-p) link)
  8033. (progn
  8034. (setq org-stored-links
  8035. (cons (list cpltxt link desc) org-stored-links))
  8036. (message "Stored: %s" (or cpltxt link)))
  8037. (org-make-link-string link desc))))
  8038. (defun org-make-org-heading-search-string (&optional string heading)
  8039. "Make search string for STRING or current headline."
  8040. (interactive)
  8041. (let ((s (or string (org-get-heading))))
  8042. (unless (and string (not heading))
  8043. ;; We are using a headline, clean up garbage in there.
  8044. (if (string-match org-todo-regexp s)
  8045. (setq s (replace-match "" t t s)))
  8046. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  8047. (setq s (replace-match "" t t s)))
  8048. (setq s (org-trim s))
  8049. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8050. org-comment-string "\\)") s)
  8051. (setq s (replace-match "" t t s)))
  8052. (while (string-match org-ts-regexp s)
  8053. (setq s (replace-match "" t t s))))
  8054. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  8055. (setq s (replace-match " " t t s)))
  8056. (or string (setq s (concat "*" s))) ; Add * for headlines
  8057. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8058. (defun org-make-org-heading-camel (&optional string heading)
  8059. "Make a CamelCase string for STRING or the current headline."
  8060. (interactive)
  8061. (let ((s (or string (org-get-heading))))
  8062. (unless (and string (not heading))
  8063. ;; We are using a headline, clean up garbage in there.
  8064. (if (string-match org-todo-regexp s)
  8065. (setq s (replace-match "" t t s)))
  8066. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  8067. (setq s (replace-match "" t t s)))
  8068. (setq s (org-trim s))
  8069. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8070. org-comment-string "\\)") s)
  8071. (setq s (replace-match "" t t s)))
  8072. (while (string-match org-ts-regexp s)
  8073. (setq s (replace-match "" t t s))))
  8074. (while (string-match "[^a-zA-Z_ \t]+" s)
  8075. (setq s (replace-match " " t t s)))
  8076. (or string (setq s (concat "*" s))) ; Add * for headlines
  8077. (mapconcat 'capitalize (org-split-string s "[ \t]+") "")))
  8078. (defun org-make-link (&rest strings)
  8079. "Concatenate STRINGS, format resulting string with `org-link-format'."
  8080. (format org-link-format (apply 'concat strings)))
  8081. (defun org-make-link-string (link &optional description)
  8082. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8083. (if (eq org-link-style 'plain)
  8084. (if (equal description link)
  8085. link
  8086. (concat description "\n" link))
  8087. (when (stringp description)
  8088. ;; Remove brackets from the description, they are fatal.
  8089. (while (string-match "\\[\\|\\]" description)
  8090. (setq description (replace-match "" t t description))))
  8091. (when (equal (org-link-escape link) description)
  8092. ;; No description needed, it is identical
  8093. (setq description nil))
  8094. (when (and (not description)
  8095. (not (equal link (org-link-escape link))))
  8096. (setq description link))
  8097. (concat "[[" (org-link-escape link) "]"
  8098. (if description (concat "[" description "]") "")
  8099. "]")))
  8100. (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
  8101. "Association list of escapes for some characters problematic in links.")
  8102. (defun org-link-escape (text)
  8103. "Escape charaters in TEXT that are problematic for links."
  8104. (when text
  8105. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  8106. org-link-escape-chars "\\|")))
  8107. (while (string-match re text)
  8108. (setq text
  8109. (replace-match
  8110. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  8111. t t text)))
  8112. text)))
  8113. (defun org-link-unescape (text)
  8114. "Reverse the action of `org-link-escape'."
  8115. (when text
  8116. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  8117. org-link-escape-chars "\\|")))
  8118. (while (string-match re text)
  8119. (setq text
  8120. (replace-match
  8121. (car (rassoc (match-string 0 text) org-link-escape-chars))
  8122. t t text)))
  8123. text)))
  8124. (defun org-xor (a b)
  8125. "Exclusive or."
  8126. (if a (not b) b))
  8127. (defun org-get-header (header)
  8128. "Find a header field in the current buffer."
  8129. (save-excursion
  8130. (goto-char (point-min))
  8131. (let ((case-fold-search t) s)
  8132. (cond
  8133. ((eq header 'from)
  8134. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  8135. (setq s (match-string 1)))
  8136. (while (string-match "\"" s)
  8137. (setq s (replace-match "" t t s)))
  8138. (if (string-match "[<(].*" s)
  8139. (setq s (replace-match "" t t s))))
  8140. ((eq header 'message-id)
  8141. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  8142. (setq s (match-string 1))))
  8143. ((eq header 'subject)
  8144. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  8145. (setq s (match-string 1)))))
  8146. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  8147. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  8148. s)))
  8149. (defun org-fixup-message-id-for-http (s)
  8150. "Replace special characters in a message id, so it can be used in an http query."
  8151. (while (string-match "<" s)
  8152. (setq s (replace-match "%3C" t t s)))
  8153. (while (string-match ">" s)
  8154. (setq s (replace-match "%3E" t t s)))
  8155. (while (string-match "@" s)
  8156. (setq s (replace-match "%40" t t s)))
  8157. s)
  8158. (defun org-insert-link (&optional complete-file)
  8159. "Insert a link. At the prompt, enter the link.
  8160. Completion can be used to select a link previously stored with
  8161. `org-store-link'. When the empty string is entered (i.e. if you just
  8162. press RET at the prompt), the link defaults to the most recently
  8163. stored link. As SPC triggers completion in the minibuffer, you need to
  8164. use M-SPC or C-q SPC to force the insertion of a space character.
  8165. You will also be prompted for a description, and if one is given, it will
  8166. be displayed in the buffer instead of the link.
  8167. If there is already a link at point, this command will allow you to edit link
  8168. and description parts.
  8169. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  8170. selected using completion. The path to the file will be relative to
  8171. the current directory if the file is in the current directory or a
  8172. subdirectory. Otherwise, the link will be the absolute path as
  8173. completed in the minibuffer (i.e. normally ~/path/to/file).
  8174. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  8175. is in the current directory or below."
  8176. (interactive "P")
  8177. (let (link desc entry remove file (pos (point)))
  8178. (cond
  8179. ((save-excursion
  8180. (skip-chars-forward "^]\n\r")
  8181. (and (re-search-backward "\\[\\[" nil t)
  8182. (looking-at org-bracket-link-regexp)
  8183. (<= (match-beginning 0) pos)
  8184. (>= (match-end 0) pos)))
  8185. ;; We do have a link at point, and we are going to edit it.
  8186. (setq remove (list (match-beginning 0) (match-end 0)))
  8187. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8188. (setq link (read-string "Link: "
  8189. (org-link-unescape
  8190. (org-match-string-no-properties 1)))))
  8191. (complete-file
  8192. ;; Completing read for file names.
  8193. (setq file (read-file-name "File: "))
  8194. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8195. (pwd1 (file-name-as-directory (abbreviate-file-name
  8196. (expand-file-name ".")))))
  8197. (cond
  8198. ((equal complete-file '(16))
  8199. (setq link (org-make-link
  8200. "file:"
  8201. (abbreviate-file-name (expand-file-name file)))))
  8202. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8203. (setq link (org-make-link "file:" (match-string 1 file))))
  8204. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8205. (expand-file-name file))
  8206. (setq link (org-make-link
  8207. "file:" (match-string 1 (expand-file-name file)))))
  8208. (t (setq link (org-make-link "file:" file))))))
  8209. (t
  8210. ;; Read link, with completion for stored links.
  8211. (setq link (org-completing-read
  8212. "Link: " org-stored-links nil nil nil
  8213. org-insert-link-history
  8214. (or (car (car org-stored-links)))))
  8215. (setq entry (assoc link org-stored-links))
  8216. (if (not org-keep-stored-link-after-insertion)
  8217. (setq org-stored-links (delq (assoc link org-stored-links)
  8218. org-stored-links)))
  8219. (setq link (if entry (nth 1 entry) link)
  8220. desc (or desc (nth 2 entry)))))
  8221. (if (string-match org-plain-link-re link)
  8222. ;; URL-like link, normalize the use of angular brackets.
  8223. (setq link (org-make-link (org-remove-angle-brackets link))))
  8224. ;; Check if we are linking to the current file with a search option
  8225. ;; If yes, simplify the link by using only the search option.
  8226. (when (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link)
  8227. (let* ((path (match-string 1 link))
  8228. (case-fold-search nil)
  8229. (search (match-string 2 link)))
  8230. (save-match-data
  8231. (if (equal (file-truename buffer-file-name) (file-truename path))
  8232. ;; We are linking to this same file, with a search option
  8233. (setq link search)))))
  8234. ;; Check if we can/should use a relative path. If yes, simplify the link
  8235. (when (string-match "\\<file:\\(.*\\)" link)
  8236. (let* ((path (match-string 1 link))
  8237. (case-fold-search nil))
  8238. (cond
  8239. ((eq org-link-file-path-type 'absolute)
  8240. (setq path (abbreviate-file-name (expand-file-name path))))
  8241. ((eq org-link-file-path-type 'noabbrev)
  8242. (setq path (expand-file-name path)))
  8243. ((eq org-link-file-path-type 'relative)
  8244. (setq path (file-relative-name path)))
  8245. (t
  8246. (save-match-data
  8247. (if (string-match (concat "^" (regexp-quote
  8248. (file-name-as-directory
  8249. (expand-file-name "."))))
  8250. (expand-file-name path))
  8251. ;; We are linking a file with relative path name.
  8252. (setq path (substring (expand-file-name path)
  8253. (match-end 0)))))))
  8254. (setq link (concat "file:" path))))
  8255. (setq desc (read-string "Description: " desc))
  8256. (unless (string-match "\\S-" desc) (setq desc nil))
  8257. (if remove (apply 'delete-region remove))
  8258. (insert (org-make-link-string link desc))))
  8259. (defun org-completing-read (&rest args)
  8260. (let ((minibuffer-local-completion-map
  8261. (copy-keymap minibuffer-local-completion-map)))
  8262. (define-key minibuffer-local-completion-map " " 'self-insert-command)
  8263. (apply 'completing-read args)))
  8264. ;;; Hooks for remember.el
  8265. (defvar org-finish-function nil)
  8266. ;;;###autoload
  8267. (defun org-remember-annotation ()
  8268. "Return a link to the current location as an annotation for remember.el.
  8269. If you are using Org-mode files as target for data storage with
  8270. remember.el, then the annotations should include a link compatible with the
  8271. conventions in Org-mode. This function returns such a link."
  8272. (org-store-link nil))
  8273. (defconst org-remember-help
  8274. "Select a destination location for the note.
  8275. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  8276. RET at beg-of-buf -> Append to file as level 2 headline
  8277. RET on headline -> Store as sublevel entry to current headline
  8278. <left>/<right> -> before/after current headline, same headings level")
  8279. ;;;###autoload
  8280. (defun org-remember-apply-template ()
  8281. "Initialize *remember* buffer with template, invoke `org-mode'.
  8282. This function should be placed into `remember-mode-hook' and in fact requires
  8283. to be run from that hook to fucntion properly."
  8284. (if org-remember-templates
  8285. (let* ((entry (if (= (length org-remember-templates) 1)
  8286. (cdar org-remember-templates)
  8287. (message "Select template: %s"
  8288. (mapconcat
  8289. (lambda (x) (char-to-string (car x)))
  8290. org-remember-templates " "))
  8291. (cdr (assoc (read-char-exclusive) org-remember-templates))))
  8292. (tpl (car entry))
  8293. (file (if (consp (cdr entry)) (nth 1 entry)))
  8294. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  8295. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  8296. (v-u (concat "[" (substring v-t 1 -1) "]"))
  8297. (v-U (concat "[" (substring v-T 1 -1) "]"))
  8298. (v-a annotation) ; defined in `remember-mode'
  8299. (v-i initial) ; defined in `remember-mode'
  8300. (v-n user-full-name)
  8301. )
  8302. (unless tpl (setq tpl "") (message "No template") (ding))
  8303. (insert tpl) (goto-char (point-min))
  8304. (while (re-search-forward "%\\([tTuTai]\\)" nil t)
  8305. (when (and initial (equal (match-string 0) "%i"))
  8306. (save-match-data
  8307. (let* ((lead (buffer-substring
  8308. (point-at-bol) (match-beginning 0))))
  8309. (setq v-i (mapconcat 'identity
  8310. (org-split-string initial "\n")
  8311. (concat "\n" lead))))))
  8312. (replace-match
  8313. (or (eval (intern (concat "v-" (match-string 1)))) "")
  8314. t t))
  8315. (let ((org-startup-folded nil)
  8316. (org-startup-with-deadline-check nil))
  8317. (org-mode))
  8318. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  8319. (set (make-local-variable 'org-default-notes-file) file))
  8320. (goto-char (point-min))
  8321. (if (re-search-forward "%\\?" nil t) (replace-match "")))
  8322. (let ((org-startup-folded nil)
  8323. (org-startup-with-deadline-check nil))
  8324. (org-mode)))
  8325. (set (make-local-variable 'org-finish-function) 'remember-buffer))
  8326. ;;;###autoload
  8327. (defun org-remember-handler ()
  8328. "Store stuff from remember.el into an org file.
  8329. First prompts for an org file. If the user just presses return, the value
  8330. of `org-default-notes-file' is used.
  8331. Then the command offers the headings tree of the selected file in order to
  8332. file the text at a specific location.
  8333. You can either immediately press RET to get the note appended to the
  8334. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  8335. find a better place. Then press RET or <left> or <right> in insert the note.
  8336. Key Cursor position Note gets inserted
  8337. -----------------------------------------------------------------------------
  8338. RET buffer-start as level 2 heading at end of file
  8339. RET on headline as sublevel of the heading at cursor
  8340. RET no heading at cursor position, level taken from context.
  8341. Or use prefix arg to specify level manually.
  8342. <left> on headline as same level, before current heading
  8343. <right> on headline as same level, after current heading
  8344. So the fastest way to store the note is to press RET RET to append it to
  8345. the default file. This way your current train of thought is not
  8346. interrupted, in accordance with the principles of remember.el. But with
  8347. little extra effort, you can push it directly to the correct location.
  8348. Before being stored away, the function ensures that the text has a
  8349. headline, i.e. a first line that starts with a \"*\". If not, a headline
  8350. is constructed from the current date and some additional data.
  8351. If the variable `org-adapt-indentation' is non-nil, the entire text is
  8352. also indented so that it starts in the same column as the headline
  8353. \(i.e. after the stars).
  8354. See also the variable `org-reverse-note-order'."
  8355. (catch 'quit
  8356. (let* ((txt (buffer-substring (point-min) (point-max)))
  8357. (fastp current-prefix-arg)
  8358. (file (if fastp org-default-notes-file (org-get-org-file)))
  8359. (visiting (find-buffer-visiting file))
  8360. (org-startup-with-deadline-check nil)
  8361. (org-startup-folded nil)
  8362. (org-startup-align-all-tables nil)
  8363. spos level indent reversed)
  8364. ;; Modify text so that it becomes a nice subtree which can be inserted
  8365. ;; into an org tree.
  8366. (let* ((lines (split-string txt "\n"))
  8367. first)
  8368. ;; remove empty lines at the beginning
  8369. (while (and lines (string-match "^[ \t]*\n" (car lines)))
  8370. (setq lines (cdr lines)))
  8371. (setq first (car lines) lines (cdr lines))
  8372. (if (string-match "^\\*+" first)
  8373. ;; Is already a headline
  8374. (setq indent nil)
  8375. ;; We need to add a headline: Use time and first buffer line
  8376. (setq lines (cons first lines)
  8377. first (concat "* " (current-time-string)
  8378. " (" (remember-buffer-desc) ")")
  8379. indent " "))
  8380. (if (and org-adapt-indentation indent)
  8381. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  8382. (setq txt (concat first "\n"
  8383. (mapconcat 'identity lines "\n"))))
  8384. ;; Find the file
  8385. (if (not visiting)
  8386. (find-file-noselect file))
  8387. (with-current-buffer (get-file-buffer file)
  8388. (save-excursion (and (goto-char (point-min))
  8389. (not (re-search-forward "^\\* " nil t))
  8390. (insert "\n* Notes\n")))
  8391. (setq reversed (org-notes-order-reversed-p))
  8392. (save-excursion
  8393. (save-restriction
  8394. (widen)
  8395. ;; Ask the User for a location
  8396. (setq spos (if fastp 1 (org-get-location
  8397. (current-buffer)
  8398. org-remember-help)))
  8399. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  8400. ; not handle this note
  8401. (goto-char spos)
  8402. (cond ((bobp)
  8403. ;; Put it at the start or end, as level 2
  8404. (save-restriction
  8405. (widen)
  8406. (goto-char (if reversed (point-min) (point-max)))
  8407. (if (not (bolp)) (newline))
  8408. (org-paste-subtree 2 txt)))
  8409. ((and (org-on-heading-p nil) (not current-prefix-arg))
  8410. ;; Put it below this entry, at the beg/end of the subtree
  8411. (org-back-to-heading)
  8412. (setq level (funcall outline-level))
  8413. (if reversed
  8414. (outline-end-of-heading)
  8415. (outline-end-of-subtree))
  8416. (if (not (bolp)) (newline))
  8417. (beginning-of-line 1)
  8418. (org-paste-subtree (1+ level) txt))
  8419. (t
  8420. ;; Put it right there, with automatic level determined by
  8421. ;; org-paste-subtree or from prefix arg
  8422. (org-paste-subtree current-prefix-arg txt)))
  8423. (when remember-save-after-remembering
  8424. (save-buffer)
  8425. (if (not visiting) (kill-buffer (current-buffer)))))))))
  8426. t) ;; return t to indicate that we took care of this note.
  8427. (defun org-get-org-file ()
  8428. "Read a filename, with default directory `org-directory'."
  8429. (let ((default (or org-default-notes-file remember-data-file)))
  8430. (read-file-name (format "File name [%s]: " default)
  8431. (file-name-as-directory org-directory)
  8432. default)))
  8433. (defun org-notes-order-reversed-p ()
  8434. "Check if the current file should receive notes in reversed order."
  8435. (cond
  8436. ((not org-reverse-note-order) nil)
  8437. ((eq t org-reverse-note-order) t)
  8438. ((not (listp org-reverse-note-order)) nil)
  8439. (t (catch 'exit
  8440. (let ((all org-reverse-note-order)
  8441. entry)
  8442. (while (setq entry (pop all))
  8443. (if (string-match (car entry) buffer-file-name)
  8444. (throw 'exit (cdr entry))))
  8445. nil)))))
  8446. ;;; Tables
  8447. ;; Watch out: Here we are talking about two different kind of tables.
  8448. ;; Most of the code is for the tables created with the Org-mode table editor.
  8449. ;; Sometimes, we talk about tables created and edited with the table.el
  8450. ;; Emacs package. We call the former org-type tables, and the latter
  8451. ;; table.el-type tables.
  8452. (defun org-before-change-function (beg end)
  8453. "Every change indicates that a table might need an update."
  8454. (setq org-table-may-need-update t))
  8455. (defconst org-table-line-regexp "^[ \t]*|"
  8456. "Detects an org-type table line.")
  8457. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  8458. "Detects an org-type table line.")
  8459. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  8460. "Detects a table line marked for automatic recalculation.")
  8461. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  8462. "Detects a table line marked for automatic recalculation.")
  8463. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  8464. "Detects a table line marked for automatic recalculation.")
  8465. (defconst org-table-hline-regexp "^[ \t]*|-"
  8466. "Detects an org-type table hline.")
  8467. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  8468. "Detects a table-type table hline.")
  8469. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  8470. "Detects an org-type or table-type table.")
  8471. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  8472. "Searching from within a table (any type) this finds the first line
  8473. outside the table.")
  8474. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  8475. "Searching from within a table (any type) this finds the first line
  8476. outside the table.")
  8477. (defun org-table-create-with-table.el ()
  8478. "Use the table.el package to insert a new table.
  8479. If there is already a table at point, convert between Org-mode tables
  8480. and table.el tables."
  8481. (interactive)
  8482. (require 'table)
  8483. (cond
  8484. ((org-at-table.el-p)
  8485. (if (y-or-n-p "Convert table to Org-mode table? ")
  8486. (org-table-convert)))
  8487. ((org-at-table-p)
  8488. (if (y-or-n-p "Convert table to table.el table? ")
  8489. (org-table-convert)))
  8490. (t (call-interactively 'table-insert))))
  8491. (defun org-table-create-or-convert-from-region (arg)
  8492. "Convert region to table, or create an empty table.
  8493. If there is an active region, convert it to a table. If there is no such
  8494. region, create an empty table."
  8495. (interactive "P")
  8496. (if (org-region-active-p)
  8497. (org-table-convert-region (region-beginning) (region-end) arg)
  8498. (org-table-create arg)))
  8499. (defun org-table-create (&optional size)
  8500. "Query for a size and insert a table skeleton.
  8501. SIZE is a string Columns x Rows like for example \"3x2\"."
  8502. (interactive "P")
  8503. (unless size
  8504. (setq size (read-string
  8505. (concat "Table size Columns x Rows [e.g. "
  8506. org-table-default-size "]: ")
  8507. "" nil org-table-default-size)))
  8508. (let* ((pos (point))
  8509. (indent (make-string (current-column) ?\ ))
  8510. (split (org-split-string size " *x *"))
  8511. (rows (string-to-number (nth 1 split)))
  8512. (columns (string-to-number (car split)))
  8513. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  8514. "\n")))
  8515. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  8516. (point-at-bol) (point)))
  8517. (beginning-of-line 1)
  8518. (newline))
  8519. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  8520. (dotimes (i rows) (insert line))
  8521. (goto-char pos)
  8522. (if (> rows 1)
  8523. ;; Insert a hline after the first row.
  8524. (progn
  8525. (end-of-line 1)
  8526. (insert "\n|-")
  8527. (goto-char pos)))
  8528. (org-table-align)))
  8529. (defun org-table-convert-region (beg0 end0 &optional nspace)
  8530. "Convert region to a table.
  8531. The region goes from BEG0 to END0, but these borders will be moved
  8532. slightly, to make sure a beginning of line in the first line is included.
  8533. When NSPACE is non-nil, it indicates the minimum number of spaces that
  8534. separate columns (default: just one space)."
  8535. (interactive "rP")
  8536. (let* ((beg (min beg0 end0))
  8537. (end (max beg0 end0))
  8538. (tabsep t)
  8539. re)
  8540. (goto-char beg)
  8541. (beginning-of-line 1)
  8542. (setq beg (move-marker (make-marker) (point)))
  8543. (goto-char end)
  8544. (if (bolp) (backward-char 1) (end-of-line 1))
  8545. (setq end (move-marker (make-marker) (point)))
  8546. ;; Lets see if this is tab-separated material. If every nonempty line
  8547. ;; contains a tab, we will assume that it is tab-separated material
  8548. (if nspace
  8549. (setq tabsep nil)
  8550. (goto-char beg)
  8551. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  8552. (if nspace (setq tabsep nil))
  8553. (if tabsep
  8554. (setq re "^\\|\t")
  8555. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  8556. (max 1 (prefix-numeric-value nspace)))))
  8557. (goto-char beg)
  8558. (while (re-search-forward re end t)
  8559. (replace-match "|" t t))
  8560. (goto-char beg)
  8561. (insert " ")
  8562. (org-table-align)))
  8563. (defun org-table-import (file arg)
  8564. "Import FILE as a table.
  8565. The file is assumed to be tab-separated. Such files can be produced by most
  8566. spreadsheet and database applications. If no tabs (at least one per line)
  8567. are found, lines will be split on whitespace into fields."
  8568. (interactive "f\nP")
  8569. (or (bolp) (newline))
  8570. (let ((beg (point))
  8571. (pm (point-max)))
  8572. (insert-file-contents file)
  8573. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  8574. (defun org-table-export ()
  8575. "Export table as a tab-separated file.
  8576. Such a file can be imported into a spreadsheet program like Excel."
  8577. (interactive)
  8578. (let* ((beg (org-table-begin))
  8579. (end (org-table-end))
  8580. (table (buffer-substring beg end))
  8581. (file (read-file-name "Export table to: "))
  8582. buf)
  8583. (unless (or (not (file-exists-p file))
  8584. (y-or-n-p (format "Overwrite file %s? " file)))
  8585. (error "Abort"))
  8586. (with-current-buffer (find-file-noselect file)
  8587. (setq buf (current-buffer))
  8588. (erase-buffer)
  8589. (fundamental-mode)
  8590. (insert table)
  8591. (goto-char (point-min))
  8592. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  8593. (replace-match "" t t)
  8594. (end-of-line 1))
  8595. (goto-char (point-min))
  8596. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  8597. (replace-match "" t t)
  8598. (goto-char (min (1+ (point)) (point-max))))
  8599. (goto-char (point-min))
  8600. (while (re-search-forward "^-[-+]*$" nil t)
  8601. (replace-match "")
  8602. (if (looking-at "\n")
  8603. (delete-char 1)))
  8604. (goto-char (point-min))
  8605. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  8606. (replace-match "\t" t t))
  8607. (save-buffer))
  8608. (kill-buffer buf)))
  8609. (defvar org-table-aligned-begin-marker (make-marker)
  8610. "Marker at the beginning of the table last aligned.
  8611. Used to check if cursor still is in that table, to minimize realignment.")
  8612. (defvar org-table-aligned-end-marker (make-marker)
  8613. "Marker at the end of the table last aligned.
  8614. Used to check if cursor still is in that table, to minimize realignment.")
  8615. (defvar org-table-last-alignment nil
  8616. "List of flags for flushright alignment, from the last re-alignment.
  8617. This is being used to correctly align a single field after TAB or RET.")
  8618. (defvar org-table-last-column-widths nil
  8619. "List of max width of fields in each column.
  8620. This is being used to correctly align a single field after TAB or RET.")
  8621. (defvar org-last-recalc-line nil)
  8622. (defconst org-narrow-column-arrow "=>"
  8623. "Used as display property in narrowed table columns.")
  8624. (defun org-table-align ()
  8625. "Align the table at point by aligning all vertical bars."
  8626. (interactive)
  8627. (let* (
  8628. ;; Limits of table
  8629. (beg (org-table-begin))
  8630. (end (org-table-end))
  8631. ;; Current cursor position
  8632. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  8633. (colpos (org-table-current-column))
  8634. (winstart (window-start))
  8635. text lines (new "") lengths l typenums ty fields maxfields i
  8636. column
  8637. (indent "") cnt frac
  8638. rfmt hfmt
  8639. (spaces '(1 . 1))
  8640. (sp1 (car spaces))
  8641. (sp2 (cdr spaces))
  8642. (rfmt1 (concat
  8643. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  8644. (hfmt1 (concat
  8645. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  8646. emptystrings xx links narrow fmax fmin f1 len c e)
  8647. (untabify beg end)
  8648. (remove-text-properties beg end '(org-cwidth t display t))
  8649. ;; Check if we have links
  8650. (goto-char beg)
  8651. (setq links (re-search-forward org-bracket-link-regexp end t))
  8652. ;; Make sure the link properties are right FIXME: Can this be optimized????
  8653. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  8654. ;; Check if we are narrowing any columns
  8655. (goto-char beg)
  8656. (setq narrow (and org-format-transports-properties-p
  8657. (re-search-forward "<[0-9]+>" end t)))
  8658. ;; Get the rows
  8659. (setq lines (org-split-string
  8660. (buffer-substring beg end) "\n"))
  8661. ;; Store the indentation of the first line
  8662. (if (string-match "^ *" (car lines))
  8663. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  8664. ;; Mark the hlines by setting the corresponding element to nil
  8665. ;; At the same time, we remove trailing space.
  8666. (setq lines (mapcar (lambda (l)
  8667. (if (string-match "^ *|-" l)
  8668. nil
  8669. (if (string-match "[ \t]+$" l)
  8670. (substring l 0 (match-beginning 0))
  8671. l)))
  8672. lines))
  8673. ;; Get the data fields by splitting the lines.
  8674. (setq fields (mapcar
  8675. (lambda (l)
  8676. (org-split-string l " *| *"))
  8677. (delq nil (copy-sequence lines))))
  8678. ;; How many fields in the longest line?
  8679. (condition-case nil
  8680. (setq maxfields (apply 'max (mapcar 'length fields)))
  8681. (error
  8682. (kill-region beg end)
  8683. (org-table-create org-table-default-size)
  8684. (error "Empty table - created default table")))
  8685. ;; A list of empty string to fill any short rows on output
  8686. (setq emptystrings (make-list maxfields ""))
  8687. ;; Check for special formatting.
  8688. (setq i -1)
  8689. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  8690. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  8691. ;; Check if there is an explicit width specified
  8692. (when (and org-table-limit-column-width narrow)
  8693. (setq c column fmax nil)
  8694. (while c
  8695. (setq e (pop c))
  8696. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  8697. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  8698. ;; Find fields that are wider than fmax, and shorten them
  8699. (when fmax
  8700. (loop for xx in column do
  8701. (when (and (stringp xx)
  8702. (> (org-string-width xx) fmax))
  8703. (org-add-props xx nil
  8704. 'help-echo
  8705. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  8706. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  8707. (unless (> f1 1)
  8708. (error "Cannot narrow field starting with wide link \"%s\""
  8709. (match-string 0 xx)))
  8710. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  8711. (add-text-properties (- f1 2) f1
  8712. (list 'display org-narrow-column-arrow)
  8713. xx)))))
  8714. ;; Get the maximum width for each column
  8715. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  8716. ;; Get the fraction of numbers, to decide about alignment of the column
  8717. (setq cnt 0 frac 0.0)
  8718. (loop for x in column do
  8719. (if (equal x "")
  8720. nil
  8721. (setq frac ( / (+ (* frac cnt)
  8722. (if (string-match org-table-number-regexp x) 1 0))
  8723. (setq cnt (1+ cnt))))))
  8724. (push (>= frac org-table-number-fraction) typenums))
  8725. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  8726. ;; Store the alignment of this table, for later editing of single fields
  8727. (setq org-table-last-alignment typenums
  8728. org-table-last-column-widths lengths)
  8729. ;; With invisible characters, `format' does not get the field width right
  8730. ;; So we need to make these fields wide by hand.
  8731. (when links
  8732. (loop for i from 0 upto (1- maxfields) do
  8733. (setq len (nth i lengths))
  8734. (loop for j from 0 upto (1- (length fields)) do
  8735. (setq c (nthcdr i (car (nthcdr j fields))))
  8736. (if (and (stringp (car c))
  8737. (string-match org-bracket-link-regexp (car c))
  8738. (< (org-string-width (car c)) len))
  8739. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  8740. ;; Compute the formats needed for output of the table
  8741. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  8742. (while (setq l (pop lengths))
  8743. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  8744. (setq rfmt (concat rfmt (format rfmt1 ty l))
  8745. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  8746. (setq rfmt (concat rfmt "\n")
  8747. hfmt (concat (substring hfmt 0 -1) "|\n"))
  8748. (setq new (mapconcat
  8749. (lambda (l)
  8750. (if l (apply 'format rfmt
  8751. (append (pop fields) emptystrings))
  8752. hfmt))
  8753. lines ""))
  8754. ;; Replace the old one
  8755. (delete-region beg end)
  8756. (move-marker end nil)
  8757. (move-marker org-table-aligned-begin-marker (point))
  8758. (insert new)
  8759. (move-marker org-table-aligned-end-marker (point))
  8760. (when (and orgtbl-mode (not (eq major-mode 'org-mode)))
  8761. (goto-char org-table-aligned-begin-marker)
  8762. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  8763. ;; Try to move to the old location (approximately)
  8764. (goto-line linepos)
  8765. (set-window-start (selected-window) winstart 'noforce)
  8766. (org-table-goto-column colpos)
  8767. (setq org-table-may-need-update nil)
  8768. ))
  8769. (defun org-string-width (s)
  8770. "Compute width of string, ignoring invisible characters.
  8771. This ignores character with invisibility property `org-link', and also
  8772. characters with property `org-cwidth', because these will become invisible
  8773. upon the next fontification round."
  8774. (let (b)
  8775. (when (or (eq t buffer-invisibility-spec)
  8776. (assq 'org-link buffer-invisibility-spec))
  8777. (while (setq b (text-property-any 0 (length s)
  8778. 'invisible 'org-link s))
  8779. (setq s (concat (substring s 0 b)
  8780. (substring s (or (next-single-property-change
  8781. b 'invisible s) (length s)))))))
  8782. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  8783. (setq s (concat (substring s 0 b)
  8784. (substring s (or (next-single-property-change
  8785. b 'org-cwidth s) (length s))))))
  8786. (string-width s)))
  8787. (defun org-table-begin (&optional table-type)
  8788. "Find the beginning of the table and return its position.
  8789. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  8790. (save-excursion
  8791. (if (not (re-search-backward
  8792. (if table-type org-table-any-border-regexp
  8793. org-table-border-regexp)
  8794. nil t))
  8795. (progn (goto-char (point-min)) (point))
  8796. (goto-char (match-beginning 0))
  8797. (beginning-of-line 2)
  8798. (point))))
  8799. (defun org-table-end (&optional table-type)
  8800. "Find the end of the table and return its position.
  8801. With argument TABLE-TYPE, go to the end of a table.el-type table."
  8802. (save-excursion
  8803. (if (not (re-search-forward
  8804. (if table-type org-table-any-border-regexp
  8805. org-table-border-regexp)
  8806. nil t))
  8807. (goto-char (point-max))
  8808. (goto-char (match-beginning 0)))
  8809. (point-marker)))
  8810. (defun org-table-justify-field-maybe (&optional new)
  8811. "Justify the current field, text to left, number to right.
  8812. Optional argument NEW may specify text to replace the current field content."
  8813. (cond
  8814. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  8815. ((org-at-table-hline-p))
  8816. ((and (not new)
  8817. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  8818. (current-buffer)))
  8819. (< (point) org-table-aligned-begin-marker)
  8820. (>= (point) org-table-aligned-end-marker)))
  8821. ;; This is not the same table, force a full re-align
  8822. (setq org-table-may-need-update t))
  8823. (t ;; realign the current field, based on previous full realign
  8824. (let* ((pos (point)) s
  8825. (col (org-table-current-column))
  8826. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  8827. l f n o e)
  8828. (when (> col 0)
  8829. (skip-chars-backward "^|\n")
  8830. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  8831. (progn
  8832. (setq s (match-string 1)
  8833. o (match-string 0)
  8834. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  8835. e (not (= (match-beginning 2) (match-end 2))))
  8836. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  8837. l (if e "|" (setq org-table-may-need-update t) ""))
  8838. n (format f s))
  8839. (if new
  8840. (if (<= (length new) l) ;; FIXME: length -> str-width?
  8841. (setq n (format f new))
  8842. (setq n (concat new "|") org-table-may-need-update t)))
  8843. (or (equal n o)
  8844. (let (org-table-may-need-update)
  8845. (replace-match n))))
  8846. (setq org-table-may-need-update t))
  8847. (goto-char pos))))))
  8848. (defun org-table-next-field ()
  8849. "Go to the next field in the current table, creating new lines as needed.
  8850. Before doing so, re-align the table if necessary."
  8851. (interactive)
  8852. (org-table-maybe-eval-formula)
  8853. (org-table-maybe-recalculate-line)
  8854. (if (and org-table-automatic-realign
  8855. org-table-may-need-update)
  8856. (org-table-align))
  8857. (let ((end (org-table-end)))
  8858. (if (org-at-table-hline-p)
  8859. (end-of-line 1))
  8860. (condition-case nil
  8861. (progn
  8862. (re-search-forward "|" end)
  8863. (if (looking-at "[ \t]*$")
  8864. (re-search-forward "|" end))
  8865. (if (and (looking-at "-")
  8866. org-table-tab-jumps-over-hlines
  8867. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  8868. (goto-char (match-beginning 1)))
  8869. (if (looking-at "-")
  8870. (progn
  8871. (beginning-of-line 0)
  8872. (org-table-insert-row 'below))
  8873. (if (looking-at " ") (forward-char 1))))
  8874. (error
  8875. (org-table-insert-row 'below)))))
  8876. (defun org-table-previous-field ()
  8877. "Go to the previous field in the table.
  8878. Before doing so, re-align the table if necessary."
  8879. (interactive)
  8880. (org-table-justify-field-maybe)
  8881. (org-table-maybe-recalculate-line)
  8882. (if (and org-table-automatic-realign
  8883. org-table-may-need-update)
  8884. (org-table-align))
  8885. (if (org-at-table-hline-p)
  8886. (end-of-line 1))
  8887. (re-search-backward "|" (org-table-begin))
  8888. (re-search-backward "|" (org-table-begin))
  8889. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  8890. (re-search-backward "|" (org-table-begin)))
  8891. (if (looking-at "| ?")
  8892. (goto-char (match-end 0))))
  8893. (defun org-table-next-row ()
  8894. "Go to the next row (same column) in the current table.
  8895. Before doing so, re-align the table if necessary."
  8896. (interactive)
  8897. (org-table-maybe-eval-formula)
  8898. (org-table-maybe-recalculate-line)
  8899. (if (or (looking-at "[ \t]*$")
  8900. (save-excursion (skip-chars-backward " \t") (bolp)))
  8901. (newline)
  8902. (if (and org-table-automatic-realign
  8903. org-table-may-need-update)
  8904. (org-table-align))
  8905. (let ((col (org-table-current-column)))
  8906. (beginning-of-line 2)
  8907. (if (or (not (org-at-table-p))
  8908. (org-at-table-hline-p))
  8909. (progn
  8910. (beginning-of-line 0)
  8911. (org-table-insert-row 'below)))
  8912. (org-table-goto-column col)
  8913. (skip-chars-backward "^|\n\r")
  8914. (if (looking-at " ") (forward-char 1)))))
  8915. (defun org-table-copy-down (n)
  8916. "Copy a field down in the current column.
  8917. If the field at the cursor is empty, copy into it the content of the nearest
  8918. non-empty field above. With argument N, use the Nth non-empty field.
  8919. If the current field is not empty, it is copied down to the next row, and
  8920. the cursor is moved with it. Therefore, repeating this command causes the
  8921. column to be filled row-by-row.
  8922. If the variable `org-table-copy-increment' is non-nil and the field is an
  8923. integer, it will be incremented while copying."
  8924. (interactive "p")
  8925. (let* ((colpos (org-table-current-column))
  8926. (field (org-table-get-field))
  8927. (non-empty (string-match "[^ \t]" field))
  8928. (beg (org-table-begin))
  8929. txt)
  8930. (org-table-check-inside-data-field)
  8931. (if non-empty
  8932. (progn
  8933. (setq txt (org-trim field))
  8934. (org-table-next-row)
  8935. (org-table-blank-field))
  8936. (save-excursion
  8937. (setq txt
  8938. (catch 'exit
  8939. (while (progn (beginning-of-line 1)
  8940. (re-search-backward org-table-dataline-regexp
  8941. beg t))
  8942. (org-table-goto-column colpos t)
  8943. (if (and (looking-at
  8944. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  8945. (= (setq n (1- n)) 0))
  8946. (throw 'exit (match-string 1))))))))
  8947. (if txt
  8948. (progn
  8949. (if (and org-table-copy-increment
  8950. (string-match "^[0-9]+$" txt))
  8951. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  8952. (insert txt)
  8953. (org-table-maybe-recalculate-line)
  8954. (org-table-align))
  8955. (error "No non-empty field found"))))
  8956. (defun org-table-check-inside-data-field ()
  8957. "Is point inside a table data field?
  8958. I.e. not on a hline or before the first or after the last column?
  8959. This actually throws an error, so it aborts the current command."
  8960. (if (or (not (org-at-table-p))
  8961. (= (org-table-current-column) 0)
  8962. (org-at-table-hline-p)
  8963. (looking-at "[ \t]*$"))
  8964. (error "Not in table data field")))
  8965. (defvar org-table-clip nil
  8966. "Clipboard for table regions.")
  8967. (defun org-table-blank-field ()
  8968. "Blank the current table field or active region."
  8969. (interactive)
  8970. (org-table-check-inside-data-field)
  8971. (if (and (interactive-p) (org-region-active-p))
  8972. (let (org-table-clip)
  8973. (org-table-cut-region (region-beginning) (region-end)))
  8974. (skip-chars-backward "^|")
  8975. (backward-char 1)
  8976. (if (looking-at "|[^|\n]+")
  8977. (let* ((pos (match-beginning 0))
  8978. (match (match-string 0))
  8979. (len (org-string-width match)))
  8980. (replace-match (concat "|" (make-string (1- len) ?\ )))
  8981. (goto-char (+ 2 pos))
  8982. (substring match 1)))))
  8983. (defun org-table-get-field (&optional n replace)
  8984. "Return the value of the field in column N of current row.
  8985. N defaults to current field.
  8986. If REPLACE is a string, replace field with this value. The return value
  8987. is always the old value."
  8988. (and n (org-table-goto-column n))
  8989. (skip-chars-backward "^|\n")
  8990. (backward-char 1)
  8991. (if (looking-at "|[^|\r\n]*")
  8992. (let* ((pos (match-beginning 0))
  8993. (val (buffer-substring (1+ pos) (match-end 0))))
  8994. (if replace
  8995. (replace-match (concat "|" replace)))
  8996. (goto-char (min (point-at-eol) (+ 2 pos)))
  8997. val)
  8998. (forward-char 1) ""))
  8999. (defun org-table-current-column ()
  9000. "Find out which column we are in.
  9001. When called interactively, column is also displayed in echo area."
  9002. (interactive)
  9003. (if (interactive-p) (org-table-check-inside-data-field))
  9004. (save-excursion
  9005. (let ((cnt 0) (pos (point)))
  9006. (beginning-of-line 1)
  9007. (while (search-forward "|" pos t)
  9008. (setq cnt (1+ cnt)))
  9009. (if (interactive-p) (message "This is table column %d" cnt))
  9010. cnt)))
  9011. (defun org-table-goto-column (n &optional on-delim force)
  9012. "Move the cursor to the Nth column in the current table line.
  9013. With optional argument ON-DELIM, stop with point before the left delimiter
  9014. of the field.
  9015. If there are less than N fields, just go to after the last delimiter.
  9016. However, when FORCE is non-nil, create new columns if necessary."
  9017. (interactive "p")
  9018. (let ((pos (point-at-eol)))
  9019. (beginning-of-line 1)
  9020. (when (> n 0)
  9021. (while (and (> (setq n (1- n)) -1)
  9022. (or (search-forward "|" pos t)
  9023. (and force
  9024. (progn (end-of-line 1)
  9025. (skip-chars-backward "^|")
  9026. (insert " | "))))))
  9027. ; (backward-char 2) t)))))
  9028. (when (and force (not (looking-at ".*|")))
  9029. (save-excursion (end-of-line 1) (insert " | ")))
  9030. (if on-delim
  9031. (backward-char 1)
  9032. (if (looking-at " ") (forward-char 1))))))
  9033. (defun org-at-table-p (&optional table-type)
  9034. "Return t if the cursor is inside an org-type table.
  9035. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  9036. (if org-enable-table-editor
  9037. (save-excursion
  9038. (beginning-of-line 1)
  9039. (looking-at (if table-type org-table-any-line-regexp
  9040. org-table-line-regexp)))
  9041. nil))
  9042. (defun org-at-table.el-p ()
  9043. "Return t if and only if we are at a table.el table."
  9044. (and (org-at-table-p 'any)
  9045. (save-excursion
  9046. (goto-char (org-table-begin 'any))
  9047. (looking-at org-table1-hline-regexp))))
  9048. (defun org-table-recognize-table.el ()
  9049. "If there is a table.el table nearby, recognize it and move into it."
  9050. (if org-table-tab-recognizes-table.el
  9051. (if (org-at-table.el-p)
  9052. (progn
  9053. (beginning-of-line 1)
  9054. (if (looking-at org-table-dataline-regexp)
  9055. nil
  9056. (if (looking-at org-table1-hline-regexp)
  9057. (progn
  9058. (beginning-of-line 2)
  9059. (if (looking-at org-table-any-border-regexp)
  9060. (beginning-of-line -1)))))
  9061. (if (re-search-forward "|" (org-table-end t) t)
  9062. (progn
  9063. (require 'table)
  9064. (if (table--at-cell-p (point))
  9065. t
  9066. (message "recognizing table.el table...")
  9067. (table-recognize-table)
  9068. (message "recognizing table.el table...done")))
  9069. (error "This should not happen..."))
  9070. t)
  9071. nil)
  9072. nil))
  9073. (defun org-at-table-hline-p ()
  9074. "Return t if the cursor is inside a hline in a table."
  9075. (if org-enable-table-editor
  9076. (save-excursion
  9077. (beginning-of-line 1)
  9078. (looking-at org-table-hline-regexp))
  9079. nil))
  9080. (defun org-table-insert-column ()
  9081. "Insert a new column into the table."
  9082. (interactive)
  9083. (if (not (org-at-table-p))
  9084. (error "Not at a table"))
  9085. (org-table-find-dataline)
  9086. (let* ((col (max 1 (org-table-current-column)))
  9087. (beg (org-table-begin))
  9088. (end (org-table-end))
  9089. ;; Current cursor position
  9090. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9091. (colpos col))
  9092. (goto-char beg)
  9093. (while (< (point) end)
  9094. (if (org-at-table-hline-p)
  9095. nil
  9096. (org-table-goto-column col t)
  9097. (insert "| "))
  9098. (beginning-of-line 2))
  9099. (move-marker end nil)
  9100. (goto-line linepos)
  9101. (org-table-goto-column colpos)
  9102. (org-table-align)
  9103. (org-table-modify-formulas 'insert col)))
  9104. (defun org-table-find-dataline ()
  9105. "Find a dataline in the current table, which is needed for column commands."
  9106. (if (and (org-at-table-p)
  9107. (not (org-at-table-hline-p)))
  9108. t
  9109. (let ((col (current-column))
  9110. (end (org-table-end)))
  9111. (move-to-column col)
  9112. (while (and (< (point) end)
  9113. (or (not (= (current-column) col))
  9114. (org-at-table-hline-p)))
  9115. (beginning-of-line 2)
  9116. (move-to-column col))
  9117. (if (and (org-at-table-p)
  9118. (not (org-at-table-hline-p)))
  9119. t
  9120. (error
  9121. "Please position cursor in a data line for column operations")))))
  9122. (defun org-table-delete-column ()
  9123. "Delete a column from the table."
  9124. (interactive)
  9125. (if (not (org-at-table-p))
  9126. (error "Not at a table"))
  9127. (org-table-find-dataline)
  9128. (org-table-check-inside-data-field)
  9129. (let* ((col (org-table-current-column))
  9130. (beg (org-table-begin))
  9131. (end (org-table-end))
  9132. ;; Current cursor position
  9133. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9134. (colpos col))
  9135. (goto-char beg)
  9136. (while (< (point) end)
  9137. (if (org-at-table-hline-p)
  9138. nil
  9139. (org-table-goto-column col t)
  9140. (and (looking-at "|[^|\n]+|")
  9141. (replace-match "|")))
  9142. (beginning-of-line 2))
  9143. (move-marker end nil)
  9144. (goto-line linepos)
  9145. (org-table-goto-column colpos)
  9146. (org-table-align)
  9147. (org-table-modify-formulas 'remove col)))
  9148. (defun org-table-move-column-right ()
  9149. "Move column to the right."
  9150. (interactive)
  9151. (org-table-move-column nil))
  9152. (defun org-table-move-column-left ()
  9153. "Move column to the left."
  9154. (interactive)
  9155. (org-table-move-column 'left))
  9156. (defun org-table-move-column (&optional left)
  9157. "Move the current column to the right. With arg LEFT, move to the left."
  9158. (interactive "P")
  9159. (if (not (org-at-table-p))
  9160. (error "Not at a table"))
  9161. (org-table-find-dataline)
  9162. (org-table-check-inside-data-field)
  9163. (let* ((col (org-table-current-column))
  9164. (col1 (if left (1- col) col))
  9165. (beg (org-table-begin))
  9166. (end (org-table-end))
  9167. ;; Current cursor position
  9168. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9169. (colpos (if left (1- col) (1+ col))))
  9170. (if (and left (= col 1))
  9171. (error "Cannot move column further left"))
  9172. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  9173. (error "Cannot move column further right"))
  9174. (goto-char beg)
  9175. (while (< (point) end)
  9176. (if (org-at-table-hline-p)
  9177. nil
  9178. (org-table-goto-column col1 t)
  9179. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  9180. (replace-match "|\\2|\\1|")))
  9181. (beginning-of-line 2))
  9182. (move-marker end nil)
  9183. (goto-line linepos)
  9184. (org-table-goto-column colpos)
  9185. (org-table-align)
  9186. (org-table-modify-formulas 'swap col (if left (1- col) (1+ col)))))
  9187. (defun org-table-move-row-down ()
  9188. "Move table row down."
  9189. (interactive)
  9190. (org-table-move-row nil))
  9191. (defun org-table-move-row-up ()
  9192. "Move table row up."
  9193. (interactive)
  9194. (org-table-move-row 'up))
  9195. (defun org-table-move-row (&optional up)
  9196. "Move the current table line down. With arg UP, move it up."
  9197. (interactive "P")
  9198. (let ((col (current-column))
  9199. (pos (point))
  9200. (tonew (if up 0 2))
  9201. txt)
  9202. (beginning-of-line tonew)
  9203. (if (not (org-at-table-p))
  9204. (progn
  9205. (goto-char pos)
  9206. (error "Cannot move row further")))
  9207. (goto-char pos)
  9208. (beginning-of-line 1)
  9209. (setq pos (point))
  9210. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  9211. (delete-region (point) (1+ (point-at-eol)))
  9212. (beginning-of-line tonew)
  9213. (insert txt)
  9214. (beginning-of-line 0)
  9215. (move-to-column col)))
  9216. (defun org-table-insert-row (&optional arg)
  9217. "Insert a new row above the current line into the table.
  9218. With prefix ARG, insert below the current line."
  9219. (interactive "P")
  9220. (if (not (org-at-table-p))
  9221. (error "Not at a table"))
  9222. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  9223. (new (org-table-clean-line line)))
  9224. ;; Fix the first field if necessary
  9225. (if (string-match "^[ \t]*| *[#$] *|" line)
  9226. (setq new (replace-match (match-string 0 line) t t new)))
  9227. (beginning-of-line (if arg 2 1))
  9228. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  9229. (beginning-of-line 0)
  9230. (re-search-forward "| ?" (point-at-eol) t)
  9231. (and org-table-may-need-update (org-table-align))))
  9232. (defun org-table-insert-hline (&optional arg)
  9233. "Insert a horizontal-line below the current line into the table.
  9234. With prefix ARG, insert above the current line."
  9235. (interactive "P")
  9236. (if (not (org-at-table-p))
  9237. (error "Not at a table"))
  9238. (let ((line (org-table-clean-line
  9239. (buffer-substring (point-at-bol) (point-at-eol))))
  9240. (col (current-column)))
  9241. (while (string-match "|\\( +\\)|" line)
  9242. (setq line (replace-match
  9243. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  9244. ?-) "|") t t line)))
  9245. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  9246. (beginning-of-line (if arg 1 2))
  9247. (insert line "\n")
  9248. (beginning-of-line (if arg 1 -1))
  9249. (move-to-column col)))
  9250. (defun org-table-clean-line (s)
  9251. "Convert a table line S into a string with only \"|\" and space.
  9252. In particular, this does handle wide and invisible characters."
  9253. (if (string-match "^[ \t]*|-" s)
  9254. ;; It's a hline, just map the characters
  9255. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  9256. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  9257. (setq s (replace-match
  9258. (concat "|" (make-string (org-string-width (match-string 1 s))
  9259. ?\ ) "|")
  9260. t t s)))
  9261. s))
  9262. (defun org-table-kill-row ()
  9263. "Delete the current row or horizontal line from the table."
  9264. (interactive)
  9265. (if (not (org-at-table-p))
  9266. (error "Not at a table"))
  9267. (let ((col (current-column)))
  9268. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  9269. (if (not (org-at-table-p)) (beginning-of-line 0))
  9270. (move-to-column col)))
  9271. (defun org-table-sort-lines (beg end numericp)
  9272. "Sort table lines in region.
  9273. Point and mark define the first and last line to include. Both point and
  9274. mark should be in the column that is used for sorting. For example, to
  9275. sort according to column 3, put the mark in the first line to sort, in
  9276. table column 3. Put point into the last line to be included in the sorting,
  9277. also in table column 3. The command will prompt for the sorting method
  9278. \(n for numerical, a for alphanumeric)."
  9279. (interactive "r\nsSorting method: [n]=numeric [a]=alpha: ")
  9280. (setq numericp (string-match "[nN]" numericp))
  9281. (org-table-align) ;; Just to be safe
  9282. (let* (bcol ecol cmp column lns)
  9283. (goto-char beg)
  9284. (org-table-check-inside-data-field)
  9285. (setq column (org-table-current-column)
  9286. beg (move-marker (make-marker) (point-at-bol)))
  9287. (goto-char end)
  9288. (org-table-check-inside-data-field)
  9289. (setq end (move-marker (make-marker) (1+ (point-at-eol))))
  9290. (untabify beg end)
  9291. (goto-char beg)
  9292. (org-table-goto-column column)
  9293. (skip-chars-backward "^|")
  9294. (setq bcol (current-column))
  9295. (org-table-goto-column (1+ column))
  9296. (skip-chars-backward "^|")
  9297. (setq ecol (1- (current-column)))
  9298. (setq cmp (if numericp
  9299. (lambda (a b) (< (car a) (car b)))
  9300. (lambda (a b) (string< (car a) (car b)))))
  9301. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  9302. (org-split-string (buffer-substring beg end) "\n")))
  9303. (if numericp
  9304. (setq lns (mapcar (lambda(x)
  9305. (cons (string-to-number (car x)) (cdr x)))
  9306. lns)))
  9307. (delete-region beg end)
  9308. (move-marker beg nil)
  9309. (move-marker end nil)
  9310. (insert (mapconcat 'cdr (setq lns (sort lns cmp)) "\n") "\n")
  9311. (message "%d lines sorted %s based on column %d"
  9312. (length lns)
  9313. (if numericp "numerically" "alphabetically") column)))
  9314. (defun org-table-cut-region (beg end)
  9315. "Copy region in table to the clipboard and blank all relevant fields."
  9316. (interactive "r")
  9317. (org-table-copy-region beg end 'cut))
  9318. (defun org-table-copy-region (beg end &optional cut)
  9319. "Copy rectangular region in table to clipboard.
  9320. A special clipboard is used which can only be accessed
  9321. with `org-table-paste-rectangle'."
  9322. (interactive "rP")
  9323. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  9324. region cols
  9325. (rpl (if cut " " nil)))
  9326. (goto-char beg)
  9327. (org-table-check-inside-data-field)
  9328. (setq l01 (count-lines (point-min) (point))
  9329. c01 (org-table-current-column))
  9330. (goto-char end)
  9331. (org-table-check-inside-data-field)
  9332. (setq l02 (count-lines (point-min) (point))
  9333. c02 (org-table-current-column))
  9334. (setq l1 (min l01 l02) l2 (max l01 l02)
  9335. c1 (min c01 c02) c2 (max c01 c02))
  9336. (catch 'exit
  9337. (while t
  9338. (catch 'nextline
  9339. (if (> l1 l2) (throw 'exit t))
  9340. (goto-line l1)
  9341. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  9342. (setq cols nil ic1 c1 ic2 c2)
  9343. (while (< ic1 (1+ ic2))
  9344. (push (org-table-get-field ic1 rpl) cols)
  9345. (setq ic1 (1+ ic1)))
  9346. (push (nreverse cols) region)
  9347. (setq l1 (1+ l1)))))
  9348. (setq org-table-clip (nreverse region))
  9349. (if cut (org-table-align))
  9350. org-table-clip))
  9351. (defun org-table-paste-rectangle ()
  9352. "Paste a rectangular region into a table.
  9353. The upper right corner ends up in the current field. All involved fields
  9354. will be overwritten. If the rectangle does not fit into the present table,
  9355. the table is enlarged as needed. The process ignores horizontal separator
  9356. lines."
  9357. (interactive)
  9358. (unless (and org-table-clip (listp org-table-clip))
  9359. (error "First cut/copy a region to paste!"))
  9360. (org-table-check-inside-data-field)
  9361. (let* ((clip org-table-clip)
  9362. (line (count-lines (point-min) (point)))
  9363. (col (org-table-current-column))
  9364. (org-enable-table-editor t)
  9365. (org-table-automatic-realign nil)
  9366. c cols field)
  9367. (while (setq cols (pop clip))
  9368. (while (org-at-table-hline-p) (beginning-of-line 2))
  9369. (if (not (org-at-table-p))
  9370. (progn (end-of-line 0) (org-table-next-field)))
  9371. (setq c col)
  9372. (while (setq field (pop cols))
  9373. (org-table-goto-column c nil 'force)
  9374. (org-table-get-field nil field)
  9375. (setq c (1+ c)))
  9376. (beginning-of-line 2))
  9377. (goto-line line)
  9378. (org-table-goto-column col)
  9379. (org-table-align)))
  9380. (defun org-table-convert ()
  9381. "Convert from `org-mode' table to table.el and back.
  9382. Obviously, this only works within limits. When an Org-mode table is
  9383. converted to table.el, all horizontal separator lines get lost, because
  9384. table.el uses these as cell boundaries and has no notion of horizontal lines.
  9385. A table.el table can be converted to an Org-mode table only if it does not
  9386. do row or column spanning. Multiline cells will become multiple cells.
  9387. Beware, Org-mode does not test if the table can be successfully converted - it
  9388. blindly applies a recipe that works for simple tables."
  9389. (interactive)
  9390. (require 'table)
  9391. (if (org-at-table.el-p)
  9392. ;; convert to Org-mode table
  9393. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  9394. (end (move-marker (make-marker) (org-table-end t))))
  9395. (table-unrecognize-region beg end)
  9396. (goto-char beg)
  9397. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  9398. (replace-match ""))
  9399. (goto-char beg))
  9400. (if (org-at-table-p)
  9401. ;; convert to table.el table
  9402. (let ((beg (move-marker (make-marker) (org-table-begin)))
  9403. (end (move-marker (make-marker) (org-table-end))))
  9404. ;; first, get rid of all horizontal lines
  9405. (goto-char beg)
  9406. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  9407. (replace-match ""))
  9408. ;; insert a hline before first
  9409. (goto-char beg)
  9410. (org-table-insert-hline 'above)
  9411. (beginning-of-line -1)
  9412. ;; insert a hline after each line
  9413. (while (progn (beginning-of-line 3) (< (point) end))
  9414. (org-table-insert-hline))
  9415. (goto-char beg)
  9416. (setq end (move-marker end (org-table-end)))
  9417. ;; replace "+" at beginning and ending of hlines
  9418. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  9419. (replace-match "\\1+-"))
  9420. (goto-char beg)
  9421. (while (re-search-forward "-|[ \t]*$" end t)
  9422. (replace-match "-+"))
  9423. (goto-char beg)))))
  9424. (defun org-table-wrap-region (arg)
  9425. "Wrap several fields in a column like a paragraph.
  9426. This is useful if you'd like to spread the contents of a field over several
  9427. lines, in order to keep the table compact.
  9428. If there is an active region, and both point and mark are in the same column,
  9429. the text in the column is wrapped to minimum width for the given number of
  9430. lines. Generally, this makes the table more compact. A prefix ARG may be
  9431. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  9432. formats the selected text to two lines. If the region was longer than two
  9433. lines, the remaining lines remain empty. A negative prefix argument reduces
  9434. the current number of lines by that amount. The wrapped text is pasted back
  9435. into the table. If you formatted it to more lines than it was before, fields
  9436. further down in the table get overwritten - so you might need to make space in
  9437. the table first.
  9438. If there is no region, the current field is split at the cursor position and
  9439. the text fragment to the right of the cursor is prepended to the field one
  9440. line down.
  9441. If there is no region, but you specify a prefix ARG, the current field gets
  9442. blank, and the content is appended to the field above."
  9443. (interactive "P")
  9444. (org-table-check-inside-data-field)
  9445. (if (org-region-active-p)
  9446. ;; There is a region: fill as a paragraph
  9447. (let ((beg (region-beginning))
  9448. nlines)
  9449. (org-table-cut-region (region-beginning) (region-end))
  9450. (if (> (length (car org-table-clip)) 1)
  9451. (error "Region must be limited to single column"))
  9452. (setq nlines (if arg
  9453. (if (< arg 1)
  9454. (+ (length org-table-clip) arg)
  9455. arg)
  9456. (length org-table-clip)))
  9457. (setq org-table-clip
  9458. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  9459. nil nlines)))
  9460. (goto-char beg)
  9461. (org-table-paste-rectangle))
  9462. ;; No region, split the current field at point
  9463. (if arg
  9464. ;; combine with field above
  9465. (let ((s (org-table-blank-field))
  9466. (col (org-table-current-column)))
  9467. (beginning-of-line 0)
  9468. (while (org-at-table-hline-p) (beginning-of-line 0))
  9469. (org-table-goto-column col)
  9470. (skip-chars-forward "^|")
  9471. (skip-chars-backward " ")
  9472. (insert " " (org-trim s))
  9473. (org-table-align))
  9474. ;; split field
  9475. (when (looking-at "\\([^|]+\\)+|")
  9476. (let ((s (match-string 1)))
  9477. (replace-match " |")
  9478. (goto-char (match-beginning 0))
  9479. (org-table-next-row)
  9480. (insert (org-trim s) " ")
  9481. (org-table-align))))))
  9482. (defvar org-field-marker nil)
  9483. (defun org-table-edit-field (arg)
  9484. "Edit table field in a different window.
  9485. This is mainly useful for fields that contain hidden parts.
  9486. When called with a \\[universal-argument] prefix, just make the full field visible so that
  9487. it can be edited in place."
  9488. (interactive "P")
  9489. (if arg
  9490. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  9491. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  9492. (remove-text-properties b e '(org-cwidth t invisible t
  9493. display t intangible t))
  9494. (if (and (boundp 'font-lock-mode) font-lock-mode)
  9495. (font-lock-fontify-block)))
  9496. (let ((pos (move-marker (make-marker) (point)))
  9497. (field (org-table-get-field))
  9498. (cw (current-window-configuration))
  9499. p)
  9500. (switch-to-buffer-other-window "*Org tmp*")
  9501. (erase-buffer)
  9502. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  9503. (org-mode)
  9504. (goto-char (setq p (point-max)))
  9505. (insert (org-trim field))
  9506. (remove-text-properties p (point-max)
  9507. '(invisible t org-cwidth t display t
  9508. intangible t))
  9509. (goto-char p)
  9510. (set (make-local-variable 'org-finish-function)
  9511. 'org-table-finish-edit-field)
  9512. (set (make-local-variable 'org-window-configuration) cw)
  9513. (set (make-local-variable 'org-field-marker) pos)
  9514. (message "Edit and finish with C-c C-c"))))
  9515. (defun org-table-finish-edit-field ()
  9516. "Finish editing a table data field.
  9517. Remove all newline characters, insert the result into the table, realign
  9518. the table and kill the editing buffer."
  9519. (let ((pos org-field-marker)
  9520. (cw org-window-configuration)
  9521. (cb (current-buffer))
  9522. text)
  9523. (goto-char (point-min))
  9524. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  9525. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  9526. (replace-match " "))
  9527. (setq text (org-trim (buffer-string)))
  9528. (set-window-configuration cw)
  9529. (kill-buffer cb)
  9530. (select-window (get-buffer-window (marker-buffer pos)))
  9531. (goto-char pos)
  9532. (move-marker pos nil)
  9533. (org-table-check-inside-data-field)
  9534. (org-table-get-field nil text)
  9535. (org-table-align)
  9536. (message "New field value inserted")))
  9537. (defun org-trim (s)
  9538. "Remove whitespace at beginning and end of string."
  9539. (if (string-match "^[ \t]+" s) (setq s (replace-match "" t t s)))
  9540. (if (string-match "[ \t]+$" s) (setq s (replace-match "" t t s)))
  9541. s)
  9542. (defun org-wrap (string &optional width lines)
  9543. "Wrap string to either a number of lines, or a width in characters.
  9544. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  9545. that costs. If there is a word longer than WIDTH, the text is actually
  9546. wrapped to the length of that word.
  9547. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  9548. many lines, whatever width that takes.
  9549. The return value is a list of lines, without newlines at the end."
  9550. (let* ((words (org-split-string string "[ \t\n]+"))
  9551. (maxword (apply 'max (mapcar 'org-string-width words)))
  9552. w ll)
  9553. (cond (width
  9554. (org-do-wrap words (max maxword width)))
  9555. (lines
  9556. (setq w maxword)
  9557. (setq ll (org-do-wrap words maxword))
  9558. (if (<= (length ll) lines)
  9559. ll
  9560. (setq ll words)
  9561. (while (> (length ll) lines)
  9562. (setq w (1+ w))
  9563. (setq ll (org-do-wrap words w)))
  9564. ll))
  9565. (t (error "Cannot wrap this")))))
  9566. (defun org-do-wrap (words width)
  9567. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  9568. (let (lines line)
  9569. (while words
  9570. (setq line (pop words))
  9571. (while (and words (< (+ (length line) (length (car words))) width))
  9572. (setq line (concat line " " (pop words))))
  9573. (setq lines (push line lines)))
  9574. (nreverse lines)))
  9575. (defun org-split-string (string &optional separators)
  9576. "Splits STRING into substrings at SEPARATORS.
  9577. No empty strings are returned if there are matches at the beginning
  9578. and end of string."
  9579. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  9580. (start 0)
  9581. notfirst
  9582. (list nil))
  9583. (while (and (string-match rexp string
  9584. (if (and notfirst
  9585. (= start (match-beginning 0))
  9586. (< start (length string)))
  9587. (1+ start) start))
  9588. (< (match-beginning 0) (length string)))
  9589. (setq notfirst t)
  9590. (or (eq (match-beginning 0) 0)
  9591. (and (eq (match-beginning 0) (match-end 0))
  9592. (eq (match-beginning 0) start))
  9593. (setq list
  9594. (cons (substring string start (match-beginning 0))
  9595. list)))
  9596. (setq start (match-end 0)))
  9597. (or (eq start (length string))
  9598. (setq list
  9599. (cons (substring string start)
  9600. list)))
  9601. (nreverse list)))
  9602. (defun org-table-map-tables (function)
  9603. "Apply FUNCTION to the start of all tables in the buffer."
  9604. (save-excursion
  9605. (save-restriction
  9606. (widen)
  9607. (goto-char (point-min))
  9608. (while (re-search-forward org-table-any-line-regexp nil t)
  9609. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  9610. (beginning-of-line 1)
  9611. (if (looking-at org-table-line-regexp)
  9612. (save-excursion (funcall function)))
  9613. (re-search-forward org-table-any-border-regexp nil 1))))
  9614. (message "Mapping tables: done"))
  9615. (defun org-table-sum (&optional beg end nlast)
  9616. "Sum numbers in region of current table column.
  9617. The result will be displayed in the echo area, and will be available
  9618. as kill to be inserted with \\[yank].
  9619. If there is an active region, it is interpreted as a rectangle and all
  9620. numbers in that rectangle will be summed. If there is no active
  9621. region and point is located in a table column, sum all numbers in that
  9622. column.
  9623. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  9624. numbers are assumed to be times as well (in decimal hours) and the
  9625. numbers are added as such.
  9626. If NLAST is a number, only the NLAST fields will actually be summed."
  9627. (interactive)
  9628. (save-excursion
  9629. (let (col (timecnt 0) diff h m s org-table-clip)
  9630. (cond
  9631. ((and beg end)) ; beg and end given explicitly
  9632. ((org-region-active-p)
  9633. (setq beg (region-beginning) end (region-end)))
  9634. (t
  9635. (setq col (org-table-current-column))
  9636. (goto-char (org-table-begin))
  9637. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  9638. (error "No table data"))
  9639. (org-table-goto-column col)
  9640. ;not needed? (skip-chars-backward "^|")
  9641. (setq beg (point))
  9642. (goto-char (org-table-end))
  9643. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  9644. (error "No table data"))
  9645. (org-table-goto-column col)
  9646. ;not needed? (skip-chars-forward "^|")
  9647. (setq end (point))))
  9648. (let* ((items (apply 'append (org-table-copy-region beg end)))
  9649. (items1 (cond ((not nlast) items)
  9650. ((>= nlast (length items)) items)
  9651. (t (setq items (reverse items))
  9652. (setcdr (nthcdr (1- nlast) items) nil)
  9653. (nreverse items))))
  9654. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  9655. items1)))
  9656. (res (apply '+ numbers))
  9657. (sres (if (= timecnt 0)
  9658. (format "%g" res)
  9659. (setq diff (* 3600 res)
  9660. h (floor (/ diff 3600)) diff (mod diff 3600)
  9661. m (floor (/ diff 60)) diff (mod diff 60)
  9662. s diff)
  9663. (format "%d:%02d:%02d" h m s))))
  9664. (kill-new sres)
  9665. (if (interactive-p)
  9666. (message "%s"
  9667. (substitute-command-keys
  9668. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  9669. (length numbers) sres))))
  9670. sres))))
  9671. (defun org-table-get-number-for-summing (s)
  9672. (let (n)
  9673. (if (string-match "^ *|? *" s)
  9674. (setq s (replace-match "" nil nil s)))
  9675. (if (string-match " *|? *$" s)
  9676. (setq s (replace-match "" nil nil s)))
  9677. (setq n (string-to-number s))
  9678. (cond
  9679. ((and (string-match "0" s)
  9680. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  9681. ((string-match "\\`[ \t]+\\'" s) nil)
  9682. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  9683. (let ((h (string-to-number (or (match-string 1 s) "0")))
  9684. (m (string-to-number (or (match-string 2 s) "0")))
  9685. (s (string-to-number (or (match-string 4 s) "0"))))
  9686. (if (boundp 'timecnt) (setq timecnt (1+ timecnt)))
  9687. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  9688. ((equal n 0) nil)
  9689. (t n))))
  9690. (defun org-table-get-vertical-vector (desc &optional tbeg col)
  9691. "Get a calc vector from a column, accorting to descriptor DESC.
  9692. Optional arguments TBEG and COL can give the beginning of the table and
  9693. the current column, to avoid unnecessary parsing."
  9694. (save-excursion
  9695. (or tbeg (setq tbeg (org-table-begin)))
  9696. (or col (setq col (org-table-current-column)))
  9697. (let (beg end nn n n1 n2 l (thisline (org-current-line)) hline-list)
  9698. (cond
  9699. ((string-match "\\(I+\\)\\(-\\(I+\\)\\)?" desc)
  9700. (setq n1 (- (match-end 1) (match-beginning 1)))
  9701. (if (match-beginning 3)
  9702. (setq n2 (- (match-end 2) (match-beginning 3))))
  9703. (setq n (if n2 (max n1 n2) n1))
  9704. (setq n1 (if n2 (min n1 n2)))
  9705. (setq nn n)
  9706. (while (and (> nn 0)
  9707. (re-search-backward org-table-hline-regexp tbeg t))
  9708. (push (org-current-line) hline-list)
  9709. (setq nn (1- nn)))
  9710. (setq hline-list (nreverse hline-list))
  9711. (goto-line (nth (1- n) hline-list))
  9712. (when (re-search-forward org-table-dataline-regexp)
  9713. (org-table-goto-column col)
  9714. (setq beg (point)))
  9715. (goto-line (if n1 (nth (1- n1) hline-list) thisline))
  9716. (when (re-search-backward org-table-dataline-regexp)
  9717. (org-table-goto-column col)
  9718. (setq end (point)))
  9719. (setq l (apply 'append (org-table-copy-region beg end)))
  9720. (concat "[" (mapconcat (lambda (x) (setq x (org-trim x))
  9721. (if (equal x "") "0" x))
  9722. l ",") "]"))
  9723. ((string-match "\\([0-9]+\\)-\\([0-9]+\\)" desc)
  9724. (setq n1 (string-to-number (match-string 1 desc))
  9725. n2 (string-to-number (match-string 2 desc)))
  9726. (beginning-of-line 1)
  9727. (save-excursion
  9728. (when (re-search-backward org-table-dataline-regexp tbeg t n1)
  9729. (org-table-goto-column col)
  9730. (setq beg (point))))
  9731. (when (re-search-backward org-table-dataline-regexp tbeg t n2)
  9732. (org-table-goto-column col)
  9733. (setq end (point)))
  9734. (setq l (apply 'append (org-table-copy-region beg end)))
  9735. (concat "[" (mapconcat
  9736. (lambda (x) (setq x (org-trim x))
  9737. (if (equal x "") "0" x))
  9738. l ",") "]"))
  9739. ((string-match "\\([0-9]+\\)" desc)
  9740. (beginning-of-line 1)
  9741. (when (re-search-backward org-table-dataline-regexp tbeg t
  9742. (string-to-number (match-string 0 desc)))
  9743. (org-table-goto-column col)
  9744. (org-trim (org-table-get-field))))))))
  9745. (defvar org-table-formula-history nil)
  9746. (defvar org-table-column-names nil
  9747. "Alist with column names, derived from the `!' line.")
  9748. (defvar org-table-column-name-regexp nil
  9749. "Regular expression matching the current column names.")
  9750. (defvar org-table-local-parameters nil
  9751. "Alist with parameter names, derived from the `$' line.")
  9752. (defvar org-table-named-field-locations nil
  9753. "Alist with locations of named fields.")
  9754. (defun org-table-get-formula (&optional equation named)
  9755. "Read a formula from the minibuffer, offer stored formula as default."
  9756. (let* ((name (car (rassoc (list (org-current-line)
  9757. (org-table-current-column))
  9758. org-table-named-field-locations)))
  9759. (scol (if named
  9760. (if name name
  9761. (error "Not in a named field"))
  9762. (int-to-string (org-table-current-column))))
  9763. (dummy (and name (not named)
  9764. (not (y-or-n-p "Replace named-field formula with column equation? " ))
  9765. (error "Abort")))
  9766. (org-table-may-need-update nil)
  9767. (stored-list (org-table-get-stored-formulas))
  9768. (stored (cdr (assoc scol stored-list)))
  9769. (eq (cond
  9770. ((and stored equation (string-match "^ *=? *$" equation))
  9771. stored)
  9772. ((stringp equation)
  9773. equation)
  9774. (t (read-string
  9775. (format "%s formula $%s=" (if named "Field" "Column") scol)
  9776. (or stored "") 'org-table-formula-history
  9777. ;stored
  9778. ))))
  9779. mustsave)
  9780. (when (not (string-match "\\S-" eq))
  9781. ;; remove formula
  9782. (setq stored-list (delq (assoc scol stored-list) stored-list))
  9783. (org-table-store-formulas stored-list)
  9784. (error "Formula removed"))
  9785. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  9786. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  9787. (if (and name (not named))
  9788. ;; We set the column equation, delete the named one.
  9789. (setq stored-list (delq (assoc name stored-list) stored-list)
  9790. mustsave t))
  9791. (if stored
  9792. (setcdr (assoc scol stored-list) eq)
  9793. (setq stored-list (cons (cons scol eq) stored-list)))
  9794. (if (or mustsave (not (equal stored eq)))
  9795. (org-table-store-formulas stored-list))
  9796. eq))
  9797. (defun org-table-store-formulas (alist)
  9798. "Store the list of formulas below the current table."
  9799. (setq alist (sort alist (lambda (a b) (string< (car a) (car b)))))
  9800. (save-excursion
  9801. (goto-char (org-table-end))
  9802. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:.*\n?")
  9803. (delete-region (point) (match-end 0)))
  9804. (insert "#+TBLFM: "
  9805. (mapconcat (lambda (x)
  9806. (concat "$" (car x) "=" (cdr x)))
  9807. alist "::")
  9808. "\n")))
  9809. (defun org-table-get-stored-formulas ()
  9810. "Return an alist with the stored formulas directly after current table."
  9811. (interactive)
  9812. (let (scol eq eq-alist strings string seen)
  9813. (save-excursion
  9814. (goto-char (org-table-end))
  9815. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  9816. (setq strings (org-split-string (match-string 2) " *:: *"))
  9817. (while (setq string (pop strings))
  9818. (when (string-match "\\$\\([a-zA-Z0-9]+\\) *= *\\(.*[^ \t]\\)" string)
  9819. (setq scol (match-string 1 string)
  9820. eq (match-string 2 string)
  9821. eq-alist (cons (cons scol eq) eq-alist))
  9822. (if (member scol seen)
  9823. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  9824. (push scol seen))))))
  9825. (nreverse eq-alist)))
  9826. (defun org-table-modify-formulas (action &rest columns)
  9827. "Modify the formulas stored below the current table.
  9828. ACTION can be `remove', `insert', `swap'. For `swap', two column numbers are
  9829. expected, for the other actions only a single column number is needed."
  9830. (let ((list (org-table-get-stored-formulas))
  9831. (nmax (length (org-split-string
  9832. (buffer-substring (point-at-bol) (point-at-eol))
  9833. "|")))
  9834. col col1 col2 scol si sc1 sc2)
  9835. (cond
  9836. ((null list)) ; No action needed if there are no stored formulas
  9837. ((eq action 'remove)
  9838. (setq col (car columns)
  9839. scol (int-to-string col))
  9840. (org-table-replace-in-formulas list scol "INVALID")
  9841. (if (assoc scol list) (setq list (delq (assoc scol list) list)))
  9842. (loop for i from (1+ col) upto nmax by 1 do
  9843. (setq si (int-to-string i))
  9844. (org-table-replace-in-formulas list si (int-to-string (1- i)))
  9845. (if (assoc si list) (setcar (assoc si list)
  9846. (int-to-string (1- i))))))
  9847. ((eq action 'insert)
  9848. (setq col (car columns))
  9849. (loop for i from nmax downto col by 1 do
  9850. (setq si (int-to-string i))
  9851. (org-table-replace-in-formulas list si (int-to-string (1+ i)))
  9852. (if (assoc si list) (setcar (assoc si list)
  9853. (int-to-string (1+ i))))))
  9854. ((eq action 'swap)
  9855. (setq col1 (car columns) col2 (nth 1 columns)
  9856. sc1 (int-to-string col1) sc2 (int-to-string col2))
  9857. ;; Hopefully, ZqZtZ will never be a name in a table
  9858. (org-table-replace-in-formulas list sc1 "ZqZtZ")
  9859. (org-table-replace-in-formulas list sc2 sc1)
  9860. (org-table-replace-in-formulas list "ZqZtZ" sc2)
  9861. (if (assoc sc1 list) (setcar (assoc sc1 list) "ZqZtZ"))
  9862. (if (assoc sc2 list) (setcar (assoc sc2 list) sc1))
  9863. (if (assoc "ZqZtZ" list) (setcar (assoc "ZqZtZ" list) sc2)))
  9864. (t (error "Invalid action in `org-table-modify-formulas'")))
  9865. (if list (org-table-store-formulas list))))
  9866. (defun org-table-replace-in-formulas (list s1 s2)
  9867. (let (elt re s)
  9868. (setq s1 (concat "$" (if (integerp s1) (int-to-string s1) s1))
  9869. s2 (concat "$" (if (integerp s2) (int-to-string s2) s2))
  9870. re (concat (regexp-quote s1) "\\>"))
  9871. (while (setq elt (pop list))
  9872. (setq s (cdr elt))
  9873. (while (string-match re s)
  9874. (setq s (replace-match s2 t t s)))
  9875. (setcdr elt s))))
  9876. (defun org-table-get-specials ()
  9877. "Get the column names and local parameters for this table."
  9878. (save-excursion
  9879. (let ((beg (org-table-begin)) (end (org-table-end))
  9880. names name fields fields1 field cnt c v line col)
  9881. (setq org-table-column-names nil
  9882. org-table-local-parameters nil
  9883. org-table-named-field-locations nil)
  9884. (goto-char beg)
  9885. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  9886. (setq names (org-split-string (match-string 1) " *| *")
  9887. cnt 1)
  9888. (while (setq name (pop names))
  9889. (setq cnt (1+ cnt))
  9890. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  9891. (push (cons name (int-to-string cnt)) org-table-column-names))))
  9892. (setq org-table-column-names (nreverse org-table-column-names))
  9893. (setq org-table-column-name-regexp
  9894. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  9895. (goto-char beg)
  9896. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  9897. (setq fields (org-split-string (match-string 1) " *| *"))
  9898. (while (setq field (pop fields))
  9899. (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  9900. (push (cons (match-string 1 field) (match-string 2 field))
  9901. org-table-local-parameters))))
  9902. (goto-char beg)
  9903. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  9904. (setq c (match-string 1)
  9905. fields (org-split-string (match-string 2) " *| *"))
  9906. (save-excursion
  9907. (beginning-of-line (if (equal c "_") 2 0))
  9908. (setq line (org-current-line) col 1)
  9909. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  9910. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  9911. (while (and fields1 (setq field (pop fields)))
  9912. (setq v (pop fields1) col (1+ col))
  9913. (when (and (stringp field) (stringp v)
  9914. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  9915. (push (cons field v) org-table-local-parameters)
  9916. (push (list field line col) org-table-named-field-locations)))))))
  9917. (defun org-this-word ()
  9918. ;; Get the current word
  9919. (save-excursion
  9920. (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
  9921. (end (progn (skip-chars-forward "^ \t\n") (point))))
  9922. (buffer-substring-no-properties beg end))))
  9923. (defun org-table-maybe-eval-formula ()
  9924. "Check if the current field starts with \"=\" or \":=\".
  9925. If yes, store the formula and apply it."
  9926. ;; We already know we are in a table. Get field will only return a formula
  9927. ;; when appropriate. It might return a separator line, but no problem.
  9928. (when org-table-formula-evaluate-inline
  9929. (let* ((field (org-trim (or (org-table-get-field) "")))
  9930. named eq)
  9931. (when (string-match "^:?=\\(.*\\)" field)
  9932. (setq named (equal (string-to-char field) ?:)
  9933. eq (match-string 1 field))
  9934. (if (fboundp 'calc-eval)
  9935. (org-table-eval-formula (if named '(4) nil) eq))))))
  9936. (defvar org-recalc-commands nil
  9937. "List of commands triggering the recalculation of a line.
  9938. Will be filled automatically during use.")
  9939. (defvar org-recalc-marks
  9940. '((" " . "Unmarked: no special line, no automatic recalculation")
  9941. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9942. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9943. ("!" . "Column name definition line. Reference in formula as $name.")
  9944. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9945. ("_" . "Names for values in row below this one.")
  9946. ("^" . "Names for values in row above this one.")))
  9947. (defun org-table-rotate-recalc-marks (&optional newchar)
  9948. "Rotate the recalculation mark in the first column.
  9949. If in any row, the first field is not consistent with a mark,
  9950. insert a new column for the markers.
  9951. When there is an active region, change all the lines in the region,
  9952. after prompting for the marking character.
  9953. After each change, a message will be displayed indicating the meaning
  9954. of the new mark."
  9955. (interactive)
  9956. (unless (org-at-table-p) (error "Not at a table"))
  9957. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9958. (beg (org-table-begin))
  9959. (end (org-table-end))
  9960. (l (org-current-line))
  9961. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9962. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9963. (have-col
  9964. (save-excursion
  9965. (goto-char beg)
  9966. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9967. (col (org-table-current-column))
  9968. (forcenew (car (assoc newchar org-recalc-marks)))
  9969. epos new)
  9970. (when l1
  9971. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9972. (setq newchar (char-to-string (read-char-exclusive))
  9973. forcenew (car (assoc newchar org-recalc-marks))))
  9974. (if (and newchar (not forcenew))
  9975. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9976. newchar))
  9977. (if l1 (goto-line l1))
  9978. (save-excursion
  9979. (beginning-of-line 1)
  9980. (unless (looking-at org-table-dataline-regexp)
  9981. (error "Not at a table data line")))
  9982. (unless have-col
  9983. (org-table-goto-column 1)
  9984. (org-table-insert-column)
  9985. (org-table-goto-column (1+ col)))
  9986. (setq epos (point-at-eol))
  9987. (save-excursion
  9988. (beginning-of-line 1)
  9989. (org-table-get-field
  9990. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9991. (concat " "
  9992. (setq new (or forcenew
  9993. (cadr (member (match-string 1) marks))))
  9994. " ")
  9995. " # ")))
  9996. (if (and l1 l2)
  9997. (progn
  9998. (goto-line l1)
  9999. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  10000. (and (looking-at org-table-dataline-regexp)
  10001. (org-table-get-field 1 (concat " " new " "))))
  10002. (goto-line l1)))
  10003. (if (not (= epos (point-at-eol))) (org-table-align))
  10004. (goto-line l)
  10005. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  10006. (defun org-table-maybe-recalculate-line ()
  10007. "Recompute the current line if marked for it, and if we haven't just done it."
  10008. (interactive)
  10009. (and org-table-allow-automatic-line-recalculation
  10010. (not (and (memq last-command org-recalc-commands)
  10011. (equal org-last-recalc-line (org-current-line))))
  10012. (save-excursion (beginning-of-line 1)
  10013. (looking-at org-table-auto-recalculate-regexp))
  10014. (fboundp 'calc-eval)
  10015. (org-table-recalculate) t))
  10016. (defvar org-table-formula-debug nil
  10017. "Non-nil means, debug table formulas.
  10018. When nil, simply write \"#ERROR\" in corrupted fields.")
  10019. (defvar modes)
  10020. (defsubst org-set-calc-mode (var &optional value)
  10021. (if (stringp var)
  10022. (setq var (assoc var '(("D" calc-angle-mode deg)
  10023. ("R" calc-angle-mode rad)
  10024. ("F" calc-prefer-frac t)
  10025. ("S" calc-symbolic-mode t)))
  10026. value (nth 2 var) var (nth 1 var)))
  10027. (if (memq var modes)
  10028. (setcar (cdr (memq var modes)) value)
  10029. (cons var (cons value modes)))
  10030. modes)
  10031. (defun org-table-eval-formula (&optional arg equation
  10032. suppress-align suppress-const
  10033. suppress-store)
  10034. "Replace the table field value at the cursor by the result of a calculation.
  10035. This function makes use of Dave Gillespie's Calc package, in my view the
  10036. most exciting program ever written for GNU Emacs. So you need to have Calc
  10037. installed in order to use this function.
  10038. In a table, this command replaces the value in the current field with the
  10039. result of a formula. It also installs the formula as the \"current\" column
  10040. formula, by storing it in a special line below the table. When called
  10041. with a `C-u' prefix, the current field must ba a named field, and the
  10042. formula is installed as valid in only this specific field.
  10043. When called, the command first prompts for a formula, which is read in
  10044. the minibuffer. Previously entered formulas are available through the
  10045. history list, and the last used formula is offered as a default.
  10046. These stored formulas are adapted correctly when moving, inserting, or
  10047. deleting columns with the corresponding commands.
  10048. The formula can be any algebraic expression understood by the Calc package.
  10049. For details, see the Org-mode manual.
  10050. This function can also be called from Lisp programs and offers
  10051. additional arguments: EQUATION can be the formula to apply. If this
  10052. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  10053. used to speed-up recursive calls by by-passing unnecessary aligns.
  10054. SUPPRESS-CONST suppresses the interpretation of constants in the
  10055. formula, assuming that this has been done already outside the function.
  10056. SUPPRESS-STORE means the formula should not be stored, either because
  10057. it is already stored, or because it is a modified equation that should
  10058. not overwrite the stored one."
  10059. (interactive "P")
  10060. (require 'calc)
  10061. (org-table-check-inside-data-field)
  10062. (org-table-get-specials)
  10063. (let* (fields
  10064. (ndown (if (integerp arg) arg 1))
  10065. (org-table-automatic-realign nil)
  10066. (case-fold-search nil)
  10067. (down (> ndown 1))
  10068. (formula (if (and equation suppress-store)
  10069. equation
  10070. (org-table-get-formula equation (equal arg '(4)))))
  10071. (n0 (org-table-current-column))
  10072. (modes (copy-sequence org-calc-default-modes))
  10073. n form fmt x ev orig c lispp)
  10074. ;; Parse the format string. Since we have a lot of modes, this is
  10075. ;; a lot of work. However, I think calc still uses most of the time.
  10076. (if (string-match ";" formula)
  10077. (let ((tmp (org-split-string formula ";")))
  10078. (setq formula (car tmp)
  10079. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  10080. (nth 1 tmp)))
  10081. (while (string-match "[pnfse]\\(-?[0-9]+\\)" fmt)
  10082. (setq c (string-to-char (match-string 1 fmt))
  10083. n (string-to-number (or (match-string 1 fmt) "")))
  10084. (if (= c ?p) (setq modes (org-set-calc-mode 'calc-internal-prec n))
  10085. (setq modes (org-set-calc-mode
  10086. 'calc-float-format
  10087. (list (cdr (assoc c '((?n . float) (?f . fix)
  10088. (?s . sci) (?e . eng))))
  10089. n))))
  10090. (setq fmt (replace-match "" t t fmt)))
  10091. (while (string-match "[DRFS]" fmt)
  10092. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  10093. (setq fmt (replace-match "" t t fmt)))
  10094. (unless (string-match "\\S-" fmt)
  10095. (setq fmt nil))))
  10096. (if (and (not suppress-const) org-table-formula-use-constants)
  10097. (setq formula (org-table-formula-substitute-names formula)))
  10098. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  10099. (while (> ndown 0)
  10100. (setq fields (org-split-string
  10101. (buffer-substring
  10102. (point-at-bol) (point-at-eol)) " *| *"))
  10103. (if org-table-formula-numbers-only
  10104. (setq fields (mapcar
  10105. (lambda (x) (number-to-string (string-to-number x)))
  10106. fields)))
  10107. (setq ndown (1- ndown))
  10108. (setq form (copy-sequence formula)
  10109. lispp (equal (substring form 0 2) "'("))
  10110. ;; Insert the references to fields in same row
  10111. (while (string-match "\\$\\([0-9]+\\)?" form)
  10112. (setq n (if (match-beginning 1)
  10113. (string-to-number (match-string 1 form))
  10114. n0)
  10115. x (nth (1- n) fields))
  10116. (unless x (error "Invalid field specifier \"%s\""
  10117. (match-string 0 form)))
  10118. (if (equal x "") (setq x "0"))
  10119. (setq form (replace-match
  10120. (if lispp x (concat "(" x ")"))
  10121. t t form)))
  10122. ;; Insert ranges in current column
  10123. (while (string-match "\\&[-I0-9]+" form)
  10124. (setq form (replace-match
  10125. (save-match-data
  10126. (org-table-get-vertical-vector (match-string 0 form)
  10127. nil n0))
  10128. t t form)))
  10129. (if lispp
  10130. (setq ev (eval (eval (read form)))
  10131. ev (if (numberp ev) (number-to-string ev) ev))
  10132. (setq ev (calc-eval (cons form modes)
  10133. (if org-table-formula-numbers-only 'num))))
  10134. (when org-table-formula-debug
  10135. (with-output-to-temp-buffer "*Help*"
  10136. (princ (format "Substitution history of formula
  10137. Orig: %s
  10138. $xyz-> %s
  10139. $1-> %s\n" orig formula form))
  10140. (if (listp ev)
  10141. (princ (format " %s^\nError: %s"
  10142. (make-string (car ev) ?\-) (nth 1 ev)))
  10143. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  10144. ev (or fmt "NONE")
  10145. (if fmt (format fmt (string-to-number ev)) ev)))))
  10146. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  10147. (unless (and (interactive-p) (not ndown))
  10148. (unless (let (inhibit-redisplay)
  10149. (y-or-n-p "Debugging Formula. Continue to next? "))
  10150. (org-table-align)
  10151. (error "Abort"))
  10152. (delete-window (get-buffer-window "*Help*"))
  10153. (message "")))
  10154. (if (listp ev) (setq fmt nil ev "#ERROR"))
  10155. (org-table-justify-field-maybe
  10156. (if fmt (format fmt (string-to-number ev)) ev))
  10157. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  10158. (call-interactively 'org-return)
  10159. (setq ndown 0)))
  10160. (and down (org-table-maybe-recalculate-line))
  10161. (or suppress-align (and org-table-may-need-update
  10162. (org-table-align)))))
  10163. (defun org-table-recalculate (&optional all noalign)
  10164. "Recalculate the current table line by applying all stored formulas.
  10165. With prefix arg ALL, do this for all lines in the table."
  10166. (interactive "P")
  10167. (or (memq this-command org-recalc-commands)
  10168. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  10169. (unless (org-at-table-p) (error "Not at a table"))
  10170. (org-table-get-specials)
  10171. (let* ((eqlist (sort (org-table-get-stored-formulas)
  10172. (lambda (a b) (string< (car a) (car b)))))
  10173. (inhibit-redisplay t)
  10174. (line-re org-table-dataline-regexp)
  10175. (thisline (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  10176. (thiscol (org-table-current-column))
  10177. beg end entry eqlnum eqlname eql (cnt 0) eq a name)
  10178. ;; Insert constants in all formulas
  10179. (setq eqlist
  10180. (mapcar (lambda (x)
  10181. (setcdr x (org-table-formula-substitute-names (cdr x)))
  10182. x)
  10183. eqlist))
  10184. ;; Split the equation list
  10185. (while (setq eq (pop eqlist))
  10186. (if (<= (string-to-char (car eq)) ?9)
  10187. (push eq eqlnum)
  10188. (push eq eqlname)))
  10189. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  10190. (if all
  10191. (progn
  10192. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  10193. (goto-char (setq beg (org-table-begin)))
  10194. (if (re-search-forward org-table-calculate-mark-regexp end t)
  10195. ;; This is a table with marked lines, only compute selected lines
  10196. (setq line-re org-table-recalculate-regexp)
  10197. ;; Move forward to the first non-header line
  10198. (if (and (re-search-forward org-table-dataline-regexp end t)
  10199. (re-search-forward org-table-hline-regexp end t)
  10200. (re-search-forward org-table-dataline-regexp end t))
  10201. (setq beg (match-beginning 0))
  10202. nil))) ;; just leave beg where it is
  10203. (setq beg (point-at-bol)
  10204. end (move-marker (make-marker) (1+ (point-at-eol)))))
  10205. (goto-char beg)
  10206. (and all (message "Re-applying formulas to full table..."))
  10207. (while (re-search-forward line-re end t)
  10208. (unless (string-match "^ *[_^!$] *$" (org-table-get-field 1))
  10209. ;; Unprotected line, recalculate
  10210. (and all (message "Re-applying formulas to full table...(line %d)"
  10211. (setq cnt (1+ cnt))))
  10212. (setq org-last-recalc-line (org-current-line))
  10213. (setq eql eqlnum)
  10214. (while (setq entry (pop eql))
  10215. (goto-line org-last-recalc-line)
  10216. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  10217. (org-table-eval-formula nil (cdr entry) 'noalign 'nocst 'nostore))))
  10218. (goto-line thisline)
  10219. (org-table-goto-column thiscol)
  10220. (or noalign (and org-table-may-need-update (org-table-align))
  10221. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  10222. ;; Now do the names fields
  10223. (while (setq eq (pop eqlname))
  10224. (setq name (car eq)
  10225. a (assoc name org-table-named-field-locations))
  10226. (when a
  10227. (message "Re-applying formula to named field: %s" name)
  10228. (goto-line (nth 1 a))
  10229. (org-table-goto-column (nth 2 a))
  10230. (org-table-eval-formula nil (cdr eq) 'noalign 'nocst 'nostore)))
  10231. ;; back to initial position
  10232. (goto-line thisline)
  10233. (org-table-goto-column thiscol)
  10234. (or noalign (and org-table-may-need-update (org-table-align))
  10235. (and all (message "Re-applying formulas...done")))))
  10236. (defun org-table-formula-substitute-names (f)
  10237. "Replace $const with values in string F."
  10238. (let ((start 0) a n1 n2 nn1 nn2 s (f1 f))
  10239. ;; First, check for column names
  10240. (while (setq start (string-match org-table-column-name-regexp f start))
  10241. (setq start (1+ start))
  10242. (setq a (assoc (match-string 1 f) org-table-column-names))
  10243. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  10244. ;; Expand ranges to vectors
  10245. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\.?\\$\\([0-9]+\\)" f)
  10246. (setq n1 (string-to-number (match-string 1 f))
  10247. n2 (string-to-number (match-string 2 f))
  10248. nn1 (1+ (min n1 n2)) nn2 (max n1 n2)
  10249. s (concat "[($" (number-to-string (1- nn1)) ")"))
  10250. (loop for i from nn1 upto nn2 do
  10251. (setq s (concat s ",($" (int-to-string i) ")")))
  10252. (setq s (concat s "]"))
  10253. (if (< n2 n1) (setq s (concat "rev(" s ")")))
  10254. (setq f (replace-match s t t f)))
  10255. ;; Parameters and constants
  10256. (setq start 0)
  10257. (while (setq start (string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start))
  10258. (setq start (1+ start))
  10259. (if (setq a (save-match-data
  10260. (org-table-get-constant (match-string 1 f))))
  10261. (setq f (replace-match (concat "(" a ")") t t f))))
  10262. (if org-table-formula-debug
  10263. (put-text-property 0 (length f) :orig-formula f1 f))
  10264. f))
  10265. (defun org-table-get-constant (const)
  10266. "Find the value for a parameter or constant in a formula.
  10267. Parameters get priority."
  10268. (or (cdr (assoc const org-table-local-parameters))
  10269. (cdr (assoc const org-table-formula-constants))
  10270. (and (fboundp 'constants-get) (constants-get const))
  10271. "#UNDEFINED_NAME"))
  10272. (defvar org-edit-formulas-map (make-sparse-keymap))
  10273. (define-key org-edit-formulas-map "\C-c\C-c" 'org-finish-edit-formulas)
  10274. (define-key org-edit-formulas-map "\C-c\C-q" 'org-abort-edit-formulas)
  10275. (define-key org-edit-formulas-map "\C-c?" 'org-show-variable)
  10276. (defvar org-pos)
  10277. (defun org-table-edit-formulas ()
  10278. "Edit the formulas of the current table in a separate buffer."
  10279. (interactive)
  10280. (unless (org-at-table-p)
  10281. (error "Not at a table"))
  10282. (org-table-get-specials)
  10283. (let ((eql (org-table-get-stored-formulas))
  10284. (pos (move-marker (make-marker) (point)))
  10285. (wc (current-window-configuration))
  10286. entry loc s)
  10287. (switch-to-buffer-other-window "*Edit Formulas*")
  10288. (erase-buffer)
  10289. (fundamental-mode)
  10290. (set (make-local-variable 'org-pos) pos)
  10291. (set (make-local-variable 'org-window-configuration) wc)
  10292. (use-local-map org-edit-formulas-map)
  10293. (setq s "# Edit formulas and finish with `C-c C-c'.
  10294. # Use `C-u C-c C-c' to also appy them immediately to the entire table.
  10295. # Use `C-c ?' to get information about $name at point.
  10296. # To cancel editing, press `C-c C-q'.\n")
  10297. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  10298. (insert s)
  10299. (while (setq entry (pop eql))
  10300. (when (setq loc (assoc (car entry) org-table-named-field-locations))
  10301. (setq s (format "# Named formula, referring to column %d in line %d\n"
  10302. (nth 2 loc) (nth 1 loc)))
  10303. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  10304. (insert s))
  10305. (setq s (concat "$" (car entry) "=" (cdr entry) "\n"))
  10306. (remove-text-properties 0 (length s) '(face nil) s)
  10307. (insert s))
  10308. (goto-char (point-min))
  10309. (message "Edit formulas and finish with `C-c C-c'.")))
  10310. (defun org-show-variable ()
  10311. "Show the location/value of the $ expression at point."
  10312. (interactive)
  10313. (let (var (pos org-pos) (win (selected-window)) e)
  10314. (save-excursion
  10315. (or (looking-at "\\$") (skip-chars-backward "$a-zA-Z0-9"))
  10316. (if (looking-at "\\$\\([a-zA-Z0-9]+\\)")
  10317. (setq var (match-string 1))
  10318. (error "No variable at point")))
  10319. (cond
  10320. ((setq e (assoc var org-table-named-field-locations))
  10321. (switch-to-buffer-other-window (marker-buffer pos))
  10322. (goto-line (nth 1 e))
  10323. (org-table-goto-column (nth 2 e))
  10324. (select-window win)
  10325. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10326. ((setq e (assoc var org-table-column-names))
  10327. (switch-to-buffer-other-window (marker-buffer pos))
  10328. (goto-char pos)
  10329. (goto-char (org-table-begin))
  10330. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10331. (org-table-end) t)
  10332. (progn
  10333. (goto-char (match-beginning 1))
  10334. (message "Named column (column %s)" (cdr e)))
  10335. (error "Column name not found"))
  10336. (select-window win))
  10337. ((string-match "^[0-9]$" var)
  10338. ;; column number
  10339. (switch-to-buffer-other-window (marker-buffer pos))
  10340. (goto-char pos)
  10341. (goto-char (org-table-begin))
  10342. (recenter 1)
  10343. (if (re-search-forward org-table-dataline-regexp
  10344. (org-table-end) t)
  10345. (progn
  10346. (goto-char (match-beginning 0))
  10347. (org-table-goto-column (string-to-number var))
  10348. (message "Column %s" var))
  10349. (error "Column name not found"))
  10350. (select-window win))
  10351. ((setq e (assoc var org-table-local-parameters))
  10352. (switch-to-buffer-other-window (marker-buffer pos))
  10353. (goto-char pos)
  10354. (goto-char (org-table-begin))
  10355. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10356. (progn
  10357. (goto-char (match-beginning 1))
  10358. (message "Local parameter."))
  10359. (error "Parameter not found"))
  10360. (select-window win))
  10361. (t
  10362. (cond
  10363. ((setq e (assoc var org-table-formula-constants))
  10364. (message "Constant: $%s=%s in `org-table-formula-constants'." var (cdr e)))
  10365. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10366. (message "Constant: $%s=%s, retrieved from `constants.el'." var e))
  10367. (t (error "Undefined name $%s" var)))))))
  10368. (defun org-finish-edit-formulas (&optional arg)
  10369. "Parse the buffer for formula definitions and install them.
  10370. With prefix ARG, apply the new formulas to the table."
  10371. (interactive "P")
  10372. (let ((pos org-pos) eql)
  10373. (goto-char (point-min))
  10374. (while (re-search-forward "^\\$\\([a-zA-Z0-9]+\\) *= *\\(.*?\\) *$" nil t)
  10375. (push (cons (match-string 1) (match-string 2)) eql))
  10376. (set-window-configuration org-window-configuration)
  10377. (select-window (get-buffer-window (marker-buffer pos)))
  10378. (goto-char pos)
  10379. (unless (org-at-table-p)
  10380. (error "Lost table position - cannot install formulae"))
  10381. (org-table-store-formulas eql)
  10382. (move-marker pos nil)
  10383. (kill-buffer "*Edit Formulas*")
  10384. (if arg
  10385. (org-table-recalculate 'all)
  10386. (message "New formulas installed - press C-u C-c C-c to apply."))))
  10387. (defun org-abort-edit-formulas ()
  10388. "Abort editing formulas, without installing the changes."
  10389. (interactive)
  10390. (let ((pos org-pos))
  10391. (set-window-configuration org-window-configuration)
  10392. (select-window (get-buffer-window (marker-buffer pos)))
  10393. (goto-char pos)
  10394. (message "Formula editing aborted without installing changes")))
  10395. ;;; The orgtbl minor mode
  10396. ;; Define a minor mode which can be used in other modes in order to
  10397. ;; integrate the org-mode table editor.
  10398. ;; This is really a hack, because the org-mode table editor uses several
  10399. ;; keys which normally belong to the major mode, for example the TAB and
  10400. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10401. ;; necessary to operate the table editor, but wraps the commands into a
  10402. ;; function which tests if the cursor is currently inside a table. If that
  10403. ;; is the case, the table editor command is executed. However, when any of
  10404. ;; those keys is used outside a table, the function uses `key-binding' to
  10405. ;; look up if the key has an associated command in another currently active
  10406. ;; keymap (minor modes, major mode, global), and executes that command.
  10407. ;; There might be problems if any of the keys used by the table editor is
  10408. ;; otherwise used as a prefix key.
  10409. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10410. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10411. ;; addresses this by checking explicitly for both bindings.
  10412. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10413. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10414. ;; Some modes bind other commands to simple characters, for example
  10415. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10416. ;; active, this binding is ignored inside tables and replaced with a
  10417. ;; modified self-insert.
  10418. (defvar orgtbl-mode nil
  10419. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10420. table editor in arbitrary modes.")
  10421. (make-variable-buffer-local 'orgtbl-mode)
  10422. (defvar orgtbl-mode-map (make-keymap)
  10423. "Keymap for `orgtbl-mode'.")
  10424. ;;;###autoload
  10425. (defun turn-on-orgtbl ()
  10426. "Unconditionally turn on `orgtbl-mode'."
  10427. (orgtbl-mode 1))
  10428. ;;;###autoload
  10429. (defun orgtbl-mode (&optional arg)
  10430. "The `org-mode' table editor as a minor mode for use in other modes."
  10431. (interactive)
  10432. (if (eq major-mode 'org-mode)
  10433. ;; Exit without error, in case some hook functions calls this
  10434. ;; by accident in org-mode.
  10435. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10436. (setq orgtbl-mode
  10437. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10438. (if orgtbl-mode
  10439. (progn
  10440. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10441. ;; Make sure we are first in minor-mode-map-alist
  10442. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10443. (and c (setq minor-mode-map-alist
  10444. (cons c (delq c minor-mode-map-alist)))))
  10445. (set (make-local-variable (quote org-table-may-need-update)) t)
  10446. (org-add-hook 'before-change-functions 'org-before-change-function
  10447. nil 'local)
  10448. (set (make-local-variable 'org-old-auto-fill-inhibit-regexp)
  10449. auto-fill-inhibit-regexp)
  10450. (set (make-local-variable 'auto-fill-inhibit-regexp)
  10451. (if auto-fill-inhibit-regexp
  10452. (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp)
  10453. "[ \t]*|"))
  10454. (org-add-to-invisibility-spec '(org-cwidth))
  10455. (easy-menu-add orgtbl-mode-menu)
  10456. (run-hooks 'orgtbl-mode-hook))
  10457. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10458. (org-cleanup-narrow-column-properties)
  10459. (org-remove-from-invisibility-spec '(org-cwidth))
  10460. (remove-hook 'before-change-functions 'org-before-change-function t)
  10461. (easy-menu-remove orgtbl-mode-menu)
  10462. (force-mode-line-update 'all))))
  10463. (defun org-cleanup-narrow-column-properties ()
  10464. "Remove all properties related to narrow-column invisibility."
  10465. (let ((s 1))
  10466. (while (setq s (text-property-any s (point-max)
  10467. 'display org-narrow-column-arrow))
  10468. (remove-text-properties s (1+ s) '(display t)))
  10469. (setq s 1)
  10470. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10471. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10472. (setq s 1)
  10473. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10474. (remove-text-properties s (1+ s) '(invisible t)))))
  10475. ;; Install it as a minor mode.
  10476. (put 'orgtbl-mode :included t)
  10477. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10478. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10479. (defun orgtbl-make-binding (fun n &rest keys)
  10480. "Create a function for binding in the table minor mode.
  10481. FUN is the command to call inside a table. N is used to create a unique
  10482. command name. KEYS are keys that should be checked in for a command
  10483. to execute outside of tables."
  10484. (eval
  10485. (list 'defun
  10486. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10487. '(arg)
  10488. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10489. "Outside of tables, run the binding of `"
  10490. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10491. "'.")
  10492. '(interactive "p")
  10493. (list 'if
  10494. '(org-at-table-p)
  10495. (list 'call-interactively (list 'quote fun))
  10496. (list 'let '(orgtbl-mode)
  10497. (list 'call-interactively
  10498. (append '(or)
  10499. (mapcar (lambda (k)
  10500. (list 'key-binding k))
  10501. keys)
  10502. '('orgtbl-error))))))))
  10503. (defun orgtbl-error ()
  10504. "Error when there is no default binding for a table key."
  10505. (interactive)
  10506. (error "This key is has no function outside tables"))
  10507. (defun orgtbl-setup ()
  10508. "Setup orgtbl keymaps."
  10509. (let ((nfunc 0)
  10510. (bindings
  10511. (list
  10512. '([(meta shift left)] org-table-delete-column)
  10513. '([(meta left)] org-table-move-column-left)
  10514. '([(meta right)] org-table-move-column-right)
  10515. '([(meta shift right)] org-table-insert-column)
  10516. '([(meta shift up)] org-table-kill-row)
  10517. '([(meta shift down)] org-table-insert-row)
  10518. '([(meta up)] org-table-move-row-up)
  10519. '([(meta down)] org-table-move-row-down)
  10520. '("\C-c\C-w" org-table-cut-region)
  10521. '("\C-c\M-w" org-table-copy-region)
  10522. '("\C-c\C-y" org-table-paste-rectangle)
  10523. '("\C-c-" org-table-insert-hline)
  10524. ; '([(shift tab)] org-table-previous-field)
  10525. '("\C-m" org-table-next-row)
  10526. (list (org-key 'S-return) 'org-table-copy-down)
  10527. '([(meta return)] org-table-wrap-region)
  10528. '("\C-c\C-q" org-table-wrap-region)
  10529. '("\C-c?" org-table-current-column)
  10530. '("\C-c " org-table-blank-field)
  10531. '("\C-c+" org-table-sum)
  10532. '("\C-c=" org-table-eval-formula)
  10533. '("\C-c'" org-table-edit-formulas)
  10534. '("\C-c`" org-table-edit-field)
  10535. '("\C-c*" org-table-recalculate)
  10536. '("\C-c|" org-table-create-or-convert-from-region)
  10537. '("\C-c^" org-table-sort-lines)
  10538. '([(control ?#)] org-table-rotate-recalc-marks)))
  10539. elt key fun cmd)
  10540. (while (setq elt (pop bindings))
  10541. (setq nfunc (1+ nfunc))
  10542. (setq key (car elt)
  10543. fun (nth 1 elt)
  10544. cmd (orgtbl-make-binding fun nfunc key))
  10545. (define-key orgtbl-mode-map key cmd))
  10546. ;; Special treatment needed for TAB and RET
  10547. (define-key orgtbl-mode-map [(return)]
  10548. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10549. (define-key orgtbl-mode-map "\C-m"
  10550. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10551. (define-key orgtbl-mode-map [(tab)]
  10552. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10553. (define-key orgtbl-mode-map "\C-i"
  10554. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)])))
  10555. (define-key orgtbl-mode-map "\C-i"
  10556. (orgtbl-make-binding 'orgtbl-tab 104 [(shift tab)]))
  10557. (define-key orgtbl-mode-map "\C-c\C-c"
  10558. (orgtbl-make-binding 'org-ctrl-c-ctrl-c 105 "\C-c\C-c"))
  10559. (when orgtbl-optimized
  10560. ;; If the user wants maximum table support, we need to hijack
  10561. ;; some standard editing functions
  10562. (org-remap orgtbl-mode-map
  10563. 'self-insert-command 'orgtbl-self-insert-command
  10564. 'delete-char 'org-delete-char
  10565. 'delete-backward-char 'org-delete-backward-char)
  10566. (define-key orgtbl-mode-map "|" 'org-force-self-insert))
  10567. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10568. '("OrgTbl"
  10569. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10570. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10571. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10572. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10573. "--"
  10574. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10575. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10576. ["Copy Field from Above"
  10577. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10578. "--"
  10579. ("Column"
  10580. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10581. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10582. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10583. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"]
  10584. "--"
  10585. ["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])
  10586. ("Row"
  10587. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10588. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10589. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10590. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10591. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10592. "--"
  10593. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10594. ("Rectangle"
  10595. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10596. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10597. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10598. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10599. "--"
  10600. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10601. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10602. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10603. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10604. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10605. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10606. ["Sum Column/Rectangle" org-table-sum
  10607. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10608. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10609. ["Debug Formulas"
  10610. (setq org-table-formula-debug (not org-table-formula-debug))
  10611. :style toggle :selected org-table-formula-debug]
  10612. ))
  10613. t)
  10614. (defun orgtbl-tab (arg)
  10615. "Justification and field motion for `orgtbl-mode'."
  10616. (interactive "P")
  10617. (if arg (org-table-edit-field t)
  10618. (org-table-justify-field-maybe)
  10619. (org-table-next-field)))
  10620. (defun orgtbl-ret ()
  10621. "Justification and field motion for `orgtbl-mode'."
  10622. (interactive)
  10623. (org-table-justify-field-maybe)
  10624. (org-table-next-row))
  10625. (defun orgtbl-self-insert-command (N)
  10626. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10627. If the cursor is in a table looking at whitespace, the whitespace is
  10628. overwritten, and the table is not marked as requiring realignment."
  10629. (interactive "p")
  10630. (if (and (org-at-table-p)
  10631. (or
  10632. (and org-table-auto-blank-field
  10633. (member last-command
  10634. '(orgtbl-hijacker-command-100
  10635. orgtbl-hijacker-command-101
  10636. orgtbl-hijacker-command-102
  10637. orgtbl-hijacker-command-103
  10638. orgtbl-hijacker-command-104
  10639. orgtbl-hijacker-command-105))
  10640. (org-table-blank-field))
  10641. t)
  10642. (eq N 1)
  10643. (looking-at "[^|\n]* +|"))
  10644. (let (org-table-may-need-update)
  10645. (goto-char (1- (match-end 0)))
  10646. (delete-backward-char 1)
  10647. (goto-char (match-beginning 0))
  10648. (self-insert-command N))
  10649. (setq org-table-may-need-update t)
  10650. (let (orgtbl-mode)
  10651. (call-interactively (key-binding (vector last-input-event))))))
  10652. (defun org-force-self-insert (N)
  10653. "Needed to enforce self-insert under remapping."
  10654. (interactive "p")
  10655. (self-insert-command N))
  10656. ;;; Exporting
  10657. (defconst org-level-max 20)
  10658. (defvar org-export-html-preamble nil
  10659. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  10660. (defvar org-export-html-postamble nil
  10661. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  10662. (defvar org-export-html-auto-preamble t
  10663. "Should default preamble be inserted? Set by publishing functions.")
  10664. (defvar org-export-html-auto-postamble t
  10665. "Should default postamble be inserted? Set by publishing functions.")
  10666. (defconst org-export-plist-vars
  10667. '((:language . org-export-default-language)
  10668. (:headline-levels . org-export-headline-levels)
  10669. (:section-numbers . org-export-with-section-numbers)
  10670. (:table-of-contents . org-export-with-toc)
  10671. (:emphasize . org-export-with-emphasize)
  10672. (:sub-superscript . org-export-with-sub-superscripts)
  10673. (:TeX-macros . org-export-with-TeX-macros)
  10674. (:fixed-width . org-export-with-fixed-width)
  10675. (:timestamps . org-export-with-timestamps)
  10676. (:tables . org-export-with-tables)
  10677. (:table-auto-headline . org-export-highlight-first-table-line)
  10678. (:style . org-export-html-style)
  10679. (:convert-org-links . org-export-html-link-org-files-as-html)
  10680. (:inline-images . org-export-html-inline-images)
  10681. (:expand-quoted-html . org-export-html-expand)
  10682. (:timestamp . org-export-html-with-timestamp)
  10683. (:publishing-directory . org-export-publishing-directory)
  10684. (:preamble . org-export-html-preamble)
  10685. (:postamble . org-export-html-postamble)
  10686. (:auto-preamble . org-export-html-auto-preamble)
  10687. (:auto-postamble . org-export-html-auto-postamble)
  10688. (:author . user-full-name)
  10689. (:email . user-mail-address)))
  10690. (defun org-default-export-plist ()
  10691. "Return the property list with default settings for the export variables."
  10692. (let ((l org-export-plist-vars) rtn e)
  10693. (while (setq e (pop l))
  10694. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  10695. rtn))
  10696. (defun org-infile-export-plist ()
  10697. "Return the property list with file-local settings for export."
  10698. (save-excursion
  10699. (goto-char 0)
  10700. (let ((re (org-make-options-regexp
  10701. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  10702. (text nil)
  10703. p key val text options)
  10704. (while (re-search-forward re nil t)
  10705. (setq key (org-match-string-no-properties 1)
  10706. val (org-match-string-no-properties 2))
  10707. (cond
  10708. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  10709. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  10710. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  10711. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  10712. ((string-equal key "TEXT")
  10713. (setq text (if text (concat text "\n" val) val)))
  10714. ((string-equal key "OPTIONS") (setq options val))))
  10715. (setq p (plist-put p :text text))
  10716. (when options
  10717. (let ((op '(("H" . :headline-levels)
  10718. ("num" . :section-numbers)
  10719. ("toc" . :table-of-contents)
  10720. ("\\n" . :preserve-breaks)
  10721. ("@" . :expand-quoted-html)
  10722. (":" . :fixed-width)
  10723. ("|" . :tables)
  10724. ("^" . :sub-superscript)
  10725. ("*" . :emphasize)
  10726. ("TeX" . :TeX-macros)))
  10727. o)
  10728. (while (setq o (pop op))
  10729. (if (string-match (concat (regexp-quote (car o))
  10730. ":\\([^ \t\n\r;,.]*\\)")
  10731. options)
  10732. (setq p (plist-put p (cdr o)
  10733. (car (read-from-string
  10734. (match-string 1 options)))))))))
  10735. p)))
  10736. (defun org-combine-plists (&rest plists)
  10737. "Create a single property list from all plists in PLISTS.
  10738. The process starts by copying the last list, and then setting properties
  10739. from the other lists. Settings in the first list are the most significant
  10740. ones and overrule settings in the other lists."
  10741. (let ((rtn (copy-sequence (pop plists)))
  10742. p v ls)
  10743. (while plists
  10744. (setq ls (pop plists))
  10745. (while ls
  10746. (setq p (pop ls) v (pop ls))
  10747. (setq rtn (plist-put rtn p v))))
  10748. rtn))
  10749. (defun org-export-directory (type plist)
  10750. (let* ((val (plist-get plist :publishing-directory))
  10751. (dir (if (listp val)
  10752. (or (cdr (assoc type val)) ".")
  10753. val)))
  10754. dir))
  10755. (defun org-export-find-first-heading-line (list)
  10756. "Remove all lines from LIST which are before the first headline."
  10757. (let ((orig-list list)
  10758. (re (concat "^" outline-regexp)))
  10759. (while (and list
  10760. (not (string-match re (car list))))
  10761. (pop list))
  10762. (or list orig-list)))
  10763. (defun org-skip-comments (lines)
  10764. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  10765. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  10766. (re2 "^\\(\\*+\\)[ \t\n\r]")
  10767. rtn line level)
  10768. (while (setq line (pop lines))
  10769. (cond
  10770. ((and (string-match re1 line)
  10771. (setq level (- (match-end 1) (match-beginning 1))))
  10772. ;; Beginning of a COMMENT subtree. Skip it.
  10773. (while (and (setq line (pop lines))
  10774. (or (not (string-match re2 line))
  10775. (> (- (match-end 1) (match-beginning 1)) level))))
  10776. (setq lines (cons line lines)))
  10777. ((string-match "^#" line)
  10778. ;; an ordinary comment line
  10779. )
  10780. ((and org-export-table-remove-special-lines
  10781. (string-match "^[ \t]*| *[!_^] *|" line))
  10782. ;; a special table line that should be removed
  10783. )
  10784. (t (setq rtn (cons line rtn)))))
  10785. (nreverse rtn)))
  10786. ;; ASCII
  10787. (defconst org-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  10788. "Characters for underlining headings in ASCII export.")
  10789. (defconst org-html-entities
  10790. '(("nbsp")
  10791. ("iexcl")
  10792. ("cent")
  10793. ("pound")
  10794. ("curren")
  10795. ("yen")
  10796. ("brvbar")
  10797. ("sect")
  10798. ("uml")
  10799. ("copy")
  10800. ("ordf")
  10801. ("laquo")
  10802. ("not")
  10803. ("shy")
  10804. ("reg")
  10805. ("macr")
  10806. ("deg")
  10807. ("plusmn")
  10808. ("sup2")
  10809. ("sup3")
  10810. ("acute")
  10811. ("micro")
  10812. ("para")
  10813. ("middot")
  10814. ("odot"."o")
  10815. ("star"."*")
  10816. ("cedil")
  10817. ("sup1")
  10818. ("ordm")
  10819. ("raquo")
  10820. ("frac14")
  10821. ("frac12")
  10822. ("frac34")
  10823. ("iquest")
  10824. ("Agrave")
  10825. ("Aacute")
  10826. ("Acirc")
  10827. ("Atilde")
  10828. ("Auml")
  10829. ("Aring") ("AA"."&Aring;")
  10830. ("AElig")
  10831. ("Ccedil")
  10832. ("Egrave")
  10833. ("Eacute")
  10834. ("Ecirc")
  10835. ("Euml")
  10836. ("Igrave")
  10837. ("Iacute")
  10838. ("Icirc")
  10839. ("Iuml")
  10840. ("ETH")
  10841. ("Ntilde")
  10842. ("Ograve")
  10843. ("Oacute")
  10844. ("Ocirc")
  10845. ("Otilde")
  10846. ("Ouml")
  10847. ("times")
  10848. ("Oslash")
  10849. ("Ugrave")
  10850. ("Uacute")
  10851. ("Ucirc")
  10852. ("Uuml")
  10853. ("Yacute")
  10854. ("THORN")
  10855. ("szlig")
  10856. ("agrave")
  10857. ("aacute")
  10858. ("acirc")
  10859. ("atilde")
  10860. ("auml")
  10861. ("aring")
  10862. ("aelig")
  10863. ("ccedil")
  10864. ("egrave")
  10865. ("eacute")
  10866. ("ecirc")
  10867. ("euml")
  10868. ("igrave")
  10869. ("iacute")
  10870. ("icirc")
  10871. ("iuml")
  10872. ("eth")
  10873. ("ntilde")
  10874. ("ograve")
  10875. ("oacute")
  10876. ("ocirc")
  10877. ("otilde")
  10878. ("ouml")
  10879. ("divide")
  10880. ("oslash")
  10881. ("ugrave")
  10882. ("uacute")
  10883. ("ucirc")
  10884. ("uuml")
  10885. ("yacute")
  10886. ("thorn")
  10887. ("yuml")
  10888. ("fnof")
  10889. ("Alpha")
  10890. ("Beta")
  10891. ("Gamma")
  10892. ("Delta")
  10893. ("Epsilon")
  10894. ("Zeta")
  10895. ("Eta")
  10896. ("Theta")
  10897. ("Iota")
  10898. ("Kappa")
  10899. ("Lambda")
  10900. ("Mu")
  10901. ("Nu")
  10902. ("Xi")
  10903. ("Omicron")
  10904. ("Pi")
  10905. ("Rho")
  10906. ("Sigma")
  10907. ("Tau")
  10908. ("Upsilon")
  10909. ("Phi")
  10910. ("Chi")
  10911. ("Psi")
  10912. ("Omega")
  10913. ("alpha")
  10914. ("beta")
  10915. ("gamma")
  10916. ("delta")
  10917. ("epsilon")
  10918. ("varepsilon"."&epsilon;")
  10919. ("zeta")
  10920. ("eta")
  10921. ("theta")
  10922. ("iota")
  10923. ("kappa")
  10924. ("lambda")
  10925. ("mu")
  10926. ("nu")
  10927. ("xi")
  10928. ("omicron")
  10929. ("pi")
  10930. ("rho")
  10931. ("sigmaf") ("varsigma"."&sigmaf;")
  10932. ("sigma")
  10933. ("tau")
  10934. ("upsilon")
  10935. ("phi")
  10936. ("chi")
  10937. ("psi")
  10938. ("omega")
  10939. ("thetasym") ("vartheta"."&thetasym;")
  10940. ("upsih")
  10941. ("piv")
  10942. ("bull") ("bullet"."&bull;")
  10943. ("hellip") ("dots"."&hellip;")
  10944. ("prime")
  10945. ("Prime")
  10946. ("oline")
  10947. ("frasl")
  10948. ("weierp")
  10949. ("image")
  10950. ("real")
  10951. ("trade")
  10952. ("alefsym")
  10953. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  10954. ("uarr") ("uparrow"."&uarr;")
  10955. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  10956. ("darr")("downarrow"."&darr;")
  10957. ("harr") ("leftrightarrow"."&harr;")
  10958. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  10959. ("lArr") ("Leftarrow"."&lArr;")
  10960. ("uArr") ("Uparrow"."&uArr;")
  10961. ("rArr") ("Rightarrow"."&rArr;")
  10962. ("dArr") ("Downarrow"."&dArr;")
  10963. ("hArr") ("Leftrightarrow"."&hArr;")
  10964. ("forall")
  10965. ("part") ("partial"."&part;")
  10966. ("exist") ("exists"."&exist;")
  10967. ("empty") ("emptyset"."&empty;")
  10968. ("nabla")
  10969. ("isin") ("in"."&isin;")
  10970. ("notin")
  10971. ("ni")
  10972. ("prod")
  10973. ("sum")
  10974. ("minus")
  10975. ("lowast") ("ast"."&lowast;")
  10976. ("radic")
  10977. ("prop") ("proptp"."&prop;")
  10978. ("infin") ("infty"."&infin;")
  10979. ("ang") ("angle"."&ang;")
  10980. ("and") ("vee"."&and;")
  10981. ("or") ("wedge"."&or;")
  10982. ("cap")
  10983. ("cup")
  10984. ("int")
  10985. ("there4")
  10986. ("sim")
  10987. ("cong") ("simeq"."&cong;")
  10988. ("asymp")("approx"."&asymp;")
  10989. ("ne") ("neq"."&ne;")
  10990. ("equiv")
  10991. ("le")
  10992. ("ge")
  10993. ("sub") ("subset"."&sub;")
  10994. ("sup") ("supset"."&sup;")
  10995. ("nsub")
  10996. ("sube")
  10997. ("supe")
  10998. ("oplus")
  10999. ("otimes")
  11000. ("perp")
  11001. ("sdot") ("cdot"."&sdot;")
  11002. ("lceil")
  11003. ("rceil")
  11004. ("lfloor")
  11005. ("rfloor")
  11006. ("lang")
  11007. ("rang")
  11008. ("loz") ("Diamond"."&loz;")
  11009. ("spades") ("spadesuit"."&spades;")
  11010. ("clubs") ("clubsuit"."&clubs;")
  11011. ("hearts") ("diamondsuit"."&hearts;")
  11012. ("diams") ("diamondsuit"."&diams;")
  11013. ("quot")
  11014. ("amp")
  11015. ("lt")
  11016. ("gt")
  11017. ("OElig")
  11018. ("oelig")
  11019. ("Scaron")
  11020. ("scaron")
  11021. ("Yuml")
  11022. ("circ")
  11023. ("tilde")
  11024. ("ensp")
  11025. ("emsp")
  11026. ("thinsp")
  11027. ("zwnj")
  11028. ("zwj")
  11029. ("lrm")
  11030. ("rlm")
  11031. ("ndash")
  11032. ("mdash")
  11033. ("lsquo")
  11034. ("rsquo")
  11035. ("sbquo")
  11036. ("ldquo")
  11037. ("rdquo")
  11038. ("bdquo")
  11039. ("dagger")
  11040. ("Dagger")
  11041. ("permil")
  11042. ("lsaquo")
  11043. ("rsaquo")
  11044. ("euro")
  11045. ("arccos"."arccos")
  11046. ("arcsin"."arcsin")
  11047. ("arctan"."arctan")
  11048. ("arg"."arg")
  11049. ("cos"."cos")
  11050. ("cosh"."cosh")
  11051. ("cot"."cot")
  11052. ("coth"."coth")
  11053. ("csc"."csc")
  11054. ("deg"."deg")
  11055. ("det"."det")
  11056. ("dim"."dim")
  11057. ("exp"."exp")
  11058. ("gcd"."gcd")
  11059. ("hom"."hom")
  11060. ("inf"."inf")
  11061. ("ker"."ker")
  11062. ("lg"."lg")
  11063. ("lim"."lim")
  11064. ("liminf"."liminf")
  11065. ("limsup"."limsup")
  11066. ("ln"."ln")
  11067. ("log"."log")
  11068. ("max"."max")
  11069. ("min"."min")
  11070. ("Pr"."Pr")
  11071. ("sec"."sec")
  11072. ("sin"."sin")
  11073. ("sinh"."sinh")
  11074. ("sup"."sup")
  11075. ("tan"."tan")
  11076. ("tanh"."tanh")
  11077. )
  11078. "Entities for TeX->HTML translation.
  11079. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  11080. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  11081. In that case, \"\\ent\" will be translated to \"&other;\".
  11082. The list contains HTML entities for Latin-1, Greek and other symbols.
  11083. It is supplemented by a number of commonly used TeX macros with appropriate
  11084. translations. There is currently no way for users to extend this.")
  11085. (defun org-cleaned-string-for-export (string)
  11086. "Cleanup a buffer substring so that links can be created safely."
  11087. (interactive)
  11088. (let* ((cb (current-buffer))
  11089. (re-radio (and org-target-link-regexp
  11090. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  11091. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  11092. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  11093. rtn)
  11094. (save-excursion
  11095. (set-buffer (get-buffer-create " org-mode-tmp"))
  11096. (erase-buffer)
  11097. (insert string)
  11098. (org-mode)
  11099. ;; Find targets in comments and move them out of comments,
  11100. ;; but mark them as targets that should be invisible
  11101. (goto-char (point-min))
  11102. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  11103. (replace-match "\\1(INVISIBLE)"))
  11104. ;; Find matches for radio targets and turn them into internal links
  11105. (goto-char (point-min))
  11106. (when re-radio
  11107. (while (re-search-forward re-radio nil t)
  11108. (replace-match "\\1[[\\2]]")))
  11109. ;; Find all links that contain a newline and put them into a single line
  11110. (goto-char (point-min))
  11111. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  11112. (replace-match "\\1 \\3")
  11113. (goto-char (match-beginning 0)))
  11114. ;; Normalize links: Convert angle and plain links into bracket links
  11115. (goto-char (point-min))
  11116. (while (re-search-forward re-plain-link nil t)
  11117. (replace-match
  11118. (concat
  11119. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  11120. t t))
  11121. (goto-char (point-min))
  11122. (while (re-search-forward re-angle-link nil t)
  11123. (replace-match
  11124. (concat
  11125. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  11126. t t))
  11127. ;; Remove comments
  11128. (goto-char (point-min))
  11129. (while (re-search-forward "^#.*\n?" nil t)
  11130. (replace-match ""))
  11131. (setq rtn (buffer-string)))
  11132. (kill-buffer " org-mode-tmp")
  11133. rtn))
  11134. (defun org-solidify-link-text (s &optional alist)
  11135. "Take link text and make a safe target out of it."
  11136. (save-match-data
  11137. (let* ((rtn
  11138. (mapconcat
  11139. 'identity
  11140. (org-split-string s "[ \t\r\n]+") "--"))
  11141. (a (assoc rtn alist)))
  11142. (or (cdr a) rtn))))
  11143. (defun org-convert-to-odd-levels ()
  11144. "Convert an org-mode file with all levels allowed to one with odd levels.
  11145. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  11146. level 5 etc."
  11147. (interactive)
  11148. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  11149. (let ((org-odd-levels-only nil) n)
  11150. (save-excursion
  11151. (goto-char (point-min))
  11152. (while (re-search-forward "^\\*\\*+" nil t)
  11153. (setq n (1- (length (match-string 0))))
  11154. (while (>= (setq n (1- n)) 0)
  11155. (org-demote))
  11156. (end-of-line 1))))))
  11157. (defun org-convert-to-oddeven-levels ()
  11158. "Convert an org-mode file with only odd levels to one with odd and even levels.
  11159. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  11160. section with an even level, conversion would destroy the structure of the file. An error
  11161. is signaled in this case."
  11162. (interactive)
  11163. (goto-char (point-min))
  11164. ;; First check if there are no even levels
  11165. (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
  11166. (org-show-hierarchy-above)
  11167. (error "Not all levels are odd in this file. Conversion not possible."))
  11168. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  11169. (let ((org-odd-levels-only nil) n)
  11170. (save-excursion
  11171. (goto-char (point-min))
  11172. (while (re-search-forward "^\\*\\*+" nil t)
  11173. (setq n (/ (length (match-string 0)) 2))
  11174. (while (>= (setq n (1- n)) 0)
  11175. (org-promote))
  11176. (end-of-line 1))))))
  11177. (defun org-tr-level (n)
  11178. "Make N odd if required."
  11179. (if org-odd-levels-only (1+ (/ n 2)) n))
  11180. (defvar org-last-level nil) ; dynamically scoped variable
  11181. (defun org-export-as-ascii (arg)
  11182. "Export the outline as a pretty ASCII file.
  11183. If there is an active region, export only the region.
  11184. The prefix ARG specifies how many levels of the outline should become
  11185. underlined headlines. The default is 3."
  11186. (interactive "P")
  11187. (setq-default org-todo-line-regexp org-todo-line-regexp)
  11188. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  11189. (org-infile-export-plist)))
  11190. (region
  11191. (buffer-substring
  11192. (if (org-region-active-p) (region-beginning) (point-min))
  11193. (if (org-region-active-p) (region-end) (point-max))))
  11194. (lines (org-export-find-first-heading-line
  11195. (org-skip-comments
  11196. (org-split-string
  11197. (org-cleaned-string-for-export region)
  11198. "[\r\n]"))))
  11199. (org-startup-with-deadline-check nil)
  11200. (level 0) line txt
  11201. (umax nil)
  11202. (case-fold-search nil)
  11203. (filename (concat (file-name-as-directory
  11204. (org-export-directory :ascii opt-plist))
  11205. (file-name-sans-extension
  11206. (file-name-nondirectory buffer-file-name))
  11207. ".txt"))
  11208. (buffer (find-file-noselect filename))
  11209. (levels-open (make-vector org-level-max nil))
  11210. (odd org-odd-levels-only)
  11211. (date (format-time-string "%Y/%m/%d" (current-time)))
  11212. (time (format-time-string "%X" (org-current-time)))
  11213. (author (plist-get opt-plist :author))
  11214. (title (or (plist-get opt-plist :title)
  11215. (file-name-sans-extension
  11216. (file-name-nondirectory buffer-file-name))))
  11217. (options nil)
  11218. (email (plist-get opt-plist :email))
  11219. (language (plist-get opt-plist :language))
  11220. (text nil)
  11221. (todo nil)
  11222. (lang-words nil))
  11223. (setq org-last-level 1)
  11224. (org-init-section-numbers)
  11225. (find-file-noselect filename)
  11226. (setq lang-words (or (assoc language org-export-language-setup)
  11227. (assoc "en" org-export-language-setup)))
  11228. (if org-export-ascii-show-new-buffer
  11229. (switch-to-buffer-other-window buffer)
  11230. (set-buffer buffer))
  11231. (erase-buffer)
  11232. (fundamental-mode)
  11233. ;; create local variables for all options, to make sure all called
  11234. ;; functions get the correct information
  11235. (mapcar (lambda (x)
  11236. (set (make-local-variable (cdr x))
  11237. (plist-get opt-plist (car x))))
  11238. org-export-plist-vars)
  11239. (set (make-local-variable 'org-odd-levels-only) odd)
  11240. (setq umax (if arg (prefix-numeric-value arg)
  11241. org-export-headline-levels))
  11242. ;; File header
  11243. (if title (org-insert-centered title ?=))
  11244. (insert "\n")
  11245. (if (or author email)
  11246. (insert (concat (nth 1 lang-words) ": " (or author "")
  11247. (if email (concat " <" email ">") "")
  11248. "\n")))
  11249. (if (and date time)
  11250. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  11251. (if text (insert (concat (org-html-expand-for-ascii text) "\n\n")))
  11252. (insert "\n\n")
  11253. (if org-export-with-toc
  11254. (progn
  11255. (insert (nth 3 lang-words) "\n"
  11256. (make-string (length (nth 3 lang-words)) ?=) "\n")
  11257. (mapcar '(lambda (line)
  11258. (if (string-match org-todo-line-regexp
  11259. line)
  11260. ;; This is a headline
  11261. (progn
  11262. (setq level (- (match-end 1) (match-beginning 1))
  11263. level (org-tr-level level)
  11264. txt (match-string 3 line)
  11265. todo
  11266. (or (and org-export-mark-todo-in-toc
  11267. (match-beginning 2)
  11268. (not (equal (match-string 2 line)
  11269. org-done-string)))
  11270. ; TODO, not DONE
  11271. (and org-export-mark-todo-in-toc
  11272. (= level umax)
  11273. (org-search-todo-below
  11274. line lines level))))
  11275. (setq txt (org-html-expand-for-ascii txt))
  11276. (if org-export-with-section-numbers
  11277. (setq txt (concat (org-section-number level)
  11278. " " txt)))
  11279. (if (<= level umax)
  11280. (progn
  11281. (insert
  11282. (make-string (* (1- level) 4) ?\ )
  11283. (format (if todo "%s (*)\n" "%s\n") txt))
  11284. (setq org-last-level level))
  11285. ))))
  11286. lines)))
  11287. (org-init-section-numbers)
  11288. (while (setq line (pop lines))
  11289. ;; Remove the quoted HTML tags.
  11290. (setq line (org-html-expand-for-ascii line))
  11291. ;; Remove targets
  11292. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  11293. (setq line (replace-match "" t t line)))
  11294. ;; Replace internal links
  11295. (while (string-match org-bracket-link-regexp line)
  11296. (setq line (replace-match
  11297. (if (match-end 3) "[\\3]" "[\\1]")
  11298. t nil line)))
  11299. (cond
  11300. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  11301. ;; a Headline
  11302. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  11303. txt (match-string 2 line))
  11304. (org-ascii-level-start level txt umax))
  11305. (t (insert line "\n"))))
  11306. (normal-mode)
  11307. (save-buffer)
  11308. ;; remove display and invisible chars
  11309. (let (beg end s)
  11310. (goto-char (point-min))
  11311. (while (setq beg (next-single-property-change (point) 'display))
  11312. (setq end (next-single-property-change beg 'display))
  11313. (delete-region beg end)
  11314. (goto-char beg)
  11315. (insert "=>"))
  11316. (goto-char (point-min))
  11317. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  11318. (setq end (next-single-property-change beg 'org-cwidth))
  11319. (delete-region beg end)
  11320. (goto-char beg)))
  11321. (goto-char (point-min))))
  11322. (defun org-search-todo-below (line lines level)
  11323. "Search the subtree below LINE for any TODO entries."
  11324. (let ((rest (cdr (memq line lines)))
  11325. (re org-todo-line-regexp)
  11326. line lv todo)
  11327. (catch 'exit
  11328. (while (setq line (pop rest))
  11329. (if (string-match re line)
  11330. (progn
  11331. (setq lv (- (match-end 1) (match-beginning 1))
  11332. todo (and (match-beginning 2)
  11333. (not (equal (match-string 2 line)
  11334. org-done-string))))
  11335. ; TODO, not DONE
  11336. (if (<= lv level) (throw 'exit nil))
  11337. (if todo (throw 'exit t))))))))
  11338. ;; FIXME: Try to handle <b> and <i> as faces via text properties.
  11339. ;; We could also implement *bold*,/italic/ and _underline_ for ASCII export
  11340. (defun org-html-expand-for-ascii (line)
  11341. "Handle quoted HTML for ASCII export."
  11342. (if org-export-html-expand
  11343. (while (string-match "@<[^<>\n]*>" line)
  11344. ;; We just remove the tags for now.
  11345. (setq line (replace-match "" nil nil line))))
  11346. line)
  11347. (defun org-insert-centered (s &optional underline)
  11348. "Insert the string S centered and underline it with character UNDERLINE."
  11349. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  11350. (insert (make-string ind ?\ ) s "\n")
  11351. (if underline
  11352. (insert (make-string ind ?\ )
  11353. (make-string (string-width s) underline)
  11354. "\n"))))
  11355. (defun org-ascii-level-start (level title umax)
  11356. "Insert a new level in ASCII export."
  11357. (let (char)
  11358. (if (> level umax)
  11359. (insert (make-string (* 2 (- level umax 1)) ?\ ) "* " title "\n")
  11360. (if (or (not (equal (char-before) ?\n))
  11361. (not (equal (char-before (1- (point))) ?\n)))
  11362. (insert "\n"))
  11363. (setq char (nth (- umax level) (reverse org-ascii-underline)))
  11364. (if org-export-with-section-numbers
  11365. (setq title (concat (org-section-number level) " " title)))
  11366. (insert title "\n" (make-string (string-width title) char) "\n"))))
  11367. (defun org-export-visible (type arg)
  11368. "Create a copy of the visible part of the current buffer, and export it.
  11369. The copy is created in a temporary buffer and removed after use.
  11370. TYPE is the final key (as a string) of the `C-c C-x' key sequence that will
  11371. run the export command - in interactive use, the command prompts for this
  11372. key. As a special case, if the you type SPC at the prompt, the temporary
  11373. org-mode file will not be removed but presented to you so that you can
  11374. continue to use it. The prefix arg ARG is passed through to the exporting
  11375. command."
  11376. (interactive
  11377. (list (progn
  11378. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [x]OXO [ ]keep buffer")
  11379. (char-to-string (read-char-exclusive)))
  11380. current-prefix-arg))
  11381. (if (not (member type '("a" "\C-a" "b" "\C-b" "h" "x" " ")))
  11382. (error "Invalid export key"))
  11383. (let* ((binding (key-binding (concat "\C-c\C-x" type)))
  11384. (keepp (equal type " "))
  11385. (file buffer-file-name)
  11386. (buffer (get-buffer-create "*Org Export Visible*"))
  11387. s e)
  11388. (with-current-buffer buffer (erase-buffer))
  11389. (save-excursion
  11390. (setq s (goto-char (point-min)))
  11391. (while (not (= (point) (point-max)))
  11392. (goto-char (org-find-invisible))
  11393. (append-to-buffer buffer s (point))
  11394. (setq s (goto-char (org-find-visible))))
  11395. (goto-char (point-min))
  11396. (unless keepp
  11397. ;; Copy all comment lines to the end, to make sure #+ settings are
  11398. ;; still available for the second export step. Kind of a hack, but
  11399. ;; does do the trick.
  11400. (if (looking-at "#[^\r\n]*")
  11401. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  11402. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  11403. (append-to-buffer buffer (1+ (match-beginning 0))
  11404. (min (point-max) (1+ (match-end 0))))))
  11405. (set-buffer buffer)
  11406. (let ((buffer-file-name file)
  11407. (org-inhibit-startup t))
  11408. (org-mode)
  11409. (show-all)
  11410. (unless keepp (funcall binding arg))))
  11411. (if (not keepp)
  11412. (kill-buffer buffer)
  11413. (switch-to-buffer-other-window buffer)
  11414. (goto-char (point-min)))))
  11415. (defun org-find-visible ()
  11416. (if (featurep 'noutline)
  11417. (let ((s (point)))
  11418. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  11419. (get-char-property s 'invisible)))
  11420. s)
  11421. (skip-chars-forward "^\n")
  11422. (point)))
  11423. (defun org-find-invisible ()
  11424. (if (featurep 'noutline)
  11425. (let ((s (point)))
  11426. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  11427. (not (get-char-property s 'invisible))))
  11428. s)
  11429. (skip-chars-forward "^\r")
  11430. (point)))
  11431. ;; HTML
  11432. (defun org-get-current-options ()
  11433. "Return a string with current options as keyword options.
  11434. Does include HTML export options as well as TODO and CATEGORY stuff."
  11435. (format
  11436. "#+TITLE: %s
  11437. #+AUTHOR: %s
  11438. #+EMAIL: %s
  11439. #+LANGUAGE: %s
  11440. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  11441. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s
  11442. #+CATEGORY: %s
  11443. #+SEQ_TODO: %s
  11444. #+TYP_TODO: %s
  11445. #+STARTUP: %s %s %s %s %s %s
  11446. #+TAGS: %s
  11447. #+ARCHIVE: %s
  11448. "
  11449. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  11450. org-export-headline-levels
  11451. org-export-with-section-numbers
  11452. org-export-with-toc
  11453. org-export-preserve-breaks
  11454. org-export-html-expand
  11455. org-export-with-fixed-width
  11456. org-export-with-tables
  11457. org-export-with-sub-superscripts
  11458. org-export-with-emphasize
  11459. org-export-with-TeX-macros
  11460. (file-name-nondirectory buffer-file-name)
  11461. (if (equal org-todo-interpretation 'sequence)
  11462. (mapconcat 'identity org-todo-keywords " ")
  11463. "TODO FEEDBACK VERIFY DONE")
  11464. (if (equal org-todo-interpretation 'type)
  11465. (mapconcat 'identity org-todo-keywords " ")
  11466. "Me Jason Marie DONE")
  11467. (cdr (assoc org-startup-folded
  11468. '((nil . "showall") (t . "overview") (content . "content"))))
  11469. (if org-startup-with-deadline-check "dlcheck" "nodlcheck")
  11470. (if org-odd-levels-only "odd" "oddeven")
  11471. (if org-hide-leading-stars "hidestars" "showstars")
  11472. (if org-startup-align-all-tables "align" "noalign")
  11473. (if org-log-done "logging" "nologging")
  11474. (if org-tag-alist (mapconcat 'car org-tag-alist " ") "")
  11475. org-archive-location
  11476. ))
  11477. (defun org-insert-export-options-template ()
  11478. "Insert into the buffer a template with information for exporting."
  11479. (interactive)
  11480. (if (not (bolp)) (newline))
  11481. (let ((s (org-get-current-options)))
  11482. (and (string-match "#\\+CATEGORY" s)
  11483. (setq s (substring s 0 (match-beginning 0))))
  11484. (insert s)))
  11485. (defun org-toggle-fixed-width-section (arg)
  11486. "Toggle the fixed-width export.
  11487. If there is no active region, the QUOTE keyword at the current headline is
  11488. inserted or removed. When present, it causes the text between this headline
  11489. and the next to be exported as fixed-width text, and unmodified.
  11490. If there is an active region, this command adds or removes a colon as the
  11491. first character of this line. If the first character of a line is a colon,
  11492. this line is also exported in fixed-width font."
  11493. (interactive "P")
  11494. (let* ((cc 0)
  11495. (regionp (org-region-active-p))
  11496. (beg (if regionp (region-beginning) (point)))
  11497. (end (if regionp (region-end)))
  11498. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  11499. (re "[ \t]*\\(:\\)")
  11500. off)
  11501. (if regionp
  11502. (save-excursion
  11503. (goto-char beg)
  11504. (setq cc (current-column))
  11505. (beginning-of-line 1)
  11506. (setq off (looking-at re))
  11507. (while (> nlines 0)
  11508. (setq nlines (1- nlines))
  11509. (beginning-of-line 1)
  11510. (cond
  11511. (arg
  11512. (move-to-column cc t)
  11513. (insert ":\n")
  11514. (forward-line -1))
  11515. ((and off (looking-at re))
  11516. (replace-match "" t t nil 1))
  11517. ((not off) (move-to-column cc t) (insert ":")))
  11518. (forward-line 1)))
  11519. (save-excursion
  11520. (org-back-to-heading)
  11521. (if (looking-at (concat outline-regexp
  11522. "\\( +\\<" org-quote-string "\\>\\)"))
  11523. (replace-match "" t t nil 1)
  11524. (if (looking-at outline-regexp)
  11525. (progn
  11526. (goto-char (match-end 0))
  11527. (insert " " org-quote-string))))))))
  11528. (defun org-export-as-html-and-open (arg)
  11529. "Export the outline as HTML and immediately open it with a browser.
  11530. If there is an active region, export only the region.
  11531. The prefix ARG specifies how many levels of the outline should become
  11532. headlines. The default is 3. Lower levels will become bulleted lists."
  11533. (interactive "P")
  11534. (org-export-as-html arg 'hidden)
  11535. (org-open-file buffer-file-name))
  11536. (defun org-export-as-html-batch ()
  11537. "Call `org-export-as-html', may be used in batch processing as
  11538. emacs --batch
  11539. --load=$HOME/lib/emacs/org.el
  11540. --eval \"(setq org-export-headline-levels 2)\"
  11541. --visit=MyFile --funcall org-export-as-html-batch"
  11542. (org-export-as-html org-export-headline-levels 'hidden))
  11543. (defun org-export-as-html (arg &optional hidden ext-plist)
  11544. "Export the outline as a pretty HTML file.
  11545. If there is an active region, export only the region.
  11546. The prefix ARG specifies how many levels of the outline should become
  11547. headlines. The default is 3. Lower levels will become bulleted lists.
  11548. When HIDDEN is non-nil, don't display the HTML buffer.
  11549. EXT-PLIST is a property list with external parameters overriding
  11550. org-mode's default settings, but still inferior to file-local settings."
  11551. (interactive "P")
  11552. (setq-default org-todo-line-regexp org-todo-line-regexp)
  11553. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  11554. (setq-default org-done-string org-done-string)
  11555. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  11556. ext-plist
  11557. (org-infile-export-plist)))
  11558. (style (plist-get opt-plist :style))
  11559. (odd org-odd-levels-only)
  11560. (region-p (org-region-active-p))
  11561. (region
  11562. (buffer-substring
  11563. (if region-p (region-beginning) (point-min))
  11564. (if region-p (region-end) (point-max))))
  11565. (all_lines
  11566. (org-skip-comments (org-split-string
  11567. (org-cleaned-string-for-export region)
  11568. "[\r\n]")))
  11569. (lines (org-export-find-first-heading-line all_lines))
  11570. (level 0) (line "") (origline "") txt todo
  11571. (umax nil)
  11572. (filename (concat (file-name-as-directory
  11573. (org-export-directory :html opt-plist))
  11574. (file-name-sans-extension
  11575. (file-name-nondirectory buffer-file-name))
  11576. ".html"))
  11577. (buffer (find-file-noselect filename))
  11578. (levels-open (make-vector org-level-max nil))
  11579. (date (format-time-string "%Y/%m/%d" (current-time)))
  11580. (time (format-time-string "%X" (org-current-time)))
  11581. (author (plist-get opt-plist :author))
  11582. (title (or (plist-get opt-plist :title)
  11583. (file-name-sans-extension
  11584. (file-name-nondirectory buffer-file-name))))
  11585. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  11586. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  11587. (inquote nil)
  11588. (infixed nil)
  11589. (in-local-list nil)
  11590. (local-list-num nil)
  11591. (local-list-indent nil)
  11592. (llt org-plain-list-ordered-item-terminator)
  11593. (email (plist-get opt-plist :email))
  11594. (language (plist-get opt-plist :language))
  11595. (text (plist-get opt-plist :text))
  11596. (lang-words nil)
  11597. (target-alist nil) tg
  11598. (head-count 0) cnt
  11599. (start 0)
  11600. ;; FIXME: The following returns always nil under XEmacs
  11601. (coding-system (and (fboundp 'coding-system-get)
  11602. (boundp 'buffer-file-coding-system)
  11603. buffer-file-coding-system))
  11604. (coding-system-for-write (or coding-system coding-system-for-write))
  11605. (save-buffer-coding-system (or coding-system save-buffer-coding-system))
  11606. (charset (and coding-system
  11607. (coding-system-get coding-system 'mime-charset)))
  11608. table-open type
  11609. table-buffer table-orig-buffer
  11610. ind start-is-num starter
  11611. rpl path desc descp desc1 desc2 link
  11612. )
  11613. (message "Exporting...")
  11614. (setq org-last-level 1)
  11615. (org-init-section-numbers)
  11616. ;; Get the language-dependent settings
  11617. (setq lang-words (or (assoc language org-export-language-setup)
  11618. (assoc "en" org-export-language-setup)))
  11619. ;; Switch to the output buffer
  11620. (if (or hidden (not org-export-html-show-new-buffer))
  11621. (set-buffer buffer)
  11622. (switch-to-buffer-other-window buffer))
  11623. (erase-buffer)
  11624. (fundamental-mode)
  11625. (let ((case-fold-search nil)
  11626. (org-odd-levels-only odd))
  11627. ;; create local variables for all options, to make sure all called
  11628. ;; functions get the correct information
  11629. (mapcar (lambda (x)
  11630. (set (make-local-variable (cdr x))
  11631. (plist-get opt-plist (car x))))
  11632. org-export-plist-vars)
  11633. (setq umax (if arg (prefix-numeric-value arg)
  11634. org-export-headline-levels))
  11635. ;; File header
  11636. (insert (format
  11637. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"
  11638. \"http://www.w3.org/TR/REC-html40/loose.dtd\">
  11639. <html lang=\"%s\"><head>
  11640. <title>%s</title>
  11641. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\">
  11642. <meta name=generator content=\"Org-mode\">
  11643. <meta name=generated content=\"%s %s\">
  11644. <meta name=author content=\"%s\">
  11645. %s
  11646. </head><body>
  11647. "
  11648. language (org-html-expand title) (or charset "iso-8859-1")
  11649. date time author style))
  11650. (insert (or (plist-get opt-plist :preamble) ""))
  11651. (when (plist-get opt-plist :auto-preamble)
  11652. (if title (insert (concat "<H1 class=\"title\">"
  11653. (org-html-expand title) "</H1>\n")))
  11654. ; (if author (insert (concat (nth 1 lang-words) ": " author "\n")))
  11655. ; (if email (insert (concat "<a href=\"mailto:" email "\">&lt;"
  11656. ; email "&gt;</a>\n")))
  11657. ; (if (or author email) (insert "<br>\n"))
  11658. ; (if (and date time) (insert (concat (nth 2 lang-words) ": "
  11659. ; date " " time "<br>\n")))
  11660. (if text (insert (concat "<p>\n" (org-html-expand text)))))
  11661. (if org-export-with-toc
  11662. (progn
  11663. (insert (format "<H2>%s</H2>\n" (nth 3 lang-words)))
  11664. (insert "<ul>\n")
  11665. (setq lines
  11666. (mapcar '(lambda (line)
  11667. (if (string-match org-todo-line-regexp line)
  11668. ;; This is a headline
  11669. (progn
  11670. (setq level (- (match-end 1) (match-beginning 1))
  11671. level (org-tr-level level)
  11672. txt (save-match-data
  11673. (org-html-expand
  11674. (org-html-cleanup-toc-line
  11675. (match-string 3 line))))
  11676. todo
  11677. (or (and org-export-mark-todo-in-toc
  11678. (match-beginning 2)
  11679. (not (equal (match-string 2 line)
  11680. org-done-string)))
  11681. ; TODO, not DONE
  11682. (and org-export-mark-todo-in-toc
  11683. (= level umax)
  11684. (org-search-todo-below
  11685. line lines level))))
  11686. (if org-export-with-section-numbers
  11687. (setq txt (concat (org-section-number level)
  11688. " " txt)))
  11689. (if (<= level umax)
  11690. (progn
  11691. (setq head-count (+ head-count 1))
  11692. (if (> level org-last-level)
  11693. (progn
  11694. (setq cnt (- level org-last-level))
  11695. (while (>= (setq cnt (1- cnt)) 0)
  11696. (insert "<ul>"))
  11697. (insert "\n")))
  11698. (if (< level org-last-level)
  11699. (progn
  11700. (setq cnt (- org-last-level level))
  11701. (while (>= (setq cnt (1- cnt)) 0)
  11702. (insert "</ul>"))
  11703. (insert "\n")))
  11704. ;; Check for targets
  11705. (while (string-match org-target-regexp line)
  11706. (setq tg (match-string 1 line)
  11707. line (replace-match
  11708. (concat "@<span class=\"target\">" tg "@</span> ")
  11709. t t line))
  11710. (push (cons (org-solidify-link-text tg)
  11711. (format "sec-%d" head-count))
  11712. target-alist))
  11713. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  11714. (setq txt (replace-match "" t t txt)))
  11715. (insert
  11716. (format
  11717. (if todo
  11718. "<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>\n"
  11719. "<li><a href=\"#sec-%d\">%s</a>\n")
  11720. head-count txt))
  11721. (setq org-last-level level))
  11722. )))
  11723. line)
  11724. lines))
  11725. (while (> org-last-level 0)
  11726. (setq org-last-level (1- org-last-level))
  11727. (insert "</ul>\n"))
  11728. ))
  11729. (setq head-count 0)
  11730. (org-init-section-numbers)
  11731. (while (setq line (pop lines) origline line)
  11732. (catch 'nextline
  11733. ;; end of quote section?
  11734. (when (and inquote (string-match "^\\*+" line))
  11735. (insert "</pre>\n")
  11736. (setq inquote nil))
  11737. ;; inside a quote section?
  11738. (when inquote
  11739. (insert (org-html-protect line) "\n")
  11740. (throw 'nextline nil))
  11741. ;; verbatim lines
  11742. (when (and org-export-with-fixed-width
  11743. (string-match "^[ \t]*:\\(.*\\)" line))
  11744. (when (not infixed)
  11745. (setq infixed t)
  11746. (insert "<pre>\n"))
  11747. (insert (org-html-protect (match-string 1 line)) "\n")
  11748. (when (and lines
  11749. (not (string-match "^[ \t]*\\(:.*\\)"
  11750. (car lines))))
  11751. (setq infixed nil)
  11752. (insert "</pre>\n"))
  11753. (throw 'nextline nil))
  11754. ;; make targets to anchors
  11755. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  11756. (cond
  11757. ((match-end 2)
  11758. (setq line (replace-match
  11759. (concat "@<a name=\""
  11760. (org-solidify-link-text (match-string 1 line))
  11761. "\">\\nbsp@</a>")
  11762. t t line)))
  11763. ((and org-export-with-toc (equal (string-to-char line) ?*))
  11764. (setq line (replace-match
  11765. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  11766. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  11767. t t line)))
  11768. (t
  11769. (setq line (replace-match
  11770. (concat "@<a name=\""
  11771. (org-solidify-link-text (match-string 1 line))
  11772. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  11773. t t line)))))
  11774. (setq line (org-html-handle-time-stamps line))
  11775. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  11776. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  11777. ;; Also handle sub_superscripts and checkboxes
  11778. ;; FIXME: is there no better place for checkboxes
  11779. (setq line (org-html-expand line))
  11780. ;; Format the links
  11781. (setq start 0)
  11782. (while (string-match org-bracket-link-analytic-regexp line start)
  11783. (setq start (match-beginning 0))
  11784. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  11785. (setq path (match-string 3 line))
  11786. (setq desc1 (if (match-end 5) (match-string 5 line))
  11787. desc2 (if (match-end 2) (concat type ":" path) path)
  11788. descp (and desc1 (not (equal desc1 desc2)))
  11789. desc (or desc1 desc2))
  11790. ;; FIXME: do we need to unescape here somewhere?
  11791. (cond
  11792. ((equal type "internal")
  11793. (setq rpl
  11794. (concat
  11795. "<a href=\"#"
  11796. (org-solidify-link-text path target-alist)
  11797. "\">" desc "</a>")))
  11798. ((member type '("http" "https" "ftp" "mailto" "news"))
  11799. ;; standard URL
  11800. (setq link (concat type ":" path))
  11801. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  11802. ((string= type "file")
  11803. ;; FILE link
  11804. (let* ((filename path)
  11805. (abs-p (file-name-absolute-p filename))
  11806. thefile file-is-image-p search)
  11807. (save-match-data
  11808. (if (string-match "::\\(.*\\)" filename)
  11809. (setq search (match-string 1 filename)
  11810. filename (replace-match "" nil nil filename)))
  11811. (setq file-is-image-p
  11812. (string-match (org-image-file-name-regexp) filename))
  11813. (setq thefile (if abs-p (expand-file-name filename) filename))
  11814. (when (and org-export-html-link-org-files-as-html
  11815. (string-match "\\.org$" thefile))
  11816. (setq thefile (concat (substring thefile 0
  11817. (match-beginning 0))
  11818. ".html"))
  11819. (if (and search
  11820. ;; make sure this is can be used as target search
  11821. (not (string-match "^[0-9]*$" search))
  11822. (not (string-match "^\\*" search))
  11823. (not (string-match "^/.*/$" search)))
  11824. (setq thefile (concat thefile "#"
  11825. (org-solidify-link-text
  11826. (org-link-unescape search)))))
  11827. (when (string-match "^file:" desc)
  11828. (setq desc (replace-match "" t t desc))
  11829. (if (string-match "\\.org$" desc)
  11830. (setq desc (replace-match "" t t desc))))))
  11831. (setq rpl (if (and file-is-image-p
  11832. (or (eq t org-export-html-inline-images)
  11833. (and org-export-html-inline-images
  11834. (not descp))))
  11835. (concat "<img src=\"" thefile "\"/>")
  11836. (concat "<a href=\"" thefile "\">" desc "</a>")))))
  11837. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  11838. (setq rpl (concat "<i>&lt;" type ":"
  11839. (save-match-data (org-link-unescape path))
  11840. "&gt;</i>"))))
  11841. (setq line (replace-match rpl t t line)
  11842. start (+ start (length rpl))))
  11843. ;; TODO items
  11844. (if (and (string-match org-todo-line-regexp line)
  11845. (match-beginning 2))
  11846. (if (equal (match-string 2 line) org-done-string)
  11847. (setq line (replace-match
  11848. "<span class=\"done\">\\2</span>"
  11849. nil nil line 2))
  11850. (setq line (replace-match "<span class=\"todo\">\\2</span>"
  11851. nil nil line 2))))
  11852. (cond
  11853. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  11854. ;; This is a headline
  11855. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  11856. txt (match-string 2 line))
  11857. (if (string-match quote-re0 txt)
  11858. (setq txt (replace-match "" t t txt)))
  11859. (if (<= level umax) (setq head-count (+ head-count 1)))
  11860. (when in-local-list
  11861. ;; Close any local lists before inserting a new header line
  11862. (while local-list-num
  11863. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  11864. (pop local-list-num))
  11865. (setq local-list-indent nil
  11866. in-local-list nil))
  11867. (org-html-level-start level txt umax
  11868. (and org-export-with-toc (<= level umax))
  11869. head-count)
  11870. ;; QUOTES
  11871. (when (string-match quote-re line)
  11872. (insert "<pre>")
  11873. (setq inquote t)))
  11874. ((and org-export-with-tables
  11875. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  11876. (if (not table-open)
  11877. ;; New table starts
  11878. (setq table-open t table-buffer nil table-orig-buffer nil))
  11879. ;; Accumulate lines
  11880. (setq table-buffer (cons line table-buffer)
  11881. table-orig-buffer (cons origline table-orig-buffer))
  11882. (when (or (not lines)
  11883. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  11884. (car lines))))
  11885. (setq table-open nil
  11886. table-buffer (nreverse table-buffer)
  11887. table-orig-buffer (nreverse table-orig-buffer))
  11888. (insert (org-format-table-html table-buffer table-orig-buffer))))
  11889. (t
  11890. ;; Normal lines
  11891. (when (string-match
  11892. (cond
  11893. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  11894. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  11895. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  11896. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  11897. line)
  11898. (setq ind (org-get-string-indentation line)
  11899. start-is-num (match-beginning 4)
  11900. starter (if (match-beginning 2)
  11901. (substring (match-string 2 line) 0 -1))
  11902. line (substring line (match-beginning 5)))
  11903. (unless (string-match "[^ \t]" line)
  11904. ;; empty line. Pretend indentation is large.
  11905. (setq ind (1+ (or (car local-list-indent) 1))))
  11906. (while (and in-local-list
  11907. (or (and (= ind (car local-list-indent))
  11908. (not starter))
  11909. (< ind (car local-list-indent))))
  11910. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  11911. (pop local-list-num) (pop local-list-indent)
  11912. (setq in-local-list local-list-indent))
  11913. (cond
  11914. ((and starter
  11915. (or (not in-local-list)
  11916. (> ind (car local-list-indent))))
  11917. ;; Start new (level of ) list
  11918. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  11919. (push start-is-num local-list-num)
  11920. (push ind local-list-indent)
  11921. (setq in-local-list t))
  11922. (starter
  11923. ;; continue current list
  11924. (insert "<li>\n")))
  11925. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  11926. (setq line
  11927. (replace-match
  11928. (if (equal (match-string 1 line) "X")
  11929. "<b>[X]</b>"
  11930. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  11931. t t line))))
  11932. ;; Empty lines start a new paragraph. If hand-formatted lists
  11933. ;; are not fully interpreted, lines starting with "-", "+", "*"
  11934. ;; also start a new paragraph.
  11935. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (insert "<p>"))
  11936. (insert line (if org-export-preserve-breaks "<br>\n" "\n"))))
  11937. ))
  11938. ;; Properly close all local lists and other lists
  11939. (when inquote (insert "</pre>\n"))
  11940. (when in-local-list
  11941. ;; Close any local lists before inserting a new header line
  11942. (while local-list-num
  11943. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  11944. (pop local-list-num))
  11945. (setq local-list-indent nil
  11946. in-local-list nil))
  11947. (org-html-level-start 1 nil umax
  11948. (and org-export-with-toc (<= level umax))
  11949. head-count)
  11950. (when (plist-get opt-plist :auto-postamble)
  11951. (insert "<p>")
  11952. (if author (insert (concat (nth 1 lang-words) ": " author "\n")))
  11953. (if email (insert (concat "<a href=\"mailto:" email "\">&lt;"
  11954. email "&gt;</a>\n")))
  11955. (if (or author email) (insert "<br>\n"))
  11956. (if (and date time) (insert (concat (nth 2 lang-words) ": "
  11957. date " " time "<br>\n"))))
  11958. (if org-export-html-with-timestamp
  11959. (insert org-export-html-html-helper-timestamp))
  11960. (insert (or (plist-get opt-plist :postamble) ""))
  11961. (insert "</body>\n</html>\n")
  11962. (normal-mode)
  11963. (save-buffer)
  11964. (goto-char (point-min)))))
  11965. (defun org-format-table-html (lines olines)
  11966. "Find out which HTML converter to use and return the HTML code."
  11967. (if (string-match "^[ \t]*|" (car lines))
  11968. ;; A normal org table
  11969. (org-format-org-table-html lines)
  11970. ;; Table made by table.el - test for spanning
  11971. (let* ((hlines (delq nil (mapcar
  11972. (lambda (x)
  11973. (if (string-match "^[ \t]*\\+-" x) x
  11974. nil))
  11975. lines)))
  11976. (first (car hlines))
  11977. (ll (and (string-match "\\S-+" first)
  11978. (match-string 0 first)))
  11979. (re (concat "^[ \t]*" (regexp-quote ll)))
  11980. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  11981. hlines))))
  11982. (if (and (not spanning)
  11983. (not org-export-prefer-native-exporter-for-tables))
  11984. ;; We can use my own converter with HTML conversions
  11985. (org-format-table-table-html lines)
  11986. ;; Need to use the code generator in table.el, with the original text.
  11987. (org-format-table-table-html-using-table-generate-source olines)))))
  11988. (defun org-format-org-table-html (lines)
  11989. "Format a table into HTML."
  11990. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  11991. (setq lines (nreverse lines))
  11992. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  11993. (setq lines (nreverse lines))
  11994. (when org-export-table-remove-special-lines
  11995. ;; Check if the table has a marking column. If yes remove the
  11996. ;; column and the special lines
  11997. (let* ((special
  11998. (not
  11999. (memq nil
  12000. (mapcar
  12001. (lambda (x)
  12002. (or (string-match "^[ \t]*|-" x)
  12003. (string-match "^[ \t]*| *\\([#!$*_^ ]\\) *|" x)))
  12004. lines)))))
  12005. (if special
  12006. (setq lines
  12007. (delq nil
  12008. (mapcar
  12009. (lambda (x)
  12010. (if (string-match "^[ \t]*| *[!_^] *|" x)
  12011. nil ; ignore this line
  12012. (and (or (string-match "^[ \t]*|-+\\+" x)
  12013. (string-match "^[ \t]*|[^|]*|" x))
  12014. (replace-match "|" t t x))))
  12015. lines))))))
  12016. (let ((head (and org-export-highlight-first-table-line
  12017. (delq nil (mapcar
  12018. (lambda (x) (string-match "^[ \t]*|-" x))
  12019. (cdr lines)))))
  12020. line fields html)
  12021. (setq html (concat org-export-html-table-tag "\n"))
  12022. (while (setq line (pop lines))
  12023. (catch 'next-line
  12024. (if (string-match "^[ \t]*|-" line)
  12025. (progn
  12026. (setq head nil) ;; head ends here, first time around
  12027. ;; ignore this line
  12028. (throw 'next-line t)))
  12029. ;; Break the line into fields
  12030. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  12031. (setq html (concat
  12032. html
  12033. "<tr>"
  12034. (mapconcat (lambda (x)
  12035. (if head
  12036. (concat "<th>" x "</th>")
  12037. (concat "<td>" x "</td>")))
  12038. fields "")
  12039. "</tr>\n"))))
  12040. (setq html (concat html "</table>\n"))
  12041. html))
  12042. (defun org-fake-empty-table-line (line)
  12043. "Replace everything except \"|\" with spaces."
  12044. (let ((i (length line))
  12045. (newstr (copy-sequence line)))
  12046. (while (> i 0)
  12047. (setq i (1- i))
  12048. (if (not (eq (aref newstr i) ?|))
  12049. (aset newstr i ?\ )))
  12050. newstr))
  12051. (defun org-format-table-table-html (lines)
  12052. "Format a table generated by table.el into HTML.
  12053. This conversion does *not* use `table-generate-source' from table.el.
  12054. This has the advantage that Org-mode's HTML conversions can be used.
  12055. But it has the disadvantage, that no cell- or row-spanning is allowed."
  12056. (let (line field-buffer
  12057. (head org-export-highlight-first-table-line)
  12058. fields html empty)
  12059. (setq html (concat org-export-html-table-tag "\n"))
  12060. (while (setq line (pop lines))
  12061. (setq empty "&nbsp")
  12062. (catch 'next-line
  12063. (if (string-match "^[ \t]*\\+-" line)
  12064. (progn
  12065. (if field-buffer
  12066. (progn
  12067. (setq html (concat
  12068. html
  12069. "<tr>"
  12070. (mapconcat
  12071. (lambda (x)
  12072. (if (equal x "") (setq x empty))
  12073. (if head
  12074. (concat "<th>" x "</th>\n")
  12075. (concat "<td>" x "</td>\n")))
  12076. field-buffer "\n")
  12077. "</tr>\n"))
  12078. (setq head nil)
  12079. (setq field-buffer nil)))
  12080. ;; Ignore this line
  12081. (throw 'next-line t)))
  12082. ;; Break the line into fields and store the fields
  12083. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  12084. (if field-buffer
  12085. (setq field-buffer (mapcar
  12086. (lambda (x)
  12087. (concat x "<br>" (pop fields)))
  12088. field-buffer))
  12089. (setq field-buffer fields))))
  12090. (setq html (concat html "</table>\n"))
  12091. html))
  12092. (defun org-format-table-table-html-using-table-generate-source (lines)
  12093. "Format a table into html, using `table-generate-source' from table.el.
  12094. This has the advantage that cell- or row-spanning is allowed.
  12095. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  12096. (require 'table)
  12097. (with-current-buffer (get-buffer-create " org-tmp1 ")
  12098. (erase-buffer)
  12099. (insert (mapconcat 'identity lines "\n"))
  12100. (goto-char (point-min))
  12101. (if (not (re-search-forward "|[^+]" nil t))
  12102. (error "Error processing table"))
  12103. (table-recognize-table)
  12104. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  12105. (table-generate-source 'html " org-tmp2 ")
  12106. (set-buffer " org-tmp2 ")
  12107. (buffer-substring (point-min) (point-max))))
  12108. (defun org-html-handle-time-stamps (s)
  12109. "Format time stamps in string S, or remove them."
  12110. (let (r b)
  12111. (while (string-match org-maybe-keyword-time-regexp s)
  12112. (or b (setq b (substring s 0 (match-beginning 0))))
  12113. (if (not org-export-with-timestamps)
  12114. (setq r (concat r (substring s 0 (match-beginning 0)))
  12115. s (substring s (match-end 0)))
  12116. (setq r (concat
  12117. r (substring s 0 (match-beginning 0))
  12118. (if (match-end 1)
  12119. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  12120. (match-string 1 s)))
  12121. (format " @<span class=\"timestamp\">%s@</span>"
  12122. (substring (match-string 3 s) 1 -1)))
  12123. s (substring s (match-end 0)))))
  12124. ;; Line break of line started and ended with time stamp stuff
  12125. (if (not r)
  12126. s
  12127. (setq r (concat r s))
  12128. (unless (string-match "\\S-" (concat b s))
  12129. (setq r (concat r "@<br>")))
  12130. r)))
  12131. (defun org-html-protect (s)
  12132. ;; convert & to &amp;, < to &lt; and > to &gt;
  12133. (let ((start 0))
  12134. (while (string-match "&" s start)
  12135. (setq s (replace-match "&amp;" t t s)
  12136. start (1+ (match-beginning 0))))
  12137. (while (string-match "<" s)
  12138. (setq s (replace-match "&lt;" t t s)))
  12139. (while (string-match ">" s)
  12140. (setq s (replace-match "&gt;" t t s))))
  12141. s)
  12142. (defun org-html-cleanup-toc-line (s)
  12143. "Remove tags and time staps from lines going into the toc."
  12144. (if (string-match " +:[a-zA-Z0-9_@:]+: *$" s)
  12145. (setq s (replace-match "" t t s)))
  12146. (while (string-match org-maybe-keyword-time-regexp s)
  12147. (setq s (replace-match "" t t s)))
  12148. s)
  12149. (defun org-html-expand (string)
  12150. "Prepare STRING for HTML export. Applies all active conversions.
  12151. If there are links in the string, don't modify these."
  12152. (let* (m s l res)
  12153. (while (setq m (string-match org-bracket-link-regexp string))
  12154. (setq s (substring string 0 m)
  12155. l (match-string 0 string)
  12156. string (substring string (match-end 0)))
  12157. (push (org-html-do-expand s) res)
  12158. (push l res))
  12159. (push (org-html-do-expand string) res)
  12160. (apply 'concat (nreverse res))))
  12161. (defun org-html-do-expand (s)
  12162. "Apply all active conversions to translate special ASCII to HTML."
  12163. (setq s (org-html-protect s))
  12164. (if org-export-html-expand
  12165. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  12166. (setq s (replace-match "<\\1>" nil nil s))))
  12167. (if org-export-with-emphasize
  12168. (setq s (org-export-html-convert-emphasize s)))
  12169. (if org-export-with-sub-superscripts
  12170. (setq s (org-export-html-convert-sub-super s)))
  12171. (if org-export-with-TeX-macros
  12172. (let ((start 0) wd ass)
  12173. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  12174. (setq wd (match-string 1 s))
  12175. (if (setq ass (assoc wd org-html-entities))
  12176. (setq s (replace-match (or (cdr ass)
  12177. (concat "&" (car ass) ";"))
  12178. t t s))
  12179. (setq start (+ start (length wd)))))))
  12180. s)
  12181. (defun org-create-multibrace-regexp (left right n)
  12182. "Create a regular expression which will match a balanced sexp.
  12183. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  12184. as single character strings.
  12185. The regexp returned will match the entire expression including the
  12186. delimiters. It will also define a single group which contains the
  12187. match except for the outermost delimiters. The maximum depth of
  12188. stacked delimiters is N. Escaping delimiters is not possible."
  12189. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  12190. (or "\\|")
  12191. (re nothing)
  12192. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  12193. (while (> n 1)
  12194. (setq n (1- n)
  12195. re (concat re or next)
  12196. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  12197. (concat left "\\(" re "\\)" right)))
  12198. (defvar org-match-substring-regexp
  12199. (concat
  12200. "\\([^\\]\\)\\([_^]\\)\\("
  12201. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  12202. "\\|"
  12203. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  12204. "\\|"
  12205. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  12206. "The regular expression matching a sub- or superscript.")
  12207. (defun org-export-html-convert-sub-super (string)
  12208. "Convert sub- and superscripts in STRING to HTML."
  12209. (let (key c)
  12210. (while (string-match org-match-substring-regexp string)
  12211. (setq key (if (string= (match-string 2 string) "_") "sub" "sup"))
  12212. (setq c (or (match-string 8 string)
  12213. (match-string 6 string)
  12214. (match-string 5 string)))
  12215. (setq string (replace-match
  12216. (concat (match-string 1 string)
  12217. "<" key ">" c "</" key ">")
  12218. t t string)))
  12219. (while (string-match "\\\\\\([_^]\\)" string)
  12220. (setq string (replace-match (match-string 1 string) t t string))))
  12221. string)
  12222. (defun org-export-html-convert-emphasize (string)
  12223. (while (string-match org-italic-re string)
  12224. (setq string (replace-match "\\1<i>\\3</i>\\4" t nil string)))
  12225. (while (string-match org-bold-re string)
  12226. (setq string (replace-match "\\1<b>\\3</b>\\4" t nil string)))
  12227. (while (string-match org-underline-re string)
  12228. (setq string (replace-match "\\1<u>\\3</u>\\4" t nil string)))
  12229. string)
  12230. (defun org-html-level-start (level title umax with-toc head-count)
  12231. "Insert a new level in HTML export.
  12232. When TITLE is nil, just close all open levels."
  12233. (let ((l (1+ (max level umax))))
  12234. (while (<= l org-level-max)
  12235. (if (aref levels-open (1- l))
  12236. (progn
  12237. (org-html-level-close l)
  12238. (aset levels-open (1- l) nil)))
  12239. (setq l (1+ l)))
  12240. (when title
  12241. ;; If title is nil, this means this function is called to close
  12242. ;; all levels, so the rest is done only if title is given
  12243. (if (> level umax)
  12244. (progn
  12245. (if (aref levels-open (1- level))
  12246. (insert "<li>" title "<p>\n")
  12247. (aset levels-open (1- level) t)
  12248. (insert "<ul><li>" title "<p>\n")))
  12249. (if org-export-with-section-numbers
  12250. (setq title (concat (org-section-number level) " " title)))
  12251. (setq level (+ level 1))
  12252. (when (string-match "\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  12253. (setq title (replace-match
  12254. (if org-export-with-tags
  12255. (save-match-data
  12256. (concat
  12257. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  12258. (mapconcat 'identity (org-split-string
  12259. (match-string 1 title) ":")
  12260. "&nbsp;")
  12261. "</span>"))
  12262. "")
  12263. t t title)))
  12264. (if with-toc
  12265. (insert (format "\n<H%d><a name=\"sec-%d\">%s</a></H%d>\n"
  12266. level head-count title level))
  12267. (insert (format "\n<H%d>%s</H%d>\n" level title level)))))))
  12268. (defun org-html-level-close (&rest args)
  12269. "Terminate one level in HTML export."
  12270. (insert "</ul>"))
  12271. ;; Variable holding the vector with section numbers
  12272. (defvar org-section-numbers (make-vector org-level-max 0))
  12273. (defun org-init-section-numbers ()
  12274. "Initialize the vector for the section numbers."
  12275. (let* ((level -1)
  12276. (numbers (nreverse (org-split-string "" "\\.")))
  12277. (depth (1- (length org-section-numbers)))
  12278. (i depth) number-string)
  12279. (while (>= i 0)
  12280. (if (> i level)
  12281. (aset org-section-numbers i 0)
  12282. (setq number-string (or (car numbers) "0"))
  12283. (if (string-match "\\`[A-Z]\\'" number-string)
  12284. (aset org-section-numbers i
  12285. (- (string-to-char number-string) ?A -1))
  12286. (aset org-section-numbers i (string-to-number number-string)))
  12287. (pop numbers))
  12288. (setq i (1- i)))))
  12289. (defun org-section-number (&optional level)
  12290. "Return a string with the current section number.
  12291. When LEVEL is non-nil, increase section numbers on that level."
  12292. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  12293. (when level
  12294. (when (> level -1)
  12295. (aset org-section-numbers
  12296. level (1+ (aref org-section-numbers level))))
  12297. (setq idx (1+ level))
  12298. (while (<= idx depth)
  12299. (if (not (= idx 1))
  12300. (aset org-section-numbers idx 0))
  12301. (setq idx (1+ idx))))
  12302. (setq idx 0)
  12303. (while (<= idx depth)
  12304. (setq n (aref org-section-numbers idx))
  12305. (setq string (concat string (if (not (string= string "")) "." "")
  12306. (int-to-string n)))
  12307. (setq idx (1+ idx)))
  12308. (save-match-data
  12309. (if (string-match "\\`\\([@0]\\.\\)+" string)
  12310. (setq string (replace-match "" nil nil string)))
  12311. (if (string-match "\\(\\.0\\)+\\'" string)
  12312. (setq string (replace-match "" nil nil string))))
  12313. string))
  12314. (defun org-export-icalendar-this-file ()
  12315. "Export current file as an iCalendar file.
  12316. The iCalendar file will be located in the same directory as the Org-mode
  12317. file, but with extension `.ics'."
  12318. (interactive)
  12319. (org-export-icalendar nil buffer-file-name))
  12320. (defun org-export-as-xml (arg)
  12321. "Export current buffer as XOXO XML buffer."
  12322. (interactive "P")
  12323. (cond ((eq org-export-xml-type 'xoxo)
  12324. (org-export-as-xoxo (current-buffer)))))
  12325. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  12326. (with-current-buffer buffer
  12327. (apply 'insert output)))
  12328. (defun org-export-as-xoxo (&optional buffer)
  12329. "Export the org buffer as XOXO.
  12330. The XOXO buffer is named *xoxo-<source buffer name>*"
  12331. (interactive (list (current-buffer)))
  12332. ;; A quickie abstraction
  12333. ;; Output everything as XOXO
  12334. (with-current-buffer (get-buffer buffer)
  12335. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  12336. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  12337. (org-infile-export-plist)))
  12338. (filename (concat (file-name-as-directory
  12339. (org-export-directory :xoxo opt-plist))
  12340. (file-name-sans-extension
  12341. (file-name-nondirectory buffer-file-name))
  12342. ".html"))
  12343. (out (find-file-noselect filename))
  12344. (last-level 1)
  12345. (hanging-li nil))
  12346. ;; Check the output buffer is empty.
  12347. (with-current-buffer out (erase-buffer))
  12348. ;; Kick off the output
  12349. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  12350. (while (re-search-forward "^\\(\\*+\\) \\(.+\\)" (point-max) 't)
  12351. (let* ((hd (match-string-no-properties 1))
  12352. (level (length hd))
  12353. (text (concat
  12354. (match-string-no-properties 2)
  12355. (save-excursion
  12356. (goto-char (match-end 0))
  12357. (let ((str ""))
  12358. (catch 'loop
  12359. (while 't
  12360. (forward-line)
  12361. (if (looking-at "^[ \t]\\(.*\\)")
  12362. (setq str (concat str (match-string-no-properties 1)))
  12363. (throw 'loop str)))))))))
  12364. ;; Handle level rendering
  12365. (cond
  12366. ((> level last-level)
  12367. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  12368. ((< level last-level)
  12369. (dotimes (- (- last-level level) 1)
  12370. (if hanging-li
  12371. (org-export-as-xoxo-insert-into out "</li>\n"))
  12372. (org-export-as-xoxo-insert-into out "</ol>\n"))
  12373. (when hanging-li
  12374. (org-export-as-xoxo-insert-into out "</li>\n")
  12375. (setq hanging-li nil)))
  12376. ((equal level last-level)
  12377. (if hanging-li
  12378. (org-export-as-xoxo-insert-into out "</li>\n")))
  12379. )
  12380. (setq last-level level)
  12381. ;; And output the new li
  12382. (setq hanging-li 't)
  12383. (if (equal ?+ (elt text 0))
  12384. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  12385. (org-export-as-xoxo-insert-into out "<li>" text))))
  12386. ;; Finally finish off the ol
  12387. (dotimes (- last-level 1)
  12388. (if hanging-li
  12389. (org-export-as-xoxo-insert-into out "</li>\n"))
  12390. (org-export-as-xoxo-insert-into out "</ol>\n"))
  12391. ;; Finish the buffer off and clean it up.
  12392. (switch-to-buffer-other-window out)
  12393. (indent-region (point-min) (point-max) nil)
  12394. (save-buffer)
  12395. (goto-char (point-min))
  12396. )))
  12397. ;;;###autoload
  12398. (defun org-export-icalendar-all-agenda-files ()
  12399. "Export all files in `org-agenda-files' to iCalendar .ics files.
  12400. Each iCalendar file will be located in the same directory as the Org-mode
  12401. file, but with extension `.ics'."
  12402. (interactive)
  12403. (apply 'org-export-icalendar nil (org-agenda-files t)))
  12404. ;;;###autoload
  12405. (defun org-export-icalendar-combine-agenda-files ()
  12406. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  12407. The file is stored under the name `org-combined-agenda-icalendar-file'."
  12408. (interactive)
  12409. (apply 'org-export-icalendar t (org-agenda-files t)))
  12410. (defun org-export-icalendar (combine &rest files)
  12411. "Create iCalendar files for all elements of FILES.
  12412. If COMBINE is non-nil, combine all calendar entries into a single large
  12413. file and store it under the name `org-combined-agenda-icalendar-file'."
  12414. (save-excursion
  12415. (let* ((dir (org-export-directory
  12416. :ical (list :publishing-directory
  12417. org-export-publishing-directory)))
  12418. file ical-file ical-buffer category started org-agenda-new-buffers)
  12419. (when combine
  12420. (setq ical-file
  12421. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  12422. org-combined-agenda-icalendar-file
  12423. (expand-file-name org-combined-agenda-icalendar-file dir))
  12424. ical-buffer (org-get-agenda-file-buffer ical-file))
  12425. (set-buffer ical-buffer) (erase-buffer))
  12426. (while (setq file (pop files))
  12427. (catch 'nextfile
  12428. (org-check-agenda-file file)
  12429. (set-buffer (org-get-agenda-file-buffer file))
  12430. (unless combine
  12431. (setq ical-file (concat (file-name-as-directory dir)
  12432. (file-name-sans-extension
  12433. (file-name-nondirectory buffer-file-name))
  12434. ".ics"))
  12435. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  12436. (with-current-buffer ical-buffer (erase-buffer)))
  12437. (setq category (or org-category
  12438. (file-name-sans-extension
  12439. (file-name-nondirectory buffer-file-name))))
  12440. (if (symbolp category) (setq category (symbol-name category)))
  12441. (let ((standard-output ical-buffer))
  12442. (if combine
  12443. (and (not started) (setq started t)
  12444. (org-start-icalendar-file org-icalendar-combined-name))
  12445. (org-start-icalendar-file category))
  12446. (org-print-icalendar-entries combine category)
  12447. (when (or (and combine (not files)) (not combine))
  12448. (org-finish-icalendar-file)
  12449. (set-buffer ical-buffer)
  12450. (save-buffer)
  12451. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  12452. (org-release-buffers org-agenda-new-buffers))))
  12453. (defvar org-after-save-iCalendar-file-hook nil
  12454. "Hook run after an iCalendar file has been saved.
  12455. The iCalendar buffer is still current when this hook is run.
  12456. A good way to use this is to tell a desktop calenndar application to re-read
  12457. the iCalendar file.")
  12458. (defun org-print-icalendar-entries (&optional combine category)
  12459. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  12460. When COMBINE is non nil, add the category to each line."
  12461. (let ((re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  12462. (dts (org-ical-ts-to-string
  12463. (format-time-string (cdr org-time-stamp-formats) (current-time))
  12464. "DTSTART"))
  12465. hd ts ts2 state (inc t) pos scheduledp deadlinep tmp pri)
  12466. (save-excursion
  12467. (goto-char (point-min))
  12468. (while (re-search-forward org-ts-regexp nil t)
  12469. (setq pos (match-beginning 0)
  12470. ts (match-string 0)
  12471. inc t
  12472. hd (org-get-heading))
  12473. (if (looking-at re2)
  12474. (progn
  12475. (goto-char (match-end 0))
  12476. (setq ts2 (match-string 1) inc nil))
  12477. (setq ts2 ts
  12478. tmp (buffer-substring (max (point-min)
  12479. (- pos org-ds-keyword-length))
  12480. pos)
  12481. deadlinep (string-match org-deadline-regexp tmp)
  12482. scheduledp (string-match org-scheduled-regexp tmp)
  12483. ;; donep (org-entry-is-done-p)
  12484. ))
  12485. (if (or (string-match org-tr-regexp hd)
  12486. (string-match org-ts-regexp hd))
  12487. (setq hd (replace-match "" t t hd)))
  12488. (if combine
  12489. (setq hd (concat hd " (category " category ")")))
  12490. (if deadlinep (setq hd (concat "DL: " hd " This is a deadline")))
  12491. (if scheduledp (setq hd (concat "S: " hd " Scheduled for this date")))
  12492. (princ (format "BEGIN:VEVENT
  12493. %s
  12494. %s
  12495. SUMMARY:%s
  12496. END:VEVENT\n"
  12497. (org-ical-ts-to-string ts "DTSTART")
  12498. (org-ical-ts-to-string ts2 "DTEND" inc)
  12499. hd)))
  12500. (when org-icalendar-include-todo
  12501. (goto-char (point-min))
  12502. (while (re-search-forward org-todo-line-regexp nil t)
  12503. (setq state (match-string 1))
  12504. (unless (equal state org-done-string)
  12505. (setq hd (match-string 3))
  12506. (if (string-match org-priority-regexp hd)
  12507. (setq pri (string-to-char (match-string 2 hd))
  12508. hd (concat (substring hd 0 (match-beginning 1))
  12509. (substring hd (- (match-end 1)))))
  12510. (setq pri org-default-priority))
  12511. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  12512. (- org-lowest-priority ?A))))))
  12513. (princ (format "BEGIN:VTODO
  12514. %s
  12515. SUMMARY:%s
  12516. SEQUENCE:1
  12517. PRIORITY:%d
  12518. END:VTODO\n"
  12519. dts hd pri))))))))
  12520. (defun org-start-icalendar-file (name)
  12521. "Start an iCalendar file by inserting the header."
  12522. (let ((user user-full-name)
  12523. (name (or name "unknown"))
  12524. (timezone (cadr (current-time-zone))))
  12525. (princ
  12526. (format "BEGIN:VCALENDAR
  12527. VERSION:2.0
  12528. X-WR-CALNAME:%s
  12529. PRODID:-//%s//Emacs with Org-mode//EN
  12530. X-WR-TIMEZONE:%s
  12531. CALSCALE:GREGORIAN\n" name user timezone))))
  12532. (defun org-finish-icalendar-file ()
  12533. "Finish an iCalendar file by inserting the END statement."
  12534. (princ "END:VCALENDAR\n"))
  12535. (defun org-ical-ts-to-string (s keyword &optional inc)
  12536. "Take a time string S and convert it to iCalendar format.
  12537. KEYWORD is added in front, to make a complete line like DTSTART....
  12538. When INC is non-nil, increase the hour by two (if time string contains
  12539. a time), or the day by one (if it does not contain a time)."
  12540. (let ((t1 (org-parse-time-string s 'nodefault))
  12541. t2 fmt have-time time)
  12542. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  12543. (setq t2 t1 have-time t)
  12544. (setq t2 (org-parse-time-string s)))
  12545. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  12546. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  12547. (when inc
  12548. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  12549. (setq time (encode-time s mi h d m y)))
  12550. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  12551. (concat keyword (format-time-string fmt time))))
  12552. ;;; Key bindings
  12553. ;; - Bindings in Org-mode map are currently
  12554. ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
  12555. ;; abcd fgh j lmnopqrstuvwxyz!? #$ ^ -+*/= [] ; |,.<>~ '\t necessary bindings
  12556. ;; e (?) useful from outline-mode
  12557. ;; i k @ expendable from outline-mode
  12558. ;; 0123456789 % & ()_{} " ` free
  12559. ;; Make `C-c C-x' a prefix key
  12560. (define-key org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12561. (define-key org-mode-map "\C-c\C-e" (make-sparse-keymap))
  12562. ;; TAB key with modifiers
  12563. (define-key org-mode-map "\C-i" 'org-cycle)
  12564. (define-key org-mode-map [(tab)] 'org-cycle)
  12565. (define-key org-mode-map [(meta tab)] 'org-complete)
  12566. (define-key org-mode-map "\M-\C-i" 'org-complete) ; for tty emacs
  12567. ;; The following line is necessary under Suse GNU/Linux
  12568. (unless (featurep 'xemacs)
  12569. (define-key org-mode-map [S-iso-lefttab] 'org-shifttab))
  12570. (define-key org-mode-map [(shift tab)] 'org-shifttab)
  12571. (define-key org-mode-map (org-key 'S-return) 'org-table-copy-down)
  12572. (define-key org-mode-map "\C-c\C-xc" 'org-table-copy-down) ; tty
  12573. (define-key org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12574. (define-key org-mode-map "\C-c\C-xM" 'org-insert-todo-heading) ; tty
  12575. (define-key org-mode-map [(meta return)] 'org-meta-return)
  12576. (define-key org-mode-map "\C-c\C-xm" 'org-meta-return) ; tty emacs
  12577. (define-key org-mode-map [?\e (return)] 'org-meta-return) ; tty emacs
  12578. ;; Cursor keys with modifiers
  12579. (define-key org-mode-map [(meta left)] 'org-metaleft)
  12580. (define-key org-mode-map [?\e (left)] 'org-metaleft) ; for tty emacs
  12581. (define-key org-mode-map "\C-c\C-xl" 'org-metaleft) ; for tty emacs
  12582. (define-key org-mode-map [(meta right)] 'org-metaright)
  12583. (define-key org-mode-map [?\e (right)] 'org-metaright) ; for tty emacs
  12584. (define-key org-mode-map "\C-c\C-xr" 'org-metaright) ; for tty emacs
  12585. (define-key org-mode-map [(meta up)] 'org-metaup)
  12586. (define-key org-mode-map [?\e (up)] 'org-metaup) ; for tty emacs
  12587. (define-key org-mode-map "\C-c\C-xu" 'org-metaup) ; for tty emacs
  12588. (define-key org-mode-map [(meta down)] 'org-metadown)
  12589. (define-key org-mode-map [?\e (down)] 'org-metadown) ; for tty emacs
  12590. (define-key org-mode-map "\C-c\C-xd" 'org-metadown) ; for tty emacs
  12591. (define-key org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12592. (define-key org-mode-map "\C-c\C-xL" 'org-shiftmetaleft) ; tty
  12593. (define-key org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12594. (define-key org-mode-map "\C-c\C-xR" 'org-shiftmetaright) ; tty
  12595. (define-key org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12596. (define-key org-mode-map "\C-c\C-xU" 'org-shiftmetaup) ; tty
  12597. (define-key org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12598. (define-key org-mode-map "\C-c\C-xD" 'org-shiftmetadown) ; tty
  12599. (define-key org-mode-map (org-key 'S-up) 'org-shiftup)
  12600. (define-key org-mode-map [?\C-c ?\C-x (up)] 'org-shiftup)
  12601. (define-key org-mode-map (org-key 'S-down) 'org-shiftdown)
  12602. (define-key org-mode-map [?\C-c ?\C-x (down)] 'org-shiftdown)
  12603. (define-key org-mode-map (org-key 'S-left) 'org-shiftleft)
  12604. (define-key org-mode-map [?\C-c ?\C-x (left)] 'org-shiftleft)
  12605. (define-key org-mode-map (org-key 'S-right) 'org-shiftright)
  12606. (define-key org-mode-map [?\C-c ?\C-x (right)] 'org-shiftright)
  12607. ;; All the other keys
  12608. (define-key org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12609. (define-key org-mode-map "\C-c$" 'org-archive-subtree)
  12610. (define-key org-mode-map "\C-c\C-j" 'org-goto)
  12611. (define-key org-mode-map "\C-c\C-t" 'org-todo)
  12612. (define-key org-mode-map "\C-c\C-s" 'org-schedule)
  12613. (define-key org-mode-map "\C-c\C-d" 'org-deadline)
  12614. (define-key org-mode-map "\C-c;" 'org-toggle-comment)
  12615. (define-key org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12616. (define-key org-mode-map "\C-c\C-w" 'org-check-deadlines)
  12617. (define-key org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  12618. (define-key org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  12619. (define-key org-mode-map "\C-c\C-m" 'org-insert-heading)
  12620. (define-key org-mode-map "\M-\C-m" 'org-insert-heading)
  12621. (define-key org-mode-map "\C-c\C-l" 'org-insert-link)
  12622. (define-key org-mode-map "\C-c\C-o" 'org-open-at-point)
  12623. (define-key org-mode-map "\C-c%" 'org-mark-ring-push)
  12624. (define-key org-mode-map "\C-c&" 'org-mark-ring-goto)
  12625. (define-key org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  12626. (define-key org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12627. (define-key org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12628. (define-key org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12629. (define-key org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12630. (define-key org-mode-map "\C-c>" 'org-goto-calendar)
  12631. (define-key org-mode-map "\C-c<" 'org-date-from-calendar)
  12632. (define-key org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12633. (define-key org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12634. (define-key org-mode-map "\C-c]" 'org-remove-file)
  12635. (define-key org-mode-map "\C-c\C-r" 'org-timeline)
  12636. (define-key org-mode-map "\C-c-" 'org-table-insert-hline)
  12637. (define-key org-mode-map "\C-c^" 'org-table-sort-lines)
  12638. (define-key org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12639. (define-key org-mode-map "\C-m" 'org-return)
  12640. (define-key org-mode-map "\C-c?" 'org-table-current-column)
  12641. (define-key org-mode-map "\C-c " 'org-table-blank-field)
  12642. (define-key org-mode-map "\C-c+" 'org-table-sum)
  12643. (define-key org-mode-map "\C-c=" 'org-table-eval-formula)
  12644. (define-key org-mode-map "\C-c'" 'org-table-edit-formulas)
  12645. (define-key org-mode-map "\C-c`" 'org-table-edit-field)
  12646. (define-key org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12647. (define-key org-mode-map "\C-c*" 'org-table-recalculate)
  12648. (define-key org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12649. (define-key org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12650. (define-key org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  12651. (define-key org-mode-map "\C-c\C-xa" 'org-export-as-ascii)
  12652. (define-key org-mode-map "\C-c\C-x\C-a" 'org-export-as-ascii)
  12653. (define-key org-mode-map "\C-c\C-xv" 'org-export-visible)
  12654. (define-key org-mode-map "\C-c\C-x\C-v" 'org-export-visible)
  12655. ;; OPML support is only an option for the future
  12656. ;(define-key org-mode-map "\C-c\C-xo" 'org-export-as-opml)
  12657. ;(define-key org-mode-map "\C-c\C-x\C-o" 'org-export-as-opml)
  12658. (define-key org-mode-map "\C-c\C-xi" 'org-export-icalendar-this-file)
  12659. (define-key org-mode-map "\C-c\C-x\C-i" 'org-export-icalendar-all-agenda-files)
  12660. (define-key org-mode-map "\C-c\C-xc" 'org-export-icalendar-combine-agenda-files)
  12661. (define-key org-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  12662. (define-key org-mode-map "\C-c\C-xt" 'org-insert-export-options-template)
  12663. (define-key org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12664. (define-key org-mode-map "\C-c\C-xh" 'org-export-as-html)
  12665. (define-key org-mode-map "\C-c\C-xx" 'org-export-as-xml)
  12666. (define-key org-mode-map "\C-c\C-x\C-x" 'org-export-as-xml)
  12667. (define-key org-mode-map "\C-c\C-xb" 'org-export-as-html-and-open)
  12668. (define-key org-mode-map "\C-c\C-x\C-b" 'org-export-as-html-and-open)
  12669. (define-key org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  12670. (define-key org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12671. (define-key org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12672. (define-key org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12673. (define-key org-mode-map "\C-c\C-ef" 'org-publish-current-file)
  12674. (define-key org-mode-map "\C-c\C-ep" 'org-publish-current-project)
  12675. (define-key org-mode-map "\C-c\C-ec" 'org-publish)
  12676. (define-key org-mode-map "\C-c\C-ea" 'org-publish-all)
  12677. (define-key org-mode-map "\C-c\C-e\C-f" 'org-publish-current-file)
  12678. (define-key org-mode-map "\C-c\C-e\C-p" 'org-publish-current-project)
  12679. (define-key org-mode-map "\C-c\C-e\C-c" 'org-publish)
  12680. (define-key org-mode-map "\C-c\C-e\C-a" 'org-publish-all)
  12681. (when (featurep 'xemacs)
  12682. (define-key org-mode-map 'button3 'popup-mode-menu))
  12683. (defsubst org-table-p () (org-at-table-p))
  12684. (defun org-self-insert-command (N)
  12685. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12686. If the cursor is in a table looking at whitespace, the whitespace is
  12687. overwritten, and the table is not marked as requiring realignment."
  12688. (interactive "p")
  12689. (if (and (org-table-p)
  12690. (progn
  12691. ;; check if we blank the field, and if that triggers align
  12692. (and org-table-auto-blank-field
  12693. (member last-command
  12694. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12695. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12696. ;; got extra space, this field does not determine column width
  12697. (let (org-table-may-need-update) (org-table-blank-field))
  12698. ;; no extra space, this field may determine column width
  12699. (org-table-blank-field)))
  12700. t)
  12701. (eq N 1)
  12702. (looking-at "[^|\n]* |"))
  12703. (let (org-table-may-need-update)
  12704. (goto-char (1- (match-end 0)))
  12705. (delete-backward-char 1)
  12706. (goto-char (match-beginning 0))
  12707. (self-insert-command N))
  12708. (setq org-table-may-need-update t)
  12709. (self-insert-command N)))
  12710. (defun org-delete-backward-char (N)
  12711. "Like `delete-backward-char', insert whitespace at field end in tables.
  12712. When deleting backwards, in tables this function will insert whitespace in
  12713. front of the next \"|\" separator, to keep the table aligned. The table will
  12714. still be marked for re-alignment if the field did fill the entire column,
  12715. because, in this case the deletion might narrow the column."
  12716. (interactive "p")
  12717. (if (and (org-table-p)
  12718. (eq N 1)
  12719. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12720. (looking-at ".*?|"))
  12721. (let ((pos (point))
  12722. (noalign (looking-at "[^|\n\r]* |"))
  12723. (c org-table-may-need-update))
  12724. (backward-delete-char N)
  12725. (skip-chars-forward "^|")
  12726. (insert " ")
  12727. (goto-char (1- pos))
  12728. ;; noalign: if there were two spaces at the end, this field
  12729. ;; does not determine the width of the column.
  12730. (if noalign (setq org-table-may-need-update c)))
  12731. (backward-delete-char N)))
  12732. (defun org-delete-char (N)
  12733. "Like `delete-char', but insert whitespace at field end in tables.
  12734. When deleting characters, in tables this function will insert whitespace in
  12735. front of the next \"|\" separator, to keep the table aligned. The table will
  12736. still be marked for re-alignment if the field did fill the entire column,
  12737. because, in this case the deletion might narrow the column."
  12738. (interactive "p")
  12739. (if (and (org-table-p)
  12740. (not (bolp))
  12741. (not (= (char-after) ?|))
  12742. (eq N 1))
  12743. (if (looking-at ".*?|")
  12744. (let ((pos (point))
  12745. (noalign (looking-at "[^|\n\r]* |"))
  12746. (c org-table-may-need-update))
  12747. (replace-match (concat
  12748. (substring (match-string 0) 1 -1)
  12749. " |"))
  12750. (goto-char pos)
  12751. ;; noalign: if there were two spaces at the end, this field
  12752. ;; does not determine the width of the column.
  12753. (if noalign (setq org-table-may-need-update c)))
  12754. (delete-char N))
  12755. (delete-char N)))
  12756. ;; How to do this: Measure non-white length of current string
  12757. ;; If equal to column width, we should realign.
  12758. (defun org-remap (map &rest commands)
  12759. "In MAP, remap the functions given in COMMANDS.
  12760. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12761. (let (new old)
  12762. (while commands
  12763. (setq old (pop commands) new (pop commands))
  12764. (if (fboundp 'command-remapping)
  12765. (define-key map (vector 'remap old) new)
  12766. (substitute-key-definition old new map global-map)))))
  12767. (when (eq org-enable-table-editor 'optimized)
  12768. ;; If the user wants maximum table support, we need to hijack
  12769. ;; some standard editing functions
  12770. (org-remap org-mode-map
  12771. 'self-insert-command 'org-self-insert-command
  12772. 'delete-char 'org-delete-char
  12773. 'delete-backward-char 'org-delete-backward-char)
  12774. (define-key org-mode-map "|" 'org-force-self-insert))
  12775. (defun org-shiftcursor-error ()
  12776. "Throw an error because Shift-Cursor command was applied in wrong context."
  12777. (error "This command is active in special context like tables, headlines or timestamps"))
  12778. (defun org-shifttab ()
  12779. "Global visibility cycling or move to previous table field.
  12780. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12781. on context.
  12782. See the individual commands for more information."
  12783. (interactive)
  12784. (cond
  12785. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12786. (t (call-interactively 'org-global-cycle))))
  12787. (defun org-shiftmetaleft ()
  12788. "Promote subtree or delete table column.
  12789. Calls `org-promote-subtree' or `org-table-delete-column', depending on context.
  12790. See the individual commands for more information."
  12791. (interactive)
  12792. (cond
  12793. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12794. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12795. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12796. (t (org-shiftcursor-error))))
  12797. (defun org-shiftmetaright ()
  12798. "Demote subtree or insert table column.
  12799. Calls `org-demote-subtree' or `org-table-insert-column', depending on context.
  12800. See the individual commands for more information."
  12801. (interactive)
  12802. (cond
  12803. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12804. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12805. ((org-at-item-p) (call-interactively 'org-indent-item))
  12806. (t (org-shiftcursor-error))))
  12807. (defun org-shiftmetaup (&optional arg)
  12808. "Move subtree up or kill table row.
  12809. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12810. `org-move-item-up' depending on context. See the individual commands
  12811. for more information."
  12812. (interactive "P")
  12813. (cond
  12814. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12815. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12816. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12817. (t (org-shiftcursor-error))))
  12818. (defun org-shiftmetadown (&optional arg)
  12819. "Move subtree down or insert table row.
  12820. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12821. `org-move-item-down', depending on context. See the individual
  12822. commands for more information."
  12823. (interactive "P")
  12824. (cond
  12825. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12826. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12827. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12828. (t (org-shiftcursor-error))))
  12829. (defun org-metaleft (&optional arg)
  12830. "Promote heading or move table column to left.
  12831. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12832. With no specific context, calls the Emacs default `backward-word'.
  12833. See the individual commands for more information."
  12834. (interactive "P")
  12835. (cond
  12836. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12837. ((or (org-on-heading-p) (org-region-active-p))
  12838. (call-interactively 'org-do-promote))
  12839. (t (call-interactively 'backward-word))))
  12840. (defun org-metaright (&optional arg)
  12841. "Demote subtree or move table column to right.
  12842. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12843. With no specific context, calls the Emacs default `forward-word'.
  12844. See the individual commands for more information."
  12845. (interactive "P")
  12846. (cond
  12847. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12848. ((or (org-on-heading-p) (org-region-active-p))
  12849. (call-interactively 'org-do-demote))
  12850. (t (call-interactively 'forward-word))))
  12851. (defun org-metaup (&optional arg)
  12852. "Move subtree up or move table row up.
  12853. Calls `org-move-subtree-up' or `org-table-move-row' or
  12854. `org-move-item-up', depending on context. See the individual commands
  12855. for more information."
  12856. (interactive "P")
  12857. (cond
  12858. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12859. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12860. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12861. (t (org-shiftcursor-error))))
  12862. (defun org-metadown (&optional arg)
  12863. "Move subtree down or move table row down.
  12864. Calls `org-move-subtree-down' or `org-table-move-row' or
  12865. `org-move-item-down', depending on context. See the individual
  12866. commands for more information."
  12867. (interactive "P")
  12868. (cond
  12869. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12870. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12871. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12872. (t (org-shiftcursor-error))))
  12873. (defun org-shiftup (&optional arg)
  12874. "Increase item in timestamp or increase priority of current headline.
  12875. Calls `org-timestamp-up' or `org-priority-up', depending on context.
  12876. See the individual commands for more information."
  12877. (interactive "P")
  12878. (cond
  12879. ((org-at-timestamp-p) (call-interactively 'org-timestamp-up))
  12880. ((org-on-heading-p) (call-interactively 'org-priority-up))
  12881. ((org-at-item-p) (call-interactively 'org-previous-item))
  12882. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  12883. (defun org-shiftdown (&optional arg)
  12884. "Decrease item in timestamp or decrease priority of current headline.
  12885. Calls `org-timestamp-down' or `org-priority-down', depending on context.
  12886. See the individual commands for more information."
  12887. (interactive "P")
  12888. (cond
  12889. ((org-at-timestamp-p) (call-interactively 'org-timestamp-down))
  12890. ((org-on-heading-p) (call-interactively 'org-priority-down))
  12891. (t (call-interactively 'org-next-item))))
  12892. (defun org-shiftright ()
  12893. "Next TODO keyword or timestamp one day later, depending on context."
  12894. (interactive)
  12895. (cond
  12896. ((org-at-timestamp-p) (call-interactively 'org-timestamp-up-day))
  12897. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  12898. (t (org-shiftcursor-error))))
  12899. (defun org-shiftleft ()
  12900. "Previous TODO keyword or timestamp one day earlier, depending on context."
  12901. (interactive)
  12902. (cond
  12903. ((org-at-timestamp-p) (call-interactively 'org-timestamp-down-day))
  12904. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  12905. (t (org-shiftcursor-error))))
  12906. (defun org-copy-special ()
  12907. "Copy region in table or copy current subtree.
  12908. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12909. See the individual commands for more information."
  12910. (interactive)
  12911. (call-interactively
  12912. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12913. (defun org-cut-special ()
  12914. "Cut region in table or cut current subtree.
  12915. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12916. See the individual commands for more information."
  12917. (interactive)
  12918. (call-interactively
  12919. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12920. (defun org-paste-special (arg)
  12921. "Paste rectangular region into table, or past subtree relative to level.
  12922. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12923. See the individual commands for more information."
  12924. (interactive "P")
  12925. (if (org-at-table-p)
  12926. (org-table-paste-rectangle)
  12927. (org-paste-subtree arg)))
  12928. (defun org-ctrl-c-ctrl-c (&optional arg)
  12929. "Set tags in headline, or update according to changed information at point.
  12930. This command does many different things, depending on context:
  12931. - If the cursor is in a headline, prompt for tags and insert them
  12932. into the current line, aligned to `org-tags-column'. When called
  12933. with prefix arg, realign all tags in the current buffer.
  12934. - If the cursor is in one of the special #+KEYWORD lines, this
  12935. triggers scanning the buffer for these lines and updating the
  12936. information.
  12937. - If the cursor is inside a table, realign the table. This command
  12938. works even if the automatic table editor has been turned off.
  12939. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12940. the entire table.
  12941. - If the cursor is inside a table created by the table.el package,
  12942. activate that table.
  12943. - If the current buffer is a remember buffer, close note and file it.
  12944. with a prefix argument, file it without further interaction to the default
  12945. location.
  12946. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12947. links in this buffer.
  12948. - If the cursor is on a numbered item in a plain list, renumber the
  12949. ordered list."
  12950. (interactive "P")
  12951. (let ((org-enable-table-editor t))
  12952. (cond
  12953. ((and (local-variable-p 'org-finish-function (current-buffer))
  12954. (fboundp org-finish-function))
  12955. (funcall org-finish-function))
  12956. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12957. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12958. ((org-at-table.el-p)
  12959. (require 'table)
  12960. (beginning-of-line 1)
  12961. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12962. (call-interactively 'table-recognize-table))
  12963. ((org-at-table-p)
  12964. (org-table-maybe-eval-formula)
  12965. (if arg
  12966. (call-interactively 'org-table-recalculate)
  12967. (org-table-maybe-recalculate-line))
  12968. (call-interactively 'org-table-align))
  12969. ((org-at-item-checkbox-p)
  12970. (call-interactively 'org-toggle-checkbox))
  12971. ((org-at-item-p)
  12972. (call-interactively 'org-renumber-ordered-list))
  12973. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12974. (cond
  12975. ((equal (match-string 1) "TBLFM")
  12976. ;; Recalculate the table before this line
  12977. (save-excursion
  12978. (beginning-of-line 1)
  12979. (skip-chars-backward " \r\n\t")
  12980. (if (org-at-table-p)
  12981. (org-call-with-arg 'org-table-recalculate t))))
  12982. (t
  12983. (call-interactively 'org-mode-restart))))
  12984. (t (error "C-c C-c can do nothing useful at this location.")))))
  12985. (defun org-mode-restart ()
  12986. "Restart Org-mode, to scan again for special lines.
  12987. Also updates the keyword regular expressions."
  12988. (interactive)
  12989. (let ((org-inhibit-startup t)) (org-mode))
  12990. (message "Org-mode restarted to refresh keyword and special line setup"))
  12991. (defun org-return ()
  12992. "Goto next table row or insert a newline.
  12993. Calls `org-table-next-row' or `newline', depending on context.
  12994. See the individual commands for more information."
  12995. (interactive)
  12996. (cond
  12997. ((org-at-table-p)
  12998. (org-table-justify-field-maybe)
  12999. (call-interactively 'org-table-next-row))
  13000. (t (newline))))
  13001. (defun org-meta-return (&optional arg)
  13002. "Insert a new heading or wrap a region in a table.
  13003. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13004. See the individual commands for more information."
  13005. (interactive "P")
  13006. (cond
  13007. ((org-at-table-p)
  13008. (call-interactively 'org-table-wrap-region))
  13009. (t (call-interactively 'org-insert-heading))))
  13010. ;;; Menu entries
  13011. ;; Define the Org-mode menus
  13012. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13013. '("Tbl"
  13014. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  13015. ["Next Field" org-cycle (org-at-table-p)]
  13016. ["Previous Field" org-shifttab (org-at-table-p)]
  13017. ["Next Row" org-return (org-at-table-p)]
  13018. "--"
  13019. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13020. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13021. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13022. "--"
  13023. ("Column"
  13024. ["Move Column Left" org-metaleft (org-at-table-p)]
  13025. ["Move Column Right" org-metaright (org-at-table-p)]
  13026. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13027. ["Insert Column" org-shiftmetaright (org-at-table-p)]
  13028. "--"
  13029. ["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])
  13030. ("Row"
  13031. ["Move Row Up" org-metaup (org-at-table-p)]
  13032. ["Move Row Down" org-metadown (org-at-table-p)]
  13033. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13034. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13035. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13036. "--"
  13037. ["Insert Hline" org-table-insert-hline (org-at-table-p)])
  13038. ("Rectangle"
  13039. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13040. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13041. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13042. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13043. "--"
  13044. ("Calculate"
  13045. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13046. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13047. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  13048. "--"
  13049. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13050. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13051. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13052. "--"
  13053. ["Sum Column/Rectangle" org-table-sum
  13054. (or (org-at-table-p) (org-region-active-p))]
  13055. ["Which Column?" org-table-current-column (org-at-table-p)])
  13056. ["Debug Formulas"
  13057. (setq org-table-formula-debug (not org-table-formula-debug))
  13058. :style toggle :selected org-table-formula-debug]
  13059. "--"
  13060. ["Create" org-table-create (and (not (org-at-table-p))
  13061. org-enable-table-editor)]
  13062. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13063. ["Import from File" org-table-import (not (org-at-table-p))]
  13064. ["Export to File" org-table-export (org-at-table-p)]
  13065. "--"
  13066. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13067. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13068. '("Org"
  13069. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  13070. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  13071. ["Sparse Tree" org-occur t]
  13072. ["Show All" show-all t]
  13073. "--"
  13074. ["New Heading" org-insert-heading t]
  13075. ("Navigate Headings"
  13076. ["Up" outline-up-heading t]
  13077. ["Next" outline-next-visible-heading t]
  13078. ["Previous" outline-previous-visible-heading t]
  13079. ["Next Same Level" outline-forward-same-level t]
  13080. ["Previous Same Level" outline-backward-same-level t]
  13081. "--"
  13082. ["Jump" org-goto t])
  13083. ("Edit Structure"
  13084. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13085. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13086. "--"
  13087. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13088. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13089. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13090. "--"
  13091. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13092. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13093. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13094. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13095. "--"
  13096. ["Archive Subtree" org-archive-subtree t]
  13097. "--"
  13098. ["Convert to odd levels" org-convert-to-odd-levels t]
  13099. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13100. "--"
  13101. ("TODO Lists"
  13102. ["TODO/DONE/-" org-todo t]
  13103. ["Show TODO Tree" org-show-todo-tree t]
  13104. ["Global TODO list" org-todo-list t]
  13105. "--"
  13106. ["Set Priority" org-priority t]
  13107. ["Priority Up" org-shiftup t]
  13108. ["Priority Down" org-shiftdown t])
  13109. ("Dates and Scheduling"
  13110. ["Timestamp" org-time-stamp t]
  13111. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13112. ("Change Date"
  13113. ["1 Day Later" org-timestamp-up-day t]
  13114. ["1 Day Earlier" org-timestamp-down-day t]
  13115. ["1 ... Later" org-shiftup t]
  13116. ["1 ... Earlier" org-shiftdown t])
  13117. ["Compute Time Range" org-evaluate-time-range t]
  13118. ["Schedule Item" org-schedule t]
  13119. ["Deadline" org-deadline t]
  13120. "--"
  13121. ["Goto Calendar" org-goto-calendar t]
  13122. ["Date from Calendar" org-date-from-calendar t])
  13123. "--"
  13124. ["Agenda Command" org-agenda t]
  13125. ("File List for Agenda")
  13126. ("Special views current file"
  13127. ["TODO Tree" org-show-todo-tree t]
  13128. ["Check Deadlines" org-check-deadlines t]
  13129. ["Timeline" org-timeline t]
  13130. ["Tags Tree" org-tags-sparse-tree t])
  13131. "--"
  13132. ("Hyperlinks"
  13133. ["Store Link (Global)" org-store-link t]
  13134. ["Insert Link" org-insert-link t]
  13135. ["Follow Link" org-open-at-point t]
  13136. "--"
  13137. ["Descriptive Links"
  13138. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13139. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  13140. ["Literal Links"
  13141. (progn
  13142. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13143. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))]
  13144. "--"
  13145. ["Upgrade all <link> to [[link][desc]]" org-upgrade-old-links
  13146. (save-excursion (goto-char (point-min))
  13147. (re-search-forward "<[a-z]+:" nil t))])
  13148. "--"
  13149. ("Export"
  13150. ["ASCII" org-export-as-ascii t]
  13151. ["Export visible part..." org-export-visible t]
  13152. ["HTML" org-export-as-html t]
  13153. ["HTML and Open" org-export-as-html-and-open t]
  13154. ["XOXO" org-export-as-xml t]
  13155. "--"
  13156. ["iCalendar this file" org-export-icalendar-this-file t]
  13157. ["iCalendar all agenda files" org-export-icalendar-all-agenda-files
  13158. :active t :keys "C-c C-x C-i"]
  13159. ["iCalendar combined" org-export-icalendar-combine-agenda-files t]
  13160. "--"
  13161. ["Option Template" org-insert-export-options-template t]
  13162. ["Toggle Fixed Width" org-toggle-fixed-width-section t])
  13163. ("Publish"
  13164. ["Current File" org-publish-current-file t]
  13165. ["Current Project" org-publish-current-project t]
  13166. ["Project..." org-publish t]
  13167. ["All Projects" org-publish-all t])
  13168. "--"
  13169. ("Documentation"
  13170. ["Show Version" org-version t]
  13171. ["Info Documentation" org-info t])
  13172. ("Customize"
  13173. ["Browse Org Group" org-customize t]
  13174. "--"
  13175. ["Expand This Menu" org-create-customize-menu
  13176. (fboundp 'customize-menu-create)])
  13177. "--"
  13178. ["Refresh setup" org-mode-restart t]
  13179. ))
  13180. (defun org-info (&optional node)
  13181. "Read documentation for Org-mode in the info system.
  13182. With optional NODE, go directly to that node."
  13183. (interactive)
  13184. (require 'info)
  13185. (Info-goto-node (format "(org)%s" (or node ""))))
  13186. (defun org-install-agenda-files-menu ()
  13187. (let ((bl (buffer-list)))
  13188. (save-excursion
  13189. (while bl
  13190. (set-buffer (pop bl))
  13191. (if (eq major-mode 'org-mode) (setq bl nil)))
  13192. (when (eq major-mode 'org-mode)
  13193. (easy-menu-change
  13194. '("Org") "File List for Agenda"
  13195. (append
  13196. (list
  13197. ["Edit File List" (org-edit-agenda-file-list) t]
  13198. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13199. ["Remove Current File from List" org-remove-file t]
  13200. ["Cycle through agenda files" org-cycle-agenda-files t]
  13201. "--")
  13202. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13203. ;;; Documentation
  13204. (defun org-customize ()
  13205. "Call the customize function with org as argument."
  13206. (interactive)
  13207. (customize-browse 'org))
  13208. (defun org-create-customize-menu ()
  13209. "Create a full customization menu for Org-mode, insert it into the menu."
  13210. (interactive)
  13211. (if (fboundp 'customize-menu-create)
  13212. (progn
  13213. (easy-menu-change
  13214. '("Org") "Customize"
  13215. `(["Browse Org group" org-customize t]
  13216. "--"
  13217. ,(customize-menu-create 'org)
  13218. ["Set" Custom-set t]
  13219. ["Save" Custom-save t]
  13220. ["Reset to Current" Custom-reset-current t]
  13221. ["Reset to Saved" Custom-reset-saved t]
  13222. ["Reset to Standard Settings" Custom-reset-standard t]))
  13223. (message "\"Org\"-menu now contains full customization menu"))
  13224. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13225. ;;; Miscellaneous stuff
  13226. (defun org-move-line-down (arg)
  13227. "Move the current line down. With prefix argument, move it past ARG lines."
  13228. (interactive "p")
  13229. (let ((col (current-column))
  13230. beg end pos)
  13231. (beginning-of-line 1) (setq beg (point))
  13232. (beginning-of-line 2) (setq end (point))
  13233. (beginning-of-line (+ 1 arg))
  13234. (setq pos (move-marker (make-marker) (point)))
  13235. (insert (delete-and-extract-region beg end))
  13236. (goto-char pos)
  13237. (move-to-column col)))
  13238. (defun org-move-line-up (arg)
  13239. "Move the current line up. With prefix argument, move it past ARG lines."
  13240. (interactive "p")
  13241. (let ((col (current-column))
  13242. beg end pos)
  13243. (beginning-of-line 1) (setq beg (point))
  13244. (beginning-of-line 2) (setq end (point))
  13245. (beginning-of-line (- arg))
  13246. (setq pos (move-marker (make-marker) (point)))
  13247. (insert (delete-and-extract-region beg end))
  13248. (goto-char pos)
  13249. (move-to-column col)))
  13250. ;; Paragraph filling stuff.
  13251. ;; We want this to be just right, so use the full arsenal.
  13252. ;; FIXME: This very likely does not work correctly for XEmacs, because the
  13253. ;; filladapt package works slightly differently.
  13254. (defun org-set-autofill-regexps ()
  13255. (interactive)
  13256. ;; In the paragraph separator we include headlines, because filling
  13257. ;; text in a line directly attached to a headline would otherwise
  13258. ;; fill the headline as well.
  13259. (set (make-local-variable 'paragraph-separate) "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
  13260. ;; The paragraph starter includes hand-formatted lists.
  13261. (set (make-local-variable 'paragraph-start)
  13262. "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*]\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13263. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13264. ;; But only if the user has not turned off tables or fixed-width regions
  13265. (set (make-local-variable 'auto-fill-inhibit-regexp)
  13266. (concat "\\*\\|#"
  13267. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13268. (concat
  13269. "\\|[ \t]*["
  13270. (if org-enable-table-editor "|" "")
  13271. (if org-enable-fixed-width-editor ":" "")
  13272. "]"))))
  13273. ;; We use our own fill-paragraph function, to make sure that tables
  13274. ;; and fixed-width regions are not wrapped. That function will pass
  13275. ;; through to `fill-paragraph' when appropriate.
  13276. (set (make-local-variable 'fill-paragraph-function) 'org-fill-paragraph)
  13277. ;; Adaptive filling: To get full control, first make sure that
  13278. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13279. (set (make-local-variable 'adaptive-fill-regexp) "\000")
  13280. (set (make-local-variable 'adaptive-fill-function)
  13281. 'org-adaptive-fill-function))
  13282. (defun org-fill-paragraph (&optional justify)
  13283. "Re-align a table, pass through to fill-paragraph if no table."
  13284. (let ((table-p (org-at-table-p))
  13285. (table.el-p (org-at-table.el-p)))
  13286. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  13287. (table.el-p t) ; skip table.el tables
  13288. (table-p (org-table-align) t) ; align org-mode tables
  13289. (t nil)))) ; call paragraph-fill
  13290. ;; For reference, this is the default value of adaptive-fill-regexp
  13291. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13292. (defun org-adaptive-fill-function ()
  13293. "Return a fill prefix for org-mode files.
  13294. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13295. work correctly."
  13296. (if (looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?")
  13297. (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  13298. ;; Functions needed for Emacs/XEmacs region compatibility
  13299. (defun org-add-hook (hook function &optional append local)
  13300. "Add-hook, compatible with both Emacsen."
  13301. (if (and local (featurep 'xemacs))
  13302. (add-local-hook hook function append)
  13303. (add-hook hook function append local)))
  13304. (defun org-region-active-p ()
  13305. "Is `transient-mark-mode' on and the region active?
  13306. Works on both Emacs and XEmacs."
  13307. (if org-ignore-region
  13308. nil
  13309. (if (featurep 'xemacs)
  13310. (and zmacs-regions (region-active-p))
  13311. (and transient-mark-mode mark-active))))
  13312. (defun org-add-to-invisibility-spec (arg)
  13313. "Add elements to `buffer-invisibility-spec'.
  13314. See documentation for `buffer-invisibility-spec' for the kind of elements
  13315. that can be added."
  13316. (cond
  13317. ((fboundp 'add-to-invisibility-spec)
  13318. (add-to-invisibility-spec arg))
  13319. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  13320. (setq buffer-invisibility-spec (list arg)))
  13321. (t
  13322. (setq buffer-invisibility-spec
  13323. (cons arg buffer-invisibility-spec)))))
  13324. (defun org-remove-from-invisibility-spec (arg)
  13325. "Remove elements from `buffer-invisibility-spec'."
  13326. (if (fboundp 'remove-from-invisibility-spec)
  13327. (remove-from-invisibility-spec arg)
  13328. (if (consp buffer-invisibility-spec)
  13329. (setq buffer-invisibility-spec
  13330. (delete arg buffer-invisibility-spec)))))
  13331. (defun org-in-invisibility-spec-p (arg)
  13332. "Is ARG a member of `buffer-invisibility-spec'?"
  13333. (if (consp buffer-invisibility-spec)
  13334. (member arg buffer-invisibility-spec)
  13335. nil))
  13336. (defun org-image-file-name-regexp ()
  13337. "Return regexp matching the file names of images."
  13338. (if (fboundp 'image-file-name-regexp)
  13339. (image-file-name-regexp)
  13340. (let ((image-file-name-extensions
  13341. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13342. "xbm" "xpm" "pbm" "pgm" "ppm")))
  13343. (concat "\\."
  13344. (regexp-opt (nconc (mapcar 'upcase
  13345. image-file-name-extensions)
  13346. image-file-name-extensions)
  13347. t)
  13348. "\\'"))))
  13349. ;; Functions needed for compatibility with old outline.el.
  13350. ;; Programming for the old outline.el (that uses selective display
  13351. ;; instead of `invisible' text properties) is a nightmare, mostly
  13352. ;; because regular expressions can no longer be anchored at
  13353. ;; beginning/end of line. Therefore a number of function need special
  13354. ;; treatment when the old outline.el is being used.
  13355. ;; The following functions capture almost the entire compatibility code
  13356. ;; between the different versions of outline-mode. The only other
  13357. ;; places where this is important are the font-lock-keywords, and in
  13358. ;; `org-export-visible'. Search for `org-noutline-p' to find them.
  13359. ;; C-a should go to the beginning of a *visible* line, also in the
  13360. ;; new outline.el. I guess this should be patched into Emacs?
  13361. (defun org-beginning-of-line ()
  13362. "Go to the beginning of the current line. If that is invisible, continue
  13363. to a visible line beginning. This makes the function of C-a more intuitive."
  13364. (interactive)
  13365. (beginning-of-line 1)
  13366. (if (bobp)
  13367. nil
  13368. (backward-char 1)
  13369. (if (org-invisible-p)
  13370. (while (and (not (bobp)) (org-invisible-p))
  13371. (backward-char 1)
  13372. (beginning-of-line 1))
  13373. (forward-char 1))))
  13374. (when org-noutline-p
  13375. (define-key org-mode-map "\C-a" 'org-beginning-of-line))
  13376. ;; FIXME: should I use substitute-key-definition to reach other bindings
  13377. ;; of beginning-of-line?
  13378. (defun org-invisible-p ()
  13379. "Check if point is at a character currently not visible."
  13380. (if org-noutline-p
  13381. ;; Early versions of noutline don't have `outline-invisible-p'.
  13382. (if (fboundp 'outline-invisible-p)
  13383. (outline-invisible-p)
  13384. (get-char-property (point) 'invisible))
  13385. (save-excursion
  13386. (skip-chars-backward "^\r\n")
  13387. (equal (char-before) ?\r))))
  13388. (defun org-invisible-p2 ()
  13389. "Check if point is at a character currently not visible."
  13390. (save-excursion
  13391. (if org-noutline-p
  13392. (progn
  13393. (if (and (eolp) (not (bobp))) (backward-char 1))
  13394. ;; Early versions of noutline don't have `outline-invisible-p'.
  13395. (if (fboundp 'outline-invisible-p)
  13396. (outline-invisible-p)
  13397. (get-char-property (point) 'invisible)))
  13398. (skip-chars-backward "^\r\n")
  13399. (equal (char-before) ?\r))))
  13400. (defun org-back-to-heading (&optional invisible-ok)
  13401. "Move to previous heading line, or beg of this line if it's a heading.
  13402. Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
  13403. (if org-noutline-p
  13404. (outline-back-to-heading invisible-ok)
  13405. (if (and (or (bobp) (memq (char-before) '(?\n ?\r)))
  13406. (looking-at outline-regexp))
  13407. t
  13408. (if (re-search-backward (concat (if invisible-ok "\\([\r\n]\\|^\\)" "^")
  13409. outline-regexp)
  13410. nil t)
  13411. (if invisible-ok
  13412. (progn (goto-char (or (match-end 1) (match-beginning 0)))
  13413. (looking-at outline-regexp)))
  13414. (error "Before first heading")))))
  13415. (defun org-on-heading-p (&optional invisible-ok)
  13416. "Return t if point is on a (visible) heading line.
  13417. If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
  13418. (if org-noutline-p
  13419. (outline-on-heading-p 'invisible-ok)
  13420. (save-excursion
  13421. (skip-chars-backward "^\n\r")
  13422. (and (looking-at outline-regexp)
  13423. (or invisible-ok
  13424. (bobp)
  13425. (equal (char-before) ?\n))))))
  13426. (defun org-on-target-p ()
  13427. (let ((pos (point)))
  13428. (save-excursion
  13429. (skip-chars-forward "<")
  13430. (and (re-search-backward "<<" nil t)
  13431. (or (looking-at org-radio-target-regexp)
  13432. (looking-at org-target-regexp))
  13433. (<= (match-beginning 0) pos)
  13434. (>= (1+ (match-end 0)) pos)))))
  13435. (defun org-up-heading-all (arg)
  13436. "Move to the heading line of which the present line is a subheading.
  13437. This function considers both visible and invisible heading lines.
  13438. With argument, move up ARG levels."
  13439. (if org-noutline-p
  13440. (if (fboundp 'outline-up-heading-all)
  13441. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13442. (outline-up-heading arg t)) ; emacs 22 version of outline.el
  13443. (org-back-to-heading t)
  13444. (looking-at outline-regexp)
  13445. (if (<= (- (match-end 0) (match-beginning 0)) arg)
  13446. (error "Cannot move up %d levels" arg)
  13447. (re-search-backward
  13448. (concat "[\n\r]" (regexp-quote
  13449. (make-string (- (match-end 0) (match-beginning 0) arg)
  13450. ?*))
  13451. "[^*]"))
  13452. (forward-char 1))))
  13453. (defun org-show-hidden-entry ()
  13454. "Show an entry where even the heading is hidden."
  13455. (save-excursion
  13456. (if (not org-noutline-p)
  13457. (progn
  13458. (org-back-to-heading t)
  13459. (org-flag-heading nil)))
  13460. (org-show-entry)))
  13461. (defun org-check-occur-regexp (regexp)
  13462. "If REGEXP starts with \"^\", modify it to check for \\r as well.
  13463. Of course, only for the old outline mode."
  13464. (if org-noutline-p
  13465. regexp
  13466. (if (string-match "^\\^" regexp)
  13467. (concat "[\n\r]" (substring regexp 1))
  13468. regexp)))
  13469. (defun org-flag-heading (flag &optional entry)
  13470. "Flag the current heading. FLAG non-nil means make invisible.
  13471. When ENTRY is non-nil, show the entire entry."
  13472. (save-excursion
  13473. (org-back-to-heading t)
  13474. (if (not org-noutline-p)
  13475. ;; Make the current headline visible
  13476. (outline-flag-region (max 1 (1- (point))) (point) (if flag ?\r ?\n)))
  13477. ;; Check if we should show the entire entry
  13478. (if entry
  13479. (progn
  13480. (org-show-entry)
  13481. (save-excursion
  13482. (and (outline-next-heading)
  13483. (org-flag-heading nil))))
  13484. (outline-flag-region (max 1 (1- (point)))
  13485. (save-excursion (outline-end-of-heading) (point))
  13486. (if org-noutline-p
  13487. flag
  13488. (if flag ?\r ?\n))))))
  13489. (defun org-end-of-subtree (&optional invisible-OK)
  13490. ;; This is an exact copy of the original function, but it uses
  13491. ;; `org-back-to-heading', to make it work also in invisible
  13492. ;; trees. And is uses an invisible-OK argument.
  13493. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13494. (org-back-to-heading invisible-OK)
  13495. (let ((first t)
  13496. (level (funcall outline-level)))
  13497. (while (and (not (eobp))
  13498. (or first (> (funcall outline-level) level)))
  13499. (setq first nil)
  13500. (outline-next-heading))
  13501. (if (memq (preceding-char) '(?\n ?\^M))
  13502. (progn
  13503. ;; Go to end of line before heading
  13504. (forward-char -1)
  13505. (if (memq (preceding-char) '(?\n ?\^M))
  13506. ;; leave blank line before heading
  13507. (forward-char -1))))))
  13508. (defun org-show-subtree ()
  13509. "Show everything after this heading at deeper levels."
  13510. (outline-flag-region
  13511. (point)
  13512. (save-excursion
  13513. (outline-end-of-subtree) (outline-next-heading) (point))
  13514. (if org-noutline-p nil ?\n)))
  13515. (defun org-show-entry ()
  13516. "Show the body directly following this heading.
  13517. Show the heading too, if it is currently invisible."
  13518. (interactive)
  13519. (save-excursion
  13520. (org-back-to-heading t)
  13521. (outline-flag-region
  13522. (max 1 (1- (point)))
  13523. (save-excursion
  13524. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13525. (or (match-beginning 1) (point-max)))
  13526. (if org-noutline-p nil ?\n))))
  13527. (defun org-make-options-regexp (kwds)
  13528. "Make a regular expression for keyword lines."
  13529. (concat
  13530. (if org-noutline-p "^" "[\n\r]")
  13531. "#?[ \t]*\\+\\("
  13532. (mapconcat 'regexp-quote kwds "\\|")
  13533. "\\):[ \t]*"
  13534. (if org-noutline-p "\\(.+\\)" "\\([^\n\r]+\\)")))
  13535. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13536. (eval-after-load "bookmark"
  13537. '(if (boundp 'bookmark-after-jump-hook)
  13538. ;; We can use the hook
  13539. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13540. ;; Hook not available, use advice
  13541. (defadvice bookmark-jump (after org-make-visible activate)
  13542. "Make the position visible."
  13543. (org-bookmark-jump-unhide))))
  13544. (defun org-bookmark-jump-unhide ()
  13545. "Unhide the current position, to show the bookmark location."
  13546. (and (eq major-mode 'org-mode)
  13547. (or (org-invisible-p)
  13548. (save-excursion (goto-char (max (point-min) (1- (point))))
  13549. (org-invisible-p)))
  13550. (org-show-hierarchy-above)))
  13551. ;;; Finish up
  13552. (provide 'org)
  13553. (run-hooks 'org-load-hook)
  13554. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13555. ;;; org.el ends here