org.el 524 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 6.04a
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. ;;;; Customization variables
  84. ;;; Version
  85. (defconst org-version "6.04a"
  86. "The version number of the file org.el.")
  87. (defun org-version (&optional here)
  88. "Show the org-mode version in the echo area.
  89. With prefix arg HERE, insert it at point."
  90. (interactive "P")
  91. (let ((version (format "Org-mode version %s" org-version)))
  92. (message version)
  93. (if here
  94. (insert version))))
  95. ;;; Compatibility constants
  96. ;;; The custom variables
  97. (defgroup org nil
  98. "Outline-based notes management and organizer."
  99. :tag "Org"
  100. :group 'outlines
  101. :group 'hypermedia
  102. :group 'calendar)
  103. (defcustom org-load-hook nil
  104. "Hook that is run after org.el has been loaded."
  105. :group 'org
  106. :type 'hook)
  107. (defvar org-modules) ; defined below
  108. (defvar org-modules-loaded nil
  109. "Have the modules been loaded already?")
  110. (defun org-load-modules-maybe (&optional force)
  111. "Load all extensions listed in `org-default-extensions'."
  112. (when (or force (not org-modules-loaded))
  113. (mapc (lambda (ext)
  114. (condition-case nil (require ext)
  115. (error (message "Problems while trying to load feature `%s'" ext))))
  116. org-modules)
  117. (setq org-modules-loaded t)))
  118. (defun org-set-modules (var value)
  119. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  120. (set var value)
  121. (when (featurep 'org)
  122. (org-load-modules-maybe 'force)))
  123. (when (org-bound-and-true-p org-modules)
  124. (let ((a (member 'org-infojs org-modules)))
  125. (and a (setcar a 'org-jsinfo))))
  126. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-wl)
  127. "Modules that should always be loaded together with org.el.
  128. If a description starts with <C>, the file is not part of Emacs
  129. and loading it will require that you have downloaded and properly installed
  130. the org-mode distribution.
  131. You can also use this system to load external packages (i.e. neither Org
  132. core modules, not modules from the CONTRIB directory). Just add symbols
  133. to the end of the list. If the package is called org-xyz.el, then you need
  134. to add the symbol `xyz', and the package must have a call to
  135. (provide 'org-xyz)"
  136. :group 'org
  137. :set 'org-set-modules
  138. :type
  139. '(set :greedy t
  140. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  141. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  142. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  143. (const :tag " id: Global id's for identifying entries" org-id)
  144. (const :tag " info: Links to Info nodes" org-info)
  145. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  146. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  147. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  148. (const :tag " mew Links to Mew folders/messages" org-mew)
  149. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  150. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  151. (const :tag " vm: Links to VM folders/messages" org-vm)
  152. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  153. (const :tag " mouse: Additional mouse support" org-mouse)
  154. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  155. (const :tag "C annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
  156. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  157. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  158. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  159. (const :tag "C eval: Include command output as text" org-eval)
  160. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  161. (const :tag "C id: Global id's for identifying entries" org-id)
  162. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  163. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  164. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  165. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  166. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  167. (const :tag "C registry: A registry for Org links" org-registry)
  168. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  169. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  170. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  171. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  172. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  173. (defgroup org-startup nil
  174. "Options concerning startup of Org-mode."
  175. :tag "Org Startup"
  176. :group 'org)
  177. (defcustom org-startup-folded t
  178. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  179. This can also be configured on a per-file basis by adding one of
  180. the following lines anywhere in the buffer:
  181. #+STARTUP: fold
  182. #+STARTUP: nofold
  183. #+STARTUP: content"
  184. :group 'org-startup
  185. :type '(choice
  186. (const :tag "nofold: show all" nil)
  187. (const :tag "fold: overview" t)
  188. (const :tag "content: all headlines" content)))
  189. (defcustom org-startup-truncated t
  190. "Non-nil means, entering Org-mode will set `truncate-lines'.
  191. This is useful since some lines containing links can be very long and
  192. uninteresting. Also tables look terrible when wrapped."
  193. :group 'org-startup
  194. :type 'boolean)
  195. (defcustom org-startup-indented nil
  196. "Non-nil means, turn on `org-indent-mode' on startup.
  197. This can also be configured on a per-file basis by adding one of
  198. the following lines anywhere in the buffer:
  199. #+STARTUP: localindent
  200. #+STARTUP: indent
  201. #+STARTUP: noindent"
  202. :group 'org-structure
  203. :type '(choice
  204. (const :tag "Not" nil)
  205. (const :tag "Locally" local)
  206. (const :tag "Globally (slow on startup in large files)" t)))
  207. (defcustom org-startup-align-all-tables nil
  208. "Non-nil means, align all tables when visiting a file.
  209. This is useful when the column width in tables is forced with <N> cookies
  210. in table fields. Such tables will look correct only after the first re-align.
  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: align
  214. #+STARTUP: noalign"
  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-replace-disputed-keys nil
  227. "Non-nil means use alternative key bindings for some keys.
  228. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  229. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  230. If you want to use Org-mode together with one of these other modes,
  231. or more generally if you would like to move some Org-mode commands to
  232. other keys, set this variable and configure the keys with the variable
  233. `org-disputed-keys'.
  234. This option is only relevant at load-time of Org-mode, and must be set
  235. *before* org.el is loaded. Changing it requires a restart of Emacs to
  236. become effective."
  237. :group 'org-startup
  238. :type 'boolean)
  239. (if (fboundp 'defvaralias)
  240. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  241. (defcustom org-disputed-keys
  242. '(([(shift up)] . [(meta p)])
  243. ([(shift down)] . [(meta n)])
  244. ([(shift left)] . [(meta -)])
  245. ([(shift right)] . [(meta +)])
  246. ([(control shift right)] . [(meta shift +)])
  247. ([(control shift left)] . [(meta shift -)]))
  248. "Keys for which Org-mode and other modes compete.
  249. This is an alist, cars are the default keys, second element specifies
  250. the alternative to use when `org-replace-disputed-keys' is t.
  251. Keys can be specified in any syntax supported by `define-key'.
  252. The value of this option takes effect only at Org-mode's startup,
  253. therefore you'll have to restart Emacs to apply it after changing."
  254. :group 'org-startup
  255. :type 'alist)
  256. (defun org-key (key)
  257. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  258. Or return the original if not disputed."
  259. (if org-replace-disputed-keys
  260. (let* ((nkey (key-description key))
  261. (x (org-find-if (lambda (x)
  262. (equal (key-description (car x)) nkey))
  263. org-disputed-keys)))
  264. (if x (cdr x) key))
  265. key))
  266. (defun org-find-if (predicate seq)
  267. (catch 'exit
  268. (while seq
  269. (if (funcall predicate (car seq))
  270. (throw 'exit (car seq))
  271. (pop seq)))))
  272. (defun org-defkey (keymap key def)
  273. "Define a key, possibly translated, as returned by `org-key'."
  274. (define-key keymap (org-key key) def))
  275. (defcustom org-ellipsis nil
  276. "The ellipsis to use in the Org-mode outline.
  277. When nil, just use the standard three dots. When a string, use that instead,
  278. When a face, use the standart 3 dots, but with the specified face.
  279. The change affects only Org-mode (which will then use its own display table).
  280. Changing this requires executing `M-x org-mode' in a buffer to become
  281. effective."
  282. :group 'org-startup
  283. :type '(choice (const :tag "Default" nil)
  284. (face :tag "Face" :value org-warning)
  285. (string :tag "String" :value "...#")))
  286. (defvar org-display-table nil
  287. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  288. (defgroup org-keywords nil
  289. "Keywords in Org-mode."
  290. :tag "Org Keywords"
  291. :group 'org)
  292. (defcustom org-deadline-string "DEADLINE:"
  293. "String to mark deadline entries.
  294. A deadline is this string, followed by a time stamp. Should be a word,
  295. terminated by a colon. You can insert a schedule keyword and
  296. a timestamp with \\[org-deadline].
  297. Changes become only effective after restarting Emacs."
  298. :group 'org-keywords
  299. :type 'string)
  300. (defcustom org-scheduled-string "SCHEDULED:"
  301. "String to mark scheduled TODO entries.
  302. A schedule is this string, followed by a time stamp. Should be a word,
  303. terminated by a colon. You can insert a schedule keyword and
  304. a timestamp with \\[org-schedule].
  305. Changes become only effective after restarting Emacs."
  306. :group 'org-keywords
  307. :type 'string)
  308. (defcustom org-closed-string "CLOSED:"
  309. "String used as the prefix for timestamps logging closing a TODO entry."
  310. :group 'org-keywords
  311. :type 'string)
  312. (defcustom org-clock-string "CLOCK:"
  313. "String used as prefix for timestamps clocking work hours on an item."
  314. :group 'org-keywords
  315. :type 'string)
  316. (defcustom org-comment-string "COMMENT"
  317. "Entries starting with this keyword will never be exported.
  318. An entry can be toggled between COMMENT and normal with
  319. \\[org-toggle-comment].
  320. Changes become only effective after restarting Emacs."
  321. :group 'org-keywords
  322. :type 'string)
  323. (defcustom org-quote-string "QUOTE"
  324. "Entries starting with this keyword will be exported in fixed-width font.
  325. Quoting applies only to the text in the entry following the headline, and does
  326. not extend beyond the next headline, even if that is lower level.
  327. An entry can be toggled between QUOTE and normal with
  328. \\[org-toggle-fixed-width-section]."
  329. :group 'org-keywords
  330. :type 'string)
  331. (defconst org-repeat-re
  332. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  333. "Regular expression for specifying repeated events.
  334. After a match, group 1 contains the repeat expression.")
  335. (defgroup org-structure nil
  336. "Options concerning the general structure of Org-mode files."
  337. :tag "Org Structure"
  338. :group 'org)
  339. (defgroup org-reveal-location nil
  340. "Options about how to make context of a location visible."
  341. :tag "Org Reveal Location"
  342. :group 'org-structure)
  343. (defconst org-context-choice
  344. '(choice
  345. (const :tag "Always" t)
  346. (const :tag "Never" nil)
  347. (repeat :greedy t :tag "Individual contexts"
  348. (cons
  349. (choice :tag "Context"
  350. (const agenda)
  351. (const org-goto)
  352. (const occur-tree)
  353. (const tags-tree)
  354. (const link-search)
  355. (const mark-goto)
  356. (const bookmark-jump)
  357. (const isearch)
  358. (const default))
  359. (boolean))))
  360. "Contexts for the reveal options.")
  361. (defcustom org-show-hierarchy-above '((default . t))
  362. "Non-nil means, show full hierarchy when revealing a location.
  363. Org-mode often shows locations in an org-mode file which might have
  364. been invisible before. When this is set, the hierarchy of headings
  365. above the exposed location is shown.
  366. Turning this off for example for sparse trees makes them very compact.
  367. Instead of t, this can also be an alist specifying this option for different
  368. contexts. Valid contexts are
  369. agenda when exposing an entry from the agenda
  370. org-goto when using the command `org-goto' on key C-c C-j
  371. occur-tree when using the command `org-occur' on key C-c /
  372. tags-tree when constructing a sparse tree based on tags matches
  373. link-search when exposing search matches associated with a link
  374. mark-goto when exposing the jump goal of a mark
  375. bookmark-jump when exposing a bookmark location
  376. isearch when exiting from an incremental search
  377. default default for all contexts not set explicitly"
  378. :group 'org-reveal-location
  379. :type org-context-choice)
  380. (defcustom org-show-following-heading '((default . nil))
  381. "Non-nil means, show following heading when revealing a location.
  382. Org-mode often shows locations in an org-mode file which might have
  383. been invisible before. When this is set, the heading following the
  384. match is shown.
  385. Turning this off for example for sparse trees makes them very compact,
  386. but makes it harder to edit the location of the match. In such a case,
  387. use the command \\[org-reveal] to show more context.
  388. Instead of t, this can also be an alist specifying this option for different
  389. contexts. See `org-show-hierarchy-above' for valid contexts."
  390. :group 'org-reveal-location
  391. :type org-context-choice)
  392. (defcustom org-show-siblings '((default . nil) (isearch t))
  393. "Non-nil means, show all sibling heading when revealing a location.
  394. Org-mode often shows locations in an org-mode file which might have
  395. been invisible before. When this is set, the sibling of the current entry
  396. heading are all made visible. If `org-show-hierarchy-above' is t,
  397. the same happens on each level of the hierarchy above the current entry.
  398. By default this is on for the isearch context, off for all other contexts.
  399. Turning this off for example for sparse trees makes them very compact,
  400. but makes it harder to edit the location of the match. In such a case,
  401. use the command \\[org-reveal] to show more context.
  402. Instead of t, this can also be an alist specifying this option for different
  403. contexts. See `org-show-hierarchy-above' for valid contexts."
  404. :group 'org-reveal-location
  405. :type org-context-choice)
  406. (defcustom org-show-entry-below '((default . nil))
  407. "Non-nil means, show the entry below a headline when revealing a location.
  408. Org-mode often shows locations in an org-mode file which might have
  409. been invisible before. When this is set, the text below the headline that is
  410. exposed is also shown.
  411. By default this is off for all contexts.
  412. Instead of t, this can also be an alist specifying this option for different
  413. contexts. See `org-show-hierarchy-above' for valid contexts."
  414. :group 'org-reveal-location
  415. :type org-context-choice)
  416. (defcustom org-indirect-buffer-display 'other-window
  417. "How should indirect tree buffers be displayed?
  418. This applies to indirect buffers created with the commands
  419. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  420. Valid values are:
  421. current-window Display in the current window
  422. other-window Just display in another window.
  423. dedicated-frame Create one new frame, and re-use it each time.
  424. new-frame Make a new frame each time. Note that in this case
  425. previously-made indirect buffers are kept, and you need to
  426. kill these buffers yourself."
  427. :group 'org-structure
  428. :group 'org-agenda-windows
  429. :type '(choice
  430. (const :tag "In current window" current-window)
  431. (const :tag "In current frame, other window" other-window)
  432. (const :tag "Each time a new frame" new-frame)
  433. (const :tag "One dedicated frame" dedicated-frame)))
  434. (defgroup org-cycle nil
  435. "Options concerning visibility cycling in Org-mode."
  436. :tag "Org Cycle"
  437. :group 'org-structure)
  438. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  439. "Names of drawers. Drawers are not opened by cycling on the headline above.
  440. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  441. this:
  442. :DRAWERNAME:
  443. .....
  444. :END:
  445. The drawer \"PROPERTIES\" is special for capturing properties through
  446. the property API.
  447. Drawers can be defined on the per-file basis with a line like:
  448. #+DRAWERS: HIDDEN STATE PROPERTIES"
  449. :group 'org-structure
  450. :type '(repeat (string :tag "Drawer Name")))
  451. (defcustom org-cycle-global-at-bob nil
  452. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  453. This makes it possible to do global cycling without having to use S-TAB or
  454. C-u TAB. For this special case to work, the first line of the buffer
  455. must not be a headline - it may be empty ot some other text. When used in
  456. this way, `org-cycle-hook' is disables temporarily, to make sure the
  457. cursor stays at the beginning of the buffer.
  458. When this option is nil, don't do anything special at the beginning
  459. of the buffer."
  460. :group 'org-cycle
  461. :type 'boolean)
  462. (defcustom org-cycle-emulate-tab t
  463. "Where should `org-cycle' emulate TAB.
  464. nil Never
  465. white Only in completely white lines
  466. whitestart Only at the beginning of lines, before the first non-white char
  467. t Everywhere except in headlines
  468. exc-hl-bol Everywhere except at the start of a headline
  469. If TAB is used in a place where it does not emulate TAB, the current subtree
  470. visibility is cycled."
  471. :group 'org-cycle
  472. :type '(choice (const :tag "Never" nil)
  473. (const :tag "Only in completely white lines" white)
  474. (const :tag "Before first char in a line" whitestart)
  475. (const :tag "Everywhere except in headlines" t)
  476. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  477. ))
  478. (defcustom org-cycle-separator-lines 2
  479. "Number of empty lines needed to keep an empty line between collapsed trees.
  480. If you leave an empty line between the end of a subtree and the following
  481. headline, this empty line is hidden when the subtree is folded.
  482. Org-mode will leave (exactly) one empty line visible if the number of
  483. empty lines is equal or larger to the number given in this variable.
  484. So the default 2 means, at least 2 empty lines after the end of a subtree
  485. are needed to produce free space between a collapsed subtree and the
  486. following headline.
  487. Special case: when 0, never leave empty lines in collapsed view."
  488. :group 'org-cycle
  489. :type 'integer)
  490. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  491. org-cycle-hide-drawers
  492. org-cycle-show-empty-lines
  493. org-optimize-window-after-visibility-change)
  494. "Hook that is run after `org-cycle' has changed the buffer visibility.
  495. The function(s) in this hook must accept a single argument which indicates
  496. the new state that was set by the most recent `org-cycle' command. The
  497. argument is a symbol. After a global state change, it can have the values
  498. `overview', `content', or `all'. After a local state change, it can have
  499. the values `folded', `children', or `subtree'."
  500. :group 'org-cycle
  501. :type 'hook)
  502. (defgroup org-edit-structure nil
  503. "Options concerning structure editing in Org-mode."
  504. :tag "Org Edit Structure"
  505. :group 'org-structure)
  506. (defcustom org-odd-levels-only nil
  507. "Non-nil means, skip even levels and only use odd levels for the outline.
  508. This has the effect that two stars are being added/taken away in
  509. promotion/demotion commands. It also influences how levels are
  510. handled by the exporters.
  511. Changing it requires restart of `font-lock-mode' to become effective
  512. for fontification also in regions already fontified.
  513. You may also set this on a per-file basis by adding one of the following
  514. lines to the buffer:
  515. #+STARTUP: odd
  516. #+STARTUP: oddeven"
  517. :group 'org-edit-structure
  518. :group 'org-font-lock
  519. :type 'boolean)
  520. (defcustom org-adapt-indentation t
  521. "Non-nil means, adapt indentation when promoting and demoting.
  522. When this is set and the *entire* text in an entry is indented, the
  523. indentation is increased by one space in a demotion command, and
  524. decreased by one in a promotion command. If any line in the entry
  525. body starts at column 0, indentation is not changed at all."
  526. :group 'org-edit-structure
  527. :type 'boolean)
  528. (defcustom org-special-ctrl-a/e nil
  529. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  530. When t, `C-a' will bring back the cursor to the beginning of the
  531. headline text, i.e. after the stars and after a possible TODO keyword.
  532. In an item, this will be the position after the bullet.
  533. When the cursor is already at that position, another `C-a' will bring
  534. it to the beginning of the line.
  535. `C-e' will jump to the end of the headline, ignoring the presence of tags
  536. in the headline. A second `C-e' will then jump to the true end of the
  537. line, after any tags.
  538. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  539. and only a directly following, identical keypress will bring the cursor
  540. to the special positions."
  541. :group 'org-edit-structure
  542. :type '(choice
  543. (const :tag "off" nil)
  544. (const :tag "after bullet first" t)
  545. (const :tag "border first" reversed)))
  546. (if (fboundp 'defvaralias)
  547. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  548. (defcustom org-special-ctrl-k nil
  549. "Non-nil means `C-k' will behave specially in headlines.
  550. When nil, `C-k' will call the default `kill-line' command.
  551. When t, the following will happen while the cursor is in the headline:
  552. - When the cursor is at the beginning of a headline, kill the entire
  553. line and possible the folded subtree below the line.
  554. - When in the middle of the headline text, kill the headline up to the tags.
  555. - When after the headline text, kill the tags."
  556. :group 'org-edit-structure
  557. :type 'boolean)
  558. (defcustom org-M-RET-may-split-line '((default . t))
  559. "Non-nil means, M-RET will split the line at the cursor position.
  560. When nil, it will go to the end of the line before making a
  561. new line.
  562. You may also set this option in a different way for different
  563. contexts. Valid contexts are:
  564. headline when creating a new headline
  565. item when creating a new item
  566. table in a table field
  567. default the value to be used for all contexts not explicitly
  568. customized"
  569. :group 'org-structure
  570. :group 'org-table
  571. :type '(choice
  572. (const :tag "Always" t)
  573. (const :tag "Never" nil)
  574. (repeat :greedy t :tag "Individual contexts"
  575. (cons
  576. (choice :tag "Context"
  577. (const headline)
  578. (const item)
  579. (const table)
  580. (const default))
  581. (boolean)))))
  582. (defcustom org-blank-before-new-entry '((heading . nil)
  583. (plain-list-item . nil))
  584. "Should `org-insert-heading' leave a blank line before new heading/item?
  585. The value is an alist, with `heading' and `plain-list-item' as car,
  586. and a boolean flag as cdr."
  587. :group 'org-edit-structure
  588. :type '(list
  589. (cons (const heading) (boolean))
  590. (cons (const plain-list-item) (boolean))))
  591. (defcustom org-insert-heading-hook nil
  592. "Hook being run after inserting a new heading."
  593. :group 'org-edit-structure
  594. :type 'hook)
  595. (defcustom org-enable-fixed-width-editor t
  596. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  597. This currently only means, they are never auto-wrapped.
  598. When nil, such lines will be treated like ordinary lines.
  599. See also the QUOTE keyword."
  600. :group 'org-edit-structure
  601. :type 'boolean)
  602. (defcustom org-goto-auto-isearch t
  603. "Non-nil means, typing characters in org-goto starts incremental search."
  604. :group 'org-edit-structure
  605. :type 'boolean)
  606. (defgroup org-sparse-trees nil
  607. "Options concerning sparse trees in Org-mode."
  608. :tag "Org Sparse Trees"
  609. :group 'org-structure)
  610. (defcustom org-highlight-sparse-tree-matches t
  611. "Non-nil means, highlight all matches that define a sparse tree.
  612. The highlights will automatically disappear the next time the buffer is
  613. changed by an edit command."
  614. :group 'org-sparse-trees
  615. :type 'boolean)
  616. (defcustom org-remove-highlights-with-change t
  617. "Non-nil means, any change to the buffer will remove temporary highlights.
  618. Such highlights are created by `org-occur' and `org-clock-display'.
  619. When nil, `C-c C-c needs to be used to get rid of the highlights.
  620. The highlights created by `org-preview-latex-fragment' always need
  621. `C-c C-c' to be removed."
  622. :group 'org-sparse-trees
  623. :group 'org-time
  624. :type 'boolean)
  625. (defcustom org-occur-hook '(org-first-headline-recenter)
  626. "Hook that is run after `org-occur' has constructed a sparse tree.
  627. This can be used to recenter the window to show as much of the structure
  628. as possible."
  629. :group 'org-sparse-trees
  630. :type 'hook)
  631. (defgroup org-plain-lists nil
  632. "Options concerning plain lists in Org-mode."
  633. :tag "Org Plain lists"
  634. :group 'org-structure)
  635. (defcustom org-cycle-include-plain-lists nil
  636. "Non-nil means, include plain lists into visibility cycling.
  637. This means that during cycling, plain list items will *temporarily* be
  638. interpreted as outline headlines with a level given by 1000+i where i is the
  639. indentation of the bullet. In all other operations, plain list items are
  640. not seen as headlines. For example, you cannot assign a TODO keyword to
  641. such an item."
  642. :group 'org-plain-lists
  643. :type 'boolean)
  644. (defcustom org-plain-list-ordered-item-terminator t
  645. "The character that makes a line with leading number an ordered list item.
  646. Valid values are ?. and ?\). To get both terminators, use t. While
  647. ?. may look nicer, it creates the danger that a line with leading
  648. number may be incorrectly interpreted as an item. ?\) therefore is
  649. the safe choice."
  650. :group 'org-plain-lists
  651. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  652. (const :tag "paren like in \"2)\"" ?\))
  653. (const :tab "both" t)))
  654. (defcustom org-empty-line-terminates-plain-lists nil
  655. "Non-nil means, an empty line ends all plain list levels.
  656. When nil, empty lines are part of the preceeding item."
  657. :group 'org-plain-lists
  658. :type 'boolean)
  659. (defcustom org-auto-renumber-ordered-lists t
  660. "Non-nil means, automatically renumber ordered plain lists.
  661. Renumbering happens when the sequence have been changed with
  662. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  663. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  664. :group 'org-plain-lists
  665. :type 'boolean)
  666. (defcustom org-provide-checkbox-statistics t
  667. "Non-nil means, update checkbox statistics after insert and toggle.
  668. When this is set, checkbox statistics is updated each time you either insert
  669. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  670. with \\[org-ctrl-c-ctrl-c\\]."
  671. :group 'org-plain-lists
  672. :type 'boolean)
  673. (defcustom org-description-max-indent 20
  674. "Maximum indentation for the second line of a description list.
  675. When the indentation would be larger than this, it will become
  676. 5 characters instead."
  677. :group 'org-plain-lists
  678. :type 'integer)
  679. (defgroup org-imenu-and-speedbar nil
  680. "Options concerning imenu and speedbar in Org-mode."
  681. :tag "Org Imenu and Speedbar"
  682. :group 'org-structure)
  683. (defcustom org-imenu-depth 2
  684. "The maximum level for Imenu access to Org-mode headlines.
  685. This also applied for speedbar access."
  686. :group 'org-imenu-and-speedbar
  687. :type 'number)
  688. (defgroup org-table nil
  689. "Options concerning tables in Org-mode."
  690. :tag "Org Table"
  691. :group 'org)
  692. (defcustom org-enable-table-editor 'optimized
  693. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  694. When nil, such lines will be treated like ordinary lines.
  695. When equal to the symbol `optimized', the table editor will be optimized to
  696. do the following:
  697. - Automatic overwrite mode in front of whitespace in table fields.
  698. This makes the structure of the table stay in tact as long as the edited
  699. field does not exceed the column width.
  700. - Minimize the number of realigns. Normally, the table is aligned each time
  701. TAB or RET are pressed to move to another field. With optimization this
  702. happens only if changes to a field might have changed the column width.
  703. Optimization requires replacing the functions `self-insert-command',
  704. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  705. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  706. very good at guessing when a re-align will be necessary, but you can always
  707. force one with \\[org-ctrl-c-ctrl-c].
  708. If you would like to use the optimized version in Org-mode, but the
  709. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  710. This variable can be used to turn on and off the table editor during a session,
  711. but in order to toggle optimization, a restart is required.
  712. See also the variable `org-table-auto-blank-field'."
  713. :group 'org-table
  714. :type '(choice
  715. (const :tag "off" nil)
  716. (const :tag "on" t)
  717. (const :tag "on, optimized" optimized)))
  718. (defcustom org-table-tab-recognizes-table.el t
  719. "Non-nil means, TAB will automatically notice a table.el table.
  720. When it sees such a table, it moves point into it and - if necessary -
  721. calls `table-recognize-table'."
  722. :group 'org-table-editing
  723. :type 'boolean)
  724. (defgroup org-link nil
  725. "Options concerning links in Org-mode."
  726. :tag "Org Link"
  727. :group 'org)
  728. (defvar org-link-abbrev-alist-local nil
  729. "Buffer-local version of `org-link-abbrev-alist', which see.
  730. The value of this is taken from the #+LINK lines.")
  731. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  732. (defcustom org-link-abbrev-alist nil
  733. "Alist of link abbreviations.
  734. The car of each element is a string, to be replaced at the start of a link.
  735. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  736. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  737. [[linkkey:tag][description]]
  738. If REPLACE is a string, the tag will simply be appended to create the link.
  739. If the string contains \"%s\", the tag will be inserted there.
  740. REPLACE may also be a function that will be called with the tag as the
  741. only argument to create the link, which should be returned as a string.
  742. See the manual for examples."
  743. :group 'org-link
  744. :type 'alist)
  745. (defcustom org-descriptive-links t
  746. "Non-nil means, hide link part and only show description of bracket links.
  747. Bracket links are like [[link][descritpion]]. This variable sets the initial
  748. state in new org-mode buffers. The setting can then be toggled on a
  749. per-buffer basis from the Org->Hyperlinks menu."
  750. :group 'org-link
  751. :type 'boolean)
  752. (defcustom org-link-file-path-type 'adaptive
  753. "How the path name in file links should be stored.
  754. Valid values are:
  755. relative Relative to the current directory, i.e. the directory of the file
  756. into which the link is being inserted.
  757. absolute Absolute path, if possible with ~ for home directory.
  758. noabbrev Absolute path, no abbreviation of home directory.
  759. adaptive Use relative path for files in the current directory and sub-
  760. directories of it. For other files, use an absolute path."
  761. :group 'org-link
  762. :type '(choice
  763. (const relative)
  764. (const absolute)
  765. (const noabbrev)
  766. (const adaptive)))
  767. (defcustom org-activate-links '(bracket angle plain radio tag date)
  768. "Types of links that should be activated in Org-mode files.
  769. This is a list of symbols, each leading to the activation of a certain link
  770. type. In principle, it does not hurt to turn on most link types - there may
  771. be a small gain when turning off unused link types. The types are:
  772. bracket The recommended [[link][description]] or [[link]] links with hiding.
  773. angular Links in angular brackes that may contain whitespace like
  774. <bbdb:Carsten Dominik>.
  775. plain Plain links in normal text, no whitespace, like http://google.com.
  776. radio Text that is matched by a radio target, see manual for details.
  777. tag Tag settings in a headline (link to tag search).
  778. date Time stamps (link to calendar).
  779. Changing this variable requires a restart of Emacs to become effective."
  780. :group 'org-link
  781. :type '(set (const :tag "Double bracket links (new style)" bracket)
  782. (const :tag "Angular bracket links (old style)" angular)
  783. (const :tag "Plain text links" plain)
  784. (const :tag "Radio target matches" radio)
  785. (const :tag "Tags" tag)
  786. (const :tag "Timestamps" date)))
  787. (defcustom org-make-link-description-function nil
  788. "Function to use to generate link descriptions from links. If
  789. nil the link location will be used. This function must take two
  790. parameters; the first is the link and the second the description
  791. org-insert-link has generated, and should return the description
  792. to use."
  793. :group 'org-link
  794. :type 'function)
  795. (defgroup org-link-store nil
  796. "Options concerning storing links in Org-mode."
  797. :tag "Org Store Link"
  798. :group 'org-link)
  799. (defcustom org-email-link-description-format "Email %c: %.30s"
  800. "Format of the description part of a link to an email or usenet message.
  801. The following %-excapes will be replaced by corresponding information:
  802. %F full \"From\" field
  803. %f name, taken from \"From\" field, address if no name
  804. %T full \"To\" field
  805. %t first name in \"To\" field, address if no name
  806. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  807. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  808. %s subject
  809. %m message-id.
  810. You may use normal field width specification between the % and the letter.
  811. This is for example useful to limit the length of the subject.
  812. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  813. :group 'org-link-store
  814. :type 'string)
  815. (defcustom org-from-is-user-regexp
  816. (let (r1 r2)
  817. (when (and user-mail-address (not (string= user-mail-address "")))
  818. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  819. (when (and user-full-name (not (string= user-full-name "")))
  820. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  821. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  822. "Regexp mached against the \"From:\" header of an email or usenet message.
  823. It should match if the message is from the user him/herself."
  824. :group 'org-link-store
  825. :type 'regexp)
  826. (defcustom org-context-in-file-links t
  827. "Non-nil means, file links from `org-store-link' contain context.
  828. A search string will be added to the file name with :: as separator and
  829. used to find the context when the link is activated by the command
  830. `org-open-at-point'.
  831. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  832. negates this setting for the duration of the command."
  833. :group 'org-link-store
  834. :type 'boolean)
  835. (defcustom org-keep-stored-link-after-insertion nil
  836. "Non-nil means, keep link in list for entire session.
  837. The command `org-store-link' adds a link pointing to the current
  838. location to an internal list. These links accumulate during a session.
  839. The command `org-insert-link' can be used to insert links into any
  840. Org-mode file (offering completion for all stored links). When this
  841. option is nil, every link which has been inserted once using \\[org-insert-link]
  842. will be removed from the list, to make completing the unused links
  843. more efficient."
  844. :group 'org-link-store
  845. :type 'boolean)
  846. (defgroup org-link-follow nil
  847. "Options concerning following links in Org-mode."
  848. :tag "Org Follow Link"
  849. :group 'org-link)
  850. (defcustom org-follow-link-hook nil
  851. "Hook that is run after a link has been followed."
  852. :group 'org-link-follow
  853. :type 'hook)
  854. (defcustom org-tab-follows-link nil
  855. "Non-nil means, on links TAB will follow the link.
  856. Needs to be set before org.el is loaded."
  857. :group 'org-link-follow
  858. :type 'boolean)
  859. (defcustom org-return-follows-link nil
  860. "Non-nil means, on links RET will follow the link.
  861. Needs to be set before org.el is loaded."
  862. :group 'org-link-follow
  863. :type 'boolean)
  864. (defcustom org-mouse-1-follows-link
  865. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  866. "Non-nil means, mouse-1 on a link will follow the link.
  867. A longer mouse click will still set point. Does not work on XEmacs.
  868. Needs to be set before org.el is loaded."
  869. :group 'org-link-follow
  870. :type 'boolean)
  871. (defcustom org-mark-ring-length 4
  872. "Number of different positions to be recorded in the ring
  873. Changing this requires a restart of Emacs to work correctly."
  874. :group 'org-link-follow
  875. :type 'interger)
  876. (defcustom org-link-frame-setup
  877. '((vm . vm-visit-folder-other-frame)
  878. (gnus . gnus-other-frame)
  879. (file . find-file-other-window))
  880. "Setup the frame configuration for following links.
  881. When following a link with Emacs, it may often be useful to display
  882. this link in another window or frame. This variable can be used to
  883. set this up for the different types of links.
  884. For VM, use any of
  885. `vm-visit-folder'
  886. `vm-visit-folder-other-frame'
  887. For Gnus, use any of
  888. `gnus'
  889. `gnus-other-frame'
  890. For FILE, use any of
  891. `find-file'
  892. `find-file-other-window'
  893. `find-file-other-frame'
  894. For the calendar, use the variable `calendar-setup'.
  895. For BBDB, it is currently only possible to display the matches in
  896. another window."
  897. :group 'org-link-follow
  898. :type '(list
  899. (cons (const vm)
  900. (choice
  901. (const vm-visit-folder)
  902. (const vm-visit-folder-other-window)
  903. (const vm-visit-folder-other-frame)))
  904. (cons (const gnus)
  905. (choice
  906. (const gnus)
  907. (const gnus-other-frame)))
  908. (cons (const file)
  909. (choice
  910. (const find-file)
  911. (const find-file-other-window)
  912. (const find-file-other-frame)))))
  913. (defcustom org-display-internal-link-with-indirect-buffer nil
  914. "Non-nil means, use indirect buffer to display infile links.
  915. Activating internal links (from one location in a file to another location
  916. in the same file) normally just jumps to the location. When the link is
  917. activated with a C-u prefix (or with mouse-3), the link is displayed in
  918. another window. When this option is set, the other window actually displays
  919. an indirect buffer clone of the current buffer, to avoid any visibility
  920. changes to the current buffer."
  921. :group 'org-link-follow
  922. :type 'boolean)
  923. (defcustom org-open-non-existing-files nil
  924. "Non-nil means, `org-open-file' will open non-existing files.
  925. When nil, an error will be generated."
  926. :group 'org-link-follow
  927. :type 'boolean)
  928. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  929. "Function and arguments to call for following mailto links.
  930. This is a list with the first element being a lisp function, and the
  931. remaining elements being arguments to the function. In string arguments,
  932. %a will be replaced by the address, and %s will be replaced by the subject
  933. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  934. :group 'org-link-follow
  935. :type '(choice
  936. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  937. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  938. (const :tag "message-mail" (message-mail "%a" "%s"))
  939. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  940. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  941. "Non-nil means, ask for confirmation before executing shell links.
  942. Shell links can be dangerous: just think about a link
  943. [[shell:rm -rf ~/*][Google Search]]
  944. This link would show up in your Org-mode document as \"Google Search\",
  945. but really it would remove your entire home directory.
  946. Therefore we advise against setting this variable to nil.
  947. Just change it to `y-or-n-p' of you want to confirm with a
  948. single keystroke rather than having to type \"yes\"."
  949. :group 'org-link-follow
  950. :type '(choice
  951. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  952. (const :tag "with y-or-n (faster)" y-or-n-p)
  953. (const :tag "no confirmation (dangerous)" nil)))
  954. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  955. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  956. Elisp links can be dangerous: just think about a link
  957. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  958. This link would show up in your Org-mode document as \"Google Search\",
  959. but really it would remove your entire home directory.
  960. Therefore we advise against setting this variable to nil.
  961. Just change it to `y-or-n-p' of you want to confirm with a
  962. single keystroke rather than having to type \"yes\"."
  963. :group 'org-link-follow
  964. :type '(choice
  965. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  966. (const :tag "with y-or-n (faster)" y-or-n-p)
  967. (const :tag "no confirmation (dangerous)" nil)))
  968. (defconst org-file-apps-defaults-gnu
  969. '((remote . emacs)
  970. (t . mailcap))
  971. "Default file applications on a UNIX or GNU/Linux system.
  972. See `org-file-apps'.")
  973. (defconst org-file-apps-defaults-macosx
  974. '((remote . emacs)
  975. (t . "open %s")
  976. ("ps" . "gv %s")
  977. ("ps.gz" . "gv %s")
  978. ("eps" . "gv %s")
  979. ("eps.gz" . "gv %s")
  980. ("dvi" . "xdvi %s")
  981. ("fig" . "xfig %s"))
  982. "Default file applications on a MacOS X system.
  983. The system \"open\" is known as a default, but we use X11 applications
  984. for some files for which the OS does not have a good default.
  985. See `org-file-apps'.")
  986. (defconst org-file-apps-defaults-windowsnt
  987. (list
  988. '(remote . emacs)
  989. (cons t
  990. (list (if (featurep 'xemacs)
  991. 'mswindows-shell-execute
  992. 'w32-shell-execute)
  993. "open" 'file)))
  994. "Default file applications on a Windows NT system.
  995. The system \"open\" is used for most files.
  996. See `org-file-apps'.")
  997. (defcustom org-file-apps
  998. '(
  999. ("txt" . emacs)
  1000. ("tex" . emacs)
  1001. ("ltx" . emacs)
  1002. ("org" . emacs)
  1003. ("el" . emacs)
  1004. ("bib" . emacs)
  1005. )
  1006. "External applications for opening `file:path' items in a document.
  1007. Org-mode uses system defaults for different file types, but
  1008. you can use this variable to set the application for a given file
  1009. extension. The entries in this list are cons cells where the car identifies
  1010. files and the cdr the corresponding command. Possible values for the
  1011. file identifier are
  1012. \"ext\" A string identifying an extension
  1013. `directory' Matches a directory
  1014. `remote' Matches a remote file, accessible through tramp or efs.
  1015. Remote files most likely should be visited through Emacs
  1016. because external applications cannot handle such paths.
  1017. t Default for all remaining files
  1018. Possible values for the command are:
  1019. `emacs' The file will be visited by the current Emacs process.
  1020. `default' Use the default application for this file type.
  1021. string A command to be executed by a shell; %s will be replaced
  1022. by the path to the file.
  1023. sexp A Lisp form which will be evaluated. The file path will
  1024. be available in the Lisp variable `file'.
  1025. For more examples, see the system specific constants
  1026. `org-file-apps-defaults-macosx'
  1027. `org-file-apps-defaults-windowsnt'
  1028. `org-file-apps-defaults-gnu'."
  1029. :group 'org-link-follow
  1030. :type '(repeat
  1031. (cons (choice :value ""
  1032. (string :tag "Extension")
  1033. (const :tag "Default for unrecognized files" t)
  1034. (const :tag "Remote file" remote)
  1035. (const :tag "Links to a directory" directory))
  1036. (choice :value ""
  1037. (const :tag "Visit with Emacs" emacs)
  1038. (const :tag "Use system default" default)
  1039. (string :tag "Command")
  1040. (sexp :tag "Lisp form")))))
  1041. (defgroup org-refile nil
  1042. "Options concerning refiling entries in Org-mode."
  1043. :tag "Org Remember"
  1044. :group 'org)
  1045. (defcustom org-directory "~/org"
  1046. "Directory with org files.
  1047. This directory will be used as default to prompt for org files.
  1048. Used by the hooks for remember.el."
  1049. :group 'org-refile
  1050. :group 'org-remember
  1051. :type 'directory)
  1052. (defcustom org-default-notes-file "~/.notes"
  1053. "Default target for storing notes.
  1054. Used by the hooks for remember.el. This can be a string, or nil to mean
  1055. the value of `remember-data-file'.
  1056. You can set this on a per-template basis with the variable
  1057. `org-remember-templates'."
  1058. :group 'org-refile
  1059. :group 'org-remember
  1060. :type '(choice
  1061. (const :tag "Default from remember-data-file" nil)
  1062. file))
  1063. (defcustom org-goto-interface 'outline
  1064. "The default interface to be used for `org-goto'.
  1065. Allowed vaues are:
  1066. outline The interface shows an outline of the relevant file
  1067. and the correct heading is found by moving through
  1068. the outline or by searching with incremental search.
  1069. outline-path-completion Headlines in the current buffer are offered via
  1070. completion."
  1071. :group 'org-refile
  1072. :type '(choice
  1073. (const :tag "Outline" outline)
  1074. (const :tag "Outline-path-completion" outline-path-completion)))
  1075. (defcustom org-reverse-note-order nil
  1076. "Non-nil means, store new notes at the beginning of a file or entry.
  1077. When nil, new notes will be filed to the end of a file or entry.
  1078. This can also be a list with cons cells of regular expressions that
  1079. are matched against file names, and values."
  1080. :group 'org-remember
  1081. :type '(choice
  1082. (const :tag "Reverse always" t)
  1083. (const :tag "Reverse never" nil)
  1084. (repeat :tag "By file name regexp"
  1085. (cons regexp boolean))))
  1086. (defcustom org-refile-targets nil
  1087. "Targets for refiling entries with \\[org-refile].
  1088. This is list of cons cells. Each cell contains:
  1089. - a specification of the files to be considered, either a list of files,
  1090. or a symbol whose function or variable value will be used to retrieve
  1091. a file name or a list of file names. Nil means, refile to a different
  1092. heading in the current buffer.
  1093. - A specification of how to find candidate refile targets. This may be
  1094. any of
  1095. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1096. This tag has to be present in all target headlines, inheritance will
  1097. not be considered.
  1098. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1099. todo keyword.
  1100. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1101. headlines that are refiling targets.
  1102. - a cons cell (:level . N). Any headline of level N is considered a target.
  1103. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1104. :group 'org-remember
  1105. :type '(repeat
  1106. (cons
  1107. (choice :value org-agenda-files
  1108. (const :tag "All agenda files" org-agenda-files)
  1109. (const :tag "Current buffer" nil)
  1110. (function) (variable) (file))
  1111. (choice :tag "Identify target headline by"
  1112. (cons :tag "Specific tag" (const :tag) (string))
  1113. (cons :tag "TODO keyword" (const :todo) (string))
  1114. (cons :tag "Regular expression" (const :regexp) (regexp))
  1115. (cons :tag "Level number" (const :level) (integer))
  1116. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1117. (defcustom org-refile-use-outline-path nil
  1118. "Non-nil means, provide refile targets as paths.
  1119. So a level 3 headline will be available as level1/level2/level3.
  1120. When the value is `file', also include the file name (without directory)
  1121. into the path. When `full-file-path', include the full file path."
  1122. :group 'org-remember
  1123. :type '(choice
  1124. (const :tag "Not" nil)
  1125. (const :tag "Yes" t)
  1126. (const :tag "Start with file name" file)
  1127. (const :tag "Start with full file path" full-file-path)))
  1128. (defgroup org-todo nil
  1129. "Options concerning TODO items in Org-mode."
  1130. :tag "Org TODO"
  1131. :group 'org)
  1132. (defgroup org-progress nil
  1133. "Options concerning Progress logging in Org-mode."
  1134. :tag "Org Progress"
  1135. :group 'org-time)
  1136. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1137. "List of TODO entry keyword sequences and their interpretation.
  1138. \\<org-mode-map>This is a list of sequences.
  1139. Each sequence starts with a symbol, either `sequence' or `type',
  1140. indicating if the keywords should be interpreted as a sequence of
  1141. action steps, or as different types of TODO items. The first
  1142. keywords are states requiring action - these states will select a headline
  1143. for inclusion into the global TODO list Org-mode produces. If one of
  1144. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1145. signify that no further action is necessary. If \"|\" is not found,
  1146. the last keyword is treated as the only DONE state of the sequence.
  1147. The command \\[org-todo] cycles an entry through these states, and one
  1148. additional state where no keyword is present. For details about this
  1149. cycling, see the manual.
  1150. TODO keywords and interpretation can also be set on a per-file basis with
  1151. the special #+SEQ_TODO and #+TYP_TODO lines.
  1152. Each keyword can optionally specify a character for fast state selection
  1153. \(in combination with the variable `org-use-fast-todo-selection')
  1154. and specifiers for state change logging, using the same syntax
  1155. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1156. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1157. indicates to record a time stamp each time this state is selected.
  1158. Each keyword may also specify if a timestamp or a note should be
  1159. recorded when entering or leaving the state, by adding additional
  1160. characters in the parenthesis after the keyword. This looks like this:
  1161. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1162. record only the time of the state change. With X and Y being either
  1163. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1164. Y when leaving the state if and only if the *target* state does not
  1165. define X. You may omit any of the fast-selection key or X or /Y,
  1166. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1167. For backward compatibility, this variable may also be just a list
  1168. of keywords - in this case the interptetation (sequence or type) will be
  1169. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1170. :group 'org-todo
  1171. :group 'org-keywords
  1172. :type '(choice
  1173. (repeat :tag "Old syntax, just keywords"
  1174. (string :tag "Keyword"))
  1175. (repeat :tag "New syntax"
  1176. (cons
  1177. (choice
  1178. :tag "Interpretation"
  1179. (const :tag "Sequence (cycling hits every state)" sequence)
  1180. (const :tag "Type (cycling directly to DONE)" type))
  1181. (repeat
  1182. (string :tag "Keyword"))))))
  1183. (defvar org-todo-keywords-1 nil
  1184. "All TODO and DONE keywords active in a buffer.")
  1185. (make-variable-buffer-local 'org-todo-keywords-1)
  1186. (defvar org-todo-keywords-for-agenda nil)
  1187. (defvar org-done-keywords-for-agenda nil)
  1188. (defvar org-agenda-contributing-files nil)
  1189. (defvar org-not-done-keywords nil)
  1190. (make-variable-buffer-local 'org-not-done-keywords)
  1191. (defvar org-done-keywords nil)
  1192. (make-variable-buffer-local 'org-done-keywords)
  1193. (defvar org-todo-heads nil)
  1194. (make-variable-buffer-local 'org-todo-heads)
  1195. (defvar org-todo-sets nil)
  1196. (make-variable-buffer-local 'org-todo-sets)
  1197. (defvar org-todo-log-states nil)
  1198. (make-variable-buffer-local 'org-todo-log-states)
  1199. (defvar org-todo-kwd-alist nil)
  1200. (make-variable-buffer-local 'org-todo-kwd-alist)
  1201. (defvar org-todo-key-alist nil)
  1202. (make-variable-buffer-local 'org-todo-key-alist)
  1203. (defvar org-todo-key-trigger nil)
  1204. (make-variable-buffer-local 'org-todo-key-trigger)
  1205. (defcustom org-todo-interpretation 'sequence
  1206. "Controls how TODO keywords are interpreted.
  1207. This variable is in principle obsolete and is only used for
  1208. backward compatibility, if the interpretation of todo keywords is
  1209. not given already in `org-todo-keywords'. See that variable for
  1210. more information."
  1211. :group 'org-todo
  1212. :group 'org-keywords
  1213. :type '(choice (const sequence)
  1214. (const type)))
  1215. (defcustom org-use-fast-todo-selection 'prefix
  1216. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1217. This variable describes if and under what circumstances the cycling
  1218. mechanism for TODO keywords will be replaced by a single-key, direct
  1219. selection scheme.
  1220. When nil, fast selection is never used.
  1221. When the symbol `prefix', it will be used when `org-todo' is called with
  1222. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1223. in an agenda buffer.
  1224. When t, fast selection is used by default. In this case, the prefix
  1225. argument forces cycling instead.
  1226. In all cases, the special interface is only used if access keys have actually
  1227. been assigned by the user, i.e. if keywords in the configuration are followed
  1228. by a letter in parenthesis, like TODO(t)."
  1229. :group 'org-todo
  1230. :type '(choice
  1231. (const :tag "Never" nil)
  1232. (const :tag "By default" t)
  1233. (const :tag "Only with C-u C-c C-t" prefix)))
  1234. (defcustom org-provide-todo-statistics t
  1235. "Non-nil means, update todo statistics after insert and toggle.
  1236. When this is set, todo statistics is updated in the parent of the current
  1237. entry each time a todo state is changed."
  1238. :group 'org-todo
  1239. :type 'boolean)
  1240. (defcustom org-after-todo-state-change-hook nil
  1241. "Hook which is run after the state of a TODO item was changed.
  1242. The new state (a string with a TODO keyword, or nil) is available in the
  1243. Lisp variable `state'."
  1244. :group 'org-todo
  1245. :type 'hook)
  1246. (defcustom org-log-done nil
  1247. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1248. When equal to the list (done), also prompt for a closing note.
  1249. This can also be configured on a per-file basis by adding one of
  1250. the following lines anywhere in the buffer:
  1251. #+STARTUP: logdone
  1252. #+STARTUP: lognotedone
  1253. #+STARTUP: nologdone"
  1254. :group 'org-todo
  1255. :group 'org-progress
  1256. :type '(choice
  1257. (const :tag "No logging" nil)
  1258. (const :tag "Record CLOSED timestamp" time)
  1259. (const :tag "Record CLOSED timestamp with closing note." note)))
  1260. ;; Normalize old uses of org-log-done.
  1261. (cond
  1262. ((eq org-log-done t) (setq org-log-done 'time))
  1263. ((and (listp org-log-done) (memq 'done org-log-done))
  1264. (setq org-log-done 'note)))
  1265. (defcustom org-log-note-clock-out nil
  1266. "Non-nil means, recored a note when clocking out of an item.
  1267. This can also be configured on a per-file basis by adding one of
  1268. the following lines anywhere in the buffer:
  1269. #+STARTUP: lognoteclock-out
  1270. #+STARTUP: nolognoteclock-out"
  1271. :group 'org-todo
  1272. :group 'org-progress
  1273. :type 'boolean)
  1274. (defcustom org-log-done-with-time t
  1275. "Non-nil means, the CLOSED time stamp will contain date and time.
  1276. When nil, only the date will be recorded."
  1277. :group 'org-progress
  1278. :type 'boolean)
  1279. (defcustom org-log-note-headings
  1280. '((done . "CLOSING NOTE %t")
  1281. (state . "State %-12s %t")
  1282. (note . "Note taken on %t")
  1283. (clock-out . ""))
  1284. "Headings for notes added to entries.
  1285. The value is an alist, with the car being a symbol indicating the note
  1286. context, and the cdr is the heading to be used. The heading may also be the
  1287. empty string.
  1288. %t in the heading will be replaced by a time stamp.
  1289. %s will be replaced by the new TODO state, in double quotes.
  1290. %u will be replaced by the user name.
  1291. %U will be replaced by the full user name."
  1292. :group 'org-todo
  1293. :group 'org-progress
  1294. :type '(list :greedy t
  1295. (cons (const :tag "Heading when closing an item" done) string)
  1296. (cons (const :tag
  1297. "Heading when changing todo state (todo sequence only)"
  1298. state) string)
  1299. (cons (const :tag "Heading when just taking a note" note) string)
  1300. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1301. (unless (assq 'note org-log-note-headings)
  1302. (push '(note . "%t") org-log-note-headings))
  1303. (defcustom org-log-states-order-reversed t
  1304. "Non-nil means, the latest state change note will be directly after heading.
  1305. When nil, the notes will be orderer according to time."
  1306. :group 'org-todo
  1307. :group 'org-progress
  1308. :type 'boolean)
  1309. (defcustom org-log-repeat 'time
  1310. "Non-nil means, record moving through the DONE state when triggering repeat.
  1311. An auto-repeating tasks is immediately switched back to TODO when marked
  1312. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1313. the TODO keyword definition, or recording a closing note by setting
  1314. `org-log-done', there will be no record of the task moving through DONE.
  1315. This variable forces taking a note anyway. Possible values are:
  1316. nil Don't force a record
  1317. time Record a time stamp
  1318. note Record a note
  1319. This option can also be set with on a per-file-basis with
  1320. #+STARTUP: logrepeat
  1321. #+STARTUP: lognoterepeat
  1322. #+STARTUP: nologrepeat
  1323. You can have local logging settings for a subtree by setting the LOGGING
  1324. property to one or more of these keywords."
  1325. :group 'org-todo
  1326. :group 'org-progress
  1327. :type '(choice
  1328. (const :tag "Don't force a record" nil)
  1329. (const :tag "Force recording the DONE state" time)
  1330. (const :tag "Force recording a note with the DONE state" note)))
  1331. (defgroup org-priorities nil
  1332. "Priorities in Org-mode."
  1333. :tag "Org Priorities"
  1334. :group 'org-todo)
  1335. (defcustom org-highest-priority ?A
  1336. "The highest priority of TODO items. A character like ?A, ?B etc.
  1337. Must have a smaller ASCII number than `org-lowest-priority'."
  1338. :group 'org-priorities
  1339. :type 'character)
  1340. (defcustom org-lowest-priority ?C
  1341. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1342. Must have a larger ASCII number than `org-highest-priority'."
  1343. :group 'org-priorities
  1344. :type 'character)
  1345. (defcustom org-default-priority ?B
  1346. "The default priority of TODO items.
  1347. This is the priority an item get if no explicit priority is given."
  1348. :group 'org-priorities
  1349. :type 'character)
  1350. (defcustom org-priority-start-cycle-with-default t
  1351. "Non-nil means, start with default priority when starting to cycle.
  1352. When this is nil, the first step in the cycle will be (depending on the
  1353. command used) one higher or lower that the default priority."
  1354. :group 'org-priorities
  1355. :type 'boolean)
  1356. (defgroup org-time nil
  1357. "Options concerning time stamps and deadlines in Org-mode."
  1358. :tag "Org Time"
  1359. :group 'org)
  1360. (defcustom org-insert-labeled-timestamps-at-point nil
  1361. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1362. When nil, these labeled time stamps are forces into the second line of an
  1363. entry, just after the headline. When scheduling from the global TODO list,
  1364. the time stamp will always be forced into the second line."
  1365. :group 'org-time
  1366. :type 'boolean)
  1367. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1368. "Formats for `format-time-string' which are used for time stamps.
  1369. It is not recommended to change this constant.")
  1370. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1371. "Number of minutes to round time stamps to.
  1372. These are two values, the first applies when first creating a time stamp.
  1373. The second applies when changing it with the commands `S-up' and `S-down'.
  1374. When changing the time stamp, this means that it will change in steps
  1375. of N minutes, as given by the second value.
  1376. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1377. numbers should be factors of 60, so for example 5, 10, 15.
  1378. When this is larger than 1, you can still force an exact time-stamp by using
  1379. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1380. and by using a prefix arg to `S-up/down' to specify the exact number
  1381. of minutes to shift."
  1382. :group 'org-time
  1383. :get '(lambda (var) ; Make sure all entries have 5 elements
  1384. (if (integerp (default-value var))
  1385. (list (default-value var) 5)
  1386. (default-value var)))
  1387. :type '(list
  1388. (integer :tag "when inserting times")
  1389. (integer :tag "when modifying times")))
  1390. ;; Normalize old customizations of this variable.
  1391. (when (integerp org-time-stamp-rounding-minutes)
  1392. (setq org-time-stamp-rounding-minutes
  1393. (list org-time-stamp-rounding-minutes
  1394. org-time-stamp-rounding-minutes)))
  1395. (defcustom org-display-custom-times nil
  1396. "Non-nil means, overlay custom formats over all time stamps.
  1397. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1398. To turn this on on a per-file basis, insert anywhere in the file:
  1399. #+STARTUP: customtime"
  1400. :group 'org-time
  1401. :set 'set-default
  1402. :type 'sexp)
  1403. (make-variable-buffer-local 'org-display-custom-times)
  1404. (defcustom org-time-stamp-custom-formats
  1405. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1406. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1407. These are overlayed over the default ISO format if the variable
  1408. `org-display-custom-times' is set. Time like %H:%M should be at the
  1409. end of the second format."
  1410. :group 'org-time
  1411. :type 'sexp)
  1412. (defun org-time-stamp-format (&optional long inactive)
  1413. "Get the right format for a time string."
  1414. (let ((f (if long (cdr org-time-stamp-formats)
  1415. (car org-time-stamp-formats))))
  1416. (if inactive
  1417. (concat "[" (substring f 1 -1) "]")
  1418. f)))
  1419. (defcustom org-time-clocksum-format "%d:%02d"
  1420. "The format string used when creating CLOCKSUM lines, or when
  1421. org-mode generates a time duration."
  1422. :group 'org-time
  1423. :type 'string)
  1424. (defcustom org-deadline-warning-days 14
  1425. "No. of days before expiration during which a deadline becomes active.
  1426. This variable governs the display in sparse trees and in the agenda.
  1427. When 0 or negative, it means use this number (the absolute value of it)
  1428. even if a deadline has a different individual lead time specified."
  1429. :group 'org-time
  1430. :group 'org-agenda-daily/weekly
  1431. :type 'number)
  1432. (defcustom org-read-date-prefer-future t
  1433. "Non-nil means, assume future for incomplete date input from user.
  1434. This affects the following situations:
  1435. 1. The user gives a day, but no month.
  1436. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1437. read this as the third of *next* month. However, if you enter \"17\",
  1438. it will be considered as *this* month.
  1439. 2. The user gives a month but not a year.
  1440. For example, if it is april and you enter \"feb 2\", this will be read
  1441. as feb 2, *next* year. \"May 5\", however, will be this year.
  1442. Currently this does not work for ISO week specifications.
  1443. When this option is nil, the current month and year will always be used
  1444. as defaults."
  1445. :group 'org-time
  1446. :type 'boolean)
  1447. (defcustom org-read-date-display-live t
  1448. "Non-nil means, display current interpretation of date prompt live.
  1449. This display will be in an overlay, in the minibuffer."
  1450. :group 'org-time
  1451. :type 'boolean)
  1452. (defcustom org-read-date-popup-calendar t
  1453. "Non-nil means, pop up a calendar when prompting for a date.
  1454. In the calendar, the date can be selected with mouse-1. However, the
  1455. minibuffer will also be active, and you can simply enter the date as well.
  1456. When nil, only the minibuffer will be available."
  1457. :group 'org-time
  1458. :type 'boolean)
  1459. (if (fboundp 'defvaralias)
  1460. (defvaralias 'org-popup-calendar-for-date-prompt
  1461. 'org-read-date-popup-calendar))
  1462. (defcustom org-extend-today-until 0
  1463. "The hour when your day really ends.
  1464. This has influence for the following applications:
  1465. - When switching the agenda to \"today\". It it is still earlier than
  1466. the time given here, the day recognized as TODAY is actually yesterday.
  1467. - When a date is read from the user and it is still before the time given
  1468. here, the current date and time will be assumed to be yesterday, 23:59.
  1469. FIXME:
  1470. IMPORTANT: This is still a very experimental feature, it may disappear
  1471. again or it may be extended to mean more things."
  1472. :group 'org-time
  1473. :type 'number)
  1474. (defcustom org-edit-timestamp-down-means-later nil
  1475. "Non-nil means, S-down will increase the time in a time stamp.
  1476. When nil, S-up will increase."
  1477. :group 'org-time
  1478. :type 'boolean)
  1479. (defcustom org-calendar-follow-timestamp-change t
  1480. "Non-nil means, make the calendar window follow timestamp changes.
  1481. When a timestamp is modified and the calendar window is visible, it will be
  1482. moved to the new date."
  1483. :group 'org-time
  1484. :type 'boolean)
  1485. (defgroup org-tags nil
  1486. "Options concerning tags in Org-mode."
  1487. :tag "Org Tags"
  1488. :group 'org)
  1489. (defcustom org-tag-alist nil
  1490. "List of tags allowed in Org-mode files.
  1491. When this list is nil, Org-mode will base TAG input on what is already in the
  1492. buffer.
  1493. The value of this variable is an alist, the car of each entry must be a
  1494. keyword as a string, the cdr may be a character that is used to select
  1495. that tag through the fast-tag-selection interface.
  1496. See the manual for details."
  1497. :group 'org-tags
  1498. :type '(repeat
  1499. (choice
  1500. (cons (string :tag "Tag name")
  1501. (character :tag "Access char"))
  1502. (const :tag "Start radio group" (:startgroup))
  1503. (const :tag "End radio group" (:endgroup)))))
  1504. (defvar org-file-tags nil
  1505. "List of tags that can be inherited by all entries in the file.
  1506. The tags will be inherited if the variable `org-use-tag-inheritance'
  1507. says they should be.
  1508. This variable is populated from #+TAG lines.")
  1509. (defcustom org-use-fast-tag-selection 'auto
  1510. "Non-nil means, use fast tag selection scheme.
  1511. This is a special interface to select and deselect tags with single keys.
  1512. When nil, fast selection is never used.
  1513. When the symbol `auto', fast selection is used if and only if selection
  1514. characters for tags have been configured, either through the variable
  1515. `org-tag-alist' or through a #+TAGS line in the buffer.
  1516. When t, fast selection is always used and selection keys are assigned
  1517. automatically if necessary."
  1518. :group 'org-tags
  1519. :type '(choice
  1520. (const :tag "Always" t)
  1521. (const :tag "Never" nil)
  1522. (const :tag "When selection characters are configured" 'auto)))
  1523. (defcustom org-fast-tag-selection-single-key nil
  1524. "Non-nil means, fast tag selection exits after first change.
  1525. When nil, you have to press RET to exit it.
  1526. During fast tag selection, you can toggle this flag with `C-c'.
  1527. This variable can also have the value `expert'. In this case, the window
  1528. displaying the tags menu is not even shown, until you press C-c again."
  1529. :group 'org-tags
  1530. :type '(choice
  1531. (const :tag "No" nil)
  1532. (const :tag "Yes" t)
  1533. (const :tag "Expert" expert)))
  1534. (defvar org-fast-tag-selection-include-todo nil
  1535. "Non-nil means, fast tags selection interface will also offer TODO states.
  1536. This is an undocumented feature, you should not rely on it.")
  1537. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1538. "The column to which tags should be indented in a headline.
  1539. If this number is positive, it specifies the column. If it is negative,
  1540. it means that the tags should be flushright to that column. For example,
  1541. -80 works well for a normal 80 character screen."
  1542. :group 'org-tags
  1543. :type 'integer)
  1544. (defcustom org-auto-align-tags t
  1545. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1546. These operations change the length of a headline and therefore shift
  1547. the tags around. With this options turned on, after each such operation
  1548. the tags are again aligned to `org-tags-column'."
  1549. :group 'org-tags
  1550. :type 'boolean)
  1551. (defcustom org-use-tag-inheritance t
  1552. "Non-nil means, tags in levels apply also for sublevels.
  1553. When nil, only the tags directly given in a specific line apply there.
  1554. If this option is t, a match early-on in a tree can lead to a large
  1555. number of matches in the subtree. If you only want to see the first
  1556. match in a tree during a search, check out the variable
  1557. `org-tags-match-list-sublevels'.
  1558. This may also be a list of tags that should be inherited, or a regexp that
  1559. matches tags that should be inherited."
  1560. :group 'org-tags
  1561. :type '(choice
  1562. (const :tag "Not" nil)
  1563. (const :tag "Always" t)
  1564. (repeat :tag "Specific tags" (string :tag "Tag"))
  1565. (regexp :tag "Tags matched by regexp")))
  1566. (defun org-tag-inherit-p (tag)
  1567. "Check if TAG is one that should be inherited."
  1568. (cond
  1569. ((eq org-use-tag-inheritance t) t)
  1570. ((not org-use-tag-inheritance) nil)
  1571. ((stringp org-use-tag-inheritance)
  1572. (string-match org-use-tag-inheritance tag))
  1573. ((listp org-use-tag-inheritance)
  1574. (member tag org-use-tag-inheritance))
  1575. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1576. (defcustom org-tags-match-list-sublevels t
  1577. "Non-nil means list also sublevels of headlines matching tag search.
  1578. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1579. the sublevels of a headline matching a tag search often also match
  1580. the same search. Listing all of them can create very long lists.
  1581. Setting this variable to nil causes subtrees of a match to be skipped.
  1582. This option is off by default, because inheritance in on. If you turn
  1583. inheritance off, you very likely want to turn this option on.
  1584. As a special case, if the tag search is restricted to TODO items, the
  1585. value of this variable is ignored and sublevels are always checked, to
  1586. make sure all corresponding TODO items find their way into the list."
  1587. :group 'org-tags
  1588. :type 'boolean)
  1589. (defvar org-tags-history nil
  1590. "History of minibuffer reads for tags.")
  1591. (defvar org-last-tags-completion-table nil
  1592. "The last used completion table for tags.")
  1593. (defvar org-after-tags-change-hook nil
  1594. "Hook that is run after the tags in a line have changed.")
  1595. (defgroup org-properties nil
  1596. "Options concerning properties in Org-mode."
  1597. :tag "Org Properties"
  1598. :group 'org)
  1599. (defcustom org-property-format "%-10s %s"
  1600. "How property key/value pairs should be formatted by `indent-line'.
  1601. When `indent-line' hits a property definition, it will format the line
  1602. according to this format, mainly to make sure that the values are
  1603. lined-up with respect to each other."
  1604. :group 'org-properties
  1605. :type 'string)
  1606. (defcustom org-use-property-inheritance nil
  1607. "Non-nil means, properties apply also for sublevels.
  1608. This setting is chiefly used during property searches. Turning it on can
  1609. cause significant overhead when doing a search, which is why it is not
  1610. on by default.
  1611. When nil, only the properties directly given in the current entry count.
  1612. When t, every property is inherited. The value may also be a list of
  1613. properties that should have inheritance, or a regular expression matching
  1614. properties that should be inherited.
  1615. However, note that some special properties use inheritance under special
  1616. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1617. and the properties ending in \"_ALL\" when they are used as descriptor
  1618. for valid values of a property.
  1619. Note for programmers:
  1620. When querying an entry with `org-entry-get', you can control if inheritance
  1621. should be used. By default, `org-entry-get' looks only at the local
  1622. properties. You can request inheritance by setting the inherit argument
  1623. to t (to force inheritance) or to `selective' (to respect the setting
  1624. in this variable)."
  1625. :group 'org-properties
  1626. :type '(choice
  1627. (const :tag "Not" nil)
  1628. (const :tag "Always" t)
  1629. (repeat :tag "Specific properties" (string :tag "Property"))
  1630. (regexp :tag "Properties matched by regexp")))
  1631. (defun org-property-inherit-p (property)
  1632. "Check if PROPERTY is one that should be inherited."
  1633. (cond
  1634. ((eq org-use-property-inheritance t) t)
  1635. ((not org-use-property-inheritance) nil)
  1636. ((stringp org-use-property-inheritance)
  1637. (string-match org-use-property-inheritance property))
  1638. ((listp org-use-property-inheritance)
  1639. (member property org-use-property-inheritance))
  1640. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1641. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1642. "The default column format, if no other format has been defined.
  1643. This variable can be set on the per-file basis by inserting a line
  1644. #+COLUMNS: %25ITEM ....."
  1645. :group 'org-properties
  1646. :type 'string)
  1647. (defcustom org-effort-property "Effort"
  1648. "The property that is being used to keep track of effort estimates.
  1649. Effort estimates given in this property need to have the format H:MM."
  1650. :group 'org-properties
  1651. :group 'org-progress
  1652. :type '(string :tag "Property"))
  1653. (defconst org-global-properties-fixed
  1654. '(("VISIBILITY_ALL" . "folded children content all"))
  1655. "List of property/value pairs that can be inherited by any entry.
  1656. These are fixed values, for the preset properties.")
  1657. (defcustom org-global-properties nil
  1658. "List of property/value pairs that can be inherited by any entry.
  1659. You can set buffer-local values for this by adding lines like
  1660. #+PROPERTY: NAME VALUE"
  1661. :group 'org-properties
  1662. :type '(repeat
  1663. (cons (string :tag "Property")
  1664. (string :tag "Value"))))
  1665. (defvar org-file-properties nil
  1666. "List of property/value pairs that can be inherited by any entry.
  1667. Valid for the current buffer.
  1668. This variable is populated from #+PROPERTY lines.")
  1669. (defgroup org-agenda nil
  1670. "Options concerning agenda views in Org-mode."
  1671. :tag "Org Agenda"
  1672. :group 'org)
  1673. (defvar org-category nil
  1674. "Variable used by org files to set a category for agenda display.
  1675. Such files should use a file variable to set it, for example
  1676. # -*- mode: org; org-category: \"ELisp\"
  1677. or contain a special line
  1678. #+CATEGORY: ELisp
  1679. If the file does not specify a category, then file's base name
  1680. is used instead.")
  1681. (make-variable-buffer-local 'org-category)
  1682. (defcustom org-agenda-files nil
  1683. "The files to be used for agenda display.
  1684. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1685. \\[org-remove-file]. You can also use customize to edit the list.
  1686. If an entry is a directory, all files in that directory that are matched by
  1687. `org-agenda-file-regexp' will be part of the file list.
  1688. If the value of the variable is not a list but a single file name, then
  1689. the list of agenda files is actually stored and maintained in that file, one
  1690. agenda file per line."
  1691. :group 'org-agenda
  1692. :type '(choice
  1693. (repeat :tag "List of files and directories" file)
  1694. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1695. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1696. "Regular expression to match files for `org-agenda-files'.
  1697. If any element in the list in that variable contains a directory instead
  1698. of a normal file, all files in that directory that are matched by this
  1699. regular expression will be included."
  1700. :group 'org-agenda
  1701. :type 'regexp)
  1702. (defcustom org-agenda-text-search-extra-files nil
  1703. "List of extra files to be searched by text search commands.
  1704. These files will be search in addition to the agenda files by the
  1705. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1706. Note that these files will only be searched for text search commands,
  1707. not for the other agenda views like todo lists, tag searches or the weekly
  1708. agenda. This variable is intended to list notes and possibly archive files
  1709. that should also be searched by these two commands.
  1710. In fact, if the first element in the list is the symbol `agenda-archives',
  1711. than all archive files of all agenda files will be added to the search
  1712. scope."
  1713. :group 'org-agenda
  1714. :type '(set :greedy t
  1715. (const :tag "Agenda Archives" agenda-archives)
  1716. (repeat :inline t (file))))
  1717. (if (fboundp 'defvaralias)
  1718. (defvaralias 'org-agenda-multi-occur-extra-files
  1719. 'org-agenda-text-search-extra-files))
  1720. (defcustom org-agenda-skip-unavailable-files nil
  1721. "t means to just skip non-reachable files in `org-agenda-files'.
  1722. Nil means to remove them, after a query, from the list."
  1723. :group 'org-agenda
  1724. :type 'boolean)
  1725. (defcustom org-calendar-to-agenda-key [?c]
  1726. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1727. The command `org-calendar-goto-agenda' will be bound to this key. The
  1728. default is the character `c' because then `c' can be used to switch back and
  1729. forth between agenda and calendar."
  1730. :group 'org-agenda
  1731. :type 'sexp)
  1732. (eval-after-load "calendar"
  1733. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1734. 'org-calendar-goto-agenda))
  1735. (defgroup org-latex nil
  1736. "Options for embedding LaTeX code into Org-mode."
  1737. :tag "Org LaTeX"
  1738. :group 'org)
  1739. (defcustom org-format-latex-options
  1740. '(:foreground default :background default :scale 1.0
  1741. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1742. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1743. "Options for creating images from LaTeX fragments.
  1744. This is a property list with the following properties:
  1745. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1746. `default' means use the foreground of the default face.
  1747. :background the background color, or \"Transparent\".
  1748. `default' means use the background of the default face.
  1749. :scale a scaling factor for the size of the images.
  1750. :html-foreground, :html-background, :html-scale
  1751. the same numbers for HTML export.
  1752. :matchers a list indicating which matchers should be used to
  1753. find LaTeX fragments. Valid members of this list are:
  1754. \"begin\" find environments
  1755. \"$\" find math expressions surrounded by $...$
  1756. \"$$\" find math expressions surrounded by $$....$$
  1757. \"\\(\" find math expressions surrounded by \\(...\\)
  1758. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1759. :group 'org-latex
  1760. :type 'plist)
  1761. (defcustom org-format-latex-header "\\documentclass{article}
  1762. \\usepackage{fullpage} % do not remove
  1763. \\usepackage{amssymb}
  1764. \\usepackage[usenames]{color}
  1765. \\usepackage{amsmath}
  1766. \\usepackage{latexsym}
  1767. \\usepackage[mathscr]{eucal}
  1768. \\pagestyle{empty} % do not remove"
  1769. "The document header used for processing LaTeX fragments."
  1770. :group 'org-latex
  1771. :type 'string)
  1772. (defgroup org-font-lock nil
  1773. "Font-lock settings for highlighting in Org-mode."
  1774. :tag "Org Font Lock"
  1775. :group 'org)
  1776. (defcustom org-level-color-stars-only nil
  1777. "Non-nil means fontify only the stars in each headline.
  1778. When nil, the entire headline is fontified.
  1779. Changing it requires restart of `font-lock-mode' to become effective
  1780. also in regions already fontified."
  1781. :group 'org-font-lock
  1782. :type 'boolean)
  1783. (defcustom org-hide-leading-stars nil
  1784. "Non-nil means, hide the first N-1 stars in a headline.
  1785. This works by using the face `org-hide' for these stars. This
  1786. face is white for a light background, and black for a dark
  1787. background. You may have to customize the face `org-hide' to
  1788. make this work.
  1789. Changing it requires restart of `font-lock-mode' to become effective
  1790. also in regions already fontified.
  1791. You may also set this on a per-file basis by adding one of the following
  1792. lines to the buffer:
  1793. #+STARTUP: hidestars
  1794. #+STARTUP: showstars"
  1795. :group 'org-font-lock
  1796. :type 'boolean)
  1797. (defcustom org-fontify-done-headline nil
  1798. "Non-nil means, change the face of a headline if it is marked DONE.
  1799. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1800. When this is non-nil, the headline after the keyword is set to the
  1801. `org-headline-done' as an additional indication."
  1802. :group 'org-font-lock
  1803. :type 'boolean)
  1804. (defcustom org-fontify-emphasized-text t
  1805. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1806. Changing this variable requires a restart of Emacs to take effect."
  1807. :group 'org-font-lock
  1808. :type 'boolean)
  1809. (defcustom org-highlight-latex-fragments-and-specials nil
  1810. "Non-nil means, fontify what is treated specially by the exporters."
  1811. :group 'org-font-lock
  1812. :type 'boolean)
  1813. (defcustom org-hide-emphasis-markers nil
  1814. "Non-nil mean font-lock should hide the emphasis marker characters."
  1815. :group 'org-font-lock
  1816. :type 'boolean)
  1817. (defvar org-emph-re nil
  1818. "Regular expression for matching emphasis.")
  1819. (defvar org-verbatim-re nil
  1820. "Regular expression for matching verbatim text.")
  1821. (defvar org-emphasis-regexp-components) ; defined just below
  1822. (defvar org-emphasis-alist) ; defined just below
  1823. (defun org-set-emph-re (var val)
  1824. "Set variable and compute the emphasis regular expression."
  1825. (set var val)
  1826. (when (and (boundp 'org-emphasis-alist)
  1827. (boundp 'org-emphasis-regexp-components)
  1828. org-emphasis-alist org-emphasis-regexp-components)
  1829. (let* ((e org-emphasis-regexp-components)
  1830. (pre (car e))
  1831. (post (nth 1 e))
  1832. (border (nth 2 e))
  1833. (body (nth 3 e))
  1834. (nl (nth 4 e))
  1835. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1836. (body1 (concat body "*?"))
  1837. (markers (mapconcat 'car org-emphasis-alist ""))
  1838. (vmarkers (mapconcat
  1839. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1840. org-emphasis-alist "")))
  1841. ;; make sure special characters appear at the right position in the class
  1842. (if (string-match "\\^" markers)
  1843. (setq markers (concat (replace-match "" t t markers) "^")))
  1844. (if (string-match "-" markers)
  1845. (setq markers (concat (replace-match "" t t markers) "-")))
  1846. (if (string-match "\\^" vmarkers)
  1847. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1848. (if (string-match "-" vmarkers)
  1849. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1850. (if (> nl 0)
  1851. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1852. (int-to-string nl) "\\}")))
  1853. ;; Make the regexp
  1854. (setq org-emph-re
  1855. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1856. "\\("
  1857. "\\([" markers "]\\)"
  1858. "\\("
  1859. "[^" border "]\\|"
  1860. "[^" border (if (and nil stacked) markers) "]"
  1861. body1
  1862. "[^" border (if (and nil stacked) markers) "]"
  1863. "\\)"
  1864. "\\3\\)"
  1865. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1866. (setq org-verbatim-re
  1867. (concat "\\([" pre "]\\|^\\)"
  1868. "\\("
  1869. "\\([" vmarkers "]\\)"
  1870. "\\("
  1871. "[^" border "]\\|"
  1872. "[^" border "]"
  1873. body1
  1874. "[^" border "]"
  1875. "\\)"
  1876. "\\3\\)"
  1877. "\\([" post "]\\|$\\)")))))
  1878. (defcustom org-emphasis-regexp-components
  1879. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1880. "Components used to build the regular expression for emphasis.
  1881. This is a list with 6 entries. Terminology: In an emphasis string
  1882. like \" *strong word* \", we call the initial space PREMATCH, the final
  1883. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1884. and \"trong wor\" is the body. The different components in this variable
  1885. specify what is allowed/forbidden in each part:
  1886. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1887. post Chars allowed as postmatch. End of line will be allowed too.
  1888. border The chars *forbidden* as border characters.
  1889. body-regexp A regexp like \".\" to match a body character. Don't use
  1890. non-shy groups here, and don't allow newline here.
  1891. newline The maximum number of newlines allowed in an emphasis exp.
  1892. Use customize to modify this, or restart Emacs after changing it."
  1893. :group 'org-font-lock
  1894. :set 'org-set-emph-re
  1895. :type '(list
  1896. (sexp :tag "Allowed chars in pre ")
  1897. (sexp :tag "Allowed chars in post ")
  1898. (sexp :tag "Forbidden chars in border ")
  1899. (sexp :tag "Regexp for body ")
  1900. (integer :tag "number of newlines allowed")
  1901. (option (boolean :tag "Stacking (DISABLED) "))))
  1902. (defcustom org-emphasis-alist
  1903. `(("*" bold "<b>" "</b>")
  1904. ("/" italic "<i>" "</i>")
  1905. ("_" underline "<u>" "</u>")
  1906. ("=" org-code "<code>" "</code>" verbatim)
  1907. ("~" org-verbatim "" "" verbatim)
  1908. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1909. "<del>" "</del>")
  1910. )
  1911. "Special syntax for emphasized text.
  1912. Text starting and ending with a special character will be emphasized, for
  1913. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1914. characters, the face to be used by font-lock for highlighting in Org-mode
  1915. Emacs buffers, and the HTML tags to be used for this.
  1916. Use customize to modify this, or restart Emacs after changing it."
  1917. :group 'org-font-lock
  1918. :set 'org-set-emph-re
  1919. :type '(repeat
  1920. (list
  1921. (string :tag "Marker character")
  1922. (choice
  1923. (face :tag "Font-lock-face")
  1924. (plist :tag "Face property list"))
  1925. (string :tag "HTML start tag")
  1926. (string :tag "HTML end tag")
  1927. (option (const verbatim)))))
  1928. ;;; Miscellaneous options
  1929. (defgroup org-completion nil
  1930. "Completion in Org-mode."
  1931. :tag "Org Completion"
  1932. :group 'org)
  1933. (defcustom org-completion-fallback-command 'hippie-expand
  1934. "The expansion command called by \\[org-complete] in normal context.
  1935. Normal means, no org-mode-specific context."
  1936. :group 'org-completion
  1937. :type 'function)
  1938. ;;; Functions and variables from ther packages
  1939. ;; Declared here to avoid compiler warnings
  1940. ;; XEmacs only
  1941. (defvar outline-mode-menu-heading)
  1942. (defvar outline-mode-menu-show)
  1943. (defvar outline-mode-menu-hide)
  1944. (defvar zmacs-regions) ; XEmacs regions
  1945. ;; Emacs only
  1946. (defvar mark-active)
  1947. ;; Various packages
  1948. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  1949. (declare-function calendar-forward-day "cal-move" (arg))
  1950. (declare-function calendar-goto-date "cal-move" (date))
  1951. (declare-function calendar-goto-today "cal-move" ())
  1952. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  1953. (defvar calc-embedded-close-formula)
  1954. (defvar calc-embedded-open-formula)
  1955. (declare-function cdlatex-tab "ext:cdlatex" ())
  1956. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1957. (defvar font-lock-unfontify-region-function)
  1958. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1959. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  1960. (defvar iswitchb-temp-buflist)
  1961. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  1962. (declare-function org-agenda-skip "org-agenda" ())
  1963. (declare-function org-format-agenda-item "org-agenda"
  1964. (extra txt &optional category tags dotime noprefix remove-re))
  1965. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  1966. (declare-function org-agenda-change-all-lines "org-agenda"
  1967. (newhead hdmarker &optional fixface))
  1968. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  1969. (declare-function org-agenda-maybe-redo "org-agenda" ())
  1970. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  1971. (beg end))
  1972. (declare-function parse-time-string "parse-time" (string))
  1973. (declare-function remember "remember" (&optional initial))
  1974. (declare-function remember-buffer-desc "remember" ())
  1975. (declare-function remember-finalize "remember" ())
  1976. (defvar remember-save-after-remembering)
  1977. (defvar remember-data-file)
  1978. (defvar remember-register)
  1979. (defvar remember-buffer)
  1980. (defvar remember-handler-functions)
  1981. (defvar remember-annotation-functions)
  1982. (defvar texmathp-why)
  1983. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  1984. (declare-function table--at-cell-p "table" (position &optional object at-column))
  1985. (defvar w3m-current-url)
  1986. (defvar w3m-current-title)
  1987. (defvar org-latex-regexps)
  1988. ;;; Autoload and prepare some org modules
  1989. ;; Some table stuff that needs to be defined here, because it is used
  1990. ;; by the functions setting up org-mode or checking for table context.
  1991. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  1992. "Detects an org-type or table-type table.")
  1993. (defconst org-table-line-regexp "^[ \t]*|"
  1994. "Detects an org-type table line.")
  1995. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  1996. "Detects an org-type table line.")
  1997. (defconst org-table-hline-regexp "^[ \t]*|-"
  1998. "Detects an org-type table hline.")
  1999. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2000. "Detects a table-type table hline.")
  2001. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2002. "Searching from within a table (any type) this finds the first line
  2003. outside the table.")
  2004. ;; Autoload the functions in org-table.el that are needed by functions here.
  2005. (eval-and-compile
  2006. (org-autoload "org-table"
  2007. '(org-table-align org-table-begin org-table-blank-field
  2008. org-table-convert org-table-convert-region org-table-copy-down
  2009. org-table-copy-region org-table-create
  2010. org-table-create-or-convert-from-region
  2011. org-table-create-with-table.el org-table-current-dline
  2012. org-table-cut-region org-table-delete-column org-table-edit-field
  2013. org-table-edit-formulas org-table-end org-table-eval-formula
  2014. org-table-export org-table-field-info
  2015. org-table-get-stored-formulas org-table-goto-column
  2016. org-table-hline-and-move org-table-import org-table-insert-column
  2017. org-table-insert-hline org-table-insert-row org-table-iterate
  2018. org-table-justify-field-maybe org-table-kill-row
  2019. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2020. org-table-move-column org-table-move-column-left
  2021. org-table-move-column-right org-table-move-row
  2022. org-table-move-row-down org-table-move-row-up
  2023. org-table-next-field org-table-next-row org-table-paste-rectangle
  2024. org-table-previous-field org-table-recalculate
  2025. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2026. org-table-toggle-coordinate-overlays
  2027. org-table-toggle-formula-debugger org-table-wrap-region
  2028. orgtbl-mode turn-on-orgtbl)))
  2029. (defun org-at-table-p (&optional table-type)
  2030. "Return t if the cursor is inside an org-type table.
  2031. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2032. (if org-enable-table-editor
  2033. (save-excursion
  2034. (beginning-of-line 1)
  2035. (looking-at (if table-type org-table-any-line-regexp
  2036. org-table-line-regexp)))
  2037. nil))
  2038. (defsubst org-table-p () (org-at-table-p))
  2039. (defun org-at-table.el-p ()
  2040. "Return t if and only if we are at a table.el table."
  2041. (and (org-at-table-p 'any)
  2042. (save-excursion
  2043. (goto-char (org-table-begin 'any))
  2044. (looking-at org-table1-hline-regexp))))
  2045. (defun org-table-recognize-table.el ()
  2046. "If there is a table.el table nearby, recognize it and move into it."
  2047. (if org-table-tab-recognizes-table.el
  2048. (if (org-at-table.el-p)
  2049. (progn
  2050. (beginning-of-line 1)
  2051. (if (looking-at org-table-dataline-regexp)
  2052. nil
  2053. (if (looking-at org-table1-hline-regexp)
  2054. (progn
  2055. (beginning-of-line 2)
  2056. (if (looking-at org-table-any-border-regexp)
  2057. (beginning-of-line -1)))))
  2058. (if (re-search-forward "|" (org-table-end t) t)
  2059. (progn
  2060. (require 'table)
  2061. (if (table--at-cell-p (point))
  2062. t
  2063. (message "recognizing table.el table...")
  2064. (table-recognize-table)
  2065. (message "recognizing table.el table...done")))
  2066. (error "This should not happen..."))
  2067. t)
  2068. nil)
  2069. nil))
  2070. (defun org-at-table-hline-p ()
  2071. "Return t if the cursor is inside a hline in a table."
  2072. (if org-enable-table-editor
  2073. (save-excursion
  2074. (beginning-of-line 1)
  2075. (looking-at org-table-hline-regexp))
  2076. nil))
  2077. (defvar org-table-clean-did-remove-column nil)
  2078. (defun org-table-map-tables (function)
  2079. "Apply FUNCTION to the start of all tables in the buffer."
  2080. (save-excursion
  2081. (save-restriction
  2082. (widen)
  2083. (goto-char (point-min))
  2084. (while (re-search-forward org-table-any-line-regexp nil t)
  2085. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2086. (beginning-of-line 1)
  2087. (if (looking-at org-table-line-regexp)
  2088. (save-excursion (funcall function)))
  2089. (re-search-forward org-table-any-border-regexp nil 1))))
  2090. (message "Mapping tables: done"))
  2091. ;; Declare and autoload functions from org-exp.el
  2092. (declare-function org-default-export-plist "org-exp")
  2093. (declare-function org-infile-export-plist "org-exp")
  2094. (declare-function org-get-current-options "org-exp")
  2095. (eval-and-compile
  2096. (org-autoload "org-exp"
  2097. '(org-export org-export-as-ascii org-export-visible
  2098. org-insert-export-options-template org-export-as-html-and-open
  2099. org-export-as-html-batch org-export-as-html-to-buffer
  2100. org-replace-region-by-html org-export-region-as-html
  2101. org-export-as-html org-export-icalendar-this-file
  2102. org-export-icalendar-all-agenda-files
  2103. org-table-clean-before-export
  2104. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2105. ;; Declare and autoload functions from org-exp.el
  2106. (eval-and-compile
  2107. (org-autoload "org-exp"
  2108. '(org-agenda org-agenda-list org-search-view
  2109. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2110. org-diary org-agenda-to-appt)))
  2111. ;; Autoload org-remember
  2112. (eval-and-compile
  2113. (org-autoload "org-remember"
  2114. '(org-remember-insinuate org-remember-annotation
  2115. org-remember-apply-template org-remember org-remember-handler)))
  2116. ;; Autoload org-clock.el
  2117. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2118. (beg end))
  2119. (declare-function org-update-mode-line "org-clock" ())
  2120. (defvar org-clock-start-time)
  2121. (defvar org-clock-marker (make-marker)
  2122. "Marker recording the last clock-in.")
  2123. (eval-and-compile
  2124. (org-autoload
  2125. "org-clock"
  2126. '(org-clock-in org-clock-out org-clock-cancel
  2127. org-clock-goto org-clock-sum org-clock-display
  2128. org-remove-clock-overlays org-clock-report
  2129. org-clocktable-shift org-dblock-write:clocktable
  2130. org-get-clocktable)))
  2131. (defun org-clock-update-time-maybe ()
  2132. "If this is a CLOCK line, update it and return t.
  2133. Otherwise, return nil."
  2134. (interactive)
  2135. (save-excursion
  2136. (beginning-of-line 1)
  2137. (skip-chars-forward " \t")
  2138. (when (looking-at org-clock-string)
  2139. (let ((re (concat "[ \t]*" org-clock-string
  2140. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2141. "\\([ \t]*=>.*\\)?\\)?"))
  2142. ts te h m s)
  2143. (cond
  2144. ((not (looking-at re))
  2145. nil)
  2146. ((not (match-end 2))
  2147. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2148. (> org-clock-marker (point))
  2149. (<= org-clock-marker (point-at-eol)))
  2150. ;; The clock is running here
  2151. (setq org-clock-start-time
  2152. (apply 'encode-time
  2153. (org-parse-time-string (match-string 1))))
  2154. (org-update-mode-line)))
  2155. (t
  2156. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2157. (end-of-line 1)
  2158. (setq ts (match-string 1)
  2159. te (match-string 3))
  2160. (setq s (- (time-to-seconds
  2161. (apply 'encode-time (org-parse-time-string te)))
  2162. (time-to-seconds
  2163. (apply 'encode-time (org-parse-time-string ts))))
  2164. h (floor (/ s 3600))
  2165. s (- s (* 3600 h))
  2166. m (floor (/ s 60))
  2167. s (- s (* 60 s)))
  2168. (insert " => " (format "%2d:%02d" h m))
  2169. t))))))
  2170. (defun org-check-running-clock ()
  2171. "Check if the current buffer contains the running clock.
  2172. If yes, offer to stop it and to save the buffer with the changes."
  2173. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2174. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2175. (buffer-name))))
  2176. (org-clock-out)
  2177. (when (y-or-n-p "Save changed buffer?")
  2178. (save-buffer))))
  2179. (defun org-clocktable-try-shift (dir n)
  2180. "Check if this line starts a clock table, if yes, shift the time block."
  2181. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2182. (org-clocktable-shift dir n)))
  2183. ;; Autoload archiving code
  2184. ;; The stuff that is needed for cycling and tags has to be defined here.
  2185. (defgroup org-archive nil
  2186. "Options concerning archiving in Org-mode."
  2187. :tag "Org Archive"
  2188. :group 'org-structure)
  2189. (defcustom org-archive-location "%s_archive::"
  2190. "The location where subtrees should be archived.
  2191. Otherwise, the value of this variable is a string, consisting of two
  2192. parts, separated by a double-colon.
  2193. The first part is a file name - when omitted, archiving happens in the same
  2194. file. %s will be replaced by the current file name (without directory part).
  2195. Archiving to a different file is useful to keep archived entries from
  2196. contributing to the Org-mode Agenda.
  2197. The part after the double colon is a headline. The archived entries will be
  2198. filed under that headline. When omitted, the subtrees are simply filed away
  2199. at the end of the file, as top-level entries.
  2200. Here are a few examples:
  2201. \"%s_archive::\"
  2202. If the current file is Projects.org, archive in file
  2203. Projects.org_archive, as top-level trees. This is the default.
  2204. \"::* Archived Tasks\"
  2205. Archive in the current file, under the top-level headline
  2206. \"* Archived Tasks\".
  2207. \"~/org/archive.org::\"
  2208. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2209. \"basement::** Finished Tasks\"
  2210. Archive in file ./basement (relative path), as level 3 trees
  2211. below the level 2 heading \"** Finished Tasks\".
  2212. You may set this option on a per-file basis by adding to the buffer a
  2213. line like
  2214. #+ARCHIVE: basement::** Finished Tasks
  2215. You may also define it locally for a subtree by setting an ARCHIVE property
  2216. in the entry. If such a property is found in an entry, or anywhere up
  2217. the hierarchy, it will be used."
  2218. :group 'org-archive
  2219. :type 'string)
  2220. (defcustom org-archive-tag "ARCHIVE"
  2221. "The tag that marks a subtree as archived.
  2222. An archived subtree does not open during visibility cycling, and does
  2223. not contribute to the agenda listings.
  2224. After changing this, font-lock must be restarted in the relevant buffers to
  2225. get the proper fontification."
  2226. :group 'org-archive
  2227. :group 'org-keywords
  2228. :type 'string)
  2229. (defcustom org-agenda-skip-archived-trees t
  2230. "Non-nil means, the agenda will skip any items located in archived trees.
  2231. An archived tree is a tree marked with the tag ARCHIVE."
  2232. :group 'org-archive
  2233. :group 'org-agenda-skip
  2234. :type 'boolean)
  2235. (defcustom org-cycle-open-archived-trees nil
  2236. "Non-nil means, `org-cycle' will open archived trees.
  2237. An archived tree is a tree marked with the tag ARCHIVE.
  2238. When nil, archived trees will stay folded. You can still open them with
  2239. normal outline commands like `show-all', but not with the cycling commands."
  2240. :group 'org-archive
  2241. :group 'org-cycle
  2242. :type 'boolean)
  2243. (defcustom org-sparse-tree-open-archived-trees nil
  2244. "Non-nil means sparse tree construction shows matches in archived trees.
  2245. When nil, matches in these trees are highlighted, but the trees are kept in
  2246. collapsed state."
  2247. :group 'org-archive
  2248. :group 'org-sparse-trees
  2249. :type 'boolean)
  2250. (defun org-cycle-hide-archived-subtrees (state)
  2251. "Re-hide all archived subtrees after a visibility state change."
  2252. (when (and (not org-cycle-open-archived-trees)
  2253. (not (memq state '(overview folded))))
  2254. (save-excursion
  2255. (let* ((globalp (memq state '(contents all)))
  2256. (beg (if globalp (point-min) (point)))
  2257. (end (if globalp (point-max) (org-end-of-subtree t))))
  2258. (org-hide-archived-subtrees beg end)
  2259. (goto-char beg)
  2260. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2261. (message "%s" (substitute-command-keys
  2262. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2263. (defun org-force-cycle-archived ()
  2264. "Cycle subtree even if it is archived."
  2265. (interactive)
  2266. (setq this-command 'org-cycle)
  2267. (let ((org-cycle-open-archived-trees t))
  2268. (call-interactively 'org-cycle)))
  2269. (defun org-hide-archived-subtrees (beg end)
  2270. "Re-hide all archived subtrees after a visibility state change."
  2271. (save-excursion
  2272. (let* ((re (concat ":" org-archive-tag ":")))
  2273. (goto-char beg)
  2274. (while (re-search-forward re end t)
  2275. (and (org-on-heading-p) (hide-subtree))
  2276. (org-end-of-subtree t)))))
  2277. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2278. (eval-and-compile
  2279. (org-autoload "org-archive"
  2280. '(org-add-archive-files org-archive-subtree
  2281. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2282. ;; Autoload Column View Code
  2283. (declare-function org-columns-number-to-string "org-colview")
  2284. (declare-function org-columns-get-format-and-top-level "org-colview")
  2285. (declare-function org-columns-compute "org-colview")
  2286. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2287. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2288. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2289. org-columns org-insert-columns-dblock))
  2290. ;; Autoload ID code
  2291. (org-autoload "org-id"
  2292. '(org-id-get-create org-id-new org-id-copy org-id-get
  2293. org-id-get-with-outline-path-completion
  2294. org-id-get-with-outline-drilling
  2295. org-id-goto org-id-find))
  2296. ;;; Variables for pre-computed regular expressions, all buffer local
  2297. (defvar org-drawer-regexp nil
  2298. "Matches first line of a hidden block.")
  2299. (make-variable-buffer-local 'org-drawer-regexp)
  2300. (defvar org-todo-regexp nil
  2301. "Matches any of the TODO state keywords.")
  2302. (make-variable-buffer-local 'org-todo-regexp)
  2303. (defvar org-not-done-regexp nil
  2304. "Matches any of the TODO state keywords except the last one.")
  2305. (make-variable-buffer-local 'org-not-done-regexp)
  2306. (defvar org-todo-line-regexp nil
  2307. "Matches a headline and puts TODO state into group 2 if present.")
  2308. (make-variable-buffer-local 'org-todo-line-regexp)
  2309. (defvar org-complex-heading-regexp nil
  2310. "Matches a headline and puts everything into groups:
  2311. group 1: the stars
  2312. group 2: The todo keyword, maybe
  2313. group 3: Priority cookie
  2314. group 4: True headline
  2315. group 5: Tags")
  2316. (make-variable-buffer-local 'org-complex-heading-regexp)
  2317. (defvar org-todo-line-tags-regexp nil
  2318. "Matches a headline and puts TODO state into group 2 if present.
  2319. Also put tags into group 4 if tags are present.")
  2320. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2321. (defvar org-nl-done-regexp nil
  2322. "Matches newline followed by a headline with the DONE keyword.")
  2323. (make-variable-buffer-local 'org-nl-done-regexp)
  2324. (defvar org-looking-at-done-regexp nil
  2325. "Matches the DONE keyword a point.")
  2326. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2327. (defvar org-ds-keyword-length 12
  2328. "Maximum length of the Deadline and SCHEDULED keywords.")
  2329. (make-variable-buffer-local 'org-ds-keyword-length)
  2330. (defvar org-deadline-regexp nil
  2331. "Matches the DEADLINE keyword.")
  2332. (make-variable-buffer-local 'org-deadline-regexp)
  2333. (defvar org-deadline-time-regexp nil
  2334. "Matches the DEADLINE keyword together with a time stamp.")
  2335. (make-variable-buffer-local 'org-deadline-time-regexp)
  2336. (defvar org-deadline-line-regexp nil
  2337. "Matches the DEADLINE keyword and the rest of the line.")
  2338. (make-variable-buffer-local 'org-deadline-line-regexp)
  2339. (defvar org-scheduled-regexp nil
  2340. "Matches the SCHEDULED keyword.")
  2341. (make-variable-buffer-local 'org-scheduled-regexp)
  2342. (defvar org-scheduled-time-regexp nil
  2343. "Matches the SCHEDULED keyword together with a time stamp.")
  2344. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2345. (defvar org-closed-time-regexp nil
  2346. "Matches the CLOSED keyword together with a time stamp.")
  2347. (make-variable-buffer-local 'org-closed-time-regexp)
  2348. (defvar org-keyword-time-regexp nil
  2349. "Matches any of the 4 keywords, together with the time stamp.")
  2350. (make-variable-buffer-local 'org-keyword-time-regexp)
  2351. (defvar org-keyword-time-not-clock-regexp nil
  2352. "Matches any of the 3 keywords, together with the time stamp.")
  2353. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2354. (defvar org-maybe-keyword-time-regexp nil
  2355. "Matches a timestamp, possibly preceeded by a keyword.")
  2356. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2357. (defvar org-planning-or-clock-line-re nil
  2358. "Matches a line with planning or clock info.")
  2359. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2360. (defconst org-plain-time-of-day-regexp
  2361. (concat
  2362. "\\(\\<[012]?[0-9]"
  2363. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2364. "\\(--?"
  2365. "\\(\\<[012]?[0-9]"
  2366. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2367. "\\)?")
  2368. "Regular expression to match a plain time or time range.
  2369. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2370. groups carry important information:
  2371. 0 the full match
  2372. 1 the first time, range or not
  2373. 8 the second time, if it is a range.")
  2374. (defconst org-plain-time-extension-regexp
  2375. (concat
  2376. "\\(\\<[012]?[0-9]"
  2377. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2378. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2379. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2380. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2381. groups carry important information:
  2382. 0 the full match
  2383. 7 hours of duration
  2384. 9 minutes of duration")
  2385. (defconst org-stamp-time-of-day-regexp
  2386. (concat
  2387. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2388. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2389. "\\(--?"
  2390. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2391. "Regular expression to match a timestamp time or time range.
  2392. After a match, the following groups carry important information:
  2393. 0 the full match
  2394. 1 date plus weekday, for backreferencing to make sure both times on same day
  2395. 2 the first time, range or not
  2396. 4 the second time, if it is a range.")
  2397. (defconst org-startup-options
  2398. '(("fold" org-startup-folded t)
  2399. ("overview" org-startup-folded t)
  2400. ("nofold" org-startup-folded nil)
  2401. ("showall" org-startup-folded nil)
  2402. ("content" org-startup-folded content)
  2403. ("hidestars" org-hide-leading-stars t)
  2404. ("showstars" org-hide-leading-stars nil)
  2405. ("odd" org-odd-levels-only t)
  2406. ("oddeven" org-odd-levels-only nil)
  2407. ("align" org-startup-align-all-tables t)
  2408. ("noalign" org-startup-align-all-tables nil)
  2409. ("customtime" org-display-custom-times t)
  2410. ("logdone" org-log-done time)
  2411. ("lognotedone" org-log-done note)
  2412. ("nologdone" org-log-done nil)
  2413. ("lognoteclock-out" org-log-note-clock-out t)
  2414. ("nolognoteclock-out" org-log-note-clock-out nil)
  2415. ("logrepeat" org-log-repeat state)
  2416. ("lognoterepeat" org-log-repeat note)
  2417. ("nologrepeat" org-log-repeat nil)
  2418. ("constcgs" constants-unit-system cgs)
  2419. ("constSI" constants-unit-system SI))
  2420. "Variable associated with STARTUP options for org-mode.
  2421. Each element is a list of three items: The startup options as written
  2422. in the #+STARTUP line, the corresponding variable, and the value to
  2423. set this variable to if the option is found. An optional forth element PUSH
  2424. means to push this value onto the list in the variable.")
  2425. (defun org-set-regexps-and-options ()
  2426. "Precompute regular expressions for current buffer."
  2427. (when (org-mode-p)
  2428. (org-set-local 'org-todo-kwd-alist nil)
  2429. (org-set-local 'org-todo-key-alist nil)
  2430. (org-set-local 'org-todo-key-trigger nil)
  2431. (org-set-local 'org-todo-keywords-1 nil)
  2432. (org-set-local 'org-done-keywords nil)
  2433. (org-set-local 'org-todo-heads nil)
  2434. (org-set-local 'org-todo-sets nil)
  2435. (org-set-local 'org-todo-log-states nil)
  2436. (let ((re (org-make-options-regexp
  2437. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2438. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2439. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2440. (splitre "[ \t]+")
  2441. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2442. tail sep kws1 prio props ftags drawers
  2443. ext-setup-or-nil setup-contents (start 0))
  2444. (save-excursion
  2445. (save-restriction
  2446. (widen)
  2447. (goto-char (point-min))
  2448. (while (or (and ext-setup-or-nil
  2449. (string-match re ext-setup-or-nil start)
  2450. (setq start (match-end 0)))
  2451. (and (setq ext-setup-or-nil nil start 0)
  2452. (re-search-forward re nil t)))
  2453. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2454. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2455. (cond
  2456. ((equal key "CATEGORY")
  2457. (if (string-match "[ \t]+$" value)
  2458. (setq value (replace-match "" t t value)))
  2459. (setq cat value))
  2460. ((member key '("SEQ_TODO" "TODO"))
  2461. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2462. ((equal key "TYP_TODO")
  2463. (push (cons 'type (org-split-string value splitre)) kwds))
  2464. ((equal key "TAGS")
  2465. (setq tags (append tags (org-split-string value splitre))))
  2466. ((equal key "COLUMNS")
  2467. (org-set-local 'org-columns-default-format value))
  2468. ((equal key "LINK")
  2469. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2470. (push (cons (match-string 1 value)
  2471. (org-trim (match-string 2 value)))
  2472. links)))
  2473. ((equal key "PRIORITIES")
  2474. (setq prio (org-split-string value " +")))
  2475. ((equal key "PROPERTY")
  2476. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2477. (push (cons (match-string 1 value) (match-string 2 value))
  2478. props)))
  2479. ((equal key "FILETAGS")
  2480. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2481. (setq ftags
  2482. (append
  2483. ftags
  2484. (apply 'append
  2485. (mapcar (lambda (x) (org-split-string x ":"))
  2486. (org-split-string value)))))))
  2487. ((equal key "DRAWERS")
  2488. (setq drawers (org-split-string value splitre)))
  2489. ((equal key "CONSTANTS")
  2490. (setq const (append const (org-split-string value splitre))))
  2491. ((equal key "STARTUP")
  2492. (let ((opts (org-split-string value splitre))
  2493. l var val)
  2494. (while (setq l (pop opts))
  2495. (when (setq l (assoc l org-startup-options))
  2496. (setq var (nth 1 l) val (nth 2 l))
  2497. (if (not (nth 3 l))
  2498. (set (make-local-variable var) val)
  2499. (if (not (listp (symbol-value var)))
  2500. (set (make-local-variable var) nil))
  2501. (set (make-local-variable var) (symbol-value var))
  2502. (add-to-list var val))))))
  2503. ((equal key "ARCHIVE")
  2504. (string-match " *$" value)
  2505. (setq arch (replace-match "" t t value))
  2506. (remove-text-properties 0 (length arch)
  2507. '(face t fontified t) arch))
  2508. ((equal key "SETUPFILE")
  2509. (setq setup-contents (org-file-contents
  2510. (expand-file-name
  2511. (org-remove-double-quotes value))
  2512. 'noerror))
  2513. (if (not ext-setup-or-nil)
  2514. (setq ext-setup-or-nil setup-contents start 0)
  2515. (setq ext-setup-or-nil
  2516. (concat (substring ext-setup-or-nil 0 start)
  2517. "\n" setup-contents "\n"
  2518. (substring ext-setup-or-nil start)))))
  2519. ))))
  2520. (when cat
  2521. (org-set-local 'org-category (intern cat))
  2522. (push (cons "CATEGORY" cat) props))
  2523. (when prio
  2524. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2525. (setq prio (mapcar 'string-to-char prio))
  2526. (org-set-local 'org-highest-priority (nth 0 prio))
  2527. (org-set-local 'org-lowest-priority (nth 1 prio))
  2528. (org-set-local 'org-default-priority (nth 2 prio)))
  2529. (and props (org-set-local 'org-file-properties (nreverse props)))
  2530. (and ftags (org-set-local 'org-file-tags ftags))
  2531. (and drawers (org-set-local 'org-drawers drawers))
  2532. (and arch (org-set-local 'org-archive-location arch))
  2533. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2534. ;; Process the TODO keywords
  2535. (unless kwds
  2536. ;; Use the global values as if they had been given locally.
  2537. (setq kwds (default-value 'org-todo-keywords))
  2538. (if (stringp (car kwds))
  2539. (setq kwds (list (cons org-todo-interpretation
  2540. (default-value 'org-todo-keywords)))))
  2541. (setq kwds (reverse kwds)))
  2542. (setq kwds (nreverse kwds))
  2543. (let (inter kws kw)
  2544. (while (setq kws (pop kwds))
  2545. (setq inter (pop kws) sep (member "|" kws)
  2546. kws0 (delete "|" (copy-sequence kws))
  2547. kwsa nil
  2548. kws1 (mapcar
  2549. (lambda (x)
  2550. ;; 1 2
  2551. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2552. (progn
  2553. (setq kw (match-string 1 x)
  2554. key (and (match-end 2) (match-string 2 x))
  2555. log (org-extract-log-state-settings x))
  2556. (push (cons kw (and key (string-to-char key))) kwsa)
  2557. (and log (push log org-todo-log-states))
  2558. kw)
  2559. (error "Invalid TODO keyword %s" x)))
  2560. kws0)
  2561. kwsa (if kwsa (append '((:startgroup))
  2562. (nreverse kwsa)
  2563. '((:endgroup))))
  2564. hw (car kws1)
  2565. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2566. tail (list inter hw (car dws) (org-last dws)))
  2567. (add-to-list 'org-todo-heads hw 'append)
  2568. (push kws1 org-todo-sets)
  2569. (setq org-done-keywords (append org-done-keywords dws nil))
  2570. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2571. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2572. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2573. (setq org-todo-sets (nreverse org-todo-sets)
  2574. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2575. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2576. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2577. ;; Process the constants
  2578. (when const
  2579. (let (e cst)
  2580. (while (setq e (pop const))
  2581. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2582. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2583. (setq org-table-formula-constants-local cst)))
  2584. ;; Process the tags.
  2585. (when tags
  2586. (let (e tgs)
  2587. (while (setq e (pop tags))
  2588. (cond
  2589. ((equal e "{") (push '(:startgroup) tgs))
  2590. ((equal e "}") (push '(:endgroup) tgs))
  2591. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2592. (push (cons (match-string 1 e)
  2593. (string-to-char (match-string 2 e)))
  2594. tgs))
  2595. (t (push (list e) tgs))))
  2596. (org-set-local 'org-tag-alist nil)
  2597. (while (setq e (pop tgs))
  2598. (or (and (stringp (car e))
  2599. (assoc (car e) org-tag-alist))
  2600. (push e org-tag-alist)))))
  2601. ;; Compute the regular expressions and other local variables
  2602. (if (not org-done-keywords)
  2603. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2604. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2605. (length org-scheduled-string)
  2606. (length org-clock-string)
  2607. (length org-closed-string)))
  2608. org-drawer-regexp
  2609. (concat "^[ \t]*:\\("
  2610. (mapconcat 'regexp-quote org-drawers "\\|")
  2611. "\\):[ \t]*$")
  2612. org-not-done-keywords
  2613. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2614. org-todo-regexp
  2615. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2616. "\\|") "\\)\\>")
  2617. org-not-done-regexp
  2618. (concat "\\<\\("
  2619. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2620. "\\)\\>")
  2621. org-todo-line-regexp
  2622. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2623. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2624. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2625. org-complex-heading-regexp
  2626. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2627. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2628. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2629. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2630. org-nl-done-regexp
  2631. (concat "\n\\*+[ \t]+"
  2632. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2633. "\\)" "\\>")
  2634. org-todo-line-tags-regexp
  2635. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2636. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2637. (org-re
  2638. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2639. org-looking-at-done-regexp
  2640. (concat "^" "\\(?:"
  2641. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2642. "\\>")
  2643. org-deadline-regexp (concat "\\<" org-deadline-string)
  2644. org-deadline-time-regexp
  2645. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2646. org-deadline-line-regexp
  2647. (concat "\\<\\(" org-deadline-string "\\).*")
  2648. org-scheduled-regexp
  2649. (concat "\\<" org-scheduled-string)
  2650. org-scheduled-time-regexp
  2651. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2652. org-closed-time-regexp
  2653. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2654. org-keyword-time-regexp
  2655. (concat "\\<\\(" org-scheduled-string
  2656. "\\|" org-deadline-string
  2657. "\\|" org-closed-string
  2658. "\\|" org-clock-string "\\)"
  2659. " *[[<]\\([^]>]+\\)[]>]")
  2660. org-keyword-time-not-clock-regexp
  2661. (concat "\\<\\(" org-scheduled-string
  2662. "\\|" org-deadline-string
  2663. "\\|" org-closed-string
  2664. "\\)"
  2665. " *[[<]\\([^]>]+\\)[]>]")
  2666. org-maybe-keyword-time-regexp
  2667. (concat "\\(\\<\\(" org-scheduled-string
  2668. "\\|" org-deadline-string
  2669. "\\|" org-closed-string
  2670. "\\|" org-clock-string "\\)\\)?"
  2671. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2672. org-planning-or-clock-line-re
  2673. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2674. "\\|" org-deadline-string
  2675. "\\|" org-closed-string "\\|" org-clock-string
  2676. "\\)\\>\\)")
  2677. )
  2678. (org-compute-latex-and-specials-regexp)
  2679. (org-set-font-lock-defaults))))
  2680. (defun org-file-contents (file &optional noerror)
  2681. "Return the contents of FILE, as a string."
  2682. (if (or (not file)
  2683. (not (file-readable-p file)))
  2684. (if noerror
  2685. (progn
  2686. (message "Cannot read file %s" file)
  2687. (ding) (sit-for 2)
  2688. "")
  2689. (error "Cannot read file %s" file))
  2690. (with-temp-buffer
  2691. (insert-file-contents file)
  2692. (buffer-string))))
  2693. (defun org-extract-log-state-settings (x)
  2694. "Extract the log state setting from a TODO keyword string.
  2695. This will extract info from a string like \"WAIT(w@/!)\"."
  2696. (let (kw key log1 log2)
  2697. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2698. (setq kw (match-string 1 x)
  2699. key (and (match-end 2) (match-string 2 x))
  2700. log1 (and (match-end 3) (match-string 3 x))
  2701. log2 (and (match-end 4) (match-string 4 x)))
  2702. (and (or log1 log2)
  2703. (list kw
  2704. (and log1 (if (equal log1 "!") 'time 'note))
  2705. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2706. (defun org-remove-keyword-keys (list)
  2707. "Remove a pair of parenthesis at the end of each string in LIST."
  2708. (mapcar (lambda (x)
  2709. (if (string-match "(.*)$" x)
  2710. (substring x 0 (match-beginning 0))
  2711. x))
  2712. list))
  2713. ;; FIXME: this could be done much better, using second characters etc.
  2714. (defun org-assign-fast-keys (alist)
  2715. "Assign fast keys to a keyword-key alist.
  2716. Respect keys that are already there."
  2717. (let (new e k c c1 c2 (char ?a))
  2718. (while (setq e (pop alist))
  2719. (cond
  2720. ((equal e '(:startgroup)) (push e new))
  2721. ((equal e '(:endgroup)) (push e new))
  2722. (t
  2723. (setq k (car e) c2 nil)
  2724. (if (cdr e)
  2725. (setq c (cdr e))
  2726. ;; automatically assign a character.
  2727. (setq c1 (string-to-char
  2728. (downcase (substring
  2729. k (if (= (string-to-char k) ?@) 1 0)))))
  2730. (if (or (rassoc c1 new) (rassoc c1 alist))
  2731. (while (or (rassoc char new) (rassoc char alist))
  2732. (setq char (1+ char)))
  2733. (setq c2 c1))
  2734. (setq c (or c2 char)))
  2735. (push (cons k c) new))))
  2736. (nreverse new)))
  2737. ;;; Some variables used in various places
  2738. (defvar org-window-configuration nil
  2739. "Used in various places to store a window configuration.")
  2740. (defvar org-finish-function nil
  2741. "Function to be called when `C-c C-c' is used.
  2742. This is for getting out of special buffers like remember.")
  2743. ;; FIXME: Occasionally check by commenting these, to make sure
  2744. ;; no other functions uses these, forgetting to let-bind them.
  2745. (defvar entry)
  2746. (defvar state)
  2747. (defvar last-state)
  2748. (defvar date)
  2749. (defvar description)
  2750. ;; Defined somewhere in this file, but used before definition.
  2751. (defvar org-html-entities)
  2752. (defvar org-struct-menu)
  2753. (defvar org-org-menu)
  2754. (defvar org-tbl-menu)
  2755. (defvar org-agenda-keymap)
  2756. ;;;; Define the Org-mode
  2757. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2758. (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."))
  2759. ;; We use a before-change function to check if a table might need
  2760. ;; an update.
  2761. (defvar org-table-may-need-update t
  2762. "Indicates that a table might need an update.
  2763. This variable is set by `org-before-change-function'.
  2764. `org-table-align' sets it back to nil.")
  2765. (defun org-before-change-function (beg end)
  2766. "Every change indicates that a table might need an update."
  2767. (setq org-table-may-need-update t))
  2768. (defvar org-mode-map)
  2769. (defvar org-mode-hook nil)
  2770. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2771. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2772. (defvar org-table-buffer-is-an nil)
  2773. (defconst org-outline-regexp "\\*+ ")
  2774. ;;;###autoload
  2775. (define-derived-mode org-mode outline-mode "Org"
  2776. "Outline-based notes management and organizer, alias
  2777. \"Carsten's outline-mode for keeping track of everything.\"
  2778. Org-mode develops organizational tasks around a NOTES file which
  2779. contains information about projects as plain text. Org-mode is
  2780. implemented on top of outline-mode, which is ideal to keep the content
  2781. of large files well structured. It supports ToDo items, deadlines and
  2782. time stamps, which magically appear in the diary listing of the Emacs
  2783. calendar. Tables are easily created with a built-in table editor.
  2784. Plain text URL-like links connect to websites, emails (VM), Usenet
  2785. messages (Gnus), BBDB entries, and any files related to the project.
  2786. For printing and sharing of notes, an Org-mode file (or a part of it)
  2787. can be exported as a structured ASCII or HTML file.
  2788. The following commands are available:
  2789. \\{org-mode-map}"
  2790. ;; Get rid of Outline menus, they are not needed
  2791. ;; Need to do this here because define-derived-mode sets up
  2792. ;; the keymap so late. Still, it is a waste to call this each time
  2793. ;; we switch another buffer into org-mode.
  2794. (if (featurep 'xemacs)
  2795. (when (boundp 'outline-mode-menu-heading)
  2796. ;; Assume this is Greg's port, it used easymenu
  2797. (easy-menu-remove outline-mode-menu-heading)
  2798. (easy-menu-remove outline-mode-menu-show)
  2799. (easy-menu-remove outline-mode-menu-hide))
  2800. (define-key org-mode-map [menu-bar headings] 'undefined)
  2801. (define-key org-mode-map [menu-bar hide] 'undefined)
  2802. (define-key org-mode-map [menu-bar show] 'undefined))
  2803. (org-load-modules-maybe)
  2804. (easy-menu-add org-org-menu)
  2805. (easy-menu-add org-tbl-menu)
  2806. (org-install-agenda-files-menu)
  2807. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2808. (org-add-to-invisibility-spec '(org-cwidth))
  2809. (when (featurep 'xemacs)
  2810. (org-set-local 'line-move-ignore-invisible t))
  2811. (org-set-local 'outline-regexp org-outline-regexp)
  2812. (org-set-local 'outline-level 'org-outline-level)
  2813. (when (and org-ellipsis
  2814. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2815. (fboundp 'make-glyph-code))
  2816. (unless org-display-table
  2817. (setq org-display-table (make-display-table)))
  2818. (set-display-table-slot
  2819. org-display-table 4
  2820. (vconcat (mapcar
  2821. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2822. org-ellipsis)))
  2823. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2824. (setq buffer-display-table org-display-table))
  2825. (org-set-regexps-and-options)
  2826. ;; Calc embedded
  2827. (org-set-local 'calc-embedded-open-mode "# ")
  2828. (modify-syntax-entry ?# "<")
  2829. (modify-syntax-entry ?@ "w")
  2830. (if org-startup-truncated (setq truncate-lines t))
  2831. (org-set-local 'font-lock-unfontify-region-function
  2832. 'org-unfontify-region)
  2833. ;; Activate before-change-function
  2834. (org-set-local 'org-table-may-need-update t)
  2835. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2836. 'local)
  2837. ;; Check for running clock before killing a buffer
  2838. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2839. ;; Paragraphs and auto-filling
  2840. (org-set-autofill-regexps)
  2841. (setq indent-line-function 'org-indent-line-function)
  2842. (org-update-radio-target-regexp)
  2843. ;; Comment characters
  2844. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2845. (org-set-local 'comment-padding " ")
  2846. ;; Align options lines
  2847. (org-set-local
  2848. 'align-mode-rules-list
  2849. '((org-in-buffer-settings
  2850. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2851. (modes . '(org-mode)))))
  2852. ;; Imenu
  2853. (org-set-local 'imenu-create-index-function
  2854. 'org-imenu-get-tree)
  2855. ;; Make isearch reveal context
  2856. (if (or (featurep 'xemacs)
  2857. (not (boundp 'outline-isearch-open-invisible-function)))
  2858. ;; Emacs 21 and XEmacs make use of the hook
  2859. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2860. ;; Emacs 22 deals with this through a special variable
  2861. (org-set-local 'outline-isearch-open-invisible-function
  2862. (lambda (&rest ignore) (org-show-context 'isearch))))
  2863. ;; If empty file that did not turn on org-mode automatically, make it to.
  2864. (if (and org-insert-mode-line-in-empty-file
  2865. (interactive-p)
  2866. (= (point-min) (point-max)))
  2867. (insert "# -*- mode: org -*-\n\n"))
  2868. (unless org-inhibit-startup
  2869. (when org-startup-align-all-tables
  2870. (let ((bmp (buffer-modified-p)))
  2871. (org-table-map-tables 'org-table-align)
  2872. (set-buffer-modified-p bmp)))
  2873. (org-set-startup-visibility)))
  2874. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2875. (defun org-current-time ()
  2876. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2877. (if (> (car org-time-stamp-rounding-minutes) 1)
  2878. (let ((r (car org-time-stamp-rounding-minutes))
  2879. (time (decode-time)))
  2880. (apply 'encode-time
  2881. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2882. (nthcdr 2 time))))
  2883. (current-time)))
  2884. ;;;; Font-Lock stuff, including the activators
  2885. (defvar org-mouse-map (make-sparse-keymap))
  2886. (org-defkey org-mouse-map
  2887. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2888. (org-defkey org-mouse-map
  2889. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2890. (when org-mouse-1-follows-link
  2891. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2892. (when org-tab-follows-link
  2893. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2894. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2895. (when org-return-follows-link
  2896. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2897. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2898. (require 'font-lock)
  2899. (defconst org-non-link-chars "]\t\n\r<>")
  2900. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2901. "shell" "elisp"))
  2902. (defvar org-link-types-re nil
  2903. "Matches a link that has a url-like prefix like \"http:\"")
  2904. (defvar org-link-re-with-space nil
  2905. "Matches a link with spaces, optional angular brackets around it.")
  2906. (defvar org-link-re-with-space2 nil
  2907. "Matches a link with spaces, optional angular brackets around it.")
  2908. (defvar org-angle-link-re nil
  2909. "Matches link with angular brackets, spaces are allowed.")
  2910. (defvar org-plain-link-re nil
  2911. "Matches plain link, without spaces.")
  2912. (defvar org-bracket-link-regexp nil
  2913. "Matches a link in double brackets.")
  2914. (defvar org-bracket-link-analytic-regexp nil
  2915. "Regular expression used to analyze links.
  2916. Here is what the match groups contain after a match:
  2917. 1: http:
  2918. 2: http
  2919. 3: path
  2920. 4: [desc]
  2921. 5: desc")
  2922. (defvar org-any-link-re nil
  2923. "Regular expression matching any link.")
  2924. (defun org-make-link-regexps ()
  2925. "Update the link regular expressions.
  2926. This should be called after the variable `org-link-types' has changed."
  2927. (setq org-link-types-re
  2928. (concat
  2929. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2930. org-link-re-with-space
  2931. (concat
  2932. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2933. "\\([^" org-non-link-chars " ]"
  2934. "[^" org-non-link-chars "]*"
  2935. "[^" org-non-link-chars " ]\\)>?")
  2936. org-link-re-with-space2
  2937. (concat
  2938. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2939. "\\([^" org-non-link-chars " ]"
  2940. "[^]\t\n\r]*"
  2941. "[^" org-non-link-chars " ]\\)>?")
  2942. org-angle-link-re
  2943. (concat
  2944. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2945. "\\([^" org-non-link-chars " ]"
  2946. "[^" org-non-link-chars "]*"
  2947. "\\)>")
  2948. org-plain-link-re
  2949. (concat
  2950. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2951. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2952. org-bracket-link-regexp
  2953. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2954. org-bracket-link-analytic-regexp
  2955. (concat
  2956. "\\[\\["
  2957. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2958. "\\([^]]+\\)"
  2959. "\\]"
  2960. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2961. "\\]")
  2962. org-any-link-re
  2963. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  2964. org-angle-link-re "\\)\\|\\("
  2965. org-plain-link-re "\\)")))
  2966. (org-make-link-regexps)
  2967. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  2968. "Regular expression for fast time stamp matching.")
  2969. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  2970. "Regular expression for fast time stamp matching.")
  2971. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2972. "Regular expression matching time strings for analysis.
  2973. This one does not require the space after the date, so it can be used
  2974. on a string that terminates immediately after the date.")
  2975. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2976. "Regular expression matching time strings for analysis.")
  2977. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  2978. "Regular expression matching time stamps, with groups.")
  2979. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  2980. "Regular expression matching time stamps (also [..]), with groups.")
  2981. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2982. "Regular expression matching a time stamp range.")
  2983. (defconst org-tr-regexp-both
  2984. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  2985. "Regular expression matching a time stamp range.")
  2986. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2987. org-ts-regexp "\\)?")
  2988. "Regular expression matching a time stamp or time stamp range.")
  2989. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  2990. org-ts-regexp-both "\\)?")
  2991. "Regular expression matching a time stamp or time stamp range.
  2992. The time stamps may be either active or inactive.")
  2993. (defvar org-emph-face nil)
  2994. (defun org-do-emphasis-faces (limit)
  2995. "Run through the buffer and add overlays to links."
  2996. (let (rtn)
  2997. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  2998. (if (not (= (char-after (match-beginning 3))
  2999. (char-after (match-beginning 4))))
  3000. (progn
  3001. (setq rtn t)
  3002. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3003. 'face
  3004. (nth 1 (assoc (match-string 3)
  3005. org-emphasis-alist)))
  3006. (add-text-properties (match-beginning 2) (match-end 2)
  3007. '(font-lock-multiline t))
  3008. (when org-hide-emphasis-markers
  3009. (add-text-properties (match-end 4) (match-beginning 5)
  3010. '(invisible org-link))
  3011. (add-text-properties (match-beginning 3) (match-end 3)
  3012. '(invisible org-link)))))
  3013. (backward-char 1))
  3014. rtn))
  3015. (defun org-emphasize (&optional char)
  3016. "Insert or change an emphasis, i.e. a font like bold or italic.
  3017. If there is an active region, change that region to a new emphasis.
  3018. If there is no region, just insert the marker characters and position
  3019. the cursor between them.
  3020. CHAR should be either the marker character, or the first character of the
  3021. HTML tag associated with that emphasis. If CHAR is a space, the means
  3022. to remove the emphasis of the selected region.
  3023. If char is not given (for example in an interactive call) it
  3024. will be prompted for."
  3025. (interactive)
  3026. (let ((eal org-emphasis-alist) e det
  3027. (erc org-emphasis-regexp-components)
  3028. (prompt "")
  3029. (string "") beg end move tag c s)
  3030. (if (org-region-active-p)
  3031. (setq beg (region-beginning) end (region-end)
  3032. string (buffer-substring beg end))
  3033. (setq move t))
  3034. (while (setq e (pop eal))
  3035. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3036. c (aref tag 0))
  3037. (push (cons c (string-to-char (car e))) det)
  3038. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3039. (substring tag 1)))))
  3040. (unless char
  3041. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3042. (setq char (read-char-exclusive)))
  3043. (setq char (or (cdr (assoc char det)) char))
  3044. (if (equal char ?\ )
  3045. (setq s "" move nil)
  3046. (unless (assoc (char-to-string char) org-emphasis-alist)
  3047. (error "No such emphasis marker: \"%c\"" char))
  3048. (setq s (char-to-string char)))
  3049. (while (and (> (length string) 1)
  3050. (equal (substring string 0 1) (substring string -1))
  3051. (assoc (substring string 0 1) org-emphasis-alist))
  3052. (setq string (substring string 1 -1)))
  3053. (setq string (concat s string s))
  3054. (if beg (delete-region beg end))
  3055. (unless (or (bolp)
  3056. (string-match (concat "[" (nth 0 erc) "\n]")
  3057. (char-to-string (char-before (point)))))
  3058. (insert " "))
  3059. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3060. (char-to-string (char-after (point))))
  3061. (insert " ") (backward-char 1))
  3062. (insert string)
  3063. (and move (backward-char 1))))
  3064. (defconst org-nonsticky-props
  3065. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3066. (defun org-activate-plain-links (limit)
  3067. "Run through the buffer and add overlays to links."
  3068. (catch 'exit
  3069. (let (f)
  3070. (while (re-search-forward org-plain-link-re limit t)
  3071. (setq f (get-text-property (match-beginning 0) 'face))
  3072. (if (or (eq f 'org-tag)
  3073. (and (listp f) (memq 'org-tag f)))
  3074. nil
  3075. (add-text-properties (match-beginning 0) (match-end 0)
  3076. (list 'mouse-face 'highlight
  3077. 'rear-nonsticky org-nonsticky-props
  3078. 'keymap org-mouse-map
  3079. ))
  3080. (throw 'exit t))))))
  3081. (defun org-activate-code (limit)
  3082. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  3083. (unless (get-text-property (match-beginning 1) 'face)
  3084. (remove-text-properties (match-beginning 0) (match-end 0)
  3085. '(display t invisible t intangible t))
  3086. t)))
  3087. (defun org-activate-angle-links (limit)
  3088. "Run through the buffer and add overlays to links."
  3089. (if (re-search-forward org-angle-link-re limit t)
  3090. (progn
  3091. (add-text-properties (match-beginning 0) (match-end 0)
  3092. (list 'mouse-face 'highlight
  3093. 'rear-nonsticky org-nonsticky-props
  3094. 'keymap org-mouse-map
  3095. ))
  3096. t)))
  3097. (defun org-activate-bracket-links (limit)
  3098. "Run through the buffer and add overlays to bracketed links."
  3099. (if (re-search-forward org-bracket-link-regexp limit t)
  3100. (let* ((help (concat "LINK: "
  3101. (org-match-string-no-properties 1)))
  3102. ;; FIXME: above we should remove the escapes.
  3103. ;; but that requires another match, protecting match data,
  3104. ;; a lot of overhead for font-lock.
  3105. (ip (org-maybe-intangible
  3106. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3107. 'keymap org-mouse-map 'mouse-face 'highlight
  3108. 'font-lock-multiline t 'help-echo help)))
  3109. (vp (list 'rear-nonsticky org-nonsticky-props
  3110. 'keymap org-mouse-map 'mouse-face 'highlight
  3111. ' font-lock-multiline t 'help-echo help)))
  3112. ;; We need to remove the invisible property here. Table narrowing
  3113. ;; may have made some of this invisible.
  3114. (remove-text-properties (match-beginning 0) (match-end 0)
  3115. '(invisible nil))
  3116. (if (match-end 3)
  3117. (progn
  3118. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3119. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3120. (add-text-properties (match-end 3) (match-end 0) ip))
  3121. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3122. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3123. (add-text-properties (match-end 1) (match-end 0) ip))
  3124. t)))
  3125. (defun org-activate-dates (limit)
  3126. "Run through the buffer and add overlays to dates."
  3127. (if (re-search-forward org-tsr-regexp-both limit t)
  3128. (progn
  3129. (add-text-properties (match-beginning 0) (match-end 0)
  3130. (list 'mouse-face 'highlight
  3131. 'rear-nonsticky org-nonsticky-props
  3132. 'keymap org-mouse-map))
  3133. (when org-display-custom-times
  3134. (if (match-end 3)
  3135. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3136. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3137. t)))
  3138. (defvar org-target-link-regexp nil
  3139. "Regular expression matching radio targets in plain text.")
  3140. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3141. "Regular expression matching a link target.")
  3142. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3143. "Regular expression matching a radio target.")
  3144. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3145. "Regular expression matching any target.")
  3146. (defun org-activate-target-links (limit)
  3147. "Run through the buffer and add overlays to target matches."
  3148. (when org-target-link-regexp
  3149. (let ((case-fold-search t))
  3150. (if (re-search-forward org-target-link-regexp limit t)
  3151. (progn
  3152. (add-text-properties (match-beginning 0) (match-end 0)
  3153. (list 'mouse-face 'highlight
  3154. 'rear-nonsticky org-nonsticky-props
  3155. 'keymap org-mouse-map
  3156. 'help-echo "Radio target link"
  3157. 'org-linked-text t))
  3158. t)))))
  3159. (defun org-update-radio-target-regexp ()
  3160. "Find all radio targets in this file and update the regular expression."
  3161. (interactive)
  3162. (when (memq 'radio org-activate-links)
  3163. (setq org-target-link-regexp
  3164. (org-make-target-link-regexp (org-all-targets 'radio)))
  3165. (org-restart-font-lock)))
  3166. (defun org-hide-wide-columns (limit)
  3167. (let (s e)
  3168. (setq s (text-property-any (point) (or limit (point-max))
  3169. 'org-cwidth t))
  3170. (when s
  3171. (setq e (next-single-property-change s 'org-cwidth))
  3172. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3173. (goto-char e)
  3174. t)))
  3175. (defvar org-latex-and-specials-regexp nil
  3176. "Regular expression for highlighting export special stuff.")
  3177. (defvar org-match-substring-regexp)
  3178. (defvar org-match-substring-with-braces-regexp)
  3179. (defvar org-export-html-special-string-regexps)
  3180. (defun org-compute-latex-and-specials-regexp ()
  3181. "Compute regular expression for stuff treated specially by exporters."
  3182. (if (not org-highlight-latex-fragments-and-specials)
  3183. (org-set-local 'org-latex-and-specials-regexp nil)
  3184. (require 'org-exp)
  3185. (let*
  3186. ((matchers (plist-get org-format-latex-options :matchers))
  3187. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3188. org-latex-regexps)))
  3189. (options (org-combine-plists (org-default-export-plist)
  3190. (org-infile-export-plist)))
  3191. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3192. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3193. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3194. (org-export-html-expand (plist-get options :expand-quoted-html))
  3195. (org-export-with-special-strings (plist-get options :special-strings))
  3196. (re-sub
  3197. (cond
  3198. ((equal org-export-with-sub-superscripts '{})
  3199. (list org-match-substring-with-braces-regexp))
  3200. (org-export-with-sub-superscripts
  3201. (list org-match-substring-regexp))
  3202. (t nil)))
  3203. (re-latex
  3204. (if org-export-with-LaTeX-fragments
  3205. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3206. (re-macros
  3207. (if org-export-with-TeX-macros
  3208. (list (concat "\\\\"
  3209. (regexp-opt
  3210. (append (mapcar 'car org-html-entities)
  3211. (if (boundp 'org-latex-entities)
  3212. org-latex-entities nil))
  3213. 'words))) ; FIXME
  3214. ))
  3215. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3216. (re-special (if org-export-with-special-strings
  3217. (mapcar (lambda (x) (car x))
  3218. org-export-html-special-string-regexps)))
  3219. (re-rest
  3220. (delq nil
  3221. (list
  3222. (if org-export-html-expand "@<[^>\n]+>")
  3223. ))))
  3224. (org-set-local
  3225. 'org-latex-and-specials-regexp
  3226. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3227. re-rest) "\\|")))))
  3228. (defun org-do-latex-and-special-faces (limit)
  3229. "Run through the buffer and add overlays to links."
  3230. (when org-latex-and-specials-regexp
  3231. (let (rtn d)
  3232. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3233. limit t))
  3234. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3235. 'face))
  3236. '(org-code org-verbatim underline)))
  3237. (progn
  3238. (setq rtn t
  3239. d (cond ((member (char-after (1+ (match-beginning 0)))
  3240. '(?_ ?^)) 1)
  3241. (t 0)))
  3242. (font-lock-prepend-text-property
  3243. (+ d (match-beginning 0)) (match-end 0)
  3244. 'face 'org-latex-and-export-specials)
  3245. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3246. '(font-lock-multiline t)))))
  3247. rtn)))
  3248. (defun org-restart-font-lock ()
  3249. "Restart font-lock-mode, to force refontification."
  3250. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3251. (font-lock-mode -1)
  3252. (font-lock-mode 1)))
  3253. (defun org-all-targets (&optional radio)
  3254. "Return a list of all targets in this file.
  3255. With optional argument RADIO, only find radio targets."
  3256. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3257. rtn)
  3258. (save-excursion
  3259. (goto-char (point-min))
  3260. (while (re-search-forward re nil t)
  3261. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3262. rtn)))
  3263. (defun org-make-target-link-regexp (targets)
  3264. "Make regular expression matching all strings in TARGETS.
  3265. The regular expression finds the targets also if there is a line break
  3266. between words."
  3267. (and targets
  3268. (concat
  3269. "\\<\\("
  3270. (mapconcat
  3271. (lambda (x)
  3272. (while (string-match " +" x)
  3273. (setq x (replace-match "\\s-+" t t x)))
  3274. x)
  3275. targets
  3276. "\\|")
  3277. "\\)\\>")))
  3278. (defun org-activate-tags (limit)
  3279. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3280. (progn
  3281. (add-text-properties (match-beginning 1) (match-end 1)
  3282. (list 'mouse-face 'highlight
  3283. 'rear-nonsticky org-nonsticky-props
  3284. 'keymap org-mouse-map))
  3285. t)))
  3286. (defun org-outline-level ()
  3287. (save-excursion
  3288. (looking-at outline-regexp)
  3289. (if (match-beginning 1)
  3290. (+ (org-get-string-indentation (match-string 1)) 1000)
  3291. (1- (- (match-end 0) (match-beginning 0))))))
  3292. (defvar org-font-lock-keywords nil)
  3293. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3294. "Regular expression matching a property line.")
  3295. (defvar org-font-lock-hook nil
  3296. "Functions to be called for special font lock stuff.")
  3297. (defun org-font-lock-hook (limit)
  3298. (run-hook-with-args 'org-font-lock-hook limit))
  3299. (defun org-set-font-lock-defaults ()
  3300. (let* ((em org-fontify-emphasized-text)
  3301. (lk org-activate-links)
  3302. (org-font-lock-extra-keywords
  3303. (list
  3304. ;; Call the hook
  3305. '(org-font-lock-hook)
  3306. ;; Headlines
  3307. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3308. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3309. ;; Table lines
  3310. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3311. (1 'org-table t))
  3312. ;; Table internals
  3313. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3314. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3315. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3316. ;; Drawers
  3317. (list org-drawer-regexp '(0 'org-special-keyword t))
  3318. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3319. ;; Properties
  3320. (list org-property-re
  3321. '(1 'org-special-keyword t)
  3322. '(3 'org-property-value t))
  3323. (if org-format-transports-properties-p
  3324. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3325. ;; Links
  3326. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3327. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3328. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3329. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3330. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3331. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3332. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3333. '(org-hide-wide-columns (0 nil append))
  3334. ;; TODO lines
  3335. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3336. '(1 (org-get-todo-face 1) t))
  3337. ;; DONE
  3338. (if org-fontify-done-headline
  3339. (list (concat "^[*]+ +\\<\\("
  3340. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3341. "\\)\\(.*\\)")
  3342. '(2 'org-headline-done t))
  3343. nil)
  3344. ;; Priorities
  3345. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3346. ;; Special keywords
  3347. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3348. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3349. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3350. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3351. ;; Emphasis
  3352. (if em
  3353. (if (featurep 'xemacs)
  3354. '(org-do-emphasis-faces (0 nil append))
  3355. '(org-do-emphasis-faces)))
  3356. ;; Checkboxes
  3357. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3358. 2 'bold prepend)
  3359. (if org-provide-checkbox-statistics
  3360. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3361. (0 (org-get-checkbox-statistics-face) t)))
  3362. ;; Description list items
  3363. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3364. 2 'bold prepend)
  3365. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3366. '(1 'org-archived prepend))
  3367. ;; Specials
  3368. '(org-do-latex-and-special-faces)
  3369. ;; Code
  3370. '(org-activate-code (1 'org-code t))
  3371. ;; COMMENT
  3372. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3373. "\\|" org-quote-string "\\)\\>")
  3374. '(1 'org-special-keyword t))
  3375. '("^#.*" (0 'font-lock-comment-face t))
  3376. )))
  3377. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3378. ;; Now set the full font-lock-keywords
  3379. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3380. (org-set-local 'font-lock-defaults
  3381. '(org-font-lock-keywords t nil nil backward-paragraph))
  3382. (kill-local-variable 'font-lock-keywords) nil))
  3383. (defvar org-m nil)
  3384. (defvar org-l nil)
  3385. (defvar org-f nil)
  3386. (defun org-get-level-face (n)
  3387. "Get the right face for match N in font-lock matching of healdines."
  3388. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3389. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3390. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3391. (cond
  3392. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3393. ((eq n 2) org-f)
  3394. (t (if org-level-color-stars-only nil org-f))))
  3395. (defun org-get-todo-face (kwd)
  3396. "Get the right face for a TODO keyword KWD.
  3397. If KWD is a number, get the corresponding match group."
  3398. (if (numberp kwd) (setq kwd (match-string kwd)))
  3399. (or (cdr (assoc kwd org-todo-keyword-faces))
  3400. (and (member kwd org-done-keywords) 'org-done)
  3401. 'org-todo))
  3402. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3403. "Remove fontification and activation overlays from links."
  3404. (font-lock-default-unfontify-region beg end)
  3405. (let* ((buffer-undo-list t)
  3406. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3407. (inhibit-modification-hooks t)
  3408. deactivate-mark buffer-file-name buffer-file-truename)
  3409. (remove-text-properties beg end
  3410. '(mouse-face t keymap t org-linked-text t
  3411. invisible t intangible t))))
  3412. ;;;; Visibility cycling, including org-goto and indirect buffer
  3413. ;;; Cycling
  3414. (defvar org-cycle-global-status nil)
  3415. (make-variable-buffer-local 'org-cycle-global-status)
  3416. (defvar org-cycle-subtree-status nil)
  3417. (make-variable-buffer-local 'org-cycle-subtree-status)
  3418. ;;;###autoload
  3419. (defun org-cycle (&optional arg)
  3420. "Visibility cycling for Org-mode.
  3421. - When this function is called with a prefix argument, rotate the entire
  3422. buffer through 3 states (global cycling)
  3423. 1. OVERVIEW: Show only top-level headlines.
  3424. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3425. 3. SHOW ALL: Show everything.
  3426. When called with two C-c C-u prefixes, switch to the startup visibility,
  3427. determined by the variable `org-startup-folded', and by any VISIBILITY
  3428. properties in the buffer.
  3429. - When point is at the beginning of a headline, rotate the subtree started
  3430. by this line through 3 different states (local cycling)
  3431. 1. FOLDED: Only the main headline is shown.
  3432. 2. CHILDREN: The main headline and the direct children are shown.
  3433. From this state, you can move to one of the children
  3434. and zoom in further.
  3435. 3. SUBTREE: Show the entire subtree, including body text.
  3436. - When there is a numeric prefix, go up to a heading with level ARG, do
  3437. a `show-subtree' and return to the previous cursor position. If ARG
  3438. is negative, go up that many levels.
  3439. - When point is not at the beginning of a headline, execute the global
  3440. binding for TAB, which is re-indenting the line. See the option
  3441. `org-cycle-emulate-tab' for details.
  3442. - Special case: if point is at the beginning of the buffer and there is
  3443. no headline in line 1, this function will act as if called with prefix arg.
  3444. But only if also the variable `org-cycle-global-at-bob' is t."
  3445. (interactive "P")
  3446. (org-load-modules-maybe)
  3447. (let* ((outline-regexp
  3448. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3449. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3450. outline-regexp))
  3451. (bob-special (and org-cycle-global-at-bob (bobp)
  3452. (not (looking-at outline-regexp))))
  3453. (org-cycle-hook
  3454. (if bob-special
  3455. (delq 'org-optimize-window-after-visibility-change
  3456. (copy-sequence org-cycle-hook))
  3457. org-cycle-hook))
  3458. (pos (point)))
  3459. (if (or bob-special (equal arg '(4)))
  3460. ;; special case: use global cycling
  3461. (setq arg t))
  3462. (cond
  3463. ((equal arg '(16))
  3464. (org-set-startup-visibility)
  3465. (message "Startup visibility, plus VISIBILITY properties."))
  3466. ((org-at-table-p 'any)
  3467. ;; Enter the table or move to the next field in the table
  3468. (or (org-table-recognize-table.el)
  3469. (progn
  3470. (if arg (org-table-edit-field t)
  3471. (org-table-justify-field-maybe)
  3472. (call-interactively 'org-table-next-field)))))
  3473. ((eq arg t) ;; Global cycling
  3474. (cond
  3475. ((and (eq last-command this-command)
  3476. (eq org-cycle-global-status 'overview))
  3477. ;; We just created the overview - now do table of contents
  3478. ;; This can be slow in very large buffers, so indicate action
  3479. (message "CONTENTS...")
  3480. (org-content)
  3481. (message "CONTENTS...done")
  3482. (setq org-cycle-global-status 'contents)
  3483. (run-hook-with-args 'org-cycle-hook 'contents))
  3484. ((and (eq last-command this-command)
  3485. (eq org-cycle-global-status 'contents))
  3486. ;; We just showed the table of contents - now show everything
  3487. (show-all)
  3488. (message "SHOW ALL")
  3489. (setq org-cycle-global-status 'all)
  3490. (run-hook-with-args 'org-cycle-hook 'all))
  3491. (t
  3492. ;; Default action: go to overview
  3493. (org-overview)
  3494. (message "OVERVIEW")
  3495. (setq org-cycle-global-status 'overview)
  3496. (run-hook-with-args 'org-cycle-hook 'overview))))
  3497. ((and org-drawers org-drawer-regexp
  3498. (save-excursion
  3499. (beginning-of-line 1)
  3500. (looking-at org-drawer-regexp)))
  3501. ;; Toggle block visibility
  3502. (org-flag-drawer
  3503. (not (get-char-property (match-end 0) 'invisible))))
  3504. ((integerp arg)
  3505. ;; Show-subtree, ARG levels up from here.
  3506. (save-excursion
  3507. (org-back-to-heading)
  3508. (outline-up-heading (if (< arg 0) (- arg)
  3509. (- (funcall outline-level) arg)))
  3510. (org-show-subtree)))
  3511. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3512. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3513. ;; At a heading: rotate between three different views
  3514. (org-back-to-heading)
  3515. (let ((goal-column 0) eoh eol eos)
  3516. ;; First, some boundaries
  3517. (save-excursion
  3518. (org-back-to-heading)
  3519. (save-excursion
  3520. (beginning-of-line 2)
  3521. (while (and (not (eobp)) ;; this is like `next-line'
  3522. (get-char-property (1- (point)) 'invisible))
  3523. (beginning-of-line 2)) (setq eol (point)))
  3524. (outline-end-of-heading) (setq eoh (point))
  3525. (org-end-of-subtree t)
  3526. (unless (eobp)
  3527. (skip-chars-forward " \t\n")
  3528. (beginning-of-line 1) ; in case this is an item
  3529. )
  3530. (setq eos (1- (point))))
  3531. ;; Find out what to do next and set `this-command'
  3532. (cond
  3533. ((= eos eoh)
  3534. ;; Nothing is hidden behind this heading
  3535. (message "EMPTY ENTRY")
  3536. (setq org-cycle-subtree-status nil)
  3537. (save-excursion
  3538. (goto-char eos)
  3539. (outline-next-heading)
  3540. (if (org-invisible-p) (org-flag-heading nil))))
  3541. ((or (>= eol eos)
  3542. (not (string-match "\\S-" (buffer-substring eol eos))))
  3543. ;; Entire subtree is hidden in one line: open it
  3544. (org-show-entry)
  3545. (show-children)
  3546. (message "CHILDREN")
  3547. (save-excursion
  3548. (goto-char eos)
  3549. (outline-next-heading)
  3550. (if (org-invisible-p) (org-flag-heading nil)))
  3551. (setq org-cycle-subtree-status 'children)
  3552. (run-hook-with-args 'org-cycle-hook 'children))
  3553. ((and (eq last-command this-command)
  3554. (eq org-cycle-subtree-status 'children))
  3555. ;; We just showed the children, now show everything.
  3556. (org-show-subtree)
  3557. (message "SUBTREE")
  3558. (setq org-cycle-subtree-status 'subtree)
  3559. (run-hook-with-args 'org-cycle-hook 'subtree))
  3560. (t
  3561. ;; Default action: hide the subtree.
  3562. (hide-subtree)
  3563. (message "FOLDED")
  3564. (setq org-cycle-subtree-status 'folded)
  3565. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3566. ;; TAB emulation and template completion
  3567. (buffer-read-only (org-back-to-heading))
  3568. ((org-try-structure-completion))
  3569. ((org-try-cdlatex-tab))
  3570. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3571. (or (not (bolp))
  3572. (not (looking-at outline-regexp))))
  3573. (call-interactively (global-key-binding "\t")))
  3574. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3575. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3576. (or (and (eq org-cycle-emulate-tab 'white)
  3577. (= (match-end 0) (point-at-eol)))
  3578. (and (eq org-cycle-emulate-tab 'whitestart)
  3579. (>= (match-end 0) pos))))
  3580. t
  3581. (eq org-cycle-emulate-tab t))
  3582. (call-interactively (global-key-binding "\t")))
  3583. (t (save-excursion
  3584. (org-back-to-heading)
  3585. (org-cycle))))))
  3586. ;;;###autoload
  3587. (defun org-global-cycle (&optional arg)
  3588. "Cycle the global visibility. For details see `org-cycle'.
  3589. With C-u prefix arg, switch to startup visibility.
  3590. With a numeric prefix, show all headlines up to that level."
  3591. (interactive "P")
  3592. (let ((org-cycle-include-plain-lists
  3593. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3594. (cond
  3595. ((integerp arg)
  3596. (show-all)
  3597. (hide-sublevels arg)
  3598. (setq org-cycle-global-status 'contents))
  3599. ((equal arg '(4))
  3600. (org-set-startup-visibility)
  3601. (message "Startup visibility, plus VISIBILITY properties."))
  3602. (t
  3603. (org-cycle '(4))))))
  3604. (defun org-set-startup-visibility ()
  3605. "Set the visibility required by startup options and properties."
  3606. (cond
  3607. ((eq org-startup-folded t)
  3608. (org-cycle '(4)))
  3609. ((eq org-startup-folded 'content)
  3610. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3611. (org-cycle '(4)) (org-cycle '(4)))))
  3612. (org-set-visibility-according-to-property 'no-cleanup)
  3613. (org-cycle-hide-archived-subtrees 'all)
  3614. (org-cycle-hide-drawers 'all)
  3615. (org-cycle-show-empty-lines 'all))
  3616. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3617. "Switch subtree visibilities according to :VISIBILITY: property."
  3618. (interactive)
  3619. (let (state)
  3620. (save-excursion
  3621. (goto-char (point-min))
  3622. (while (re-search-forward
  3623. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3624. nil t)
  3625. (setq state (match-string 1))
  3626. (save-excursion
  3627. (org-back-to-heading t)
  3628. (hide-subtree)
  3629. (org-reveal)
  3630. (cond
  3631. ((equal state '("fold" "folded"))
  3632. (hide-subtree))
  3633. ((equal state "children")
  3634. (org-show-hidden-entry)
  3635. (show-children))
  3636. ((equal state "content")
  3637. (save-excursion
  3638. (save-restriction
  3639. (org-narrow-to-subtree)
  3640. (org-content))))
  3641. ((member state '("all" "showall"))
  3642. (show-subtree)))))
  3643. (unless no-cleanup
  3644. (org-cycle-hide-archived-subtrees 'all)
  3645. (org-cycle-hide-drawers 'all)
  3646. (org-cycle-show-empty-lines 'all)))))
  3647. (defun org-overview ()
  3648. "Switch to overview mode, shoing only top-level headlines.
  3649. Really, this shows all headlines with level equal or greater than the level
  3650. of the first headline in the buffer. This is important, because if the
  3651. first headline is not level one, then (hide-sublevels 1) gives confusing
  3652. results."
  3653. (interactive)
  3654. (let ((level (save-excursion
  3655. (goto-char (point-min))
  3656. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3657. (progn
  3658. (goto-char (match-beginning 0))
  3659. (funcall outline-level))))))
  3660. (and level (hide-sublevels level))))
  3661. (defun org-content (&optional arg)
  3662. "Show all headlines in the buffer, like a table of contents.
  3663. With numerical argument N, show content up to level N."
  3664. (interactive "P")
  3665. (save-excursion
  3666. ;; Visit all headings and show their offspring
  3667. (and (integerp arg) (org-overview))
  3668. (goto-char (point-max))
  3669. (catch 'exit
  3670. (while (and (progn (condition-case nil
  3671. (outline-previous-visible-heading 1)
  3672. (error (goto-char (point-min))))
  3673. t)
  3674. (looking-at outline-regexp))
  3675. (if (integerp arg)
  3676. (show-children (1- arg))
  3677. (show-branches))
  3678. (if (bobp) (throw 'exit nil))))))
  3679. (defun org-optimize-window-after-visibility-change (state)
  3680. "Adjust the window after a change in outline visibility.
  3681. This function is the default value of the hook `org-cycle-hook'."
  3682. (when (get-buffer-window (current-buffer))
  3683. (cond
  3684. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3685. ; ((eq state 'overview) (org-beginning-of-line))
  3686. ((eq state 'content) nil)
  3687. ((eq state 'all) nil)
  3688. ((eq state 'folded) nil)
  3689. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3690. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3691. (defun org-compact-display-after-subtree-move ()
  3692. (let (beg end)
  3693. (save-excursion
  3694. (if (org-up-heading-safe)
  3695. (progn
  3696. (hide-subtree)
  3697. (show-entry)
  3698. (show-children)
  3699. (org-cycle-show-empty-lines 'children)
  3700. (org-cycle-hide-drawers 'children))
  3701. (org-overview)))))
  3702. (defun org-cycle-show-empty-lines (state)
  3703. "Show empty lines above all visible headlines.
  3704. The region to be covered depends on STATE when called through
  3705. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3706. entire buffer covered. Note that an empty line is only shown if there
  3707. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3708. (when (> org-cycle-separator-lines 0)
  3709. (save-excursion
  3710. (let* ((n org-cycle-separator-lines)
  3711. (re (cond
  3712. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3713. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3714. (t (let ((ns (number-to-string (- n 2))))
  3715. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3716. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3717. beg end)
  3718. (cond
  3719. ((memq state '(overview contents t))
  3720. (setq beg (point-min) end (point-max)))
  3721. ((memq state '(children folded))
  3722. (setq beg (point) end (progn (org-end-of-subtree t t)
  3723. (beginning-of-line 2)
  3724. (point)))))
  3725. (when beg
  3726. (goto-char beg)
  3727. (while (re-search-forward re end t)
  3728. (if (not (get-char-property (match-end 1) 'invisible))
  3729. (outline-flag-region
  3730. (match-beginning 1) (match-end 1) nil)))))))
  3731. ;; Never hide empty lines at the end of the file.
  3732. (save-excursion
  3733. (goto-char (point-max))
  3734. (outline-previous-heading)
  3735. (outline-end-of-heading)
  3736. (if (and (looking-at "[ \t\n]+")
  3737. (= (match-end 0) (point-max)))
  3738. (outline-flag-region (point) (match-end 0) nil))))
  3739. (defun org-cycle-hide-drawers (state)
  3740. "Re-hide all drawers after a visibility state change."
  3741. (when (and (org-mode-p)
  3742. (not (memq state '(overview folded))))
  3743. (save-excursion
  3744. (let* ((globalp (memq state '(contents all)))
  3745. (beg (if globalp (point-min) (point)))
  3746. (end (if globalp (point-max) (org-end-of-subtree t))))
  3747. (goto-char beg)
  3748. (while (re-search-forward org-drawer-regexp end t)
  3749. (org-flag-drawer t))))))
  3750. (defun org-flag-drawer (flag)
  3751. (save-excursion
  3752. (beginning-of-line 1)
  3753. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3754. (let ((b (match-end 0))
  3755. (outline-regexp org-outline-regexp))
  3756. (if (re-search-forward
  3757. "^[ \t]*:END:"
  3758. (save-excursion (outline-next-heading) (point)) t)
  3759. (outline-flag-region b (point-at-eol) flag)
  3760. (error ":END: line missing"))))))
  3761. (defun org-subtree-end-visible-p ()
  3762. "Is the end of the current subtree visible?"
  3763. (pos-visible-in-window-p
  3764. (save-excursion (org-end-of-subtree t) (point))))
  3765. (defun org-first-headline-recenter (&optional N)
  3766. "Move cursor to the first headline and recenter the headline.
  3767. Optional argument N means, put the headline into the Nth line of the window."
  3768. (goto-char (point-min))
  3769. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3770. (beginning-of-line)
  3771. (recenter (prefix-numeric-value N))))
  3772. ;;; Org-goto
  3773. (defvar org-goto-window-configuration nil)
  3774. (defvar org-goto-marker nil)
  3775. (defvar org-goto-map
  3776. (let ((map (make-sparse-keymap)))
  3777. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3778. (while (setq cmd (pop cmds))
  3779. (substitute-key-definition cmd cmd map global-map)))
  3780. (suppress-keymap map)
  3781. (org-defkey map "\C-m" 'org-goto-ret)
  3782. (org-defkey map [(return)] 'org-goto-ret)
  3783. (org-defkey map [(left)] 'org-goto-left)
  3784. (org-defkey map [(right)] 'org-goto-right)
  3785. (org-defkey map [(control ?g)] 'org-goto-quit)
  3786. (org-defkey map "\C-i" 'org-cycle)
  3787. (org-defkey map [(tab)] 'org-cycle)
  3788. (org-defkey map [(down)] 'outline-next-visible-heading)
  3789. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3790. (if org-goto-auto-isearch
  3791. (if (fboundp 'define-key-after)
  3792. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3793. nil)
  3794. (org-defkey map "q" 'org-goto-quit)
  3795. (org-defkey map "n" 'outline-next-visible-heading)
  3796. (org-defkey map "p" 'outline-previous-visible-heading)
  3797. (org-defkey map "f" 'outline-forward-same-level)
  3798. (org-defkey map "b" 'outline-backward-same-level)
  3799. (org-defkey map "u" 'outline-up-heading))
  3800. (org-defkey map "/" 'org-occur)
  3801. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3802. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3803. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3804. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3805. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3806. map))
  3807. (defconst org-goto-help
  3808. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3809. RET=jump to location [Q]uit and return to previous location
  3810. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3811. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3812. ;; FIXME: Docstring doe not mention both interfaces
  3813. (defun org-goto (&optional alternative-interface)
  3814. "Look up a different location in the current file, keeping current visibility.
  3815. When you want look-up or go to a different location in a document, the
  3816. fastest way is often to fold the entire buffer and then dive into the tree.
  3817. This method has the disadvantage, that the previous location will be folded,
  3818. which may not be what you want.
  3819. This command works around this by showing a copy of the current buffer
  3820. in an indirect buffer, in overview mode. You can dive into the tree in
  3821. that copy, use org-occur and incremental search to find a location.
  3822. When pressing RET or `Q', the command returns to the original buffer in
  3823. which the visibility is still unchanged. After RET is will also jump to
  3824. the location selected in the indirect buffer and expose the
  3825. the headline hierarchy above."
  3826. (interactive "P")
  3827. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3828. (org-refile-use-outline-path t)
  3829. (interface
  3830. (if (not alternative-interface)
  3831. org-goto-interface
  3832. (if (eq org-goto-interface 'outline)
  3833. 'outline-path-completion
  3834. 'outline)))
  3835. (org-goto-start-pos (point))
  3836. (selected-point
  3837. (if (eq interface 'outline)
  3838. (car (org-get-location (current-buffer) org-goto-help))
  3839. (nth 3 (org-refile-get-location "Goto: ")))))
  3840. (if selected-point
  3841. (progn
  3842. (org-mark-ring-push org-goto-start-pos)
  3843. (goto-char selected-point)
  3844. (if (or (org-invisible-p) (org-invisible-p2))
  3845. (org-show-context 'org-goto)))
  3846. (message "Quit"))))
  3847. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3848. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3849. (defvar org-goto-local-auto-isearch-map) ; defined below
  3850. (defun org-get-location (buf help)
  3851. "Let the user select a location in the Org-mode buffer BUF.
  3852. This function uses a recursive edit. It returns the selected position
  3853. or nil."
  3854. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3855. (isearch-hide-immediately nil)
  3856. (isearch-search-fun-function
  3857. (lambda () 'org-goto-local-search-forward-headings))
  3858. (org-goto-selected-point org-goto-exit-command))
  3859. (save-excursion
  3860. (save-window-excursion
  3861. (delete-other-windows)
  3862. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3863. (switch-to-buffer
  3864. (condition-case nil
  3865. (make-indirect-buffer (current-buffer) "*org-goto*")
  3866. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3867. (with-output-to-temp-buffer "*Help*"
  3868. (princ help))
  3869. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3870. (setq buffer-read-only nil)
  3871. (let ((org-startup-truncated t)
  3872. (org-startup-folded nil)
  3873. (org-startup-align-all-tables nil))
  3874. (org-mode)
  3875. (org-overview))
  3876. (setq buffer-read-only t)
  3877. (if (and (boundp 'org-goto-start-pos)
  3878. (integer-or-marker-p org-goto-start-pos))
  3879. (let ((org-show-hierarchy-above t)
  3880. (org-show-siblings t)
  3881. (org-show-following-heading t))
  3882. (goto-char org-goto-start-pos)
  3883. (and (org-invisible-p) (org-show-context)))
  3884. (goto-char (point-min)))
  3885. (org-beginning-of-line)
  3886. (message "Select location and press RET")
  3887. (use-local-map org-goto-map)
  3888. (recursive-edit)
  3889. ))
  3890. (kill-buffer "*org-goto*")
  3891. (cons org-goto-selected-point org-goto-exit-command)))
  3892. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3893. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3894. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3895. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3896. (defun org-goto-local-search-forward-headings (string bound noerror)
  3897. "Search and make sure that anu matches are in headlines."
  3898. (catch 'return
  3899. (while (search-forward string bound noerror)
  3900. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3901. (and (member :headline context)
  3902. (not (member :tags context))))
  3903. (throw 'return (point))))))
  3904. (defun org-goto-local-auto-isearch ()
  3905. "Start isearch."
  3906. (interactive)
  3907. (goto-char (point-min))
  3908. (let ((keys (this-command-keys)))
  3909. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3910. (isearch-mode t)
  3911. (isearch-process-search-char (string-to-char keys)))))
  3912. (defun org-goto-ret (&optional arg)
  3913. "Finish `org-goto' by going to the new location."
  3914. (interactive "P")
  3915. (setq org-goto-selected-point (point)
  3916. org-goto-exit-command 'return)
  3917. (throw 'exit nil))
  3918. (defun org-goto-left ()
  3919. "Finish `org-goto' by going to the new location."
  3920. (interactive)
  3921. (if (org-on-heading-p)
  3922. (progn
  3923. (beginning-of-line 1)
  3924. (setq org-goto-selected-point (point)
  3925. org-goto-exit-command 'left)
  3926. (throw 'exit nil))
  3927. (error "Not on a heading")))
  3928. (defun org-goto-right ()
  3929. "Finish `org-goto' by going to the new location."
  3930. (interactive)
  3931. (if (org-on-heading-p)
  3932. (progn
  3933. (setq org-goto-selected-point (point)
  3934. org-goto-exit-command 'right)
  3935. (throw 'exit nil))
  3936. (error "Not on a heading")))
  3937. (defun org-goto-quit ()
  3938. "Finish `org-goto' without cursor motion."
  3939. (interactive)
  3940. (setq org-goto-selected-point nil)
  3941. (setq org-goto-exit-command 'quit)
  3942. (throw 'exit nil))
  3943. ;;; Indirect buffer display of subtrees
  3944. (defvar org-indirect-dedicated-frame nil
  3945. "This is the frame being used for indirect tree display.")
  3946. (defvar org-last-indirect-buffer nil)
  3947. (defun org-tree-to-indirect-buffer (&optional arg)
  3948. "Create indirect buffer and narrow it to current subtree.
  3949. With numerical prefix ARG, go up to this level and then take that tree.
  3950. If ARG is negative, go up that many levels.
  3951. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  3952. indirect buffer previously made with this command, to avoid proliferation of
  3953. indirect buffers. However, when you call the command with a `C-u' prefix, or
  3954. when `org-indirect-buffer-display' is `new-frame', the last buffer
  3955. is kept so that you can work with several indirect buffers at the same time.
  3956. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  3957. requests that a new frame be made for the new buffer, so that the dedicated
  3958. frame is not changed."
  3959. (interactive "P")
  3960. (let ((cbuf (current-buffer))
  3961. (cwin (selected-window))
  3962. (pos (point))
  3963. beg end level heading ibuf)
  3964. (save-excursion
  3965. (org-back-to-heading t)
  3966. (when (numberp arg)
  3967. (setq level (org-outline-level))
  3968. (if (< arg 0) (setq arg (+ level arg)))
  3969. (while (> (setq level (org-outline-level)) arg)
  3970. (outline-up-heading 1 t)))
  3971. (setq beg (point)
  3972. heading (org-get-heading))
  3973. (org-end-of-subtree t) (setq end (point)))
  3974. (if (and (buffer-live-p org-last-indirect-buffer)
  3975. (not (eq org-indirect-buffer-display 'new-frame))
  3976. (not arg))
  3977. (kill-buffer org-last-indirect-buffer))
  3978. (setq ibuf (org-get-indirect-buffer cbuf)
  3979. org-last-indirect-buffer ibuf)
  3980. (cond
  3981. ((or (eq org-indirect-buffer-display 'new-frame)
  3982. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  3983. (select-frame (make-frame))
  3984. (delete-other-windows)
  3985. (switch-to-buffer ibuf)
  3986. (org-set-frame-title heading))
  3987. ((eq org-indirect-buffer-display 'dedicated-frame)
  3988. (raise-frame
  3989. (select-frame (or (and org-indirect-dedicated-frame
  3990. (frame-live-p org-indirect-dedicated-frame)
  3991. org-indirect-dedicated-frame)
  3992. (setq org-indirect-dedicated-frame (make-frame)))))
  3993. (delete-other-windows)
  3994. (switch-to-buffer ibuf)
  3995. (org-set-frame-title (concat "Indirect: " heading)))
  3996. ((eq org-indirect-buffer-display 'current-window)
  3997. (switch-to-buffer ibuf))
  3998. ((eq org-indirect-buffer-display 'other-window)
  3999. (pop-to-buffer ibuf))
  4000. (t (error "Invalid value.")))
  4001. (if (featurep 'xemacs)
  4002. (save-excursion (org-mode) (turn-on-font-lock)))
  4003. (narrow-to-region beg end)
  4004. (show-all)
  4005. (goto-char pos)
  4006. (and (window-live-p cwin) (select-window cwin))))
  4007. (defun org-get-indirect-buffer (&optional buffer)
  4008. (setq buffer (or buffer (current-buffer)))
  4009. (let ((n 1) (base (buffer-name buffer)) bname)
  4010. (while (buffer-live-p
  4011. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4012. (setq n (1+ n)))
  4013. (condition-case nil
  4014. (make-indirect-buffer buffer bname 'clone)
  4015. (error (make-indirect-buffer buffer bname)))))
  4016. (defun org-set-frame-title (title)
  4017. "Set the title of the current frame to the string TITLE."
  4018. ;; FIXME: how to name a single frame in XEmacs???
  4019. (unless (featurep 'xemacs)
  4020. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4021. ;;;; Structure editing
  4022. ;;; Inserting headlines
  4023. (defun org-insert-heading (&optional force-heading)
  4024. "Insert a new heading or item with same depth at point.
  4025. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4026. If point is at the beginning of a headline, insert a sibling before the
  4027. current headline. If point is not at the beginning, do not split the line,
  4028. but create the new hedline after the current line."
  4029. (interactive "P")
  4030. (if (= (buffer-size) 0)
  4031. (insert "\n* ")
  4032. (when (or force-heading (not (org-insert-item)))
  4033. (let* ((head (save-excursion
  4034. (condition-case nil
  4035. (progn
  4036. (org-back-to-heading)
  4037. (match-string 0))
  4038. (error "*"))))
  4039. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4040. pos)
  4041. (cond
  4042. ((and (org-on-heading-p) (bolp)
  4043. (or (bobp)
  4044. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4045. ;; insert before the current line
  4046. (open-line (if blank 2 1)))
  4047. ((and (bolp)
  4048. (or (bobp)
  4049. (save-excursion
  4050. (backward-char 1) (not (org-invisible-p)))))
  4051. ;; insert right here
  4052. nil)
  4053. (t
  4054. ;; in the middle of the line
  4055. (org-show-entry)
  4056. (let ((split
  4057. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4058. tags pos)
  4059. (if (org-on-heading-p)
  4060. (progn
  4061. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4062. (setq tags (and (match-end 2) (match-string 2)))
  4063. (and (match-end 1)
  4064. (delete-region (match-beginning 1) (match-end 1)))
  4065. (setq pos (point-at-bol))
  4066. (or split (end-of-line 1))
  4067. (delete-horizontal-space)
  4068. (newline (if blank 2 1))
  4069. (when tags
  4070. (save-excursion
  4071. (goto-char pos)
  4072. (end-of-line 1)
  4073. (insert " " tags)
  4074. (org-set-tags nil 'align))))
  4075. (or split (end-of-line 1))
  4076. (newline (if blank 2 1))))))
  4077. (insert head) (just-one-space)
  4078. (setq pos (point))
  4079. (end-of-line 1)
  4080. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4081. (run-hooks 'org-insert-heading-hook)))))
  4082. (defun org-get-heading (&optional no-tags)
  4083. "Return the heading of the current entry, without the stars."
  4084. (save-excursion
  4085. (org-back-to-heading t)
  4086. (if (looking-at
  4087. (if no-tags
  4088. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4089. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4090. (match-string 1) "")))
  4091. (defun org-insert-heading-after-current ()
  4092. "Insert a new heading with same level as current, after current subtree."
  4093. (interactive)
  4094. (org-back-to-heading)
  4095. (org-insert-heading)
  4096. (org-move-subtree-down)
  4097. (end-of-line 1))
  4098. (defun org-insert-todo-heading (arg)
  4099. "Insert a new heading with the same level and TODO state as current heading.
  4100. If the heading has no TODO state, or if the state is DONE, use the first
  4101. state (TODO by default). Also with prefix arg, force first state."
  4102. (interactive "P")
  4103. (when (not (org-insert-item 'checkbox))
  4104. (org-insert-heading)
  4105. (save-excursion
  4106. (org-back-to-heading)
  4107. (outline-previous-heading)
  4108. (looking-at org-todo-line-regexp))
  4109. (if (or arg
  4110. (not (match-beginning 2))
  4111. (member (match-string 2) org-done-keywords))
  4112. (insert (car org-todo-keywords-1) " ")
  4113. (insert (match-string 2) " "))
  4114. (when org-provide-todo-statistics
  4115. (org-update-parent-todo-statistics))))
  4116. (defun org-insert-subheading (arg)
  4117. "Insert a new subheading and demote it.
  4118. Works for outline headings and for plain lists alike."
  4119. (interactive "P")
  4120. (org-insert-heading arg)
  4121. (cond
  4122. ((org-on-heading-p) (org-do-demote))
  4123. ((org-at-item-p) (org-indent-item 1))))
  4124. (defun org-insert-todo-subheading (arg)
  4125. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4126. Works for outline headings and for plain lists alike."
  4127. (interactive "P")
  4128. (org-insert-todo-heading arg)
  4129. (cond
  4130. ((org-on-heading-p) (org-do-demote))
  4131. ((org-at-item-p) (org-indent-item 1))))
  4132. ;;; Promotion and Demotion
  4133. (defun org-promote-subtree ()
  4134. "Promote the entire subtree.
  4135. See also `org-promote'."
  4136. (interactive)
  4137. (save-excursion
  4138. (org-map-tree 'org-promote))
  4139. (org-fix-position-after-promote))
  4140. (defun org-demote-subtree ()
  4141. "Demote the entire subtree. See `org-demote'.
  4142. See also `org-promote'."
  4143. (interactive)
  4144. (save-excursion
  4145. (org-map-tree 'org-demote))
  4146. (org-fix-position-after-promote))
  4147. (defun org-do-promote ()
  4148. "Promote the current heading higher up the tree.
  4149. If the region is active in `transient-mark-mode', promote all headings
  4150. in the region."
  4151. (interactive)
  4152. (save-excursion
  4153. (if (org-region-active-p)
  4154. (org-map-region 'org-promote (region-beginning) (region-end))
  4155. (org-promote)))
  4156. (org-fix-position-after-promote))
  4157. (defun org-do-demote ()
  4158. "Demote the current heading lower down the tree.
  4159. If the region is active in `transient-mark-mode', demote all headings
  4160. in the region."
  4161. (interactive)
  4162. (save-excursion
  4163. (if (org-region-active-p)
  4164. (org-map-region 'org-demote (region-beginning) (region-end))
  4165. (org-demote)))
  4166. (org-fix-position-after-promote))
  4167. (defun org-fix-position-after-promote ()
  4168. "Make sure that after pro/demotion cursor position is right."
  4169. (let ((pos (point)))
  4170. (when (save-excursion
  4171. (beginning-of-line 1)
  4172. (looking-at org-todo-line-regexp)
  4173. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4174. (cond ((eobp) (insert " "))
  4175. ((eolp) (insert " "))
  4176. ((equal (char-after) ?\ ) (forward-char 1))))))
  4177. (defun org-reduced-level (l)
  4178. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4179. (defun org-get-valid-level (level &optional change)
  4180. "Rectify a level change under the influence of `org-odd-levels-only'
  4181. LEVEL is a current level, CHANGE is by how much the level should be
  4182. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4183. even level numbers will become the next higher odd number."
  4184. (if org-odd-levels-only
  4185. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4186. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4187. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4188. (max 1 (+ level change))))
  4189. (if (boundp 'define-obsolete-function-alias)
  4190. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4191. (define-obsolete-function-alias 'org-get-legal-level
  4192. 'org-get-valid-level)
  4193. (define-obsolete-function-alias 'org-get-legal-level
  4194. 'org-get-valid-level "23.1")))
  4195. (defun org-promote ()
  4196. "Promote the current heading higher up the tree.
  4197. If the region is active in `transient-mark-mode', promote all headings
  4198. in the region."
  4199. (org-back-to-heading t)
  4200. (let* ((level (save-match-data (funcall outline-level)))
  4201. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4202. (diff (abs (- level (length up-head) -1))))
  4203. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4204. (replace-match up-head nil t)
  4205. ;; Fixup tag positioning
  4206. (and org-auto-align-tags (org-set-tags nil t))
  4207. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4208. (defun org-demote ()
  4209. "Demote the current heading lower down the tree.
  4210. If the region is active in `transient-mark-mode', demote all headings
  4211. in the region."
  4212. (org-back-to-heading t)
  4213. (let* ((level (save-match-data (funcall outline-level)))
  4214. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4215. (diff (abs (- level (length down-head) -1))))
  4216. (replace-match down-head nil t)
  4217. ;; Fixup tag positioning
  4218. (and org-auto-align-tags (org-set-tags nil t))
  4219. (if org-adapt-indentation (org-fixup-indentation diff))))
  4220. (defun org-map-tree (fun)
  4221. "Call FUN for every heading underneath the current one."
  4222. (org-back-to-heading)
  4223. (let ((level (funcall outline-level)))
  4224. (save-excursion
  4225. (funcall fun)
  4226. (while (and (progn
  4227. (outline-next-heading)
  4228. (> (funcall outline-level) level))
  4229. (not (eobp)))
  4230. (funcall fun)))))
  4231. (defun org-map-region (fun beg end)
  4232. "Call FUN for every heading between BEG and END."
  4233. (let ((org-ignore-region t))
  4234. (save-excursion
  4235. (setq end (copy-marker end))
  4236. (goto-char beg)
  4237. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4238. (< (point) end))
  4239. (funcall fun))
  4240. (while (and (progn
  4241. (outline-next-heading)
  4242. (< (point) end))
  4243. (not (eobp)))
  4244. (funcall fun)))))
  4245. (defun org-fixup-indentation (diff)
  4246. "Change the indentation in the current entry by DIFF
  4247. However, if any line in the current entry has no indentation, or if it
  4248. would end up with no indentation after the change, nothing at all is done."
  4249. (save-excursion
  4250. (let ((end (save-excursion (outline-next-heading)
  4251. (point-marker)))
  4252. (prohibit (if (> diff 0)
  4253. "^\\S-"
  4254. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4255. col)
  4256. (unless (save-excursion (end-of-line 1)
  4257. (re-search-forward prohibit end t))
  4258. (while (and (< (point) end)
  4259. (re-search-forward "^[ \t]+" end t))
  4260. (goto-char (match-end 0))
  4261. (setq col (current-column))
  4262. (if (< diff 0) (replace-match ""))
  4263. (indent-to (+ diff col))))
  4264. (move-marker end nil))))
  4265. (defun org-convert-to-odd-levels ()
  4266. "Convert an org-mode file with all levels allowed to one with odd levels.
  4267. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4268. level 5 etc."
  4269. (interactive)
  4270. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4271. (let ((org-odd-levels-only nil) n)
  4272. (save-excursion
  4273. (goto-char (point-min))
  4274. (while (re-search-forward "^\\*\\*+ " nil t)
  4275. (setq n (- (length (match-string 0)) 2))
  4276. (while (>= (setq n (1- n)) 0)
  4277. (org-demote))
  4278. (end-of-line 1))))))
  4279. (defun org-convert-to-oddeven-levels ()
  4280. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4281. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4282. section with an even level, conversion would destroy the structure of the file. An error
  4283. is signaled in this case."
  4284. (interactive)
  4285. (goto-char (point-min))
  4286. ;; First check if there are no even levels
  4287. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4288. (org-show-context t)
  4289. (error "Not all levels are odd in this file. Conversion not possible."))
  4290. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4291. (let ((org-odd-levels-only nil) n)
  4292. (save-excursion
  4293. (goto-char (point-min))
  4294. (while (re-search-forward "^\\*\\*+ " nil t)
  4295. (setq n (/ (1- (length (match-string 0))) 2))
  4296. (while (>= (setq n (1- n)) 0)
  4297. (org-promote))
  4298. (end-of-line 1))))))
  4299. (defun org-tr-level (n)
  4300. "Make N odd if required."
  4301. (if org-odd-levels-only (1+ (/ n 2)) n))
  4302. ;;; Vertical tree motion, cutting and pasting of subtrees
  4303. (defun org-move-subtree-up (&optional arg)
  4304. "Move the current subtree up past ARG headlines of the same level."
  4305. (interactive "p")
  4306. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4307. (defun org-move-subtree-down (&optional arg)
  4308. "Move the current subtree down past ARG headlines of the same level."
  4309. (interactive "p")
  4310. (setq arg (prefix-numeric-value arg))
  4311. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4312. 'outline-get-last-sibling))
  4313. (ins-point (make-marker))
  4314. (cnt (abs arg))
  4315. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4316. ;; Select the tree
  4317. (org-back-to-heading)
  4318. (setq beg0 (point))
  4319. (save-excursion
  4320. (setq ne-beg (org-back-over-empty-lines))
  4321. (setq beg (point)))
  4322. (save-match-data
  4323. (save-excursion (outline-end-of-heading)
  4324. (setq folded (org-invisible-p)))
  4325. (outline-end-of-subtree))
  4326. (outline-next-heading)
  4327. (setq ne-end (org-back-over-empty-lines))
  4328. (setq end (point))
  4329. (goto-char beg0)
  4330. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4331. ;; include less whitespace
  4332. (save-excursion
  4333. (goto-char beg)
  4334. (forward-line (- ne-beg ne-end))
  4335. (setq beg (point))))
  4336. ;; Find insertion point, with error handling
  4337. (while (> cnt 0)
  4338. (or (and (funcall movfunc) (looking-at outline-regexp))
  4339. (progn (goto-char beg0)
  4340. (error "Cannot move past superior level or buffer limit")))
  4341. (setq cnt (1- cnt)))
  4342. (if (> arg 0)
  4343. ;; Moving forward - still need to move over subtree
  4344. (progn (org-end-of-subtree t t)
  4345. (save-excursion
  4346. (org-back-over-empty-lines)
  4347. (or (bolp) (newline)))))
  4348. (setq ne-ins (org-back-over-empty-lines))
  4349. (move-marker ins-point (point))
  4350. (setq txt (buffer-substring beg end))
  4351. (org-save-markers-in-region beg end)
  4352. (delete-region beg end)
  4353. (outline-flag-region (1- beg) beg nil)
  4354. (outline-flag-region (1- (point)) (point) nil)
  4355. (let ((bbb (point)))
  4356. (insert-before-markers txt)
  4357. (org-reinstall-markers-in-region bbb)
  4358. (move-marker ins-point bbb))
  4359. (or (bolp) (insert "\n"))
  4360. (setq ins-end (point))
  4361. (goto-char ins-point)
  4362. (org-skip-whitespace)
  4363. (when (and (< arg 0)
  4364. (org-first-sibling-p)
  4365. (> ne-ins ne-beg))
  4366. ;; Move whitespace back to beginning
  4367. (save-excursion
  4368. (goto-char ins-end)
  4369. (let ((kill-whole-line t))
  4370. (kill-line (- ne-ins ne-beg)) (point)))
  4371. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4372. (move-marker ins-point nil)
  4373. (org-compact-display-after-subtree-move)
  4374. (unless folded
  4375. (org-show-entry)
  4376. (show-children)
  4377. (org-cycle-hide-drawers 'children))))
  4378. (defvar org-subtree-clip ""
  4379. "Clipboard for cut and paste of subtrees.
  4380. This is actually only a copy of the kill, because we use the normal kill
  4381. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4382. (defvar org-subtree-clip-folded nil
  4383. "Was the last copied subtree folded?
  4384. This is used to fold the tree back after pasting.")
  4385. (defun org-cut-subtree (&optional n)
  4386. "Cut the current subtree into the clipboard.
  4387. With prefix arg N, cut this many sequential subtrees.
  4388. This is a short-hand for marking the subtree and then cutting it."
  4389. (interactive "p")
  4390. (org-copy-subtree n 'cut))
  4391. (defun org-copy-subtree (&optional n cut force-store-markers)
  4392. "Cut the current subtree into the clipboard.
  4393. With prefix arg N, cut this many sequential subtrees.
  4394. This is a short-hand for marking the subtree and then copying it.
  4395. If CUT is non-nil, actually cut the subtree.
  4396. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4397. of some markers in the region, even if CUT is non-nil. This is
  4398. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4399. (interactive "p")
  4400. (let (beg end folded (beg0 (point)))
  4401. (if (interactive-p)
  4402. (org-back-to-heading nil) ; take what looks like a subtree
  4403. (org-back-to-heading t)) ; take what is really there
  4404. (org-back-over-empty-lines)
  4405. (setq beg (point))
  4406. (skip-chars-forward " \t\r\n")
  4407. (save-match-data
  4408. (save-excursion (outline-end-of-heading)
  4409. (setq folded (org-invisible-p)))
  4410. (condition-case nil
  4411. (outline-forward-same-level (1- n))
  4412. (error nil))
  4413. (org-end-of-subtree t t))
  4414. (org-back-over-empty-lines)
  4415. (setq end (point))
  4416. (goto-char beg0)
  4417. (when (> end beg)
  4418. (setq org-subtree-clip-folded folded)
  4419. (when (or cut force-store-markers)
  4420. (org-save-markers-in-region beg end))
  4421. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4422. (setq org-subtree-clip (current-kill 0))
  4423. (message "%s: Subtree(s) with %d characters"
  4424. (if cut "Cut" "Copied")
  4425. (length org-subtree-clip)))))
  4426. (defun org-paste-subtree (&optional level tree)
  4427. "Paste the clipboard as a subtree, with modification of headline level.
  4428. The entire subtree is promoted or demoted in order to match a new headline
  4429. level. By default, the new level is derived from the visible headings
  4430. before and after the insertion point, and taken to be the inferior headline
  4431. level of the two. So if the previous visible heading is level 3 and the
  4432. next is level 4 (or vice versa), level 4 will be used for insertion.
  4433. This makes sure that the subtree remains an independent subtree and does
  4434. not swallow low level entries.
  4435. You can also force a different level, either by using a numeric prefix
  4436. argument, or by inserting the heading marker by hand. For example, if the
  4437. cursor is after \"*****\", then the tree will be shifted to level 5.
  4438. If you want to insert the tree as is, just use \\[yank].
  4439. If optional TREE is given, use this text instead of the kill ring."
  4440. (interactive "P")
  4441. (unless (org-kill-is-subtree-p tree)
  4442. (error "%s"
  4443. (substitute-command-keys
  4444. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4445. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4446. (^re (concat "^\\(" outline-regexp "\\)"))
  4447. (re (concat "\\(" outline-regexp "\\)"))
  4448. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4449. (old-level (if (string-match ^re txt)
  4450. (- (match-end 0) (match-beginning 0) 1)
  4451. -1))
  4452. (force-level (cond (level (prefix-numeric-value level))
  4453. ((string-match
  4454. ^re_ (buffer-substring (point-at-bol) (point)))
  4455. (- (match-end 1) (match-beginning 1)))
  4456. (t nil)))
  4457. (previous-level (save-excursion
  4458. (condition-case nil
  4459. (progn
  4460. (outline-previous-visible-heading 1)
  4461. (if (looking-at re)
  4462. (- (match-end 0) (match-beginning 0) 1)
  4463. 1))
  4464. (error 1))))
  4465. (next-level (save-excursion
  4466. (condition-case nil
  4467. (progn
  4468. (or (looking-at outline-regexp)
  4469. (outline-next-visible-heading 1))
  4470. (if (looking-at re)
  4471. (- (match-end 0) (match-beginning 0) 1)
  4472. 1))
  4473. (error 1))))
  4474. (new-level (or force-level (max previous-level next-level)))
  4475. (shift (if (or (= old-level -1)
  4476. (= new-level -1)
  4477. (= old-level new-level))
  4478. 0
  4479. (- new-level old-level)))
  4480. (delta (if (> shift 0) -1 1))
  4481. (func (if (> shift 0) 'org-demote 'org-promote))
  4482. (org-odd-levels-only nil)
  4483. beg end)
  4484. ;; Remove the forced level indicator
  4485. (if force-level
  4486. (delete-region (point-at-bol) (point)))
  4487. ;; Paste
  4488. (beginning-of-line 1)
  4489. (org-back-over-empty-lines)
  4490. (setq beg (point))
  4491. (insert-before-markers txt)
  4492. (unless (string-match "\n\\'" txt) (insert "\n"))
  4493. (org-reinstall-markers-in-region beg)
  4494. (setq end (point))
  4495. (goto-char beg)
  4496. (skip-chars-forward " \t\n\r")
  4497. (setq beg (point))
  4498. ;; Shift if necessary
  4499. (unless (= shift 0)
  4500. (save-restriction
  4501. (narrow-to-region beg end)
  4502. (while (not (= shift 0))
  4503. (org-map-region func (point-min) (point-max))
  4504. (setq shift (+ delta shift)))
  4505. (goto-char (point-min))))
  4506. (when (interactive-p)
  4507. (message "Clipboard pasted as level %d subtree" new-level))
  4508. (if (and kill-ring
  4509. (eq org-subtree-clip (current-kill 0))
  4510. org-subtree-clip-folded)
  4511. ;; The tree was folded before it was killed/copied
  4512. (hide-subtree))))
  4513. (defun org-kill-is-subtree-p (&optional txt)
  4514. "Check if the current kill is an outline subtree, or a set of trees.
  4515. Returns nil if kill does not start with a headline, or if the first
  4516. headline level is not the largest headline level in the tree.
  4517. So this will actually accept several entries of equal levels as well,
  4518. which is OK for `org-paste-subtree'.
  4519. If optional TXT is given, check this string instead of the current kill."
  4520. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4521. (start-level (and kill
  4522. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4523. org-outline-regexp "\\)")
  4524. kill)
  4525. (- (match-end 2) (match-beginning 2) 1)))
  4526. (re (concat "^" org-outline-regexp))
  4527. (start (1+ (match-beginning 2))))
  4528. (if (not start-level)
  4529. (progn
  4530. nil) ;; does not even start with a heading
  4531. (catch 'exit
  4532. (while (setq start (string-match re kill (1+ start)))
  4533. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4534. (throw 'exit nil)))
  4535. t))))
  4536. (defvar org-markers-to-move nil
  4537. "Markers that should be moved with a cut-and-paste operation.
  4538. Those markers are stored together with their positions relative to
  4539. the start of the region.")
  4540. (defun org-save-markers-in-region (beg end)
  4541. "Check markers in region.
  4542. If these markers are between BEG and END, record their position relative
  4543. to BEG, so that after moving the block of text, we can put the markers back
  4544. into place.
  4545. This function gets called just before an entry or tree gets cut from the
  4546. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4547. called immediately, to move the markers with the entries."
  4548. (setq org-markers-to-move nil)
  4549. (when (featurep 'org-clock)
  4550. (org-clock-save-markers-for-cut-and-paste beg end))
  4551. (when (featurep 'org-agenda)
  4552. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4553. (defun org-check-and-save-marker (marker beg end)
  4554. "Check if MARKER is between BEG and END.
  4555. If yes, remember the marker and the distance to BEG."
  4556. (when (and (marker-buffer marker)
  4557. (equal (marker-buffer marker) (current-buffer)))
  4558. (if (and (>= marker beg) (< marker end))
  4559. (push (cons marker (- marker beg)) org-markers-to-move))))
  4560. (defun org-reinstall-markers-in-region (beg)
  4561. "Move all remembered markers to their position relative to BEG."
  4562. (mapc (lambda (x)
  4563. (move-marker (car x) (+ beg (cdr x))))
  4564. org-markers-to-move)
  4565. (setq org-markers-to-move nil))
  4566. (defun org-narrow-to-subtree ()
  4567. "Narrow buffer to the current subtree."
  4568. (interactive)
  4569. (save-excursion
  4570. (save-match-data
  4571. (narrow-to-region
  4572. (progn (org-back-to-heading) (point))
  4573. (progn (org-end-of-subtree t t) (point))))))
  4574. ;;; Outline Sorting
  4575. (defun org-sort (with-case)
  4576. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4577. Optional argument WITH-CASE means sort case-sensitively."
  4578. (interactive "P")
  4579. (if (org-at-table-p)
  4580. (org-call-with-arg 'org-table-sort-lines with-case)
  4581. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4582. (defun org-sort-remove-invisible (s)
  4583. (remove-text-properties 0 (length s) org-rm-props s)
  4584. (while (string-match org-bracket-link-regexp s)
  4585. (setq s (replace-match (if (match-end 2)
  4586. (match-string 3 s)
  4587. (match-string 1 s)) t t s)))
  4588. s)
  4589. (defvar org-priority-regexp) ; defined later in the file
  4590. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4591. "Sort entries on a certain level of an outline tree.
  4592. If there is an active region, the entries in the region are sorted.
  4593. Else, if the cursor is before the first entry, sort the top-level items.
  4594. Else, the children of the entry at point are sorted.
  4595. Sorting can be alphabetically, numerically, and by date/time as given by
  4596. the first time stamp in the entry. The command prompts for the sorting
  4597. type unless it has been given to the function through the SORTING-TYPE
  4598. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4599. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4600. called with point at the beginning of the record. It must return either
  4601. a string or a number that should serve as the sorting key for that record.
  4602. Comparing entries ignores case by default. However, with an optional argument
  4603. WITH-CASE, the sorting considers case as well."
  4604. (interactive "P")
  4605. (let ((case-func (if with-case 'identity 'downcase))
  4606. start beg end stars re re2
  4607. txt what tmp plain-list-p)
  4608. ;; Find beginning and end of region to sort
  4609. (cond
  4610. ((org-region-active-p)
  4611. ;; we will sort the region
  4612. (setq end (region-end)
  4613. what "region")
  4614. (goto-char (region-beginning))
  4615. (if (not (org-on-heading-p)) (outline-next-heading))
  4616. (setq start (point)))
  4617. ((org-at-item-p)
  4618. ;; we will sort this plain list
  4619. (org-beginning-of-item-list) (setq start (point))
  4620. (org-end-of-item-list) (setq end (point))
  4621. (goto-char start)
  4622. (setq plain-list-p t
  4623. what "plain list"))
  4624. ((or (org-on-heading-p)
  4625. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4626. ;; we will sort the children of the current headline
  4627. (org-back-to-heading)
  4628. (setq start (point)
  4629. end (progn (org-end-of-subtree t t)
  4630. (org-back-over-empty-lines)
  4631. (point))
  4632. what "children")
  4633. (goto-char start)
  4634. (show-subtree)
  4635. (outline-next-heading))
  4636. (t
  4637. ;; we will sort the top-level entries in this file
  4638. (goto-char (point-min))
  4639. (or (org-on-heading-p) (outline-next-heading))
  4640. (setq start (point) end (point-max) what "top-level")
  4641. (goto-char start)
  4642. (show-all)))
  4643. (setq beg (point))
  4644. (if (>= beg end) (error "Nothing to sort"))
  4645. (unless plain-list-p
  4646. (looking-at "\\(\\*+\\)")
  4647. (setq stars (match-string 1)
  4648. re (concat "^" (regexp-quote stars) " +")
  4649. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4650. txt (buffer-substring beg end))
  4651. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4652. (if (and (not (equal stars "*")) (string-match re2 txt))
  4653. (error "Region to sort contains a level above the first entry")))
  4654. (unless sorting-type
  4655. (message
  4656. (if plain-list-p
  4657. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4658. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  4659. what)
  4660. (setq sorting-type (read-char-exclusive))
  4661. (and (= (downcase sorting-type) ?f)
  4662. (setq getkey-func
  4663. (completing-read "Sort using function: "
  4664. obarray 'fboundp t nil nil))
  4665. (setq getkey-func (intern getkey-func)))
  4666. (and (= (downcase sorting-type) ?r)
  4667. (setq property
  4668. (completing-read "Property: "
  4669. (mapcar 'list (org-buffer-property-keys t))
  4670. nil t))))
  4671. (message "Sorting entries...")
  4672. (save-restriction
  4673. (narrow-to-region start end)
  4674. (let ((dcst (downcase sorting-type))
  4675. (now (current-time)))
  4676. (sort-subr
  4677. (/= dcst sorting-type)
  4678. ;; This function moves to the beginning character of the "record" to
  4679. ;; be sorted.
  4680. (if plain-list-p
  4681. (lambda nil
  4682. (if (org-at-item-p) t (goto-char (point-max))))
  4683. (lambda nil
  4684. (if (re-search-forward re nil t)
  4685. (goto-char (match-beginning 0))
  4686. (goto-char (point-max)))))
  4687. ;; This function moves to the last character of the "record" being
  4688. ;; sorted.
  4689. (if plain-list-p
  4690. 'org-end-of-item
  4691. (lambda nil
  4692. (save-match-data
  4693. (condition-case nil
  4694. (outline-forward-same-level 1)
  4695. (error
  4696. (goto-char (point-max)))))))
  4697. ;; This function returns the value that gets sorted against.
  4698. (if plain-list-p
  4699. (lambda nil
  4700. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4701. (cond
  4702. ((= dcst ?n)
  4703. (string-to-number (buffer-substring (match-end 0)
  4704. (point-at-eol))))
  4705. ((= dcst ?a)
  4706. (buffer-substring (match-end 0) (point-at-eol)))
  4707. ((= dcst ?t)
  4708. (if (re-search-forward org-ts-regexp
  4709. (point-at-eol) t)
  4710. (org-time-string-to-time (match-string 0))
  4711. now))
  4712. ((= dcst ?f)
  4713. (if getkey-func
  4714. (progn
  4715. (setq tmp (funcall getkey-func))
  4716. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4717. tmp)
  4718. (error "Invalid key function `%s'" getkey-func)))
  4719. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4720. (lambda nil
  4721. (cond
  4722. ((= dcst ?n)
  4723. (if (looking-at outline-regexp)
  4724. (string-to-number (buffer-substring (match-end 0)
  4725. (point-at-eol)))
  4726. nil))
  4727. ((= dcst ?a)
  4728. (funcall case-func (buffer-substring (point-at-bol)
  4729. (point-at-eol))))
  4730. ((= dcst ?t)
  4731. (if (re-search-forward org-ts-regexp
  4732. (save-excursion
  4733. (forward-line 2)
  4734. (point)) t)
  4735. (org-time-string-to-time (match-string 0))
  4736. now))
  4737. ((= dcst ?p)
  4738. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4739. (string-to-char (match-string 2))
  4740. org-default-priority))
  4741. ((= dcst ?r)
  4742. (or (org-entry-get nil property) ""))
  4743. ((= dcst ?o)
  4744. (if (looking-at org-complex-heading-regexp)
  4745. (- 9999 (length (member (match-string 2)
  4746. org-todo-keywords-1)))))
  4747. ((= dcst ?f)
  4748. (if getkey-func
  4749. (progn
  4750. (setq tmp (funcall getkey-func))
  4751. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4752. tmp)
  4753. (error "Invalid key function `%s'" getkey-func)))
  4754. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4755. nil
  4756. (cond
  4757. ((= dcst ?a) 'string<)
  4758. ((= dcst ?t) 'time-less-p)
  4759. (t nil)))))
  4760. (message "Sorting entries...done")))
  4761. (defun org-do-sort (table what &optional with-case sorting-type)
  4762. "Sort TABLE of WHAT according to SORTING-TYPE.
  4763. The user will be prompted for the SORTING-TYPE if the call to this
  4764. function does not specify it. WHAT is only for the prompt, to indicate
  4765. what is being sorted. The sorting key will be extracted from
  4766. the car of the elements of the table.
  4767. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4768. (unless sorting-type
  4769. (message
  4770. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4771. what)
  4772. (setq sorting-type (read-char-exclusive)))
  4773. (let ((dcst (downcase sorting-type))
  4774. extractfun comparefun)
  4775. ;; Define the appropriate functions
  4776. (cond
  4777. ((= dcst ?n)
  4778. (setq extractfun 'string-to-number
  4779. comparefun (if (= dcst sorting-type) '< '>)))
  4780. ((= dcst ?a)
  4781. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4782. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4783. comparefun (if (= dcst sorting-type)
  4784. 'string<
  4785. (lambda (a b) (and (not (string< a b))
  4786. (not (string= a b)))))))
  4787. ((= dcst ?t)
  4788. (setq extractfun
  4789. (lambda (x)
  4790. (if (string-match org-ts-regexp x)
  4791. (time-to-seconds
  4792. (org-time-string-to-time (match-string 0 x)))
  4793. 0))
  4794. comparefun (if (= dcst sorting-type) '< '>)))
  4795. (t (error "Invalid sorting type `%c'" sorting-type)))
  4796. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4797. table)
  4798. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4799. ;;; Editing source examples
  4800. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  4801. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  4802. (define-minor-mode org-exit-edit-mode
  4803. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  4804. (defun org-edit-src-example ()
  4805. "Edit the source code example at point.
  4806. An indirect buffer is created, and that buffer is then narrowed to the
  4807. example at point and switched to the correct language mode. When done,
  4808. exit by killing the buffer with \\[kill-buffer]. It is important to exit
  4809. in this way because some Org quoting of the example will take place."
  4810. (interactive)
  4811. (let ((line (org-current-line))
  4812. (case-fold-search t)
  4813. (msg (substitute-command-keys
  4814. "Edit, then kill this indirect buffer with C-c ' (C-c and single quote)"))
  4815. beg end)
  4816. (if (not (org-find-src-example-start))
  4817. ;; not at an example
  4818. nil
  4819. (if (not (save-excursion
  4820. (goto-char (point-at-bol))
  4821. (or (looking-at "#\\+begin_src[ \t]+\\([^ \t\n]+\\)")
  4822. (looking-at "[ \t]*<src .*?lang=\"\\(.*?\\)\""))))
  4823. (error "This should not happen"))
  4824. (setq beg (point-at-bol 2)
  4825. lang (match-string 1)
  4826. lang-f (intern (concat lang "-mode")))
  4827. (unless (functionp lang-f)
  4828. "No such language mode: %s" lang-f)
  4829. (unless (re-search-forward "^\\(#\\+end_src\\|[ \t]*</src>\\)" nil t)
  4830. (error "Cannot find end of src"))
  4831. (setq end (match-beginning 0))
  4832. (goto-line line)
  4833. (if (get-buffer "*Org Edit Src Example*")
  4834. (kill-buffer "*Org Edit Src Example*"))
  4835. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4836. "*Org Edit Src Example*"))
  4837. (narrow-to-region beg end)
  4838. (let ((org-inhibit-startup t))
  4839. (funcall lang-f))
  4840. (goto-char (point-min))
  4841. (while (re-search-forward "^," nil t)
  4842. (replace-match ""))
  4843. (goto-char (point-min))
  4844. (goto-line line)
  4845. (org-exit-edit-mode)
  4846. (org-set-local 'header-line-format msg)
  4847. (message "%s" msg)
  4848. t)))
  4849. (defun org-find-src-example-start ()
  4850. "If point is in a src example, move to the beginning of it.
  4851. If not, just return nil."
  4852. (let ((re "^\\(#\\+begin_src\\|[ \t]*<src\\)")
  4853. (pos (point))
  4854. p1)
  4855. (beginning-of-line 1)
  4856. (if (looking-at re)
  4857. (point)
  4858. (if (and (setq p1 (re-search-backward re nil t))
  4859. (re-search-forward "^\\(#\\+end_src\\|[ \t]*</src\\)" nil t)
  4860. (>= (point-at-eol) pos))
  4861. (goto-char p1)
  4862. (goto-char pos)
  4863. nil))))
  4864. (defun org-edit-src-exit ()
  4865. "Exit special edit and protect problematic lines."
  4866. (interactive)
  4867. (unless (> (point-min) 1)
  4868. (error "This buffer is not narrowed, something is wrong..."))
  4869. (goto-char (point-min))
  4870. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  4871. (replace-match ",\\1"))
  4872. (when font-lock-mode
  4873. (font-lock-unfontify-region (point-min) (point-max)))
  4874. (put-text-property (point-min) (point-max) 'font-lock-fontified t)
  4875. (kill-buffer (current-buffer)))
  4876. ;;;; Plain list items, including checkboxes
  4877. ;;; Plain list items
  4878. (defun org-at-item-p ()
  4879. "Is point in a line starting a hand-formatted item?"
  4880. (let ((llt org-plain-list-ordered-item-terminator))
  4881. (save-excursion
  4882. (goto-char (point-at-bol))
  4883. (looking-at
  4884. (cond
  4885. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4886. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4887. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4888. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4889. (defun org-in-item-p ()
  4890. "It the cursor inside a plain list item.
  4891. Does not have to be the first line."
  4892. (save-excursion
  4893. (condition-case nil
  4894. (progn
  4895. (org-beginning-of-item)
  4896. (org-at-item-p)
  4897. t)
  4898. (error nil))))
  4899. (defun org-insert-item (&optional checkbox)
  4900. "Insert a new item at the current level.
  4901. Return t when things worked, nil when we are not in an item."
  4902. (when (save-excursion
  4903. (condition-case nil
  4904. (progn
  4905. (org-beginning-of-item)
  4906. (org-at-item-p)
  4907. (if (org-invisible-p) (error "Invisible item"))
  4908. t)
  4909. (error nil)))
  4910. (let* ((bul (match-string 0))
  4911. (descp (save-excursion (goto-char (match-beginning 0))
  4912. (beginning-of-line 1)
  4913. (save-match-data
  4914. (looking-at "[ \t]*.*? ::"))))
  4915. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4916. (match-end 0)))
  4917. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4918. pos)
  4919. (if descp (setq checkbox nil))
  4920. (cond
  4921. ((and (org-at-item-p) (<= (point) eow))
  4922. ;; before the bullet
  4923. (beginning-of-line 1)
  4924. (open-line (if blank 2 1)))
  4925. ((<= (point) eow)
  4926. (beginning-of-line 1))
  4927. (t
  4928. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4929. (end-of-line 1)
  4930. (delete-horizontal-space))
  4931. (newline (if blank 2 1))))
  4932. (insert bul
  4933. (if checkbox "[ ]" "")
  4934. (if descp (concat (if checkbox " " "")
  4935. (read-string "Term: ") " :: ") ""))
  4936. (just-one-space)
  4937. (setq pos (point))
  4938. (end-of-line 1)
  4939. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4940. (org-maybe-renumber-ordered-list)
  4941. (and checkbox (org-update-checkbox-count-maybe))
  4942. t))
  4943. ;;; Checkboxes
  4944. (defun org-at-item-checkbox-p ()
  4945. "Is point at a line starting a plain-list item with a checklet?"
  4946. (and (org-at-item-p)
  4947. (save-excursion
  4948. (goto-char (match-end 0))
  4949. (skip-chars-forward " \t")
  4950. (looking-at "\\[[- X]\\]"))))
  4951. (defun org-toggle-checkbox (&optional arg)
  4952. "Toggle the checkbox in the current line."
  4953. (interactive "P")
  4954. (catch 'exit
  4955. (let (beg end status (firstnew 'unknown))
  4956. (cond
  4957. ((org-region-active-p)
  4958. (setq beg (region-beginning) end (region-end)))
  4959. ((org-on-heading-p)
  4960. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4961. ((org-at-item-checkbox-p)
  4962. (let ((pos (point)))
  4963. (replace-match
  4964. (cond (arg "[-]")
  4965. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4966. (t "[ ]"))
  4967. t t)
  4968. (goto-char pos))
  4969. (throw 'exit t))
  4970. (t (error "Not at a checkbox or heading, and no active region")))
  4971. (save-excursion
  4972. (goto-char beg)
  4973. (while (< (point) end)
  4974. (when (org-at-item-checkbox-p)
  4975. (setq status (equal (match-string 0) "[X]"))
  4976. (when (eq firstnew 'unknown)
  4977. (setq firstnew (not status)))
  4978. (replace-match
  4979. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4980. (beginning-of-line 2)))))
  4981. (org-update-checkbox-count-maybe))
  4982. (defun org-update-checkbox-count-maybe ()
  4983. "Update checkbox statistics unless turned off by user."
  4984. (when org-provide-checkbox-statistics
  4985. (org-update-checkbox-count)))
  4986. (defun org-update-checkbox-count (&optional all)
  4987. "Update the checkbox statistics in the current section.
  4988. This will find all statistic cookies like [57%] and [6/12] and update them
  4989. with the current numbers. With optional prefix argument ALL, do this for
  4990. the whole buffer."
  4991. (interactive "P")
  4992. (save-excursion
  4993. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4994. (beg (condition-case nil
  4995. (progn (outline-back-to-heading) (point))
  4996. (error (point-min))))
  4997. (end (move-marker (make-marker)
  4998. (progn (outline-next-heading) (point))))
  4999. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  5000. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  5001. (re-find (concat re "\\|" re-box))
  5002. beg-cookie end-cookie is-percent c-on c-off lim
  5003. eline curr-ind next-ind continue-from startsearch
  5004. (cstat 0)
  5005. )
  5006. (when all
  5007. (goto-char (point-min))
  5008. (outline-next-heading)
  5009. (setq beg (point) end (point-max)))
  5010. (goto-char end)
  5011. ;; find each statistic cookie
  5012. (while (re-search-backward re-find beg t)
  5013. (setq beg-cookie (match-beginning 1)
  5014. end-cookie (match-end 1)
  5015. cstat (+ cstat (if end-cookie 1 0))
  5016. startsearch (point-at-eol)
  5017. continue-from (point-at-bol)
  5018. is-percent (match-beginning 2)
  5019. lim (cond
  5020. ((org-on-heading-p) (outline-next-heading) (point))
  5021. ((org-at-item-p) (org-end-of-item) (point))
  5022. (t nil))
  5023. c-on 0
  5024. c-off 0)
  5025. (when lim
  5026. ;; find first checkbox for this cookie and gather
  5027. ;; statistics from all that are at this indentation level
  5028. (goto-char startsearch)
  5029. (if (re-search-forward re-box lim t)
  5030. (progn
  5031. (org-beginning-of-item)
  5032. (setq curr-ind (org-get-indentation))
  5033. (setq next-ind curr-ind)
  5034. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  5035. (save-excursion (end-of-line) (setq eline (point)))
  5036. (if (re-search-forward re-box eline t)
  5037. (if (member (match-string 2) '("[ ]" "[-]"))
  5038. (setq c-off (1+ c-off))
  5039. (setq c-on (1+ c-on))
  5040. )
  5041. )
  5042. (org-end-of-item)
  5043. (setq next-ind (org-get-indentation))
  5044. )))
  5045. (goto-char continue-from)
  5046. ;; update cookie
  5047. (when end-cookie
  5048. (delete-region beg-cookie end-cookie)
  5049. (goto-char beg-cookie)
  5050. (insert
  5051. (if is-percent
  5052. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5053. (format "[%d/%d]" c-on (+ c-on c-off)))))
  5054. ;; update items checkbox if it has one
  5055. (when (org-at-item-p)
  5056. (org-beginning-of-item)
  5057. (when (and (> (+ c-on c-off) 0)
  5058. (re-search-forward re-box (point-at-eol) t))
  5059. (setq beg-cookie (match-beginning 2)
  5060. end-cookie (match-end 2))
  5061. (delete-region beg-cookie end-cookie)
  5062. (goto-char beg-cookie)
  5063. (cond ((= c-off 0) (insert "[X]"))
  5064. ((= c-on 0) (insert "[ ]"))
  5065. (t (insert "[-]")))
  5066. )))
  5067. (goto-char continue-from))
  5068. (when (interactive-p)
  5069. (message "Checkbox satistics updated %s (%d places)"
  5070. (if all "in entire file" "in current outline entry") cstat)))))
  5071. (defun org-get-checkbox-statistics-face ()
  5072. "Select the face for checkbox statistics.
  5073. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5074. it will be `org-todo'."
  5075. (if (match-end 1)
  5076. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5077. (if (and (> (match-end 2) (match-beginning 2))
  5078. (equal (match-string 2) (match-string 3)))
  5079. 'org-done
  5080. 'org-todo)))
  5081. (defun org-get-indentation (&optional line)
  5082. "Get the indentation of the current line, interpreting tabs.
  5083. When LINE is given, assume it represents a line and compute its indentation."
  5084. (if line
  5085. (if (string-match "^ *" (org-remove-tabs line))
  5086. (match-end 0))
  5087. (save-excursion
  5088. (beginning-of-line 1)
  5089. (skip-chars-forward " \t")
  5090. (current-column))))
  5091. (defun org-remove-tabs (s &optional width)
  5092. "Replace tabulators in S with spaces.
  5093. Assumes that s is a single line, starting in column 0."
  5094. (setq width (or width tab-width))
  5095. (while (string-match "\t" s)
  5096. (setq s (replace-match
  5097. (make-string
  5098. (- (* width (/ (+ (match-beginning 0) width) width))
  5099. (match-beginning 0)) ?\ )
  5100. t t s)))
  5101. s)
  5102. (defun org-fix-indentation (line ind)
  5103. "Fix indentation in LINE.
  5104. IND is a cons cell with target and minimum indentation.
  5105. If the current indenation in LINE is smaller than the minimum,
  5106. leave it alone. If it is larger than ind, set it to the target."
  5107. (let* ((l (org-remove-tabs line))
  5108. (i (org-get-indentation l))
  5109. (i1 (car ind)) (i2 (cdr ind)))
  5110. (if (>= i i2) (setq l (substring line i2)))
  5111. (if (> i1 0)
  5112. (concat (make-string i1 ?\ ) l)
  5113. l)))
  5114. (defun org-beginning-of-item ()
  5115. "Go to the beginning of the current hand-formatted item.
  5116. If the cursor is not in an item, throw an error."
  5117. (interactive)
  5118. (let ((pos (point))
  5119. (limit (save-excursion
  5120. (condition-case nil
  5121. (progn
  5122. (org-back-to-heading)
  5123. (beginning-of-line 2) (point))
  5124. (error (point-min)))))
  5125. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5126. ind ind1)
  5127. (if (org-at-item-p)
  5128. (beginning-of-line 1)
  5129. (beginning-of-line 1)
  5130. (skip-chars-forward " \t")
  5131. (setq ind (current-column))
  5132. (if (catch 'exit
  5133. (while t
  5134. (beginning-of-line 0)
  5135. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5136. (if (looking-at "[ \t]*$")
  5137. (setq ind1 ind-empty)
  5138. (skip-chars-forward " \t")
  5139. (setq ind1 (current-column)))
  5140. (if (< ind1 ind)
  5141. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5142. nil
  5143. (goto-char pos)
  5144. (error "Not in an item")))))
  5145. (defun org-end-of-item ()
  5146. "Go to the end of the current hand-formatted item.
  5147. If the cursor is not in an item, throw an error."
  5148. (interactive)
  5149. (let* ((pos (point))
  5150. ind1
  5151. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5152. (limit (save-excursion (outline-next-heading) (point)))
  5153. (ind (save-excursion
  5154. (org-beginning-of-item)
  5155. (skip-chars-forward " \t")
  5156. (current-column)))
  5157. (end (catch 'exit
  5158. (while t
  5159. (beginning-of-line 2)
  5160. (if (eobp) (throw 'exit (point)))
  5161. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5162. (if (looking-at "[ \t]*$")
  5163. (setq ind1 ind-empty)
  5164. (skip-chars-forward " \t")
  5165. (setq ind1 (current-column)))
  5166. (if (<= ind1 ind)
  5167. (throw 'exit (point-at-bol)))))))
  5168. (if end
  5169. (goto-char end)
  5170. (goto-char pos)
  5171. (error "Not in an item"))))
  5172. (defun org-next-item ()
  5173. "Move to the beginning of the next item in the current plain list.
  5174. Error if not at a plain list, or if this is the last item in the list."
  5175. (interactive)
  5176. (let (ind ind1 (pos (point)))
  5177. (org-beginning-of-item)
  5178. (setq ind (org-get-indentation))
  5179. (org-end-of-item)
  5180. (setq ind1 (org-get-indentation))
  5181. (unless (and (org-at-item-p) (= ind ind1))
  5182. (goto-char pos)
  5183. (error "On last item"))))
  5184. (defun org-previous-item ()
  5185. "Move to the beginning of the previous item in the current plain list.
  5186. Error if not at a plain list, or if this is the first item in the list."
  5187. (interactive)
  5188. (let (beg ind ind1 (pos (point)))
  5189. (org-beginning-of-item)
  5190. (setq beg (point))
  5191. (setq ind (org-get-indentation))
  5192. (goto-char beg)
  5193. (catch 'exit
  5194. (while t
  5195. (beginning-of-line 0)
  5196. (if (looking-at "[ \t]*$")
  5197. nil
  5198. (if (<= (setq ind1 (org-get-indentation)) ind)
  5199. (throw 'exit t)))))
  5200. (condition-case nil
  5201. (if (or (not (org-at-item-p))
  5202. (< ind1 (1- ind)))
  5203. (error "")
  5204. (org-beginning-of-item))
  5205. (error (goto-char pos)
  5206. (error "On first item")))))
  5207. (defun org-first-list-item-p ()
  5208. "Is this heading the item in a plain list?"
  5209. (unless (org-at-item-p)
  5210. (error "Not at a plain list item"))
  5211. (org-beginning-of-item)
  5212. (= (point) (save-excursion (org-beginning-of-item-list))))
  5213. (defun org-move-item-down ()
  5214. "Move the plain list item at point down, i.e. swap with following item.
  5215. Subitems (items with larger indentation) are considered part of the item,
  5216. so this really moves item trees."
  5217. (interactive)
  5218. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5219. (org-beginning-of-item)
  5220. (setq beg0 (point))
  5221. (save-excursion
  5222. (setq ne-beg (org-back-over-empty-lines))
  5223. (setq beg (point)))
  5224. (goto-char beg0)
  5225. (setq ind (org-get-indentation))
  5226. (org-end-of-item)
  5227. (setq end0 (point))
  5228. (setq ind1 (org-get-indentation))
  5229. (setq ne-end (org-back-over-empty-lines))
  5230. (setq end (point))
  5231. (goto-char beg0)
  5232. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5233. ;; include less whitespace
  5234. (save-excursion
  5235. (goto-char beg)
  5236. (forward-line (- ne-beg ne-end))
  5237. (setq beg (point))))
  5238. (goto-char end0)
  5239. (if (and (org-at-item-p) (= ind ind1))
  5240. (progn
  5241. (org-end-of-item)
  5242. (org-back-over-empty-lines)
  5243. (setq txt (buffer-substring beg end))
  5244. (save-excursion
  5245. (delete-region beg end))
  5246. (setq pos (point))
  5247. (insert txt)
  5248. (goto-char pos) (org-skip-whitespace)
  5249. (org-maybe-renumber-ordered-list))
  5250. (goto-char pos)
  5251. (error "Cannot move this item further down"))))
  5252. (defun org-move-item-up (arg)
  5253. "Move the plain list item at point up, i.e. swap with previous item.
  5254. Subitems (items with larger indentation) are considered part of the item,
  5255. so this really moves item trees."
  5256. (interactive "p")
  5257. (let (beg beg0 end ind ind1 (pos (point)) txt
  5258. ne-beg ne-ins ins-end)
  5259. (org-beginning-of-item)
  5260. (setq beg0 (point))
  5261. (setq ind (org-get-indentation))
  5262. (save-excursion
  5263. (setq ne-beg (org-back-over-empty-lines))
  5264. (setq beg (point)))
  5265. (goto-char beg0)
  5266. (org-end-of-item)
  5267. (setq end (point))
  5268. (goto-char beg0)
  5269. (catch 'exit
  5270. (while t
  5271. (beginning-of-line 0)
  5272. (if (looking-at "[ \t]*$")
  5273. (if org-empty-line-terminates-plain-lists
  5274. (progn
  5275. (goto-char pos)
  5276. (error "Cannot move this item further up"))
  5277. nil)
  5278. (if (<= (setq ind1 (org-get-indentation)) ind)
  5279. (throw 'exit t)))))
  5280. (condition-case nil
  5281. (org-beginning-of-item)
  5282. (error (goto-char beg)
  5283. (error "Cannot move this item further up")))
  5284. (setq ind1 (org-get-indentation))
  5285. (if (and (org-at-item-p) (= ind ind1))
  5286. (progn
  5287. (setq ne-ins (org-back-over-empty-lines))
  5288. (setq txt (buffer-substring beg end))
  5289. (save-excursion
  5290. (delete-region beg end))
  5291. (setq pos (point))
  5292. (insert txt)
  5293. (setq ins-end (point))
  5294. (goto-char pos) (org-skip-whitespace)
  5295. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5296. ;; Move whitespace back to beginning
  5297. (save-excursion
  5298. (goto-char ins-end)
  5299. (let ((kill-whole-line t))
  5300. (kill-line (- ne-ins ne-beg)) (point)))
  5301. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5302. (org-maybe-renumber-ordered-list))
  5303. (goto-char pos)
  5304. (error "Cannot move this item further up"))))
  5305. (defun org-maybe-renumber-ordered-list ()
  5306. "Renumber the ordered list at point if setup allows it.
  5307. This tests the user option `org-auto-renumber-ordered-lists' before
  5308. doing the renumbering."
  5309. (interactive)
  5310. (when (and org-auto-renumber-ordered-lists
  5311. (org-at-item-p))
  5312. (if (match-beginning 3)
  5313. (org-renumber-ordered-list 1)
  5314. (org-fix-bullet-type))))
  5315. (defun org-maybe-renumber-ordered-list-safe ()
  5316. (condition-case nil
  5317. (save-excursion
  5318. (org-maybe-renumber-ordered-list))
  5319. (error nil)))
  5320. (defun org-cycle-list-bullet (&optional which)
  5321. "Cycle through the different itemize/enumerate bullets.
  5322. This cycle the entire list level through the sequence:
  5323. `-' -> `+' -> `*' -> `1.' -> `1)'
  5324. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5325. 0 meand `-', 1 means `+' etc."
  5326. (interactive "P")
  5327. (org-preserve-lc
  5328. (org-beginning-of-item-list)
  5329. (org-at-item-p)
  5330. (beginning-of-line 1)
  5331. (let ((current (match-string 0))
  5332. (prevp (eq which 'previous))
  5333. new)
  5334. (setq new (cond
  5335. ((and (numberp which)
  5336. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5337. ((string-match "-" current) (if prevp "1)" "+"))
  5338. ((string-match "\\+" current)
  5339. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5340. ((string-match "\\*" current) (if prevp "+" "1."))
  5341. ((string-match "\\." current) (if prevp "*" "1)"))
  5342. ((string-match ")" current) (if prevp "1." "-"))
  5343. (t (error "This should not happen"))))
  5344. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5345. (org-fix-bullet-type)
  5346. (org-maybe-renumber-ordered-list))))
  5347. (defun org-get-string-indentation (s)
  5348. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5349. (let ((n -1) (i 0) (w tab-width) c)
  5350. (catch 'exit
  5351. (while (< (setq n (1+ n)) (length s))
  5352. (setq c (aref s n))
  5353. (cond ((= c ?\ ) (setq i (1+ i)))
  5354. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5355. (t (throw 'exit t)))))
  5356. i))
  5357. (defun org-renumber-ordered-list (arg)
  5358. "Renumber an ordered plain list.
  5359. Cursor needs to be in the first line of an item, the line that starts
  5360. with something like \"1.\" or \"2)\"."
  5361. (interactive "p")
  5362. (unless (and (org-at-item-p)
  5363. (match-beginning 3))
  5364. (error "This is not an ordered list"))
  5365. (let ((line (org-current-line))
  5366. (col (current-column))
  5367. (ind (org-get-string-indentation
  5368. (buffer-substring (point-at-bol) (match-beginning 3))))
  5369. ;; (term (substring (match-string 3) -1))
  5370. ind1 (n (1- arg))
  5371. fmt)
  5372. ;; find where this list begins
  5373. (org-beginning-of-item-list)
  5374. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5375. (setq fmt (concat "%d" (match-string 1)))
  5376. (beginning-of-line 0)
  5377. ;; walk forward and replace these numbers
  5378. (catch 'exit
  5379. (while t
  5380. (catch 'next
  5381. (beginning-of-line 2)
  5382. (if (eobp) (throw 'exit nil))
  5383. (if (looking-at "[ \t]*$") (throw 'next nil))
  5384. (skip-chars-forward " \t") (setq ind1 (current-column))
  5385. (if (> ind1 ind) (throw 'next t))
  5386. (if (< ind1 ind) (throw 'exit t))
  5387. (if (not (org-at-item-p)) (throw 'exit nil))
  5388. (delete-region (match-beginning 2) (match-end 2))
  5389. (goto-char (match-beginning 2))
  5390. (insert (format fmt (setq n (1+ n)))))))
  5391. (goto-line line)
  5392. (org-move-to-column col)))
  5393. (defun org-fix-bullet-type ()
  5394. "Make sure all items in this list have the same bullet as the firsst item."
  5395. (interactive)
  5396. (unless (org-at-item-p) (error "This is not a list"))
  5397. (let ((line (org-current-line))
  5398. (col (current-column))
  5399. (ind (current-indentation))
  5400. ind1 bullet)
  5401. ;; find where this list begins
  5402. (org-beginning-of-item-list)
  5403. (beginning-of-line 1)
  5404. ;; find out what the bullet type is
  5405. (looking-at "[ \t]*\\(\\S-+\\)")
  5406. (setq bullet (match-string 1))
  5407. ;; walk forward and replace these numbers
  5408. (beginning-of-line 0)
  5409. (catch 'exit
  5410. (while t
  5411. (catch 'next
  5412. (beginning-of-line 2)
  5413. (if (eobp) (throw 'exit nil))
  5414. (if (looking-at "[ \t]*$") (throw 'next nil))
  5415. (skip-chars-forward " \t") (setq ind1 (current-column))
  5416. (if (> ind1 ind) (throw 'next t))
  5417. (if (< ind1 ind) (throw 'exit t))
  5418. (if (not (org-at-item-p)) (throw 'exit nil))
  5419. (skip-chars-forward " \t")
  5420. (looking-at "\\S-+")
  5421. (replace-match bullet))))
  5422. (goto-line line)
  5423. (org-move-to-column col)
  5424. (if (string-match "[0-9]" bullet)
  5425. (org-renumber-ordered-list 1))))
  5426. (defun org-beginning-of-item-list ()
  5427. "Go to the beginning of the current item list.
  5428. I.e. to the first item in this list."
  5429. (interactive)
  5430. (org-beginning-of-item)
  5431. (let ((pos (point-at-bol))
  5432. (ind (org-get-indentation))
  5433. ind1)
  5434. ;; find where this list begins
  5435. (catch 'exit
  5436. (while t
  5437. (catch 'next
  5438. (beginning-of-line 0)
  5439. (if (looking-at "[ \t]*$")
  5440. (throw (if (bobp) 'exit 'next) t))
  5441. (skip-chars-forward " \t") (setq ind1 (current-column))
  5442. (if (or (< ind1 ind)
  5443. (and (= ind1 ind)
  5444. (not (org-at-item-p)))
  5445. (bobp))
  5446. (throw 'exit t)
  5447. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5448. (goto-char pos)))
  5449. (defun org-end-of-item-list ()
  5450. "Go to the end of the current item list.
  5451. I.e. to the text after the last item."
  5452. (interactive)
  5453. (org-beginning-of-item)
  5454. (let ((pos (point-at-bol))
  5455. (ind (org-get-indentation))
  5456. ind1)
  5457. ;; find where this list begins
  5458. (catch 'exit
  5459. (while t
  5460. (catch 'next
  5461. (beginning-of-line 2)
  5462. (if (looking-at "[ \t]*$")
  5463. (throw (if (eobp) 'exit 'next) t))
  5464. (skip-chars-forward " \t") (setq ind1 (current-column))
  5465. (if (or (< ind1 ind)
  5466. (and (= ind1 ind)
  5467. (not (org-at-item-p)))
  5468. (eobp))
  5469. (progn
  5470. (setq pos (point-at-bol))
  5471. (throw 'exit t))))))
  5472. (goto-char pos)))
  5473. (defvar org-last-indent-begin-marker (make-marker))
  5474. (defvar org-last-indent-end-marker (make-marker))
  5475. (defun org-outdent-item (arg)
  5476. "Outdent a local list item."
  5477. (interactive "p")
  5478. (org-indent-item (- arg)))
  5479. (defun org-indent-item (arg)
  5480. "Indent a local list item."
  5481. (interactive "p")
  5482. (unless (org-at-item-p)
  5483. (error "Not on an item"))
  5484. (save-excursion
  5485. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5486. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5487. (setq beg org-last-indent-begin-marker
  5488. end org-last-indent-end-marker)
  5489. (org-beginning-of-item)
  5490. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5491. (org-end-of-item)
  5492. (setq end (move-marker org-last-indent-end-marker (point))))
  5493. (goto-char beg)
  5494. (setq tmp (org-item-indent-positions)
  5495. ind (car tmp)
  5496. ind-down (nth 2 tmp)
  5497. ind-up (nth 1 tmp)
  5498. delta (if (> arg 0)
  5499. (if ind-down (- ind-down ind) 2)
  5500. (if ind-up (- ind-up ind) -2)))
  5501. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5502. (while (< (point) end)
  5503. (beginning-of-line 1)
  5504. (skip-chars-forward " \t") (setq ind1 (current-column))
  5505. (delete-region (point-at-bol) (point))
  5506. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5507. (beginning-of-line 2))))
  5508. (org-fix-bullet-type)
  5509. (org-maybe-renumber-ordered-list-safe)
  5510. (save-excursion
  5511. (beginning-of-line 0)
  5512. (condition-case nil (org-beginning-of-item) (error nil))
  5513. (org-maybe-renumber-ordered-list-safe)))
  5514. (defun org-item-indent-positions ()
  5515. "Return indentation for plain list items.
  5516. This returns a list with three values: The current indentation, the
  5517. parent indentation and the indentation a child should habe.
  5518. Assumes cursor in item line."
  5519. (let* ((bolpos (point-at-bol))
  5520. (ind (org-get-indentation))
  5521. ind-down ind-up pos)
  5522. (save-excursion
  5523. (org-beginning-of-item-list)
  5524. (skip-chars-backward "\n\r \t")
  5525. (when (org-in-item-p)
  5526. (org-beginning-of-item)
  5527. (setq ind-up (org-get-indentation))))
  5528. (setq pos (point))
  5529. (save-excursion
  5530. (cond
  5531. ((and (condition-case nil (progn (org-previous-item) t)
  5532. (error nil))
  5533. (or (forward-char 1) t)
  5534. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5535. (setq ind-down (org-get-indentation)))
  5536. ((and (goto-char pos)
  5537. (org-at-item-p))
  5538. (goto-char (match-end 0))
  5539. (skip-chars-forward " \t")
  5540. (setq ind-down (current-column)))))
  5541. (list ind ind-up ind-down)))
  5542. ;;; The orgstruct minor mode
  5543. ;; Define a minor mode which can be used in other modes in order to
  5544. ;; integrate the org-mode structure editing commands.
  5545. ;; This is really a hack, because the org-mode structure commands use
  5546. ;; keys which normally belong to the major mode. Here is how it
  5547. ;; works: The minor mode defines all the keys necessary to operate the
  5548. ;; structure commands, but wraps the commands into a function which
  5549. ;; tests if the cursor is currently at a headline or a plain list
  5550. ;; item. If that is the case, the structure command is used,
  5551. ;; temporarily setting many Org-mode variables like regular
  5552. ;; expressions for filling etc. However, when any of those keys is
  5553. ;; used at a different location, function uses `key-binding' to look
  5554. ;; up if the key has an associated command in another currently active
  5555. ;; keymap (minor modes, major mode, global), and executes that
  5556. ;; command. There might be problems if any of the keys is otherwise
  5557. ;; used as a prefix key.
  5558. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5559. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5560. ;; addresses this by checking explicitly for both bindings.
  5561. (defvar orgstruct-mode-map (make-sparse-keymap)
  5562. "Keymap for the minor `orgstruct-mode'.")
  5563. (defvar org-local-vars nil
  5564. "List of local variables, for use by `orgstruct-mode'")
  5565. ;;;###autoload
  5566. (define-minor-mode orgstruct-mode
  5567. "Toggle the minor more `orgstruct-mode'.
  5568. This mode is for using Org-mode structure commands in other modes.
  5569. The following key behave as if Org-mode was active, if the cursor
  5570. is on a headline, or on a plain list item (both in the definition
  5571. of Org-mode).
  5572. M-up Move entry/item up
  5573. M-down Move entry/item down
  5574. M-left Promote
  5575. M-right Demote
  5576. M-S-up Move entry/item up
  5577. M-S-down Move entry/item down
  5578. M-S-left Promote subtree
  5579. M-S-right Demote subtree
  5580. M-q Fill paragraph and items like in Org-mode
  5581. C-c ^ Sort entries
  5582. C-c - Cycle list bullet
  5583. TAB Cycle item visibility
  5584. M-RET Insert new heading/item
  5585. S-M-RET Insert new TODO heading / Chekbox item
  5586. C-c C-c Set tags / toggle checkbox"
  5587. nil " OrgStruct" nil
  5588. (org-load-modules-maybe)
  5589. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5590. ;;;###autoload
  5591. (defun turn-on-orgstruct ()
  5592. "Unconditionally turn on `orgstruct-mode'."
  5593. (orgstruct-mode 1))
  5594. ;;;###autoload
  5595. (defun turn-on-orgstruct++ ()
  5596. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5597. In addition to setting orgstruct-mode, this also exports all indentation and
  5598. autofilling variables from org-mode into the buffer. Note that turning
  5599. off orgstruct-mode will *not* remove these additional settings."
  5600. (orgstruct-mode 1)
  5601. (let (var val)
  5602. (mapc
  5603. (lambda (x)
  5604. (when (string-match
  5605. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5606. (symbol-name (car x)))
  5607. (setq var (car x) val (nth 1 x))
  5608. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5609. org-local-vars)))
  5610. (defun orgstruct-error ()
  5611. "Error when there is no default binding for a structure key."
  5612. (interactive)
  5613. (error "This key has no function outside structure elements"))
  5614. (defun orgstruct-setup ()
  5615. "Setup orgstruct keymaps."
  5616. (let ((nfunc 0)
  5617. (bindings
  5618. (list
  5619. '([(meta up)] org-metaup)
  5620. '([(meta down)] org-metadown)
  5621. '([(meta left)] org-metaleft)
  5622. '([(meta right)] org-metaright)
  5623. '([(meta shift up)] org-shiftmetaup)
  5624. '([(meta shift down)] org-shiftmetadown)
  5625. '([(meta shift left)] org-shiftmetaleft)
  5626. '([(meta shift right)] org-shiftmetaright)
  5627. '([(shift up)] org-shiftup)
  5628. '([(shift down)] org-shiftdown)
  5629. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5630. '("\M-q" fill-paragraph)
  5631. '("\C-c^" org-sort)
  5632. '("\C-c-" org-cycle-list-bullet)))
  5633. elt key fun cmd)
  5634. (while (setq elt (pop bindings))
  5635. (setq nfunc (1+ nfunc))
  5636. (setq key (org-key (car elt))
  5637. fun (nth 1 elt)
  5638. cmd (orgstruct-make-binding fun nfunc key))
  5639. (org-defkey orgstruct-mode-map key cmd))
  5640. ;; Special treatment needed for TAB and RET
  5641. (org-defkey orgstruct-mode-map [(tab)]
  5642. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5643. (org-defkey orgstruct-mode-map "\C-i"
  5644. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5645. (org-defkey orgstruct-mode-map "\M-\C-m"
  5646. (orgstruct-make-binding 'org-insert-heading 105
  5647. "\M-\C-m" [(meta return)]))
  5648. (org-defkey orgstruct-mode-map [(meta return)]
  5649. (orgstruct-make-binding 'org-insert-heading 106
  5650. [(meta return)] "\M-\C-m"))
  5651. (org-defkey orgstruct-mode-map [(shift meta return)]
  5652. (orgstruct-make-binding 'org-insert-todo-heading 107
  5653. [(meta return)] "\M-\C-m"))
  5654. (unless org-local-vars
  5655. (setq org-local-vars (org-get-local-variables)))
  5656. t))
  5657. (defun orgstruct-make-binding (fun n &rest keys)
  5658. "Create a function for binding in the structure minor mode.
  5659. FUN is the command to call inside a table. N is used to create a unique
  5660. command name. KEYS are keys that should be checked in for a command
  5661. to execute outside of tables."
  5662. (eval
  5663. (list 'defun
  5664. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5665. '(arg)
  5666. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5667. "Outside of structure, run the binding of `"
  5668. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5669. "'.")
  5670. '(interactive "p")
  5671. (list 'if
  5672. '(org-context-p 'headline 'item)
  5673. (list 'org-run-like-in-org-mode (list 'quote fun))
  5674. (list 'let '(orgstruct-mode)
  5675. (list 'call-interactively
  5676. (append '(or)
  5677. (mapcar (lambda (k)
  5678. (list 'key-binding k))
  5679. keys)
  5680. '('orgstruct-error))))))))
  5681. (defun org-context-p (&rest contexts)
  5682. "Check if local context is and of CONTEXTS.
  5683. Possible values in the list of contexts are `table', `headline', and `item'."
  5684. (let ((pos (point)))
  5685. (goto-char (point-at-bol))
  5686. (prog1 (or (and (memq 'table contexts)
  5687. (looking-at "[ \t]*|"))
  5688. (and (memq 'headline contexts)
  5689. (looking-at "\\*+"))
  5690. (and (memq 'item contexts)
  5691. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5692. (goto-char pos))))
  5693. (defun org-get-local-variables ()
  5694. "Return a list of all local variables in an org-mode buffer."
  5695. (let (varlist)
  5696. (with-current-buffer (get-buffer-create "*Org tmp*")
  5697. (erase-buffer)
  5698. (org-mode)
  5699. (setq varlist (buffer-local-variables)))
  5700. (kill-buffer "*Org tmp*")
  5701. (delq nil
  5702. (mapcar
  5703. (lambda (x)
  5704. (setq x
  5705. (if (symbolp x)
  5706. (list x)
  5707. (list (car x) (list 'quote (cdr x)))))
  5708. (if (string-match
  5709. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5710. (symbol-name (car x)))
  5711. x nil))
  5712. varlist))))
  5713. ;;;###autoload
  5714. (defun org-run-like-in-org-mode (cmd)
  5715. (org-load-modules-maybe)
  5716. (unless org-local-vars
  5717. (setq org-local-vars (org-get-local-variables)))
  5718. (eval (list 'let org-local-vars
  5719. (list 'call-interactively (list 'quote cmd)))))
  5720. ;;;; Archiving
  5721. (defun org-get-category (&optional pos)
  5722. "Get the category applying to position POS."
  5723. (get-text-property (or pos (point)) 'org-category))
  5724. (defun org-refresh-category-properties ()
  5725. "Refresh category text properties in the buffer."
  5726. (let ((def-cat (cond
  5727. ((null org-category)
  5728. (if buffer-file-name
  5729. (file-name-sans-extension
  5730. (file-name-nondirectory buffer-file-name))
  5731. "???"))
  5732. ((symbolp org-category) (symbol-name org-category))
  5733. (t org-category)))
  5734. beg end cat pos optionp)
  5735. (org-unmodified
  5736. (save-excursion
  5737. (save-restriction
  5738. (widen)
  5739. (goto-char (point-min))
  5740. (put-text-property (point) (point-max) 'org-category def-cat)
  5741. (while (re-search-forward
  5742. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5743. (setq pos (match-end 0)
  5744. optionp (equal (char-after (match-beginning 0)) ?#)
  5745. cat (org-trim (match-string 2)))
  5746. (if optionp
  5747. (setq beg (point-at-bol) end (point-max))
  5748. (org-back-to-heading t)
  5749. (setq beg (point) end (org-end-of-subtree t t)))
  5750. (put-text-property beg end 'org-category cat)
  5751. (goto-char pos)))))))
  5752. ;;;; Link Stuff
  5753. ;;; Link abbreviations
  5754. (defun org-link-expand-abbrev (link)
  5755. "Apply replacements as defined in `org-link-abbrev-alist."
  5756. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5757. (let* ((key (match-string 1 link))
  5758. (as (or (assoc key org-link-abbrev-alist-local)
  5759. (assoc key org-link-abbrev-alist)))
  5760. (tag (and (match-end 2) (match-string 3 link)))
  5761. rpl)
  5762. (if (not as)
  5763. link
  5764. (setq rpl (cdr as))
  5765. (cond
  5766. ((symbolp rpl) (funcall rpl tag))
  5767. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5768. (t (concat rpl tag)))))
  5769. link))
  5770. ;;; Storing and inserting links
  5771. (defvar org-insert-link-history nil
  5772. "Minibuffer history for links inserted with `org-insert-link'.")
  5773. (defvar org-stored-links nil
  5774. "Contains the links stored with `org-store-link'.")
  5775. (defvar org-store-link-plist nil
  5776. "Plist with info about the most recently link created with `org-store-link'.")
  5777. (defvar org-link-protocols nil
  5778. "Link protocols added to Org-mode using `org-add-link-type'.")
  5779. (defvar org-store-link-functions nil
  5780. "List of functions that are called to create and store a link.
  5781. Each function will be called in turn until one returns a non-nil
  5782. value. Each function should check if it is responsible for creating
  5783. this link (for example by looking at the major mode).
  5784. If not, it must exit and return nil.
  5785. If yes, it should return a non-nil value after a calling
  5786. `org-store-link-props' with a list of properties and values.
  5787. Special properties are:
  5788. :type The link prefix. like \"http\". This must be given.
  5789. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5790. This is obligatory as well.
  5791. :description Optional default description for the second pair
  5792. of brackets in an Org-mode link. The user can still change
  5793. this when inserting this link into an Org-mode buffer.
  5794. In addition to these, any additional properties can be specified
  5795. and then used in remember templates.")
  5796. (defun org-add-link-type (type &optional follow export)
  5797. "Add TYPE to the list of `org-link-types'.
  5798. Re-compute all regular expressions depending on `org-link-types'
  5799. FOLLOW and EXPORT are two functions.
  5800. FOLLOW should take the link path as the single argument and do whatever
  5801. is necessary to follow the link, for example find a file or display
  5802. a mail message.
  5803. EXPORT should format the link path for export to one of the export formats.
  5804. It should be a function accepting three arguments:
  5805. path the path of the link, the text after the prefix (like \"http:\")
  5806. desc the description of the link, if any, nil if there was no descripton
  5807. format the export format, a symbol like `html' or `latex'.
  5808. The function may use the FORMAT information to return different values
  5809. depending on the format. The return value will be put literally into
  5810. the exported file.
  5811. Org-mode has a built-in default for exporting links. If you are happy with
  5812. this default, there is no need to define an export function for the link
  5813. type. For a simple example of an export function, see `org-bbdb.el'."
  5814. (add-to-list 'org-link-types type t)
  5815. (org-make-link-regexps)
  5816. (if (assoc type org-link-protocols)
  5817. (setcdr (assoc type org-link-protocols) (list follow export))
  5818. (push (list type follow export) org-link-protocols)))
  5819. ;;;###autoload
  5820. (defun org-store-link (arg)
  5821. "\\<org-mode-map>Store an org-link to the current location.
  5822. This link is added to `org-stored-links' and can later be inserted
  5823. into an org-buffer with \\[org-insert-link].
  5824. For some link types, a prefix arg is interpreted:
  5825. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5826. For file links, arg negates `org-context-in-file-links'."
  5827. (interactive "P")
  5828. (org-load-modules-maybe)
  5829. (setq org-store-link-plist nil) ; reset
  5830. (let (link cpltxt desc description search txt)
  5831. (cond
  5832. ((run-hook-with-args-until-success 'org-store-link-functions)
  5833. (setq link (plist-get org-store-link-plist :link)
  5834. desc (or (plist-get org-store-link-plist :description) link)))
  5835. ((eq major-mode 'calendar-mode)
  5836. (let ((cd (calendar-cursor-to-date)))
  5837. (setq link
  5838. (format-time-string
  5839. (car org-time-stamp-formats)
  5840. (apply 'encode-time
  5841. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5842. nil nil nil))))
  5843. (org-store-link-props :type "calendar" :date cd)))
  5844. ((eq major-mode 'w3-mode)
  5845. (setq cpltxt (url-view-url t)
  5846. link (org-make-link cpltxt))
  5847. (org-store-link-props :type "w3" :url (url-view-url t)))
  5848. ((eq major-mode 'w3m-mode)
  5849. (setq cpltxt (or w3m-current-title w3m-current-url)
  5850. link (org-make-link w3m-current-url))
  5851. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5852. ((setq search (run-hook-with-args-until-success
  5853. 'org-create-file-search-functions))
  5854. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5855. "::" search))
  5856. (setq cpltxt (or description link)))
  5857. ((eq major-mode 'image-mode)
  5858. (setq cpltxt (concat "file:"
  5859. (abbreviate-file-name buffer-file-name))
  5860. link (org-make-link cpltxt))
  5861. (org-store-link-props :type "image" :file buffer-file-name))
  5862. ((eq major-mode 'dired-mode)
  5863. ;; link to the file in the current line
  5864. (setq cpltxt (concat "file:"
  5865. (abbreviate-file-name
  5866. (expand-file-name
  5867. (dired-get-filename nil t))))
  5868. link (org-make-link cpltxt)))
  5869. ((and buffer-file-name (org-mode-p))
  5870. ;; Just link to current headline
  5871. (setq cpltxt (concat "file:"
  5872. (abbreviate-file-name buffer-file-name)))
  5873. ;; Add a context search string
  5874. (when (org-xor org-context-in-file-links arg)
  5875. ;; Check if we are on a target
  5876. (if (org-in-regexp "<<\\(.*?\\)>>")
  5877. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5878. (setq txt (cond
  5879. ((org-on-heading-p) nil)
  5880. ((org-region-active-p)
  5881. (buffer-substring (region-beginning) (region-end)))
  5882. (t nil)))
  5883. (when (or (null txt) (string-match "\\S-" txt))
  5884. (setq cpltxt
  5885. (concat cpltxt "::"
  5886. (condition-case nil
  5887. (org-make-org-heading-search-string txt)
  5888. (error "")))
  5889. desc "NONE"))))
  5890. (if (string-match "::\\'" cpltxt)
  5891. (setq cpltxt (substring cpltxt 0 -2)))
  5892. (setq link (org-make-link cpltxt)))
  5893. ((buffer-file-name (buffer-base-buffer))
  5894. ;; Just link to this file here.
  5895. (setq cpltxt (concat "file:"
  5896. (abbreviate-file-name
  5897. (buffer-file-name (buffer-base-buffer)))))
  5898. ;; Add a context string
  5899. (when (org-xor org-context-in-file-links arg)
  5900. (setq txt (if (org-region-active-p)
  5901. (buffer-substring (region-beginning) (region-end))
  5902. (buffer-substring (point-at-bol) (point-at-eol))))
  5903. ;; Only use search option if there is some text.
  5904. (when (string-match "\\S-" txt)
  5905. (setq cpltxt
  5906. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5907. desc "NONE")))
  5908. (setq link (org-make-link cpltxt)))
  5909. ((interactive-p)
  5910. (error "Cannot link to a buffer which is not visiting a file"))
  5911. (t (setq link nil)))
  5912. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5913. (setq link (or link cpltxt)
  5914. desc (or desc cpltxt))
  5915. (if (equal desc "NONE") (setq desc nil))
  5916. (if (and (interactive-p) link)
  5917. (progn
  5918. (setq org-stored-links
  5919. (cons (list link desc) org-stored-links))
  5920. (message "Stored: %s" (or desc link)))
  5921. (and link (org-make-link-string link desc)))))
  5922. (defun org-store-link-props (&rest plist)
  5923. "Store link properties, extract names and addresses."
  5924. (let (x adr)
  5925. (when (setq x (plist-get plist :from))
  5926. (setq adr (mail-extract-address-components x))
  5927. (plist-put plist :fromname (car adr))
  5928. (plist-put plist :fromaddress (nth 1 adr)))
  5929. (when (setq x (plist-get plist :to))
  5930. (setq adr (mail-extract-address-components x))
  5931. (plist-put plist :toname (car adr))
  5932. (plist-put plist :toaddress (nth 1 adr))))
  5933. (let ((from (plist-get plist :from))
  5934. (to (plist-get plist :to)))
  5935. (when (and from to org-from-is-user-regexp)
  5936. (plist-put plist :fromto
  5937. (if (string-match org-from-is-user-regexp from)
  5938. (concat "to %t")
  5939. (concat "from %f")))))
  5940. (setq org-store-link-plist plist))
  5941. (defun org-add-link-props (&rest plist)
  5942. "Add these properties to the link property list."
  5943. (let (key value)
  5944. (while plist
  5945. (setq key (pop plist) value (pop plist))
  5946. (setq org-store-link-plist
  5947. (plist-put org-store-link-plist key value)))))
  5948. (defun org-email-link-description (&optional fmt)
  5949. "Return the description part of an email link.
  5950. This takes information from `org-store-link-plist' and formats it
  5951. according to FMT (default from `org-email-link-description-format')."
  5952. (setq fmt (or fmt org-email-link-description-format))
  5953. (let* ((p org-store-link-plist)
  5954. (to (plist-get p :toaddress))
  5955. (from (plist-get p :fromaddress))
  5956. (table
  5957. (list
  5958. (cons "%c" (plist-get p :fromto))
  5959. (cons "%F" (plist-get p :from))
  5960. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5961. (cons "%T" (plist-get p :to))
  5962. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5963. (cons "%s" (plist-get p :subject))
  5964. (cons "%m" (plist-get p :message-id)))))
  5965. (when (string-match "%c" fmt)
  5966. ;; Check if the user wrote this message
  5967. (if (and org-from-is-user-regexp from to
  5968. (save-match-data (string-match org-from-is-user-regexp from)))
  5969. (setq fmt (replace-match "to %t" t t fmt))
  5970. (setq fmt (replace-match "from %f" t t fmt))))
  5971. (org-replace-escapes fmt table)))
  5972. (defun org-make-org-heading-search-string (&optional string heading)
  5973. "Make search string for STRING or current headline."
  5974. (interactive)
  5975. (let ((s (or string (org-get-heading))))
  5976. (unless (and string (not heading))
  5977. ;; We are using a headline, clean up garbage in there.
  5978. (if (string-match org-todo-regexp s)
  5979. (setq s (replace-match "" t t s)))
  5980. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5981. (setq s (replace-match "" t t s)))
  5982. (setq s (org-trim s))
  5983. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5984. org-comment-string "\\)") s)
  5985. (setq s (replace-match "" t t s)))
  5986. (while (string-match org-ts-regexp s)
  5987. (setq s (replace-match "" t t s))))
  5988. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5989. (setq s (replace-match " " t t s)))
  5990. (or string (setq s (concat "*" s))) ; Add * for headlines
  5991. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5992. (defun org-make-link (&rest strings)
  5993. "Concatenate STRINGS."
  5994. (apply 'concat strings))
  5995. (defun org-make-link-string (link &optional description)
  5996. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5997. (unless (string-match "\\S-" link)
  5998. (error "Empty link"))
  5999. (when (stringp description)
  6000. ;; Remove brackets from the description, they are fatal.
  6001. (while (string-match "\\[" description)
  6002. (setq description (replace-match "{" t t description)))
  6003. (while (string-match "\\]" description)
  6004. (setq description (replace-match "}" t t description))))
  6005. (when (equal (org-link-escape link) description)
  6006. ;; No description needed, it is identical
  6007. (setq description nil))
  6008. (when (and (not description)
  6009. (not (equal link (org-link-escape link))))
  6010. (setq description link))
  6011. (concat "[[" (org-link-escape link) "]"
  6012. (if description (concat "[" description "]") "")
  6013. "]"))
  6014. (defconst org-link-escape-chars
  6015. '((?\ . "%20")
  6016. (?\[ . "%5B")
  6017. (?\] . "%5D")
  6018. (?\340 . "%E0") ; `a
  6019. (?\342 . "%E2") ; ^a
  6020. (?\347 . "%E7") ; ,c
  6021. (?\350 . "%E8") ; `e
  6022. (?\351 . "%E9") ; 'e
  6023. (?\352 . "%EA") ; ^e
  6024. (?\356 . "%EE") ; ^i
  6025. (?\364 . "%F4") ; ^o
  6026. (?\371 . "%F9") ; `u
  6027. (?\373 . "%FB") ; ^u
  6028. (?\; . "%3B")
  6029. (?? . "%3F")
  6030. (?= . "%3D")
  6031. (?+ . "%2B")
  6032. )
  6033. "Association list of escapes for some characters problematic in links.
  6034. This is the list that is used for internal purposes.")
  6035. (defconst org-link-escape-chars-browser
  6036. '((?\ . "%20")) ; 32 for the SPC char
  6037. "Association list of escapes for some characters problematic in links.
  6038. This is the list that is used before handing over to the browser.")
  6039. (defun org-link-escape (text &optional table)
  6040. "Escape charaters in TEXT that are problematic for links."
  6041. (setq table (or table org-link-escape-chars))
  6042. (when text
  6043. (let ((re (mapconcat (lambda (x) (regexp-quote
  6044. (char-to-string (car x))))
  6045. table "\\|")))
  6046. (while (string-match re text)
  6047. (setq text
  6048. (replace-match
  6049. (cdr (assoc (string-to-char (match-string 0 text))
  6050. table))
  6051. t t text)))
  6052. text)))
  6053. (defun org-link-unescape (text &optional table)
  6054. "Reverse the action of `org-link-escape'."
  6055. (setq table (or table org-link-escape-chars))
  6056. (when text
  6057. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6058. table "\\|")))
  6059. (while (string-match re text)
  6060. (setq text
  6061. (replace-match
  6062. (char-to-string (car (rassoc (match-string 0 text) table)))
  6063. t t text)))
  6064. text)))
  6065. (defun org-xor (a b)
  6066. "Exclusive or."
  6067. (if a (not b) b))
  6068. (defun org-get-header (header)
  6069. "Find a header field in the current buffer."
  6070. (save-excursion
  6071. (goto-char (point-min))
  6072. (let ((case-fold-search t) s)
  6073. (cond
  6074. ((eq header 'from)
  6075. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  6076. (setq s (match-string 1)))
  6077. (while (string-match "\"" s)
  6078. (setq s (replace-match "" t t s)))
  6079. (if (string-match "[<(].*" s)
  6080. (setq s (replace-match "" t t s))))
  6081. ((eq header 'message-id)
  6082. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  6083. (setq s (match-string 1))))
  6084. ((eq header 'subject)
  6085. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  6086. (setq s (match-string 1)))))
  6087. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  6088. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  6089. s)))
  6090. (defun org-fixup-message-id-for-http (s)
  6091. "Replace special characters in a message id, so it can be used in an http query."
  6092. (while (string-match "<" s)
  6093. (setq s (replace-match "%3C" t t s)))
  6094. (while (string-match ">" s)
  6095. (setq s (replace-match "%3E" t t s)))
  6096. (while (string-match "@" s)
  6097. (setq s (replace-match "%40" t t s)))
  6098. s)
  6099. ;;;###autoload
  6100. (defun org-insert-link-global ()
  6101. "Insert a link like Org-mode does.
  6102. This command can be called in any mode to insert a link in Org-mode syntax."
  6103. (interactive)
  6104. (org-load-modules-maybe)
  6105. (org-run-like-in-org-mode 'org-insert-link))
  6106. (defun org-insert-link (&optional complete-file link-location)
  6107. "Insert a link. At the prompt, enter the link.
  6108. Completion can be used to select a link previously stored with
  6109. `org-store-link'. When the empty string is entered (i.e. if you just
  6110. press RET at the prompt), the link defaults to the most recently
  6111. stored link. As SPC triggers completion in the minibuffer, you need to
  6112. use M-SPC or C-q SPC to force the insertion of a space character.
  6113. You will also be prompted for a description, and if one is given, it will
  6114. be displayed in the buffer instead of the link.
  6115. If there is already a link at point, this command will allow you to edit link
  6116. and description parts.
  6117. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6118. be selected using completion. The path to the file will be relative to the
  6119. current directory if the file is in the current directory or a subdirectory.
  6120. Otherwise, the link will be the absolute path as completed in the minibuffer
  6121. \(i.e. normally ~/path/to/file).
  6122. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6123. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  6124. of `org-keep-stored-link-after-insertion'.
  6125. If `org-make-link-description-function' is non-nil, this function will be
  6126. called with the link target, and the result will be the default
  6127. link description.
  6128. If the LINK-LOCATION parameter is non-nil, this value will be
  6129. used as the link location instead of reading one interactively."
  6130. (interactive "P")
  6131. (let* ((wcf (current-window-configuration))
  6132. (region (if (org-region-active-p)
  6133. (buffer-substring (region-beginning) (region-end))))
  6134. (remove (and region (list (region-beginning) (region-end))))
  6135. (desc region)
  6136. tmphist ; byte-compile incorrectly complains about this
  6137. (link link-location)
  6138. entry file)
  6139. (cond
  6140. (link-location) ; specified by arg, just use it.
  6141. ((org-in-regexp org-bracket-link-regexp 1)
  6142. ;; We do have a link at point, and we are going to edit it.
  6143. (setq remove (list (match-beginning 0) (match-end 0)))
  6144. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6145. (setq link (read-string "Link: "
  6146. (org-link-unescape
  6147. (org-match-string-no-properties 1)))))
  6148. ((or (org-in-regexp org-angle-link-re)
  6149. (org-in-regexp org-plain-link-re))
  6150. ;; Convert to bracket link
  6151. (setq remove (list (match-beginning 0) (match-end 0))
  6152. link (read-string "Link: "
  6153. (org-remove-angle-brackets (match-string 0)))))
  6154. ((equal complete-file '(4))
  6155. ;; Completing read for file names.
  6156. (setq file (read-file-name "File: "))
  6157. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6158. (pwd1 (file-name-as-directory (abbreviate-file-name
  6159. (expand-file-name ".")))))
  6160. (cond
  6161. ((equal complete-file '(16))
  6162. (setq link (org-make-link
  6163. "file:"
  6164. (abbreviate-file-name (expand-file-name file)))))
  6165. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6166. (setq link (org-make-link "file:" (match-string 1 file))))
  6167. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6168. (expand-file-name file))
  6169. (setq link (org-make-link
  6170. "file:" (match-string 1 (expand-file-name file)))))
  6171. (t (setq link (org-make-link "file:" file))))))
  6172. (t
  6173. ;; Read link, with completion for stored links.
  6174. (with-output-to-temp-buffer "*Org Links*"
  6175. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6176. (when org-stored-links
  6177. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6178. (princ (mapconcat
  6179. (lambda (x)
  6180. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6181. (reverse org-stored-links) "\n"))))
  6182. (let ((cw (selected-window)))
  6183. (select-window (get-buffer-window "*Org Links*"))
  6184. (shrink-window-if-larger-than-buffer)
  6185. (setq truncate-lines t)
  6186. (select-window cw))
  6187. ;; Fake a link history, containing the stored links.
  6188. (setq tmphist (append (mapcar 'car org-stored-links)
  6189. org-insert-link-history))
  6190. (unwind-protect
  6191. (setq link (org-completing-read
  6192. "Link: "
  6193. (append
  6194. (mapcar (lambda (x) (list (concat (car x) ":")))
  6195. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6196. (mapcar (lambda (x) (list (concat x ":")))
  6197. org-link-types))
  6198. nil nil nil
  6199. 'tmphist
  6200. (or (car (car org-stored-links)))))
  6201. (set-window-configuration wcf)
  6202. (kill-buffer "*Org Links*"))
  6203. (setq entry (assoc link org-stored-links))
  6204. (or entry (push link org-insert-link-history))
  6205. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6206. (not org-keep-stored-link-after-insertion))
  6207. (setq org-stored-links (delq (assoc link org-stored-links)
  6208. org-stored-links)))
  6209. (setq desc (or desc (nth 1 entry)))))
  6210. (if (string-match org-plain-link-re link)
  6211. ;; URL-like link, normalize the use of angular brackets.
  6212. (setq link (org-make-link (org-remove-angle-brackets link))))
  6213. ;; Check if we are linking to the current file with a search option
  6214. ;; If yes, simplify the link by using only the search option.
  6215. (when (and buffer-file-name
  6216. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6217. (let* ((path (match-string 1 link))
  6218. (case-fold-search nil)
  6219. (search (match-string 2 link)))
  6220. (save-match-data
  6221. (if (equal (file-truename buffer-file-name) (file-truename path))
  6222. ;; We are linking to this same file, with a search option
  6223. (setq link search)))))
  6224. ;; Check if we can/should use a relative path. If yes, simplify the link
  6225. (when (string-match "\\<file:\\(.*\\)" link)
  6226. (let* ((path (match-string 1 link))
  6227. (origpath path)
  6228. (case-fold-search nil))
  6229. (cond
  6230. ((eq org-link-file-path-type 'absolute)
  6231. (setq path (abbreviate-file-name (expand-file-name path))))
  6232. ((eq org-link-file-path-type 'noabbrev)
  6233. (setq path (expand-file-name path)))
  6234. ((eq org-link-file-path-type 'relative)
  6235. (setq path (file-relative-name path)))
  6236. (t
  6237. (save-match-data
  6238. (if (string-match (concat "^" (regexp-quote
  6239. (file-name-as-directory
  6240. (expand-file-name "."))))
  6241. (expand-file-name path))
  6242. ;; We are linking a file with relative path name.
  6243. (setq path (substring (expand-file-name path)
  6244. (match-end 0)))))))
  6245. (setq link (concat "file:" path))
  6246. (if (equal desc origpath)
  6247. (setq desc path))))
  6248. (if org-make-link-description-function
  6249. (setq desc (funcall org-make-link-description-function link desc)))
  6250. (setq desc (read-string "Description: " desc))
  6251. (unless (string-match "\\S-" desc) (setq desc nil))
  6252. (if remove (apply 'delete-region remove))
  6253. (insert (org-make-link-string link desc))))
  6254. (defun org-completing-read (&rest args)
  6255. (let ((minibuffer-local-completion-map
  6256. (copy-keymap minibuffer-local-completion-map)))
  6257. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6258. (apply 'completing-read args)))
  6259. ;;; Opening/following a link
  6260. (defvar org-link-search-failed nil)
  6261. (defun org-next-link ()
  6262. "Move forward to the next link.
  6263. If the link is in hidden text, expose it."
  6264. (interactive)
  6265. (when (and org-link-search-failed (eq this-command last-command))
  6266. (goto-char (point-min))
  6267. (message "Link search wrapped back to beginning of buffer"))
  6268. (setq org-link-search-failed nil)
  6269. (let* ((pos (point))
  6270. (ct (org-context))
  6271. (a (assoc :link ct)))
  6272. (if a (goto-char (nth 2 a)))
  6273. (if (re-search-forward org-any-link-re nil t)
  6274. (progn
  6275. (goto-char (match-beginning 0))
  6276. (if (org-invisible-p) (org-show-context)))
  6277. (goto-char pos)
  6278. (setq org-link-search-failed t)
  6279. (error "No further link found"))))
  6280. (defun org-previous-link ()
  6281. "Move backward to the previous link.
  6282. If the link is in hidden text, expose it."
  6283. (interactive)
  6284. (when (and org-link-search-failed (eq this-command last-command))
  6285. (goto-char (point-max))
  6286. (message "Link search wrapped back to end of buffer"))
  6287. (setq org-link-search-failed nil)
  6288. (let* ((pos (point))
  6289. (ct (org-context))
  6290. (a (assoc :link ct)))
  6291. (if a (goto-char (nth 1 a)))
  6292. (if (re-search-backward org-any-link-re nil t)
  6293. (progn
  6294. (goto-char (match-beginning 0))
  6295. (if (org-invisible-p) (org-show-context)))
  6296. (goto-char pos)
  6297. (setq org-link-search-failed t)
  6298. (error "No further link found"))))
  6299. (defun org-find-file-at-mouse (ev)
  6300. "Open file link or URL at mouse."
  6301. (interactive "e")
  6302. (mouse-set-point ev)
  6303. (org-open-at-point 'in-emacs))
  6304. (defun org-open-at-mouse (ev)
  6305. "Open file link or URL at mouse."
  6306. (interactive "e")
  6307. (mouse-set-point ev)
  6308. (org-open-at-point))
  6309. (defvar org-window-config-before-follow-link nil
  6310. "The window configuration before following a link.
  6311. This is saved in case the need arises to restore it.")
  6312. (defvar org-open-link-marker (make-marker)
  6313. "Marker pointing to the location where `org-open-at-point; was called.")
  6314. ;;;###autoload
  6315. (defun org-open-at-point-global ()
  6316. "Follow a link like Org-mode does.
  6317. This command can be called in any mode to follow a link that has
  6318. Org-mode syntax."
  6319. (interactive)
  6320. (org-run-like-in-org-mode 'org-open-at-point))
  6321. ;;;###autoload
  6322. (defun org-open-link-from-string (s &optional arg)
  6323. "Open a link in the string S, as if it was in Org-mode."
  6324. (interactive "sLink: \nP")
  6325. (with-temp-buffer
  6326. (let ((org-inhibit-startup t))
  6327. (org-mode)
  6328. (insert s)
  6329. (goto-char (point-min))
  6330. (org-open-at-point arg))))
  6331. (defun org-open-at-point (&optional in-emacs)
  6332. "Open link at or after point.
  6333. If there is no link at point, this function will search forward up to
  6334. the end of the current subtree.
  6335. Normally, files will be opened by an appropriate application. If the
  6336. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6337. (interactive "P")
  6338. (org-load-modules-maybe)
  6339. (move-marker org-open-link-marker (point))
  6340. (setq org-window-config-before-follow-link (current-window-configuration))
  6341. (org-remove-occur-highlights nil nil t)
  6342. (if (org-at-timestamp-p t)
  6343. (org-follow-timestamp-link)
  6344. (let (type path link line search (pos (point)))
  6345. (catch 'match
  6346. (save-excursion
  6347. (skip-chars-forward "^]\n\r")
  6348. (when (org-in-regexp org-bracket-link-regexp)
  6349. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6350. (while (string-match " *\n *" link)
  6351. (setq link (replace-match " " t t link)))
  6352. (setq link (org-link-expand-abbrev link))
  6353. (if (string-match org-link-re-with-space2 link)
  6354. (setq type (match-string 1 link) path (match-string 2 link))
  6355. (setq type "thisfile" path link))
  6356. (throw 'match t)))
  6357. (when (get-text-property (point) 'org-linked-text)
  6358. (setq type "thisfile"
  6359. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6360. (1+ (point)) (point))
  6361. path (buffer-substring
  6362. (previous-single-property-change pos 'org-linked-text)
  6363. (next-single-property-change pos 'org-linked-text)))
  6364. (throw 'match t))
  6365. (save-excursion
  6366. (when (or (org-in-regexp org-angle-link-re)
  6367. (org-in-regexp org-plain-link-re))
  6368. (setq type (match-string 1) path (match-string 2))
  6369. (throw 'match t)))
  6370. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6371. (setq type "tree-match"
  6372. path (match-string 1))
  6373. (throw 'match t))
  6374. (save-excursion
  6375. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6376. (setq type "tags"
  6377. path (match-string 1))
  6378. (while (string-match ":" path)
  6379. (setq path (replace-match "+" t t path)))
  6380. (throw 'match t))))
  6381. (unless path
  6382. (error "No link found"))
  6383. ;; Remove any trailing spaces in path
  6384. (if (string-match " +\\'" path)
  6385. (setq path (replace-match "" t t path)))
  6386. (cond
  6387. ((assoc type org-link-protocols)
  6388. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6389. ((equal type "mailto")
  6390. (let ((cmd (car org-link-mailto-program))
  6391. (args (cdr org-link-mailto-program)) args1
  6392. (address path) (subject "") a)
  6393. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6394. (setq address (match-string 1 path)
  6395. subject (org-link-escape (match-string 2 path))))
  6396. (while args
  6397. (cond
  6398. ((not (stringp (car args))) (push (pop args) args1))
  6399. (t (setq a (pop args))
  6400. (if (string-match "%a" a)
  6401. (setq a (replace-match address t t a)))
  6402. (if (string-match "%s" a)
  6403. (setq a (replace-match subject t t a)))
  6404. (push a args1))))
  6405. (apply cmd (nreverse args1))))
  6406. ((member type '("http" "https" "ftp" "news"))
  6407. (browse-url (concat type ":" (org-link-escape
  6408. path org-link-escape-chars-browser))))
  6409. ((member type '("message"))
  6410. (browse-url (concat type ":" path)))
  6411. ((string= type "tags")
  6412. (org-tags-view in-emacs path))
  6413. ((string= type "thisfile")
  6414. (if in-emacs
  6415. (switch-to-buffer-other-window
  6416. (org-get-buffer-for-internal-link (current-buffer)))
  6417. (org-mark-ring-push))
  6418. (let ((cmd `(org-link-search
  6419. ,path
  6420. ,(cond ((equal in-emacs '(4)) 'occur)
  6421. ((equal in-emacs '(16)) 'org-occur)
  6422. (t nil))
  6423. ,pos)))
  6424. (condition-case nil (eval cmd)
  6425. (error (progn (widen) (eval cmd))))))
  6426. ((string= type "tree-match")
  6427. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6428. ((string= type "file")
  6429. (if (string-match "::\\([0-9]+\\)\\'" path)
  6430. (setq line (string-to-number (match-string 1 path))
  6431. path (substring path 0 (match-beginning 0)))
  6432. (if (string-match "::\\(.+\\)\\'" path)
  6433. (setq search (match-string 1 path)
  6434. path (substring path 0 (match-beginning 0)))))
  6435. (if (string-match "[*?{]" (file-name-nondirectory path))
  6436. (dired path)
  6437. (org-open-file path in-emacs line search)))
  6438. ((string= type "news")
  6439. (require 'org-gnus)
  6440. (org-gnus-follow-link path))
  6441. ((string= type "shell")
  6442. (let ((cmd path))
  6443. (if (or (not org-confirm-shell-link-function)
  6444. (funcall org-confirm-shell-link-function
  6445. (format "Execute \"%s\" in shell? "
  6446. (org-add-props cmd nil
  6447. 'face 'org-warning))))
  6448. (progn
  6449. (message "Executing %s" cmd)
  6450. (shell-command cmd))
  6451. (error "Abort"))))
  6452. ((string= type "elisp")
  6453. (let ((cmd path))
  6454. (if (or (not org-confirm-elisp-link-function)
  6455. (funcall org-confirm-elisp-link-function
  6456. (format "Execute \"%s\" as elisp? "
  6457. (org-add-props cmd nil
  6458. 'face 'org-warning))))
  6459. (message "%s => %s" cmd (eval (read cmd)))
  6460. (error "Abort"))))
  6461. (t
  6462. (browse-url-at-point)))))
  6463. (move-marker org-open-link-marker nil)
  6464. (run-hook-with-args 'org-follow-link-hook))
  6465. ;;;; Time estimates
  6466. (defun org-get-effort (&optional pom)
  6467. "Get the effort estimate for the current entry."
  6468. (org-entry-get pom org-effort-property))
  6469. ;;; File search
  6470. (defvar org-create-file-search-functions nil
  6471. "List of functions to construct the right search string for a file link.
  6472. These functions are called in turn with point at the location to
  6473. which the link should point.
  6474. A function in the hook should first test if it would like to
  6475. handle this file type, for example by checking the major-mode or
  6476. the file extension. If it decides not to handle this file, it
  6477. should just return nil to give other functions a chance. If it
  6478. does handle the file, it must return the search string to be used
  6479. when following the link. The search string will be part of the
  6480. file link, given after a double colon, and `org-open-at-point'
  6481. will automatically search for it. If special measures must be
  6482. taken to make the search successful, another function should be
  6483. added to the companion hook `org-execute-file-search-functions',
  6484. which see.
  6485. A function in this hook may also use `setq' to set the variable
  6486. `description' to provide a suggestion for the descriptive text to
  6487. be used for this link when it gets inserted into an Org-mode
  6488. buffer with \\[org-insert-link].")
  6489. (defvar org-execute-file-search-functions nil
  6490. "List of functions to execute a file search triggered by a link.
  6491. Functions added to this hook must accept a single argument, the
  6492. search string that was part of the file link, the part after the
  6493. double colon. The function must first check if it would like to
  6494. handle this search, for example by checking the major-mode or the
  6495. file extension. If it decides not to handle this search, it
  6496. should just return nil to give other functions a chance. If it
  6497. does handle the search, it must return a non-nil value to keep
  6498. other functions from trying.
  6499. Each function can access the current prefix argument through the
  6500. variable `current-prefix-argument'. Note that a single prefix is
  6501. used to force opening a link in Emacs, so it may be good to only
  6502. use a numeric or double prefix to guide the search function.
  6503. In case this is needed, a function in this hook can also restore
  6504. the window configuration before `org-open-at-point' was called using:
  6505. (set-window-configuration org-window-config-before-follow-link)")
  6506. (defun org-link-search (s &optional type avoid-pos)
  6507. "Search for a link search option.
  6508. If S is surrounded by forward slashes, it is interpreted as a
  6509. regular expression. In org-mode files, this will create an `org-occur'
  6510. sparse tree. In ordinary files, `occur' will be used to list matches.
  6511. If the current buffer is in `dired-mode', grep will be used to search
  6512. in all files. If AVOID-POS is given, ignore matches near that position."
  6513. (let ((case-fold-search t)
  6514. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6515. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6516. (append '(("") (" ") ("\t") ("\n"))
  6517. org-emphasis-alist)
  6518. "\\|") "\\)"))
  6519. (pos (point))
  6520. (pre nil) (post nil)
  6521. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6522. (cond
  6523. ;; First check if there are any special
  6524. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6525. ;; Now try the builtin stuff
  6526. ((save-excursion
  6527. (goto-char (point-min))
  6528. (and
  6529. (re-search-forward
  6530. (concat "<<" (regexp-quote s0) ">>") nil t)
  6531. (setq type 'dedicated
  6532. pos (match-beginning 0))))
  6533. ;; There is an exact target for this
  6534. (goto-char pos))
  6535. ((string-match "^/\\(.*\\)/$" s)
  6536. ;; A regular expression
  6537. (cond
  6538. ((org-mode-p)
  6539. (org-occur (match-string 1 s)))
  6540. ;;((eq major-mode 'dired-mode)
  6541. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6542. (t (org-do-occur (match-string 1 s)))))
  6543. (t
  6544. ;; A normal search strings
  6545. (when (equal (string-to-char s) ?*)
  6546. ;; Anchor on headlines, post may include tags.
  6547. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6548. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6549. s (substring s 1)))
  6550. (remove-text-properties
  6551. 0 (length s)
  6552. '(face nil mouse-face nil keymap nil fontified nil) s)
  6553. ;; Make a series of regular expressions to find a match
  6554. (setq words (org-split-string s "[ \n\r\t]+")
  6555. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6556. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6557. "\\)" markers)
  6558. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6559. re2a (concat "[ \t\r\n]" re2a_)
  6560. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6561. re4 (concat "[^a-zA-Z_]" re4_)
  6562. re1 (concat pre re2 post)
  6563. re3 (concat pre (if pre re4_ re4) post)
  6564. re5 (concat pre ".*" re4)
  6565. re2 (concat pre re2)
  6566. re2a (concat pre (if pre re2a_ re2a))
  6567. re4 (concat pre (if pre re4_ re4))
  6568. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6569. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6570. re5 "\\)"
  6571. ))
  6572. (cond
  6573. ((eq type 'org-occur) (org-occur reall))
  6574. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6575. (t (goto-char (point-min))
  6576. (setq type 'fuzzy)
  6577. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6578. (org-search-not-self 1 re1 nil t)
  6579. (org-search-not-self 1 re2 nil t)
  6580. (org-search-not-self 1 re2a nil t)
  6581. (org-search-not-self 1 re3 nil t)
  6582. (org-search-not-self 1 re4 nil t)
  6583. (org-search-not-self 1 re5 nil t)
  6584. )
  6585. (goto-char (match-beginning 1))
  6586. (goto-char pos)
  6587. (error "No match")))))
  6588. (t
  6589. ;; Normal string-search
  6590. (goto-char (point-min))
  6591. (if (search-forward s nil t)
  6592. (goto-char (match-beginning 0))
  6593. (error "No match"))))
  6594. (and (org-mode-p) (org-show-context 'link-search))
  6595. type))
  6596. (defun org-search-not-self (group &rest args)
  6597. "Execute `re-search-forward', but only accept matches that do not
  6598. enclose the position of `org-open-link-marker'."
  6599. (let ((m org-open-link-marker))
  6600. (catch 'exit
  6601. (while (apply 're-search-forward args)
  6602. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6603. (goto-char (match-end group))
  6604. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6605. (> (match-beginning 0) (marker-position m))
  6606. (< (match-end 0) (marker-position m)))
  6607. (save-match-data
  6608. (or (not (org-in-regexp
  6609. org-bracket-link-analytic-regexp 1))
  6610. (not (match-end 4)) ; no description
  6611. (and (<= (match-beginning 4) (point))
  6612. (>= (match-end 4) (point))))))
  6613. (throw 'exit (point))))))))
  6614. (defun org-get-buffer-for-internal-link (buffer)
  6615. "Return a buffer to be used for displaying the link target of internal links."
  6616. (cond
  6617. ((not org-display-internal-link-with-indirect-buffer)
  6618. buffer)
  6619. ((string-match "(Clone)$" (buffer-name buffer))
  6620. (message "Buffer is already a clone, not making another one")
  6621. ;; we also do not modify visibility in this case
  6622. buffer)
  6623. (t ; make a new indirect buffer for displaying the link
  6624. (let* ((bn (buffer-name buffer))
  6625. (ibn (concat bn "(Clone)"))
  6626. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6627. (with-current-buffer ib (org-overview))
  6628. ib))))
  6629. (defun org-do-occur (regexp &optional cleanup)
  6630. "Call the Emacs command `occur'.
  6631. If CLEANUP is non-nil, remove the printout of the regular expression
  6632. in the *Occur* buffer. This is useful if the regex is long and not useful
  6633. to read."
  6634. (occur regexp)
  6635. (when cleanup
  6636. (let ((cwin (selected-window)) win beg end)
  6637. (when (setq win (get-buffer-window "*Occur*"))
  6638. (select-window win))
  6639. (goto-char (point-min))
  6640. (when (re-search-forward "match[a-z]+" nil t)
  6641. (setq beg (match-end 0))
  6642. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6643. (setq end (1- (match-beginning 0)))))
  6644. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6645. (goto-char (point-min))
  6646. (select-window cwin))))
  6647. ;;; The mark ring for links jumps
  6648. (defvar org-mark-ring nil
  6649. "Mark ring for positions before jumps in Org-mode.")
  6650. (defvar org-mark-ring-last-goto nil
  6651. "Last position in the mark ring used to go back.")
  6652. ;; Fill and close the ring
  6653. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6654. (loop for i from 1 to org-mark-ring-length do
  6655. (push (make-marker) org-mark-ring))
  6656. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6657. org-mark-ring)
  6658. (defun org-mark-ring-push (&optional pos buffer)
  6659. "Put the current position or POS into the mark ring and rotate it."
  6660. (interactive)
  6661. (setq pos (or pos (point)))
  6662. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6663. (move-marker (car org-mark-ring)
  6664. (or pos (point))
  6665. (or buffer (current-buffer)))
  6666. (message "%s"
  6667. (substitute-command-keys
  6668. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6669. (defun org-mark-ring-goto (&optional n)
  6670. "Jump to the previous position in the mark ring.
  6671. With prefix arg N, jump back that many stored positions. When
  6672. called several times in succession, walk through the entire ring.
  6673. Org-mode commands jumping to a different position in the current file,
  6674. or to another Org-mode file, automatically push the old position
  6675. onto the ring."
  6676. (interactive "p")
  6677. (let (p m)
  6678. (if (eq last-command this-command)
  6679. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6680. (setq p org-mark-ring))
  6681. (setq org-mark-ring-last-goto p)
  6682. (setq m (car p))
  6683. (switch-to-buffer (marker-buffer m))
  6684. (goto-char m)
  6685. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6686. (defun org-remove-angle-brackets (s)
  6687. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6688. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6689. s)
  6690. (defun org-add-angle-brackets (s)
  6691. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6692. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6693. s)
  6694. (defun org-remove-double-quotes (s)
  6695. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6696. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6697. s)
  6698. ;;; Following specific links
  6699. (defun org-follow-timestamp-link ()
  6700. (cond
  6701. ((org-at-date-range-p t)
  6702. (let ((org-agenda-start-on-weekday)
  6703. (t1 (match-string 1))
  6704. (t2 (match-string 2)))
  6705. (setq t1 (time-to-days (org-time-string-to-time t1))
  6706. t2 (time-to-days (org-time-string-to-time t2)))
  6707. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6708. ((org-at-timestamp-p t)
  6709. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6710. (substring (match-string 1) 0 10)))
  6711. 1))
  6712. (t (error "This should not happen"))))
  6713. ;;; Following file links
  6714. (defvar org-wait nil)
  6715. (defun org-open-file (path &optional in-emacs line search)
  6716. "Open the file at PATH.
  6717. First, this expands any special file name abbreviations. Then the
  6718. configuration variable `org-file-apps' is checked if it contains an
  6719. entry for this file type, and if yes, the corresponding command is launched.
  6720. If no application is found, Emacs simply visits the file.
  6721. With optional argument IN-EMACS, Emacs will visit the file.
  6722. Optional LINE specifies a line to go to, optional SEARCH a string to
  6723. search for. If LINE or SEARCH is given, the file will always be
  6724. opened in Emacs.
  6725. If the file does not exist, an error is thrown."
  6726. (setq in-emacs (or in-emacs line search))
  6727. (let* ((file (if (equal path "")
  6728. buffer-file-name
  6729. (substitute-in-file-name (expand-file-name path))))
  6730. (apps (append org-file-apps (org-default-apps)))
  6731. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6732. (dirp (if remp nil (file-directory-p file)))
  6733. (dfile (downcase file))
  6734. (old-buffer (current-buffer))
  6735. (old-pos (point))
  6736. (old-mode major-mode)
  6737. ext cmd)
  6738. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6739. (setq ext (match-string 1 dfile))
  6740. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6741. (setq ext (match-string 1 dfile))))
  6742. (if in-emacs
  6743. (setq cmd 'emacs)
  6744. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6745. (and dirp (cdr (assoc 'directory apps)))
  6746. (cdr (assoc ext apps))
  6747. (cdr (assoc t apps)))))
  6748. (when (eq cmd 'mailcap)
  6749. (require 'mailcap)
  6750. (mailcap-parse-mailcaps)
  6751. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6752. (command (mailcap-mime-info mime-type)))
  6753. (if (stringp command)
  6754. (setq cmd command)
  6755. (setq cmd 'emacs))))
  6756. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6757. (not (file-exists-p file))
  6758. (not org-open-non-existing-files))
  6759. (error "No such file: %s" file))
  6760. (cond
  6761. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6762. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6763. (while (string-match "['\"]%s['\"]" cmd)
  6764. (setq cmd (replace-match "%s" t t cmd)))
  6765. (while (string-match "%s" cmd)
  6766. (setq cmd (replace-match
  6767. (save-match-data
  6768. (shell-quote-argument
  6769. (convert-standard-filename file)))
  6770. t t cmd)))
  6771. (save-window-excursion
  6772. (start-process-shell-command cmd nil cmd)
  6773. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6774. ))
  6775. ((or (stringp cmd)
  6776. (eq cmd 'emacs))
  6777. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6778. (widen)
  6779. (if line (goto-line line)
  6780. (if search (org-link-search search))))
  6781. ((consp cmd)
  6782. (let ((file (convert-standard-filename file)))
  6783. (eval cmd)))
  6784. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6785. (and (org-mode-p) (eq old-mode 'org-mode)
  6786. (or (not (equal old-buffer (current-buffer)))
  6787. (not (equal old-pos (point))))
  6788. (org-mark-ring-push old-pos old-buffer))))
  6789. (defun org-default-apps ()
  6790. "Return the default applications for this operating system."
  6791. (cond
  6792. ((eq system-type 'darwin)
  6793. org-file-apps-defaults-macosx)
  6794. ((eq system-type 'windows-nt)
  6795. org-file-apps-defaults-windowsnt)
  6796. (t org-file-apps-defaults-gnu)))
  6797. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6798. (defun org-file-remote-p (file)
  6799. "Test whether FILE specifies a location on a remote system.
  6800. Return non-nil if the location is indeed remote.
  6801. For example, the filename \"/user@host:/foo\" specifies a location
  6802. on the system \"/user@host:\"."
  6803. (cond ((fboundp 'file-remote-p)
  6804. (file-remote-p file))
  6805. ((fboundp 'tramp-handle-file-remote-p)
  6806. (tramp-handle-file-remote-p file))
  6807. ((and (boundp 'ange-ftp-name-format)
  6808. (string-match (car ange-ftp-name-format) file))
  6809. t)
  6810. (t nil)))
  6811. ;;;; Refiling
  6812. (defun org-get-org-file ()
  6813. "Read a filename, with default directory `org-directory'."
  6814. (let ((default (or org-default-notes-file remember-data-file)))
  6815. (read-file-name (format "File name [%s]: " default)
  6816. (file-name-as-directory org-directory)
  6817. default)))
  6818. (defun org-notes-order-reversed-p ()
  6819. "Check if the current file should receive notes in reversed order."
  6820. (cond
  6821. ((not org-reverse-note-order) nil)
  6822. ((eq t org-reverse-note-order) t)
  6823. ((not (listp org-reverse-note-order)) nil)
  6824. (t (catch 'exit
  6825. (let ((all org-reverse-note-order)
  6826. entry)
  6827. (while (setq entry (pop all))
  6828. (if (string-match (car entry) buffer-file-name)
  6829. (throw 'exit (cdr entry))))
  6830. nil)))))
  6831. (defvar org-refile-target-table nil
  6832. "The list of refile targets, created by `org-refile'.")
  6833. (defvar org-agenda-new-buffers nil
  6834. "Buffers created to visit agenda files.")
  6835. (defun org-get-refile-targets (&optional default-buffer)
  6836. "Produce a table with refile targets."
  6837. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6838. targets txt re files f desc descre)
  6839. (with-current-buffer (or default-buffer (current-buffer))
  6840. (while (setq entry (pop entries))
  6841. (setq files (car entry) desc (cdr entry))
  6842. (cond
  6843. ((null files) (setq files (list (current-buffer))))
  6844. ((eq files 'org-agenda-files)
  6845. (setq files (org-agenda-files 'unrestricted)))
  6846. ((and (symbolp files) (fboundp files))
  6847. (setq files (funcall files)))
  6848. ((and (symbolp files) (boundp files))
  6849. (setq files (symbol-value files))))
  6850. (if (stringp files) (setq files (list files)))
  6851. (cond
  6852. ((eq (car desc) :tag)
  6853. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6854. ((eq (car desc) :todo)
  6855. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6856. ((eq (car desc) :regexp)
  6857. (setq descre (cdr desc)))
  6858. ((eq (car desc) :level)
  6859. (setq descre (concat "^\\*\\{" (number-to-string
  6860. (if org-odd-levels-only
  6861. (1- (* 2 (cdr desc)))
  6862. (cdr desc)))
  6863. "\\}[ \t]")))
  6864. ((eq (car desc) :maxlevel)
  6865. (setq descre (concat "^\\*\\{1," (number-to-string
  6866. (if org-odd-levels-only
  6867. (1- (* 2 (cdr desc)))
  6868. (cdr desc)))
  6869. "\\}[ \t]")))
  6870. (t (error "Bad refiling target description %s" desc)))
  6871. (while (setq f (pop files))
  6872. (save-excursion
  6873. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6874. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6875. (save-excursion
  6876. (save-restriction
  6877. (widen)
  6878. (goto-char (point-min))
  6879. (while (re-search-forward descre nil t)
  6880. (goto-char (point-at-bol))
  6881. (when (looking-at org-complex-heading-regexp)
  6882. (setq txt (match-string 4)
  6883. re (concat "^" (regexp-quote
  6884. (buffer-substring (match-beginning 1)
  6885. (match-end 4)))))
  6886. (if (match-end 5) (setq re (concat re "[ \t]+"
  6887. (regexp-quote
  6888. (match-string 5)))))
  6889. (setq re (concat re "[ \t]*$"))
  6890. (when org-refile-use-outline-path
  6891. (setq txt (mapconcat 'identity
  6892. (append
  6893. (if (eq org-refile-use-outline-path 'file)
  6894. (list (file-name-nondirectory
  6895. (buffer-file-name (buffer-base-buffer))))
  6896. (if (eq org-refile-use-outline-path 'full-file-path)
  6897. (list (buffer-file-name (buffer-base-buffer)))))
  6898. (org-get-outline-path)
  6899. (list txt))
  6900. "/")))
  6901. (push (list txt f re (point)) targets))
  6902. (goto-char (point-at-eol))))))))
  6903. (nreverse targets))))
  6904. (defun org-get-outline-path ()
  6905. "Return the outline path to the current entry, as a list."
  6906. (let (rtn)
  6907. (save-excursion
  6908. (while (org-up-heading-safe)
  6909. (when (looking-at org-complex-heading-regexp)
  6910. (push (org-match-string-no-properties 4) rtn)))
  6911. rtn)))
  6912. (defvar org-refile-history nil
  6913. "History for refiling operations.")
  6914. (defun org-refile (&optional goto default-buffer)
  6915. "Move the entry at point to another heading.
  6916. The list of target headings is compiled using the information in
  6917. `org-refile-targets', which see. This list is created before each use
  6918. and will therefore always be up-to-date.
  6919. At the target location, the entry is filed as a subitem of the target heading.
  6920. Depending on `org-reverse-note-order', the new subitem will either be the
  6921. first of the last subitem.
  6922. With prefix arg GOTO, the command will only visit the target location,
  6923. not actually move anything.
  6924. With a double prefix `C-c C-c', go to the location where the last refiling
  6925. operation has put the subtree."
  6926. (interactive "P")
  6927. (let* ((cbuf (current-buffer))
  6928. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6929. pos it nbuf file re level reversed)
  6930. (if (equal goto '(16))
  6931. (org-refile-goto-last-stored)
  6932. (when (setq it (org-refile-get-location
  6933. (if goto "Goto: " "Refile to: ") default-buffer))
  6934. (setq file (nth 1 it)
  6935. re (nth 2 it)
  6936. pos (nth 3 it))
  6937. (setq nbuf (or (find-buffer-visiting file)
  6938. (find-file-noselect file)))
  6939. (if goto
  6940. (progn
  6941. (switch-to-buffer nbuf)
  6942. (goto-char pos)
  6943. (org-show-context 'org-goto))
  6944. (org-copy-subtree 1 nil t)
  6945. (save-excursion
  6946. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6947. (find-file-noselect file))))
  6948. (setq reversed (org-notes-order-reversed-p))
  6949. (save-excursion
  6950. (save-restriction
  6951. (widen)
  6952. (goto-char pos)
  6953. (looking-at outline-regexp)
  6954. (setq level (org-get-valid-level (funcall outline-level) 1))
  6955. (goto-char
  6956. (if reversed
  6957. (outline-next-heading)
  6958. (or (save-excursion (outline-get-next-sibling))
  6959. (org-end-of-subtree t t)
  6960. (point-max))))
  6961. (bookmark-set "org-refile-last-stored")
  6962. (org-paste-subtree level))))
  6963. (org-cut-subtree)
  6964. (setq org-markers-to-move nil)
  6965. (message "Entry refiled to \"%s\"" (car it)))))))
  6966. (defun org-refile-goto-last-stored ()
  6967. "Go to the location where the last refile was stored."
  6968. (interactive)
  6969. (bookmark-jump "org-refile-last-stored")
  6970. (message "This is the location of the last refile"))
  6971. (defun org-refile-get-location (&optional prompt default-buffer)
  6972. "Prompt the user for a refile location, using PROMPT."
  6973. (let ((org-refile-targets org-refile-targets)
  6974. (org-refile-use-outline-path org-refile-use-outline-path))
  6975. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6976. (unless org-refile-target-table
  6977. (error "No refile targets"))
  6978. (let* ((cbuf (current-buffer))
  6979. (cfunc (if org-refile-use-outline-path
  6980. 'org-olpath-completing-read
  6981. 'completing-read))
  6982. (extra (if org-refile-use-outline-path "/" ""))
  6983. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6984. (fname (and filename (file-truename filename)))
  6985. (tbl (mapcar
  6986. (lambda (x)
  6987. (if (not (equal fname (file-truename (nth 1 x))))
  6988. (cons (concat (car x) extra " ("
  6989. (file-name-nondirectory (nth 1 x)) ")")
  6990. (cdr x))
  6991. (cons (concat (car x) extra) (cdr x))))
  6992. org-refile-target-table))
  6993. (completion-ignore-case t))
  6994. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6995. tbl)))
  6996. (defun org-olpath-completing-read (prompt collection &rest args)
  6997. "Read an outline path like a file name."
  6998. (let ((thetable collection))
  6999. (apply
  7000. 'completing-read prompt
  7001. (lambda (string predicate &optional flag)
  7002. (let (rtn r s f (l (length string)))
  7003. (cond
  7004. ((eq flag nil)
  7005. ;; try completion
  7006. (try-completion string thetable))
  7007. ((eq flag t)
  7008. ;; all-completions
  7009. (setq rtn (all-completions string thetable predicate))
  7010. (mapcar
  7011. (lambda (x)
  7012. (setq r (substring x l))
  7013. (if (string-match " ([^)]*)$" x)
  7014. (setq f (match-string 0 x))
  7015. (setq f ""))
  7016. (if (string-match "/" r)
  7017. (concat string (substring r 0 (match-end 0)) f)
  7018. x))
  7019. rtn))
  7020. ((eq flag 'lambda)
  7021. ;; exact match?
  7022. (assoc string thetable)))
  7023. ))
  7024. args)))
  7025. ;;;; Dynamic blocks
  7026. (defun org-find-dblock (name)
  7027. "Find the first dynamic block with name NAME in the buffer.
  7028. If not found, stay at current position and return nil."
  7029. (let (pos)
  7030. (save-excursion
  7031. (goto-char (point-min))
  7032. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7033. nil t)
  7034. (match-beginning 0))))
  7035. (if pos (goto-char pos))
  7036. pos))
  7037. (defconst org-dblock-start-re
  7038. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7039. "Matches the startline of a dynamic block, with parameters.")
  7040. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7041. "Matches the end of a dyhamic block.")
  7042. (defun org-create-dblock (plist)
  7043. "Create a dynamic block section, with parameters taken from PLIST.
  7044. PLIST must containe a :name entry which is used as name of the block."
  7045. (unless (bolp) (newline))
  7046. (let ((name (plist-get plist :name)))
  7047. (insert "#+BEGIN: " name)
  7048. (while plist
  7049. (if (eq (car plist) :name)
  7050. (setq plist (cddr plist))
  7051. (insert " " (prin1-to-string (pop plist)))))
  7052. (insert "\n\n#+END:\n")
  7053. (beginning-of-line -2)))
  7054. (defun org-prepare-dblock ()
  7055. "Prepare dynamic block for refresh.
  7056. This empties the block, puts the cursor at the insert position and returns
  7057. the property list including an extra property :name with the block name."
  7058. (unless (looking-at org-dblock-start-re)
  7059. (error "Not at a dynamic block"))
  7060. (let* ((begdel (1+ (match-end 0)))
  7061. (name (org-no-properties (match-string 1)))
  7062. (params (append (list :name name)
  7063. (read (concat "(" (match-string 3) ")")))))
  7064. (unless (re-search-forward org-dblock-end-re nil t)
  7065. (error "Dynamic block not terminated"))
  7066. (setq params
  7067. (append params
  7068. (list :content (buffer-substring
  7069. begdel (match-beginning 0)))))
  7070. (delete-region begdel (match-beginning 0))
  7071. (goto-char begdel)
  7072. (open-line 1)
  7073. params))
  7074. (defun org-map-dblocks (&optional command)
  7075. "Apply COMMAND to all dynamic blocks in the current buffer.
  7076. If COMMAND is not given, use `org-update-dblock'."
  7077. (let ((cmd (or command 'org-update-dblock))
  7078. pos)
  7079. (save-excursion
  7080. (goto-char (point-min))
  7081. (while (re-search-forward org-dblock-start-re nil t)
  7082. (goto-char (setq pos (match-beginning 0)))
  7083. (condition-case nil
  7084. (funcall cmd)
  7085. (error (message "Error during update of dynamic block")))
  7086. (goto-char pos)
  7087. (unless (re-search-forward org-dblock-end-re nil t)
  7088. (error "Dynamic block not terminated"))))))
  7089. (defun org-dblock-update (&optional arg)
  7090. "User command for updating dynamic blocks.
  7091. Update the dynamic block at point. With prefix ARG, update all dynamic
  7092. blocks in the buffer."
  7093. (interactive "P")
  7094. (if arg
  7095. (org-update-all-dblocks)
  7096. (or (looking-at org-dblock-start-re)
  7097. (org-beginning-of-dblock))
  7098. (org-update-dblock)))
  7099. (defun org-update-dblock ()
  7100. "Update the dynamic block at point
  7101. This means to empty the block, parse for parameters and then call
  7102. the correct writing function."
  7103. (save-window-excursion
  7104. (let* ((pos (point))
  7105. (line (org-current-line))
  7106. (params (org-prepare-dblock))
  7107. (name (plist-get params :name))
  7108. (cmd (intern (concat "org-dblock-write:" name))))
  7109. (message "Updating dynamic block `%s' at line %d..." name line)
  7110. (funcall cmd params)
  7111. (message "Updating dynamic block `%s' at line %d...done" name line)
  7112. (goto-char pos))))
  7113. (defun org-beginning-of-dblock ()
  7114. "Find the beginning of the dynamic block at point.
  7115. Error if there is no scuh block at point."
  7116. (let ((pos (point))
  7117. beg)
  7118. (end-of-line 1)
  7119. (if (and (re-search-backward org-dblock-start-re nil t)
  7120. (setq beg (match-beginning 0))
  7121. (re-search-forward org-dblock-end-re nil t)
  7122. (> (match-end 0) pos))
  7123. (goto-char beg)
  7124. (goto-char pos)
  7125. (error "Not in a dynamic block"))))
  7126. (defun org-update-all-dblocks ()
  7127. "Update all dynamic blocks in the buffer.
  7128. This function can be used in a hook."
  7129. (when (org-mode-p)
  7130. (org-map-dblocks 'org-update-dblock)))
  7131. ;;;; Completion
  7132. (defconst org-additional-option-like-keywords
  7133. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7134. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7135. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  7136. (defcustom org-structure-template-alist
  7137. '(
  7138. ("s" "#+begin_src ?\n\n#+end_src"
  7139. "<src lang=\"?\">\n\n</src>")
  7140. ("e" "#+begin_example\n?\n#+end_example"
  7141. "<example>\n?\n</example>")
  7142. ("q" "#+begin_quote\n?\n#+end_quote"
  7143. "<quote>\n?\n</quote>")
  7144. ("v" "#+begin_verse\n?\n#+end_verse"
  7145. "<verse>\n?\n/verse>")
  7146. ("l" "#+begin_latex\n?\n#+end_latex"
  7147. "<literal style=\"latex\">\n?\n</literal>")
  7148. ("L" "#+latex: "
  7149. "<literal style=\"latex\">?</literal>")
  7150. ("h" "#+begin_html\n?\n#+end_html"
  7151. "<literal style=\"html\">\n?\n</literal>")
  7152. ("H" "#+html: "
  7153. "<literal style=\"html\">?</literal>")
  7154. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7155. ("A" "#+ascii: ")
  7156. ("i" "#+include %file ?"
  7157. "<include file=%file markup=\"?\">")
  7158. )
  7159. "Structure completion elements.
  7160. This is a list of abbreviation keys and values. The value gets inserted
  7161. it you type @samp{.} followed by the key and then the completion key,
  7162. usually `M-TAB'. %file will be replaced by a file name after prompting
  7163. for the file uning completion.
  7164. There are two templates for each key, the first uses the original Org syntax,
  7165. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7166. the default when the /org-mtags.el/ module has been loaded. See also the
  7167. variable `org-mtags-prefere-muse-templates'.
  7168. This is an experimental feature, it is undecided if it is going to stay in."
  7169. :group 'org-completion
  7170. :type '(repeat
  7171. (string :tag "Key")
  7172. (string :tag "Template")
  7173. (string :tag "Muse Template")))
  7174. (defun org-try-structure-completion ()
  7175. "Try to complete a structure template before point.
  7176. This looks for strings like \"<e\" on an otherwise empty line and
  7177. expands them."
  7178. (let ((l (buffer-substring (point-at-bol) (point)))
  7179. a)
  7180. (when (and (looking-at "[ \t]*$")
  7181. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7182. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7183. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7184. (match-beginning 1)) a)
  7185. t)))
  7186. (defun org-complete-expand-structure-template (start cell)
  7187. "Expand a structure template."
  7188. (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
  7189. (rpl (nth (if musep 2 1) cell)))
  7190. (delete-region start (point))
  7191. (when (string-match "\\`#\\+" rpl)
  7192. (cond
  7193. ((bolp))
  7194. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7195. (delete-region (point-at-bol) (point)))
  7196. (t (newline))))
  7197. (setq start (point))
  7198. (if (string-match "%file" rpl)
  7199. (setq rpl (replace-match
  7200. (concat
  7201. "\""
  7202. (save-match-data
  7203. (abbreviate-file-name (read-file-name "Include file: ")))
  7204. "\"")
  7205. t t rpl)))
  7206. (insert rpl)
  7207. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7208. (defun org-complete (&optional arg)
  7209. "Perform completion on word at point.
  7210. At the beginning of a headline, this completes TODO keywords as given in
  7211. `org-todo-keywords'.
  7212. If the current word is preceded by a backslash, completes the TeX symbols
  7213. that are supported for HTML support.
  7214. If the current word is preceded by \"#+\", completes special words for
  7215. setting file options.
  7216. In the line after \"#+STARTUP:, complete valid keywords.\"
  7217. At all other locations, this simply calls the value of
  7218. `org-completion-fallback-command'."
  7219. (interactive "P")
  7220. (org-without-partial-completion
  7221. (catch 'exit
  7222. (let* ((a nil)
  7223. (end (point))
  7224. (beg1 (save-excursion
  7225. (skip-chars-backward (org-re "[:alnum:]_@"))
  7226. (point)))
  7227. (beg (save-excursion
  7228. (skip-chars-backward "a-zA-Z0-9_:$")
  7229. (point)))
  7230. (confirm (lambda (x) (stringp (car x))))
  7231. (searchhead (equal (char-before beg) ?*))
  7232. (struct
  7233. (when (and (member (char-before beg1) '(?. ?<))
  7234. (setq a (assoc (buffer-substring beg1 (point))
  7235. org-structure-template-alist)))
  7236. (org-complete-expand-structure-template (1- beg1) a)
  7237. (throw 'exit t)))
  7238. (tag (and (equal (char-before beg1) ?:)
  7239. (equal (char-after (point-at-bol)) ?*)))
  7240. (prop (and (equal (char-before beg1) ?:)
  7241. (not (equal (char-after (point-at-bol)) ?*))))
  7242. (texp (equal (char-before beg) ?\\))
  7243. (link (equal (char-before beg) ?\[))
  7244. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7245. beg)
  7246. "#+"))
  7247. (startup (string-match "^#\\+STARTUP:.*"
  7248. (buffer-substring (point-at-bol) (point))))
  7249. (completion-ignore-case opt)
  7250. (type nil)
  7251. (tbl nil)
  7252. (table (cond
  7253. (opt
  7254. (setq type :opt)
  7255. (require 'org-exp)
  7256. (append
  7257. (mapcar
  7258. (lambda (x)
  7259. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7260. (cons (match-string 2 x) (match-string 1 x)))
  7261. (org-split-string (org-get-current-options) "\n"))
  7262. (mapcar 'list org-additional-option-like-keywords)))
  7263. (startup
  7264. (setq type :startup)
  7265. org-startup-options)
  7266. (link (append org-link-abbrev-alist-local
  7267. org-link-abbrev-alist))
  7268. (texp
  7269. (setq type :tex)
  7270. org-html-entities)
  7271. ((string-match "\\`\\*+[ \t]+\\'"
  7272. (buffer-substring (point-at-bol) beg))
  7273. (setq type :todo)
  7274. (mapcar 'list org-todo-keywords-1))
  7275. (searchhead
  7276. (setq type :searchhead)
  7277. (save-excursion
  7278. (goto-char (point-min))
  7279. (while (re-search-forward org-todo-line-regexp nil t)
  7280. (push (list
  7281. (org-make-org-heading-search-string
  7282. (match-string 3) t))
  7283. tbl)))
  7284. tbl)
  7285. (tag (setq type :tag beg beg1)
  7286. (or org-tag-alist (org-get-buffer-tags)))
  7287. (prop (setq type :prop beg beg1)
  7288. (mapcar 'list (org-buffer-property-keys nil t t)))
  7289. (t (progn
  7290. (call-interactively org-completion-fallback-command)
  7291. (throw 'exit nil)))))
  7292. (pattern (buffer-substring-no-properties beg end))
  7293. (completion (try-completion pattern table confirm)))
  7294. (cond ((eq completion t)
  7295. (if (not (assoc (upcase pattern) table))
  7296. (message "Already complete")
  7297. (if (and (equal type :opt)
  7298. (not (member (car (assoc (upcase pattern) table))
  7299. org-additional-option-like-keywords)))
  7300. (insert (substring (cdr (assoc (upcase pattern) table))
  7301. (length pattern)))
  7302. (if (memq type '(:tag :prop)) (insert ":")))))
  7303. ((null completion)
  7304. (message "Can't find completion for \"%s\"" pattern)
  7305. (ding))
  7306. ((not (string= pattern completion))
  7307. (delete-region beg end)
  7308. (if (string-match " +$" completion)
  7309. (setq completion (replace-match "" t t completion)))
  7310. (insert completion)
  7311. (if (get-buffer-window "*Completions*")
  7312. (delete-window (get-buffer-window "*Completions*")))
  7313. (if (assoc completion table)
  7314. (if (eq type :todo) (insert " ")
  7315. (if (memq type '(:tag :prop)) (insert ":"))))
  7316. (if (and (equal type :opt) (assoc completion table))
  7317. (message "%s" (substitute-command-keys
  7318. "Press \\[org-complete] again to insert example settings"))))
  7319. (t
  7320. (message "Making completion list...")
  7321. (let ((list (sort (all-completions pattern table confirm)
  7322. 'string<)))
  7323. (with-output-to-temp-buffer "*Completions*"
  7324. (condition-case nil
  7325. ;; Protection needed for XEmacs and emacs 21
  7326. (display-completion-list list pattern)
  7327. (error (display-completion-list list)))))
  7328. (message "Making completion list...%s" "done")))))))
  7329. ;;;; TODO, DEADLINE, Comments
  7330. (defun org-toggle-comment ()
  7331. "Change the COMMENT state of an entry."
  7332. (interactive)
  7333. (save-excursion
  7334. (org-back-to-heading)
  7335. (let (case-fold-search)
  7336. (if (looking-at (concat outline-regexp
  7337. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7338. (replace-match "" t t nil 1)
  7339. (if (looking-at outline-regexp)
  7340. (progn
  7341. (goto-char (match-end 0))
  7342. (insert org-comment-string " ")))))))
  7343. (defvar org-last-todo-state-is-todo nil
  7344. "This is non-nil when the last TODO state change led to a TODO state.
  7345. If the last change removed the TODO tag or switched to DONE, then
  7346. this is nil.")
  7347. (defvar org-setting-tags nil) ; dynamically skiped
  7348. (defun org-parse-local-options (string var)
  7349. "Parse STRING for startup setting relevant for variable VAR."
  7350. (let ((rtn (symbol-value var))
  7351. e opts)
  7352. (save-match-data
  7353. (if (or (not string) (not (string-match "\\S-" string)))
  7354. rtn
  7355. (setq opts (delq nil (mapcar (lambda (x)
  7356. (setq e (assoc x org-startup-options))
  7357. (if (eq (nth 1 e) var) e nil))
  7358. (org-split-string string "[ \t]+"))))
  7359. (if (not opts)
  7360. rtn
  7361. (setq rtn nil)
  7362. (while (setq e (pop opts))
  7363. (if (not (nth 3 e))
  7364. (setq rtn (nth 2 e))
  7365. (if (not (listp rtn)) (setq rtn nil))
  7366. (push (nth 2 e) rtn)))
  7367. rtn)))))
  7368. (defvar org-blocker-hook nil
  7369. "Hook for functions that are allowed to block a state change.
  7370. Each function gets as its single argument a property list, see
  7371. `org-trigger-hook' for more information about this list.
  7372. If any of the functions in this hook returns nil, the state change
  7373. is blocked.")
  7374. (defvar org-trigger-hook nil
  7375. "Hook for functions that are triggered by a state change.
  7376. Each function gets as its single argument a property list with at least
  7377. the following elements:
  7378. (:type type-of-change :position pos-at-entry-start
  7379. :from old-state :to new-state)
  7380. Depending on the type, more properties may be present.
  7381. This mechanism is currently implemented for:
  7382. TODO state changes
  7383. ------------------
  7384. :type todo-state-change
  7385. :from previous state (keyword as a string), or nil
  7386. :to new state (keyword as a string), or nil")
  7387. (defun org-todo (&optional arg)
  7388. "Change the TODO state of an item.
  7389. The state of an item is given by a keyword at the start of the heading,
  7390. like
  7391. *** TODO Write paper
  7392. *** DONE Call mom
  7393. The different keywords are specified in the variable `org-todo-keywords'.
  7394. By default the available states are \"TODO\" and \"DONE\".
  7395. So for this example: when the item starts with TODO, it is changed to DONE.
  7396. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7397. DONE are present, add TODO at the beginning of the heading.
  7398. With C-u prefix arg, use completion to determine the new state.
  7399. With numeric prefix arg, switch to that state.
  7400. For calling through lisp, arg is also interpreted in the following way:
  7401. 'none -> empty state
  7402. \"\"(empty string) -> switch to empty state
  7403. 'done -> switch to DONE
  7404. 'nextset -> switch to the next set of keywords
  7405. 'previousset -> switch to the previous set of keywords
  7406. \"WAITING\" -> switch to the specified keyword, but only if it
  7407. really is a member of `org-todo-keywords'."
  7408. (interactive "P")
  7409. (save-excursion
  7410. (catch 'exit
  7411. (org-back-to-heading)
  7412. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7413. (or (looking-at (concat " +" org-todo-regexp " *"))
  7414. (looking-at " *"))
  7415. (let* ((match-data (match-data))
  7416. (startpos (point-at-bol))
  7417. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7418. (org-log-done org-log-done)
  7419. (org-log-repeat org-log-repeat)
  7420. (org-todo-log-states org-todo-log-states)
  7421. (this (match-string 1))
  7422. (hl-pos (match-beginning 0))
  7423. (head (org-get-todo-sequence-head this))
  7424. (ass (assoc head org-todo-kwd-alist))
  7425. (interpret (nth 1 ass))
  7426. (done-word (nth 3 ass))
  7427. (final-done-word (nth 4 ass))
  7428. (last-state (or this ""))
  7429. (completion-ignore-case t)
  7430. (member (member this org-todo-keywords-1))
  7431. (tail (cdr member))
  7432. (state (cond
  7433. ((and org-todo-key-trigger
  7434. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7435. (and (not arg) org-use-fast-todo-selection
  7436. (not (eq org-use-fast-todo-selection 'prefix)))))
  7437. ;; Use fast selection
  7438. (org-fast-todo-selection))
  7439. ((and (equal arg '(4))
  7440. (or (not org-use-fast-todo-selection)
  7441. (not org-todo-key-trigger)))
  7442. ;; Read a state with completion
  7443. (completing-read "State: " (mapcar (lambda(x) (list x))
  7444. org-todo-keywords-1)
  7445. nil t))
  7446. ((eq arg 'right)
  7447. (if this
  7448. (if tail (car tail) nil)
  7449. (car org-todo-keywords-1)))
  7450. ((eq arg 'left)
  7451. (if (equal member org-todo-keywords-1)
  7452. nil
  7453. (if this
  7454. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7455. org-todo-keywords-1)
  7456. (org-last org-todo-keywords-1))))
  7457. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7458. (setq arg nil))) ; hack to fall back to cycling
  7459. (arg
  7460. ;; user or caller requests a specific state
  7461. (cond
  7462. ((equal arg "") nil)
  7463. ((eq arg 'none) nil)
  7464. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7465. ((eq arg 'nextset)
  7466. (or (car (cdr (member head org-todo-heads)))
  7467. (car org-todo-heads)))
  7468. ((eq arg 'previousset)
  7469. (let ((org-todo-heads (reverse org-todo-heads)))
  7470. (or (car (cdr (member head org-todo-heads)))
  7471. (car org-todo-heads))))
  7472. ((car (member arg org-todo-keywords-1)))
  7473. ((nth (1- (prefix-numeric-value arg))
  7474. org-todo-keywords-1))))
  7475. ((null member) (or head (car org-todo-keywords-1)))
  7476. ((equal this final-done-word) nil) ;; -> make empty
  7477. ((null tail) nil) ;; -> first entry
  7478. ((eq interpret 'sequence)
  7479. (car tail))
  7480. ((memq interpret '(type priority))
  7481. (if (eq this-command last-command)
  7482. (car tail)
  7483. (if (> (length tail) 0)
  7484. (or done-word (car org-done-keywords))
  7485. nil)))
  7486. (t nil)))
  7487. (next (if state (concat " " state " ") " "))
  7488. (change-plist (list :type 'todo-state-change :from this :to state
  7489. :position startpos))
  7490. dolog now-done-p)
  7491. (when org-blocker-hook
  7492. (unless (save-excursion
  7493. (save-match-data
  7494. (run-hook-with-args-until-failure
  7495. 'org-blocker-hook change-plist)))
  7496. (if (interactive-p)
  7497. (error "TODO state change from %s to %s blocked" this state)
  7498. ;; fail silently
  7499. (message "TODO state change from %s to %s blocked" this state)
  7500. (throw 'exit nil))))
  7501. (store-match-data match-data)
  7502. (replace-match next t t)
  7503. (unless (pos-visible-in-window-p hl-pos)
  7504. (message "TODO state changed to %s" (org-trim next)))
  7505. (unless head
  7506. (setq head (org-get-todo-sequence-head state)
  7507. ass (assoc head org-todo-kwd-alist)
  7508. interpret (nth 1 ass)
  7509. done-word (nth 3 ass)
  7510. final-done-word (nth 4 ass)))
  7511. (when (memq arg '(nextset previousset))
  7512. (message "Keyword-Set %d/%d: %s"
  7513. (- (length org-todo-sets) -1
  7514. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7515. (length org-todo-sets)
  7516. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7517. (setq org-last-todo-state-is-todo
  7518. (not (member state org-done-keywords)))
  7519. (setq now-done-p (and (member state org-done-keywords)
  7520. (not (member this org-done-keywords))))
  7521. (and logging (org-local-logging logging))
  7522. (when (and (or org-todo-log-states org-log-done)
  7523. (not (memq arg '(nextset previousset))))
  7524. ;; we need to look at recording a time and note
  7525. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7526. (nth 2 (assoc this org-todo-log-states))))
  7527. (when (and state
  7528. (member state org-not-done-keywords)
  7529. (not (member this org-not-done-keywords)))
  7530. ;; This is now a todo state and was not one before
  7531. ;; If there was a CLOSED time stamp, get rid of it.
  7532. (org-add-planning-info nil nil 'closed))
  7533. (when (and now-done-p org-log-done)
  7534. ;; It is now done, and it was not done before
  7535. (org-add-planning-info 'closed (org-current-time))
  7536. (if (and (not dolog) (eq 'note org-log-done))
  7537. (org-add-log-setup 'done state 'findpos 'note)))
  7538. (when (and state dolog)
  7539. ;; This is a non-nil state, and we need to log it
  7540. (org-add-log-setup 'state state 'findpos dolog)))
  7541. ;; Fixup tag positioning
  7542. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7543. (when org-provide-todo-statistics
  7544. (org-update-parent-todo-statistics))
  7545. (run-hooks 'org-after-todo-state-change-hook)
  7546. (if (and arg (not (member state org-done-keywords)))
  7547. (setq head (org-get-todo-sequence-head state)))
  7548. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7549. ;; Do we need to trigger a repeat?
  7550. (when now-done-p (org-auto-repeat-maybe state))
  7551. ;; Fixup cursor location if close to the keyword
  7552. (if (and (outline-on-heading-p)
  7553. (not (bolp))
  7554. (save-excursion (beginning-of-line 1)
  7555. (looking-at org-todo-line-regexp))
  7556. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7557. (progn
  7558. (goto-char (or (match-end 2) (match-end 1)))
  7559. (just-one-space)))
  7560. (when org-trigger-hook
  7561. (save-excursion
  7562. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7563. (defun org-update-parent-todo-statistics ()
  7564. "Update any statistics cookie in the parent of the current headline."
  7565. (interactive)
  7566. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7567. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7568. (catch 'exit
  7569. (save-excursion
  7570. (setq level (org-up-heading-safe))
  7571. (unless (and level
  7572. (re-search-forward box-re (point-at-eol) t))
  7573. (throw 'exit nil))
  7574. (setq is-percent (match-end 2))
  7575. (save-match-data
  7576. (unless (outline-next-heading) (throw 'exit nil))
  7577. (while (looking-at org-todo-line-regexp)
  7578. (setq kwd (match-string 2))
  7579. (and kwd (setq cnt-all (1+ cnt-all)))
  7580. (and (member kwd org-done-keywords)
  7581. (setq cnt-done (1+ cnt-done)))
  7582. (condition-case nil
  7583. (outline-forward-same-level 1)
  7584. (error (end-of-line 1)))))
  7585. (replace-match
  7586. (if is-percent
  7587. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7588. (format "[%d/%d]" cnt-done cnt-all)))
  7589. (run-hook-with-args 'org-after-todo-statistics-hook
  7590. cnt-done (- cnt-all cnt-done))))))
  7591. (defvar org-after-todo-statistics-hook nil
  7592. "Hook that is called after a TODO statistics cookie has been updated.
  7593. Each function is called with two arguments: the number of not-done entries
  7594. and the number of done entries.
  7595. For example, the following function, when added to this hook, will switch
  7596. an entry to DONE when all children are done, and back to TODO when new
  7597. entries are set to a TODO status. Note that this hook is only called
  7598. when there is a statistics cookie in the headline!
  7599. (defun org-summary-todo (n-done n-not-done)
  7600. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7601. (let (org-log-done org-log-states) ; turn off logging
  7602. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7603. ")
  7604. (defun org-local-logging (value)
  7605. "Get logging settings from a property VALUE."
  7606. (let* (words w a)
  7607. ;; directly set the variables, they are already local.
  7608. (setq org-log-done nil
  7609. org-log-repeat nil
  7610. org-todo-log-states nil)
  7611. (setq words (org-split-string value))
  7612. (while (setq w (pop words))
  7613. (cond
  7614. ((setq a (assoc w org-startup-options))
  7615. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7616. (set (nth 1 a) (nth 2 a))))
  7617. ((setq a (org-extract-log-state-settings w))
  7618. (and (member (car a) org-todo-keywords-1)
  7619. (push a org-todo-log-states)))))))
  7620. (defun org-get-todo-sequence-head (kwd)
  7621. "Return the head of the TODO sequence to which KWD belongs.
  7622. If KWD is not set, check if there is a text property remembering the
  7623. right sequence."
  7624. (let (p)
  7625. (cond
  7626. ((not kwd)
  7627. (or (get-text-property (point-at-bol) 'org-todo-head)
  7628. (progn
  7629. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7630. nil (point-at-eol)))
  7631. (get-text-property p 'org-todo-head))))
  7632. ((not (member kwd org-todo-keywords-1))
  7633. (car org-todo-keywords-1))
  7634. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7635. (defun org-fast-todo-selection ()
  7636. "Fast TODO keyword selection with single keys.
  7637. Returns the new TODO keyword, or nil if no state change should occur."
  7638. (let* ((fulltable org-todo-key-alist)
  7639. (done-keywords org-done-keywords) ;; needed for the faces.
  7640. (maxlen (apply 'max (mapcar
  7641. (lambda (x)
  7642. (if (stringp (car x)) (string-width (car x)) 0))
  7643. fulltable)))
  7644. (expert nil)
  7645. (fwidth (+ maxlen 3 1 3))
  7646. (ncol (/ (- (window-width) 4) fwidth))
  7647. tg cnt e c tbl
  7648. groups ingroup)
  7649. (save-window-excursion
  7650. (if expert
  7651. (set-buffer (get-buffer-create " *Org todo*"))
  7652. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7653. (erase-buffer)
  7654. (org-set-local 'org-done-keywords done-keywords)
  7655. (setq tbl fulltable cnt 0)
  7656. (while (setq e (pop tbl))
  7657. (cond
  7658. ((equal e '(:startgroup))
  7659. (push '() groups) (setq ingroup t)
  7660. (when (not (= cnt 0))
  7661. (setq cnt 0)
  7662. (insert "\n"))
  7663. (insert "{ "))
  7664. ((equal e '(:endgroup))
  7665. (setq ingroup nil cnt 0)
  7666. (insert "}\n"))
  7667. (t
  7668. (setq tg (car e) c (cdr e))
  7669. (if ingroup (push tg (car groups)))
  7670. (setq tg (org-add-props tg nil 'face
  7671. (org-get-todo-face tg)))
  7672. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7673. (insert "[" c "] " tg (make-string
  7674. (- fwidth 4 (length tg)) ?\ ))
  7675. (when (= (setq cnt (1+ cnt)) ncol)
  7676. (insert "\n")
  7677. (if ingroup (insert " "))
  7678. (setq cnt 0)))))
  7679. (insert "\n")
  7680. (goto-char (point-min))
  7681. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7682. (fit-window-to-buffer))
  7683. (message "[a-z..]:Set [SPC]:clear")
  7684. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7685. (cond
  7686. ((or (= c ?\C-g)
  7687. (and (= c ?q) (not (rassoc c fulltable))))
  7688. (setq quit-flag t))
  7689. ((= c ?\ ) nil)
  7690. ((setq e (rassoc c fulltable) tg (car e))
  7691. tg)
  7692. (t (setq quit-flag t))))))
  7693. (defun org-entry-is-todo-p ()
  7694. (member (org-get-todo-state) org-not-done-keywords))
  7695. (defun org-entry-is-done-p ()
  7696. (member (org-get-todo-state) org-done-keywords))
  7697. (defun org-get-todo-state ()
  7698. (save-excursion
  7699. (org-back-to-heading t)
  7700. (and (looking-at org-todo-line-regexp)
  7701. (match-end 2)
  7702. (match-string 2))))
  7703. (defun org-at-date-range-p (&optional inactive-ok)
  7704. "Is the cursor inside a date range?"
  7705. (interactive)
  7706. (save-excursion
  7707. (catch 'exit
  7708. (let ((pos (point)))
  7709. (skip-chars-backward "^[<\r\n")
  7710. (skip-chars-backward "<[")
  7711. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7712. (>= (match-end 0) pos)
  7713. (throw 'exit t))
  7714. (skip-chars-backward "^<[\r\n")
  7715. (skip-chars-backward "<[")
  7716. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7717. (>= (match-end 0) pos)
  7718. (throw 'exit t)))
  7719. nil)))
  7720. (defun org-get-repeat ()
  7721. "Check if tere is a deadline/schedule with repeater in this entry."
  7722. (save-match-data
  7723. (save-excursion
  7724. (org-back-to-heading t)
  7725. (if (re-search-forward
  7726. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7727. (match-string 1)))))
  7728. (defvar org-last-changed-timestamp)
  7729. (defvar org-log-post-message)
  7730. (defvar org-log-note-purpose)
  7731. (defvar org-log-note-how)
  7732. (defun org-auto-repeat-maybe (done-word)
  7733. "Check if the current headline contains a repeated deadline/schedule.
  7734. If yes, set TODO state back to what it was and change the base date
  7735. of repeating deadline/scheduled time stamps to new date.
  7736. This function is run automatically after each state change to a DONE state."
  7737. ;; last-state is dynamically scoped into this function
  7738. (let* ((repeat (org-get-repeat))
  7739. (aa (assoc last-state org-todo-kwd-alist))
  7740. (interpret (nth 1 aa))
  7741. (head (nth 2 aa))
  7742. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7743. (msg "Entry repeats: ")
  7744. (org-log-done nil)
  7745. (org-todo-log-states nil)
  7746. (nshiftmax 10) (nshift 0)
  7747. re type n what ts mb0 time)
  7748. (when repeat
  7749. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7750. (org-todo (if (eq interpret 'type) last-state head))
  7751. (when org-log-repeat
  7752. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7753. (memq 'org-add-log-note post-command-hook))
  7754. ;; OK, we are already setup for some record
  7755. (if (eq org-log-repeat 'note)
  7756. ;; make sure we take a note, not only a time stamp
  7757. (setq org-log-note-how 'note))
  7758. ;; Set up for taking a record
  7759. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7760. 'findpos org-log-repeat)))
  7761. (org-back-to-heading t)
  7762. (org-add-planning-info nil nil 'closed)
  7763. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7764. org-deadline-time-regexp "\\)\\|\\("
  7765. org-ts-regexp "\\)"))
  7766. (while (re-search-forward
  7767. re (save-excursion (outline-next-heading) (point)) t)
  7768. (setq type (if (match-end 1) org-scheduled-string
  7769. (if (match-end 3) org-deadline-string "Plain:"))
  7770. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7771. mb0 (match-beginning 0))
  7772. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7773. (setq n (string-to-number (match-string 2 ts))
  7774. what (match-string 3 ts))
  7775. (if (equal what "w") (setq n (* n 7) what "d"))
  7776. ;; Preparation, see if we need to modify the start date for the change
  7777. (when (match-end 1)
  7778. (setq time (save-match-data (org-time-string-to-time ts)))
  7779. (cond
  7780. ((equal (match-string 1 ts) ".")
  7781. ;; Shift starting date to today
  7782. (org-timestamp-change
  7783. (- (time-to-days (current-time)) (time-to-days time))
  7784. 'day))
  7785. ((equal (match-string 1 ts) "+")
  7786. (while (or (= nshift 0)
  7787. (<= (time-to-days time) (time-to-days (current-time))))
  7788. (when (= (incf nshift) nshiftmax)
  7789. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7790. (error "Abort")))
  7791. (org-timestamp-change n (cdr (assoc what whata)))
  7792. (org-at-timestamp-p t)
  7793. (setq ts (match-string 1))
  7794. (setq time (save-match-data (org-time-string-to-time ts))))
  7795. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7796. ;; rematch, so that we have everything in place for the real shift
  7797. (org-at-timestamp-p t)
  7798. (setq ts (match-string 1))
  7799. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7800. (org-timestamp-change n (cdr (assoc what whata)))
  7801. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7802. (setq org-log-post-message msg)
  7803. (message "%s" msg))))
  7804. (defun org-show-todo-tree (arg)
  7805. "Make a compact tree which shows all headlines marked with TODO.
  7806. The tree will show the lines where the regexp matches, and all higher
  7807. headlines above the match.
  7808. With a \\[universal-argument] prefix, also show the DONE entries.
  7809. With a numeric prefix N, construct a sparse tree for the Nth element
  7810. of `org-todo-keywords-1'."
  7811. (interactive "P")
  7812. (let ((case-fold-search nil)
  7813. (kwd-re
  7814. (cond ((null arg) org-not-done-regexp)
  7815. ((equal arg '(4))
  7816. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7817. (mapcar 'list org-todo-keywords-1))))
  7818. (concat "\\("
  7819. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7820. "\\)\\>")))
  7821. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7822. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7823. org-todo-keywords-1)))
  7824. (t (error "Invalid prefix argument: %s" arg)))))
  7825. (message "%d TODO entries found"
  7826. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7827. (defun org-deadline (&optional remove)
  7828. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7829. With argument REMOVE, remove any deadline from the item."
  7830. (interactive "P")
  7831. (if remove
  7832. (progn
  7833. (org-remove-timestamp-with-keyword org-deadline-string)
  7834. (message "Item no longer has a deadline."))
  7835. (org-add-planning-info 'deadline nil 'closed)))
  7836. (defun org-schedule (&optional remove)
  7837. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7838. With argument REMOVE, remove any scheduling date from the item."
  7839. (interactive "P")
  7840. (if remove
  7841. (progn
  7842. (org-remove-timestamp-with-keyword org-scheduled-string)
  7843. (message "Item is no longer scheduled."))
  7844. (org-add-planning-info 'scheduled nil 'closed)))
  7845. (defun org-remove-timestamp-with-keyword (keyword)
  7846. "Remove all time stamps with KEYWORD in the current entry."
  7847. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7848. beg)
  7849. (save-excursion
  7850. (org-back-to-heading t)
  7851. (setq beg (point))
  7852. (org-end-of-subtree t t)
  7853. (while (re-search-backward re beg t)
  7854. (replace-match "")
  7855. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7856. (equal (char-before) ?\ ))
  7857. (backward-delete-char 1)
  7858. (if (string-match "^[ \t]*$" (buffer-substring
  7859. (point-at-bol) (point-at-eol)))
  7860. (delete-region (point-at-bol)
  7861. (min (point-max) (1+ (point-at-eol))))))))))
  7862. (defun org-add-planning-info (what &optional time &rest remove)
  7863. "Insert new timestamp with keyword in the line directly after the headline.
  7864. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7865. If non is given, the user is prompted for a date.
  7866. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7867. be removed."
  7868. (interactive)
  7869. (let (org-time-was-given org-end-time-was-given ts
  7870. end default-time default-input)
  7871. (when (and (not time) (memq what '(scheduled deadline)))
  7872. ;; Try to get a default date/time from existing timestamp
  7873. (save-excursion
  7874. (org-back-to-heading t)
  7875. (setq end (save-excursion (outline-next-heading) (point)))
  7876. (when (re-search-forward (if (eq what 'scheduled)
  7877. org-scheduled-time-regexp
  7878. org-deadline-time-regexp)
  7879. end t)
  7880. (setq ts (match-string 1)
  7881. default-time
  7882. (apply 'encode-time (org-parse-time-string ts))
  7883. default-input (and ts (org-get-compact-tod ts))))))
  7884. (when what
  7885. ;; If necessary, get the time from the user
  7886. (setq time (or time (org-read-date nil 'to-time nil nil
  7887. default-time default-input))))
  7888. (when (and org-insert-labeled-timestamps-at-point
  7889. (member what '(scheduled deadline)))
  7890. (insert
  7891. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7892. (org-insert-time-stamp time org-time-was-given
  7893. nil nil nil (list org-end-time-was-given))
  7894. (setq what nil))
  7895. (save-excursion
  7896. (save-restriction
  7897. (let (col list elt ts buffer-invisibility-spec)
  7898. (org-back-to-heading t)
  7899. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7900. (goto-char (match-end 1))
  7901. (setq col (current-column))
  7902. (goto-char (match-end 0))
  7903. (if (eobp) (insert "\n") (forward-char 1))
  7904. (if (and (not (looking-at outline-regexp))
  7905. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7906. "[^\r\n]*"))
  7907. (not (equal (match-string 1) org-clock-string)))
  7908. (narrow-to-region (match-beginning 0) (match-end 0))
  7909. (insert-before-markers "\n")
  7910. (backward-char 1)
  7911. (narrow-to-region (point) (point))
  7912. (and org-adapt-indentation (org-indent-to-column col)))
  7913. ;; Check if we have to remove something.
  7914. (setq list (cons what remove))
  7915. (while list
  7916. (setq elt (pop list))
  7917. (goto-char (point-min))
  7918. (when (or (and (eq elt 'scheduled)
  7919. (re-search-forward org-scheduled-time-regexp nil t))
  7920. (and (eq elt 'deadline)
  7921. (re-search-forward org-deadline-time-regexp nil t))
  7922. (and (eq elt 'closed)
  7923. (re-search-forward org-closed-time-regexp nil t)))
  7924. (replace-match "")
  7925. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7926. (if (looking-at " +") (replace-match ""))))
  7927. (goto-char (point-max))
  7928. (when what
  7929. (insert
  7930. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7931. (cond ((eq what 'scheduled) org-scheduled-string)
  7932. ((eq what 'deadline) org-deadline-string)
  7933. ((eq what 'closed) org-closed-string))
  7934. " ")
  7935. (setq ts (org-insert-time-stamp
  7936. time
  7937. (or org-time-was-given
  7938. (and (eq what 'closed) org-log-done-with-time))
  7939. (eq what 'closed)
  7940. nil nil (list org-end-time-was-given)))
  7941. (end-of-line 1))
  7942. (goto-char (point-min))
  7943. (widen)
  7944. (if (and (looking-at "[ \t]+\n")
  7945. (equal (char-before) ?\n))
  7946. (delete-region (1- (point)) (point-at-eol)))
  7947. ts)))))
  7948. (defvar org-log-note-marker (make-marker))
  7949. (defvar org-log-note-purpose nil)
  7950. (defvar org-log-note-state nil)
  7951. (defvar org-log-note-how nil)
  7952. (defvar org-log-note-window-configuration nil)
  7953. (defvar org-log-note-return-to (make-marker))
  7954. (defvar org-log-post-message nil
  7955. "Message to be displayed after a log note has been stored.
  7956. The auto-repeater uses this.")
  7957. (defun org-add-note ()
  7958. "Add a note to the current entry.
  7959. This is done in the same way as adding a state change note."
  7960. (interactive)
  7961. (org-add-log-setup 'note nil t nil))
  7962. (defun org-add-log-setup (&optional purpose state findpos how)
  7963. "Set up the post command hook to take a note.
  7964. If this is about to TODO state change, the new state is expected in STATE.
  7965. When FINDPOS is non-nil, find the correct position for the note in
  7966. the current entry. If not, assume that it can be inserted at point."
  7967. (save-excursion
  7968. (when findpos
  7969. (org-back-to-heading t)
  7970. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7971. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7972. "[^\r\n]*\\)?"))
  7973. (goto-char (match-end 0))
  7974. (unless org-log-states-order-reversed
  7975. (and (= (char-after) ?\n) (forward-char 1))
  7976. (org-skip-over-state-notes)
  7977. (skip-chars-backward " \t\n\r")))
  7978. (move-marker org-log-note-marker (point))
  7979. (setq org-log-note-purpose purpose
  7980. org-log-note-state state
  7981. org-log-note-how how)
  7982. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  7983. (defun org-skip-over-state-notes ()
  7984. "Skip past the list of State notes in an entry."
  7985. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7986. (while (looking-at "[ \t]*- State")
  7987. (condition-case nil
  7988. (org-next-item)
  7989. (error (org-end-of-item)))))
  7990. (defun org-add-log-note (&optional purpose)
  7991. "Pop up a window for taking a note, and add this note later at point."
  7992. (remove-hook 'post-command-hook 'org-add-log-note)
  7993. (setq org-log-note-window-configuration (current-window-configuration))
  7994. (delete-other-windows)
  7995. (move-marker org-log-note-return-to (point))
  7996. (switch-to-buffer (marker-buffer org-log-note-marker))
  7997. (goto-char org-log-note-marker)
  7998. (org-switch-to-buffer-other-window "*Org Note*")
  7999. (erase-buffer)
  8000. (if (memq org-log-note-how '(time state))
  8001. (org-store-log-note)
  8002. (let ((org-inhibit-startup t)) (org-mode))
  8003. (insert (format "# Insert note for %s.
  8004. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8005. (cond
  8006. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8007. ((eq org-log-note-purpose 'done) "closed todo item")
  8008. ((eq org-log-note-purpose 'state)
  8009. (format "state change to \"%s\"" org-log-note-state))
  8010. ((eq org-log-note-purpose 'note)
  8011. "this entry")
  8012. (t (error "This should not happen")))))
  8013. (org-set-local 'org-finish-function 'org-store-log-note)))
  8014. (defvar org-note-abort nil) ; dynamically scoped
  8015. (defun org-store-log-note ()
  8016. "Finish taking a log note, and insert it to where it belongs."
  8017. (let ((txt (buffer-string))
  8018. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8019. lines ind)
  8020. (kill-buffer (current-buffer))
  8021. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8022. (setq txt (replace-match "" t t txt)))
  8023. (if (string-match "\\s-+\\'" txt)
  8024. (setq txt (replace-match "" t t txt)))
  8025. (setq lines (org-split-string txt "\n"))
  8026. (when (and note (string-match "\\S-" note))
  8027. (setq note
  8028. (org-replace-escapes
  8029. note
  8030. (list (cons "%u" (user-login-name))
  8031. (cons "%U" user-full-name)
  8032. (cons "%t" (format-time-string
  8033. (org-time-stamp-format 'long 'inactive)
  8034. (current-time)))
  8035. (cons "%s" (if org-log-note-state
  8036. (concat "\"" org-log-note-state "\"")
  8037. "")))))
  8038. (if lines (setq note (concat note " \\\\")))
  8039. (push note lines))
  8040. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8041. (when lines
  8042. (save-excursion
  8043. (set-buffer (marker-buffer org-log-note-marker))
  8044. (save-excursion
  8045. (goto-char org-log-note-marker)
  8046. (move-marker org-log-note-marker nil)
  8047. (end-of-line 1)
  8048. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8049. (indent-relative nil)
  8050. (insert "- " (pop lines))
  8051. (org-indent-line-function)
  8052. (beginning-of-line 1)
  8053. (looking-at "[ \t]*")
  8054. (setq ind (concat (match-string 0) " "))
  8055. (end-of-line 1)
  8056. (while lines (insert "\n" ind (pop lines)))))))
  8057. (set-window-configuration org-log-note-window-configuration)
  8058. (with-current-buffer (marker-buffer org-log-note-return-to)
  8059. (goto-char org-log-note-return-to))
  8060. (move-marker org-log-note-return-to nil)
  8061. (and org-log-post-message (message "%s" org-log-post-message)))
  8062. (defun org-sparse-tree (&optional arg)
  8063. "Create a sparse tree, prompt for the details.
  8064. This command can create sparse trees. You first need to select the type
  8065. of match used to create the tree:
  8066. t Show entries with a specific TODO keyword.
  8067. T Show entries selected by a tags match.
  8068. p Enter a property name and its value (both with completion on existing
  8069. names/values) and show entries with that property.
  8070. r Show entries matching a regular expression
  8071. d Show deadlines due within `org-deadline-warning-days'."
  8072. (interactive "P")
  8073. (let (ans kwd value)
  8074. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8075. (setq ans (read-char-exclusive))
  8076. (cond
  8077. ((equal ans ?d)
  8078. (call-interactively 'org-check-deadlines))
  8079. ((equal ans ?b)
  8080. (call-interactively 'org-check-before-date))
  8081. ((equal ans ?t)
  8082. (org-show-todo-tree '(4)))
  8083. ((equal ans ?T)
  8084. (call-interactively 'org-tags-sparse-tree))
  8085. ((member ans '(?p ?P))
  8086. (setq kwd (completing-read "Property: "
  8087. (mapcar 'list (org-buffer-property-keys))))
  8088. (setq value (completing-read "Value: "
  8089. (mapcar 'list (org-property-values kwd))))
  8090. (unless (string-match "\\`{.*}\\'" value)
  8091. (setq value (concat "\"" value "\"")))
  8092. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8093. ((member ans '(?r ?R ?/))
  8094. (call-interactively 'org-occur))
  8095. (t (error "No such sparse tree command \"%c\"" ans)))))
  8096. (defvar org-occur-highlights nil
  8097. "List of overlays used for occur matches.")
  8098. (make-variable-buffer-local 'org-occur-highlights)
  8099. (defvar org-occur-parameters nil
  8100. "Parameters of the active org-occur calls.
  8101. This is a list, each call to org-occur pushes as cons cell,
  8102. containing the regular expression and the callback, onto the list.
  8103. The list can contain several entries if `org-occur' has been called
  8104. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8105. will only contain one set of parameters. When the highlights are
  8106. removed (for example with `C-c C-c', or with the next edit (depending
  8107. on `org-remove-highlights-with-change'), this variable is emptied
  8108. as well.")
  8109. (make-variable-buffer-local 'org-occur-parameters)
  8110. (defun org-occur (regexp &optional keep-previous callback)
  8111. "Make a compact tree which shows all matches of REGEXP.
  8112. The tree will show the lines where the regexp matches, and all higher
  8113. headlines above the match. It will also show the heading after the match,
  8114. to make sure editing the matching entry is easy.
  8115. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8116. call to `org-occur' will be kept, to allow stacking of calls to this
  8117. command.
  8118. If CALLBACK is non-nil, it is a function which is called to confirm
  8119. that the match should indeed be shown."
  8120. (interactive "sRegexp: \nP")
  8121. (unless keep-previous
  8122. (org-remove-occur-highlights nil nil t))
  8123. (push (cons regexp callback) org-occur-parameters)
  8124. (let ((cnt 0))
  8125. (save-excursion
  8126. (goto-char (point-min))
  8127. (if (or (not keep-previous) ; do not want to keep
  8128. (not org-occur-highlights)) ; no previous matches
  8129. ;; hide everything
  8130. (org-overview))
  8131. (while (re-search-forward regexp nil t)
  8132. (when (or (not callback)
  8133. (save-match-data (funcall callback)))
  8134. (setq cnt (1+ cnt))
  8135. (when org-highlight-sparse-tree-matches
  8136. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8137. (org-show-context 'occur-tree))))
  8138. (when org-remove-highlights-with-change
  8139. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8140. nil 'local))
  8141. (unless org-sparse-tree-open-archived-trees
  8142. (org-hide-archived-subtrees (point-min) (point-max)))
  8143. (run-hooks 'org-occur-hook)
  8144. (if (interactive-p)
  8145. (message "%d match(es) for regexp %s" cnt regexp))
  8146. cnt))
  8147. (defun org-show-context (&optional key)
  8148. "Make sure point and context and visible.
  8149. How much context is shown depends upon the variables
  8150. `org-show-hierarchy-above', `org-show-following-heading'. and
  8151. `org-show-siblings'."
  8152. (let ((heading-p (org-on-heading-p t))
  8153. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8154. (following-p (org-get-alist-option org-show-following-heading key))
  8155. (entry-p (org-get-alist-option org-show-entry-below key))
  8156. (siblings-p (org-get-alist-option org-show-siblings key)))
  8157. (catch 'exit
  8158. ;; Show heading or entry text
  8159. (if (and heading-p (not entry-p))
  8160. (org-flag-heading nil) ; only show the heading
  8161. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8162. (org-show-hidden-entry))) ; show entire entry
  8163. (when following-p
  8164. ;; Show next sibling, or heading below text
  8165. (save-excursion
  8166. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8167. (org-flag-heading nil))))
  8168. (when siblings-p (org-show-siblings))
  8169. (when hierarchy-p
  8170. ;; show all higher headings, possibly with siblings
  8171. (save-excursion
  8172. (while (and (condition-case nil
  8173. (progn (org-up-heading-all 1) t)
  8174. (error nil))
  8175. (not (bobp)))
  8176. (org-flag-heading nil)
  8177. (when siblings-p (org-show-siblings))))))))
  8178. (defun org-reveal (&optional siblings)
  8179. "Show current entry, hierarchy above it, and the following headline.
  8180. This can be used to show a consistent set of context around locations
  8181. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8182. not t for the search context.
  8183. With optional argument SIBLINGS, on each level of the hierarchy all
  8184. siblings are shown. This repairs the tree structure to what it would
  8185. look like when opened with hierarchical calls to `org-cycle'."
  8186. (interactive "P")
  8187. (let ((org-show-hierarchy-above t)
  8188. (org-show-following-heading t)
  8189. (org-show-siblings (if siblings t org-show-siblings)))
  8190. (org-show-context nil)))
  8191. (defun org-highlight-new-match (beg end)
  8192. "Highlight from BEG to END and mark the highlight is an occur headline."
  8193. (let ((ov (org-make-overlay beg end)))
  8194. (org-overlay-put ov 'face 'secondary-selection)
  8195. (push ov org-occur-highlights)))
  8196. (defun org-remove-occur-highlights (&optional beg end noremove)
  8197. "Remove the occur highlights from the buffer.
  8198. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8199. from the `before-change-functions' in the current buffer."
  8200. (interactive)
  8201. (unless org-inhibit-highlight-removal
  8202. (mapc 'org-delete-overlay org-occur-highlights)
  8203. (setq org-occur-highlights nil)
  8204. (setq org-occur-parameters nil)
  8205. (unless noremove
  8206. (remove-hook 'before-change-functions
  8207. 'org-remove-occur-highlights 'local))))
  8208. ;;;; Priorities
  8209. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8210. "Regular expression matching the priority indicator.")
  8211. (defvar org-remove-priority-next-time nil)
  8212. (defun org-priority-up ()
  8213. "Increase the priority of the current item."
  8214. (interactive)
  8215. (org-priority 'up))
  8216. (defun org-priority-down ()
  8217. "Decrease the priority of the current item."
  8218. (interactive)
  8219. (org-priority 'down))
  8220. (defun org-priority (&optional action)
  8221. "Change the priority of an item by ARG.
  8222. ACTION can be `set', `up', `down', or a character."
  8223. (interactive)
  8224. (setq action (or action 'set))
  8225. (let (current new news have remove)
  8226. (save-excursion
  8227. (org-back-to-heading)
  8228. (if (looking-at org-priority-regexp)
  8229. (setq current (string-to-char (match-string 2))
  8230. have t)
  8231. (setq current org-default-priority))
  8232. (cond
  8233. ((or (eq action 'set)
  8234. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8235. (if (not (eq action 'set))
  8236. (setq new action)
  8237. (message "Priority %c-%c, SPC to remove: "
  8238. org-highest-priority org-lowest-priority)
  8239. (setq new (read-char-exclusive)))
  8240. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8241. (= (upcase org-lowest-priority) org-lowest-priority))
  8242. (setq new (upcase new)))
  8243. (cond ((equal new ?\ ) (setq remove t))
  8244. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8245. (error "Priority must be between `%c' and `%c'"
  8246. org-highest-priority org-lowest-priority))))
  8247. ((eq action 'up)
  8248. (if (and (not have) (eq last-command this-command))
  8249. (setq new org-lowest-priority)
  8250. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8251. org-default-priority (1- current)))))
  8252. ((eq action 'down)
  8253. (if (and (not have) (eq last-command this-command))
  8254. (setq new org-highest-priority)
  8255. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8256. org-default-priority (1+ current)))))
  8257. (t (error "Invalid action")))
  8258. (if (or (< (upcase new) org-highest-priority)
  8259. (> (upcase new) org-lowest-priority))
  8260. (setq remove t))
  8261. (setq news (format "%c" new))
  8262. (if have
  8263. (if remove
  8264. (replace-match "" t t nil 1)
  8265. (replace-match news t t nil 2))
  8266. (if remove
  8267. (error "No priority cookie found in line")
  8268. (looking-at org-todo-line-regexp)
  8269. (if (match-end 2)
  8270. (progn
  8271. (goto-char (match-end 2))
  8272. (insert " [#" news "]"))
  8273. (goto-char (match-beginning 3))
  8274. (insert "[#" news "] ")))))
  8275. (org-preserve-lc (org-set-tags nil 'align))
  8276. (if remove
  8277. (message "Priority removed")
  8278. (message "Priority of current item set to %s" news))))
  8279. (defun org-get-priority (s)
  8280. "Find priority cookie and return priority."
  8281. (save-match-data
  8282. (if (not (string-match org-priority-regexp s))
  8283. (* 1000 (- org-lowest-priority org-default-priority))
  8284. (* 1000 (- org-lowest-priority
  8285. (string-to-char (match-string 2 s)))))))
  8286. ;;;; Tags
  8287. (defun org-scan-tags (action matcher &optional todo-only)
  8288. "Scan headline tags with inheritance and produce output ACTION.
  8289. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8290. evaluated, testing if a given set of tags qualifies a headline for
  8291. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8292. are included in the output."
  8293. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8294. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8295. (org-re
  8296. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8297. (props (list 'face nil
  8298. 'done-face 'org-done
  8299. 'undone-face nil
  8300. 'mouse-face 'highlight
  8301. 'org-not-done-regexp org-not-done-regexp
  8302. 'org-todo-regexp org-todo-regexp
  8303. 'keymap org-agenda-keymap
  8304. 'help-echo
  8305. (format "mouse-2 or RET jump to org file %s"
  8306. (abbreviate-file-name
  8307. (or (buffer-file-name (buffer-base-buffer))
  8308. (buffer-name (buffer-base-buffer)))))))
  8309. (case-fold-search nil)
  8310. lspos tags tags-list
  8311. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8312. (llast 0) rtn level category i txt
  8313. todo marker entry priority)
  8314. (save-excursion
  8315. (goto-char (point-min))
  8316. (when (eq action 'sparse-tree)
  8317. (org-overview)
  8318. (org-remove-occur-highlights))
  8319. (while (re-search-forward re nil t)
  8320. (catch :skip
  8321. (setq todo (if (match-end 1) (match-string 2))
  8322. tags (if (match-end 4) (match-string 4)))
  8323. (goto-char (setq lspos (1+ (match-beginning 0))))
  8324. (setq level (org-reduced-level (funcall outline-level))
  8325. category (org-get-category))
  8326. (setq i llast llast level)
  8327. ;; remove tag lists from same and sublevels
  8328. (while (>= i level)
  8329. (when (setq entry (assoc i tags-alist))
  8330. (setq tags-alist (delete entry tags-alist)))
  8331. (setq i (1- i)))
  8332. ;; add the next tags
  8333. (when tags
  8334. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8335. tags-alist
  8336. (cons (cons level tags) tags-alist)))
  8337. ;; compile tags for current headline
  8338. (setq tags-list
  8339. (if org-use-tag-inheritance
  8340. (apply 'append (mapcar 'cdr tags-alist))
  8341. tags))
  8342. (when (and tags org-use-tag-inheritance
  8343. (not (eq t org-use-tag-inheritance)))
  8344. ;; selective inheritance, remove uninherited ones
  8345. (setcdr (car tags-alist)
  8346. (org-remove-uniherited-tags (cdar tags-alist))))
  8347. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8348. (eval matcher)
  8349. (or (not org-agenda-skip-archived-trees)
  8350. (not (member org-archive-tag tags-list))))
  8351. (and (eq action 'agenda) (org-agenda-skip))
  8352. ;; list this headline
  8353. (if (eq action 'sparse-tree)
  8354. (progn
  8355. (and org-highlight-sparse-tree-matches
  8356. (org-get-heading) (match-end 0)
  8357. (org-highlight-new-match
  8358. (match-beginning 0) (match-beginning 1)))
  8359. (org-show-context 'tags-tree))
  8360. (setq txt (org-format-agenda-item
  8361. ""
  8362. (concat
  8363. (if org-tags-match-list-sublevels
  8364. (make-string (1- level) ?.) "")
  8365. (org-get-heading))
  8366. category tags-list)
  8367. priority (org-get-priority txt))
  8368. (goto-char lspos)
  8369. (setq marker (org-agenda-new-marker))
  8370. (org-add-props txt props
  8371. 'org-marker marker 'org-hd-marker marker 'org-category category
  8372. 'priority priority 'type "tagsmatch")
  8373. (push txt rtn))
  8374. ;; if we are to skip sublevels, jump to end of subtree
  8375. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8376. (when (and (eq action 'sparse-tree)
  8377. (not org-sparse-tree-open-archived-trees))
  8378. (org-hide-archived-subtrees (point-min) (point-max)))
  8379. (nreverse rtn)))
  8380. (defun org-remove-uniherited-tags (tags)
  8381. "Remove all tags that are not inherited from the list TAGS."
  8382. (cond
  8383. ((eq org-use-tag-inheritance t) tags)
  8384. ((not org-use-tag-inheritance) nil)
  8385. ((stringp org-use-tag-inheritance)
  8386. (delq nil (mapcar
  8387. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8388. tags)))
  8389. ((listp org-use-tag-inheritance)
  8390. (org-delete-all org-use-tag-inheritance tags))))
  8391. (defvar todo-only) ;; dynamically scoped
  8392. (defun org-tags-sparse-tree (&optional todo-only match)
  8393. "Create a sparse tree according to tags string MATCH.
  8394. MATCH can contain positive and negative selection of tags, like
  8395. \"+WORK+URGENT-WITHBOSS\".
  8396. If optional argument TODO_ONLY is non-nil, only select lines that are
  8397. also TODO lines."
  8398. (interactive "P")
  8399. (org-prepare-agenda-buffers (list (current-buffer)))
  8400. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8401. (defvar org-cached-props nil)
  8402. (defun org-cached-entry-get (pom property)
  8403. (if (or (eq t org-use-property-inheritance)
  8404. (and (stringp org-use-property-inheritance)
  8405. (string-match org-use-property-inheritance property))
  8406. (and (listp org-use-property-inheritance)
  8407. (member property org-use-property-inheritance)))
  8408. ;; Caching is not possible, check it directly
  8409. (org-entry-get pom property 'inherit)
  8410. ;; Get all properties, so that we can do complicated checks easily
  8411. (cdr (assoc property (or org-cached-props
  8412. (setq org-cached-props
  8413. (org-entry-properties pom)))))))
  8414. (defun org-global-tags-completion-table (&optional files)
  8415. "Return the list of all tags in all agenda buffer/files."
  8416. (save-excursion
  8417. (org-uniquify
  8418. (delq nil
  8419. (apply 'append
  8420. (mapcar
  8421. (lambda (file)
  8422. (set-buffer (find-file-noselect file))
  8423. (append (org-get-buffer-tags)
  8424. (mapcar (lambda (x) (if (stringp (car-safe x))
  8425. (list (car-safe x)) nil))
  8426. org-tag-alist)))
  8427. (if (and files (car files))
  8428. files
  8429. (org-agenda-files))))))))
  8430. (defun org-make-tags-matcher (match)
  8431. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8432. ;; todo-only is scoped dynamically into this function, and the function
  8433. ;; may change it it the matcher asksk for it.
  8434. (unless match
  8435. ;; Get a new match request, with completion
  8436. (let ((org-last-tags-completion-table
  8437. (org-global-tags-completion-table)))
  8438. (setq match (completing-read
  8439. "Match: " 'org-tags-completion-function nil nil nil
  8440. 'org-tags-history))))
  8441. ;; Parse the string and create a lisp form
  8442. (let ((match0 match)
  8443. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8444. minus tag mm
  8445. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8446. orterms term orlist re-p str-p level-p level-op
  8447. prop-p pn pv po cat-p gv)
  8448. (if (string-match "/+" match)
  8449. ;; match contains also a todo-matching request
  8450. (progn
  8451. (setq tagsmatch (substring match 0 (match-beginning 0))
  8452. todomatch (substring match (match-end 0)))
  8453. (if (string-match "^!" todomatch)
  8454. (setq todo-only t todomatch (substring todomatch 1)))
  8455. (if (string-match "^\\s-*$" todomatch)
  8456. (setq todomatch nil)))
  8457. ;; only matching tags
  8458. (setq tagsmatch match todomatch nil))
  8459. ;; Make the tags matcher
  8460. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8461. (setq tagsmatcher t)
  8462. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8463. (while (setq term (pop orterms))
  8464. (while (and (equal (substring term -1) "\\") orterms)
  8465. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8466. (while (string-match re term)
  8467. (setq minus (and (match-end 1)
  8468. (equal (match-string 1 term) "-"))
  8469. tag (match-string 2 term)
  8470. re-p (equal (string-to-char tag) ?{)
  8471. level-p (match-end 4)
  8472. prop-p (match-end 5)
  8473. mm (cond
  8474. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8475. (level-p
  8476. (setq level-op (org-op-to-function (match-string 3 term)))
  8477. `(,level-op level ,(string-to-number
  8478. (match-string 4 term))))
  8479. (prop-p
  8480. (setq pn (match-string 5 term)
  8481. po (match-string 6 term)
  8482. pv (match-string 7 term)
  8483. cat-p (equal pn "CATEGORY")
  8484. re-p (equal (string-to-char pv) ?{)
  8485. str-p (equal (string-to-char pv) ?\")
  8486. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8487. (setq po (org-op-to-function po str-p))
  8488. (if (equal pn "CATEGORY")
  8489. (setq gv '(get-text-property (point) 'org-category))
  8490. (setq gv `(org-cached-entry-get nil ,pn)))
  8491. (if re-p
  8492. (if (eq po 'org<>)
  8493. `(not (string-match ,pv (or ,gv "")))
  8494. `(string-match ,pv (or ,gv "")))
  8495. (if str-p
  8496. `(,po (or ,gv "") ,pv)
  8497. `(,po (string-to-number (or ,gv ""))
  8498. ,(string-to-number pv) ))))
  8499. (t `(member ,(downcase tag) tags-list)))
  8500. mm (if minus (list 'not mm) mm)
  8501. term (substring term (match-end 0)))
  8502. (push mm tagsmatcher))
  8503. (push (if (> (length tagsmatcher) 1)
  8504. (cons 'and tagsmatcher)
  8505. (car tagsmatcher))
  8506. orlist)
  8507. (setq tagsmatcher nil))
  8508. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8509. (setq tagsmatcher
  8510. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8511. ;; Make the todo matcher
  8512. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8513. (setq todomatcher t)
  8514. (setq orterms (org-split-string todomatch "|") orlist nil)
  8515. (while (setq term (pop orterms))
  8516. (while (string-match re term)
  8517. (setq minus (and (match-end 1)
  8518. (equal (match-string 1 term) "-"))
  8519. kwd (match-string 2 term)
  8520. re-p (equal (string-to-char kwd) ?{)
  8521. term (substring term (match-end 0))
  8522. mm (if re-p
  8523. `(string-match ,(substring kwd 1 -1) todo)
  8524. (list 'equal 'todo kwd))
  8525. mm (if minus (list 'not mm) mm))
  8526. (push mm todomatcher))
  8527. (push (if (> (length todomatcher) 1)
  8528. (cons 'and todomatcher)
  8529. (car todomatcher))
  8530. orlist)
  8531. (setq todomatcher nil))
  8532. (setq todomatcher (if (> (length orlist) 1)
  8533. (cons 'or orlist) (car orlist))))
  8534. ;; Return the string and lisp forms of the matcher
  8535. (setq matcher (if todomatcher
  8536. (list 'and tagsmatcher todomatcher)
  8537. tagsmatcher))
  8538. (cons match0 matcher)))
  8539. (defun org-op-to-function (op &optional stringp)
  8540. (setq op
  8541. (cond
  8542. ((equal op "<" ) '(< string< ))
  8543. ((equal op ">" ) '(> org-string> ))
  8544. ((member op '("<=" "=<")) '(<= org-string<= ))
  8545. ((member op '(">=" "=>")) '(>= org-string>= ))
  8546. ((member op '("=" "==")) '(= string= ))
  8547. ((member op '("<>" "!=")) '(org<> org-string<> ))))
  8548. (nth (if stringp 1 0) op))
  8549. (defun org<> (a b) (not (= a b)))
  8550. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8551. (defun org-string>= (a b) (not (string< a b)))
  8552. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8553. (defun org-string<> (a b) (not (string= a b)))
  8554. (defun org-match-any-p (re list)
  8555. "Does re match any element of list?"
  8556. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8557. (delq nil list))
  8558. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8559. (defvar org-tags-overlay (org-make-overlay 1 1))
  8560. (org-detach-overlay org-tags-overlay)
  8561. (defun org-get-tags-at (&optional pos)
  8562. "Get a list of all headline tags applicable at POS.
  8563. POS defaults to point. If tags are inherited, the list contains
  8564. the targets in the same sequence as the headlines appear, i.e.
  8565. sthe tags of the current headline come last."
  8566. (interactive)
  8567. (let (tags ltags lastpos parent)
  8568. (save-excursion
  8569. (save-restriction
  8570. (widen)
  8571. (goto-char (or pos (point)))
  8572. (save-match-data
  8573. (condition-case nil
  8574. (progn
  8575. (org-back-to-heading t)
  8576. (while (not (equal lastpos (point)))
  8577. (setq lastpos (point))
  8578. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8579. (setq ltags (org-split-string
  8580. (org-match-string-no-properties 1) ":"))
  8581. (setq tags (append (org-remove-uniherited-tags ltags)
  8582. tags)))
  8583. (or org-use-tag-inheritance (error ""))
  8584. (org-up-heading-all 1)
  8585. (setq parent t)))
  8586. (error nil))))
  8587. tags)))
  8588. (defun org-toggle-tag (tag &optional onoff)
  8589. "Toggle the tag TAG for the current line.
  8590. If ONOFF is `on' or `off', don't toggle but set to this state."
  8591. (unless (org-on-heading-p t) (error "Not on headling"))
  8592. (let (res current)
  8593. (save-excursion
  8594. (beginning-of-line)
  8595. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8596. (point-at-eol) t)
  8597. (progn
  8598. (setq current (match-string 1))
  8599. (replace-match ""))
  8600. (setq current ""))
  8601. (setq current (nreverse (org-split-string current ":")))
  8602. (cond
  8603. ((eq onoff 'on)
  8604. (setq res t)
  8605. (or (member tag current) (push tag current)))
  8606. ((eq onoff 'off)
  8607. (or (not (member tag current)) (setq current (delete tag current))))
  8608. (t (if (member tag current)
  8609. (setq current (delete tag current))
  8610. (setq res t)
  8611. (push tag current))))
  8612. (end-of-line 1)
  8613. (if current
  8614. (progn
  8615. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8616. (org-set-tags nil t))
  8617. (delete-horizontal-space))
  8618. (run-hooks 'org-after-tags-change-hook))
  8619. res))
  8620. (defun org-align-tags-here (to-col)
  8621. ;; Assumes that this is a headline
  8622. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8623. (beginning-of-line 1)
  8624. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8625. (< pos (match-beginning 2)))
  8626. (progn
  8627. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8628. (goto-char (match-beginning 1))
  8629. (insert " ")
  8630. (delete-region (point) (1+ (match-beginning 2)))
  8631. (setq ncol (max (1+ (current-column))
  8632. (1+ col)
  8633. (if (> to-col 0)
  8634. to-col
  8635. (- (abs to-col) tags-l))))
  8636. (setq p (point))
  8637. (insert (make-string (- ncol (current-column)) ?\ ))
  8638. (setq ncol (current-column))
  8639. (tabify p (point-at-eol))
  8640. (org-move-to-column (min ncol col) t))
  8641. (goto-char pos))))
  8642. (defun org-set-tags (&optional arg just-align)
  8643. "Set the tags for the current headline.
  8644. With prefix ARG, realign all tags in headings in the current buffer."
  8645. (interactive "P")
  8646. (let* ((re (concat "^" outline-regexp))
  8647. (current (org-get-tags-string))
  8648. (col (current-column))
  8649. (org-setting-tags t)
  8650. table current-tags inherited-tags ; computed below when needed
  8651. tags p0 c0 c1 rpl)
  8652. (if arg
  8653. (save-excursion
  8654. (goto-char (point-min))
  8655. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8656. (while (re-search-forward re nil t)
  8657. (org-set-tags nil t)
  8658. (end-of-line 1)))
  8659. (message "All tags realigned to column %d" org-tags-column))
  8660. (if just-align
  8661. (setq tags current)
  8662. ;; Get a new set of tags from the user
  8663. (save-excursion
  8664. (setq table (or org-tag-alist (org-get-buffer-tags))
  8665. org-last-tags-completion-table table
  8666. current-tags (org-split-string current ":")
  8667. inherited-tags (nreverse
  8668. (nthcdr (length current-tags)
  8669. (nreverse (org-get-tags-at))))
  8670. tags
  8671. (if (or (eq t org-use-fast-tag-selection)
  8672. (and org-use-fast-tag-selection
  8673. (delq nil (mapcar 'cdr table))))
  8674. (org-fast-tag-selection
  8675. current-tags inherited-tags table
  8676. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8677. (let ((org-add-colon-after-tag-completion t))
  8678. (org-trim
  8679. (org-without-partial-completion
  8680. (completing-read "Tags: " 'org-tags-completion-function
  8681. nil nil current 'org-tags-history)))))))
  8682. (while (string-match "[-+&]+" tags)
  8683. ;; No boolean logic, just a list
  8684. (setq tags (replace-match ":" t t tags))))
  8685. (if (string-match "\\`[\t ]*\\'" tags)
  8686. (setq tags "")
  8687. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8688. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8689. ;; Insert new tags at the correct column
  8690. (beginning-of-line 1)
  8691. (cond
  8692. ((and (equal current "") (equal tags "")))
  8693. ((re-search-forward
  8694. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8695. (point-at-eol) t)
  8696. (if (equal tags "")
  8697. (setq rpl "")
  8698. (goto-char (match-beginning 0))
  8699. (setq c0 (current-column) p0 (point)
  8700. c1 (max (1+ c0) (if (> org-tags-column 0)
  8701. org-tags-column
  8702. (- (- org-tags-column) (length tags))))
  8703. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8704. (replace-match rpl t t)
  8705. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8706. tags)
  8707. (t (error "Tags alignment failed")))
  8708. (org-move-to-column col)
  8709. (unless just-align
  8710. (run-hooks 'org-after-tags-change-hook)))))
  8711. (defun org-change-tag-in-region (beg end tag off)
  8712. "Add or remove TAG for each entry in the region.
  8713. This works in the agenda, and also in an org-mode buffer."
  8714. (interactive
  8715. (list (region-beginning) (region-end)
  8716. (let ((org-last-tags-completion-table
  8717. (if (org-mode-p)
  8718. (org-get-buffer-tags)
  8719. (org-global-tags-completion-table))))
  8720. (completing-read
  8721. "Tag: " 'org-tags-completion-function nil nil nil
  8722. 'org-tags-history))
  8723. (progn
  8724. (message "[s]et or [r]emove? ")
  8725. (equal (read-char-exclusive) ?r))))
  8726. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8727. (let ((agendap (equal major-mode 'org-agenda-mode))
  8728. l1 l2 m buf pos newhead (cnt 0))
  8729. (goto-char end)
  8730. (setq l2 (1- (org-current-line)))
  8731. (goto-char beg)
  8732. (setq l1 (org-current-line))
  8733. (loop for l from l1 to l2 do
  8734. (goto-line l)
  8735. (setq m (get-text-property (point) 'org-hd-marker))
  8736. (when (or (and (org-mode-p) (org-on-heading-p))
  8737. (and agendap m))
  8738. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8739. pos (if agendap m (point)))
  8740. (with-current-buffer buf
  8741. (save-excursion
  8742. (save-restriction
  8743. (goto-char pos)
  8744. (setq cnt (1+ cnt))
  8745. (org-toggle-tag tag (if off 'off 'on))
  8746. (setq newhead (org-get-heading)))))
  8747. (and agendap (org-agenda-change-all-lines newhead m))))
  8748. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8749. (defun org-tags-completion-function (string predicate &optional flag)
  8750. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8751. (confirm (lambda (x) (stringp (car x)))))
  8752. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8753. (setq s1 (match-string 1 string)
  8754. s2 (match-string 2 string))
  8755. (setq s1 "" s2 string))
  8756. (cond
  8757. ((eq flag nil)
  8758. ;; try completion
  8759. (setq rtn (try-completion s2 ctable confirm))
  8760. (if (stringp rtn)
  8761. (setq rtn
  8762. (concat s1 s2 (substring rtn (length s2))
  8763. (if (and org-add-colon-after-tag-completion
  8764. (assoc rtn ctable))
  8765. ":" ""))))
  8766. rtn)
  8767. ((eq flag t)
  8768. ;; all-completions
  8769. (all-completions s2 ctable confirm)
  8770. )
  8771. ((eq flag 'lambda)
  8772. ;; exact match?
  8773. (assoc s2 ctable)))
  8774. ))
  8775. (defun org-fast-tag-insert (kwd tags face &optional end)
  8776. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8777. (insert (format "%-12s" (concat kwd ":"))
  8778. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8779. (or end "")))
  8780. (defun org-fast-tag-show-exit (flag)
  8781. (save-excursion
  8782. (goto-line 3)
  8783. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8784. (replace-match ""))
  8785. (when flag
  8786. (end-of-line 1)
  8787. (org-move-to-column (- (window-width) 19) t)
  8788. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8789. (defun org-set-current-tags-overlay (current prefix)
  8790. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8791. (if (featurep 'xemacs)
  8792. (org-overlay-display org-tags-overlay (concat prefix s)
  8793. 'secondary-selection)
  8794. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8795. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8796. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8797. "Fast tag selection with single keys.
  8798. CURRENT is the current list of tags in the headline, INHERITED is the
  8799. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8800. possibly with grouping information. TODO-TABLE is a similar table with
  8801. TODO keywords, should these have keys assigned to them.
  8802. If the keys are nil, a-z are automatically assigned.
  8803. Returns the new tags string, or nil to not change the current settings."
  8804. (let* ((fulltable (append table todo-table))
  8805. (maxlen (apply 'max (mapcar
  8806. (lambda (x)
  8807. (if (stringp (car x)) (string-width (car x)) 0))
  8808. fulltable)))
  8809. (buf (current-buffer))
  8810. (expert (eq org-fast-tag-selection-single-key 'expert))
  8811. (buffer-tags nil)
  8812. (fwidth (+ maxlen 3 1 3))
  8813. (ncol (/ (- (window-width) 4) fwidth))
  8814. (i-face 'org-done)
  8815. (c-face 'org-todo)
  8816. tg cnt e c char c1 c2 ntable tbl rtn
  8817. ov-start ov-end ov-prefix
  8818. (exit-after-next org-fast-tag-selection-single-key)
  8819. (done-keywords org-done-keywords)
  8820. groups ingroup)
  8821. (save-excursion
  8822. (beginning-of-line 1)
  8823. (if (looking-at
  8824. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8825. (setq ov-start (match-beginning 1)
  8826. ov-end (match-end 1)
  8827. ov-prefix "")
  8828. (setq ov-start (1- (point-at-eol))
  8829. ov-end (1+ ov-start))
  8830. (skip-chars-forward "^\n\r")
  8831. (setq ov-prefix
  8832. (concat
  8833. (buffer-substring (1- (point)) (point))
  8834. (if (> (current-column) org-tags-column)
  8835. " "
  8836. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8837. (org-move-overlay org-tags-overlay ov-start ov-end)
  8838. (save-window-excursion
  8839. (if expert
  8840. (set-buffer (get-buffer-create " *Org tags*"))
  8841. (delete-other-windows)
  8842. (split-window-vertically)
  8843. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8844. (erase-buffer)
  8845. (org-set-local 'org-done-keywords done-keywords)
  8846. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8847. (org-fast-tag-insert "Current" current c-face "\n\n")
  8848. (org-fast-tag-show-exit exit-after-next)
  8849. (org-set-current-tags-overlay current ov-prefix)
  8850. (setq tbl fulltable char ?a cnt 0)
  8851. (while (setq e (pop tbl))
  8852. (cond
  8853. ((equal e '(:startgroup))
  8854. (push '() groups) (setq ingroup t)
  8855. (when (not (= cnt 0))
  8856. (setq cnt 0)
  8857. (insert "\n"))
  8858. (insert "{ "))
  8859. ((equal e '(:endgroup))
  8860. (setq ingroup nil cnt 0)
  8861. (insert "}\n"))
  8862. (t
  8863. (setq tg (car e) c2 nil)
  8864. (if (cdr e)
  8865. (setq c (cdr e))
  8866. ;; automatically assign a character.
  8867. (setq c1 (string-to-char
  8868. (downcase (substring
  8869. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8870. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8871. (while (or (rassoc char ntable) (rassoc char table))
  8872. (setq char (1+ char)))
  8873. (setq c2 c1))
  8874. (setq c (or c2 char)))
  8875. (if ingroup (push tg (car groups)))
  8876. (setq tg (org-add-props tg nil 'face
  8877. (cond
  8878. ((not (assoc tg table))
  8879. (org-get-todo-face tg))
  8880. ((member tg current) c-face)
  8881. ((member tg inherited) i-face)
  8882. (t nil))))
  8883. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8884. (insert "[" c "] " tg (make-string
  8885. (- fwidth 4 (length tg)) ?\ ))
  8886. (push (cons tg c) ntable)
  8887. (when (= (setq cnt (1+ cnt)) ncol)
  8888. (insert "\n")
  8889. (if ingroup (insert " "))
  8890. (setq cnt 0)))))
  8891. (setq ntable (nreverse ntable))
  8892. (insert "\n")
  8893. (goto-char (point-min))
  8894. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8895. (fit-window-to-buffer))
  8896. (setq rtn
  8897. (catch 'exit
  8898. (while t
  8899. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8900. (if groups " [!] no groups" " [!]groups")
  8901. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8902. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8903. (cond
  8904. ((= c ?\r) (throw 'exit t))
  8905. ((= c ?!)
  8906. (setq groups (not groups))
  8907. (goto-char (point-min))
  8908. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8909. ((= c ?\C-c)
  8910. (if (not expert)
  8911. (org-fast-tag-show-exit
  8912. (setq exit-after-next (not exit-after-next)))
  8913. (setq expert nil)
  8914. (delete-other-windows)
  8915. (split-window-vertically)
  8916. (org-switch-to-buffer-other-window " *Org tags*")
  8917. (and (fboundp 'fit-window-to-buffer)
  8918. (fit-window-to-buffer))))
  8919. ((or (= c ?\C-g)
  8920. (and (= c ?q) (not (rassoc c ntable))))
  8921. (org-detach-overlay org-tags-overlay)
  8922. (setq quit-flag t))
  8923. ((= c ?\ )
  8924. (setq current nil)
  8925. (if exit-after-next (setq exit-after-next 'now)))
  8926. ((= c ?\t)
  8927. (condition-case nil
  8928. (setq tg (completing-read
  8929. "Tag: "
  8930. (or buffer-tags
  8931. (with-current-buffer buf
  8932. (org-get-buffer-tags)))))
  8933. (quit (setq tg "")))
  8934. (when (string-match "\\S-" tg)
  8935. (add-to-list 'buffer-tags (list tg))
  8936. (if (member tg current)
  8937. (setq current (delete tg current))
  8938. (push tg current)))
  8939. (if exit-after-next (setq exit-after-next 'now)))
  8940. ((setq e (rassoc c todo-table) tg (car e))
  8941. (with-current-buffer buf
  8942. (save-excursion (org-todo tg)))
  8943. (if exit-after-next (setq exit-after-next 'now)))
  8944. ((setq e (rassoc c ntable) tg (car e))
  8945. (if (member tg current)
  8946. (setq current (delete tg current))
  8947. (loop for g in groups do
  8948. (if (member tg g)
  8949. (mapc (lambda (x)
  8950. (setq current (delete x current)))
  8951. g)))
  8952. (push tg current))
  8953. (if exit-after-next (setq exit-after-next 'now))))
  8954. ;; Create a sorted list
  8955. (setq current
  8956. (sort current
  8957. (lambda (a b)
  8958. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8959. (if (eq exit-after-next 'now) (throw 'exit t))
  8960. (goto-char (point-min))
  8961. (beginning-of-line 2)
  8962. (delete-region (point) (point-at-eol))
  8963. (org-fast-tag-insert "Current" current c-face)
  8964. (org-set-current-tags-overlay current ov-prefix)
  8965. (while (re-search-forward
  8966. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8967. (setq tg (match-string 1))
  8968. (add-text-properties
  8969. (match-beginning 1) (match-end 1)
  8970. (list 'face
  8971. (cond
  8972. ((member tg current) c-face)
  8973. ((member tg inherited) i-face)
  8974. (t (get-text-property (match-beginning 1) 'face))))))
  8975. (goto-char (point-min)))))
  8976. (org-detach-overlay org-tags-overlay)
  8977. (if rtn
  8978. (mapconcat 'identity current ":")
  8979. nil))))
  8980. (defun org-get-tags-string ()
  8981. "Get the TAGS string in the current headline."
  8982. (unless (org-on-heading-p t)
  8983. (error "Not on a heading"))
  8984. (save-excursion
  8985. (beginning-of-line 1)
  8986. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8987. (org-match-string-no-properties 1)
  8988. "")))
  8989. (defun org-get-tags ()
  8990. "Get the list of tags specified in the current headline."
  8991. (org-split-string (org-get-tags-string) ":"))
  8992. (defun org-get-buffer-tags ()
  8993. "Get a table of all tags used in the buffer, for completion."
  8994. (let (tags)
  8995. (save-excursion
  8996. (goto-char (point-min))
  8997. (while (re-search-forward
  8998. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8999. (when (equal (char-after (point-at-bol 0)) ?*)
  9000. (mapc (lambda (x) (add-to-list 'tags x))
  9001. (org-split-string (org-match-string-no-properties 1) ":")))))
  9002. (mapcar 'list tags)))
  9003. ;;;; Properties
  9004. ;;; Setting and retrieving properties
  9005. (defconst org-special-properties
  9006. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  9007. "TIMESTAMP" "TIMESTAMP_IA")
  9008. "The special properties valid in Org-mode.
  9009. These are properties that are not defined in the property drawer,
  9010. but in some other way.")
  9011. (defconst org-default-properties
  9012. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9013. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9014. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9015. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9016. "Some properties that are used by Org-mode for various purposes.
  9017. Being in this list makes sure that they are offered for completion.")
  9018. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9019. "Regular expression matching the first line of a property drawer.")
  9020. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9021. "Regular expression matching the first line of a property drawer.")
  9022. (defun org-property-action ()
  9023. "Do an action on properties."
  9024. (interactive)
  9025. (let (c)
  9026. (org-at-property-p)
  9027. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9028. (setq c (read-char-exclusive))
  9029. (cond
  9030. ((equal c ?s)
  9031. (call-interactively 'org-set-property))
  9032. ((equal c ?d)
  9033. (call-interactively 'org-delete-property))
  9034. ((equal c ?D)
  9035. (call-interactively 'org-delete-property-globally))
  9036. ((equal c ?c)
  9037. (call-interactively 'org-compute-property-at-point))
  9038. (t (error "No such property action %c" c)))))
  9039. (defun org-at-property-p ()
  9040. "Is the cursor in a property line?"
  9041. ;; FIXME: Does not check if we are actually in the drawer.
  9042. ;; FIXME: also returns true on any drawers.....
  9043. ;; This is used by C-c C-c for property action.
  9044. (save-excursion
  9045. (beginning-of-line 1)
  9046. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9047. (defun org-get-property-block (&optional beg end force)
  9048. "Return the (beg . end) range of the body of the property drawer.
  9049. BEG and END can be beginning and end of subtree, if not given
  9050. they will be found.
  9051. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9052. (catch 'exit
  9053. (save-excursion
  9054. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9055. (end (or end (progn (outline-next-heading) (point)))))
  9056. (goto-char beg)
  9057. (if (re-search-forward org-property-start-re end t)
  9058. (setq beg (1+ (match-end 0)))
  9059. (if force
  9060. (save-excursion
  9061. (org-insert-property-drawer)
  9062. (setq end (progn (outline-next-heading) (point))))
  9063. (throw 'exit nil))
  9064. (goto-char beg)
  9065. (if (re-search-forward org-property-start-re end t)
  9066. (setq beg (1+ (match-end 0)))))
  9067. (if (re-search-forward org-property-end-re end t)
  9068. (setq end (match-beginning 0))
  9069. (or force (throw 'exit nil))
  9070. (goto-char beg)
  9071. (setq end beg)
  9072. (org-indent-line-function)
  9073. (insert ":END:\n"))
  9074. (cons beg end)))))
  9075. (defun org-entry-properties (&optional pom which)
  9076. "Get all properties of the entry at point-or-marker POM.
  9077. This includes the TODO keyword, the tags, time strings for deadline,
  9078. scheduled, and clocking, and any additional properties defined in the
  9079. entry. The return value is an alist, keys may occur multiple times
  9080. if the property key was used several times.
  9081. POM may also be nil, in which case the current entry is used.
  9082. If WHICH is nil or `all', get all properties. If WHICH is
  9083. `special' or `standard', only get that subclass."
  9084. (setq which (or which 'all))
  9085. (org-with-point-at pom
  9086. (let ((clockstr (substring org-clock-string 0 -1))
  9087. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9088. beg end range props sum-props key value string clocksum)
  9089. (save-excursion
  9090. (when (condition-case nil (org-back-to-heading t) (error nil))
  9091. (setq beg (point))
  9092. (setq sum-props (get-text-property (point) 'org-summaries))
  9093. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9094. (outline-next-heading)
  9095. (setq end (point))
  9096. (when (memq which '(all special))
  9097. ;; Get the special properties, like TODO and tags
  9098. (goto-char beg)
  9099. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9100. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9101. (when (looking-at org-priority-regexp)
  9102. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9103. (when (and (setq value (org-get-tags-string))
  9104. (string-match "\\S-" value))
  9105. (push (cons "TAGS" value) props))
  9106. (when (setq value (org-get-tags-at))
  9107. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9108. props))
  9109. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9110. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9111. string (if (equal key clockstr)
  9112. (org-no-properties
  9113. (org-trim
  9114. (buffer-substring
  9115. (match-beginning 3) (goto-char (point-at-eol)))))
  9116. (substring (org-match-string-no-properties 3) 1 -1)))
  9117. (unless key
  9118. (if (= (char-after (match-beginning 3)) ?\[)
  9119. (setq key "TIMESTAMP_IA")
  9120. (setq key "TIMESTAMP")))
  9121. (when (or (equal key clockstr) (not (assoc key props)))
  9122. (push (cons key string) props)))
  9123. )
  9124. (when (memq which '(all standard))
  9125. ;; Get the standard properties, like :PORP: ...
  9126. (setq range (org-get-property-block beg end))
  9127. (when range
  9128. (goto-char (car range))
  9129. (while (re-search-forward
  9130. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9131. (cdr range) t)
  9132. (setq key (org-match-string-no-properties 1)
  9133. value (org-trim (or (org-match-string-no-properties 2) "")))
  9134. (unless (member key excluded)
  9135. (push (cons key (or value "")) props)))))
  9136. (if clocksum
  9137. (push (cons "CLOCKSUM"
  9138. (org-columns-number-to-string (/ (float clocksum) 60.)
  9139. 'add_times))
  9140. props))
  9141. (append sum-props (nreverse props)))))))
  9142. (defun org-entry-get (pom property &optional inherit)
  9143. "Get value of PROPERTY for entry at point-or-marker POM.
  9144. If INHERIT is non-nil and the entry does not have the property,
  9145. then also check higher levels of the hierarchy.
  9146. If INHERIT is the symbol `selective', use inheritance only if the setting
  9147. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9148. If the property is present but empty, the return value is the empty string.
  9149. If the property is not present at all, nil is returned."
  9150. (org-with-point-at pom
  9151. (if (and inherit (if (eq inherit 'selective)
  9152. (org-property-inherit-p property)
  9153. t))
  9154. (org-entry-get-with-inheritance property)
  9155. (if (member property org-special-properties)
  9156. ;; We need a special property. Use brute force, get all properties.
  9157. (cdr (assoc property (org-entry-properties nil 'special)))
  9158. (let ((range (org-get-property-block)))
  9159. (if (and range
  9160. (goto-char (car range))
  9161. (re-search-forward
  9162. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9163. (cdr range) t))
  9164. ;; Found the property, return it.
  9165. (if (match-end 1)
  9166. (org-match-string-no-properties 1)
  9167. "")))))))
  9168. (defun org-property-or-variable-value (var &optional inherit)
  9169. "Check if there is a property fixing the value of VAR.
  9170. If yes, return this value. If not, return the current value of the variable."
  9171. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9172. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9173. (read prop)
  9174. (symbol-value var))))
  9175. (defun org-entry-delete (pom property)
  9176. "Delete the property PROPERTY from entry at point-or-marker POM."
  9177. (org-with-point-at pom
  9178. (if (member property org-special-properties)
  9179. nil ; cannot delete these properties.
  9180. (let ((range (org-get-property-block)))
  9181. (if (and range
  9182. (goto-char (car range))
  9183. (re-search-forward
  9184. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9185. (cdr range) t))
  9186. (progn
  9187. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9188. t)
  9189. nil)))))
  9190. ;; Multi-values properties are properties that contain multiple values
  9191. ;; These values are assumed to be single words, separated by whitespace.
  9192. (defun org-entry-add-to-multivalued-property (pom property value)
  9193. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9194. (let* ((old (org-entry-get pom property))
  9195. (values (and old (org-split-string old "[ \t]"))))
  9196. (unless (member value values)
  9197. (setq values (cons value values))
  9198. (org-entry-put pom property
  9199. (mapconcat 'identity values " ")))))
  9200. (defun org-entry-remove-from-multivalued-property (pom property value)
  9201. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9202. (let* ((old (org-entry-get pom property))
  9203. (values (and old (org-split-string old "[ \t]"))))
  9204. (when (member value values)
  9205. (setq values (delete value values))
  9206. (org-entry-put pom property
  9207. (mapconcat 'identity values " ")))))
  9208. (defun org-entry-member-in-multivalued-property (pom property value)
  9209. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9210. (let* ((old (org-entry-get pom property))
  9211. (values (and old (org-split-string old "[ \t]"))))
  9212. (member value values)))
  9213. (defvar org-entry-property-inherited-from (make-marker))
  9214. (defun org-entry-get-with-inheritance (property)
  9215. "Get entry property, and search higher levels if not present."
  9216. (let (tmp)
  9217. (save-excursion
  9218. (save-restriction
  9219. (widen)
  9220. (catch 'ex
  9221. (while t
  9222. (when (setq tmp (org-entry-get nil property))
  9223. (org-back-to-heading t)
  9224. (move-marker org-entry-property-inherited-from (point))
  9225. (throw 'ex tmp))
  9226. (or (org-up-heading-safe) (throw 'ex nil)))))
  9227. (or tmp
  9228. (cdr (assoc property org-file-properties))
  9229. (cdr (assoc property org-global-properties))
  9230. (cdr (assoc property org-global-properties-fixed))))))
  9231. (defun org-entry-put (pom property value)
  9232. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9233. (org-with-point-at pom
  9234. (org-back-to-heading t)
  9235. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9236. range)
  9237. (cond
  9238. ((equal property "TODO")
  9239. (when (and (stringp value) (string-match "\\S-" value)
  9240. (not (member value org-todo-keywords-1)))
  9241. (error "\"%s\" is not a valid TODO state" value))
  9242. (if (or (not value)
  9243. (not (string-match "\\S-" value)))
  9244. (setq value 'none))
  9245. (org-todo value)
  9246. (org-set-tags nil 'align))
  9247. ((equal property "PRIORITY")
  9248. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9249. (string-to-char value) ?\ ))
  9250. (org-set-tags nil 'align))
  9251. ((equal property "SCHEDULED")
  9252. (if (re-search-forward org-scheduled-time-regexp end t)
  9253. (cond
  9254. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9255. ((eq value 'later) (org-timestamp-change 1 'day))
  9256. (t (call-interactively 'org-schedule)))
  9257. (call-interactively 'org-schedule)))
  9258. ((equal property "DEADLINE")
  9259. (if (re-search-forward org-deadline-time-regexp end t)
  9260. (cond
  9261. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9262. ((eq value 'later) (org-timestamp-change 1 'day))
  9263. (t (call-interactively 'org-deadline)))
  9264. (call-interactively 'org-deadline)))
  9265. ((member property org-special-properties)
  9266. (error "The %s property can not yet be set with `org-entry-put'"
  9267. property))
  9268. (t ; a non-special property
  9269. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9270. (setq range (org-get-property-block beg end 'force))
  9271. (goto-char (car range))
  9272. (if (re-search-forward
  9273. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9274. (progn
  9275. (delete-region (match-beginning 1) (match-end 1))
  9276. (goto-char (match-beginning 1)))
  9277. (goto-char (cdr range))
  9278. (insert "\n")
  9279. (backward-char 1)
  9280. (org-indent-line-function)
  9281. (insert ":" property ":"))
  9282. (and value (insert " " value))
  9283. (org-indent-line-function)))))))
  9284. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9285. "Get all property keys in the current buffer.
  9286. With INCLUDE-SPECIALS, also list the special properties that relect things
  9287. like tags and TODO state.
  9288. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9289. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9290. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9291. formats in the current buffer."
  9292. (let (rtn range cfmt cols s p)
  9293. (save-excursion
  9294. (save-restriction
  9295. (widen)
  9296. (goto-char (point-min))
  9297. (while (re-search-forward org-property-start-re nil t)
  9298. (setq range (org-get-property-block))
  9299. (goto-char (car range))
  9300. (while (re-search-forward
  9301. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9302. (cdr range) t)
  9303. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9304. (outline-next-heading))))
  9305. (when include-specials
  9306. (setq rtn (append org-special-properties rtn)))
  9307. (when include-defaults
  9308. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9309. (when include-columns
  9310. (save-excursion
  9311. (save-restriction
  9312. (widen)
  9313. (goto-char (point-min))
  9314. (while (re-search-forward
  9315. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9316. nil t)
  9317. (setq cfmt (match-string 2) s 0)
  9318. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9319. cfmt s)
  9320. (setq s (match-end 0)
  9321. p (match-string 1 cfmt))
  9322. (unless (or (equal p "ITEM")
  9323. (member p org-special-properties))
  9324. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9325. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9326. (defun org-property-values (key)
  9327. "Return a list of all values of property KEY."
  9328. (save-excursion
  9329. (save-restriction
  9330. (widen)
  9331. (goto-char (point-min))
  9332. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9333. values)
  9334. (while (re-search-forward re nil t)
  9335. (add-to-list 'values (org-trim (match-string 1))))
  9336. (delete "" values)))))
  9337. (defun org-insert-property-drawer ()
  9338. "Insert a property drawer into the current entry."
  9339. (interactive)
  9340. (org-back-to-heading t)
  9341. (looking-at outline-regexp)
  9342. (let ((indent (- (match-end 0)(match-beginning 0)))
  9343. (beg (point))
  9344. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9345. end hiddenp)
  9346. (outline-next-heading)
  9347. (setq end (point))
  9348. (goto-char beg)
  9349. (while (re-search-forward re end t))
  9350. (setq hiddenp (org-invisible-p))
  9351. (end-of-line 1)
  9352. (and (equal (char-after) ?\n) (forward-char 1))
  9353. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9354. (beginning-of-line 2))
  9355. (org-skip-over-state-notes)
  9356. (skip-chars-backward " \t\n\r")
  9357. (if (eq (char-before) ?*) (forward-char 1))
  9358. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9359. (beginning-of-line 0)
  9360. (org-indent-to-column indent)
  9361. (beginning-of-line 2)
  9362. (org-indent-to-column indent)
  9363. (beginning-of-line 0)
  9364. (if hiddenp
  9365. (save-excursion
  9366. (org-back-to-heading t)
  9367. (hide-entry))
  9368. (org-flag-drawer t))))
  9369. (defun org-set-property (property value)
  9370. "In the current entry, set PROPERTY to VALUE.
  9371. When called interactively, this will prompt for a property name, offering
  9372. completion on existing and default properties. And then it will prompt
  9373. for a value, offering competion either on allowed values (via an inherited
  9374. xxx_ALL property) or on existing values in other instances of this property
  9375. in the current file."
  9376. (interactive
  9377. (let* ((completion-ignore-case t)
  9378. (keys (org-buffer-property-keys nil t t))
  9379. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9380. (prop (if (member prop0 keys)
  9381. prop0
  9382. (or (cdr (assoc (downcase prop0)
  9383. (mapcar (lambda (x) (cons (downcase x) x))
  9384. keys)))
  9385. prop0)))
  9386. (cur (org-entry-get nil prop))
  9387. (allowed (org-property-get-allowed-values nil prop 'table))
  9388. (existing (mapcar 'list (org-property-values prop)))
  9389. (val (if allowed
  9390. (org-completing-read "Value: " allowed nil 'req-match)
  9391. (org-completing-read
  9392. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9393. (concat "[" cur "]") "")
  9394. ": ")
  9395. existing nil nil "" nil cur))))
  9396. (list prop (if (equal val "") cur val))))
  9397. (unless (equal (org-entry-get nil property) value)
  9398. (org-entry-put nil property value)))
  9399. (defun org-delete-property (property)
  9400. "In the current entry, delete PROPERTY."
  9401. (interactive
  9402. (let* ((completion-ignore-case t)
  9403. (prop (completing-read
  9404. "Property: " (org-entry-properties nil 'standard))))
  9405. (list prop)))
  9406. (message "Property %s %s" property
  9407. (if (org-entry-delete nil property)
  9408. "deleted"
  9409. "was not present in the entry")))
  9410. (defun org-delete-property-globally (property)
  9411. "Remove PROPERTY globally, from all entries."
  9412. (interactive
  9413. (let* ((completion-ignore-case t)
  9414. (prop (completing-read
  9415. "Globally remove property: "
  9416. (mapcar 'list (org-buffer-property-keys)))))
  9417. (list prop)))
  9418. (save-excursion
  9419. (save-restriction
  9420. (widen)
  9421. (goto-char (point-min))
  9422. (let ((cnt 0))
  9423. (while (re-search-forward
  9424. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9425. nil t)
  9426. (setq cnt (1+ cnt))
  9427. (replace-match ""))
  9428. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9429. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9430. (defun org-compute-property-at-point ()
  9431. "Compute the property at point.
  9432. This looks for an enclosing column format, extracts the operator and
  9433. then applies it to the proerty in the column format's scope."
  9434. (interactive)
  9435. (unless (org-at-property-p)
  9436. (error "Not at a property"))
  9437. (let ((prop (org-match-string-no-properties 2)))
  9438. (org-columns-get-format-and-top-level)
  9439. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9440. (error "No operator defined for property %s" prop))
  9441. (org-columns-compute prop)))
  9442. (defun org-property-get-allowed-values (pom property &optional table)
  9443. "Get allowed values for the property PROPERTY.
  9444. When TABLE is non-nil, return an alist that can directly be used for
  9445. completion."
  9446. (let (vals)
  9447. (cond
  9448. ((equal property "TODO")
  9449. (setq vals (org-with-point-at pom
  9450. (append org-todo-keywords-1 '("")))))
  9451. ((equal property "PRIORITY")
  9452. (let ((n org-lowest-priority))
  9453. (while (>= n org-highest-priority)
  9454. (push (char-to-string n) vals)
  9455. (setq n (1- n)))))
  9456. ((member property org-special-properties))
  9457. (t
  9458. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9459. (when (and vals (string-match "\\S-" vals))
  9460. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9461. (setq vals (mapcar (lambda (x)
  9462. (cond ((stringp x) x)
  9463. ((numberp x) (number-to-string x))
  9464. ((symbolp x) (symbol-name x))
  9465. (t "???")))
  9466. vals)))))
  9467. (if table (mapcar 'list vals) vals)))
  9468. (defun org-property-previous-allowed-value (&optional previous)
  9469. "Switch to the next allowed value for this property."
  9470. (interactive)
  9471. (org-property-next-allowed-value t))
  9472. (defun org-property-next-allowed-value (&optional previous)
  9473. "Switch to the next allowed value for this property."
  9474. (interactive)
  9475. (unless (org-at-property-p)
  9476. (error "Not at a property"))
  9477. (let* ((key (match-string 2))
  9478. (value (match-string 3))
  9479. (allowed (or (org-property-get-allowed-values (point) key)
  9480. (and (member value '("[ ]" "[-]" "[X]"))
  9481. '("[ ]" "[X]"))))
  9482. nval)
  9483. (unless allowed
  9484. (error "Allowed values for this property have not been defined"))
  9485. (if previous (setq allowed (reverse allowed)))
  9486. (if (member value allowed)
  9487. (setq nval (car (cdr (member value allowed)))))
  9488. (setq nval (or nval (car allowed)))
  9489. (if (equal nval value)
  9490. (error "Only one allowed value for this property"))
  9491. (org-at-property-p)
  9492. (replace-match (concat " :" key ": " nval) t t)
  9493. (org-indent-line-function)
  9494. (beginning-of-line 1)
  9495. (skip-chars-forward " \t")))
  9496. (defun org-find-entry-with-id (ident)
  9497. "Locate the entry that contains the ID property with exact value IDENT.
  9498. IDENT can be a string, a symbol or a number, this function will search for
  9499. the string representation of it.
  9500. Return the position where this entry starts, or nil if there is no such entry."
  9501. (let ((id (cond
  9502. ((stringp ident) ident)
  9503. ((symbol-name ident) (symbol-name ident))
  9504. ((numberp ident) (number-to-string ident))
  9505. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9506. (case-fold-search nil))
  9507. (save-excursion
  9508. (save-restriction
  9509. (widen)
  9510. (goto-char (point-min))
  9511. (when (re-search-forward
  9512. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9513. nil t)
  9514. (org-back-to-heading)
  9515. (point))))))
  9516. ;;;; Timestamps
  9517. (defvar org-last-changed-timestamp nil)
  9518. (defvar org-time-was-given) ; dynamically scoped parameter
  9519. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9520. (defvar org-ts-what) ; dynamically scoped parameter
  9521. (defun org-time-stamp (arg)
  9522. "Prompt for a date/time and insert a time stamp.
  9523. If the user specifies a time like HH:MM, or if this command is called
  9524. with a prefix argument, the time stamp will contain date and time.
  9525. Otherwise, only the date will be included. All parts of a date not
  9526. specified by the user will be filled in from the current date/time.
  9527. So if you press just return without typing anything, the time stamp
  9528. will represent the current date/time. If there is already a timestamp
  9529. at the cursor, it will be modified."
  9530. (interactive "P")
  9531. (let* ((ts nil)
  9532. (default-time
  9533. ;; Default time is either today, or, when entering a range,
  9534. ;; the range start.
  9535. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9536. (save-excursion
  9537. (re-search-backward
  9538. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9539. (- (point) 20) t)))
  9540. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9541. (current-time)))
  9542. (default-input (and ts (org-get-compact-tod ts)))
  9543. org-time-was-given org-end-time-was-given time)
  9544. (cond
  9545. ((and (org-at-timestamp-p)
  9546. (eq last-command 'org-time-stamp)
  9547. (eq this-command 'org-time-stamp))
  9548. (insert "--")
  9549. (setq time (let ((this-command this-command))
  9550. (org-read-date arg 'totime nil nil default-time default-input)))
  9551. (org-insert-time-stamp time (or org-time-was-given arg)))
  9552. ((org-at-timestamp-p)
  9553. (setq time (let ((this-command this-command))
  9554. (org-read-date arg 'totime nil nil default-time default-input)))
  9555. (when (org-at-timestamp-p) ; just to get the match data
  9556. (replace-match "")
  9557. (setq org-last-changed-timestamp
  9558. (org-insert-time-stamp
  9559. time (or org-time-was-given arg)
  9560. nil nil nil (list org-end-time-was-given))))
  9561. (message "Timestamp updated"))
  9562. (t
  9563. (setq time (let ((this-command this-command))
  9564. (org-read-date arg 'totime nil nil default-time default-input)))
  9565. (org-insert-time-stamp time (or org-time-was-given arg)
  9566. nil nil nil (list org-end-time-was-given))))))
  9567. ;; FIXME: can we use this for something else, like computing time differences?
  9568. (defun org-get-compact-tod (s)
  9569. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9570. (let* ((t1 (match-string 1 s))
  9571. (h1 (string-to-number (match-string 2 s)))
  9572. (m1 (string-to-number (match-string 3 s)))
  9573. (t2 (and (match-end 4) (match-string 5 s)))
  9574. (h2 (and t2 (string-to-number (match-string 6 s))))
  9575. (m2 (and t2 (string-to-number (match-string 7 s))))
  9576. dh dm)
  9577. (if (not t2)
  9578. t1
  9579. (setq dh (- h2 h1) dm (- m2 m1))
  9580. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9581. (concat t1 "+" (number-to-string dh)
  9582. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9583. (defun org-time-stamp-inactive (&optional arg)
  9584. "Insert an inactive time stamp.
  9585. An inactive time stamp is enclosed in square brackets instead of angle
  9586. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9587. does not link to the calendar and cannot be changed with the S-cursor keys.
  9588. So these are more for recording a certain time/date."
  9589. (interactive "P")
  9590. (let (org-time-was-given org-end-time-was-given time)
  9591. (setq time (org-read-date arg 'totime))
  9592. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9593. nil nil (list org-end-time-was-given))))
  9594. (defvar org-date-ovl (org-make-overlay 1 1))
  9595. (org-overlay-put org-date-ovl 'face 'org-warning)
  9596. (org-detach-overlay org-date-ovl)
  9597. (defvar org-ans1) ; dynamically scoped parameter
  9598. (defvar org-ans2) ; dynamically scoped parameter
  9599. (defvar org-plain-time-of-day-regexp) ; defined below
  9600. (defvar org-read-date-overlay nil)
  9601. (defvar org-dcst nil) ; dynamically scoped
  9602. (defun org-read-date (&optional with-time to-time from-string prompt
  9603. default-time default-input)
  9604. "Read a date, possibly a time, and make things smooth for the user.
  9605. The prompt will suggest to enter an ISO date, but you can also enter anything
  9606. which will at least partially be understood by `parse-time-string'.
  9607. Unrecognized parts of the date will default to the current day, month, year,
  9608. hour and minute. If this command is called to replace a timestamp at point,
  9609. of to enter the second timestamp of a range, the default time is taken from the
  9610. existing stamp. For example,
  9611. 3-2-5 --> 2003-02-05
  9612. feb 15 --> currentyear-02-15
  9613. sep 12 9 --> 2009-09-12
  9614. 12:45 --> today 12:45
  9615. 22 sept 0:34 --> currentyear-09-22 0:34
  9616. 12 --> currentyear-currentmonth-12
  9617. Fri --> nearest Friday (today or later)
  9618. etc.
  9619. Furthermore you can specify a relative date by giving, as the *first* thing
  9620. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9621. change in days weeks, months, years.
  9622. With a single plus or minus, the date is relative to today. With a double
  9623. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9624. +4d --> four days from today
  9625. +4 --> same as above
  9626. +2w --> two weeks from today
  9627. ++5 --> five days from default date
  9628. The function understands only English month and weekday abbreviations,
  9629. but this can be configured with the variables `parse-time-months' and
  9630. `parse-time-weekdays'.
  9631. While prompting, a calendar is popped up - you can also select the
  9632. date with the mouse (button 1). The calendar shows a period of three
  9633. months. To scroll it to other months, use the keys `>' and `<'.
  9634. If you don't like the calendar, turn it off with
  9635. \(setq org-read-date-popup-calendar nil)
  9636. With optional argument TO-TIME, the date will immediately be converted
  9637. to an internal time.
  9638. With an optional argument WITH-TIME, the prompt will suggest to also
  9639. insert a time. Note that when WITH-TIME is not set, you can still
  9640. enter a time, and this function will inform the calling routine about
  9641. this change. The calling routine may then choose to change the format
  9642. used to insert the time stamp into the buffer to include the time.
  9643. With optional argument FROM-STRING, read from this string instead from
  9644. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9645. the time/date that is used for everything that is not specified by the
  9646. user."
  9647. (require 'parse-time)
  9648. (let* ((org-time-stamp-rounding-minutes
  9649. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9650. (org-dcst org-display-custom-times)
  9651. (ct (org-current-time))
  9652. (def (or default-time ct))
  9653. (defdecode (decode-time def))
  9654. (dummy (progn
  9655. (when (< (nth 2 defdecode) org-extend-today-until)
  9656. (setcar (nthcdr 2 defdecode) -1)
  9657. (setcar (nthcdr 1 defdecode) 59)
  9658. (setq def (apply 'encode-time defdecode)
  9659. defdecode (decode-time def)))))
  9660. (calendar-move-hook nil)
  9661. (calendar-view-diary-initially-flag nil)
  9662. (view-diary-entries-initially nil)
  9663. (calendar-view-holidays-initially-flag nil)
  9664. (view-calendar-holidays-initially nil)
  9665. (timestr (format-time-string
  9666. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9667. (prompt (concat (if prompt (concat prompt " ") "")
  9668. (format "Date+time [%s]: " timestr)))
  9669. ans (org-ans0 "") org-ans1 org-ans2 final)
  9670. (cond
  9671. (from-string (setq ans from-string))
  9672. (org-read-date-popup-calendar
  9673. (save-excursion
  9674. (save-window-excursion
  9675. (calendar)
  9676. (calendar-forward-day (- (time-to-days def)
  9677. (calendar-absolute-from-gregorian
  9678. (calendar-current-date))))
  9679. (org-eval-in-calendar nil t)
  9680. (let* ((old-map (current-local-map))
  9681. (map (copy-keymap calendar-mode-map))
  9682. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9683. (org-defkey map (kbd "RET") 'org-calendar-select)
  9684. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9685. 'org-calendar-select-mouse)
  9686. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9687. 'org-calendar-select-mouse)
  9688. (org-defkey minibuffer-local-map [(meta shift left)]
  9689. (lambda () (interactive)
  9690. (org-eval-in-calendar '(calendar-backward-month 1))))
  9691. (org-defkey minibuffer-local-map [(meta shift right)]
  9692. (lambda () (interactive)
  9693. (org-eval-in-calendar '(calendar-forward-month 1))))
  9694. (org-defkey minibuffer-local-map [(meta shift up)]
  9695. (lambda () (interactive)
  9696. (org-eval-in-calendar '(calendar-backward-year 1))))
  9697. (org-defkey minibuffer-local-map [(meta shift down)]
  9698. (lambda () (interactive)
  9699. (org-eval-in-calendar '(calendar-forward-year 1))))
  9700. (org-defkey minibuffer-local-map [(shift up)]
  9701. (lambda () (interactive)
  9702. (org-eval-in-calendar '(calendar-backward-week 1))))
  9703. (org-defkey minibuffer-local-map [(shift down)]
  9704. (lambda () (interactive)
  9705. (org-eval-in-calendar '(calendar-forward-week 1))))
  9706. (org-defkey minibuffer-local-map [(shift left)]
  9707. (lambda () (interactive)
  9708. (org-eval-in-calendar '(calendar-backward-day 1))))
  9709. (org-defkey minibuffer-local-map [(shift right)]
  9710. (lambda () (interactive)
  9711. (org-eval-in-calendar '(calendar-forward-day 1))))
  9712. (org-defkey minibuffer-local-map ">"
  9713. (lambda () (interactive)
  9714. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9715. (org-defkey minibuffer-local-map "<"
  9716. (lambda () (interactive)
  9717. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9718. (unwind-protect
  9719. (progn
  9720. (use-local-map map)
  9721. (add-hook 'post-command-hook 'org-read-date-display)
  9722. (setq org-ans0 (read-string prompt default-input nil nil))
  9723. ;; org-ans0: from prompt
  9724. ;; org-ans1: from mouse click
  9725. ;; org-ans2: from calendar motion
  9726. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9727. (remove-hook 'post-command-hook 'org-read-date-display)
  9728. (use-local-map old-map)
  9729. (when org-read-date-overlay
  9730. (org-delete-overlay org-read-date-overlay)
  9731. (setq org-read-date-overlay nil)))))))
  9732. (t ; Naked prompt only
  9733. (unwind-protect
  9734. (setq ans (read-string prompt default-input nil timestr))
  9735. (when org-read-date-overlay
  9736. (org-delete-overlay org-read-date-overlay)
  9737. (setq org-read-date-overlay nil)))))
  9738. (setq final (org-read-date-analyze ans def defdecode))
  9739. (if to-time
  9740. (apply 'encode-time final)
  9741. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9742. (format "%04d-%02d-%02d %02d:%02d"
  9743. (nth 5 final) (nth 4 final) (nth 3 final)
  9744. (nth 2 final) (nth 1 final))
  9745. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9746. (defvar def)
  9747. (defvar defdecode)
  9748. (defvar with-time)
  9749. (defun org-read-date-display ()
  9750. "Display the currrent date prompt interpretation in the minibuffer."
  9751. (when org-read-date-display-live
  9752. (when org-read-date-overlay
  9753. (org-delete-overlay org-read-date-overlay))
  9754. (let ((p (point)))
  9755. (end-of-line 1)
  9756. (while (not (equal (buffer-substring
  9757. (max (point-min) (- (point) 4)) (point))
  9758. " "))
  9759. (insert " "))
  9760. (goto-char p))
  9761. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9762. " " (or org-ans1 org-ans2)))
  9763. (org-end-time-was-given nil)
  9764. (f (org-read-date-analyze ans def defdecode))
  9765. (fmts (if org-dcst
  9766. org-time-stamp-custom-formats
  9767. org-time-stamp-formats))
  9768. (fmt (if (or with-time
  9769. (and (boundp 'org-time-was-given) org-time-was-given))
  9770. (cdr fmts)
  9771. (car fmts)))
  9772. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9773. (when (and org-end-time-was-given
  9774. (string-match org-plain-time-of-day-regexp txt))
  9775. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9776. org-end-time-was-given
  9777. (substring txt (match-end 0)))))
  9778. (setq org-read-date-overlay
  9779. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  9780. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9781. (defun org-read-date-analyze (ans def defdecode)
  9782. "Analyze the combined answer of the date prompt."
  9783. ;; FIXME: cleanup and comment
  9784. (let (delta deltan deltaw deltadef year month day
  9785. hour minute second wday pm h2 m2 tl wday1
  9786. iso-year iso-weekday iso-week iso-year iso-date)
  9787. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9788. (setq ans "+0"))
  9789. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9790. (setq ans (replace-match "" t t ans)
  9791. deltan (car delta)
  9792. deltaw (nth 1 delta)
  9793. deltadef (nth 2 delta)))
  9794. ;; Check if there is an iso week date in there
  9795. ;; If yes, sore the info and ostpone interpreting it until the rest
  9796. ;; of the parsing is done
  9797. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9798. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9799. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9800. iso-week (string-to-number (match-string 2 ans)))
  9801. (setq ans (replace-match "" t t ans)))
  9802. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9803. (when (string-match
  9804. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9805. (setq year (if (match-end 2)
  9806. (string-to-number (match-string 2 ans))
  9807. (string-to-number (format-time-string "%Y")))
  9808. month (string-to-number (match-string 3 ans))
  9809. day (string-to-number (match-string 4 ans)))
  9810. (if (< year 100) (setq year (+ 2000 year)))
  9811. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9812. t nil ans)))
  9813. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9814. ;; If there is a time with am/pm, and *no* time without it, we convert
  9815. ;; so that matching will be successful.
  9816. (loop for i from 1 to 2 do ; twice, for end time as well
  9817. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9818. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9819. (setq hour (string-to-number (match-string 1 ans))
  9820. minute (if (match-end 3)
  9821. (string-to-number (match-string 3 ans))
  9822. 0)
  9823. pm (equal ?p
  9824. (string-to-char (downcase (match-string 4 ans)))))
  9825. (if (and (= hour 12) (not pm))
  9826. (setq hour 0)
  9827. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9828. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9829. t t ans))))
  9830. ;; Check if a time range is given as a duration
  9831. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9832. (setq hour (string-to-number (match-string 1 ans))
  9833. h2 (+ hour (string-to-number (match-string 3 ans)))
  9834. minute (string-to-number (match-string 2 ans))
  9835. m2 (+ minute (if (match-end 5) (string-to-number
  9836. (match-string 5 ans))0)))
  9837. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9838. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9839. t t ans)))
  9840. ;; Check if there is a time range
  9841. (when (boundp 'org-end-time-was-given)
  9842. (setq org-time-was-given nil)
  9843. (when (and (string-match org-plain-time-of-day-regexp ans)
  9844. (match-end 8))
  9845. (setq org-end-time-was-given (match-string 8 ans))
  9846. (setq ans (concat (substring ans 0 (match-beginning 7))
  9847. (substring ans (match-end 7))))))
  9848. (setq tl (parse-time-string ans)
  9849. day (or (nth 3 tl) (nth 3 defdecode))
  9850. month (or (nth 4 tl)
  9851. (if (and org-read-date-prefer-future
  9852. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9853. (1+ (nth 4 defdecode))
  9854. (nth 4 defdecode)))
  9855. year (or (nth 5 tl)
  9856. (if (and org-read-date-prefer-future
  9857. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9858. (1+ (nth 5 defdecode))
  9859. (nth 5 defdecode)))
  9860. hour (or (nth 2 tl) (nth 2 defdecode))
  9861. minute (or (nth 1 tl) (nth 1 defdecode))
  9862. second (or (nth 0 tl) 0)
  9863. wday (nth 6 tl))
  9864. ;; Special date definitions below
  9865. (cond
  9866. (iso-week
  9867. ;; There was an iso week
  9868. (setq year (or iso-year year)
  9869. day (or iso-weekday wday 1)
  9870. wday nil ; to make sure that the trigger below does not match
  9871. iso-date (calendar-gregorian-from-absolute
  9872. (calendar-absolute-from-iso
  9873. (list iso-week day year))))
  9874. ; FIXME: Should we also push ISO weeks into the future?
  9875. ; (when (and org-read-date-prefer-future
  9876. ; (not iso-year)
  9877. ; (< (calendar-absolute-from-gregorian iso-date)
  9878. ; (time-to-days (current-time))))
  9879. ; (setq year (1+ year)
  9880. ; iso-date (calendar-gregorian-from-absolute
  9881. ; (calendar-absolute-from-iso
  9882. ; (list iso-week day year)))))
  9883. (setq month (car iso-date)
  9884. year (nth 2 iso-date)
  9885. day (nth 1 iso-date)))
  9886. (deltan
  9887. (unless deltadef
  9888. (let ((now (decode-time (current-time))))
  9889. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9890. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9891. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9892. ((equal deltaw "m") (setq month (+ month deltan)))
  9893. ((equal deltaw "y") (setq year (+ year deltan)))))
  9894. ((and wday (not (nth 3 tl)))
  9895. ;; Weekday was given, but no day, so pick that day in the week
  9896. ;; on or after the derived date.
  9897. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9898. (unless (equal wday wday1)
  9899. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9900. (if (and (boundp 'org-time-was-given)
  9901. (nth 2 tl))
  9902. (setq org-time-was-given t))
  9903. (if (< year 100) (setq year (+ 2000 year)))
  9904. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9905. (list second minute hour day month year)))
  9906. (defvar parse-time-weekdays)
  9907. (defun org-read-date-get-relative (s today default)
  9908. "Check string S for special relative date string.
  9909. TODAY and DEFAULT are internal times, for today and for a default.
  9910. Return shift list (N what def-flag)
  9911. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9912. N is the number of WHATs to shift.
  9913. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9914. the DEFAULT date rather than TODAY."
  9915. (when (and
  9916. (string-match
  9917. (concat
  9918. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9919. "\\([0-9]+\\)?"
  9920. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9921. "\\([ \t]\\|$\\)") s)
  9922. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  9923. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  9924. (string-to-char (substring (match-string 1 s) -1))
  9925. ?+))
  9926. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9927. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9928. (what (if (match-end 3) (match-string 3 s) "d"))
  9929. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9930. (date (if rel default today))
  9931. (wday (nth 6 (decode-time date)))
  9932. delta)
  9933. (if wday1
  9934. (progn
  9935. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9936. (if (= dir ?-) (setq delta (- delta 7)))
  9937. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9938. (list delta "d" rel))
  9939. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9940. (defun org-eval-in-calendar (form &optional keepdate)
  9941. "Eval FORM in the calendar window and return to current window.
  9942. Also, store the cursor date in variable org-ans2."
  9943. (let ((sw (selected-window)))
  9944. (select-window (get-buffer-window "*Calendar*"))
  9945. (eval form)
  9946. (when (and (not keepdate) (calendar-cursor-to-date))
  9947. (let* ((date (calendar-cursor-to-date))
  9948. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9949. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9950. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9951. (select-window sw)))
  9952. ; ;; Update the prompt to show new default date
  9953. ; (save-excursion
  9954. ; (goto-char (point-min))
  9955. ; (when (and org-ans2
  9956. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  9957. ; (get-text-property (match-end 0) 'field))
  9958. ; (let ((inhibit-read-only t))
  9959. ; (replace-match (concat "[" org-ans2 "]") t t)
  9960. ; (add-text-properties (point-min) (1+ (match-end 0))
  9961. ; (text-properties-at (1+ (point-min)))))))))
  9962. (defun org-calendar-select ()
  9963. "Return to `org-read-date' with the date currently selected.
  9964. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9965. (interactive)
  9966. (when (calendar-cursor-to-date)
  9967. (let* ((date (calendar-cursor-to-date))
  9968. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9969. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9970. (if (active-minibuffer-window) (exit-minibuffer))))
  9971. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9972. "Insert a date stamp for the date given by the internal TIME.
  9973. WITH-HM means, use the stamp format that includes the time of the day.
  9974. INACTIVE means use square brackets instead of angular ones, so that the
  9975. stamp will not contribute to the agenda.
  9976. PRE and POST are optional strings to be inserted before and after the
  9977. stamp.
  9978. The command returns the inserted time stamp."
  9979. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9980. stamp)
  9981. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9982. (insert-before-markers (or pre ""))
  9983. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9984. (when (listp extra)
  9985. (setq extra (car extra))
  9986. (if (and (stringp extra)
  9987. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9988. (setq extra (format "-%02d:%02d"
  9989. (string-to-number (match-string 1 extra))
  9990. (string-to-number (match-string 2 extra))))
  9991. (setq extra nil)))
  9992. (when extra
  9993. (backward-char 1)
  9994. (insert-before-markers extra)
  9995. (forward-char 1))
  9996. (insert-before-markers (or post ""))
  9997. stamp))
  9998. (defun org-toggle-time-stamp-overlays ()
  9999. "Toggle the use of custom time stamp formats."
  10000. (interactive)
  10001. (setq org-display-custom-times (not org-display-custom-times))
  10002. (unless org-display-custom-times
  10003. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10004. (while (setq p (next-single-property-change p 'display))
  10005. (if (and (get-text-property p 'display)
  10006. (eq (get-text-property p 'face) 'org-date))
  10007. (remove-text-properties
  10008. p (setq p (next-single-property-change p 'display))
  10009. '(display t))))
  10010. (set-buffer-modified-p bmp)))
  10011. (if (featurep 'xemacs)
  10012. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10013. (org-restart-font-lock)
  10014. (setq org-table-may-need-update t)
  10015. (if org-display-custom-times
  10016. (message "Time stamps are overlayed with custom format")
  10017. (message "Time stamp overlays removed")))
  10018. (defun org-display-custom-time (beg end)
  10019. "Overlay modified time stamp format over timestamp between BEG and END."
  10020. (let* ((ts (buffer-substring beg end))
  10021. t1 w1 with-hm tf time str w2 (off 0))
  10022. (save-match-data
  10023. (setq t1 (org-parse-time-string ts t))
  10024. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10025. (setq off (- (match-end 0) (match-beginning 0)))))
  10026. (setq end (- end off))
  10027. (setq w1 (- end beg)
  10028. with-hm (and (nth 1 t1) (nth 2 t1))
  10029. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10030. time (org-fix-decoded-time t1)
  10031. str (org-add-props
  10032. (format-time-string
  10033. (substring tf 1 -1) (apply 'encode-time time))
  10034. nil 'mouse-face 'highlight)
  10035. w2 (length str))
  10036. (if (not (= w2 w1))
  10037. (add-text-properties (1+ beg) (+ 2 beg)
  10038. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10039. (if (featurep 'xemacs)
  10040. (progn
  10041. (put-text-property beg end 'invisible t)
  10042. (put-text-property beg end 'end-glyph (make-glyph str)))
  10043. (put-text-property beg end 'display str))))
  10044. (defun org-translate-time (string)
  10045. "Translate all timestamps in STRING to custom format.
  10046. But do this only if the variable `org-display-custom-times' is set."
  10047. (when org-display-custom-times
  10048. (save-match-data
  10049. (let* ((start 0)
  10050. (re org-ts-regexp-both)
  10051. t1 with-hm inactive tf time str beg end)
  10052. (while (setq start (string-match re string start))
  10053. (setq beg (match-beginning 0)
  10054. end (match-end 0)
  10055. t1 (save-match-data
  10056. (org-parse-time-string (substring string beg end) t))
  10057. with-hm (and (nth 1 t1) (nth 2 t1))
  10058. inactive (equal (substring string beg (1+ beg)) "[")
  10059. tf (funcall (if with-hm 'cdr 'car)
  10060. org-time-stamp-custom-formats)
  10061. time (org-fix-decoded-time t1)
  10062. str (format-time-string
  10063. (concat
  10064. (if inactive "[" "<") (substring tf 1 -1)
  10065. (if inactive "]" ">"))
  10066. (apply 'encode-time time))
  10067. string (replace-match str t t string)
  10068. start (+ start (length str)))))))
  10069. string)
  10070. (defun org-fix-decoded-time (time)
  10071. "Set 0 instead of nil for the first 6 elements of time.
  10072. Don't touch the rest."
  10073. (let ((n 0))
  10074. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10075. (defun org-days-to-time (timestamp-string)
  10076. "Difference between TIMESTAMP-STRING and now in days."
  10077. (- (time-to-days (org-time-string-to-time timestamp-string))
  10078. (time-to-days (current-time))))
  10079. (defun org-deadline-close (timestamp-string &optional ndays)
  10080. "Is the time in TIMESTAMP-STRING close to the current date?"
  10081. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10082. (and (< (org-days-to-time timestamp-string) ndays)
  10083. (not (org-entry-is-done-p))))
  10084. (defun org-get-wdays (ts)
  10085. "Get the deadline lead time appropriate for timestring TS."
  10086. (cond
  10087. ((<= org-deadline-warning-days 0)
  10088. ;; 0 or negative, enforce this value no matter what
  10089. (- org-deadline-warning-days))
  10090. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10091. ;; lead time is specified.
  10092. (floor (* (string-to-number (match-string 1 ts))
  10093. (cdr (assoc (match-string 2 ts)
  10094. '(("d" . 1) ("w" . 7)
  10095. ("m" . 30.4) ("y" . 365.25)))))))
  10096. ;; go for the default.
  10097. (t org-deadline-warning-days)))
  10098. (defun org-calendar-select-mouse (ev)
  10099. "Return to `org-read-date' with the date currently selected.
  10100. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10101. (interactive "e")
  10102. (mouse-set-point ev)
  10103. (when (calendar-cursor-to-date)
  10104. (let* ((date (calendar-cursor-to-date))
  10105. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10106. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10107. (if (active-minibuffer-window) (exit-minibuffer))))
  10108. (defun org-check-deadlines (ndays)
  10109. "Check if there are any deadlines due or past due.
  10110. A deadline is considered due if it happens within `org-deadline-warning-days'
  10111. days from today's date. If the deadline appears in an entry marked DONE,
  10112. it is not shown. The prefix arg NDAYS can be used to test that many
  10113. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10114. (interactive "P")
  10115. (let* ((org-warn-days
  10116. (cond
  10117. ((equal ndays '(4)) 100000)
  10118. (ndays (prefix-numeric-value ndays))
  10119. (t (abs org-deadline-warning-days))))
  10120. (case-fold-search nil)
  10121. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10122. (callback
  10123. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10124. (message "%d deadlines past-due or due within %d days"
  10125. (org-occur regexp nil callback)
  10126. org-warn-days)))
  10127. (defun org-check-before-date (date)
  10128. "Check if there are deadlines or scheduled entries before DATE."
  10129. (interactive (list (org-read-date)))
  10130. (let ((case-fold-search nil)
  10131. (regexp (concat "\\<\\(" org-deadline-string
  10132. "\\|" org-scheduled-string
  10133. "\\) *<\\([^>]+\\)>"))
  10134. (callback
  10135. (lambda () (time-less-p
  10136. (org-time-string-to-time (match-string 2))
  10137. (org-time-string-to-time date)))))
  10138. (message "%d entries before %s"
  10139. (org-occur regexp nil callback) date)))
  10140. (defun org-evaluate-time-range (&optional to-buffer)
  10141. "Evaluate a time range by computing the difference between start and end.
  10142. Normally the result is just printed in the echo area, but with prefix arg
  10143. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10144. If the time range is actually in a table, the result is inserted into the
  10145. next column.
  10146. For time difference computation, a year is assumed to be exactly 365
  10147. days in order to avoid rounding problems."
  10148. (interactive "P")
  10149. (or
  10150. (org-clock-update-time-maybe)
  10151. (save-excursion
  10152. (unless (org-at-date-range-p t)
  10153. (goto-char (point-at-bol))
  10154. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10155. (if (not (org-at-date-range-p t))
  10156. (error "Not at a time-stamp range, and none found in current line")))
  10157. (let* ((ts1 (match-string 1))
  10158. (ts2 (match-string 2))
  10159. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10160. (match-end (match-end 0))
  10161. (time1 (org-time-string-to-time ts1))
  10162. (time2 (org-time-string-to-time ts2))
  10163. (t1 (time-to-seconds time1))
  10164. (t2 (time-to-seconds time2))
  10165. (diff (abs (- t2 t1)))
  10166. (negative (< (- t2 t1) 0))
  10167. ;; (ys (floor (* 365 24 60 60)))
  10168. (ds (* 24 60 60))
  10169. (hs (* 60 60))
  10170. (fy "%dy %dd %02d:%02d")
  10171. (fy1 "%dy %dd")
  10172. (fd "%dd %02d:%02d")
  10173. (fd1 "%dd")
  10174. (fh "%02d:%02d")
  10175. y d h m align)
  10176. (if havetime
  10177. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10178. y 0
  10179. d (floor (/ diff ds)) diff (mod diff ds)
  10180. h (floor (/ diff hs)) diff (mod diff hs)
  10181. m (floor (/ diff 60)))
  10182. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10183. y 0
  10184. d (floor (+ (/ diff ds) 0.5))
  10185. h 0 m 0))
  10186. (if (not to-buffer)
  10187. (message "%s" (org-make-tdiff-string y d h m))
  10188. (if (org-at-table-p)
  10189. (progn
  10190. (goto-char match-end)
  10191. (setq align t)
  10192. (and (looking-at " *|") (goto-char (match-end 0))))
  10193. (goto-char match-end))
  10194. (if (looking-at
  10195. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10196. (replace-match ""))
  10197. (if negative (insert " -"))
  10198. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10199. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10200. (insert " " (format fh h m))))
  10201. (if align (org-table-align))
  10202. (message "Time difference inserted")))))
  10203. (defun org-make-tdiff-string (y d h m)
  10204. (let ((fmt "")
  10205. (l nil))
  10206. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10207. l (push y l)))
  10208. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10209. l (push d l)))
  10210. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10211. l (push h l)))
  10212. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10213. l (push m l)))
  10214. (apply 'format fmt (nreverse l))))
  10215. (defun org-time-string-to-time (s)
  10216. (apply 'encode-time (org-parse-time-string s)))
  10217. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10218. "Convert a time stamp to an absolute day number.
  10219. If there is a specifyer for a cyclic time stamp, get the closest date to
  10220. DAYNR.
  10221. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  10222. (cond
  10223. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10224. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10225. daynr
  10226. (+ daynr 1000)))
  10227. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10228. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10229. (time-to-days (current-time))) (match-string 0 s)
  10230. prefer show-all))
  10231. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10232. (defun org-days-to-iso-week (days)
  10233. "Return the iso week number."
  10234. (require 'cal-iso)
  10235. (car (calendar-iso-from-absolute days)))
  10236. (defun org-small-year-to-year (year)
  10237. "Convert 2-digit years into 4-digit years.
  10238. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10239. The year 2000 cannot be abbreviated. Any year lager than 99
  10240. is retrned unchanged."
  10241. (if (< year 38)
  10242. (setq year (+ 2000 year))
  10243. (if (< year 100)
  10244. (setq year (+ 1900 year))))
  10245. year)
  10246. (defun org-time-from-absolute (d)
  10247. "Return the time corresponding to date D.
  10248. D may be an absolute day number, or a calendar-type list (month day year)."
  10249. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10250. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10251. (defun org-calendar-holiday ()
  10252. "List of holidays, for Diary display in Org-mode."
  10253. (require 'holidays)
  10254. (let ((hl (funcall
  10255. (if (fboundp 'calendar-check-holidays)
  10256. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10257. (if hl (mapconcat 'identity hl "; "))))
  10258. (defun org-diary-sexp-entry (sexp entry date)
  10259. "Process a SEXP diary ENTRY for DATE."
  10260. (require 'diary-lib)
  10261. (let ((result (if calendar-debug-sexp
  10262. (let ((stack-trace-on-error t))
  10263. (eval (car (read-from-string sexp))))
  10264. (condition-case nil
  10265. (eval (car (read-from-string sexp)))
  10266. (error
  10267. (beep)
  10268. (message "Bad sexp at line %d in %s: %s"
  10269. (org-current-line)
  10270. (buffer-file-name) sexp)
  10271. (sleep-for 2))))))
  10272. (cond ((stringp result) result)
  10273. ((and (consp result)
  10274. (stringp (cdr result))) (cdr result))
  10275. (result entry)
  10276. (t nil))))
  10277. (defun org-diary-to-ical-string (frombuf)
  10278. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10279. This uses the icalendar.el library."
  10280. (let* ((tmpdir (if (featurep 'xemacs)
  10281. (temp-directory)
  10282. temporary-file-directory))
  10283. (tmpfile (make-temp-name
  10284. (expand-file-name "orgics" tmpdir)))
  10285. buf rtn b e)
  10286. (save-excursion
  10287. (set-buffer frombuf)
  10288. (icalendar-export-region (point-min) (point-max) tmpfile)
  10289. (setq buf (find-buffer-visiting tmpfile))
  10290. (set-buffer buf)
  10291. (goto-char (point-min))
  10292. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10293. (setq b (match-beginning 0)))
  10294. (goto-char (point-max))
  10295. (if (re-search-backward "^END:VEVENT" nil t)
  10296. (setq e (match-end 0)))
  10297. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10298. (kill-buffer buf)
  10299. (kill-buffer frombuf)
  10300. (delete-file tmpfile)
  10301. rtn))
  10302. (defun org-closest-date (start current change prefer show-all)
  10303. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10304. When PREFER is `past' return a date that is either CURRENT or past.
  10305. When PREFER is `future', return a date that is either CURRENT or future.
  10306. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10307. ;; Make the proper lists from the dates
  10308. (catch 'exit
  10309. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10310. dn dw sday cday n1 n2
  10311. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10312. (setq start (org-date-to-gregorian start)
  10313. current (org-date-to-gregorian
  10314. (if show-all
  10315. current
  10316. (time-to-days (current-time))))
  10317. sday (calendar-absolute-from-gregorian start)
  10318. cday (calendar-absolute-from-gregorian current))
  10319. (if (<= cday sday) (throw 'exit sday))
  10320. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10321. (setq dn (string-to-number (match-string 1 change))
  10322. dw (cdr (assoc (match-string 2 change) a1)))
  10323. (error "Invalid change specifyer: %s" change))
  10324. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10325. (cond
  10326. ((eq dw 'day)
  10327. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10328. n2 (+ n1 dn)))
  10329. ((eq dw 'year)
  10330. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10331. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10332. (setq date1 (list m d y1)
  10333. n1 (calendar-absolute-from-gregorian date1)
  10334. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10335. n2 (calendar-absolute-from-gregorian date2)))
  10336. ((eq dw 'month)
  10337. ;; approx number of month between the tow dates
  10338. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10339. ;; How often does dn fit in there?
  10340. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10341. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10342. m (+ m nm)
  10343. ny (floor (/ m 12))
  10344. y (+ y ny)
  10345. m (- m (* ny 12)))
  10346. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10347. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10348. (setq m2 (+ m dn) y2 y)
  10349. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10350. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10351. (while (< n2 cday)
  10352. (setq n1 n2 m m2 y y2)
  10353. (setq m2 (+ m dn) y2 y)
  10354. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10355. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10356. (if show-all
  10357. (cond
  10358. ((eq prefer 'past) n1)
  10359. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10360. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10361. (cond
  10362. ((eq prefer 'past) n1)
  10363. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10364. (t (if (= cday n1) n1 n2)))))))
  10365. (defun org-date-to-gregorian (date)
  10366. "Turn any specification of DATE into a gregorian date for the calendar."
  10367. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10368. ((and (listp date) (= (length date) 3)) date)
  10369. ((stringp date)
  10370. (setq date (org-parse-time-string date))
  10371. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10372. ((listp date)
  10373. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10374. (defun org-parse-time-string (s &optional nodefault)
  10375. "Parse the standard Org-mode time string.
  10376. This should be a lot faster than the normal `parse-time-string'.
  10377. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10378. hour and minute fields will be nil if not given."
  10379. (if (string-match org-ts-regexp0 s)
  10380. (list 0
  10381. (if (or (match-beginning 8) (not nodefault))
  10382. (string-to-number (or (match-string 8 s) "0")))
  10383. (if (or (match-beginning 7) (not nodefault))
  10384. (string-to-number (or (match-string 7 s) "0")))
  10385. (string-to-number (match-string 4 s))
  10386. (string-to-number (match-string 3 s))
  10387. (string-to-number (match-string 2 s))
  10388. nil nil nil)
  10389. (make-list 9 0)))
  10390. (defun org-timestamp-up (&optional arg)
  10391. "Increase the date item at the cursor by one.
  10392. If the cursor is on the year, change the year. If it is on the month or
  10393. the day, change that.
  10394. With prefix ARG, change by that many units."
  10395. (interactive "p")
  10396. (org-timestamp-change (prefix-numeric-value arg)))
  10397. (defun org-timestamp-down (&optional arg)
  10398. "Decrease the date item at the cursor by one.
  10399. If the cursor is on the year, change the year. If it is on the month or
  10400. the day, change that.
  10401. With prefix ARG, change by that many units."
  10402. (interactive "p")
  10403. (org-timestamp-change (- (prefix-numeric-value arg))))
  10404. (defun org-timestamp-up-day (&optional arg)
  10405. "Increase the date in the time stamp by one day.
  10406. With prefix ARG, change that many days."
  10407. (interactive "p")
  10408. (if (and (not (org-at-timestamp-p t))
  10409. (org-on-heading-p))
  10410. (org-todo 'up)
  10411. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10412. (defun org-timestamp-down-day (&optional arg)
  10413. "Decrease the date in the time stamp by one day.
  10414. With prefix ARG, change that many days."
  10415. (interactive "p")
  10416. (if (and (not (org-at-timestamp-p t))
  10417. (org-on-heading-p))
  10418. (org-todo 'down)
  10419. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10420. (defun org-at-timestamp-p (&optional inactive-ok)
  10421. "Determine if the cursor is in or at a timestamp."
  10422. (interactive)
  10423. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10424. (pos (point))
  10425. (ans (or (looking-at tsr)
  10426. (save-excursion
  10427. (skip-chars-backward "^[<\n\r\t")
  10428. (if (> (point) (point-min)) (backward-char 1))
  10429. (and (looking-at tsr)
  10430. (> (- (match-end 0) pos) -1))))))
  10431. (and ans
  10432. (boundp 'org-ts-what)
  10433. (setq org-ts-what
  10434. (cond
  10435. ((= pos (match-beginning 0)) 'bracket)
  10436. ((= pos (1- (match-end 0))) 'bracket)
  10437. ((org-pos-in-match-range pos 2) 'year)
  10438. ((org-pos-in-match-range pos 3) 'month)
  10439. ((org-pos-in-match-range pos 7) 'hour)
  10440. ((org-pos-in-match-range pos 8) 'minute)
  10441. ((or (org-pos-in-match-range pos 4)
  10442. (org-pos-in-match-range pos 5)) 'day)
  10443. ((and (> pos (or (match-end 8) (match-end 5)))
  10444. (< pos (match-end 0)))
  10445. (- pos (or (match-end 8) (match-end 5))))
  10446. (t 'day))))
  10447. ans))
  10448. (defun org-toggle-timestamp-type ()
  10449. "Toggle the type (<active> or [inactive]) of a time stamp."
  10450. (interactive)
  10451. (when (org-at-timestamp-p t)
  10452. (save-excursion
  10453. (goto-char (match-beginning 0))
  10454. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10455. (goto-char (1- (match-end 0)))
  10456. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10457. (message "Timestamp is now %sactive"
  10458. (if (equal (char-before) ?>) "in" ""))))
  10459. (defun org-timestamp-change (n &optional what)
  10460. "Change the date in the time stamp at point.
  10461. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10462. `year', `minute', `second'. If WHAT is not given, the cursor position
  10463. in the timestamp determines what will be changed."
  10464. (let ((pos (point))
  10465. with-hm inactive
  10466. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10467. org-ts-what
  10468. extra rem
  10469. ts time time0)
  10470. (if (not (org-at-timestamp-p t))
  10471. (error "Not at a timestamp"))
  10472. (if (and (not what) (eq org-ts-what 'bracket))
  10473. (org-toggle-timestamp-type)
  10474. (if (and (not what) (not (eq org-ts-what 'day))
  10475. org-display-custom-times
  10476. (get-text-property (point) 'display)
  10477. (not (get-text-property (1- (point)) 'display)))
  10478. (setq org-ts-what 'day))
  10479. (setq org-ts-what (or what org-ts-what)
  10480. inactive (= (char-after (match-beginning 0)) ?\[)
  10481. ts (match-string 0))
  10482. (replace-match "")
  10483. (if (string-match
  10484. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10485. ts)
  10486. (setq extra (match-string 1 ts)))
  10487. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10488. (setq with-hm t))
  10489. (setq time0 (org-parse-time-string ts))
  10490. (when (and (eq org-ts-what 'minute)
  10491. (eq current-prefix-arg nil))
  10492. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10493. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10494. (setcar (cdr time0) (+ (nth 1 time0)
  10495. (if (> n 0) (- rem) (- dm rem))))))
  10496. (setq time
  10497. (encode-time (or (car time0) 0)
  10498. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10499. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10500. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10501. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10502. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10503. (nthcdr 6 time0)))
  10504. (when (integerp org-ts-what)
  10505. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10506. (if (eq what 'calendar)
  10507. (let ((cal-date (org-get-date-from-calendar)))
  10508. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10509. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10510. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10511. (setcar time0 (or (car time0) 0))
  10512. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10513. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10514. (setq time (apply 'encode-time time0))))
  10515. (setq org-last-changed-timestamp
  10516. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10517. (org-clock-update-time-maybe)
  10518. (goto-char pos)
  10519. ;; Try to recenter the calendar window, if any
  10520. (if (and org-calendar-follow-timestamp-change
  10521. (get-buffer-window "*Calendar*" t)
  10522. (memq org-ts-what '(day month year)))
  10523. (org-recenter-calendar (time-to-days time))))))
  10524. (defun org-modify-ts-extra (s pos n dm)
  10525. "Change the different parts of the lead-time and repeat fields in timestamp."
  10526. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10527. ng h m new rem)
  10528. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10529. (cond
  10530. ((or (org-pos-in-match-range pos 2)
  10531. (org-pos-in-match-range pos 3))
  10532. (setq m (string-to-number (match-string 3 s))
  10533. h (string-to-number (match-string 2 s)))
  10534. (if (org-pos-in-match-range pos 2)
  10535. (setq h (+ h n))
  10536. (setq n (* dm (org-no-warnings (signum n))))
  10537. (when (not (= 0 (setq rem (% m dm))))
  10538. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10539. (setq m (+ m n)))
  10540. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10541. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10542. (setq h (min 24 (max 0 h)))
  10543. (setq ng 1 new (format "-%02d:%02d" h m)))
  10544. ((org-pos-in-match-range pos 6)
  10545. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10546. ((org-pos-in-match-range pos 5)
  10547. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10548. ((org-pos-in-match-range pos 9)
  10549. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10550. ((org-pos-in-match-range pos 8)
  10551. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10552. (when ng
  10553. (setq s (concat
  10554. (substring s 0 (match-beginning ng))
  10555. new
  10556. (substring s (match-end ng))))))
  10557. s))
  10558. (defun org-recenter-calendar (date)
  10559. "If the calendar is visible, recenter it to DATE."
  10560. (let* ((win (selected-window))
  10561. (cwin (get-buffer-window "*Calendar*" t))
  10562. (calendar-move-hook nil))
  10563. (when cwin
  10564. (select-window cwin)
  10565. (calendar-goto-date (if (listp date) date
  10566. (calendar-gregorian-from-absolute date)))
  10567. (select-window win))))
  10568. (defun org-goto-calendar (&optional arg)
  10569. "Go to the Emacs calendar at the current date.
  10570. If there is a time stamp in the current line, go to that date.
  10571. A prefix ARG can be used to force the current date."
  10572. (interactive "P")
  10573. (let ((tsr org-ts-regexp) diff
  10574. (calendar-move-hook nil)
  10575. (calendar-view-holidays-initially-flag nil)
  10576. (view-calendar-holidays-initially nil)
  10577. (calendar-view-diary-initially-flag nil)
  10578. (view-diary-entries-initially nil))
  10579. (if (or (org-at-timestamp-p)
  10580. (save-excursion
  10581. (beginning-of-line 1)
  10582. (looking-at (concat ".*" tsr))))
  10583. (let ((d1 (time-to-days (current-time)))
  10584. (d2 (time-to-days
  10585. (org-time-string-to-time (match-string 1)))))
  10586. (setq diff (- d2 d1))))
  10587. (calendar)
  10588. (calendar-goto-today)
  10589. (if (and diff (not arg)) (calendar-forward-day diff))))
  10590. (defun org-get-date-from-calendar ()
  10591. "Return a list (month day year) of date at point in calendar."
  10592. (with-current-buffer "*Calendar*"
  10593. (save-match-data
  10594. (calendar-cursor-to-date))))
  10595. (defun org-date-from-calendar ()
  10596. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10597. If there is already a time stamp at the cursor position, update it."
  10598. (interactive)
  10599. (if (org-at-timestamp-p t)
  10600. (org-timestamp-change 0 'calendar)
  10601. (let ((cal-date (org-get-date-from-calendar)))
  10602. (org-insert-time-stamp
  10603. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10604. (defun org-minutes-to-hh:mm-string (m)
  10605. "Compute H:MM from a number of minutes."
  10606. (let ((h (/ m 60)))
  10607. (setq m (- m (* 60 h)))
  10608. (format org-time-clocksum-format h m)))
  10609. (defun org-hh:mm-string-to-minutes (s)
  10610. "Convert a string H:MM to a number of minutes."
  10611. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10612. (+ (* (string-to-number (match-string 1 s)) 60)
  10613. (string-to-number (match-string 2 s)))
  10614. 0))
  10615. ;;;; Agenda files
  10616. ;;;###autoload
  10617. (defun org-iswitchb (&optional arg)
  10618. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10619. With a prefix argument, restrict available to files.
  10620. With two prefix arguments, restrict available buffers to agenda files.
  10621. Due to some yet unresolved reason, global function
  10622. `iswitchb-mode' needs to be active for this function to work."
  10623. (interactive "P")
  10624. (require 'iswitchb)
  10625. (let ((enabled iswitchb-mode) blist)
  10626. (or enabled (iswitchb-mode 1))
  10627. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10628. ((equal arg '(16)) (org-buffer-list 'agenda))
  10629. (t (org-buffer-list))))
  10630. (unwind-protect
  10631. (let ((iswitchb-make-buflist-hook
  10632. (lambda ()
  10633. (setq iswitchb-temp-buflist
  10634. (mapcar 'buffer-name blist)))))
  10635. (switch-to-buffer
  10636. (iswitchb-read-buffer
  10637. "Switch-to: " nil t))
  10638. (or enabled (iswitchb-mode -1))))))
  10639. (defun org-buffer-list (&optional predicate tmp)
  10640. "Return a list of Org buffers.
  10641. PREDICATE can be either 'export, 'files or 'agenda.
  10642. 'export restrict the list to Export buffers.
  10643. 'files restrict the list to buffers visiting Org files.
  10644. 'agenda restrict the list to buffers visiting agenda files.
  10645. If TMP is non-nil, don't include temporary buffers."
  10646. (let (filter blist)
  10647. (setq filter
  10648. (cond ((eq predicate 'files) "\.org$")
  10649. ((eq predicate 'export) "\*Org .*Export")
  10650. (t "\*Org \\|\.org$")))
  10651. (setq blist
  10652. (mapcar
  10653. (lambda(b)
  10654. (let ((bname (buffer-name b))
  10655. (bfile (buffer-file-name b)))
  10656. (if (and (string-match filter bname)
  10657. (if (eq predicate 'agenda)
  10658. (member bfile
  10659. (mapcar (lambda(f) (file-truename f))
  10660. org-agenda-files)) t)
  10661. (if tmp (not (string-match "tmp" bname)) t)) b)))
  10662. (buffer-list)))
  10663. (delete nil blist)))
  10664. (defun org-agenda-files (&optional unrestricted ext)
  10665. "Get the list of agenda files.
  10666. Optional UNRESTRICTED means return the full list even if a restriction
  10667. is currently in place.
  10668. When EXT is non-nil, try to add all files that are created by adding EXT
  10669. to the file nemes. Basically, this is a way to add the archive files
  10670. to the list, by setting EXT to \"_archive\" If EXT is non-nil, but not
  10671. a string, \"_archive\" will be used."
  10672. (let ((files
  10673. (cond
  10674. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10675. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10676. ((listp org-agenda-files) org-agenda-files)
  10677. (t (error "Invalid value of `org-agenda-files'")))))
  10678. (setq files (apply 'append
  10679. (mapcar (lambda (f)
  10680. (if (file-directory-p f)
  10681. (directory-files
  10682. f t org-agenda-file-regexp)
  10683. (list f)))
  10684. files)))
  10685. (when org-agenda-skip-unavailable-files
  10686. (setq files (delq nil
  10687. (mapcar (function
  10688. (lambda (file)
  10689. (and (file-readable-p file) file)))
  10690. files))))
  10691. (when ext
  10692. (setq ext (if (and (stringp ext) (string-match "\\S-" ext))
  10693. ext "_archive"))
  10694. (setq files (apply 'append
  10695. (mapcar
  10696. (lambda (f)
  10697. (if (file-exists-p (concat f ext))
  10698. (list f (concat f ext))
  10699. (list f)))
  10700. files))))
  10701. files))
  10702. (defun org-edit-agenda-file-list ()
  10703. "Edit the list of agenda files.
  10704. Depending on setup, this either uses customize to edit the variable
  10705. `org-agenda-files', or it visits the file that is holding the list. In the
  10706. latter case, the buffer is set up in a way that saving it automatically kills
  10707. the buffer and restores the previous window configuration."
  10708. (interactive)
  10709. (if (stringp org-agenda-files)
  10710. (let ((cw (current-window-configuration)))
  10711. (find-file org-agenda-files)
  10712. (org-set-local 'org-window-configuration cw)
  10713. (org-add-hook 'after-save-hook
  10714. (lambda ()
  10715. (set-window-configuration
  10716. (prog1 org-window-configuration
  10717. (kill-buffer (current-buffer))))
  10718. (org-install-agenda-files-menu)
  10719. (message "New agenda file list installed"))
  10720. nil 'local)
  10721. (message "%s" (substitute-command-keys
  10722. "Edit list and finish with \\[save-buffer]")))
  10723. (customize-variable 'org-agenda-files)))
  10724. (defun org-store-new-agenda-file-list (list)
  10725. "Set new value for the agenda file list and save it correcly."
  10726. (if (stringp org-agenda-files)
  10727. (let ((f org-agenda-files) b)
  10728. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10729. (with-temp-file f
  10730. (insert (mapconcat 'identity list "\n") "\n")))
  10731. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10732. (setq org-agenda-files list)
  10733. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10734. (defun org-read-agenda-file-list ()
  10735. "Read the list of agenda files from a file."
  10736. (when (file-directory-p org-agenda-files)
  10737. (error "`org-agenda-files' cannot be a single directory"))
  10738. (when (stringp org-agenda-files)
  10739. (with-temp-buffer
  10740. (insert-file-contents org-agenda-files)
  10741. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10742. ;;;###autoload
  10743. (defun org-cycle-agenda-files ()
  10744. "Cycle through the files in `org-agenda-files'.
  10745. If the current buffer visits an agenda file, find the next one in the list.
  10746. If the current buffer does not, find the first agenda file."
  10747. (interactive)
  10748. (let* ((fs (org-agenda-files t))
  10749. (files (append fs (list (car fs))))
  10750. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10751. file)
  10752. (unless files (error "No agenda files"))
  10753. (catch 'exit
  10754. (while (setq file (pop files))
  10755. (if (equal (file-truename file) tcf)
  10756. (when (car files)
  10757. (find-file (car files))
  10758. (throw 'exit t))))
  10759. (find-file (car fs)))
  10760. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10761. (defun org-agenda-file-to-front (&optional to-end)
  10762. "Move/add the current file to the top of the agenda file list.
  10763. If the file is not present in the list, it is added to the front. If it is
  10764. present, it is moved there. With optional argument TO-END, add/move to the
  10765. end of the list."
  10766. (interactive "P")
  10767. (let ((org-agenda-skip-unavailable-files nil)
  10768. (file-alist (mapcar (lambda (x)
  10769. (cons (file-truename x) x))
  10770. (org-agenda-files t)))
  10771. (ctf (file-truename buffer-file-name))
  10772. x had)
  10773. (setq x (assoc ctf file-alist) had x)
  10774. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10775. (if to-end
  10776. (setq file-alist (append (delq x file-alist) (list x)))
  10777. (setq file-alist (cons x (delq x file-alist))))
  10778. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10779. (org-install-agenda-files-menu)
  10780. (message "File %s to %s of agenda file list"
  10781. (if had "moved" "added") (if to-end "end" "front"))))
  10782. (defun org-remove-file (&optional file)
  10783. "Remove current file from the list of files in variable `org-agenda-files'.
  10784. These are the files which are being checked for agenda entries.
  10785. Optional argument FILE means, use this file instead of the current."
  10786. (interactive)
  10787. (let* ((org-agenda-skip-unavailable-files nil)
  10788. (file (or file buffer-file-name))
  10789. (true-file (file-truename file))
  10790. (afile (abbreviate-file-name file))
  10791. (files (delq nil (mapcar
  10792. (lambda (x)
  10793. (if (equal true-file
  10794. (file-truename x))
  10795. nil x))
  10796. (org-agenda-files t)))))
  10797. (if (not (= (length files) (length (org-agenda-files t))))
  10798. (progn
  10799. (org-store-new-agenda-file-list files)
  10800. (org-install-agenda-files-menu)
  10801. (message "Removed file: %s" afile))
  10802. (message "File was not in list: %s (not removed)" afile))))
  10803. (defun org-file-menu-entry (file)
  10804. (vector file (list 'find-file file) t))
  10805. (defun org-check-agenda-file (file)
  10806. "Make sure FILE exists. If not, ask user what to do."
  10807. (when (not (file-exists-p file))
  10808. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10809. (abbreviate-file-name file))
  10810. (let ((r (downcase (read-char-exclusive))))
  10811. (cond
  10812. ((equal r ?r)
  10813. (org-remove-file file)
  10814. (throw 'nextfile t))
  10815. (t (error "Abort"))))))
  10816. (defun org-get-agenda-file-buffer (file)
  10817. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10818. it to the list of buffers which might be released later."
  10819. (let ((buf (org-find-base-buffer-visiting file)))
  10820. (if buf
  10821. buf ; just return it
  10822. ;; Make a new buffer and remember it
  10823. (setq buf (find-file-noselect file))
  10824. (if buf (push buf org-agenda-new-buffers))
  10825. buf)))
  10826. (defun org-release-buffers (blist)
  10827. "Release all buffers in list, asking the user for confirmation when needed.
  10828. When a buffer is unmodified, it is just killed. When modified, it is saved
  10829. \(if the user agrees) and then killed."
  10830. (let (buf file)
  10831. (while (setq buf (pop blist))
  10832. (setq file (buffer-file-name buf))
  10833. (when (and (buffer-modified-p buf)
  10834. file
  10835. (y-or-n-p (format "Save file %s? " file)))
  10836. (with-current-buffer buf (save-buffer)))
  10837. (kill-buffer buf))))
  10838. (defun org-prepare-agenda-buffers (files)
  10839. "Create buffers for all agenda files, protect archived trees and comments."
  10840. (interactive)
  10841. (let ((pa '(:org-archived t))
  10842. (pc '(:org-comment t))
  10843. (pall '(:org-archived t :org-comment t))
  10844. (inhibit-read-only t)
  10845. (rea (concat ":" org-archive-tag ":"))
  10846. bmp file re)
  10847. (save-excursion
  10848. (save-restriction
  10849. (while (setq file (pop files))
  10850. (if (bufferp file)
  10851. (set-buffer file)
  10852. (org-check-agenda-file file)
  10853. (set-buffer (org-get-agenda-file-buffer file)))
  10854. (widen)
  10855. (setq bmp (buffer-modified-p))
  10856. (org-refresh-category-properties)
  10857. (setq org-todo-keywords-for-agenda
  10858. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10859. (setq org-done-keywords-for-agenda
  10860. (append org-done-keywords-for-agenda org-done-keywords))
  10861. (save-excursion
  10862. (remove-text-properties (point-min) (point-max) pall)
  10863. (when org-agenda-skip-archived-trees
  10864. (goto-char (point-min))
  10865. (while (re-search-forward rea nil t)
  10866. (if (org-on-heading-p t)
  10867. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10868. (goto-char (point-min))
  10869. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10870. (while (re-search-forward re nil t)
  10871. (add-text-properties
  10872. (match-beginning 0) (org-end-of-subtree t) pc)))
  10873. (set-buffer-modified-p bmp))))))
  10874. ;;;; Embedded LaTeX
  10875. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10876. "Keymap for the minor `org-cdlatex-mode'.")
  10877. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10878. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10879. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10880. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10881. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10882. (defvar org-cdlatex-texmathp-advice-is-done nil
  10883. "Flag remembering if we have applied the advice to texmathp already.")
  10884. (define-minor-mode org-cdlatex-mode
  10885. "Toggle the minor `org-cdlatex-mode'.
  10886. This mode supports entering LaTeX environment and math in LaTeX fragments
  10887. in Org-mode.
  10888. \\{org-cdlatex-mode-map}"
  10889. nil " OCDL" nil
  10890. (when org-cdlatex-mode (require 'cdlatex))
  10891. (unless org-cdlatex-texmathp-advice-is-done
  10892. (setq org-cdlatex-texmathp-advice-is-done t)
  10893. (defadvice texmathp (around org-math-always-on activate)
  10894. "Always return t in org-mode buffers.
  10895. This is because we want to insert math symbols without dollars even outside
  10896. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10897. en embedded LaTeX fragement, let texmathp do its job.
  10898. \\[org-cdlatex-mode-map]"
  10899. (interactive)
  10900. (let (p)
  10901. (cond
  10902. ((not (org-mode-p)) ad-do-it)
  10903. ((eq this-command 'cdlatex-math-symbol)
  10904. (setq ad-return-value t
  10905. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10906. (t
  10907. (let ((p (org-inside-LaTeX-fragment-p)))
  10908. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10909. (setq ad-return-value t
  10910. texmathp-why '("Org-mode embedded math" . 0))
  10911. (if p ad-do-it)))))))))
  10912. (defun turn-on-org-cdlatex ()
  10913. "Unconditionally turn on `org-cdlatex-mode'."
  10914. (org-cdlatex-mode 1))
  10915. (defun org-inside-LaTeX-fragment-p ()
  10916. "Test if point is inside a LaTeX fragment.
  10917. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10918. sequence appearing also before point.
  10919. Even though the matchers for math are configurable, this function assumes
  10920. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10921. delimiters are skipped when they have been removed by customization.
  10922. The return value is nil, or a cons cell with the delimiter and
  10923. and the position of this delimiter.
  10924. This function does a reasonably good job, but can locally be fooled by
  10925. for example currency specifications. For example it will assume being in
  10926. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10927. fragments that are properly closed, but during editing, we have to live
  10928. with the uncertainty caused by missing closing delimiters. This function
  10929. looks only before point, not after."
  10930. (catch 'exit
  10931. (let ((pos (point))
  10932. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10933. (lim (progn
  10934. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10935. (point)))
  10936. dd-on str (start 0) m re)
  10937. (goto-char pos)
  10938. (when dodollar
  10939. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10940. re (nth 1 (assoc "$" org-latex-regexps)))
  10941. (while (string-match re str start)
  10942. (cond
  10943. ((= (match-end 0) (length str))
  10944. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10945. ((= (match-end 0) (- (length str) 5))
  10946. (throw 'exit nil))
  10947. (t (setq start (match-end 0))))))
  10948. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10949. (goto-char pos)
  10950. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10951. (and (match-beginning 2) (throw 'exit nil))
  10952. ;; count $$
  10953. (while (re-search-backward "\\$\\$" lim t)
  10954. (setq dd-on (not dd-on)))
  10955. (goto-char pos)
  10956. (if dd-on (cons "$$" m))))))
  10957. (defun org-try-cdlatex-tab ()
  10958. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10959. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10960. - inside a LaTeX fragment, or
  10961. - after the first word in a line, where an abbreviation expansion could
  10962. insert a LaTeX environment."
  10963. (when org-cdlatex-mode
  10964. (cond
  10965. ((save-excursion
  10966. (skip-chars-backward "a-zA-Z0-9*")
  10967. (skip-chars-backward " \t")
  10968. (bolp))
  10969. (cdlatex-tab) t)
  10970. ((org-inside-LaTeX-fragment-p)
  10971. (cdlatex-tab) t)
  10972. (t nil))))
  10973. (defun org-cdlatex-underscore-caret (&optional arg)
  10974. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10975. Revert to the normal definition outside of these fragments."
  10976. (interactive "P")
  10977. (if (org-inside-LaTeX-fragment-p)
  10978. (call-interactively 'cdlatex-sub-superscript)
  10979. (let (org-cdlatex-mode)
  10980. (call-interactively (key-binding (vector last-input-event))))))
  10981. (defun org-cdlatex-math-modify (&optional arg)
  10982. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10983. Revert to the normal definition outside of these fragments."
  10984. (interactive "P")
  10985. (if (org-inside-LaTeX-fragment-p)
  10986. (call-interactively 'cdlatex-math-modify)
  10987. (let (org-cdlatex-mode)
  10988. (call-interactively (key-binding (vector last-input-event))))))
  10989. (defvar org-latex-fragment-image-overlays nil
  10990. "List of overlays carrying the images of latex fragments.")
  10991. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10992. (defun org-remove-latex-fragment-image-overlays ()
  10993. "Remove all overlays with LaTeX fragment images in current buffer."
  10994. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10995. (setq org-latex-fragment-image-overlays nil))
  10996. (defun org-preview-latex-fragment (&optional subtree)
  10997. "Preview the LaTeX fragment at point, or all locally or globally.
  10998. If the cursor is in a LaTeX fragment, create the image and overlay
  10999. it over the source code. If there is no fragment at point, display
  11000. all fragments in the current text, from one headline to the next. With
  11001. prefix SUBTREE, display all fragments in the current subtree. With a
  11002. double prefix `C-u C-u', or when the cursor is before the first headline,
  11003. display all fragments in the buffer.
  11004. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11005. (interactive "P")
  11006. (org-remove-latex-fragment-image-overlays)
  11007. (save-excursion
  11008. (save-restriction
  11009. (let (beg end at msg)
  11010. (cond
  11011. ((or (equal subtree '(16))
  11012. (not (save-excursion
  11013. (re-search-backward (concat "^" outline-regexp) nil t))))
  11014. (setq beg (point-min) end (point-max)
  11015. msg "Creating images for buffer...%s"))
  11016. ((equal subtree '(4))
  11017. (org-back-to-heading)
  11018. (setq beg (point) end (org-end-of-subtree t)
  11019. msg "Creating images for subtree...%s"))
  11020. (t
  11021. (if (setq at (org-inside-LaTeX-fragment-p))
  11022. (goto-char (max (point-min) (- (cdr at) 2)))
  11023. (org-back-to-heading))
  11024. (setq beg (point) end (progn (outline-next-heading) (point))
  11025. msg (if at "Creating image...%s"
  11026. "Creating images for entry...%s"))))
  11027. (message msg "")
  11028. (narrow-to-region beg end)
  11029. (goto-char beg)
  11030. (org-format-latex
  11031. (concat "ltxpng/" (file-name-sans-extension
  11032. (file-name-nondirectory
  11033. buffer-file-name)))
  11034. default-directory 'overlays msg at 'forbuffer)
  11035. (message msg "done. Use `C-c C-c' to remove images.")))))
  11036. (defvar org-latex-regexps
  11037. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11038. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11039. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11040. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11041. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11042. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11043. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11044. "Regular expressions for matching embedded LaTeX.")
  11045. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11046. "Replace LaTeX fragments with links to an image, and produce images."
  11047. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11048. (let* ((prefixnodir (file-name-nondirectory prefix))
  11049. (absprefix (expand-file-name prefix dir))
  11050. (todir (file-name-directory absprefix))
  11051. (opt org-format-latex-options)
  11052. (matchers (plist-get opt :matchers))
  11053. (re-list org-latex-regexps)
  11054. (cnt 0) txt link beg end re e checkdir
  11055. m n block linkfile movefile ov)
  11056. ;; Check if there are old images files with this prefix, and remove them
  11057. (when (file-directory-p todir)
  11058. (mapc 'delete-file
  11059. (directory-files
  11060. todir 'full
  11061. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11062. ;; Check the different regular expressions
  11063. (while (setq e (pop re-list))
  11064. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11065. block (if (nth 3 e) "\n\n" ""))
  11066. (when (member m matchers)
  11067. (goto-char (point-min))
  11068. (while (re-search-forward re nil t)
  11069. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11070. (setq txt (match-string n)
  11071. beg (match-beginning n) end (match-end n)
  11072. cnt (1+ cnt)
  11073. linkfile (format "%s_%04d.png" prefix cnt)
  11074. movefile (format "%s_%04d.png" absprefix cnt)
  11075. link (concat block "[[file:" linkfile "]]" block))
  11076. (if msg (message msg cnt))
  11077. (goto-char beg)
  11078. (unless checkdir ; make sure the directory exists
  11079. (setq checkdir t)
  11080. (or (file-directory-p todir) (make-directory todir)))
  11081. (org-create-formula-image
  11082. txt movefile opt forbuffer)
  11083. (if overlays
  11084. (progn
  11085. (setq ov (org-make-overlay beg end))
  11086. (if (featurep 'xemacs)
  11087. (progn
  11088. (org-overlay-put ov 'invisible t)
  11089. (org-overlay-put
  11090. ov 'end-glyph
  11091. (make-glyph (vector 'png :file movefile))))
  11092. (org-overlay-put
  11093. ov 'display
  11094. (list 'image :type 'png :file movefile :ascent 'center)))
  11095. (push ov org-latex-fragment-image-overlays)
  11096. (goto-char end))
  11097. (delete-region beg end)
  11098. (insert link))))))))
  11099. ;; This function borrows from Ganesh Swami's latex2png.el
  11100. (defun org-create-formula-image (string tofile options buffer)
  11101. (let* ((tmpdir (if (featurep 'xemacs)
  11102. (temp-directory)
  11103. temporary-file-directory))
  11104. (texfilebase (make-temp-name
  11105. (expand-file-name "orgtex" tmpdir)))
  11106. (texfile (concat texfilebase ".tex"))
  11107. (dvifile (concat texfilebase ".dvi"))
  11108. (pngfile (concat texfilebase ".png"))
  11109. (fnh (if (featurep 'xemacs)
  11110. (font-height (get-face-font 'default))
  11111. (face-attribute 'default :height nil)))
  11112. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11113. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11114. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11115. "Black"))
  11116. (bg (or (plist-get options (if buffer :background :html-background))
  11117. "Transparent")))
  11118. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11119. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11120. (with-temp-file texfile
  11121. (insert org-format-latex-header
  11122. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11123. (let ((dir default-directory))
  11124. (condition-case nil
  11125. (progn
  11126. (cd tmpdir)
  11127. (call-process "latex" nil nil nil texfile))
  11128. (error nil))
  11129. (cd dir))
  11130. (if (not (file-exists-p dvifile))
  11131. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11132. (call-process "dvipng" nil nil nil
  11133. "-E" "-fg" fg "-bg" bg
  11134. "-D" dpi
  11135. ;;"-x" scale "-y" scale
  11136. "-T" "tight"
  11137. "-o" pngfile
  11138. dvifile)
  11139. (if (not (file-exists-p pngfile))
  11140. (progn (message "Failed to create png file from %s" texfile) nil)
  11141. ;; Use the requested file name and clean up
  11142. (copy-file pngfile tofile 'replace)
  11143. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11144. (delete-file (concat texfilebase e)))
  11145. pngfile))))
  11146. (defun org-dvipng-color (attr)
  11147. "Return an rgb color specification for dvipng."
  11148. (apply 'format "rgb %s %s %s"
  11149. (mapcar 'org-normalize-color
  11150. (color-values (face-attribute 'default attr nil)))))
  11151. (defun org-normalize-color (value)
  11152. "Return string to be used as color value for an RGB component."
  11153. (format "%g" (/ value 65535.0)))
  11154. ;;;; Key bindings
  11155. ;; Make `C-c C-x' a prefix key
  11156. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11157. ;; TAB key with modifiers
  11158. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11159. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11160. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11161. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11162. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11163. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11164. ;; The following line is necessary under Suse GNU/Linux
  11165. (unless (featurep 'xemacs)
  11166. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11167. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11168. (define-key org-mode-map [backtab] 'org-shifttab)
  11169. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11170. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11171. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11172. ;; Cursor keys with modifiers
  11173. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11174. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11175. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11176. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11177. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11178. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11179. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11180. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11181. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11182. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11183. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11184. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11185. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11186. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11187. ;;; Extra keys for tty access.
  11188. ;; We only set them when really needed because otherwise the
  11189. ;; menus don't show the simple keys
  11190. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11191. (not window-system))
  11192. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11193. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11194. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11195. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11196. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11197. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11198. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11199. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11200. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11201. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11202. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11203. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11204. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11205. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11206. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11207. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11208. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11209. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11210. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11211. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11212. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11213. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11214. ;; All the other keys
  11215. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11216. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11217. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  11218. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11219. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11220. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11221. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11222. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11223. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11224. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11225. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11226. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11227. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11228. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11229. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11230. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11231. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11232. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11233. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11234. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11235. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  11236. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11237. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11238. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11239. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11240. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11241. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11242. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11243. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11244. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11245. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11246. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11247. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11248. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11249. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11250. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11251. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11252. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11253. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11254. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11255. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11256. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11257. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11258. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11259. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11260. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11261. (org-defkey org-mode-map "\C-m" 'org-return)
  11262. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11263. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11264. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11265. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11266. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11267. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11268. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11269. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11270. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11271. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11272. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  11273. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11274. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11275. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11276. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11277. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11278. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  11279. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11280. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11281. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11282. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11283. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11284. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11285. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11286. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11287. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11288. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11289. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11290. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11291. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11292. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11293. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  11294. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11295. (when (featurep 'xemacs)
  11296. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11297. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11298. (defun org-self-insert-command (N)
  11299. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11300. If the cursor is in a table looking at whitespace, the whitespace is
  11301. overwritten, and the table is not marked as requiring realignment."
  11302. (interactive "p")
  11303. (if (and (org-table-p)
  11304. (progn
  11305. ;; check if we blank the field, and if that triggers align
  11306. (and (featurep 'org-table) org-table-auto-blank-field
  11307. (member last-command
  11308. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11309. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11310. ;; got extra space, this field does not determine column width
  11311. (let (org-table-may-need-update) (org-table-blank-field))
  11312. ;; no extra space, this field may determine column width
  11313. (org-table-blank-field)))
  11314. t)
  11315. (eq N 1)
  11316. (looking-at "[^|\n]* |"))
  11317. (let (org-table-may-need-update)
  11318. (goto-char (1- (match-end 0)))
  11319. (delete-backward-char 1)
  11320. (goto-char (match-beginning 0))
  11321. (self-insert-command N))
  11322. (setq org-table-may-need-update t)
  11323. (self-insert-command N)
  11324. (org-fix-tags-on-the-fly)))
  11325. (defun org-fix-tags-on-the-fly ()
  11326. (when (and (equal (char-after (point-at-bol)) ?*)
  11327. (org-on-heading-p))
  11328. (org-align-tags-here org-tags-column)))
  11329. (defun org-delete-backward-char (N)
  11330. "Like `delete-backward-char', insert whitespace at field end in tables.
  11331. When deleting backwards, in tables this function will insert whitespace in
  11332. front of the next \"|\" separator, to keep the table aligned. The table will
  11333. still be marked for re-alignment if the field did fill the entire column,
  11334. because, in this case the deletion might narrow the column."
  11335. (interactive "p")
  11336. (if (and (org-table-p)
  11337. (eq N 1)
  11338. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11339. (looking-at ".*?|"))
  11340. (let ((pos (point))
  11341. (noalign (looking-at "[^|\n\r]* |"))
  11342. (c org-table-may-need-update))
  11343. (backward-delete-char N)
  11344. (skip-chars-forward "^|")
  11345. (insert " ")
  11346. (goto-char (1- pos))
  11347. ;; noalign: if there were two spaces at the end, this field
  11348. ;; does not determine the width of the column.
  11349. (if noalign (setq org-table-may-need-update c)))
  11350. (backward-delete-char N)
  11351. (org-fix-tags-on-the-fly)))
  11352. (defun org-delete-char (N)
  11353. "Like `delete-char', but insert whitespace at field end in tables.
  11354. When deleting characters, in tables this function will insert whitespace in
  11355. front of the next \"|\" separator, to keep the table aligned. The table will
  11356. still be marked for re-alignment if the field did fill the entire column,
  11357. because, in this case the deletion might narrow the column."
  11358. (interactive "p")
  11359. (if (and (org-table-p)
  11360. (not (bolp))
  11361. (not (= (char-after) ?|))
  11362. (eq N 1))
  11363. (if (looking-at ".*?|")
  11364. (let ((pos (point))
  11365. (noalign (looking-at "[^|\n\r]* |"))
  11366. (c org-table-may-need-update))
  11367. (replace-match (concat
  11368. (substring (match-string 0) 1 -1)
  11369. " |"))
  11370. (goto-char pos)
  11371. ;; noalign: if there were two spaces at the end, this field
  11372. ;; does not determine the width of the column.
  11373. (if noalign (setq org-table-may-need-update c)))
  11374. (delete-char N))
  11375. (delete-char N)
  11376. (org-fix-tags-on-the-fly)))
  11377. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11378. (put 'org-self-insert-command 'delete-selection t)
  11379. (put 'orgtbl-self-insert-command 'delete-selection t)
  11380. (put 'org-delete-char 'delete-selection 'supersede)
  11381. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11382. ;; Make `flyspell-mode' delay after some commands
  11383. (put 'org-self-insert-command 'flyspell-delayed t)
  11384. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11385. (put 'org-delete-char 'flyspell-delayed t)
  11386. (put 'org-delete-backward-char 'flyspell-delayed t)
  11387. ;; Make pabbrev-mode expand after org-mode commands
  11388. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11389. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11390. ;; How to do this: Measure non-white length of current string
  11391. ;; If equal to column width, we should realign.
  11392. (defun org-remap (map &rest commands)
  11393. "In MAP, remap the functions given in COMMANDS.
  11394. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11395. (let (new old)
  11396. (while commands
  11397. (setq old (pop commands) new (pop commands))
  11398. (if (fboundp 'command-remapping)
  11399. (org-defkey map (vector 'remap old) new)
  11400. (substitute-key-definition old new map global-map)))))
  11401. (when (eq org-enable-table-editor 'optimized)
  11402. ;; If the user wants maximum table support, we need to hijack
  11403. ;; some standard editing functions
  11404. (org-remap org-mode-map
  11405. 'self-insert-command 'org-self-insert-command
  11406. 'delete-char 'org-delete-char
  11407. 'delete-backward-char 'org-delete-backward-char)
  11408. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11409. (defun org-shiftcursor-error ()
  11410. "Throw an error because Shift-Cursor command was applied in wrong context."
  11411. (error "This command is active in special context like tables, headlines or timestamps"))
  11412. (defun org-shifttab (&optional arg)
  11413. "Global visibility cycling or move to previous table field.
  11414. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11415. on context.
  11416. See the individual commands for more information."
  11417. (interactive "P")
  11418. (cond
  11419. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11420. ((integerp arg)
  11421. (message "Content view to level: %d" arg)
  11422. (org-content (prefix-numeric-value arg))
  11423. (setq org-cycle-global-status 'overview))
  11424. (t (call-interactively 'org-global-cycle))))
  11425. (defun org-shiftmetaleft ()
  11426. "Promote subtree or delete table column.
  11427. Calls `org-promote-subtree', `org-outdent-item',
  11428. or `org-table-delete-column', depending on context.
  11429. See the individual commands for more information."
  11430. (interactive)
  11431. (cond
  11432. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11433. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11434. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11435. (t (org-shiftcursor-error))))
  11436. (defun org-shiftmetaright ()
  11437. "Demote subtree or insert table column.
  11438. Calls `org-demote-subtree', `org-indent-item',
  11439. or `org-table-insert-column', depending on context.
  11440. See the individual commands for more information."
  11441. (interactive)
  11442. (cond
  11443. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11444. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11445. ((org-at-item-p) (call-interactively 'org-indent-item))
  11446. (t (org-shiftcursor-error))))
  11447. (defun org-shiftmetaup (&optional arg)
  11448. "Move subtree up or kill table row.
  11449. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11450. `org-move-item-up' depending on context. See the individual commands
  11451. for more information."
  11452. (interactive "P")
  11453. (cond
  11454. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11455. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11456. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11457. (t (org-shiftcursor-error))))
  11458. (defun org-shiftmetadown (&optional arg)
  11459. "Move subtree down or insert table row.
  11460. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11461. `org-move-item-down', depending on context. See the individual
  11462. commands for more information."
  11463. (interactive "P")
  11464. (cond
  11465. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11466. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11467. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11468. (t (org-shiftcursor-error))))
  11469. (defun org-metaleft (&optional arg)
  11470. "Promote heading or move table column to left.
  11471. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11472. With no specific context, calls the Emacs default `backward-word'.
  11473. See the individual commands for more information."
  11474. (interactive "P")
  11475. (cond
  11476. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11477. ((or (org-on-heading-p) (org-region-active-p))
  11478. (call-interactively 'org-do-promote))
  11479. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11480. (t (call-interactively 'backward-word))))
  11481. (defun org-metaright (&optional arg)
  11482. "Demote subtree or move table column to right.
  11483. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11484. With no specific context, calls the Emacs default `forward-word'.
  11485. See the individual commands for more information."
  11486. (interactive "P")
  11487. (cond
  11488. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11489. ((or (org-on-heading-p) (org-region-active-p))
  11490. (call-interactively 'org-do-demote))
  11491. ((org-at-item-p) (call-interactively 'org-indent-item))
  11492. (t (call-interactively 'forward-word))))
  11493. (defun org-metaup (&optional arg)
  11494. "Move subtree up or move table row up.
  11495. Calls `org-move-subtree-up' or `org-table-move-row' or
  11496. `org-move-item-up', depending on context. See the individual commands
  11497. for more information."
  11498. (interactive "P")
  11499. (cond
  11500. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11501. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11502. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11503. (t (transpose-lines 1) (beginning-of-line -1))))
  11504. (defun org-metadown (&optional arg)
  11505. "Move subtree down or move table row down.
  11506. Calls `org-move-subtree-down' or `org-table-move-row' or
  11507. `org-move-item-down', depending on context. See the individual
  11508. commands for more information."
  11509. (interactive "P")
  11510. (cond
  11511. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11512. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11513. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11514. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11515. (defun org-shiftup (&optional arg)
  11516. "Increase item in timestamp or increase priority of current headline.
  11517. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11518. depending on context. See the individual commands for more information."
  11519. (interactive "P")
  11520. (cond
  11521. ((org-at-timestamp-p t)
  11522. (call-interactively (if org-edit-timestamp-down-means-later
  11523. 'org-timestamp-down 'org-timestamp-up)))
  11524. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11525. ((org-at-item-p) (call-interactively 'org-previous-item))
  11526. ((org-clocktable-try-shift 'up arg))
  11527. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11528. (defun org-shiftdown (&optional arg)
  11529. "Decrease item in timestamp or decrease priority of current headline.
  11530. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11531. depending on context. See the individual commands for more information."
  11532. (interactive "P")
  11533. (cond
  11534. ((org-at-timestamp-p t)
  11535. (call-interactively (if org-edit-timestamp-down-means-later
  11536. 'org-timestamp-up 'org-timestamp-down)))
  11537. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11538. ((org-clocktable-try-shift 'down arg))
  11539. (t (call-interactively 'org-next-item))))
  11540. (defun org-shiftright (&optional arg)
  11541. "Next TODO keyword or timestamp one day later, depending on context."
  11542. (interactive "P")
  11543. (cond
  11544. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11545. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11546. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11547. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11548. ((org-clocktable-try-shift 'right arg))
  11549. (t (org-shiftcursor-error))))
  11550. (defun org-shiftleft (&optional arg)
  11551. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11552. (interactive "P")
  11553. (cond
  11554. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11555. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11556. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11557. ((org-at-property-p)
  11558. (call-interactively 'org-property-previous-allowed-value))
  11559. ((org-clocktable-try-shift 'left arg))
  11560. (t (org-shiftcursor-error))))
  11561. (defun org-shiftcontrolright ()
  11562. "Switch to next TODO set."
  11563. (interactive)
  11564. (cond
  11565. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11566. (t (org-shiftcursor-error))))
  11567. (defun org-shiftcontrolleft ()
  11568. "Switch to previous TODO set."
  11569. (interactive)
  11570. (cond
  11571. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11572. (t (org-shiftcursor-error))))
  11573. (defun org-ctrl-c-ret ()
  11574. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11575. (interactive)
  11576. (cond
  11577. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11578. (t (call-interactively 'org-insert-heading))))
  11579. (defun org-copy-special ()
  11580. "Copy region in table or copy current subtree.
  11581. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11582. See the individual commands for more information."
  11583. (interactive)
  11584. (call-interactively
  11585. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11586. (defun org-cut-special ()
  11587. "Cut region in table or cut current subtree.
  11588. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11589. See the individual commands for more information."
  11590. (interactive)
  11591. (call-interactively
  11592. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11593. (defun org-paste-special (arg)
  11594. "Paste rectangular region into table, or past subtree relative to level.
  11595. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11596. See the individual commands for more information."
  11597. (interactive "P")
  11598. (if (org-at-table-p)
  11599. (org-table-paste-rectangle)
  11600. (org-paste-subtree arg)))
  11601. (defun org-edit-special ()
  11602. "Call a special editor for the stuff at point.
  11603. When at a table, call the formula editor with `org-table-edit-formulas'.
  11604. When at the first line of an src example, call `org-edit-src-example'."
  11605. (interactive)
  11606. (if (org-at-table-p)
  11607. (call-interactively 'org-table-edit-formulas)
  11608. (or (org-edit-src-example)
  11609. (error "%s"
  11610. (substitute-command-keys
  11611. "\\[org-edit-special] can do nothing useful here.")))))
  11612. (defun org-ctrl-c-ctrl-c (&optional arg)
  11613. "Set tags in headline, or update according to changed information at point.
  11614. This command does many different things, depending on context:
  11615. - If the cursor is in a headline, prompt for tags and insert them
  11616. into the current line, aligned to `org-tags-column'. When called
  11617. with prefix arg, realign all tags in the current buffer.
  11618. - If the cursor is in one of the special #+KEYWORD lines, this
  11619. triggers scanning the buffer for these lines and updating the
  11620. information.
  11621. - If the cursor is inside a table, realign the table. This command
  11622. works even if the automatic table editor has been turned off.
  11623. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11624. the entire table.
  11625. - If the cursor is a the beginning of a dynamic block, update it.
  11626. - If the cursor is inside a table created by the table.el package,
  11627. activate that table.
  11628. - If the current buffer is a remember buffer, close note and file it.
  11629. with a prefix argument, file it without further interaction to the default
  11630. location.
  11631. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11632. links in this buffer.
  11633. - If the cursor is on a numbered item in a plain list, renumber the
  11634. ordered list.
  11635. - If the cursor is on a checkbox, toggle it."
  11636. (interactive "P")
  11637. (let ((org-enable-table-editor t))
  11638. (cond
  11639. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11640. org-occur-highlights
  11641. org-latex-fragment-image-overlays)
  11642. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11643. (org-remove-occur-highlights)
  11644. (org-remove-latex-fragment-image-overlays)
  11645. (message "Temporary highlights/overlays removed from current buffer"))
  11646. ((and (local-variable-p 'org-finish-function (current-buffer))
  11647. (fboundp org-finish-function))
  11648. (funcall org-finish-function))
  11649. ((org-at-property-p)
  11650. (call-interactively 'org-property-action))
  11651. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11652. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11653. ((org-at-table.el-p)
  11654. (require 'table)
  11655. (beginning-of-line 1)
  11656. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11657. (call-interactively 'table-recognize-table))
  11658. ((org-at-table-p)
  11659. (org-table-maybe-eval-formula)
  11660. (if arg
  11661. (call-interactively 'org-table-recalculate)
  11662. (org-table-maybe-recalculate-line))
  11663. (call-interactively 'org-table-align))
  11664. ((org-at-item-checkbox-p)
  11665. (call-interactively 'org-toggle-checkbox))
  11666. ((org-at-item-p)
  11667. (call-interactively 'org-maybe-renumber-ordered-list))
  11668. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11669. ;; Dynamic block
  11670. (beginning-of-line 1)
  11671. (org-update-dblock))
  11672. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11673. (cond
  11674. ((equal (match-string 1) "TBLFM")
  11675. ;; Recalculate the table before this line
  11676. (save-excursion
  11677. (beginning-of-line 1)
  11678. (skip-chars-backward " \r\n\t")
  11679. (if (org-at-table-p)
  11680. (org-call-with-arg 'org-table-recalculate t))))
  11681. (t
  11682. ; (org-set-regexps-and-options)
  11683. ; (org-restart-font-lock)
  11684. (let ((org-inhibit-startup t)) (org-mode-restart))
  11685. (message "Local setup has been refreshed"))))
  11686. (t (error "C-c C-c can do nothing useful at this location.")))))
  11687. (defun org-mode-restart ()
  11688. "Restart Org-mode, to scan again for special lines.
  11689. Also updates the keyword regular expressions."
  11690. (interactive)
  11691. (org-mode)
  11692. (message "Org-mode restarted"))
  11693. (defun org-kill-note-or-show-branches ()
  11694. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11695. (interactive)
  11696. (if (not org-finish-function)
  11697. (call-interactively 'show-branches)
  11698. (let ((org-note-abort t))
  11699. (funcall org-finish-function))))
  11700. (defun org-return (&optional indent)
  11701. "Goto next table row or insert a newline.
  11702. Calls `org-table-next-row' or `newline', depending on context.
  11703. See the individual commands for more information."
  11704. (interactive)
  11705. (cond
  11706. ((bobp) (if indent (newline-and-indent) (newline)))
  11707. ((and (org-at-heading-p)
  11708. (looking-at
  11709. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11710. (org-show-entry)
  11711. (end-of-line 1)
  11712. (newline))
  11713. ((org-at-table-p)
  11714. (org-table-justify-field-maybe)
  11715. (call-interactively 'org-table-next-row))
  11716. (t (if indent (newline-and-indent) (newline)))))
  11717. (defun org-return-indent ()
  11718. "Goto next table row or insert a newline and indent.
  11719. Calls `org-table-next-row' or `newline-and-indent', depending on
  11720. context. See the individual commands for more information."
  11721. (interactive)
  11722. (org-return t))
  11723. (defun org-ctrl-c-star ()
  11724. "Compute table, or change heading status of lines.
  11725. Calls `org-table-recalculate' or `org-toggle-region-headings',
  11726. depending on context. This will also turn a plain list item or a normal
  11727. line into a subheading."
  11728. (interactive)
  11729. (cond
  11730. ((org-at-table-p)
  11731. (call-interactively 'org-table-recalculate))
  11732. ((org-region-active-p)
  11733. ;; Convert all lines in region to list items
  11734. (call-interactively 'org-toggle-region-headings))
  11735. ((org-on-heading-p)
  11736. (org-toggle-region-headings (point-at-bol)
  11737. (min (1+ (point-at-eol)) (point-max))))
  11738. ((org-at-item-p)
  11739. ;; Convert to heading
  11740. (let ((level (save-match-data
  11741. (save-excursion
  11742. (condition-case nil
  11743. (progn
  11744. (org-back-to-heading t)
  11745. (funcall outline-level))
  11746. (error 0))))))
  11747. (replace-match
  11748. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11749. (t (org-toggle-region-headings (point-at-bol)
  11750. (min (1+ (point-at-eol)) (point-max))))))
  11751. (defun org-ctrl-c-minus ()
  11752. "Insert separator line in table or modify bullet status of line.
  11753. Also turns a plain line or a region of lines into list items.
  11754. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11755. `org-cycle-list-bullet', depending on context."
  11756. (interactive)
  11757. (cond
  11758. ((org-at-table-p)
  11759. (call-interactively 'org-table-insert-hline))
  11760. ((org-on-heading-p)
  11761. ;; Convert to item
  11762. (save-excursion
  11763. (beginning-of-line 1)
  11764. (if (looking-at "\\*+ ")
  11765. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11766. ((org-region-active-p)
  11767. ;; Convert all lines in region to list items
  11768. (call-interactively 'org-toggle-region-items))
  11769. ((org-in-item-p)
  11770. (call-interactively 'org-cycle-list-bullet))
  11771. (t (org-toggle-region-items (point-at-bol)
  11772. (min (1+ (point-at-eol)) (point-max))))))
  11773. (defun org-toggle-region-items (beg end)
  11774. "Convert all lines in region to list items.
  11775. If the first line is already an item, convert all list items in the region
  11776. to normal lines."
  11777. (interactive "r")
  11778. (let (l2 l)
  11779. (save-excursion
  11780. (goto-char end)
  11781. (setq l2 (org-current-line))
  11782. (goto-char beg)
  11783. (beginning-of-line 1)
  11784. (setq l (1- (org-current-line)))
  11785. (if (org-at-item-p)
  11786. ;; We already have items, de-itemize
  11787. (while (< (setq l (1+ l)) l2)
  11788. (when (org-at-item-p)
  11789. (goto-char (match-beginning 2))
  11790. (delete-region (match-beginning 2) (match-end 2))
  11791. (and (looking-at "[ \t]+") (replace-match "")))
  11792. (beginning-of-line 2))
  11793. (while (< (setq l (1+ l)) l2)
  11794. (unless (org-at-item-p)
  11795. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11796. (replace-match "\\1- \\2")))
  11797. (beginning-of-line 2))))))
  11798. (defun org-toggle-region-headings (beg end)
  11799. "Convert all lines in region to list items.
  11800. If the first line is already an item, convert all list items in the region
  11801. to normal lines."
  11802. (interactive "r")
  11803. (let (l2 l)
  11804. (save-excursion
  11805. (goto-char end)
  11806. (setq l2 (org-current-line))
  11807. (goto-char beg)
  11808. (beginning-of-line 1)
  11809. (setq l (1- (org-current-line)))
  11810. (if (org-on-heading-p)
  11811. ;; We already have headlines, de-star them
  11812. (while (< (setq l (1+ l)) l2)
  11813. (when (org-on-heading-p t)
  11814. (and (looking-at outline-regexp) (replace-match "")))
  11815. (beginning-of-line 2))
  11816. (let* ((stars (save-excursion
  11817. (re-search-backward org-complex-heading-regexp nil t)
  11818. (or (match-string 1) "*")))
  11819. (add-stars (if org-odd-levels-only "**" "*"))
  11820. (rpl (concat stars add-stars " \\2")))
  11821. (while (< (setq l (1+ l)) l2)
  11822. (unless (org-on-heading-p)
  11823. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11824. (replace-match rpl)))
  11825. (beginning-of-line 2)))))))
  11826. (defun org-meta-return (&optional arg)
  11827. "Insert a new heading or wrap a region in a table.
  11828. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11829. See the individual commands for more information."
  11830. (interactive "P")
  11831. (cond
  11832. ((org-at-table-p)
  11833. (call-interactively 'org-table-wrap-region))
  11834. (t (call-interactively 'org-insert-heading))))
  11835. ;;; Menu entries
  11836. ;; Define the Org-mode menus
  11837. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11838. '("Tbl"
  11839. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11840. ["Next Field" org-cycle (org-at-table-p)]
  11841. ["Previous Field" org-shifttab (org-at-table-p)]
  11842. ["Next Row" org-return (org-at-table-p)]
  11843. "--"
  11844. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11845. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11846. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11847. "--"
  11848. ("Column"
  11849. ["Move Column Left" org-metaleft (org-at-table-p)]
  11850. ["Move Column Right" org-metaright (org-at-table-p)]
  11851. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11852. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11853. ("Row"
  11854. ["Move Row Up" org-metaup (org-at-table-p)]
  11855. ["Move Row Down" org-metadown (org-at-table-p)]
  11856. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11857. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11858. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11859. "--"
  11860. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11861. ("Rectangle"
  11862. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11863. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11864. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11865. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11866. "--"
  11867. ("Calculate"
  11868. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11869. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11870. ["Edit Formulas" org-edit-special (org-at-table-p)]
  11871. "--"
  11872. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11873. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11874. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11875. "--"
  11876. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11877. "--"
  11878. ["Sum Column/Rectangle" org-table-sum
  11879. (or (org-at-table-p) (org-region-active-p))]
  11880. ["Which Column?" org-table-current-column (org-at-table-p)])
  11881. ["Debug Formulas"
  11882. org-table-toggle-formula-debugger
  11883. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11884. ["Show Col/Row Numbers"
  11885. org-table-toggle-coordinate-overlays
  11886. :style toggle
  11887. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11888. "--"
  11889. ["Create" org-table-create (and (not (org-at-table-p))
  11890. org-enable-table-editor)]
  11891. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11892. ["Import from File" org-table-import (not (org-at-table-p))]
  11893. ["Export to File" org-table-export (org-at-table-p)]
  11894. "--"
  11895. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11896. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11897. '("Org"
  11898. ("Show/Hide"
  11899. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11900. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11901. ["Sparse Tree..." org-sparse-tree t]
  11902. ["Reveal Context" org-reveal t]
  11903. ["Show All" show-all t]
  11904. "--"
  11905. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11906. "--"
  11907. ["New Heading" org-insert-heading t]
  11908. ("Navigate Headings"
  11909. ["Up" outline-up-heading t]
  11910. ["Next" outline-next-visible-heading t]
  11911. ["Previous" outline-previous-visible-heading t]
  11912. ["Next Same Level" outline-forward-same-level t]
  11913. ["Previous Same Level" outline-backward-same-level t]
  11914. "--"
  11915. ["Jump" org-goto t])
  11916. ("Edit Structure"
  11917. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11918. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11919. "--"
  11920. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11921. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11922. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11923. "--"
  11924. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11925. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11926. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11927. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11928. "--"
  11929. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11930. "--"
  11931. ["Convert to odd levels" org-convert-to-odd-levels t]
  11932. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11933. ("Editing"
  11934. ["Emphasis..." org-emphasize t]
  11935. ["Edit Source Example" org-edit-special t])
  11936. ("Archive"
  11937. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11938. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11939. ; :active t :keys "C-u C-c C-x C-a"]
  11940. ["Sparse trees open ARCHIVE trees"
  11941. (setq org-sparse-tree-open-archived-trees
  11942. (not org-sparse-tree-open-archived-trees))
  11943. :style toggle :selected org-sparse-tree-open-archived-trees]
  11944. ["Cycling opens ARCHIVE trees"
  11945. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11946. :style toggle :selected org-cycle-open-archived-trees]
  11947. ["Agenda includes ARCHIVE trees"
  11948. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  11949. :style toggle :selected (not org-agenda-skip-archived-trees)]
  11950. "--"
  11951. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11952. ; ["Check and Move Children" (org-archive-subtree '(4))
  11953. ; :active t :keys "C-u C-c C-x C-s"]
  11954. )
  11955. "--"
  11956. ("TODO Lists"
  11957. ["TODO/DONE/-" org-todo t]
  11958. ("Select keyword"
  11959. ["Next keyword" org-shiftright (org-on-heading-p)]
  11960. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11961. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11962. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11963. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11964. ["Show TODO Tree" org-show-todo-tree t]
  11965. ["Global TODO list" org-todo-list t]
  11966. "--"
  11967. ["Set Priority" org-priority t]
  11968. ["Priority Up" org-shiftup t]
  11969. ["Priority Down" org-shiftdown t])
  11970. ("TAGS and Properties"
  11971. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  11972. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11973. "--"
  11974. ["Set property" 'org-set-property t]
  11975. ["Column view of properties" org-columns t]
  11976. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11977. ("Dates and Scheduling"
  11978. ["Timestamp" org-time-stamp t]
  11979. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11980. ("Change Date"
  11981. ["1 Day Later" org-shiftright t]
  11982. ["1 Day Earlier" org-shiftleft t]
  11983. ["1 ... Later" org-shiftup t]
  11984. ["1 ... Earlier" org-shiftdown t])
  11985. ["Compute Time Range" org-evaluate-time-range t]
  11986. ["Schedule Item" org-schedule t]
  11987. ["Deadline" org-deadline t]
  11988. "--"
  11989. ["Custom time format" org-toggle-time-stamp-overlays
  11990. :style radio :selected org-display-custom-times]
  11991. "--"
  11992. ["Goto Calendar" org-goto-calendar t]
  11993. ["Date from Calendar" org-date-from-calendar t])
  11994. ("Logging work"
  11995. ["Clock in" org-clock-in t]
  11996. ["Clock out" org-clock-out t]
  11997. ["Clock cancel" org-clock-cancel t]
  11998. ["Goto running clock" org-clock-goto t]
  11999. ["Display times" org-clock-display t]
  12000. ["Create clock table" org-clock-report t]
  12001. "--"
  12002. ["Record DONE time"
  12003. (progn (setq org-log-done (not org-log-done))
  12004. (message "Switching to %s will %s record a timestamp"
  12005. (car org-done-keywords)
  12006. (if org-log-done "automatically" "not")))
  12007. :style toggle :selected org-log-done])
  12008. "--"
  12009. ["Agenda Command..." org-agenda t]
  12010. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12011. ("File List for Agenda")
  12012. ("Special views current file"
  12013. ["TODO Tree" org-show-todo-tree t]
  12014. ["Check Deadlines" org-check-deadlines t]
  12015. ["Timeline" org-timeline t]
  12016. ["Tags Tree" org-tags-sparse-tree t])
  12017. "--"
  12018. ("Hyperlinks"
  12019. ["Store Link (Global)" org-store-link t]
  12020. ["Insert Link" org-insert-link t]
  12021. ["Follow Link" org-open-at-point t]
  12022. "--"
  12023. ["Next link" org-next-link t]
  12024. ["Previous link" org-previous-link t]
  12025. "--"
  12026. ["Descriptive Links"
  12027. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12028. :style radio
  12029. :selected (member '(org-link) buffer-invisibility-spec)]
  12030. ["Literal Links"
  12031. (progn
  12032. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12033. :style radio
  12034. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12035. "--"
  12036. ["Export/Publish..." org-export t]
  12037. ("LaTeX"
  12038. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12039. :selected org-cdlatex-mode]
  12040. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12041. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12042. ["Modify math symbol" org-cdlatex-math-modify
  12043. (org-inside-LaTeX-fragment-p)]
  12044. ["Export LaTeX fragments as images"
  12045. (if (featurep 'org-exp)
  12046. (setq org-export-with-LaTeX-fragments
  12047. (not org-export-with-LaTeX-fragments))
  12048. (require 'org-exp))
  12049. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12050. org-export-with-LaTeX-fragments)])
  12051. "--"
  12052. ("Documentation"
  12053. ["Show Version" org-version t]
  12054. ["Info Documentation" org-info t])
  12055. ("Customize"
  12056. ["Browse Org Group" org-customize t]
  12057. "--"
  12058. ["Expand This Menu" org-create-customize-menu
  12059. (fboundp 'customize-menu-create)])
  12060. "--"
  12061. ["Refresh setup" org-mode-restart t]
  12062. ))
  12063. (defun org-info (&optional node)
  12064. "Read documentation for Org-mode in the info system.
  12065. With optional NODE, go directly to that node."
  12066. (interactive)
  12067. (info (format "(org)%s" (or node ""))))
  12068. (defun org-install-agenda-files-menu ()
  12069. (let ((bl (buffer-list)))
  12070. (save-excursion
  12071. (while bl
  12072. (set-buffer (pop bl))
  12073. (if (org-mode-p) (setq bl nil)))
  12074. (when (org-mode-p)
  12075. (easy-menu-change
  12076. '("Org") "File List for Agenda"
  12077. (append
  12078. (list
  12079. ["Edit File List" (org-edit-agenda-file-list) t]
  12080. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12081. ["Remove Current File from List" org-remove-file t]
  12082. ["Cycle through agenda files" org-cycle-agenda-files t]
  12083. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12084. "--")
  12085. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12086. ;;;; Documentation
  12087. (defun org-require-autoloaded-modules ()
  12088. (interactive)
  12089. (mapc 'require
  12090. '(org-agenda org-archive org-clock org-colview
  12091. org-exp org-export-latex org-publish
  12092. org-remember org-table)))
  12093. (defun org-customize ()
  12094. "Call the customize function with org as argument."
  12095. (interactive)
  12096. (org-load-modules-maybe)
  12097. (org-require-autoloaded-modules)
  12098. (customize-browse 'org))
  12099. (defun org-create-customize-menu ()
  12100. "Create a full customization menu for Org-mode, insert it into the menu."
  12101. (interactive)
  12102. (org-load-modules-maybe)
  12103. (org-require-autoloaded-modules)
  12104. (if (fboundp 'customize-menu-create)
  12105. (progn
  12106. (easy-menu-change
  12107. '("Org") "Customize"
  12108. `(["Browse Org group" org-customize t]
  12109. "--"
  12110. ,(customize-menu-create 'org)
  12111. ["Set" Custom-set t]
  12112. ["Save" Custom-save t]
  12113. ["Reset to Current" Custom-reset-current t]
  12114. ["Reset to Saved" Custom-reset-saved t]
  12115. ["Reset to Standard Settings" Custom-reset-standard t]))
  12116. (message "\"Org\"-menu now contains full customization menu"))
  12117. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12118. ;;;; Miscellaneous stuff
  12119. ;;; Generally useful functions
  12120. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12121. "Display the given MESSAGE as a warning."
  12122. (if (fboundp 'display-warning)
  12123. (display-warning 'org message
  12124. (if (featurep 'xemacs)
  12125. 'warning
  12126. :warning))
  12127. (let ((buf (get-buffer-create "*Org warnings*")))
  12128. (with-current-buffer buf
  12129. (goto-char (point-max))
  12130. (insert "Warning (Org): " message)
  12131. (unless (bolp)
  12132. (newline)))
  12133. (display-buffer buf)
  12134. (sit-for 0))))
  12135. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12136. "Go to MARKER, widen if necesary. When marker is not live, try BOOKMARK."
  12137. (if (and marker (marker-buffer marker)
  12138. (buffer-live-p (marker-buffer marker)))
  12139. (progn
  12140. (switch-to-buffer (marker-buffer marker))
  12141. (if (or (> marker (point-max)) (< marker (point-min)))
  12142. (widen))
  12143. (goto-char marker))
  12144. (if bookmark
  12145. (bookmark-jump bookmark)
  12146. (error "Cannot find location"))))
  12147. (defun org-quote-csv-field (s)
  12148. "Quote field for inclusion in CSV material."
  12149. (if (string-match "[\",]" s)
  12150. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12151. s))
  12152. (defun org-plist-delete (plist property)
  12153. "Delete PROPERTY from PLIST.
  12154. This is in contrast to merely setting it to 0."
  12155. (let (p)
  12156. (while plist
  12157. (if (not (eq property (car plist)))
  12158. (setq p (plist-put p (car plist) (nth 1 plist))))
  12159. (setq plist (cddr plist)))
  12160. p))
  12161. (defun org-force-self-insert (N)
  12162. "Needed to enforce self-insert under remapping."
  12163. (interactive "p")
  12164. (self-insert-command N))
  12165. (defun org-string-width (s)
  12166. "Compute width of string, ignoring invisible characters.
  12167. This ignores character with invisibility property `org-link', and also
  12168. characters with property `org-cwidth', because these will become invisible
  12169. upon the next fontification round."
  12170. (let (b l)
  12171. (when (or (eq t buffer-invisibility-spec)
  12172. (assq 'org-link buffer-invisibility-spec))
  12173. (while (setq b (text-property-any 0 (length s)
  12174. 'invisible 'org-link s))
  12175. (setq s (concat (substring s 0 b)
  12176. (substring s (or (next-single-property-change
  12177. b 'invisible s) (length s)))))))
  12178. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12179. (setq s (concat (substring s 0 b)
  12180. (substring s (or (next-single-property-change
  12181. b 'org-cwidth s) (length s))))))
  12182. (setq l (string-width s) b -1)
  12183. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12184. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12185. l))
  12186. (defun org-base-buffer (buffer)
  12187. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12188. (if (not buffer)
  12189. buffer
  12190. (or (buffer-base-buffer buffer)
  12191. buffer)))
  12192. (defun org-trim (s)
  12193. "Remove whitespace at beginning and end of string."
  12194. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12195. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12196. s)
  12197. (defun org-wrap (string &optional width lines)
  12198. "Wrap string to either a number of lines, or a width in characters.
  12199. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12200. that costs. If there is a word longer than WIDTH, the text is actually
  12201. wrapped to the length of that word.
  12202. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12203. many lines, whatever width that takes.
  12204. The return value is a list of lines, without newlines at the end."
  12205. (let* ((words (org-split-string string "[ \t\n]+"))
  12206. (maxword (apply 'max (mapcar 'org-string-width words)))
  12207. w ll)
  12208. (cond (width
  12209. (org-do-wrap words (max maxword width)))
  12210. (lines
  12211. (setq w maxword)
  12212. (setq ll (org-do-wrap words maxword))
  12213. (if (<= (length ll) lines)
  12214. ll
  12215. (setq ll words)
  12216. (while (> (length ll) lines)
  12217. (setq w (1+ w))
  12218. (setq ll (org-do-wrap words w)))
  12219. ll))
  12220. (t (error "Cannot wrap this")))))
  12221. (defun org-do-wrap (words width)
  12222. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12223. (let (lines line)
  12224. (while words
  12225. (setq line (pop words))
  12226. (while (and words (< (+ (length line) (length (car words))) width))
  12227. (setq line (concat line " " (pop words))))
  12228. (setq lines (push line lines)))
  12229. (nreverse lines)))
  12230. (defun org-split-string (string &optional separators)
  12231. "Splits STRING into substrings at SEPARATORS.
  12232. No empty strings are returned if there are matches at the beginning
  12233. and end of string."
  12234. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12235. (start 0)
  12236. notfirst
  12237. (list nil))
  12238. (while (and (string-match rexp string
  12239. (if (and notfirst
  12240. (= start (match-beginning 0))
  12241. (< start (length string)))
  12242. (1+ start) start))
  12243. (< (match-beginning 0) (length string)))
  12244. (setq notfirst t)
  12245. (or (eq (match-beginning 0) 0)
  12246. (and (eq (match-beginning 0) (match-end 0))
  12247. (eq (match-beginning 0) start))
  12248. (setq list
  12249. (cons (substring string start (match-beginning 0))
  12250. list)))
  12251. (setq start (match-end 0)))
  12252. (or (eq start (length string))
  12253. (setq list
  12254. (cons (substring string start)
  12255. list)))
  12256. (nreverse list)))
  12257. (defun org-context ()
  12258. "Return a list of contexts of the current cursor position.
  12259. If several contexts apply, all are returned.
  12260. Each context entry is a list with a symbol naming the context, and
  12261. two positions indicating start and end of the context. Possible
  12262. contexts are:
  12263. :headline anywhere in a headline
  12264. :headline-stars on the leading stars in a headline
  12265. :todo-keyword on a TODO keyword (including DONE) in a headline
  12266. :tags on the TAGS in a headline
  12267. :priority on the priority cookie in a headline
  12268. :item on the first line of a plain list item
  12269. :item-bullet on the bullet/number of a plain list item
  12270. :checkbox on the checkbox in a plain list item
  12271. :table in an org-mode table
  12272. :table-special on a special filed in a table
  12273. :table-table in a table.el table
  12274. :link on a hyperlink
  12275. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12276. :target on a <<target>>
  12277. :radio-target on a <<<radio-target>>>
  12278. :latex-fragment on a LaTeX fragment
  12279. :latex-preview on a LaTeX fragment with overlayed preview image
  12280. This function expects the position to be visible because it uses font-lock
  12281. faces as a help to recognize the following contexts: :table-special, :link,
  12282. and :keyword."
  12283. (let* ((f (get-text-property (point) 'face))
  12284. (faces (if (listp f) f (list f)))
  12285. (p (point)) clist o)
  12286. ;; First the large context
  12287. (cond
  12288. ((org-on-heading-p t)
  12289. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12290. (when (progn
  12291. (beginning-of-line 1)
  12292. (looking-at org-todo-line-tags-regexp))
  12293. (push (org-point-in-group p 1 :headline-stars) clist)
  12294. (push (org-point-in-group p 2 :todo-keyword) clist)
  12295. (push (org-point-in-group p 4 :tags) clist))
  12296. (goto-char p)
  12297. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12298. (if (looking-at "\\[#[A-Z0-9]\\]")
  12299. (push (org-point-in-group p 0 :priority) clist)))
  12300. ((org-at-item-p)
  12301. (push (org-point-in-group p 2 :item-bullet) clist)
  12302. (push (list :item (point-at-bol)
  12303. (save-excursion (org-end-of-item) (point)))
  12304. clist)
  12305. (and (org-at-item-checkbox-p)
  12306. (push (org-point-in-group p 0 :checkbox) clist)))
  12307. ((org-at-table-p)
  12308. (push (list :table (org-table-begin) (org-table-end)) clist)
  12309. (if (memq 'org-formula faces)
  12310. (push (list :table-special
  12311. (previous-single-property-change p 'face)
  12312. (next-single-property-change p 'face)) clist)))
  12313. ((org-at-table-p 'any)
  12314. (push (list :table-table) clist)))
  12315. (goto-char p)
  12316. ;; Now the small context
  12317. (cond
  12318. ((org-at-timestamp-p)
  12319. (push (org-point-in-group p 0 :timestamp) clist))
  12320. ((memq 'org-link faces)
  12321. (push (list :link
  12322. (previous-single-property-change p 'face)
  12323. (next-single-property-change p 'face)) clist))
  12324. ((memq 'org-special-keyword faces)
  12325. (push (list :keyword
  12326. (previous-single-property-change p 'face)
  12327. (next-single-property-change p 'face)) clist))
  12328. ((org-on-target-p)
  12329. (push (org-point-in-group p 0 :target) clist)
  12330. (goto-char (1- (match-beginning 0)))
  12331. (if (looking-at org-radio-target-regexp)
  12332. (push (org-point-in-group p 0 :radio-target) clist))
  12333. (goto-char p))
  12334. ((setq o (car (delq nil
  12335. (mapcar
  12336. (lambda (x)
  12337. (if (memq x org-latex-fragment-image-overlays) x))
  12338. (org-overlays-at (point))))))
  12339. (push (list :latex-fragment
  12340. (org-overlay-start o) (org-overlay-end o)) clist)
  12341. (push (list :latex-preview
  12342. (org-overlay-start o) (org-overlay-end o)) clist))
  12343. ((org-inside-LaTeX-fragment-p)
  12344. ;; FIXME: positions wrong.
  12345. (push (list :latex-fragment (point) (point)) clist)))
  12346. (setq clist (nreverse (delq nil clist)))
  12347. clist))
  12348. ;; FIXME: Compare with at-regexp-p Do we need both?
  12349. (defun org-in-regexp (re &optional nlines visually)
  12350. "Check if point is inside a match of regexp.
  12351. Normally only the current line is checked, but you can include NLINES extra
  12352. lines both before and after point into the search.
  12353. If VISUALLY is set, require that the cursor is not after the match but
  12354. really on, so that the block visually is on the match."
  12355. (catch 'exit
  12356. (let ((pos (point))
  12357. (eol (point-at-eol (+ 1 (or nlines 0))))
  12358. (inc (if visually 1 0)))
  12359. (save-excursion
  12360. (beginning-of-line (- 1 (or nlines 0)))
  12361. (while (re-search-forward re eol t)
  12362. (if (and (<= (match-beginning 0) pos)
  12363. (>= (+ inc (match-end 0)) pos))
  12364. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12365. (defun org-at-regexp-p (regexp)
  12366. "Is point inside a match of REGEXP in the current line?"
  12367. (catch 'exit
  12368. (save-excursion
  12369. (let ((pos (point)) (end (point-at-eol)))
  12370. (beginning-of-line 1)
  12371. (while (re-search-forward regexp end t)
  12372. (if (and (<= (match-beginning 0) pos)
  12373. (>= (match-end 0) pos))
  12374. (throw 'exit t)))
  12375. nil))))
  12376. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12377. "Call `multi-occur' with buffers for all agenda files."
  12378. (interactive "sOrg-files matching: \np")
  12379. (let* ((files (org-agenda-files))
  12380. (tnames (mapcar 'file-truename files))
  12381. (extra org-agenda-text-search-extra-files)
  12382. f)
  12383. (when (eq (car extra) 'agenda-archives)
  12384. (setq extra (cdr extra))
  12385. (setq files (org-add-archive-files files)))
  12386. (while (setq f (pop extra))
  12387. (unless (member (file-truename f) tnames)
  12388. (add-to-list 'files f 'append)
  12389. (add-to-list 'tnames (file-truename f) 'append)))
  12390. (multi-occur
  12391. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12392. regexp)))
  12393. (if (boundp 'occur-mode-find-occurrence-hook)
  12394. ;; Emacs 23
  12395. (add-hook 'occur-mode-find-occurrence-hook
  12396. (lambda ()
  12397. (when (org-mode-p)
  12398. (org-reveal))))
  12399. ;; Emacs 22
  12400. (defadvice occur-mode-goto-occurrence
  12401. (after org-occur-reveal activate)
  12402. (and (org-mode-p) (org-reveal)))
  12403. (defadvice occur-mode-goto-occurrence-other-window
  12404. (after org-occur-reveal activate)
  12405. (and (org-mode-p) (org-reveal)))
  12406. (defadvice occur-mode-display-occurrence
  12407. (after org-occur-reveal activate)
  12408. (when (org-mode-p)
  12409. (let ((pos (occur-mode-find-occurrence)))
  12410. (with-current-buffer (marker-buffer pos)
  12411. (save-excursion
  12412. (goto-char pos)
  12413. (org-reveal)))))))
  12414. (defun org-uniquify (list)
  12415. "Remove duplicate elements from LIST."
  12416. (let (res)
  12417. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12418. res))
  12419. (defun org-delete-all (elts list)
  12420. "Remove all elements in ELTS from LIST."
  12421. (while elts
  12422. (setq list (delete (pop elts) list)))
  12423. list)
  12424. (defun org-back-over-empty-lines ()
  12425. "Move backwards over witespace, to the beginning of the first empty line.
  12426. Returns the number of empty lines passed."
  12427. (let ((pos (point)))
  12428. (skip-chars-backward " \t\n\r")
  12429. (beginning-of-line 2)
  12430. (goto-char (min (point) pos))
  12431. (count-lines (point) pos)))
  12432. (defun org-skip-whitespace ()
  12433. (skip-chars-forward " \t\n\r"))
  12434. (defun org-point-in-group (point group &optional context)
  12435. "Check if POINT is in match-group GROUP.
  12436. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12437. match. If the match group does ot exist or point is not inside it,
  12438. return nil."
  12439. (and (match-beginning group)
  12440. (>= point (match-beginning group))
  12441. (<= point (match-end group))
  12442. (if context
  12443. (list context (match-beginning group) (match-end group))
  12444. t)))
  12445. (defun org-switch-to-buffer-other-window (&rest args)
  12446. "Switch to buffer in a second window on the current frame.
  12447. In particular, do not allow pop-up frames."
  12448. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12449. special-display-function)
  12450. (apply 'switch-to-buffer-other-window args)))
  12451. (defun org-combine-plists (&rest plists)
  12452. "Create a single property list from all plists in PLISTS.
  12453. The process starts by copying the first list, and then setting properties
  12454. from the other lists. Settings in the last list are the most significant
  12455. ones and overrule settings in the other lists."
  12456. (let ((rtn (copy-sequence (pop plists)))
  12457. p v ls)
  12458. (while plists
  12459. (setq ls (pop plists))
  12460. (while ls
  12461. (setq p (pop ls) v (pop ls))
  12462. (setq rtn (plist-put rtn p v))))
  12463. rtn))
  12464. (defun org-move-line-down (arg)
  12465. "Move the current line down. With prefix argument, move it past ARG lines."
  12466. (interactive "p")
  12467. (let ((col (current-column))
  12468. beg end pos)
  12469. (beginning-of-line 1) (setq beg (point))
  12470. (beginning-of-line 2) (setq end (point))
  12471. (beginning-of-line (+ 1 arg))
  12472. (setq pos (move-marker (make-marker) (point)))
  12473. (insert (delete-and-extract-region beg end))
  12474. (goto-char pos)
  12475. (org-move-to-column col)))
  12476. (defun org-move-line-up (arg)
  12477. "Move the current line up. With prefix argument, move it past ARG lines."
  12478. (interactive "p")
  12479. (let ((col (current-column))
  12480. beg end pos)
  12481. (beginning-of-line 1) (setq beg (point))
  12482. (beginning-of-line 2) (setq end (point))
  12483. (beginning-of-line (- arg))
  12484. (setq pos (move-marker (make-marker) (point)))
  12485. (insert (delete-and-extract-region beg end))
  12486. (goto-char pos)
  12487. (org-move-to-column col)))
  12488. (defun org-replace-escapes (string table)
  12489. "Replace %-escapes in STRING with values in TABLE.
  12490. TABLE is an association list with keys like \"%a\" and string values.
  12491. The sequences in STRING may contain normal field width and padding information,
  12492. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12493. so values can contain further %-escapes if they are define later in TABLE."
  12494. (let ((case-fold-search nil)
  12495. e re rpl)
  12496. (while (setq e (pop table))
  12497. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12498. (while (string-match re string)
  12499. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12500. (cdr e)))
  12501. (setq string (replace-match rpl t t string))))
  12502. string))
  12503. (defun org-sublist (list start end)
  12504. "Return a section of LIST, from START to END.
  12505. Counting starts at 1."
  12506. (let (rtn (c start))
  12507. (setq list (nthcdr (1- start) list))
  12508. (while (and list (<= c end))
  12509. (push (pop list) rtn)
  12510. (setq c (1+ c)))
  12511. (nreverse rtn)))
  12512. (defun org-find-base-buffer-visiting (file)
  12513. "Like `find-buffer-visiting' but alway return the base buffer and
  12514. not an indirect buffer."
  12515. (let ((buf (find-buffer-visiting file)))
  12516. (if buf
  12517. (or (buffer-base-buffer buf) buf)
  12518. nil)))
  12519. (defun org-image-file-name-regexp ()
  12520. "Return regexp matching the file names of images."
  12521. (if (fboundp 'image-file-name-regexp)
  12522. (image-file-name-regexp)
  12523. (let ((image-file-name-extensions
  12524. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12525. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12526. (concat "\\."
  12527. (regexp-opt (nconc (mapcar 'upcase
  12528. image-file-name-extensions)
  12529. image-file-name-extensions)
  12530. t)
  12531. "\\'"))))
  12532. (defun org-file-image-p (file)
  12533. "Return non-nil if FILE is an image."
  12534. (save-match-data
  12535. (string-match (org-image-file-name-regexp) file)))
  12536. ;;; Paragraph filling stuff.
  12537. ;; We want this to be just right, so use the full arsenal.
  12538. (defun org-indent-line-function ()
  12539. "Indent line like previous, but further if previous was headline or item."
  12540. (interactive)
  12541. (let* ((pos (point))
  12542. (itemp (org-at-item-p))
  12543. column bpos bcol tpos tcol bullet btype bullet-type)
  12544. ;; Find the previous relevant line
  12545. (beginning-of-line 1)
  12546. (cond
  12547. ((looking-at "#") (setq column 0))
  12548. ((looking-at "\\*+ ") (setq column 0))
  12549. (t
  12550. (beginning-of-line 0)
  12551. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12552. (beginning-of-line 0))
  12553. (cond
  12554. ((looking-at "\\*+[ \t]+")
  12555. (if (not org-adapt-indentation)
  12556. (setq column 0)
  12557. (goto-char (match-end 0))
  12558. (setq column (current-column))))
  12559. ((org-in-item-p)
  12560. (org-beginning-of-item)
  12561. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12562. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12563. (setq bpos (match-beginning 1) tpos (match-end 0)
  12564. bcol (progn (goto-char bpos) (current-column))
  12565. tcol (progn (goto-char tpos) (current-column))
  12566. bullet (match-string 1)
  12567. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12568. (if (> tcol (+ bcol org-description-max-indent))
  12569. (setq tcol (+ bcol 5)))
  12570. (if (not itemp)
  12571. (setq column tcol)
  12572. (goto-char pos)
  12573. (beginning-of-line 1)
  12574. (if (looking-at "\\S-")
  12575. (progn
  12576. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12577. (setq bullet (match-string 1)
  12578. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12579. (setq column (if (equal btype bullet-type) bcol tcol)))
  12580. (setq column (org-get-indentation)))))
  12581. (t (setq column (org-get-indentation))))))
  12582. (goto-char pos)
  12583. (if (<= (current-column) (current-indentation))
  12584. (org-indent-line-to column)
  12585. (save-excursion (org-indent-line-to column)))
  12586. (setq column (current-column))
  12587. (beginning-of-line 1)
  12588. (if (looking-at
  12589. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12590. (replace-match (concat "\\1" (format org-property-format
  12591. (match-string 2) (match-string 3)))
  12592. t nil))
  12593. (org-move-to-column column)))
  12594. (defun org-set-autofill-regexps ()
  12595. (interactive)
  12596. ;; In the paragraph separator we include headlines, because filling
  12597. ;; text in a line directly attached to a headline would otherwise
  12598. ;; fill the headline as well.
  12599. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12600. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12601. ;; The paragraph starter includes hand-formatted lists.
  12602. (org-set-local 'paragraph-start
  12603. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12604. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12605. ;; But only if the user has not turned off tables or fixed-width regions
  12606. (org-set-local
  12607. 'auto-fill-inhibit-regexp
  12608. (concat "\\*+ \\|#\\+"
  12609. "\\|[ \t]*" org-keyword-time-regexp
  12610. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12611. (concat
  12612. "\\|[ \t]*["
  12613. (if org-enable-table-editor "|" "")
  12614. (if org-enable-fixed-width-editor ":" "")
  12615. "]"))))
  12616. ;; We use our own fill-paragraph function, to make sure that tables
  12617. ;; and fixed-width regions are not wrapped. That function will pass
  12618. ;; through to `fill-paragraph' when appropriate.
  12619. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12620. ; Adaptive filling: To get full control, first make sure that
  12621. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12622. (org-set-local 'adaptive-fill-regexp "\000")
  12623. (org-set-local 'adaptive-fill-function
  12624. 'org-adaptive-fill-function)
  12625. (org-set-local
  12626. 'align-mode-rules-list
  12627. '((org-in-buffer-settings
  12628. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12629. (modes . '(org-mode))))))
  12630. (defun org-fill-paragraph (&optional justify)
  12631. "Re-align a table, pass through to fill-paragraph if no table."
  12632. (let ((table-p (org-at-table-p))
  12633. (table.el-p (org-at-table.el-p)))
  12634. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12635. (save-excursion (goto-char (point-at-bol))
  12636. (looking-at outline-regexp)))
  12637. t) ; skip headlines
  12638. (table.el-p t) ; skip table.el tables
  12639. (table-p (org-table-align) t) ; align org-mode tables
  12640. (t nil)))) ; call paragraph-fill
  12641. ;; For reference, this is the default value of adaptive-fill-regexp
  12642. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12643. (defun org-adaptive-fill-function ()
  12644. "Return a fill prefix for org-mode files.
  12645. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12646. work correctly."
  12647. (cond ((looking-at "#[ \t]+")
  12648. (match-string 0))
  12649. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12650. (save-excursion
  12651. (if (> (match-end 1) (+ (match-beginning 1)
  12652. org-description-max-indent))
  12653. (goto-char (+ (match-beginning 1) 5))
  12654. (goto-char (match-end 0)))
  12655. (make-string (current-column) ?\ )))
  12656. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12657. (save-excursion
  12658. (goto-char (match-end 0))
  12659. (make-string (current-column) ?\ )))
  12660. (t nil)))
  12661. ;;; Other stuff.
  12662. (defun org-toggle-fixed-width-section (arg)
  12663. "Toggle the fixed-width export.
  12664. If there is no active region, the QUOTE keyword at the current headline is
  12665. inserted or removed. When present, it causes the text between this headline
  12666. and the next to be exported as fixed-width text, and unmodified.
  12667. If there is an active region, this command adds or removes a colon as the
  12668. first character of this line. If the first character of a line is a colon,
  12669. this line is also exported in fixed-width font."
  12670. (interactive "P")
  12671. (let* ((cc 0)
  12672. (regionp (org-region-active-p))
  12673. (beg (if regionp (region-beginning) (point)))
  12674. (end (if regionp (region-end)))
  12675. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12676. (case-fold-search nil)
  12677. (re "[ \t]*\\(:\\)")
  12678. off)
  12679. (if regionp
  12680. (save-excursion
  12681. (goto-char beg)
  12682. (setq cc (current-column))
  12683. (beginning-of-line 1)
  12684. (setq off (looking-at re))
  12685. (while (> nlines 0)
  12686. (setq nlines (1- nlines))
  12687. (beginning-of-line 1)
  12688. (cond
  12689. (arg
  12690. (org-move-to-column cc t)
  12691. (insert ":\n")
  12692. (forward-line -1))
  12693. ((and off (looking-at re))
  12694. (replace-match "" t t nil 1))
  12695. ((not off) (org-move-to-column cc t) (insert ":")))
  12696. (forward-line 1)))
  12697. (save-excursion
  12698. (org-back-to-heading)
  12699. (if (looking-at (concat outline-regexp
  12700. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12701. (replace-match "" t t nil 1)
  12702. (if (looking-at outline-regexp)
  12703. (progn
  12704. (goto-char (match-end 0))
  12705. (insert org-quote-string " "))))))))
  12706. ;;;; Functions extending outline functionality
  12707. (defun org-beginning-of-line (&optional arg)
  12708. "Go to the beginning of the current line. If that is invisible, continue
  12709. to a visible line beginning. This makes the function of C-a more intuitive.
  12710. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12711. first attempt, and only move to after the tags when the cursor is already
  12712. beyond the end of the headline."
  12713. (interactive "P")
  12714. (let ((pos (point)))
  12715. (beginning-of-line 1)
  12716. (if (bobp)
  12717. nil
  12718. (backward-char 1)
  12719. (if (org-invisible-p)
  12720. (while (and (not (bobp)) (org-invisible-p))
  12721. (backward-char 1)
  12722. (beginning-of-line 1))
  12723. (forward-char 1)))
  12724. (when org-special-ctrl-a/e
  12725. (cond
  12726. ((and (looking-at org-todo-line-regexp)
  12727. (= (char-after (match-end 1)) ?\ ))
  12728. (goto-char
  12729. (if (eq org-special-ctrl-a/e t)
  12730. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  12731. ((= pos (point)) (match-beginning 3))
  12732. (t (point)))
  12733. (cond ((> pos (point)) (point))
  12734. ((not (eq last-command this-command)) (point))
  12735. (t (match-beginning 3))))))
  12736. ((org-at-item-p)
  12737. (goto-char
  12738. (if (eq org-special-ctrl-a/e t)
  12739. (cond ((> pos (match-end 4)) (match-end 4))
  12740. ((= pos (point)) (match-end 4))
  12741. (t (point)))
  12742. (cond ((> pos (point)) (point))
  12743. ((not (eq last-command this-command)) (point))
  12744. (t (match-end 4))))))))))
  12745. (defun org-end-of-line (&optional arg)
  12746. "Go to the end of the line.
  12747. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12748. first attempt, and only move to after the tags when the cursor is already
  12749. beyond the end of the headline."
  12750. (interactive "P")
  12751. (if (or (not org-special-ctrl-a/e)
  12752. (not (org-on-heading-p)))
  12753. (end-of-line arg)
  12754. (let ((pos (point)))
  12755. (beginning-of-line 1)
  12756. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12757. (if (eq org-special-ctrl-a/e t)
  12758. (if (or (< pos (match-beginning 1))
  12759. (= pos (match-end 0)))
  12760. (goto-char (match-beginning 1))
  12761. (goto-char (match-end 0)))
  12762. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12763. (goto-char (match-end 0))
  12764. (goto-char (match-beginning 1))))
  12765. (end-of-line arg)))))
  12766. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12767. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12768. (defun org-kill-line (&optional arg)
  12769. "Kill line, to tags or end of line."
  12770. (interactive "P")
  12771. (cond
  12772. ((or (not org-special-ctrl-k)
  12773. (bolp)
  12774. (not (org-on-heading-p)))
  12775. (call-interactively 'kill-line))
  12776. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12777. (kill-region (point) (match-beginning 1))
  12778. (org-set-tags nil t))
  12779. (t (kill-region (point) (point-at-eol)))))
  12780. (define-key org-mode-map "\C-k" 'org-kill-line)
  12781. (defun org-invisible-p ()
  12782. "Check if point is at a character currently not visible."
  12783. ;; Early versions of noutline don't have `outline-invisible-p'.
  12784. (if (fboundp 'outline-invisible-p)
  12785. (outline-invisible-p)
  12786. (get-char-property (point) 'invisible)))
  12787. (defun org-invisible-p2 ()
  12788. "Check if point is at a character currently not visible."
  12789. (save-excursion
  12790. (if (and (eolp) (not (bobp))) (backward-char 1))
  12791. ;; Early versions of noutline don't have `outline-invisible-p'.
  12792. (if (fboundp 'outline-invisible-p)
  12793. (outline-invisible-p)
  12794. (get-char-property (point) 'invisible))))
  12795. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12796. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12797. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12798. (defun org-at-heading-or-item-p ()
  12799. (or (org-on-heading-p) (org-at-item-p)))
  12800. (defun org-on-target-p ()
  12801. (or (org-in-regexp org-radio-target-regexp)
  12802. (org-in-regexp org-target-regexp)))
  12803. (defun org-up-heading-all (arg)
  12804. "Move to the heading line of which the present line is a subheading.
  12805. This function considers both visible and invisible heading lines.
  12806. With argument, move up ARG levels."
  12807. (if (fboundp 'outline-up-heading-all)
  12808. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12809. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12810. (defun org-up-heading-safe ()
  12811. "Move to the heading line of which the present line is a subheading.
  12812. This version will not throw an error. It will return the level of the
  12813. headline found, or nil if no higher level is found."
  12814. (let ((pos (point)) start-level level
  12815. (re (concat "^" outline-regexp)))
  12816. (catch 'exit
  12817. (outline-back-to-heading t)
  12818. (setq start-level (funcall outline-level))
  12819. (if (equal start-level 1) (throw 'exit nil))
  12820. (while (re-search-backward re nil t)
  12821. (setq level (funcall outline-level))
  12822. (if (< level start-level) (throw 'exit level)))
  12823. nil)))
  12824. (defun org-first-sibling-p ()
  12825. "Is this heading the first child of its parents?"
  12826. (interactive)
  12827. (let ((re (concat "^" outline-regexp))
  12828. level l)
  12829. (unless (org-at-heading-p t)
  12830. (error "Not at a heading"))
  12831. (setq level (funcall outline-level))
  12832. (save-excursion
  12833. (if (not (re-search-backward re nil t))
  12834. t
  12835. (setq l (funcall outline-level))
  12836. (< l level)))))
  12837. (defun org-goto-sibling (&optional previous)
  12838. "Goto the next sibling, even if it is invisible.
  12839. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12840. when a sibling was found. When none is found, return nil and don't
  12841. move point."
  12842. (let ((fun (if previous 're-search-backward 're-search-forward))
  12843. (pos (point))
  12844. (re (concat "^" outline-regexp))
  12845. level l)
  12846. (when (condition-case nil (org-back-to-heading t) (error nil))
  12847. (setq level (funcall outline-level))
  12848. (catch 'exit
  12849. (or previous (forward-char 1))
  12850. (while (funcall fun re nil t)
  12851. (setq l (funcall outline-level))
  12852. (when (< l level) (goto-char pos) (throw 'exit nil))
  12853. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12854. (goto-char pos)
  12855. nil))))
  12856. (defun org-show-siblings ()
  12857. "Show all siblings of the current headline."
  12858. (save-excursion
  12859. (while (org-goto-sibling) (org-flag-heading nil)))
  12860. (save-excursion
  12861. (while (org-goto-sibling 'previous)
  12862. (org-flag-heading nil))))
  12863. (defun org-show-hidden-entry ()
  12864. "Show an entry where even the heading is hidden."
  12865. (save-excursion
  12866. (org-show-entry)))
  12867. (defun org-flag-heading (flag &optional entry)
  12868. "Flag the current heading. FLAG non-nil means make invisible.
  12869. When ENTRY is non-nil, show the entire entry."
  12870. (save-excursion
  12871. (org-back-to-heading t)
  12872. ;; Check if we should show the entire entry
  12873. (if entry
  12874. (progn
  12875. (org-show-entry)
  12876. (save-excursion
  12877. (and (outline-next-heading)
  12878. (org-flag-heading nil))))
  12879. (outline-flag-region (max (point-min) (1- (point)))
  12880. (save-excursion (outline-end-of-heading) (point))
  12881. flag))))
  12882. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12883. ;; This is an exact copy of the original function, but it uses
  12884. ;; `org-back-to-heading', to make it work also in invisible
  12885. ;; trees. And is uses an invisible-OK argument.
  12886. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12887. (org-back-to-heading invisible-OK)
  12888. (let ((first t)
  12889. (level (funcall outline-level)))
  12890. (while (and (not (eobp))
  12891. (or first (> (funcall outline-level) level)))
  12892. (setq first nil)
  12893. (outline-next-heading))
  12894. (unless to-heading
  12895. (if (memq (preceding-char) '(?\n ?\^M))
  12896. (progn
  12897. ;; Go to end of line before heading
  12898. (forward-char -1)
  12899. (if (memq (preceding-char) '(?\n ?\^M))
  12900. ;; leave blank line before heading
  12901. (forward-char -1))))))
  12902. (point))
  12903. (defun org-show-subtree ()
  12904. "Show everything after this heading at deeper levels."
  12905. (outline-flag-region
  12906. (point)
  12907. (save-excursion
  12908. (outline-end-of-subtree) (outline-next-heading) (point))
  12909. nil))
  12910. (defun org-show-entry ()
  12911. "Show the body directly following this heading.
  12912. Show the heading too, if it is currently invisible."
  12913. (interactive)
  12914. (save-excursion
  12915. (condition-case nil
  12916. (progn
  12917. (org-back-to-heading t)
  12918. (outline-flag-region
  12919. (max (point-min) (1- (point)))
  12920. (save-excursion
  12921. (re-search-forward
  12922. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12923. (or (match-beginning 1) (point-max)))
  12924. nil))
  12925. (error nil))))
  12926. (defun org-make-options-regexp (kwds)
  12927. "Make a regular expression for keyword lines."
  12928. (concat
  12929. "^"
  12930. "#?[ \t]*\\+\\("
  12931. (mapconcat 'regexp-quote kwds "\\|")
  12932. "\\):[ \t]*"
  12933. "\\(.+\\)"))
  12934. ;; Make isearch reveal the necessary context
  12935. (defun org-isearch-end ()
  12936. "Reveal context after isearch exits."
  12937. (when isearch-success ; only if search was successful
  12938. (if (featurep 'xemacs)
  12939. ;; Under XEmacs, the hook is run in the correct place,
  12940. ;; we directly show the context.
  12941. (org-show-context 'isearch)
  12942. ;; In Emacs the hook runs *before* restoring the overlays.
  12943. ;; So we have to use a one-time post-command-hook to do this.
  12944. ;; (Emacs 22 has a special variable, see function `org-mode')
  12945. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12946. isearch-mode-end-hook-quit)
  12947. ;; Only when the isearch was not quitted.
  12948. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12949. 'append 'local)))))
  12950. (defun org-isearch-post-command ()
  12951. "Remove self from hook, and show context."
  12952. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12953. (org-show-context 'isearch))
  12954. ;;;; Integration with and fixes for other packages
  12955. ;;; Imenu support
  12956. (defvar org-imenu-markers nil
  12957. "All markers currently used by Imenu.")
  12958. (make-variable-buffer-local 'org-imenu-markers)
  12959. (defun org-imenu-new-marker (&optional pos)
  12960. "Return a new marker for use by Imenu, and remember the marker."
  12961. (let ((m (make-marker)))
  12962. (move-marker m (or pos (point)))
  12963. (push m org-imenu-markers)
  12964. m))
  12965. (defun org-imenu-get-tree ()
  12966. "Produce the index for Imenu."
  12967. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12968. (setq org-imenu-markers nil)
  12969. (let* ((n org-imenu-depth)
  12970. (re (concat "^" outline-regexp))
  12971. (subs (make-vector (1+ n) nil))
  12972. (last-level 0)
  12973. m tree level head)
  12974. (save-excursion
  12975. (save-restriction
  12976. (widen)
  12977. (goto-char (point-max))
  12978. (while (re-search-backward re nil t)
  12979. (setq level (org-reduced-level (funcall outline-level)))
  12980. (when (<= level n)
  12981. (looking-at org-complex-heading-regexp)
  12982. (setq head (org-match-string-no-properties 4)
  12983. m (org-imenu-new-marker))
  12984. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12985. (if (>= level last-level)
  12986. (push (cons head m) (aref subs level))
  12987. (push (cons head (aref subs (1+ level))) (aref subs level))
  12988. (loop for i from (1+ level) to n do (aset subs i nil)))
  12989. (setq last-level level)))))
  12990. (aref subs 1)))
  12991. (eval-after-load "imenu"
  12992. '(progn
  12993. (add-hook 'imenu-after-jump-hook
  12994. (lambda () (org-show-context 'org-goto)))))
  12995. ;; Speedbar support
  12996. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12997. "Overlay marking the agenda restriction line in speedbar.")
  12998. (org-overlay-put org-speedbar-restriction-lock-overlay
  12999. 'face 'org-agenda-restriction-lock)
  13000. (org-overlay-put org-speedbar-restriction-lock-overlay
  13001. 'help-echo "Agendas are currently limited to this item.")
  13002. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13003. (defun org-speedbar-set-agenda-restriction ()
  13004. "Restrict future agenda commands to the location at point in speedbar.
  13005. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13006. (interactive)
  13007. (require 'org-agenda)
  13008. (let (p m tp np dir txt w)
  13009. (cond
  13010. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13011. 'org-imenu t))
  13012. (setq m (get-text-property p 'org-imenu-marker))
  13013. (save-excursion
  13014. (save-restriction
  13015. (set-buffer (marker-buffer m))
  13016. (goto-char m)
  13017. (org-agenda-set-restriction-lock 'subtree))))
  13018. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13019. 'speedbar-function 'speedbar-find-file))
  13020. (setq tp (previous-single-property-change
  13021. (1+ p) 'speedbar-function)
  13022. np (next-single-property-change
  13023. tp 'speedbar-function)
  13024. dir (speedbar-line-directory)
  13025. txt (buffer-substring-no-properties (or tp (point-min))
  13026. (or np (point-max))))
  13027. (save-excursion
  13028. (save-restriction
  13029. (set-buffer (find-file-noselect
  13030. (let ((default-directory dir))
  13031. (expand-file-name txt))))
  13032. (unless (org-mode-p)
  13033. (error "Cannot restrict to non-Org-mode file"))
  13034. (org-agenda-set-restriction-lock 'file))))
  13035. (t (error "Don't know how to restrict Org-mode's agenda")))
  13036. (org-move-overlay org-speedbar-restriction-lock-overlay
  13037. (point-at-bol) (point-at-eol))
  13038. (setq current-prefix-arg nil)
  13039. (org-agenda-maybe-redo)))
  13040. (eval-after-load "speedbar"
  13041. '(progn
  13042. (speedbar-add-supported-extension ".org")
  13043. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13044. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13045. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13046. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13047. (add-hook 'speedbar-visiting-tag-hook
  13048. (lambda () (org-show-context 'org-goto)))))
  13049. ;;; Fixes and Hacks for problems with other packages
  13050. ;; Make flyspell not check words in links, to not mess up our keymap
  13051. (defun org-mode-flyspell-verify ()
  13052. "Don't let flyspell put overlays at active buttons."
  13053. (not (get-text-property (point) 'keymap)))
  13054. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13055. (eval-after-load "bookmark"
  13056. '(if (boundp 'bookmark-after-jump-hook)
  13057. ;; We can use the hook
  13058. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13059. ;; Hook not available, use advice
  13060. (defadvice bookmark-jump (after org-make-visible activate)
  13061. "Make the position visible."
  13062. (org-bookmark-jump-unhide))))
  13063. (defun org-bookmark-jump-unhide ()
  13064. "Unhide the current position, to show the bookmark location."
  13065. (and (org-mode-p)
  13066. (or (org-invisible-p)
  13067. (save-excursion (goto-char (max (point-min) (1- (point))))
  13068. (org-invisible-p)))
  13069. (org-show-context 'bookmark-jump)))
  13070. ;; Make session.el ignore our circular variable
  13071. (eval-after-load "session"
  13072. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13073. ;;;; Experimental code
  13074. (defun org-closed-in-range ()
  13075. "Sparse tree of items closed in a certain time range.
  13076. Still experimental, may disappear in the future."
  13077. (interactive)
  13078. ;; Get the time interval from the user.
  13079. (let* ((time1 (time-to-seconds
  13080. (org-read-date nil 'to-time nil "Starting date: ")))
  13081. (time2 (time-to-seconds
  13082. (org-read-date nil 'to-time nil "End date:")))
  13083. ;; callback function
  13084. (callback (lambda ()
  13085. (let ((time
  13086. (time-to-seconds
  13087. (apply 'encode-time
  13088. (org-parse-time-string
  13089. (match-string 1))))))
  13090. ;; check if time in interval
  13091. (and (>= time time1) (<= time time2))))))
  13092. ;; make tree, check each match with the callback
  13093. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13094. ;;;; Finish up
  13095. (provide 'org)
  13096. (run-hooks 'org-load-hook)
  13097. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13098. ;;; org.el ends here