org.el 552 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889
  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.16trans
  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. (require 'org-list)
  84. (require 'org-footnote)
  85. ;;;; Customization variables
  86. ;;; Version
  87. (defconst org-version "6.16trans"
  88. "The version number of the file org.el.")
  89. (defun org-version (&optional here)
  90. "Show the org-mode version in the echo area.
  91. With prefix arg HERE, insert it at point."
  92. (interactive "P")
  93. (let ((version (format "Org-mode version %s" org-version)))
  94. (message version)
  95. (if here
  96. (insert version))))
  97. ;;; Compatibility constants
  98. ;;; The custom variables
  99. (defgroup org nil
  100. "Outline-based notes management and organizer."
  101. :tag "Org"
  102. :group 'outlines
  103. :group 'hypermedia
  104. :group 'calendar)
  105. (defcustom org-load-hook nil
  106. "Hook that is run after org.el has been loaded."
  107. :group 'org
  108. :type 'hook)
  109. (defvar org-modules) ; defined below
  110. (defvar org-modules-loaded nil
  111. "Have the modules been loaded already?")
  112. (defun org-load-modules-maybe (&optional force)
  113. "Load all extensions listed in `org-modules'."
  114. (when (or force (not org-modules-loaded))
  115. (mapc (lambda (ext)
  116. (condition-case nil (require ext)
  117. (error (message "Problems while trying to load feature `%s'" ext))))
  118. org-modules)
  119. (setq org-modules-loaded t)))
  120. (defun org-set-modules (var value)
  121. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  122. (set var value)
  123. (when (featurep 'org)
  124. (org-load-modules-maybe 'force)))
  125. (when (org-bound-and-true-p org-modules)
  126. (let ((a (member 'org-infojs org-modules)))
  127. (and a (setcar a 'org-jsinfo))))
  128. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  129. "Modules that should always be loaded together with org.el.
  130. If a description starts with <C>, the file is not part of Emacs
  131. and loading it will require that you have downloaded and properly installed
  132. the org-mode distribution.
  133. You can also use this system to load external packages (i.e. neither Org
  134. core modules, not modules from the CONTRIB directory). Just add symbols
  135. to the end of the list. If the package is called org-xyz.el, then you need
  136. to add the symbol `xyz', and the package must have a call to
  137. (provide 'org-xyz)"
  138. :group 'org
  139. :set 'org-set-modules
  140. :type
  141. '(set :greedy t
  142. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  143. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  144. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  145. (const :tag " id: Global IDs for identifying entries" org-id)
  146. (const :tag " info: Links to Info nodes" org-info)
  147. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  148. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  149. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  150. (const :tag " mew Links to Mew folders/messages" org-mew)
  151. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  152. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  153. (const :tag " vm: Links to VM folders/messages" org-vm)
  154. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  155. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  156. (const :tag " mouse: Additional mouse support" org-mouse)
  157. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  158. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  159. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  160. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  161. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  162. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  163. (const :tag "C eval: Include command output as text" org-eval)
  164. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  165. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  166. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  167. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  168. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  169. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  170. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  171. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  172. (const :tag "C registry: A registry for Org links" org-registry)
  173. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  174. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  175. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  176. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  177. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  178. (defgroup org-startup nil
  179. "Options concerning startup of Org-mode."
  180. :tag "Org Startup"
  181. :group 'org)
  182. (defcustom org-startup-folded t
  183. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  184. This can also be configured on a per-file basis by adding one of
  185. the following lines anywhere in the buffer:
  186. #+STARTUP: fold
  187. #+STARTUP: nofold
  188. #+STARTUP: content"
  189. :group 'org-startup
  190. :type '(choice
  191. (const :tag "nofold: show all" nil)
  192. (const :tag "fold: overview" t)
  193. (const :tag "content: all headlines" content)))
  194. (defcustom org-startup-truncated t
  195. "Non-nil means, entering Org-mode will set `truncate-lines'.
  196. This is useful since some lines containing links can be very long and
  197. uninteresting. Also tables look terrible when wrapped."
  198. :group 'org-startup
  199. :type 'boolean)
  200. (defcustom org-startup-align-all-tables nil
  201. "Non-nil means, align all tables when visiting a file.
  202. This is useful when the column width in tables is forced with <N> cookies
  203. in table fields. Such tables will look correct only after the first re-align.
  204. This can also be configured on a per-file basis by adding one of
  205. the following lines anywhere in the buffer:
  206. #+STARTUP: align
  207. #+STARTUP: noalign"
  208. :group 'org-startup
  209. :type 'boolean)
  210. (defcustom org-insert-mode-line-in-empty-file nil
  211. "Non-nil means insert the first line setting Org-mode in empty files.
  212. When the function `org-mode' is called interactively in an empty file, this
  213. normally means that the file name does not automatically trigger Org-mode.
  214. To ensure that the file will always be in Org-mode in the future, a
  215. line enforcing Org-mode will be inserted into the buffer, if this option
  216. has been set."
  217. :group 'org-startup
  218. :type 'boolean)
  219. (defcustom org-replace-disputed-keys nil
  220. "Non-nil means use alternative key bindings for some keys.
  221. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  222. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  223. If you want to use Org-mode together with one of these other modes,
  224. or more generally if you would like to move some Org-mode commands to
  225. other keys, set this variable and configure the keys with the variable
  226. `org-disputed-keys'.
  227. This option is only relevant at load-time of Org-mode, and must be set
  228. *before* org.el is loaded. Changing it requires a restart of Emacs to
  229. become effective."
  230. :group 'org-startup
  231. :type 'boolean)
  232. (defcustom org-use-extra-keys nil
  233. "Non-nil means use extra key sequence definitions for certain
  234. commands. This happens automatically if you run XEmacs or if
  235. window-system is nil. This variable lets you do the same
  236. manually. You must set it before loading org.
  237. Example: on Carbon Emacs 22 running graphically, with an external
  238. keyboard on a Powerbook, the default way of setting M-left might
  239. not work for either Alt or ESC. Setting this variable will make
  240. it work for ESC."
  241. :group 'org-startup
  242. :type 'boolean)
  243. (if (fboundp 'defvaralias)
  244. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  245. (defcustom org-disputed-keys
  246. '(([(shift up)] . [(meta p)])
  247. ([(shift down)] . [(meta n)])
  248. ([(shift left)] . [(meta -)])
  249. ([(shift right)] . [(meta +)])
  250. ([(control shift right)] . [(meta shift +)])
  251. ([(control shift left)] . [(meta shift -)]))
  252. "Keys for which Org-mode and other modes compete.
  253. This is an alist, cars are the default keys, second element specifies
  254. the alternative to use when `org-replace-disputed-keys' is t.
  255. Keys can be specified in any syntax supported by `define-key'.
  256. The value of this option takes effect only at Org-mode's startup,
  257. therefore you'll have to restart Emacs to apply it after changing."
  258. :group 'org-startup
  259. :type 'alist)
  260. (defun org-key (key)
  261. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  262. Or return the original if not disputed."
  263. (if org-replace-disputed-keys
  264. (let* ((nkey (key-description key))
  265. (x (org-find-if (lambda (x)
  266. (equal (key-description (car x)) nkey))
  267. org-disputed-keys)))
  268. (if x (cdr x) key))
  269. key))
  270. (defun org-find-if (predicate seq)
  271. (catch 'exit
  272. (while seq
  273. (if (funcall predicate (car seq))
  274. (throw 'exit (car seq))
  275. (pop seq)))))
  276. (defun org-defkey (keymap key def)
  277. "Define a key, possibly translated, as returned by `org-key'."
  278. (define-key keymap (org-key key) def))
  279. (defcustom org-ellipsis nil
  280. "The ellipsis to use in the Org-mode outline.
  281. When nil, just use the standard three dots. When a string, use that instead,
  282. When a face, use the standard 3 dots, but with the specified face.
  283. The change affects only Org-mode (which will then use its own display table).
  284. Changing this requires executing `M-x org-mode' in a buffer to become
  285. effective."
  286. :group 'org-startup
  287. :type '(choice (const :tag "Default" nil)
  288. (face :tag "Face" :value org-warning)
  289. (string :tag "String" :value "...#")))
  290. (defvar org-display-table nil
  291. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  292. (defgroup org-keywords nil
  293. "Keywords in Org-mode."
  294. :tag "Org Keywords"
  295. :group 'org)
  296. (defcustom org-deadline-string "DEADLINE:"
  297. "String to mark deadline entries.
  298. A deadline is this string, followed by a time stamp. Should be a word,
  299. terminated by a colon. You can insert a schedule keyword and
  300. a timestamp with \\[org-deadline].
  301. Changes become only effective after restarting Emacs."
  302. :group 'org-keywords
  303. :type 'string)
  304. (defcustom org-scheduled-string "SCHEDULED:"
  305. "String to mark scheduled TODO entries.
  306. A schedule is this string, followed by a time stamp. Should be a word,
  307. terminated by a colon. You can insert a schedule keyword and
  308. a timestamp with \\[org-schedule].
  309. Changes become only effective after restarting Emacs."
  310. :group 'org-keywords
  311. :type 'string)
  312. (defcustom org-closed-string "CLOSED:"
  313. "String used as the prefix for timestamps logging closing a TODO entry."
  314. :group 'org-keywords
  315. :type 'string)
  316. (defcustom org-clock-string "CLOCK:"
  317. "String used as prefix for timestamps clocking work hours on an item."
  318. :group 'org-keywords
  319. :type 'string)
  320. (defcustom org-comment-string "COMMENT"
  321. "Entries starting with this keyword will never be exported.
  322. An entry can be toggled between COMMENT and normal with
  323. \\[org-toggle-comment].
  324. Changes become only effective after restarting Emacs."
  325. :group 'org-keywords
  326. :type 'string)
  327. (defcustom org-quote-string "QUOTE"
  328. "Entries starting with this keyword will be exported in fixed-width font.
  329. Quoting applies only to the text in the entry following the headline, and does
  330. not extend beyond the next headline, even if that is lower level.
  331. An entry can be toggled between QUOTE and normal with
  332. \\[org-toggle-fixed-width-section]."
  333. :group 'org-keywords
  334. :type 'string)
  335. (defconst org-repeat-re
  336. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  337. "Regular expression for specifying repeated events.
  338. After a match, group 1 contains the repeat expression.")
  339. (defgroup org-structure nil
  340. "Options concerning the general structure of Org-mode files."
  341. :tag "Org Structure"
  342. :group 'org)
  343. (defgroup org-reveal-location nil
  344. "Options about how to make context of a location visible."
  345. :tag "Org Reveal Location"
  346. :group 'org-structure)
  347. (defconst org-context-choice
  348. '(choice
  349. (const :tag "Always" t)
  350. (const :tag "Never" nil)
  351. (repeat :greedy t :tag "Individual contexts"
  352. (cons
  353. (choice :tag "Context"
  354. (const agenda)
  355. (const org-goto)
  356. (const occur-tree)
  357. (const tags-tree)
  358. (const link-search)
  359. (const mark-goto)
  360. (const bookmark-jump)
  361. (const isearch)
  362. (const default))
  363. (boolean))))
  364. "Contexts for the reveal options.")
  365. (defcustom org-show-hierarchy-above '((default . t))
  366. "Non-nil means, show full hierarchy when revealing a location.
  367. Org-mode often shows locations in an org-mode file which might have
  368. been invisible before. When this is set, the hierarchy of headings
  369. above the exposed location is shown.
  370. Turning this off for example for sparse trees makes them very compact.
  371. Instead of t, this can also be an alist specifying this option for different
  372. contexts. Valid contexts are
  373. agenda when exposing an entry from the agenda
  374. org-goto when using the command `org-goto' on key C-c C-j
  375. occur-tree when using the command `org-occur' on key C-c /
  376. tags-tree when constructing a sparse tree based on tags matches
  377. link-search when exposing search matches associated with a link
  378. mark-goto when exposing the jump goal of a mark
  379. bookmark-jump when exposing a bookmark location
  380. isearch when exiting from an incremental search
  381. default default for all contexts not set explicitly"
  382. :group 'org-reveal-location
  383. :type org-context-choice)
  384. (defcustom org-show-following-heading '((default . nil))
  385. "Non-nil means, show following heading when revealing a location.
  386. Org-mode often shows locations in an org-mode file which might have
  387. been invisible before. When this is set, the heading following the
  388. match is shown.
  389. Turning this off for example for sparse trees makes them very compact,
  390. but makes it harder to edit the location of the match. In such a case,
  391. use the command \\[org-reveal] to show more context.
  392. Instead of t, this can also be an alist specifying this option for different
  393. contexts. See `org-show-hierarchy-above' for valid contexts."
  394. :group 'org-reveal-location
  395. :type org-context-choice)
  396. (defcustom org-show-siblings '((default . nil) (isearch t))
  397. "Non-nil means, show all sibling heading when revealing a location.
  398. Org-mode often shows locations in an org-mode file which might have
  399. been invisible before. When this is set, the sibling of the current entry
  400. heading are all made visible. If `org-show-hierarchy-above' is t,
  401. the same happens on each level of the hierarchy above the current entry.
  402. By default this is on for the isearch context, off for all other contexts.
  403. Turning this off for example for sparse trees makes them very compact,
  404. but makes it harder to edit the location of the match. In such a case,
  405. use the command \\[org-reveal] to show more context.
  406. Instead of t, this can also be an alist specifying this option for different
  407. contexts. See `org-show-hierarchy-above' for valid contexts."
  408. :group 'org-reveal-location
  409. :type org-context-choice)
  410. (defcustom org-show-entry-below '((default . nil))
  411. "Non-nil means, show the entry below a headline when revealing a location.
  412. Org-mode often shows locations in an org-mode file which might have
  413. been invisible before. When this is set, the text below the headline that is
  414. exposed is also shown.
  415. By default this is off for all contexts.
  416. Instead of t, this can also be an alist specifying this option for different
  417. contexts. See `org-show-hierarchy-above' for valid contexts."
  418. :group 'org-reveal-location
  419. :type org-context-choice)
  420. (defcustom org-indirect-buffer-display 'other-window
  421. "How should indirect tree buffers be displayed?
  422. This applies to indirect buffers created with the commands
  423. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  424. Valid values are:
  425. current-window Display in the current window
  426. other-window Just display in another window.
  427. dedicated-frame Create one new frame, and re-use it each time.
  428. new-frame Make a new frame each time. Note that in this case
  429. previously-made indirect buffers are kept, and you need to
  430. kill these buffers yourself."
  431. :group 'org-structure
  432. :group 'org-agenda-windows
  433. :type '(choice
  434. (const :tag "In current window" current-window)
  435. (const :tag "In current frame, other window" other-window)
  436. (const :tag "Each time a new frame" new-frame)
  437. (const :tag "One dedicated frame" dedicated-frame)))
  438. (defgroup org-cycle nil
  439. "Options concerning visibility cycling in Org-mode."
  440. :tag "Org Cycle"
  441. :group 'org-structure)
  442. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  443. "Names of drawers. Drawers are not opened by cycling on the headline above.
  444. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  445. this:
  446. :DRAWERNAME:
  447. .....
  448. :END:
  449. The drawer \"PROPERTIES\" is special for capturing properties through
  450. the property API.
  451. Drawers can be defined on the per-file basis with a line like:
  452. #+DRAWERS: HIDDEN STATE PROPERTIES"
  453. :group 'org-structure
  454. :type '(repeat (string :tag "Drawer Name")))
  455. (defcustom org-cycle-global-at-bob nil
  456. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  457. This makes it possible to do global cycling without having to use S-TAB or
  458. C-u TAB. For this special case to work, the first line of the buffer
  459. must not be a headline - it may be empty ot some other text. When used in
  460. this way, `org-cycle-hook' is disables temporarily, to make sure the
  461. cursor stays at the beginning of the buffer.
  462. When this option is nil, don't do anything special at the beginning
  463. of the buffer."
  464. :group 'org-cycle
  465. :type 'boolean)
  466. (defcustom org-cycle-emulate-tab t
  467. "Where should `org-cycle' emulate TAB.
  468. nil Never
  469. white Only in completely white lines
  470. whitestart Only at the beginning of lines, before the first non-white char
  471. t Everywhere except in headlines
  472. exc-hl-bol Everywhere except at the start of a headline
  473. If TAB is used in a place where it does not emulate TAB, the current subtree
  474. visibility is cycled."
  475. :group 'org-cycle
  476. :type '(choice (const :tag "Never" nil)
  477. (const :tag "Only in completely white lines" white)
  478. (const :tag "Before first char in a line" whitestart)
  479. (const :tag "Everywhere except in headlines" t)
  480. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  481. ))
  482. (defcustom org-cycle-separator-lines 2
  483. "Number of empty lines needed to keep an empty line between collapsed trees.
  484. If you leave an empty line between the end of a subtree and the following
  485. headline, this empty line is hidden when the subtree is folded.
  486. Org-mode will leave (exactly) one empty line visible if the number of
  487. empty lines is equal or larger to the number given in this variable.
  488. So the default 2 means, at least 2 empty lines after the end of a subtree
  489. are needed to produce free space between a collapsed subtree and the
  490. following headline.
  491. Special case: when 0, never leave empty lines in collapsed view."
  492. :group 'org-cycle
  493. :type 'integer)
  494. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  495. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  496. org-cycle-hide-drawers
  497. org-cycle-show-empty-lines
  498. org-optimize-window-after-visibility-change)
  499. "Hook that is run after `org-cycle' has changed the buffer visibility.
  500. The function(s) in this hook must accept a single argument which indicates
  501. the new state that was set by the most recent `org-cycle' command. The
  502. argument is a symbol. After a global state change, it can have the values
  503. `overview', `content', or `all'. After a local state change, it can have
  504. the values `folded', `children', or `subtree'."
  505. :group 'org-cycle
  506. :type 'hook)
  507. (defgroup org-edit-structure nil
  508. "Options concerning structure editing in Org-mode."
  509. :tag "Org Edit Structure"
  510. :group 'org-structure)
  511. (defcustom org-odd-levels-only nil
  512. "Non-nil means, skip even levels and only use odd levels for the outline.
  513. This has the effect that two stars are being added/taken away in
  514. promotion/demotion commands. It also influences how levels are
  515. handled by the exporters.
  516. Changing it requires restart of `font-lock-mode' to become effective
  517. for fontification also in regions already fontified.
  518. You may also set this on a per-file basis by adding one of the following
  519. lines to the buffer:
  520. #+STARTUP: odd
  521. #+STARTUP: oddeven"
  522. :group 'org-edit-structure
  523. :group 'org-font-lock
  524. :type 'boolean)
  525. (defcustom org-adapt-indentation t
  526. "Non-nil means, adapt indentation when promoting and demoting.
  527. When this is set and the *entire* text in an entry is indented, the
  528. indentation is increased by one space in a demotion command, and
  529. decreased by one in a promotion command. If any line in the entry
  530. body starts at column 0, indentation is not changed at all."
  531. :group 'org-edit-structure
  532. :type 'boolean)
  533. (defcustom org-special-ctrl-a/e nil
  534. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  535. When t, `C-a' will bring back the cursor to the beginning of the
  536. headline text, i.e. after the stars and after a possible TODO keyword.
  537. In an item, this will be the position after the bullet.
  538. When the cursor is already at that position, another `C-a' will bring
  539. it to the beginning of the line.
  540. `C-e' will jump to the end of the headline, ignoring the presence of tags
  541. in the headline. A second `C-e' will then jump to the true end of the
  542. line, after any tags.
  543. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  544. and only a directly following, identical keypress will bring the cursor
  545. to the special positions."
  546. :group 'org-edit-structure
  547. :type '(choice
  548. (const :tag "off" nil)
  549. (const :tag "after bullet first" t)
  550. (const :tag "border first" reversed)))
  551. (if (fboundp 'defvaralias)
  552. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  553. (defcustom org-special-ctrl-k nil
  554. "Non-nil means `C-k' will behave specially in headlines.
  555. When nil, `C-k' will call the default `kill-line' command.
  556. When t, the following will happen while the cursor is in the headline:
  557. - When the cursor is at the beginning of a headline, kill the entire
  558. line and possible the folded subtree below the line.
  559. - When in the middle of the headline text, kill the headline up to the tags.
  560. - When after the headline text, kill the tags."
  561. :group 'org-edit-structure
  562. :type 'boolean)
  563. (defcustom org-yank-folded-subtrees t
  564. "Non-nil means, when yanking subtrees, fold them.
  565. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  566. it starts with a heading and all other headings in it are either children
  567. or siblings, then fold all the subtrees. However, do this only if no
  568. text after the yank would be swallowed into a folded tree by this action."
  569. :group 'org-edit-structure
  570. :type 'boolean)
  571. (defcustom org-yank-adjusted-subtrees t
  572. "Non-nil means, when yanking subtrees, adjust the level.
  573. With this setting, `org-paste-subtree' is used to insert the subtree, see
  574. this function for details."
  575. :group 'org-edit-structure
  576. :type 'boolean)
  577. (defcustom org-M-RET-may-split-line '((default . t))
  578. "Non-nil means, M-RET will split the line at the cursor position.
  579. When nil, it will go to the end of the line before making a
  580. new line.
  581. You may also set this option in a different way for different
  582. contexts. Valid contexts are:
  583. headline when creating a new headline
  584. item when creating a new item
  585. table in a table field
  586. default the value to be used for all contexts not explicitly
  587. customized"
  588. :group 'org-structure
  589. :group 'org-table
  590. :type '(choice
  591. (const :tag "Always" t)
  592. (const :tag "Never" nil)
  593. (repeat :greedy t :tag "Individual contexts"
  594. (cons
  595. (choice :tag "Context"
  596. (const headline)
  597. (const item)
  598. (const table)
  599. (const default))
  600. (boolean)))))
  601. (defcustom org-insert-heading-respect-content nil
  602. "Non-nil means, insert new headings after the current subtree.
  603. When nil, the new heading is created directly after the current line.
  604. The commands \\[org-insert-heading-respect-content] and
  605. \\[org-insert-todo-heading-respect-content] turn this variable on
  606. for the duration of the command."
  607. :group 'org-structure
  608. :type 'boolean)
  609. (defcustom org-blank-before-new-entry '((heading . nil)
  610. (plain-list-item . nil))
  611. "Should `org-insert-heading' leave a blank line before new heading/item?
  612. The value is an alist, with `heading' and `plain-list-item' as car,
  613. and a boolean flag as cdr."
  614. :group 'org-edit-structure
  615. :type '(list
  616. (cons (const heading) (boolean))
  617. (cons (const plain-list-item) (boolean))))
  618. (defcustom org-insert-heading-hook nil
  619. "Hook being run after inserting a new heading."
  620. :group 'org-edit-structure
  621. :type 'hook)
  622. (defcustom org-enable-fixed-width-editor t
  623. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  624. This currently only means, they are never auto-wrapped.
  625. When nil, such lines will be treated like ordinary lines.
  626. See also the QUOTE keyword."
  627. :group 'org-edit-structure
  628. :type 'boolean)
  629. (defcustom org-edit-src-region-extra nil
  630. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  631. For examples see the function `org-edit-src-find-region-and-lang'.
  632. The regular expression identifying the begin marker should end with a newline,
  633. and the regexp marking the end line should start with a newline, to make sure
  634. there are kept outside the narrowed region."
  635. :group 'org-edit-structure
  636. :type '(repeat
  637. (list
  638. (regexp :tag "begin regexp")
  639. (regexp :tag "end regexp")
  640. (choice :tag "language"
  641. (string :tag "specify")
  642. (integer :tag "from match group")
  643. (const :tag "from `lang' element")
  644. (const :tag "from `style' element")))))
  645. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  646. "The mode that should be used to edit fixed-width regions.
  647. These are the regions where each line starts with a colon."
  648. :group 'org-edit-structure
  649. :type '(choice
  650. (const artist-mode)
  651. (const picture-mode)
  652. (const fundamental-mode)
  653. (function :tag "Other (specify)")))
  654. (defcustom org-goto-auto-isearch t
  655. "Non-nil means, typing characters in org-goto starts incremental search."
  656. :group 'org-edit-structure
  657. :type 'boolean)
  658. (defgroup org-sparse-trees nil
  659. "Options concerning sparse trees in Org-mode."
  660. :tag "Org Sparse Trees"
  661. :group 'org-structure)
  662. (defcustom org-highlight-sparse-tree-matches t
  663. "Non-nil means, highlight all matches that define a sparse tree.
  664. The highlights will automatically disappear the next time the buffer is
  665. changed by an edit command."
  666. :group 'org-sparse-trees
  667. :type 'boolean)
  668. (defcustom org-remove-highlights-with-change t
  669. "Non-nil means, any change to the buffer will remove temporary highlights.
  670. Such highlights are created by `org-occur' and `org-clock-display'.
  671. When nil, `C-c C-c needs to be used to get rid of the highlights.
  672. The highlights created by `org-preview-latex-fragment' always need
  673. `C-c C-c' to be removed."
  674. :group 'org-sparse-trees
  675. :group 'org-time
  676. :type 'boolean)
  677. (defcustom org-occur-hook '(org-first-headline-recenter)
  678. "Hook that is run after `org-occur' has constructed a sparse tree.
  679. This can be used to recenter the window to show as much of the structure
  680. as possible."
  681. :group 'org-sparse-trees
  682. :type 'hook)
  683. (defgroup org-imenu-and-speedbar nil
  684. "Options concerning imenu and speedbar in Org-mode."
  685. :tag "Org Imenu and Speedbar"
  686. :group 'org-structure)
  687. (defcustom org-imenu-depth 2
  688. "The maximum level for Imenu access to Org-mode headlines.
  689. This also applied for speedbar access."
  690. :group 'org-imenu-and-speedbar
  691. :type 'number)
  692. (defgroup org-table nil
  693. "Options concerning tables in Org-mode."
  694. :tag "Org Table"
  695. :group 'org)
  696. (defcustom org-enable-table-editor 'optimized
  697. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  698. When nil, such lines will be treated like ordinary lines.
  699. When equal to the symbol `optimized', the table editor will be optimized to
  700. do the following:
  701. - Automatic overwrite mode in front of whitespace in table fields.
  702. This makes the structure of the table stay in tact as long as the edited
  703. field does not exceed the column width.
  704. - Minimize the number of realigns. Normally, the table is aligned each time
  705. TAB or RET are pressed to move to another field. With optimization this
  706. happens only if changes to a field might have changed the column width.
  707. Optimization requires replacing the functions `self-insert-command',
  708. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  709. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  710. very good at guessing when a re-align will be necessary, but you can always
  711. force one with \\[org-ctrl-c-ctrl-c].
  712. If you would like to use the optimized version in Org-mode, but the
  713. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  714. This variable can be used to turn on and off the table editor during a session,
  715. but in order to toggle optimization, a restart is required.
  716. See also the variable `org-table-auto-blank-field'."
  717. :group 'org-table
  718. :type '(choice
  719. (const :tag "off" nil)
  720. (const :tag "on" t)
  721. (const :tag "on, optimized" optimized)))
  722. (defcustom org-table-tab-recognizes-table.el t
  723. "Non-nil means, TAB will automatically notice a table.el table.
  724. When it sees such a table, it moves point into it and - if necessary -
  725. calls `table-recognize-table'."
  726. :group 'org-table-editing
  727. :type 'boolean)
  728. (defgroup org-link nil
  729. "Options concerning links in Org-mode."
  730. :tag "Org Link"
  731. :group 'org)
  732. (defvar org-link-abbrev-alist-local nil
  733. "Buffer-local version of `org-link-abbrev-alist', which see.
  734. The value of this is taken from the #+LINK lines.")
  735. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  736. (defcustom org-link-abbrev-alist nil
  737. "Alist of link abbreviations.
  738. The car of each element is a string, to be replaced at the start of a link.
  739. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  740. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  741. [[linkkey:tag][description]]
  742. If REPLACE is a string, the tag will simply be appended to create the link.
  743. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  744. the placeholder \"%h\" will cause a url-encoded version of the tag to
  745. be inserted at that point (see the function `url-hexify-string').
  746. REPLACE may also be a function that will be called with the tag as the
  747. only argument to create the link, which should be returned as a string.
  748. See the manual for examples."
  749. :group 'org-link
  750. :type '(repeat
  751. (cons
  752. (string :tag "Protocol")
  753. (choice
  754. (string :tag "Format")
  755. (function)))))
  756. (defcustom org-descriptive-links t
  757. "Non-nil means, hide link part and only show description of bracket links.
  758. Bracket links are like [[link][description]]. This variable sets the initial
  759. state in new org-mode buffers. The setting can then be toggled on a
  760. per-buffer basis from the Org->Hyperlinks menu."
  761. :group 'org-link
  762. :type 'boolean)
  763. (defcustom org-link-file-path-type 'adaptive
  764. "How the path name in file links should be stored.
  765. Valid values are:
  766. relative Relative to the current directory, i.e. the directory of the file
  767. into which the link is being inserted.
  768. absolute Absolute path, if possible with ~ for home directory.
  769. noabbrev Absolute path, no abbreviation of home directory.
  770. adaptive Use relative path for files in the current directory and sub-
  771. directories of it. For other files, use an absolute path."
  772. :group 'org-link
  773. :type '(choice
  774. (const relative)
  775. (const absolute)
  776. (const noabbrev)
  777. (const adaptive)))
  778. (defcustom org-activate-links '(bracket angle plain radio tag date)
  779. "Types of links that should be activated in Org-mode files.
  780. This is a list of symbols, each leading to the activation of a certain link
  781. type. In principle, it does not hurt to turn on most link types - there may
  782. be a small gain when turning off unused link types. The types are:
  783. bracket The recommended [[link][description]] or [[link]] links with hiding.
  784. angular Links in angular brackets that may contain whitespace like
  785. <bbdb:Carsten Dominik>.
  786. plain Plain links in normal text, no whitespace, like http://google.com.
  787. radio Text that is matched by a radio target, see manual for details.
  788. tag Tag settings in a headline (link to tag search).
  789. date Time stamps (link to calendar).
  790. Changing this variable requires a restart of Emacs to become effective."
  791. :group 'org-link
  792. :type '(set (const :tag "Double bracket links (new style)" bracket)
  793. (const :tag "Angular bracket links (old style)" angular)
  794. (const :tag "Plain text links" plain)
  795. (const :tag "Radio target matches" radio)
  796. (const :tag "Tags" tag)
  797. (const :tag "Timestamps" date)))
  798. (defcustom org-make-link-description-function nil
  799. "Function to use to generate link descriptions from links. If
  800. nil the link location will be used. This function must take two
  801. parameters; the first is the link and the second the description
  802. org-insert-link has generated, and should return the description
  803. to use."
  804. :group 'org-link
  805. :type 'function)
  806. (defgroup org-link-store nil
  807. "Options concerning storing links in Org-mode."
  808. :tag "Org Store Link"
  809. :group 'org-link)
  810. (defcustom org-email-link-description-format "Email %c: %.30s"
  811. "Format of the description part of a link to an email or usenet message.
  812. The following %-escapes will be replaced by corresponding information:
  813. %F full \"From\" field
  814. %f name, taken from \"From\" field, address if no name
  815. %T full \"To\" field
  816. %t first name in \"To\" field, address if no name
  817. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  818. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  819. %s subject
  820. %m message-id.
  821. You may use normal field width specification between the % and the letter.
  822. This is for example useful to limit the length of the subject.
  823. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  824. :group 'org-link-store
  825. :type 'string)
  826. (defcustom org-from-is-user-regexp
  827. (let (r1 r2)
  828. (when (and user-mail-address (not (string= user-mail-address "")))
  829. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  830. (when (and user-full-name (not (string= user-full-name "")))
  831. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  832. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  833. "Regexp matched against the \"From:\" header of an email or usenet message.
  834. It should match if the message is from the user him/herself."
  835. :group 'org-link-store
  836. :type 'regexp)
  837. (defcustom org-link-to-org-use-id 'create-if-interactive
  838. "Non-nil means, storing a link to an Org file will use entry IDs.
  839. Note that before this variable is even considered, org-id must be loaded,
  840. to please customize `org-modules' and turn it on.
  841. The variable can have the following values:
  842. t Create an ID if needed to make a link to the current entry.
  843. create-if-interactive
  844. If `org-store-link' is called directly (interactively, as a user
  845. command), do create an ID to support the link. But when doing the
  846. job for remember, only use the ID if it already exists. The
  847. purpose of this setting is to avoid proliferation of unwanted
  848. IDs, just because you happen to be in an Org file when you
  849. call `org-remember' that automatically and preemptively
  850. creates a link. If you do want to get an ID link in a remember
  851. template to an entry not having an ID, create it first by
  852. explicitly creating a link to it, using `C-c C-l' first.
  853. use-existing
  854. Use existing ID, do not create one.
  855. nil Never use an ID to make a link, instead link using a text search for
  856. the headline text."
  857. :group 'org-link-store
  858. :type '(choice
  859. (const :tag "Create ID to make link" t)
  860. (const :tag "Create if string link interactively"
  861. 'create-if-interactive)
  862. (const :tag "Only use existing" 'use-existing)
  863. (const :tag "Do not use ID to create link" nil)))
  864. (defcustom org-context-in-file-links t
  865. "Non-nil means, file links from `org-store-link' contain context.
  866. A search string will be added to the file name with :: as separator and
  867. used to find the context when the link is activated by the command
  868. `org-open-at-point'.
  869. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  870. negates this setting for the duration of the command."
  871. :group 'org-link-store
  872. :type 'boolean)
  873. (defcustom org-keep-stored-link-after-insertion nil
  874. "Non-nil means, keep link in list for entire session.
  875. The command `org-store-link' adds a link pointing to the current
  876. location to an internal list. These links accumulate during a session.
  877. The command `org-insert-link' can be used to insert links into any
  878. Org-mode file (offering completion for all stored links). When this
  879. option is nil, every link which has been inserted once using \\[org-insert-link]
  880. will be removed from the list, to make completing the unused links
  881. more efficient."
  882. :group 'org-link-store
  883. :type 'boolean)
  884. (defgroup org-link-follow nil
  885. "Options concerning following links in Org-mode."
  886. :tag "Org Follow Link"
  887. :group 'org-link)
  888. (defcustom org-link-translation-function nil
  889. "Function to translate links with different syntax to Org syntax.
  890. This can be used to translate links created for example by the Planner
  891. or emacs-wiki packages to Org syntax.
  892. The function must accept two parameters, a TYPE containing the link
  893. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  894. which is everything after the link protocol. It should return a cons
  895. with possibly modified values of type and path.
  896. Org contains a function for this, so if you set this variable to
  897. `org-translate-link-from-planner', you should be able follow many
  898. links created by planner."
  899. :group 'org-link-follow
  900. :type 'function)
  901. (defcustom org-follow-link-hook nil
  902. "Hook that is run after a link has been followed."
  903. :group 'org-link-follow
  904. :type 'hook)
  905. (defcustom org-tab-follows-link nil
  906. "Non-nil means, on links TAB will follow the link.
  907. Needs to be set before org.el is loaded."
  908. :group 'org-link-follow
  909. :type 'boolean)
  910. (defcustom org-return-follows-link nil
  911. "Non-nil means, on links RET will follow the link.
  912. Needs to be set before org.el is loaded."
  913. :group 'org-link-follow
  914. :type 'boolean)
  915. (defcustom org-mouse-1-follows-link
  916. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  917. "Non-nil means, mouse-1 on a link will follow the link.
  918. A longer mouse click will still set point. Does not work on XEmacs.
  919. Needs to be set before org.el is loaded."
  920. :group 'org-link-follow
  921. :type 'boolean)
  922. (defcustom org-mark-ring-length 4
  923. "Number of different positions to be recorded in the ring
  924. Changing this requires a restart of Emacs to work correctly."
  925. :group 'org-link-follow
  926. :type 'integer)
  927. (defcustom org-link-frame-setup
  928. '((vm . vm-visit-folder-other-frame)
  929. (gnus . gnus-other-frame)
  930. (file . find-file-other-window))
  931. "Setup the frame configuration for following links.
  932. When following a link with Emacs, it may often be useful to display
  933. this link in another window or frame. This variable can be used to
  934. set this up for the different types of links.
  935. For VM, use any of
  936. `vm-visit-folder'
  937. `vm-visit-folder-other-frame'
  938. For Gnus, use any of
  939. `gnus'
  940. `gnus-other-frame'
  941. `org-gnus-no-new-news'
  942. For FILE, use any of
  943. `find-file'
  944. `find-file-other-window'
  945. `find-file-other-frame'
  946. For the calendar, use the variable `calendar-setup'.
  947. For BBDB, it is currently only possible to display the matches in
  948. another window."
  949. :group 'org-link-follow
  950. :type '(list
  951. (cons (const vm)
  952. (choice
  953. (const vm-visit-folder)
  954. (const vm-visit-folder-other-window)
  955. (const vm-visit-folder-other-frame)))
  956. (cons (const gnus)
  957. (choice
  958. (const gnus)
  959. (const gnus-other-frame)
  960. (const org-gnus-no-new-news)))
  961. (cons (const file)
  962. (choice
  963. (const find-file)
  964. (const find-file-other-window)
  965. (const find-file-other-frame)))))
  966. (defcustom org-display-internal-link-with-indirect-buffer nil
  967. "Non-nil means, use indirect buffer to display infile links.
  968. Activating internal links (from one location in a file to another location
  969. in the same file) normally just jumps to the location. When the link is
  970. activated with a C-u prefix (or with mouse-3), the link is displayed in
  971. another window. When this option is set, the other window actually displays
  972. an indirect buffer clone of the current buffer, to avoid any visibility
  973. changes to the current buffer."
  974. :group 'org-link-follow
  975. :type 'boolean)
  976. (defcustom org-open-non-existing-files nil
  977. "Non-nil means, `org-open-file' will open non-existing files.
  978. When nil, an error will be generated."
  979. :group 'org-link-follow
  980. :type 'boolean)
  981. (defcustom org-open-directory-means-index-dot-org nil
  982. "Non-nil means, a link to a directory really means to index.org.
  983. When nil, following a directory link will run dired or open a finder/explorer
  984. window on that directory."
  985. :group 'org-link-follow
  986. :type 'boolean)
  987. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  988. "Function and arguments to call for following mailto links.
  989. This is a list with the first element being a lisp function, and the
  990. remaining elements being arguments to the function. In string arguments,
  991. %a will be replaced by the address, and %s will be replaced by the subject
  992. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  993. :group 'org-link-follow
  994. :type '(choice
  995. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  996. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  997. (const :tag "message-mail" (message-mail "%a" "%s"))
  998. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  999. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1000. "Non-nil means, ask for confirmation before executing shell links.
  1001. Shell links can be dangerous: just think about a link
  1002. [[shell:rm -rf ~/*][Google Search]]
  1003. This link would show up in your Org-mode document as \"Google Search\",
  1004. but really it would remove your entire home directory.
  1005. Therefore we advise against setting this variable to nil.
  1006. Just change it to `y-or-n-p' of you want to confirm with a
  1007. single keystroke rather than having to type \"yes\"."
  1008. :group 'org-link-follow
  1009. :type '(choice
  1010. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1011. (const :tag "with y-or-n (faster)" y-or-n-p)
  1012. (const :tag "no confirmation (dangerous)" nil)))
  1013. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1014. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1015. Elisp links can be dangerous: just think about a link
  1016. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1017. This link would show up in your Org-mode document as \"Google Search\",
  1018. but really it would remove your entire home directory.
  1019. Therefore we advise against setting this variable to nil.
  1020. Just change it to `y-or-n-p' of you want to confirm with a
  1021. single keystroke rather than having to type \"yes\"."
  1022. :group 'org-link-follow
  1023. :type '(choice
  1024. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1025. (const :tag "with y-or-n (faster)" y-or-n-p)
  1026. (const :tag "no confirmation (dangerous)" nil)))
  1027. (defconst org-file-apps-defaults-gnu
  1028. '((remote . emacs)
  1029. (system . mailcap)
  1030. (t . mailcap))
  1031. "Default file applications on a UNIX or GNU/Linux system.
  1032. See `org-file-apps'.")
  1033. (defconst org-file-apps-defaults-macosx
  1034. '((remote . emacs)
  1035. (t . "open %s")
  1036. (system . "open %s")
  1037. ("ps.gz" . "gv %s")
  1038. ("eps.gz" . "gv %s")
  1039. ("dvi" . "xdvi %s")
  1040. ("fig" . "xfig %s"))
  1041. "Default file applications on a MacOS X system.
  1042. The system \"open\" is known as a default, but we use X11 applications
  1043. for some files for which the OS does not have a good default.
  1044. See `org-file-apps'.")
  1045. (defconst org-file-apps-defaults-windowsnt
  1046. (list
  1047. '(remote . emacs)
  1048. (cons t
  1049. (list (if (featurep 'xemacs)
  1050. 'mswindows-shell-execute
  1051. 'w32-shell-execute)
  1052. "open" 'file))
  1053. (cons 'system
  1054. (list (if (featurep 'xemacs)
  1055. 'mswindows-shell-execute
  1056. 'w32-shell-execute)
  1057. "open" 'file)))
  1058. "Default file applications on a Windows NT system.
  1059. The system \"open\" is used for most files.
  1060. See `org-file-apps'.")
  1061. (defcustom org-file-apps
  1062. '(
  1063. (auto-mode . emacs)
  1064. ("\\.x?html?\\'" . default)
  1065. ("\\.pdf\\'" . default)
  1066. )
  1067. "External applications for opening `file:path' items in a document.
  1068. Org-mode uses system defaults for different file types, but
  1069. you can use this variable to set the application for a given file
  1070. extension. The entries in this list are cons cells where the car identifies
  1071. files and the cdr the corresponding command. Possible values for the
  1072. file identifier are
  1073. \"regex\" Regular expression matched against the file name. For backward
  1074. compatibility, this can also be a string with only alphanumeric
  1075. characters, which is then interpreted as an extension.
  1076. `directory' Matches a directory
  1077. `remote' Matches a remote file, accessible through tramp or efs.
  1078. Remote files most likely should be visited through Emacs
  1079. because external applications cannot handle such paths.
  1080. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1081. so all files Emacs knows how to handle. Using this with
  1082. command `emacs' will open most files in Emacs. Beware that this
  1083. will also open html files inside Emacs, unless you add
  1084. (\"html\" . default) to the list as well.
  1085. t Default for files not matched by any of the other options.
  1086. `system' The system command to open files, like `open' on Windows
  1087. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1088. that will be selected if you call `C-c C-o' with a double
  1089. `C-u C-u' prefix.
  1090. Possible values for the command are:
  1091. `emacs' The file will be visited by the current Emacs process.
  1092. `default' Use the default application for this file type, which is the
  1093. association for t in the list, most likely in the system-specific
  1094. part.
  1095. This can be used to overrule an unwanted setting in the
  1096. system-specific variable.
  1097. `system' Use the system command for opening files, like \"open\".
  1098. This command is specified by the entry whose car is `system'.
  1099. Most likely, the system-specific version of this variable
  1100. does define this command, but you can overrule/replace it
  1101. here.
  1102. string A command to be executed by a shell; %s will be replaced
  1103. by the path to the file.
  1104. sexp A Lisp form which will be evaluated. The file path will
  1105. be available in the Lisp variable `file'.
  1106. For more examples, see the system specific constants
  1107. `org-file-apps-defaults-macosx'
  1108. `org-file-apps-defaults-windowsnt'
  1109. `org-file-apps-defaults-gnu'."
  1110. :group 'org-link-follow
  1111. :type '(repeat
  1112. (cons (choice :value ""
  1113. (string :tag "Extension")
  1114. (const :tag "System command to open files" system)
  1115. (const :tag "Default for unrecognized files" t)
  1116. (const :tag "Remote file" remote)
  1117. (const :tag "Links to a directory" directory)
  1118. (const :tag "Any files that have Emacs modes"
  1119. auto-mode))
  1120. (choice :value ""
  1121. (const :tag "Visit with Emacs" emacs)
  1122. (const :tag "Use default" default)
  1123. (const :tag "Use the system command" system)
  1124. (string :tag "Command")
  1125. (sexp :tag "Lisp form")))))
  1126. (defgroup org-refile nil
  1127. "Options concerning refiling entries in Org-mode."
  1128. :tag "Org Refile"
  1129. :group 'org)
  1130. (defcustom org-directory "~/org"
  1131. "Directory with org files.
  1132. This directory will be used as default to prompt for org files.
  1133. Used by the hooks for remember.el."
  1134. :group 'org-refile
  1135. :group 'org-remember
  1136. :type 'directory)
  1137. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1138. "Default target for storing notes.
  1139. Used by the hooks for remember.el. This can be a string, or nil to mean
  1140. the value of `remember-data-file'.
  1141. You can set this on a per-template basis with the variable
  1142. `org-remember-templates'."
  1143. :group 'org-refile
  1144. :group 'org-remember
  1145. :type '(choice
  1146. (const :tag "Default from remember-data-file" nil)
  1147. file))
  1148. (defcustom org-goto-interface 'outline
  1149. "The default interface to be used for `org-goto'.
  1150. Allowed values are:
  1151. outline The interface shows an outline of the relevant file
  1152. and the correct heading is found by moving through
  1153. the outline or by searching with incremental search.
  1154. outline-path-completion Headlines in the current buffer are offered via
  1155. completion. This is the interface also used by
  1156. the refile command."
  1157. :group 'org-refile
  1158. :type '(choice
  1159. (const :tag "Outline" outline)
  1160. (const :tag "Outline-path-completion" outline-path-completion)))
  1161. (defcustom org-goto-max-level 5
  1162. "Maximum level to be considered when running org-goto with refile interface."
  1163. :group 'org-refile
  1164. :type 'number)
  1165. (defcustom org-reverse-note-order nil
  1166. "Non-nil means, store new notes at the beginning of a file or entry.
  1167. When nil, new notes will be filed to the end of a file or entry.
  1168. This can also be a list with cons cells of regular expressions that
  1169. are matched against file names, and values."
  1170. :group 'org-remember
  1171. :group 'org-refile
  1172. :type '(choice
  1173. (const :tag "Reverse always" t)
  1174. (const :tag "Reverse never" nil)
  1175. (repeat :tag "By file name regexp"
  1176. (cons regexp boolean))))
  1177. (defcustom org-refile-targets nil
  1178. "Targets for refiling entries with \\[org-refile].
  1179. This is list of cons cells. Each cell contains:
  1180. - a specification of the files to be considered, either a list of files,
  1181. or a symbol whose function or variable value will be used to retrieve
  1182. a file name or a list of file names. If you use `org-agenda-files' for
  1183. that, all agenda files will be scanned for targets. Nil means, consider
  1184. headings in the current buffer.
  1185. - A specification of how to select find candidate refile targets. This
  1186. may be any of
  1187. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1188. This tag has to be present in all target headlines, inheritance will
  1189. not be considered.
  1190. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1191. todo keyword.
  1192. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1193. headlines that are refiling targets.
  1194. - a cons cell (:level . N). Any headline of level N is considered a target.
  1195. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1196. When this variable is nil, all top-level headlines in the current buffer
  1197. are used, equivalent to the value `((nil . (:level . 1))'."
  1198. :group 'org-refile
  1199. :type '(repeat
  1200. (cons
  1201. (choice :value org-agenda-files
  1202. (const :tag "All agenda files" org-agenda-files)
  1203. (const :tag "Current buffer" nil)
  1204. (function) (variable) (file))
  1205. (choice :tag "Identify target headline by"
  1206. (cons :tag "Specific tag" (const :value :tag) (string))
  1207. (cons :tag "TODO keyword" (const :value :todo) (string))
  1208. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1209. (cons :tag "Level number" (const :value :level) (integer))
  1210. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1211. (defcustom org-refile-use-outline-path nil
  1212. "Non-nil means, provide refile targets as paths.
  1213. So a level 3 headline will be available as level1/level2/level3.
  1214. When the value is `file', also include the file name (without directory)
  1215. into the path. When `full-file-path', include the full file path."
  1216. :group 'org-refile
  1217. :type '(choice
  1218. (const :tag "Not" nil)
  1219. (const :tag "Yes" t)
  1220. (const :tag "Start with file name" file)
  1221. (const :tag "Start with full file path" full-file-path)))
  1222. (defcustom org-outline-path-complete-in-steps t
  1223. "Non-nil means, complete the outline path in hierarchical steps.
  1224. When Org-mode uses the refile interface to select an outline path
  1225. \(see variable `org-refile-use-outline-path'), the completion of
  1226. the path can be done is a single go, or if can be done in steps down
  1227. the headline hierarchy. Going in steps is probably the best if you
  1228. do not use a special completion package like `ido' or `icicles'.
  1229. However, when using these packages, going in one step can be very
  1230. fast, while still showing the whole path to the entry."
  1231. :group 'org-refile
  1232. :type 'boolean)
  1233. (defgroup org-todo nil
  1234. "Options concerning TODO items in Org-mode."
  1235. :tag "Org TODO"
  1236. :group 'org)
  1237. (defgroup org-progress nil
  1238. "Options concerning Progress logging in Org-mode."
  1239. :tag "Org Progress"
  1240. :group 'org-time)
  1241. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1242. "List of TODO entry keyword sequences and their interpretation.
  1243. \\<org-mode-map>This is a list of sequences.
  1244. Each sequence starts with a symbol, either `sequence' or `type',
  1245. indicating if the keywords should be interpreted as a sequence of
  1246. action steps, or as different types of TODO items. The first
  1247. keywords are states requiring action - these states will select a headline
  1248. for inclusion into the global TODO list Org-mode produces. If one of
  1249. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1250. signify that no further action is necessary. If \"|\" is not found,
  1251. the last keyword is treated as the only DONE state of the sequence.
  1252. The command \\[org-todo] cycles an entry through these states, and one
  1253. additional state where no keyword is present. For details about this
  1254. cycling, see the manual.
  1255. TODO keywords and interpretation can also be set on a per-file basis with
  1256. the special #+SEQ_TODO and #+TYP_TODO lines.
  1257. Each keyword can optionally specify a character for fast state selection
  1258. \(in combination with the variable `org-use-fast-todo-selection')
  1259. and specifiers for state change logging, using the same syntax
  1260. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1261. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1262. indicates to record a time stamp each time this state is selected.
  1263. Each keyword may also specify if a timestamp or a note should be
  1264. recorded when entering or leaving the state, by adding additional
  1265. characters in the parenthesis after the keyword. This looks like this:
  1266. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1267. record only the time of the state change. With X and Y being either
  1268. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1269. Y when leaving the state if and only if the *target* state does not
  1270. define X. You may omit any of the fast-selection key or X or /Y,
  1271. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1272. For backward compatibility, this variable may also be just a list
  1273. of keywords - in this case the interpretation (sequence or type) will be
  1274. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1275. :group 'org-todo
  1276. :group 'org-keywords
  1277. :type '(choice
  1278. (repeat :tag "Old syntax, just keywords"
  1279. (string :tag "Keyword"))
  1280. (repeat :tag "New syntax"
  1281. (cons
  1282. (choice
  1283. :tag "Interpretation"
  1284. (const :tag "Sequence (cycling hits every state)" sequence)
  1285. (const :tag "Type (cycling directly to DONE)" type))
  1286. (repeat
  1287. (string :tag "Keyword"))))))
  1288. (defvar org-todo-keywords-1 nil
  1289. "All TODO and DONE keywords active in a buffer.")
  1290. (make-variable-buffer-local 'org-todo-keywords-1)
  1291. (defvar org-todo-keywords-for-agenda nil)
  1292. (defvar org-done-keywords-for-agenda nil)
  1293. (defvar org-todo-keyword-alist-for-agenda nil)
  1294. (defvar org-tag-alist-for-agenda nil)
  1295. (defvar org-agenda-contributing-files nil)
  1296. (defvar org-not-done-keywords nil)
  1297. (make-variable-buffer-local 'org-not-done-keywords)
  1298. (defvar org-done-keywords nil)
  1299. (make-variable-buffer-local 'org-done-keywords)
  1300. (defvar org-todo-heads nil)
  1301. (make-variable-buffer-local 'org-todo-heads)
  1302. (defvar org-todo-sets nil)
  1303. (make-variable-buffer-local 'org-todo-sets)
  1304. (defvar org-todo-log-states nil)
  1305. (make-variable-buffer-local 'org-todo-log-states)
  1306. (defvar org-todo-kwd-alist nil)
  1307. (make-variable-buffer-local 'org-todo-kwd-alist)
  1308. (defvar org-todo-key-alist nil)
  1309. (make-variable-buffer-local 'org-todo-key-alist)
  1310. (defvar org-todo-key-trigger nil)
  1311. (make-variable-buffer-local 'org-todo-key-trigger)
  1312. (defcustom org-todo-interpretation 'sequence
  1313. "Controls how TODO keywords are interpreted.
  1314. This variable is in principle obsolete and is only used for
  1315. backward compatibility, if the interpretation of todo keywords is
  1316. not given already in `org-todo-keywords'. See that variable for
  1317. more information."
  1318. :group 'org-todo
  1319. :group 'org-keywords
  1320. :type '(choice (const sequence)
  1321. (const type)))
  1322. (defcustom org-use-fast-todo-selection 'prefix
  1323. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1324. This variable describes if and under what circumstances the cycling
  1325. mechanism for TODO keywords will be replaced by a single-key, direct
  1326. selection scheme.
  1327. When nil, fast selection is never used.
  1328. When the symbol `prefix', it will be used when `org-todo' is called with
  1329. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1330. in an agenda buffer.
  1331. When t, fast selection is used by default. In this case, the prefix
  1332. argument forces cycling instead.
  1333. In all cases, the special interface is only used if access keys have actually
  1334. been assigned by the user, i.e. if keywords in the configuration are followed
  1335. by a letter in parenthesis, like TODO(t)."
  1336. :group 'org-todo
  1337. :type '(choice
  1338. (const :tag "Never" nil)
  1339. (const :tag "By default" t)
  1340. (const :tag "Only with C-u C-c C-t" prefix)))
  1341. (defcustom org-provide-todo-statistics t
  1342. "Non-nil means, update todo statistics after insert and toggle.
  1343. When this is set, todo statistics is updated in the parent of the current
  1344. entry each time a todo state is changed."
  1345. :group 'org-todo
  1346. :type 'boolean)
  1347. (defcustom org-after-todo-state-change-hook nil
  1348. "Hook which is run after the state of a TODO item was changed.
  1349. The new state (a string with a TODO keyword, or nil) is available in the
  1350. Lisp variable `state'."
  1351. :group 'org-todo
  1352. :type 'hook)
  1353. (defcustom org-todo-state-tags-triggers nil
  1354. "Tag changes that should be triggered by TODO state changes.
  1355. This is a list. Each entry is
  1356. (state-change (tag . flag) .......)
  1357. State-change can be a string with a state, and empty string to indicate the
  1358. state that has no TODO keyword, or it can be one of the symbols `todo'
  1359. or `done', meaning any not-done or done state, respectively."
  1360. :group 'org-todo
  1361. :group 'org-tags
  1362. :type '(repeat
  1363. (cons (choice :tag "When changing to"
  1364. (const :tag "Not-done state" todo)
  1365. (const :tag "Done state" done)
  1366. (string :tag "State"))
  1367. (repeat
  1368. (cons :tag "Tag action"
  1369. (string :tag "Tag")
  1370. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1371. (defcustom org-log-done nil
  1372. "Information to record when a task moves to the DONE state.
  1373. Possible values are:
  1374. nil Don't add anything, just change the keyword
  1375. time Add a time stamp to the task
  1376. note Prompt a closing note and add it with template `org-log-note-headings'
  1377. This option can also be set with on a per-file-basis with
  1378. #+STARTUP: nologdone
  1379. #+STARTUP: logdone
  1380. #+STARTUP: lognotedone
  1381. You can have local logging settings for a subtree by setting the LOGGING
  1382. property to one or more of these keywords."
  1383. :group 'org-todo
  1384. :group 'org-progress
  1385. :type '(choice
  1386. (const :tag "No logging" nil)
  1387. (const :tag "Record CLOSED timestamp" time)
  1388. (const :tag "Record CLOSED timestamp with closing note." note)))
  1389. ;; Normalize old uses of org-log-done.
  1390. (cond
  1391. ((eq org-log-done t) (setq org-log-done 'time))
  1392. ((and (listp org-log-done) (memq 'done org-log-done))
  1393. (setq org-log-done 'note)))
  1394. (defcustom org-log-note-clock-out nil
  1395. "Non-nil means, record a note when clocking out of an item.
  1396. This can also be configured on a per-file basis by adding one of
  1397. the following lines anywhere in the buffer:
  1398. #+STARTUP: lognoteclock-out
  1399. #+STARTUP: nolognoteclock-out"
  1400. :group 'org-todo
  1401. :group 'org-progress
  1402. :type 'boolean)
  1403. (defcustom org-log-done-with-time t
  1404. "Non-nil means, the CLOSED time stamp will contain date and time.
  1405. When nil, only the date will be recorded."
  1406. :group 'org-progress
  1407. :type 'boolean)
  1408. (defcustom org-log-note-headings
  1409. '((done . "CLOSING NOTE %t")
  1410. (state . "State %-12s %t")
  1411. (note . "Note taken on %t")
  1412. (clock-out . ""))
  1413. "Headings for notes added to entries.
  1414. The value is an alist, with the car being a symbol indicating the note
  1415. context, and the cdr is the heading to be used. The heading may also be the
  1416. empty string.
  1417. %t in the heading will be replaced by a time stamp.
  1418. %s will be replaced by the new TODO state, in double quotes.
  1419. %u will be replaced by the user name.
  1420. %U will be replaced by the full user name."
  1421. :group 'org-todo
  1422. :group 'org-progress
  1423. :type '(list :greedy t
  1424. (cons (const :tag "Heading when closing an item" done) string)
  1425. (cons (const :tag
  1426. "Heading when changing todo state (todo sequence only)"
  1427. state) string)
  1428. (cons (const :tag "Heading when just taking a note" note) string)
  1429. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1430. (unless (assq 'note org-log-note-headings)
  1431. (push '(note . "%t") org-log-note-headings))
  1432. (defcustom org-log-state-notes-insert-after-drawers nil
  1433. "Non-nil means, insert state change notes after any drawers in entry.
  1434. Only the drawers that *immediately* follow the headline and the
  1435. deadline/scheduled line are skipped.
  1436. When nil, insert notes right after the heading and perhaps the line
  1437. with deadline/scheduling if present."
  1438. :group 'org-todo
  1439. :group 'org-progress
  1440. :type 'boolean)
  1441. (defcustom org-log-states-order-reversed t
  1442. "Non-nil means, the latest state change note will be directly after heading.
  1443. When nil, the notes will be orderer according to time."
  1444. :group 'org-todo
  1445. :group 'org-progress
  1446. :type 'boolean)
  1447. (defcustom org-log-repeat 'time
  1448. "Non-nil means, record moving through the DONE state when triggering repeat.
  1449. An auto-repeating tasks is immediately switched back to TODO when marked
  1450. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1451. the TODO keyword definition, or recording a closing note by setting
  1452. `org-log-done', there will be no record of the task moving through DONE.
  1453. This variable forces taking a note anyway. Possible values are:
  1454. nil Don't force a record
  1455. time Record a time stamp
  1456. note Record a note
  1457. This option can also be set with on a per-file-basis with
  1458. #+STARTUP: logrepeat
  1459. #+STARTUP: lognoterepeat
  1460. #+STARTUP: nologrepeat
  1461. You can have local logging settings for a subtree by setting the LOGGING
  1462. property to one or more of these keywords."
  1463. :group 'org-todo
  1464. :group 'org-progress
  1465. :type '(choice
  1466. (const :tag "Don't force a record" nil)
  1467. (const :tag "Force recording the DONE state" time)
  1468. (const :tag "Force recording a note with the DONE state" note)))
  1469. (defgroup org-priorities nil
  1470. "Priorities in Org-mode."
  1471. :tag "Org Priorities"
  1472. :group 'org-todo)
  1473. (defcustom org-highest-priority ?A
  1474. "The highest priority of TODO items. A character like ?A, ?B etc.
  1475. Must have a smaller ASCII number than `org-lowest-priority'."
  1476. :group 'org-priorities
  1477. :type 'character)
  1478. (defcustom org-lowest-priority ?C
  1479. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1480. Must have a larger ASCII number than `org-highest-priority'."
  1481. :group 'org-priorities
  1482. :type 'character)
  1483. (defcustom org-default-priority ?B
  1484. "The default priority of TODO items.
  1485. This is the priority an item get if no explicit priority is given."
  1486. :group 'org-priorities
  1487. :type 'character)
  1488. (defcustom org-priority-start-cycle-with-default t
  1489. "Non-nil means, start with default priority when starting to cycle.
  1490. When this is nil, the first step in the cycle will be (depending on the
  1491. command used) one higher or lower that the default priority."
  1492. :group 'org-priorities
  1493. :type 'boolean)
  1494. (defgroup org-time nil
  1495. "Options concerning time stamps and deadlines in Org-mode."
  1496. :tag "Org Time"
  1497. :group 'org)
  1498. (defcustom org-insert-labeled-timestamps-at-point nil
  1499. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1500. When nil, these labeled time stamps are forces into the second line of an
  1501. entry, just after the headline. When scheduling from the global TODO list,
  1502. the time stamp will always be forced into the second line."
  1503. :group 'org-time
  1504. :type 'boolean)
  1505. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1506. "Formats for `format-time-string' which are used for time stamps.
  1507. It is not recommended to change this constant.")
  1508. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1509. "Number of minutes to round time stamps to.
  1510. These are two values, the first applies when first creating a time stamp.
  1511. The second applies when changing it with the commands `S-up' and `S-down'.
  1512. When changing the time stamp, this means that it will change in steps
  1513. of N minutes, as given by the second value.
  1514. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1515. numbers should be factors of 60, so for example 5, 10, 15.
  1516. When this is larger than 1, you can still force an exact time-stamp by using
  1517. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1518. and by using a prefix arg to `S-up/down' to specify the exact number
  1519. of minutes to shift."
  1520. :group 'org-time
  1521. :get '(lambda (var) ; Make sure all entries have 5 elements
  1522. (if (integerp (default-value var))
  1523. (list (default-value var) 5)
  1524. (default-value var)))
  1525. :type '(list
  1526. (integer :tag "when inserting times")
  1527. (integer :tag "when modifying times")))
  1528. ;; Normalize old customizations of this variable.
  1529. (when (integerp org-time-stamp-rounding-minutes)
  1530. (setq org-time-stamp-rounding-minutes
  1531. (list org-time-stamp-rounding-minutes
  1532. org-time-stamp-rounding-minutes)))
  1533. (defcustom org-display-custom-times nil
  1534. "Non-nil means, overlay custom formats over all time stamps.
  1535. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1536. To turn this on on a per-file basis, insert anywhere in the file:
  1537. #+STARTUP: customtime"
  1538. :group 'org-time
  1539. :set 'set-default
  1540. :type 'sexp)
  1541. (make-variable-buffer-local 'org-display-custom-times)
  1542. (defcustom org-time-stamp-custom-formats
  1543. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1544. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1545. These are overlayed over the default ISO format if the variable
  1546. `org-display-custom-times' is set. Time like %H:%M should be at the
  1547. end of the second format."
  1548. :group 'org-time
  1549. :type 'sexp)
  1550. (defun org-time-stamp-format (&optional long inactive)
  1551. "Get the right format for a time string."
  1552. (let ((f (if long (cdr org-time-stamp-formats)
  1553. (car org-time-stamp-formats))))
  1554. (if inactive
  1555. (concat "[" (substring f 1 -1) "]")
  1556. f)))
  1557. (defcustom org-time-clocksum-format "%d:%02d"
  1558. "The format string used when creating CLOCKSUM lines, or when
  1559. org-mode generates a time duration."
  1560. :group 'org-time
  1561. :type 'string)
  1562. (defcustom org-deadline-warning-days 14
  1563. "No. of days before expiration during which a deadline becomes active.
  1564. This variable governs the display in sparse trees and in the agenda.
  1565. When 0 or negative, it means use this number (the absolute value of it)
  1566. even if a deadline has a different individual lead time specified."
  1567. :group 'org-time
  1568. :group 'org-agenda-daily/weekly
  1569. :type 'number)
  1570. (defcustom org-read-date-prefer-future t
  1571. "Non-nil means, assume future for incomplete date input from user.
  1572. This affects the following situations:
  1573. 1. The user gives a day, but no month.
  1574. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1575. read this as the third of *next* month. However, if you enter \"17\",
  1576. it will be considered as *this* month.
  1577. 2. The user gives a month but not a year.
  1578. For example, if it is april and you enter \"feb 2\", this will be read
  1579. as feb 2, *next* year. \"May 5\", however, will be this year.
  1580. Currently this does not work for ISO week specifications.
  1581. When this option is nil, the current month and year will always be used
  1582. as defaults."
  1583. :group 'org-time
  1584. :type 'boolean)
  1585. (defcustom org-read-date-display-live t
  1586. "Non-nil means, display current interpretation of date prompt live.
  1587. This display will be in an overlay, in the minibuffer."
  1588. :group 'org-time
  1589. :type 'boolean)
  1590. (defcustom org-read-date-popup-calendar t
  1591. "Non-nil means, pop up a calendar when prompting for a date.
  1592. In the calendar, the date can be selected with mouse-1. However, the
  1593. minibuffer will also be active, and you can simply enter the date as well.
  1594. When nil, only the minibuffer will be available."
  1595. :group 'org-time
  1596. :type 'boolean)
  1597. (if (fboundp 'defvaralias)
  1598. (defvaralias 'org-popup-calendar-for-date-prompt
  1599. 'org-read-date-popup-calendar))
  1600. (defcustom org-extend-today-until 0
  1601. "The hour when your day really ends. Must be an integer.
  1602. This has influence for the following applications:
  1603. - When switching the agenda to \"today\". It it is still earlier than
  1604. the time given here, the day recognized as TODAY is actually yesterday.
  1605. - When a date is read from the user and it is still before the time given
  1606. here, the current date and time will be assumed to be yesterday, 23:59.
  1607. Also, timestamps inserted in remember templates follow this rule.
  1608. IMPORTANT: This is a feature whose implementation is and likely will
  1609. remain incomplete. Really, it is only here because past midnight seems to
  1610. be the favorite working time of John Wiegley :-)"
  1611. :group 'org-time
  1612. :type 'number)
  1613. (defcustom org-edit-timestamp-down-means-later nil
  1614. "Non-nil means, S-down will increase the time in a time stamp.
  1615. When nil, S-up will increase."
  1616. :group 'org-time
  1617. :type 'boolean)
  1618. (defcustom org-calendar-follow-timestamp-change t
  1619. "Non-nil means, make the calendar window follow timestamp changes.
  1620. When a timestamp is modified and the calendar window is visible, it will be
  1621. moved to the new date."
  1622. :group 'org-time
  1623. :type 'boolean)
  1624. (defgroup org-tags nil
  1625. "Options concerning tags in Org-mode."
  1626. :tag "Org Tags"
  1627. :group 'org)
  1628. (defcustom org-tag-alist nil
  1629. "List of tags allowed in Org-mode files.
  1630. When this list is nil, Org-mode will base TAG input on what is already in the
  1631. buffer.
  1632. The value of this variable is an alist, the car of each entry must be a
  1633. keyword as a string, the cdr may be a character that is used to select
  1634. that tag through the fast-tag-selection interface.
  1635. See the manual for details."
  1636. :group 'org-tags
  1637. :type '(repeat
  1638. (choice
  1639. (cons (string :tag "Tag name")
  1640. (character :tag "Access char"))
  1641. (const :tag "Start radio group" (:startgroup))
  1642. (const :tag "End radio group" (:endgroup)))))
  1643. (defvar org-file-tags nil
  1644. "List of tags that can be inherited by all entries in the file.
  1645. The tags will be inherited if the variable `org-use-tag-inheritance'
  1646. says they should be.
  1647. This variable is populated from #+TAG lines.")
  1648. (defcustom org-use-fast-tag-selection 'auto
  1649. "Non-nil means, use fast tag selection scheme.
  1650. This is a special interface to select and deselect tags with single keys.
  1651. When nil, fast selection is never used.
  1652. When the symbol `auto', fast selection is used if and only if selection
  1653. characters for tags have been configured, either through the variable
  1654. `org-tag-alist' or through a #+TAGS line in the buffer.
  1655. When t, fast selection is always used and selection keys are assigned
  1656. automatically if necessary."
  1657. :group 'org-tags
  1658. :type '(choice
  1659. (const :tag "Always" t)
  1660. (const :tag "Never" nil)
  1661. (const :tag "When selection characters are configured" 'auto)))
  1662. (defcustom org-fast-tag-selection-single-key nil
  1663. "Non-nil means, fast tag selection exits after first change.
  1664. When nil, you have to press RET to exit it.
  1665. During fast tag selection, you can toggle this flag with `C-c'.
  1666. This variable can also have the value `expert'. In this case, the window
  1667. displaying the tags menu is not even shown, until you press C-c again."
  1668. :group 'org-tags
  1669. :type '(choice
  1670. (const :tag "No" nil)
  1671. (const :tag "Yes" t)
  1672. (const :tag "Expert" expert)))
  1673. (defvar org-fast-tag-selection-include-todo nil
  1674. "Non-nil means, fast tags selection interface will also offer TODO states.
  1675. This is an undocumented feature, you should not rely on it.")
  1676. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1677. "The column to which tags should be indented in a headline.
  1678. If this number is positive, it specifies the column. If it is negative,
  1679. it means that the tags should be flushright to that column. For example,
  1680. -80 works well for a normal 80 character screen."
  1681. :group 'org-tags
  1682. :type 'integer)
  1683. (defcustom org-auto-align-tags t
  1684. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1685. These operations change the length of a headline and therefore shift
  1686. the tags around. With this options turned on, after each such operation
  1687. the tags are again aligned to `org-tags-column'."
  1688. :group 'org-tags
  1689. :type 'boolean)
  1690. (defcustom org-use-tag-inheritance t
  1691. "Non-nil means, tags in levels apply also for sublevels.
  1692. When nil, only the tags directly given in a specific line apply there.
  1693. This may also be a list of tags that should be inherited, or a regexp that
  1694. matches tags that should be inherited. Additional control is possible
  1695. with the variable `org-tags-exclude-from-inheritance' which gives an
  1696. explicit list of tags to be excluded from inheritance., even if the value of
  1697. `org-use-tag-inheritance' would select it for inheritance.
  1698. If this option is t, a match early-on in a tree can lead to a large
  1699. number of matches in the subtree when constructing the agenda or creating
  1700. a sparse tree. If you only want to see the first match in a tree during
  1701. a search, check out the variable `org-tags-match-list-sublevels'."
  1702. :group 'org-tags
  1703. :type '(choice
  1704. (const :tag "Not" nil)
  1705. (const :tag "Always" t)
  1706. (repeat :tag "Specific tags" (string :tag "Tag"))
  1707. (regexp :tag "Tags matched by regexp")))
  1708. (defcustom org-tags-exclude-from-inheritance nil
  1709. "List of tags that should never be inherited.
  1710. This is a way to exclude a few tags from inheritance. For way to do
  1711. the opposite, to actively allow inheritance for selected tags,
  1712. see the variable `org-use-tag-inheritance'."
  1713. :group 'org-tags
  1714. :type '(repeat (string :tag "Tag")))
  1715. (defun org-tag-inherit-p (tag)
  1716. "Check if TAG is one that should be inherited."
  1717. (cond
  1718. ((member tag org-tags-exclude-from-inheritance) nil)
  1719. ((eq org-use-tag-inheritance t) t)
  1720. ((not org-use-tag-inheritance) nil)
  1721. ((stringp org-use-tag-inheritance)
  1722. (string-match org-use-tag-inheritance tag))
  1723. ((listp org-use-tag-inheritance)
  1724. (member tag org-use-tag-inheritance))
  1725. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1726. (defcustom org-tags-match-list-sublevels t
  1727. "Non-nil means list also sublevels of headlines matching tag search.
  1728. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1729. the sublevels of a headline matching a tag search often also match
  1730. the same search. Listing all of them can create very long lists.
  1731. Setting this variable to nil causes subtrees of a match to be skipped.
  1732. This option is off by default, because inheritance in on. If you turn
  1733. inheritance off, you very likely want to turn this option on.
  1734. As a special case, if the tag search is restricted to TODO items, the
  1735. value of this variable is ignored and sublevels are always checked, to
  1736. make sure all corresponding TODO items find their way into the list.
  1737. This variable is semi-obsolete and probably should always be true. It
  1738. is better to limit inheritance to certain tags using the variables
  1739. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1740. :group 'org-tags
  1741. :type 'boolean)
  1742. (defvar org-tags-history nil
  1743. "History of minibuffer reads for tags.")
  1744. (defvar org-last-tags-completion-table nil
  1745. "The last used completion table for tags.")
  1746. (defvar org-after-tags-change-hook nil
  1747. "Hook that is run after the tags in a line have changed.")
  1748. (defgroup org-properties nil
  1749. "Options concerning properties in Org-mode."
  1750. :tag "Org Properties"
  1751. :group 'org)
  1752. (defcustom org-property-format "%-10s %s"
  1753. "How property key/value pairs should be formatted by `indent-line'.
  1754. When `indent-line' hits a property definition, it will format the line
  1755. according to this format, mainly to make sure that the values are
  1756. lined-up with respect to each other."
  1757. :group 'org-properties
  1758. :type 'string)
  1759. (defcustom org-use-property-inheritance nil
  1760. "Non-nil means, properties apply also for sublevels.
  1761. This setting is chiefly used during property searches. Turning it on can
  1762. cause significant overhead when doing a search, which is why it is not
  1763. on by default.
  1764. When nil, only the properties directly given in the current entry count.
  1765. When t, every property is inherited. The value may also be a list of
  1766. properties that should have inheritance, or a regular expression matching
  1767. properties that should be inherited.
  1768. However, note that some special properties use inheritance under special
  1769. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1770. and the properties ending in \"_ALL\" when they are used as descriptor
  1771. for valid values of a property.
  1772. Note for programmers:
  1773. When querying an entry with `org-entry-get', you can control if inheritance
  1774. should be used. By default, `org-entry-get' looks only at the local
  1775. properties. You can request inheritance by setting the inherit argument
  1776. to t (to force inheritance) or to `selective' (to respect the setting
  1777. in this variable)."
  1778. :group 'org-properties
  1779. :type '(choice
  1780. (const :tag "Not" nil)
  1781. (const :tag "Always" t)
  1782. (repeat :tag "Specific properties" (string :tag "Property"))
  1783. (regexp :tag "Properties matched by regexp")))
  1784. (defun org-property-inherit-p (property)
  1785. "Check if PROPERTY is one that should be inherited."
  1786. (cond
  1787. ((eq org-use-property-inheritance t) t)
  1788. ((not org-use-property-inheritance) nil)
  1789. ((stringp org-use-property-inheritance)
  1790. (string-match org-use-property-inheritance property))
  1791. ((listp org-use-property-inheritance)
  1792. (member property org-use-property-inheritance))
  1793. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1794. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1795. "The default column format, if no other format has been defined.
  1796. This variable can be set on the per-file basis by inserting a line
  1797. #+COLUMNS: %25ITEM ....."
  1798. :group 'org-properties
  1799. :type 'string)
  1800. (defcustom org-columns-ellipses ".."
  1801. "The ellipses to be used when a field in column view is truncated.
  1802. When this is the empty string, as many characters as possible are shown,
  1803. but then there will be no visual indication that the field has been truncated.
  1804. When this is a string of length N, the last N characters of a truncated
  1805. field are replaced by this string. If the column is narrower than the
  1806. ellipses string, only part of the ellipses string will be shown."
  1807. :group 'org-properties
  1808. :type 'string)
  1809. (defcustom org-columns-modify-value-for-display-function nil
  1810. "Function that modifies values for display in column view.
  1811. For example, it can be used to cut out a certain part from a time stamp.
  1812. The function must take 2 arguments:
  1813. column-title The title of the column (*not* the property name)
  1814. value The value that should be modified.
  1815. The function should return the value that should be displayed,
  1816. or nil if the normal value should be used."
  1817. :group 'org-properties
  1818. :type 'function)
  1819. (defcustom org-effort-property "Effort"
  1820. "The property that is being used to keep track of effort estimates.
  1821. Effort estimates given in this property need to have the format H:MM."
  1822. :group 'org-properties
  1823. :group 'org-progress
  1824. :type '(string :tag "Property"))
  1825. (defconst org-global-properties-fixed
  1826. '(("VISIBILITY_ALL" . "folded children content all"))
  1827. "List of property/value pairs that can be inherited by any entry.
  1828. These are fixed values, for the preset properties.")
  1829. (defcustom org-global-properties nil
  1830. "List of property/value pairs that can be inherited by any entry.
  1831. You can set buffer-local values for the same purpose in the variable
  1832. `org-file-properties' this by adding lines like
  1833. #+PROPERTY: NAME VALUE"
  1834. :group 'org-properties
  1835. :type '(repeat
  1836. (cons (string :tag "Property")
  1837. (string :tag "Value"))))
  1838. (defvar org-file-properties nil
  1839. "List of property/value pairs that can be inherited by any entry.
  1840. Valid for the current buffer.
  1841. This variable is populated from #+PROPERTY lines.")
  1842. (make-variable-buffer-local 'org-file-properties)
  1843. (defgroup org-agenda nil
  1844. "Options concerning agenda views in Org-mode."
  1845. :tag "Org Agenda"
  1846. :group 'org)
  1847. (defvar org-category nil
  1848. "Variable used by org files to set a category for agenda display.
  1849. Such files should use a file variable to set it, for example
  1850. # -*- mode: org; org-category: \"ELisp\"
  1851. or contain a special line
  1852. #+CATEGORY: ELisp
  1853. If the file does not specify a category, then file's base name
  1854. is used instead.")
  1855. (make-variable-buffer-local 'org-category)
  1856. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1857. (defcustom org-agenda-files nil
  1858. "The files to be used for agenda display.
  1859. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1860. \\[org-remove-file]. You can also use customize to edit the list.
  1861. If an entry is a directory, all files in that directory that are matched by
  1862. `org-agenda-file-regexp' will be part of the file list.
  1863. If the value of the variable is not a list but a single file name, then
  1864. the list of agenda files is actually stored and maintained in that file, one
  1865. agenda file per line."
  1866. :group 'org-agenda
  1867. :type '(choice
  1868. (repeat :tag "List of files and directories" file)
  1869. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1870. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1871. "Regular expression to match files for `org-agenda-files'.
  1872. If any element in the list in that variable contains a directory instead
  1873. of a normal file, all files in that directory that are matched by this
  1874. regular expression will be included."
  1875. :group 'org-agenda
  1876. :type 'regexp)
  1877. (defcustom org-agenda-text-search-extra-files nil
  1878. "List of extra files to be searched by text search commands.
  1879. These files will be search in addition to the agenda files by the
  1880. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1881. Note that these files will only be searched for text search commands,
  1882. not for the other agenda views like todo lists, tag searches or the weekly
  1883. agenda. This variable is intended to list notes and possibly archive files
  1884. that should also be searched by these two commands.
  1885. In fact, if the first element in the list is the symbol `agenda-archives',
  1886. than all archive files of all agenda files will be added to the search
  1887. scope."
  1888. :group 'org-agenda
  1889. :type '(set :greedy t
  1890. (const :tag "Agenda Archives" agenda-archives)
  1891. (repeat :inline t (file))))
  1892. (if (fboundp 'defvaralias)
  1893. (defvaralias 'org-agenda-multi-occur-extra-files
  1894. 'org-agenda-text-search-extra-files))
  1895. (defcustom org-agenda-skip-unavailable-files nil
  1896. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1897. A nil value means to remove them, after a query, from the list."
  1898. :group 'org-agenda
  1899. :type 'boolean)
  1900. (defcustom org-calendar-to-agenda-key [?c]
  1901. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1902. The command `org-calendar-goto-agenda' will be bound to this key. The
  1903. default is the character `c' because then `c' can be used to switch back and
  1904. forth between agenda and calendar."
  1905. :group 'org-agenda
  1906. :type 'sexp)
  1907. (defcustom org-calendar-agenda-action-key [?k]
  1908. "The key to be installed in `calendar-mode-map' for agenda-action.
  1909. The command `org-agenda-action' will be bound to this key. The
  1910. default is the character `k' because we use the same key in the agenda."
  1911. :group 'org-agenda
  1912. :type 'sexp)
  1913. (eval-after-load "calendar"
  1914. '(progn
  1915. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1916. 'org-calendar-goto-agenda)
  1917. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1918. 'org-agenda-action)))
  1919. (defgroup org-latex nil
  1920. "Options for embedding LaTeX code into Org-mode."
  1921. :tag "Org LaTeX"
  1922. :group 'org)
  1923. (defcustom org-format-latex-options
  1924. '(:foreground default :background default :scale 1.0
  1925. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1926. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1927. "Options for creating images from LaTeX fragments.
  1928. This is a property list with the following properties:
  1929. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1930. `default' means use the foreground of the default face.
  1931. :background the background color, or \"Transparent\".
  1932. `default' means use the background of the default face.
  1933. :scale a scaling factor for the size of the images.
  1934. :html-foreground, :html-background, :html-scale
  1935. the same numbers for HTML export.
  1936. :matchers a list indicating which matchers should be used to
  1937. find LaTeX fragments. Valid members of this list are:
  1938. \"begin\" find environments
  1939. \"$\" find math expressions surrounded by $...$
  1940. \"$$\" find math expressions surrounded by $$....$$
  1941. \"\\(\" find math expressions surrounded by \\(...\\)
  1942. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1943. :group 'org-latex
  1944. :type 'plist)
  1945. (defcustom org-format-latex-header "\\documentclass{article}
  1946. \\usepackage{fullpage} % do not remove
  1947. \\usepackage{amssymb}
  1948. \\usepackage[usenames]{color}
  1949. \\usepackage{amsmath}
  1950. \\usepackage{latexsym}
  1951. \\usepackage[mathscr]{eucal}
  1952. \\pagestyle{empty} % do not remove"
  1953. "The document header used for processing LaTeX fragments."
  1954. :group 'org-latex
  1955. :type 'string)
  1956. (defgroup org-font-lock nil
  1957. "Font-lock settings for highlighting in Org-mode."
  1958. :tag "Org Font Lock"
  1959. :group 'org)
  1960. (defcustom org-level-color-stars-only nil
  1961. "Non-nil means fontify only the stars in each headline.
  1962. When nil, the entire headline is fontified.
  1963. Changing it requires restart of `font-lock-mode' to become effective
  1964. also in regions already fontified."
  1965. :group 'org-font-lock
  1966. :type 'boolean)
  1967. (defcustom org-hide-leading-stars nil
  1968. "Non-nil means, hide the first N-1 stars in a headline.
  1969. This works by using the face `org-hide' for these stars. This
  1970. face is white for a light background, and black for a dark
  1971. background. You may have to customize the face `org-hide' to
  1972. make this work.
  1973. Changing it requires restart of `font-lock-mode' to become effective
  1974. also in regions already fontified.
  1975. You may also set this on a per-file basis by adding one of the following
  1976. lines to the buffer:
  1977. #+STARTUP: hidestars
  1978. #+STARTUP: showstars"
  1979. :group 'org-font-lock
  1980. :type 'boolean)
  1981. (defcustom org-fontify-done-headline nil
  1982. "Non-nil means, change the face of a headline if it is marked DONE.
  1983. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1984. When this is non-nil, the headline after the keyword is set to the
  1985. `org-headline-done' as an additional indication."
  1986. :group 'org-font-lock
  1987. :type 'boolean)
  1988. (defcustom org-fontify-emphasized-text t
  1989. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1990. Changing this variable requires a restart of Emacs to take effect."
  1991. :group 'org-font-lock
  1992. :type 'boolean)
  1993. (defcustom org-highlight-latex-fragments-and-specials nil
  1994. "Non-nil means, fontify what is treated specially by the exporters."
  1995. :group 'org-font-lock
  1996. :type 'boolean)
  1997. (defcustom org-hide-emphasis-markers nil
  1998. "Non-nil mean font-lock should hide the emphasis marker characters."
  1999. :group 'org-font-lock
  2000. :type 'boolean)
  2001. (defvar org-emph-re nil
  2002. "Regular expression for matching emphasis.")
  2003. (defvar org-verbatim-re nil
  2004. "Regular expression for matching verbatim text.")
  2005. (defvar org-emphasis-regexp-components) ; defined just below
  2006. (defvar org-emphasis-alist) ; defined just below
  2007. (defun org-set-emph-re (var val)
  2008. "Set variable and compute the emphasis regular expression."
  2009. (set var val)
  2010. (when (and (boundp 'org-emphasis-alist)
  2011. (boundp 'org-emphasis-regexp-components)
  2012. org-emphasis-alist org-emphasis-regexp-components)
  2013. (let* ((e org-emphasis-regexp-components)
  2014. (pre (car e))
  2015. (post (nth 1 e))
  2016. (border (nth 2 e))
  2017. (body (nth 3 e))
  2018. (nl (nth 4 e))
  2019. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  2020. (body1 (concat body "*?"))
  2021. (markers (mapconcat 'car org-emphasis-alist ""))
  2022. (vmarkers (mapconcat
  2023. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2024. org-emphasis-alist "")))
  2025. ;; make sure special characters appear at the right position in the class
  2026. (if (string-match "\\^" markers)
  2027. (setq markers (concat (replace-match "" t t markers) "^")))
  2028. (if (string-match "-" markers)
  2029. (setq markers (concat (replace-match "" t t markers) "-")))
  2030. (if (string-match "\\^" vmarkers)
  2031. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2032. (if (string-match "-" vmarkers)
  2033. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2034. (if (> nl 0)
  2035. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2036. (int-to-string nl) "\\}")))
  2037. ;; Make the regexp
  2038. (setq org-emph-re
  2039. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  2040. "\\("
  2041. "\\([" markers "]\\)"
  2042. "\\("
  2043. "[^" border "]\\|"
  2044. "[^" border (if (and nil stacked) markers) "]"
  2045. body1
  2046. "[^" border (if (and nil stacked) markers) "]"
  2047. "\\)"
  2048. "\\3\\)"
  2049. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  2050. (setq org-verbatim-re
  2051. (concat "\\([" pre "]\\|^\\)"
  2052. "\\("
  2053. "\\([" vmarkers "]\\)"
  2054. "\\("
  2055. "[^" border "]\\|"
  2056. "[^" border "]"
  2057. body1
  2058. "[^" border "]"
  2059. "\\)"
  2060. "\\3\\)"
  2061. "\\([" post "]\\|$\\)")))))
  2062. (defcustom org-emphasis-regexp-components
  2063. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2064. "Components used to build the regular expression for emphasis.
  2065. This is a list with 6 entries. Terminology: In an emphasis string
  2066. like \" *strong word* \", we call the initial space PREMATCH, the final
  2067. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2068. and \"trong wor\" is the body. The different components in this variable
  2069. specify what is allowed/forbidden in each part:
  2070. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2071. post Chars allowed as postmatch. End of line will be allowed too.
  2072. border The chars *forbidden* as border characters.
  2073. body-regexp A regexp like \".\" to match a body character. Don't use
  2074. non-shy groups here, and don't allow newline here.
  2075. newline The maximum number of newlines allowed in an emphasis exp.
  2076. Use customize to modify this, or restart Emacs after changing it."
  2077. :group 'org-font-lock
  2078. :set 'org-set-emph-re
  2079. :type '(list
  2080. (sexp :tag "Allowed chars in pre ")
  2081. (sexp :tag "Allowed chars in post ")
  2082. (sexp :tag "Forbidden chars in border ")
  2083. (sexp :tag "Regexp for body ")
  2084. (integer :tag "number of newlines allowed")
  2085. (option (boolean :tag "Please ignore this button"))))
  2086. (defcustom org-emphasis-alist
  2087. `(("*" bold "<b>" "</b>")
  2088. ("/" italic "<i>" "</i>")
  2089. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2090. ("=" org-code "<code>" "</code>" verbatim)
  2091. ("~" org-verbatim "<code>" "</code>" verbatim)
  2092. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2093. "<del>" "</del>")
  2094. )
  2095. "Special syntax for emphasized text.
  2096. Text starting and ending with a special character will be emphasized, for
  2097. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2098. characters, the face to be used by font-lock for highlighting in Org-mode
  2099. Emacs buffers, and the HTML tags to be used for this.
  2100. Use customize to modify this, or restart Emacs after changing it."
  2101. :group 'org-font-lock
  2102. :set 'org-set-emph-re
  2103. :type '(repeat
  2104. (list
  2105. (string :tag "Marker character")
  2106. (choice
  2107. (face :tag "Font-lock-face")
  2108. (plist :tag "Face property list"))
  2109. (string :tag "HTML start tag")
  2110. (string :tag "HTML end tag")
  2111. (option (const verbatim)))))
  2112. ;;; Miscellaneous options
  2113. (defgroup org-completion nil
  2114. "Completion in Org-mode."
  2115. :tag "Org Completion"
  2116. :group 'org)
  2117. (defcustom org-completion-use-ido nil
  2118. "Non-nil means, use ido completion wherever possible.
  2119. Note that `ido-mode' must be active for this variable to be relevant.
  2120. If you decide to turn this variable on, you might well want to turn off
  2121. `org-outline-path-complete-in-steps'."
  2122. :group 'org-completion
  2123. :type 'boolean)
  2124. (defcustom org-completion-fallback-command 'hippie-expand
  2125. "The expansion command called by \\[org-complete] in normal context.
  2126. Normal means, no org-mode-specific context."
  2127. :group 'org-completion
  2128. :type 'function)
  2129. ;;; Functions and variables from ther packages
  2130. ;; Declared here to avoid compiler warnings
  2131. ;; XEmacs only
  2132. (defvar outline-mode-menu-heading)
  2133. (defvar outline-mode-menu-show)
  2134. (defvar outline-mode-menu-hide)
  2135. (defvar zmacs-regions) ; XEmacs regions
  2136. ;; Emacs only
  2137. (defvar mark-active)
  2138. ;; Various packages
  2139. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2140. (declare-function calendar-forward-day "cal-move" (arg))
  2141. (declare-function calendar-goto-date "cal-move" (date))
  2142. (declare-function calendar-goto-today "cal-move" ())
  2143. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2144. (defvar calc-embedded-close-formula)
  2145. (defvar calc-embedded-open-formula)
  2146. (declare-function cdlatex-tab "ext:cdlatex" ())
  2147. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2148. (defvar font-lock-unfontify-region-function)
  2149. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2150. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2151. (defvar iswitchb-temp-buflist)
  2152. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2153. (declare-function org-agenda-skip "org-agenda" ())
  2154. (declare-function org-format-agenda-item "org-agenda"
  2155. (extra txt &optional category tags dotime noprefix remove-re))
  2156. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2157. (declare-function org-agenda-change-all-lines "org-agenda"
  2158. (newhead hdmarker &optional fixface just-this))
  2159. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2160. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2161. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2162. (beg end))
  2163. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2164. (declare-function parse-time-string "parse-time" (string))
  2165. (declare-function remember "remember" (&optional initial))
  2166. (declare-function remember-buffer-desc "remember" ())
  2167. (declare-function remember-finalize "remember" ())
  2168. (defvar remember-save-after-remembering)
  2169. (defvar remember-data-file)
  2170. (defvar remember-register)
  2171. (defvar remember-buffer)
  2172. (defvar remember-handler-functions)
  2173. (defvar remember-annotation-functions)
  2174. (defvar texmathp-why)
  2175. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2176. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2177. (defvar w3m-current-url)
  2178. (defvar w3m-current-title)
  2179. (defvar org-latex-regexps)
  2180. ;;; Autoload and prepare some org modules
  2181. ;; Some table stuff that needs to be defined here, because it is used
  2182. ;; by the functions setting up org-mode or checking for table context.
  2183. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2184. "Detects an org-type or table-type table.")
  2185. (defconst org-table-line-regexp "^[ \t]*|"
  2186. "Detects an org-type table line.")
  2187. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2188. "Detects an org-type table line.")
  2189. (defconst org-table-hline-regexp "^[ \t]*|-"
  2190. "Detects an org-type table hline.")
  2191. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2192. "Detects a table-type table hline.")
  2193. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2194. "Searching from within a table (any type) this finds the first line
  2195. outside the table.")
  2196. ;; Autoload the functions in org-table.el that are needed by functions here.
  2197. (eval-and-compile
  2198. (org-autoload "org-table"
  2199. '(org-table-align org-table-begin org-table-blank-field
  2200. org-table-convert org-table-convert-region org-table-copy-down
  2201. org-table-copy-region org-table-create
  2202. org-table-create-or-convert-from-region
  2203. org-table-create-with-table.el org-table-current-dline
  2204. org-table-cut-region org-table-delete-column org-table-edit-field
  2205. org-table-edit-formulas org-table-end org-table-eval-formula
  2206. org-table-export org-table-field-info
  2207. org-table-get-stored-formulas org-table-goto-column
  2208. org-table-hline-and-move org-table-import org-table-insert-column
  2209. org-table-insert-hline org-table-insert-row org-table-iterate
  2210. org-table-justify-field-maybe org-table-kill-row
  2211. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2212. org-table-move-column org-table-move-column-left
  2213. org-table-move-column-right org-table-move-row
  2214. org-table-move-row-down org-table-move-row-up
  2215. org-table-next-field org-table-next-row org-table-paste-rectangle
  2216. org-table-previous-field org-table-recalculate
  2217. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2218. org-table-toggle-coordinate-overlays
  2219. org-table-toggle-formula-debugger org-table-wrap-region
  2220. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2221. (defun org-at-table-p (&optional table-type)
  2222. "Return t if the cursor is inside an org-type table.
  2223. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2224. (if org-enable-table-editor
  2225. (save-excursion
  2226. (beginning-of-line 1)
  2227. (looking-at (if table-type org-table-any-line-regexp
  2228. org-table-line-regexp)))
  2229. nil))
  2230. (defsubst org-table-p () (org-at-table-p))
  2231. (defun org-at-table.el-p ()
  2232. "Return t if and only if we are at a table.el table."
  2233. (and (org-at-table-p 'any)
  2234. (save-excursion
  2235. (goto-char (org-table-begin 'any))
  2236. (looking-at org-table1-hline-regexp))))
  2237. (defun org-table-recognize-table.el ()
  2238. "If there is a table.el table nearby, recognize it and move into it."
  2239. (if org-table-tab-recognizes-table.el
  2240. (if (org-at-table.el-p)
  2241. (progn
  2242. (beginning-of-line 1)
  2243. (if (looking-at org-table-dataline-regexp)
  2244. nil
  2245. (if (looking-at org-table1-hline-regexp)
  2246. (progn
  2247. (beginning-of-line 2)
  2248. (if (looking-at org-table-any-border-regexp)
  2249. (beginning-of-line -1)))))
  2250. (if (re-search-forward "|" (org-table-end t) t)
  2251. (progn
  2252. (require 'table)
  2253. (if (table--at-cell-p (point))
  2254. t
  2255. (message "recognizing table.el table...")
  2256. (table-recognize-table)
  2257. (message "recognizing table.el table...done")))
  2258. (error "This should not happen..."))
  2259. t)
  2260. nil)
  2261. nil))
  2262. (defun org-at-table-hline-p ()
  2263. "Return t if the cursor is inside a hline in a table."
  2264. (if org-enable-table-editor
  2265. (save-excursion
  2266. (beginning-of-line 1)
  2267. (looking-at org-table-hline-regexp))
  2268. nil))
  2269. (defvar org-table-clean-did-remove-column nil)
  2270. (defun org-table-map-tables (function)
  2271. "Apply FUNCTION to the start of all tables in the buffer."
  2272. (save-excursion
  2273. (save-restriction
  2274. (widen)
  2275. (goto-char (point-min))
  2276. (while (re-search-forward org-table-any-line-regexp nil t)
  2277. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2278. (beginning-of-line 1)
  2279. (if (looking-at org-table-line-regexp)
  2280. (save-excursion (funcall function)))
  2281. (re-search-forward org-table-any-border-regexp nil 1))))
  2282. (message "Mapping tables: done"))
  2283. ;; Declare and autoload functions from org-exp.el
  2284. (declare-function org-default-export-plist "org-exp")
  2285. (declare-function org-infile-export-plist "org-exp")
  2286. (declare-function org-get-current-options "org-exp")
  2287. (eval-and-compile
  2288. (org-autoload "org-exp"
  2289. '(org-export org-export-as-ascii org-export-visible
  2290. org-insert-export-options-template org-export-as-html-and-open
  2291. org-export-as-html-batch org-export-as-html-to-buffer
  2292. org-replace-region-by-html org-export-region-as-html
  2293. org-export-as-html org-export-icalendar-this-file
  2294. org-export-icalendar-all-agenda-files
  2295. org-table-clean-before-export
  2296. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2297. ;; Declare and autoload functions from org-agenda.el
  2298. (eval-and-compile
  2299. (org-autoload "org-agenda"
  2300. '(org-agenda org-agenda-list org-search-view
  2301. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2302. org-diary org-agenda-to-appt)))
  2303. ;; Autoload org-remember
  2304. (eval-and-compile
  2305. (org-autoload "org-remember"
  2306. '(org-remember-insinuate org-remember-annotation
  2307. org-remember-apply-template org-remember org-remember-handler)))
  2308. ;; Autoload org-clock.el
  2309. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2310. (beg end))
  2311. (declare-function org-update-mode-line "org-clock" ())
  2312. (defvar org-clock-start-time)
  2313. (defvar org-clock-marker (make-marker)
  2314. "Marker recording the last clock-in.")
  2315. (eval-and-compile
  2316. (org-autoload
  2317. "org-clock"
  2318. '(org-clock-in org-clock-out org-clock-cancel
  2319. org-clock-goto org-clock-sum org-clock-display
  2320. org-remove-clock-overlays org-clock-report
  2321. org-clocktable-shift org-dblock-write:clocktable
  2322. org-get-clocktable)))
  2323. (defun org-clock-update-time-maybe ()
  2324. "If this is a CLOCK line, update it and return t.
  2325. Otherwise, return nil."
  2326. (interactive)
  2327. (save-excursion
  2328. (beginning-of-line 1)
  2329. (skip-chars-forward " \t")
  2330. (when (looking-at org-clock-string)
  2331. (let ((re (concat "[ \t]*" org-clock-string
  2332. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2333. "\\([ \t]*=>.*\\)?\\)?"))
  2334. ts te h m s neg)
  2335. (cond
  2336. ((not (looking-at re))
  2337. nil)
  2338. ((not (match-end 2))
  2339. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2340. (> org-clock-marker (point))
  2341. (<= org-clock-marker (point-at-eol)))
  2342. ;; The clock is running here
  2343. (setq org-clock-start-time
  2344. (apply 'encode-time
  2345. (org-parse-time-string (match-string 1))))
  2346. (org-update-mode-line)))
  2347. (t
  2348. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2349. (end-of-line 1)
  2350. (setq ts (match-string 1)
  2351. te (match-string 3))
  2352. (setq s (- (time-to-seconds
  2353. (apply 'encode-time (org-parse-time-string te)))
  2354. (time-to-seconds
  2355. (apply 'encode-time (org-parse-time-string ts))))
  2356. neg (< s 0)
  2357. s (abs s)
  2358. h (floor (/ s 3600))
  2359. s (- s (* 3600 h))
  2360. m (floor (/ s 60))
  2361. s (- s (* 60 s)))
  2362. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2363. t))))))
  2364. (defun org-check-running-clock ()
  2365. "Check if the current buffer contains the running clock.
  2366. If yes, offer to stop it and to save the buffer with the changes."
  2367. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2368. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2369. (buffer-name))))
  2370. (org-clock-out)
  2371. (when (y-or-n-p "Save changed buffer?")
  2372. (save-buffer))))
  2373. (defun org-clocktable-try-shift (dir n)
  2374. "Check if this line starts a clock table, if yes, shift the time block."
  2375. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2376. (org-clocktable-shift dir n)))
  2377. ;; Autoload org-timer.el
  2378. ;(declare-function org-timer "org-timer")
  2379. (eval-and-compile
  2380. (org-autoload
  2381. "org-timer"
  2382. '(org-timer-start org-timer org-timer-item
  2383. org-timer-change-times-in-region)))
  2384. ;; Autoload archiving code
  2385. ;; The stuff that is needed for cycling and tags has to be defined here.
  2386. (defgroup org-archive nil
  2387. "Options concerning archiving in Org-mode."
  2388. :tag "Org Archive"
  2389. :group 'org-structure)
  2390. (defcustom org-archive-location "%s_archive::"
  2391. "The location where subtrees should be archived.
  2392. The value of this variable is a string, consisting of two parts,
  2393. separated by a double-colon. The first part is a filename and
  2394. the second part is a headline.
  2395. When the filename is omitted, archiving happens in the same file.
  2396. %s in the filename will be replaced by the current file
  2397. name (without the directory part). Archiving to a different file
  2398. is useful to keep archived entries from contributing to the
  2399. Org-mode Agenda.
  2400. The archived entries will be filed as subtrees of the specified
  2401. headline. When the headline is omitted, the subtrees are simply
  2402. filed away at the end of the file, as top-level entries.
  2403. Here are a few examples:
  2404. \"%s_archive::\"
  2405. If the current file is Projects.org, archive in file
  2406. Projects.org_archive, as top-level trees. This is the default.
  2407. \"::* Archived Tasks\"
  2408. Archive in the current file, under the top-level headline
  2409. \"* Archived Tasks\".
  2410. \"~/org/archive.org::\"
  2411. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2412. \"basement::** Finished Tasks\"
  2413. Archive in file ./basement (relative path), as level 3 trees
  2414. below the level 2 heading \"** Finished Tasks\".
  2415. You may set this option on a per-file basis by adding to the buffer a
  2416. line like
  2417. #+ARCHIVE: basement::** Finished Tasks
  2418. You may also define it locally for a subtree by setting an ARCHIVE property
  2419. in the entry. If such a property is found in an entry, or anywhere up
  2420. the hierarchy, it will be used."
  2421. :group 'org-archive
  2422. :type 'string)
  2423. (defcustom org-archive-tag "ARCHIVE"
  2424. "The tag that marks a subtree as archived.
  2425. An archived subtree does not open during visibility cycling, and does
  2426. not contribute to the agenda listings.
  2427. After changing this, font-lock must be restarted in the relevant buffers to
  2428. get the proper fontification."
  2429. :group 'org-archive
  2430. :group 'org-keywords
  2431. :type 'string)
  2432. (defcustom org-agenda-skip-archived-trees t
  2433. "Non-nil means, the agenda will skip any items located in archived trees.
  2434. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2435. variable is no longer recommended, you should leave it at the value t.
  2436. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2437. :group 'org-archive
  2438. :group 'org-agenda-skip
  2439. :type 'boolean)
  2440. (defcustom org-cycle-open-archived-trees nil
  2441. "Non-nil means, `org-cycle' will open archived trees.
  2442. An archived tree is a tree marked with the tag ARCHIVE.
  2443. When nil, archived trees will stay folded. You can still open them with
  2444. normal outline commands like `show-all', but not with the cycling commands."
  2445. :group 'org-archive
  2446. :group 'org-cycle
  2447. :type 'boolean)
  2448. (defcustom org-sparse-tree-open-archived-trees nil
  2449. "Non-nil means sparse tree construction shows matches in archived trees.
  2450. When nil, matches in these trees are highlighted, but the trees are kept in
  2451. collapsed state."
  2452. :group 'org-archive
  2453. :group 'org-sparse-trees
  2454. :type 'boolean)
  2455. (defun org-cycle-hide-archived-subtrees (state)
  2456. "Re-hide all archived subtrees after a visibility state change."
  2457. (when (and (not org-cycle-open-archived-trees)
  2458. (not (memq state '(overview folded))))
  2459. (save-excursion
  2460. (let* ((globalp (memq state '(contents all)))
  2461. (beg (if globalp (point-min) (point)))
  2462. (end (if globalp (point-max) (org-end-of-subtree t))))
  2463. (org-hide-archived-subtrees beg end)
  2464. (goto-char beg)
  2465. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2466. (message "%s" (substitute-command-keys
  2467. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2468. (defun org-force-cycle-archived ()
  2469. "Cycle subtree even if it is archived."
  2470. (interactive)
  2471. (setq this-command 'org-cycle)
  2472. (let ((org-cycle-open-archived-trees t))
  2473. (call-interactively 'org-cycle)))
  2474. (defun org-hide-archived-subtrees (beg end)
  2475. "Re-hide all archived subtrees after a visibility state change."
  2476. (save-excursion
  2477. (let* ((re (concat ":" org-archive-tag ":")))
  2478. (goto-char beg)
  2479. (while (re-search-forward re end t)
  2480. (and (org-on-heading-p) (hide-subtree))
  2481. (org-end-of-subtree t)))))
  2482. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2483. (eval-and-compile
  2484. (org-autoload "org-archive"
  2485. '(org-add-archive-files org-archive-subtree
  2486. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2487. ;; Autoload Column View Code
  2488. (declare-function org-columns-number-to-string "org-colview")
  2489. (declare-function org-columns-get-format-and-top-level "org-colview")
  2490. (declare-function org-columns-compute "org-colview")
  2491. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2492. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2493. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2494. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2495. ;; Autoload ID code
  2496. (declare-function org-id-store-link "org-id")
  2497. (org-autoload "org-id"
  2498. '(org-id-get-create org-id-new org-id-copy org-id-get
  2499. org-id-get-with-outline-path-completion
  2500. org-id-get-with-outline-drilling
  2501. org-id-goto org-id-find org-id-store-link))
  2502. ;;; Variables for pre-computed regular expressions, all buffer local
  2503. (defvar org-drawer-regexp nil
  2504. "Matches first line of a hidden block.")
  2505. (make-variable-buffer-local 'org-drawer-regexp)
  2506. (defvar org-todo-regexp nil
  2507. "Matches any of the TODO state keywords.")
  2508. (make-variable-buffer-local 'org-todo-regexp)
  2509. (defvar org-not-done-regexp nil
  2510. "Matches any of the TODO state keywords except the last one.")
  2511. (make-variable-buffer-local 'org-not-done-regexp)
  2512. (defvar org-todo-line-regexp nil
  2513. "Matches a headline and puts TODO state into group 2 if present.")
  2514. (make-variable-buffer-local 'org-todo-line-regexp)
  2515. (defvar org-complex-heading-regexp nil
  2516. "Matches a headline and puts everything into groups:
  2517. group 1: the stars
  2518. group 2: The todo keyword, maybe
  2519. group 3: Priority cookie
  2520. group 4: True headline
  2521. group 5: Tags")
  2522. (make-variable-buffer-local 'org-complex-heading-regexp)
  2523. (defvar org-todo-line-tags-regexp nil
  2524. "Matches a headline and puts TODO state into group 2 if present.
  2525. Also put tags into group 4 if tags are present.")
  2526. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2527. (defvar org-nl-done-regexp nil
  2528. "Matches newline followed by a headline with the DONE keyword.")
  2529. (make-variable-buffer-local 'org-nl-done-regexp)
  2530. (defvar org-looking-at-done-regexp nil
  2531. "Matches the DONE keyword a point.")
  2532. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2533. (defvar org-ds-keyword-length 12
  2534. "Maximum length of the Deadline and SCHEDULED keywords.")
  2535. (make-variable-buffer-local 'org-ds-keyword-length)
  2536. (defvar org-deadline-regexp nil
  2537. "Matches the DEADLINE keyword.")
  2538. (make-variable-buffer-local 'org-deadline-regexp)
  2539. (defvar org-deadline-time-regexp nil
  2540. "Matches the DEADLINE keyword together with a time stamp.")
  2541. (make-variable-buffer-local 'org-deadline-time-regexp)
  2542. (defvar org-deadline-line-regexp nil
  2543. "Matches the DEADLINE keyword and the rest of the line.")
  2544. (make-variable-buffer-local 'org-deadline-line-regexp)
  2545. (defvar org-scheduled-regexp nil
  2546. "Matches the SCHEDULED keyword.")
  2547. (make-variable-buffer-local 'org-scheduled-regexp)
  2548. (defvar org-scheduled-time-regexp nil
  2549. "Matches the SCHEDULED keyword together with a time stamp.")
  2550. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2551. (defvar org-closed-time-regexp nil
  2552. "Matches the CLOSED keyword together with a time stamp.")
  2553. (make-variable-buffer-local 'org-closed-time-regexp)
  2554. (defvar org-keyword-time-regexp nil
  2555. "Matches any of the 4 keywords, together with the time stamp.")
  2556. (make-variable-buffer-local 'org-keyword-time-regexp)
  2557. (defvar org-keyword-time-not-clock-regexp nil
  2558. "Matches any of the 3 keywords, together with the time stamp.")
  2559. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2560. (defvar org-maybe-keyword-time-regexp nil
  2561. "Matches a timestamp, possibly preceeded by a keyword.")
  2562. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2563. (defvar org-planning-or-clock-line-re nil
  2564. "Matches a line with planning or clock info.")
  2565. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2566. (defconst org-plain-time-of-day-regexp
  2567. (concat
  2568. "\\(\\<[012]?[0-9]"
  2569. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2570. "\\(--?"
  2571. "\\(\\<[012]?[0-9]"
  2572. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2573. "\\)?")
  2574. "Regular expression to match a plain time or time range.
  2575. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2576. groups carry important information:
  2577. 0 the full match
  2578. 1 the first time, range or not
  2579. 8 the second time, if it is a range.")
  2580. (defconst org-plain-time-extension-regexp
  2581. (concat
  2582. "\\(\\<[012]?[0-9]"
  2583. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2584. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2585. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2586. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2587. groups carry important information:
  2588. 0 the full match
  2589. 7 hours of duration
  2590. 9 minutes of duration")
  2591. (defconst org-stamp-time-of-day-regexp
  2592. (concat
  2593. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2594. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2595. "\\(--?"
  2596. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2597. "Regular expression to match a timestamp time or time range.
  2598. After a match, the following groups carry important information:
  2599. 0 the full match
  2600. 1 date plus weekday, for backreferencing to make sure both times on same day
  2601. 2 the first time, range or not
  2602. 4 the second time, if it is a range.")
  2603. (defconst org-startup-options
  2604. '(("fold" org-startup-folded t)
  2605. ("overview" org-startup-folded t)
  2606. ("nofold" org-startup-folded nil)
  2607. ("showall" org-startup-folded nil)
  2608. ("content" org-startup-folded content)
  2609. ("hidestars" org-hide-leading-stars t)
  2610. ("showstars" org-hide-leading-stars nil)
  2611. ("odd" org-odd-levels-only t)
  2612. ("oddeven" org-odd-levels-only nil)
  2613. ("align" org-startup-align-all-tables t)
  2614. ("noalign" org-startup-align-all-tables nil)
  2615. ("customtime" org-display-custom-times t)
  2616. ("logdone" org-log-done time)
  2617. ("lognotedone" org-log-done note)
  2618. ("nologdone" org-log-done nil)
  2619. ("lognoteclock-out" org-log-note-clock-out t)
  2620. ("nolognoteclock-out" org-log-note-clock-out nil)
  2621. ("logrepeat" org-log-repeat state)
  2622. ("lognoterepeat" org-log-repeat note)
  2623. ("nologrepeat" org-log-repeat nil)
  2624. ("fninline" org-footnote-define-inline t)
  2625. ("nofninline" org-footnote-define-inline nil)
  2626. ("fnauto" org-footnote-auto-label t)
  2627. ("fnprompt" org-footnote-auto-label nil)
  2628. ("fnconfirm" org-footnote-auto-label confirm)
  2629. ("fnplain" org-footnote-auto-label plain)
  2630. ("constcgs" constants-unit-system cgs)
  2631. ("constSI" constants-unit-system SI))
  2632. "Variable associated with STARTUP options for org-mode.
  2633. Each element is a list of three items: The startup options as written
  2634. in the #+STARTUP line, the corresponding variable, and the value to
  2635. set this variable to if the option is found. An optional forth element PUSH
  2636. means to push this value onto the list in the variable.")
  2637. (defun org-set-regexps-and-options ()
  2638. "Precompute regular expressions for current buffer."
  2639. (when (org-mode-p)
  2640. (org-set-local 'org-todo-kwd-alist nil)
  2641. (org-set-local 'org-todo-key-alist nil)
  2642. (org-set-local 'org-todo-key-trigger nil)
  2643. (org-set-local 'org-todo-keywords-1 nil)
  2644. (org-set-local 'org-done-keywords nil)
  2645. (org-set-local 'org-todo-heads nil)
  2646. (org-set-local 'org-todo-sets nil)
  2647. (org-set-local 'org-todo-log-states nil)
  2648. (org-set-local 'org-file-properties nil)
  2649. (org-set-local 'org-file-tags nil)
  2650. (let ((re (org-make-options-regexp
  2651. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2652. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2653. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2654. (splitre "[ \t]+")
  2655. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2656. tail sep kws1 prio props ftags drawers
  2657. ext-setup-or-nil setup-contents (start 0))
  2658. (save-excursion
  2659. (save-restriction
  2660. (widen)
  2661. (goto-char (point-min))
  2662. (while (or (and ext-setup-or-nil
  2663. (string-match re ext-setup-or-nil start)
  2664. (setq start (match-end 0)))
  2665. (and (setq ext-setup-or-nil nil start 0)
  2666. (re-search-forward re nil t)))
  2667. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2668. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2669. (cond
  2670. ((equal key "CATEGORY")
  2671. (if (string-match "[ \t]+$" value)
  2672. (setq value (replace-match "" t t value)))
  2673. (setq cat value))
  2674. ((member key '("SEQ_TODO" "TODO"))
  2675. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2676. ((equal key "TYP_TODO")
  2677. (push (cons 'type (org-split-string value splitre)) kwds))
  2678. ((equal key "TAGS")
  2679. (setq tags (append tags (org-split-string value splitre))))
  2680. ((equal key "COLUMNS")
  2681. (org-set-local 'org-columns-default-format value))
  2682. ((equal key "LINK")
  2683. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2684. (push (cons (match-string 1 value)
  2685. (org-trim (match-string 2 value)))
  2686. links)))
  2687. ((equal key "PRIORITIES")
  2688. (setq prio (org-split-string value " +")))
  2689. ((equal key "PROPERTY")
  2690. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2691. (push (cons (match-string 1 value) (match-string 2 value))
  2692. props)))
  2693. ((equal key "FILETAGS")
  2694. (when (string-match "\\S-" value)
  2695. (setq ftags
  2696. (append
  2697. ftags
  2698. (apply 'append
  2699. (mapcar (lambda (x) (org-split-string x ":"))
  2700. (org-split-string value)))))))
  2701. ((equal key "DRAWERS")
  2702. (setq drawers (org-split-string value splitre)))
  2703. ((equal key "CONSTANTS")
  2704. (setq const (append const (org-split-string value splitre))))
  2705. ((equal key "STARTUP")
  2706. (let ((opts (org-split-string value splitre))
  2707. l var val)
  2708. (while (setq l (pop opts))
  2709. (when (setq l (assoc l org-startup-options))
  2710. (setq var (nth 1 l) val (nth 2 l))
  2711. (if (not (nth 3 l))
  2712. (set (make-local-variable var) val)
  2713. (if (not (listp (symbol-value var)))
  2714. (set (make-local-variable var) nil))
  2715. (set (make-local-variable var) (symbol-value var))
  2716. (add-to-list var val))))))
  2717. ((equal key "ARCHIVE")
  2718. (string-match " *$" value)
  2719. (setq arch (replace-match "" t t value))
  2720. (remove-text-properties 0 (length arch)
  2721. '(face t fontified t) arch))
  2722. ((equal key "SETUPFILE")
  2723. (setq setup-contents (org-file-contents
  2724. (expand-file-name
  2725. (org-remove-double-quotes value))
  2726. 'noerror))
  2727. (if (not ext-setup-or-nil)
  2728. (setq ext-setup-or-nil setup-contents start 0)
  2729. (setq ext-setup-or-nil
  2730. (concat (substring ext-setup-or-nil 0 start)
  2731. "\n" setup-contents "\n"
  2732. (substring ext-setup-or-nil start)))))
  2733. ))))
  2734. (when cat
  2735. (org-set-local 'org-category (intern cat))
  2736. (push (cons "CATEGORY" cat) props))
  2737. (when prio
  2738. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2739. (setq prio (mapcar 'string-to-char prio))
  2740. (org-set-local 'org-highest-priority (nth 0 prio))
  2741. (org-set-local 'org-lowest-priority (nth 1 prio))
  2742. (org-set-local 'org-default-priority (nth 2 prio)))
  2743. (and props (org-set-local 'org-file-properties (nreverse props)))
  2744. (and ftags (org-set-local 'org-file-tags ftags))
  2745. (and drawers (org-set-local 'org-drawers drawers))
  2746. (and arch (org-set-local 'org-archive-location arch))
  2747. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2748. ;; Process the TODO keywords
  2749. (unless kwds
  2750. ;; Use the global values as if they had been given locally.
  2751. (setq kwds (default-value 'org-todo-keywords))
  2752. (if (stringp (car kwds))
  2753. (setq kwds (list (cons org-todo-interpretation
  2754. (default-value 'org-todo-keywords)))))
  2755. (setq kwds (reverse kwds)))
  2756. (setq kwds (nreverse kwds))
  2757. (let (inter kws kw)
  2758. (while (setq kws (pop kwds))
  2759. (setq inter (pop kws) sep (member "|" kws)
  2760. kws0 (delete "|" (copy-sequence kws))
  2761. kwsa nil
  2762. kws1 (mapcar
  2763. (lambda (x)
  2764. ;; 1 2
  2765. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2766. (progn
  2767. (setq kw (match-string 1 x)
  2768. key (and (match-end 2) (match-string 2 x))
  2769. log (org-extract-log-state-settings x))
  2770. (push (cons kw (and key (string-to-char key))) kwsa)
  2771. (and log (push log org-todo-log-states))
  2772. kw)
  2773. (error "Invalid TODO keyword %s" x)))
  2774. kws0)
  2775. kwsa (if kwsa (append '((:startgroup))
  2776. (nreverse kwsa)
  2777. '((:endgroup))))
  2778. hw (car kws1)
  2779. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2780. tail (list inter hw (car dws) (org-last dws)))
  2781. (add-to-list 'org-todo-heads hw 'append)
  2782. (push kws1 org-todo-sets)
  2783. (setq org-done-keywords (append org-done-keywords dws nil))
  2784. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2785. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2786. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2787. (setq org-todo-sets (nreverse org-todo-sets)
  2788. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2789. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2790. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2791. ;; Process the constants
  2792. (when const
  2793. (let (e cst)
  2794. (while (setq e (pop const))
  2795. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2796. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2797. (setq org-table-formula-constants-local cst)))
  2798. ;; Process the tags.
  2799. (when tags
  2800. (let (e tgs)
  2801. (while (setq e (pop tags))
  2802. (cond
  2803. ((equal e "{") (push '(:startgroup) tgs))
  2804. ((equal e "}") (push '(:endgroup) tgs))
  2805. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2806. (push (cons (match-string 1 e)
  2807. (string-to-char (match-string 2 e)))
  2808. tgs))
  2809. (t (push (list e) tgs))))
  2810. (org-set-local 'org-tag-alist nil)
  2811. (while (setq e (pop tgs))
  2812. (or (and (stringp (car e))
  2813. (assoc (car e) org-tag-alist))
  2814. (push e org-tag-alist)))))
  2815. ;; Compute the regular expressions and other local variables
  2816. (if (not org-done-keywords)
  2817. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2818. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2819. (length org-scheduled-string)
  2820. (length org-clock-string)
  2821. (length org-closed-string)))
  2822. org-drawer-regexp
  2823. (concat "^[ \t]*:\\("
  2824. (mapconcat 'regexp-quote org-drawers "\\|")
  2825. "\\):[ \t]*$")
  2826. org-not-done-keywords
  2827. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2828. org-todo-regexp
  2829. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2830. "\\|") "\\)\\>")
  2831. org-not-done-regexp
  2832. (concat "\\<\\("
  2833. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2834. "\\)\\>")
  2835. org-todo-line-regexp
  2836. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2837. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2838. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2839. org-complex-heading-regexp
  2840. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2841. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2842. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2843. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2844. org-nl-done-regexp
  2845. (concat "\n\\*+[ \t]+"
  2846. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2847. "\\)" "\\>")
  2848. org-todo-line-tags-regexp
  2849. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2850. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2851. (org-re
  2852. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2853. org-looking-at-done-regexp
  2854. (concat "^" "\\(?:"
  2855. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2856. "\\>")
  2857. org-deadline-regexp (concat "\\<" org-deadline-string)
  2858. org-deadline-time-regexp
  2859. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2860. org-deadline-line-regexp
  2861. (concat "\\<\\(" org-deadline-string "\\).*")
  2862. org-scheduled-regexp
  2863. (concat "\\<" org-scheduled-string)
  2864. org-scheduled-time-regexp
  2865. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2866. org-closed-time-regexp
  2867. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2868. org-keyword-time-regexp
  2869. (concat "\\<\\(" org-scheduled-string
  2870. "\\|" org-deadline-string
  2871. "\\|" org-closed-string
  2872. "\\|" org-clock-string "\\)"
  2873. " *[[<]\\([^]>]+\\)[]>]")
  2874. org-keyword-time-not-clock-regexp
  2875. (concat "\\<\\(" org-scheduled-string
  2876. "\\|" org-deadline-string
  2877. "\\|" org-closed-string
  2878. "\\)"
  2879. " *[[<]\\([^]>]+\\)[]>]")
  2880. org-maybe-keyword-time-regexp
  2881. (concat "\\(\\<\\(" org-scheduled-string
  2882. "\\|" org-deadline-string
  2883. "\\|" org-closed-string
  2884. "\\|" org-clock-string "\\)\\)?"
  2885. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2886. org-planning-or-clock-line-re
  2887. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2888. "\\|" org-deadline-string
  2889. "\\|" org-closed-string "\\|" org-clock-string
  2890. "\\)\\>\\)")
  2891. )
  2892. (org-compute-latex-and-specials-regexp)
  2893. (org-set-font-lock-defaults))))
  2894. (defun org-file-contents (file &optional noerror)
  2895. "Return the contents of FILE, as a string."
  2896. (if (or (not file)
  2897. (not (file-readable-p file)))
  2898. (if noerror
  2899. (progn
  2900. (message "Cannot read file %s" file)
  2901. (ding) (sit-for 2)
  2902. "")
  2903. (error "Cannot read file %s" file))
  2904. (with-temp-buffer
  2905. (insert-file-contents file)
  2906. (buffer-string))))
  2907. (defun org-extract-log-state-settings (x)
  2908. "Extract the log state setting from a TODO keyword string.
  2909. This will extract info from a string like \"WAIT(w@/!)\"."
  2910. (let (kw key log1 log2)
  2911. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2912. (setq kw (match-string 1 x)
  2913. key (and (match-end 2) (match-string 2 x))
  2914. log1 (and (match-end 3) (match-string 3 x))
  2915. log2 (and (match-end 4) (match-string 4 x)))
  2916. (and (or log1 log2)
  2917. (list kw
  2918. (and log1 (if (equal log1 "!") 'time 'note))
  2919. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2920. (defun org-remove-keyword-keys (list)
  2921. "Remove a pair of parenthesis at the end of each string in LIST."
  2922. (mapcar (lambda (x)
  2923. (if (string-match "(.*)$" x)
  2924. (substring x 0 (match-beginning 0))
  2925. x))
  2926. list))
  2927. ;; FIXME: this could be done much better, using second characters etc.
  2928. (defun org-assign-fast-keys (alist)
  2929. "Assign fast keys to a keyword-key alist.
  2930. Respect keys that are already there."
  2931. (let (new e k c c1 c2 (char ?a))
  2932. (while (setq e (pop alist))
  2933. (cond
  2934. ((equal e '(:startgroup)) (push e new))
  2935. ((equal e '(:endgroup)) (push e new))
  2936. (t
  2937. (setq k (car e) c2 nil)
  2938. (if (cdr e)
  2939. (setq c (cdr e))
  2940. ;; automatically assign a character.
  2941. (setq c1 (string-to-char
  2942. (downcase (substring
  2943. k (if (= (string-to-char k) ?@) 1 0)))))
  2944. (if (or (rassoc c1 new) (rassoc c1 alist))
  2945. (while (or (rassoc char new) (rassoc char alist))
  2946. (setq char (1+ char)))
  2947. (setq c2 c1))
  2948. (setq c (or c2 char)))
  2949. (push (cons k c) new))))
  2950. (nreverse new)))
  2951. ;;; Some variables used in various places
  2952. (defvar org-window-configuration nil
  2953. "Used in various places to store a window configuration.")
  2954. (defvar org-finish-function nil
  2955. "Function to be called when `C-c C-c' is used.
  2956. This is for getting out of special buffers like remember.")
  2957. ;; FIXME: Occasionally check by commenting these, to make sure
  2958. ;; no other functions uses these, forgetting to let-bind them.
  2959. (defvar entry)
  2960. (defvar state)
  2961. (defvar last-state)
  2962. (defvar date)
  2963. (defvar description)
  2964. ;; Defined somewhere in this file, but used before definition.
  2965. (defvar org-html-entities)
  2966. (defvar org-struct-menu)
  2967. (defvar org-org-menu)
  2968. (defvar org-tbl-menu)
  2969. (defvar org-agenda-keymap)
  2970. ;;;; Define the Org-mode
  2971. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2972. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22."))
  2973. ;; We use a before-change function to check if a table might need
  2974. ;; an update.
  2975. (defvar org-table-may-need-update t
  2976. "Indicates that a table might need an update.
  2977. This variable is set by `org-before-change-function'.
  2978. `org-table-align' sets it back to nil.")
  2979. (defun org-before-change-function (beg end)
  2980. "Every change indicates that a table might need an update."
  2981. (setq org-table-may-need-update t))
  2982. (defvar org-mode-map)
  2983. (defvar org-mode-hook nil
  2984. "Mode hook for Org-mode, run after the mode was turned on.")
  2985. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2986. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2987. (defvar org-table-buffer-is-an nil)
  2988. (defconst org-outline-regexp "\\*+ ")
  2989. ;;;###autoload
  2990. (define-derived-mode org-mode outline-mode "Org"
  2991. "Outline-based notes management and organizer, alias
  2992. \"Carsten's outline-mode for keeping track of everything.\"
  2993. Org-mode develops organizational tasks around a NOTES file which
  2994. contains information about projects as plain text. Org-mode is
  2995. implemented on top of outline-mode, which is ideal to keep the content
  2996. of large files well structured. It supports ToDo items, deadlines and
  2997. time stamps, which magically appear in the diary listing of the Emacs
  2998. calendar. Tables are easily created with a built-in table editor.
  2999. Plain text URL-like links connect to websites, emails (VM), Usenet
  3000. messages (Gnus), BBDB entries, and any files related to the project.
  3001. For printing and sharing of notes, an Org-mode file (or a part of it)
  3002. can be exported as a structured ASCII or HTML file.
  3003. The following commands are available:
  3004. \\{org-mode-map}"
  3005. ;; Get rid of Outline menus, they are not needed
  3006. ;; Need to do this here because define-derived-mode sets up
  3007. ;; the keymap so late. Still, it is a waste to call this each time
  3008. ;; we switch another buffer into org-mode.
  3009. (if (featurep 'xemacs)
  3010. (when (boundp 'outline-mode-menu-heading)
  3011. ;; Assume this is Greg's port, it used easymenu
  3012. (easy-menu-remove outline-mode-menu-heading)
  3013. (easy-menu-remove outline-mode-menu-show)
  3014. (easy-menu-remove outline-mode-menu-hide))
  3015. (define-key org-mode-map [menu-bar headings] 'undefined)
  3016. (define-key org-mode-map [menu-bar hide] 'undefined)
  3017. (define-key org-mode-map [menu-bar show] 'undefined))
  3018. (org-load-modules-maybe)
  3019. (easy-menu-add org-org-menu)
  3020. (easy-menu-add org-tbl-menu)
  3021. (org-install-agenda-files-menu)
  3022. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3023. (org-add-to-invisibility-spec '(org-cwidth))
  3024. (when (featurep 'xemacs)
  3025. (org-set-local 'line-move-ignore-invisible t))
  3026. (org-set-local 'outline-regexp org-outline-regexp)
  3027. (org-set-local 'outline-level 'org-outline-level)
  3028. (when (and org-ellipsis
  3029. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3030. (fboundp 'make-glyph-code))
  3031. (unless org-display-table
  3032. (setq org-display-table (make-display-table)))
  3033. (set-display-table-slot
  3034. org-display-table 4
  3035. (vconcat (mapcar
  3036. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3037. org-ellipsis)))
  3038. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3039. (setq buffer-display-table org-display-table))
  3040. (org-set-regexps-and-options)
  3041. (when (and org-tag-faces (not org-tags-special-faces-re))
  3042. ;; tag faces set outside customize.... force initialization.
  3043. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3044. ;; Calc embedded
  3045. (org-set-local 'calc-embedded-open-mode "# ")
  3046. (modify-syntax-entry ?# "<")
  3047. (modify-syntax-entry ?@ "w")
  3048. (if org-startup-truncated (setq truncate-lines t))
  3049. (org-set-local 'font-lock-unfontify-region-function
  3050. 'org-unfontify-region)
  3051. ;; Activate before-change-function
  3052. (org-set-local 'org-table-may-need-update t)
  3053. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3054. 'local)
  3055. ;; Check for running clock before killing a buffer
  3056. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3057. ;; Paragraphs and auto-filling
  3058. (org-set-autofill-regexps)
  3059. (setq indent-line-function 'org-indent-line-function)
  3060. (org-update-radio-target-regexp)
  3061. ;; Comment characters
  3062. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3063. (org-set-local 'comment-padding " ")
  3064. ;; Align options lines
  3065. (org-set-local
  3066. 'align-mode-rules-list
  3067. '((org-in-buffer-settings
  3068. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3069. (modes . '(org-mode)))))
  3070. ;; Imenu
  3071. (org-set-local 'imenu-create-index-function
  3072. 'org-imenu-get-tree)
  3073. ;; Make isearch reveal context
  3074. (if (or (featurep 'xemacs)
  3075. (not (boundp 'outline-isearch-open-invisible-function)))
  3076. ;; Emacs 21 and XEmacs make use of the hook
  3077. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3078. ;; Emacs 22 deals with this through a special variable
  3079. (org-set-local 'outline-isearch-open-invisible-function
  3080. (lambda (&rest ignore) (org-show-context 'isearch))))
  3081. ;; If empty file that did not turn on org-mode automatically, make it to.
  3082. (if (and org-insert-mode-line-in-empty-file
  3083. (interactive-p)
  3084. (= (point-min) (point-max)))
  3085. (insert "# -*- mode: org -*-\n\n"))
  3086. (unless org-inhibit-startup
  3087. (when org-startup-align-all-tables
  3088. (let ((bmp (buffer-modified-p)))
  3089. (org-table-map-tables 'org-table-align)
  3090. (set-buffer-modified-p bmp)))
  3091. (org-set-startup-visibility)))
  3092. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3093. (defun org-current-time ()
  3094. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3095. (if (> (car org-time-stamp-rounding-minutes) 1)
  3096. (let ((r (car org-time-stamp-rounding-minutes))
  3097. (time (decode-time)))
  3098. (apply 'encode-time
  3099. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3100. (nthcdr 2 time))))
  3101. (current-time)))
  3102. ;;;; Font-Lock stuff, including the activators
  3103. (defvar org-mouse-map (make-sparse-keymap))
  3104. (org-defkey org-mouse-map
  3105. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3106. (org-defkey org-mouse-map
  3107. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3108. (when org-mouse-1-follows-link
  3109. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3110. (when org-tab-follows-link
  3111. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3112. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3113. (when org-return-follows-link
  3114. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3115. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3116. (require 'font-lock)
  3117. (defconst org-non-link-chars "]\t\n\r<>")
  3118. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3119. "shell" "elisp"))
  3120. (defvar org-link-types-re nil
  3121. "Matches a link that has a url-like prefix like \"http:\"")
  3122. (defvar org-link-re-with-space nil
  3123. "Matches a link with spaces, optional angular brackets around it.")
  3124. (defvar org-link-re-with-space2 nil
  3125. "Matches a link with spaces, optional angular brackets around it.")
  3126. (defvar org-link-re-with-space3 nil
  3127. "Matches a link with spaces, only for internal part in bracket links.")
  3128. (defvar org-angle-link-re nil
  3129. "Matches link with angular brackets, spaces are allowed.")
  3130. (defvar org-plain-link-re nil
  3131. "Matches plain link, without spaces.")
  3132. (defvar org-bracket-link-regexp nil
  3133. "Matches a link in double brackets.")
  3134. (defvar org-bracket-link-analytic-regexp nil
  3135. "Regular expression used to analyze links.
  3136. Here is what the match groups contain after a match:
  3137. 1: http:
  3138. 2: http
  3139. 3: path
  3140. 4: [desc]
  3141. 5: desc")
  3142. (defvar org-bracket-link-analytic-regexp++ nil
  3143. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3144. (defvar org-any-link-re nil
  3145. "Regular expression matching any link.")
  3146. (defun org-make-link-regexps ()
  3147. "Update the link regular expressions.
  3148. This should be called after the variable `org-link-types' has changed."
  3149. (setq org-link-types-re
  3150. (concat
  3151. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3152. org-link-re-with-space
  3153. (concat
  3154. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3155. "\\([^" org-non-link-chars " ]"
  3156. "[^" org-non-link-chars "]*"
  3157. "[^" org-non-link-chars " ]\\)>?")
  3158. org-link-re-with-space2
  3159. (concat
  3160. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3161. "\\([^" org-non-link-chars " ]"
  3162. "[^\t\n\r]*"
  3163. "[^" org-non-link-chars " ]\\)>?")
  3164. org-link-re-with-space3
  3165. (concat
  3166. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3167. "\\([^" org-non-link-chars " ]"
  3168. "[^\t\n\r]*\\)")
  3169. org-angle-link-re
  3170. (concat
  3171. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3172. "\\([^" org-non-link-chars " ]"
  3173. "[^" org-non-link-chars "]*"
  3174. "\\)>")
  3175. org-plain-link-re
  3176. (concat
  3177. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3178. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3179. org-bracket-link-regexp
  3180. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3181. org-bracket-link-analytic-regexp
  3182. (concat
  3183. "\\[\\["
  3184. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3185. "\\([^]]+\\)"
  3186. "\\]"
  3187. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3188. "\\]")
  3189. org-bracket-link-analytic-regexp++
  3190. (concat
  3191. "\\[\\["
  3192. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3193. "\\([^]]+\\)"
  3194. "\\]"
  3195. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3196. "\\]")
  3197. org-any-link-re
  3198. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3199. org-angle-link-re "\\)\\|\\("
  3200. org-plain-link-re "\\)")))
  3201. (org-make-link-regexps)
  3202. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3203. "Regular expression for fast time stamp matching.")
  3204. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3205. "Regular expression for fast time stamp matching.")
  3206. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3207. "Regular expression matching time strings for analysis.
  3208. This one does not require the space after the date, so it can be used
  3209. on a string that terminates immediately after the date.")
  3210. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3211. "Regular expression matching time strings for analysis.")
  3212. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3213. "Regular expression matching time stamps, with groups.")
  3214. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3215. "Regular expression matching time stamps (also [..]), with groups.")
  3216. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3217. "Regular expression matching a time stamp range.")
  3218. (defconst org-tr-regexp-both
  3219. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3220. "Regular expression matching a time stamp range.")
  3221. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3222. org-ts-regexp "\\)?")
  3223. "Regular expression matching a time stamp or time stamp range.")
  3224. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3225. org-ts-regexp-both "\\)?")
  3226. "Regular expression matching a time stamp or time stamp range.
  3227. The time stamps may be either active or inactive.")
  3228. (defvar org-emph-face nil)
  3229. (defun org-do-emphasis-faces (limit)
  3230. "Run through the buffer and add overlays to links."
  3231. (let (rtn)
  3232. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3233. (if (not (= (char-after (match-beginning 3))
  3234. (char-after (match-beginning 4))))
  3235. (progn
  3236. (setq rtn t)
  3237. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3238. 'face
  3239. (nth 1 (assoc (match-string 3)
  3240. org-emphasis-alist)))
  3241. (add-text-properties (match-beginning 2) (match-end 2)
  3242. '(font-lock-multiline t))
  3243. (when org-hide-emphasis-markers
  3244. (add-text-properties (match-end 4) (match-beginning 5)
  3245. '(invisible org-link))
  3246. (add-text-properties (match-beginning 3) (match-end 3)
  3247. '(invisible org-link)))))
  3248. (backward-char 1))
  3249. rtn))
  3250. (defun org-emphasize (&optional char)
  3251. "Insert or change an emphasis, i.e. a font like bold or italic.
  3252. If there is an active region, change that region to a new emphasis.
  3253. If there is no region, just insert the marker characters and position
  3254. the cursor between them.
  3255. CHAR should be either the marker character, or the first character of the
  3256. HTML tag associated with that emphasis. If CHAR is a space, the means
  3257. to remove the emphasis of the selected region.
  3258. If char is not given (for example in an interactive call) it
  3259. will be prompted for."
  3260. (interactive)
  3261. (let ((eal org-emphasis-alist) e det
  3262. (erc org-emphasis-regexp-components)
  3263. (prompt "")
  3264. (string "") beg end move tag c s)
  3265. (if (org-region-active-p)
  3266. (setq beg (region-beginning) end (region-end)
  3267. string (buffer-substring beg end))
  3268. (setq move t))
  3269. (while (setq e (pop eal))
  3270. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3271. c (aref tag 0))
  3272. (push (cons c (string-to-char (car e))) det)
  3273. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3274. (substring tag 1)))))
  3275. (setq det (nreverse det))
  3276. (unless char
  3277. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3278. (setq char (read-char-exclusive)))
  3279. (setq char (or (cdr (assoc char det)) char))
  3280. (if (equal char ?\ )
  3281. (setq s "" move nil)
  3282. (unless (assoc (char-to-string char) org-emphasis-alist)
  3283. (error "No such emphasis marker: \"%c\"" char))
  3284. (setq s (char-to-string char)))
  3285. (while (and (> (length string) 1)
  3286. (equal (substring string 0 1) (substring string -1))
  3287. (assoc (substring string 0 1) org-emphasis-alist))
  3288. (setq string (substring string 1 -1)))
  3289. (setq string (concat s string s))
  3290. (if beg (delete-region beg end))
  3291. (unless (or (bolp)
  3292. (string-match (concat "[" (nth 0 erc) "\n]")
  3293. (char-to-string (char-before (point)))))
  3294. (insert " "))
  3295. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3296. (char-to-string (char-after (point))))
  3297. (insert " ") (backward-char 1))
  3298. (insert string)
  3299. (and move (backward-char 1))))
  3300. (defconst org-nonsticky-props
  3301. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3302. (defun org-activate-plain-links (limit)
  3303. "Run through the buffer and add overlays to links."
  3304. (catch 'exit
  3305. (let (f)
  3306. (while (re-search-forward org-plain-link-re limit t)
  3307. (setq f (get-text-property (match-beginning 0) 'face))
  3308. (if (or (eq f 'org-tag)
  3309. (and (listp f) (memq 'org-tag f)))
  3310. nil
  3311. (add-text-properties (match-beginning 0) (match-end 0)
  3312. (list 'mouse-face 'highlight
  3313. 'rear-nonsticky org-nonsticky-props
  3314. 'keymap org-mouse-map
  3315. ))
  3316. (throw 'exit t))))))
  3317. (defun org-activate-code (limit)
  3318. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3319. (progn
  3320. (remove-text-properties (match-beginning 0) (match-end 0)
  3321. '(display t invisible t intangible t))
  3322. t)))
  3323. (defun org-activate-angle-links (limit)
  3324. "Run through the buffer and add overlays to links."
  3325. (if (re-search-forward org-angle-link-re limit t)
  3326. (progn
  3327. (add-text-properties (match-beginning 0) (match-end 0)
  3328. (list 'mouse-face 'highlight
  3329. 'rear-nonsticky org-nonsticky-props
  3330. 'keymap org-mouse-map
  3331. ))
  3332. t)))
  3333. (defun org-activate-bracket-links (limit)
  3334. "Run through the buffer and add overlays to bracketed links."
  3335. (if (re-search-forward org-bracket-link-regexp limit t)
  3336. (let* ((help (concat "LINK: "
  3337. (org-match-string-no-properties 1)))
  3338. ;; FIXME: above we should remove the escapes.
  3339. ;; but that requires another match, protecting match data,
  3340. ;; a lot of overhead for font-lock.
  3341. (ip (org-maybe-intangible
  3342. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3343. 'keymap org-mouse-map 'mouse-face 'highlight
  3344. 'font-lock-multiline t 'help-echo help)))
  3345. (vp (list 'rear-nonsticky org-nonsticky-props
  3346. 'keymap org-mouse-map 'mouse-face 'highlight
  3347. ' font-lock-multiline t 'help-echo help)))
  3348. ;; We need to remove the invisible property here. Table narrowing
  3349. ;; may have made some of this invisible.
  3350. (remove-text-properties (match-beginning 0) (match-end 0)
  3351. '(invisible nil))
  3352. (if (match-end 3)
  3353. (progn
  3354. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3355. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3356. (add-text-properties (match-end 3) (match-end 0) ip))
  3357. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3358. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3359. (add-text-properties (match-end 1) (match-end 0) ip))
  3360. t)))
  3361. (defun org-activate-dates (limit)
  3362. "Run through the buffer and add overlays to dates."
  3363. (if (re-search-forward org-tsr-regexp-both limit t)
  3364. (progn
  3365. (add-text-properties (match-beginning 0) (match-end 0)
  3366. (list 'mouse-face 'highlight
  3367. 'rear-nonsticky org-nonsticky-props
  3368. 'keymap org-mouse-map))
  3369. (when org-display-custom-times
  3370. (if (match-end 3)
  3371. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3372. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3373. t)))
  3374. (defvar org-target-link-regexp nil
  3375. "Regular expression matching radio targets in plain text.")
  3376. (make-variable-buffer-local 'org-target-link-regexp)
  3377. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3378. "Regular expression matching a link target.")
  3379. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3380. "Regular expression matching a radio target.")
  3381. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3382. "Regular expression matching any target.")
  3383. (defun org-activate-target-links (limit)
  3384. "Run through the buffer and add overlays to target matches."
  3385. (when org-target-link-regexp
  3386. (let ((case-fold-search t))
  3387. (if (re-search-forward org-target-link-regexp limit t)
  3388. (progn
  3389. (add-text-properties (match-beginning 0) (match-end 0)
  3390. (list 'mouse-face 'highlight
  3391. 'rear-nonsticky org-nonsticky-props
  3392. 'keymap org-mouse-map
  3393. 'help-echo "Radio target link"
  3394. 'org-linked-text t))
  3395. t)))))
  3396. (defun org-update-radio-target-regexp ()
  3397. "Find all radio targets in this file and update the regular expression."
  3398. (interactive)
  3399. (when (memq 'radio org-activate-links)
  3400. (setq org-target-link-regexp
  3401. (org-make-target-link-regexp (org-all-targets 'radio)))
  3402. (org-restart-font-lock)))
  3403. (defun org-hide-wide-columns (limit)
  3404. (let (s e)
  3405. (setq s (text-property-any (point) (or limit (point-max))
  3406. 'org-cwidth t))
  3407. (when s
  3408. (setq e (next-single-property-change s 'org-cwidth))
  3409. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3410. (goto-char e)
  3411. t)))
  3412. (defvar org-latex-and-specials-regexp nil
  3413. "Regular expression for highlighting export special stuff.")
  3414. (defvar org-match-substring-regexp)
  3415. (defvar org-match-substring-with-braces-regexp)
  3416. (defvar org-export-html-special-string-regexps)
  3417. (defun org-compute-latex-and-specials-regexp ()
  3418. "Compute regular expression for stuff treated specially by exporters."
  3419. (if (not org-highlight-latex-fragments-and-specials)
  3420. (org-set-local 'org-latex-and-specials-regexp nil)
  3421. (require 'org-exp)
  3422. (let*
  3423. ((matchers (plist-get org-format-latex-options :matchers))
  3424. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3425. org-latex-regexps)))
  3426. (options (org-combine-plists (org-default-export-plist)
  3427. (org-infile-export-plist)))
  3428. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3429. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3430. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3431. (org-export-html-expand (plist-get options :expand-quoted-html))
  3432. (org-export-with-special-strings (plist-get options :special-strings))
  3433. (re-sub
  3434. (cond
  3435. ((equal org-export-with-sub-superscripts '{})
  3436. (list org-match-substring-with-braces-regexp))
  3437. (org-export-with-sub-superscripts
  3438. (list org-match-substring-regexp))
  3439. (t nil)))
  3440. (re-latex
  3441. (if org-export-with-LaTeX-fragments
  3442. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3443. (re-macros
  3444. (if org-export-with-TeX-macros
  3445. (list (concat "\\\\"
  3446. (regexp-opt
  3447. (append (mapcar 'car org-html-entities)
  3448. (if (boundp 'org-latex-entities)
  3449. org-latex-entities nil))
  3450. 'words))) ; FIXME
  3451. ))
  3452. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3453. (re-special (if org-export-with-special-strings
  3454. (mapcar (lambda (x) (car x))
  3455. org-export-html-special-string-regexps)))
  3456. (re-rest
  3457. (delq nil
  3458. (list
  3459. (if org-export-html-expand "@<[^>\n]+>")
  3460. ))))
  3461. (org-set-local
  3462. 'org-latex-and-specials-regexp
  3463. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3464. re-rest) "\\|")))))
  3465. (defun org-do-latex-and-special-faces (limit)
  3466. "Run through the buffer and add overlays to links."
  3467. (when org-latex-and-specials-regexp
  3468. (let (rtn d)
  3469. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3470. limit t))
  3471. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3472. 'face))
  3473. '(org-code org-verbatim underline)))
  3474. (progn
  3475. (setq rtn t
  3476. d (cond ((member (char-after (1+ (match-beginning 0)))
  3477. '(?_ ?^)) 1)
  3478. (t 0)))
  3479. (font-lock-prepend-text-property
  3480. (+ d (match-beginning 0)) (match-end 0)
  3481. 'face 'org-latex-and-export-specials)
  3482. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3483. '(font-lock-multiline t)))))
  3484. rtn)))
  3485. (defun org-restart-font-lock ()
  3486. "Restart font-lock-mode, to force refontification."
  3487. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3488. (font-lock-mode -1)
  3489. (font-lock-mode 1)))
  3490. (defun org-all-targets (&optional radio)
  3491. "Return a list of all targets in this file.
  3492. With optional argument RADIO, only find radio targets."
  3493. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3494. rtn)
  3495. (save-excursion
  3496. (goto-char (point-min))
  3497. (while (re-search-forward re nil t)
  3498. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3499. rtn)))
  3500. (defun org-make-target-link-regexp (targets)
  3501. "Make regular expression matching all strings in TARGETS.
  3502. The regular expression finds the targets also if there is a line break
  3503. between words."
  3504. (and targets
  3505. (concat
  3506. "\\<\\("
  3507. (mapconcat
  3508. (lambda (x)
  3509. (while (string-match " +" x)
  3510. (setq x (replace-match "\\s-+" t t x)))
  3511. x)
  3512. targets
  3513. "\\|")
  3514. "\\)\\>")))
  3515. (defun org-activate-tags (limit)
  3516. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3517. (progn
  3518. (add-text-properties (match-beginning 1) (match-end 1)
  3519. (list 'mouse-face 'highlight
  3520. 'rear-nonsticky org-nonsticky-props
  3521. 'keymap org-mouse-map))
  3522. t)))
  3523. (defun org-outline-level ()
  3524. (save-excursion
  3525. (looking-at outline-regexp)
  3526. (if (match-beginning 1)
  3527. (+ (org-get-string-indentation (match-string 1)) 1000)
  3528. (1- (- (match-end 0) (match-beginning 0))))))
  3529. (defvar org-font-lock-keywords nil)
  3530. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3531. "Regular expression matching a property line.")
  3532. (defvar org-font-lock-hook nil
  3533. "Functions to be called for special font lock stuff.")
  3534. (defun org-font-lock-hook (limit)
  3535. (run-hook-with-args 'org-font-lock-hook limit))
  3536. (defun org-set-font-lock-defaults ()
  3537. (let* ((em org-fontify-emphasized-text)
  3538. (lk org-activate-links)
  3539. (org-font-lock-extra-keywords
  3540. (list
  3541. ;; Call the hook
  3542. '(org-font-lock-hook)
  3543. ;; Headlines
  3544. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3545. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3546. ;; Table lines
  3547. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3548. (1 'org-table t))
  3549. ;; Table internals
  3550. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3551. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3552. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3553. ;; Drawers
  3554. (list org-drawer-regexp '(0 'org-special-keyword t))
  3555. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3556. ;; Properties
  3557. (list org-property-re
  3558. '(1 'org-special-keyword t)
  3559. '(3 'org-property-value t))
  3560. (if org-format-transports-properties-p
  3561. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3562. ;; Links
  3563. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3564. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3565. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3566. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3567. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3568. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3569. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3570. '(org-hide-wide-columns (0 nil append))
  3571. ;; TODO lines
  3572. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3573. '(1 (org-get-todo-face 1) t))
  3574. ;; DONE
  3575. (if org-fontify-done-headline
  3576. (list (concat "^[*]+ +\\<\\("
  3577. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3578. "\\)\\(.*\\)")
  3579. '(2 'org-headline-done t))
  3580. nil)
  3581. ;; Priorities
  3582. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3583. ;; Tags
  3584. '(org-font-lock-add-tag-faces)
  3585. ;; Special keywords
  3586. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3587. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3588. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3589. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3590. ;; Emphasis
  3591. (if em
  3592. (if (featurep 'xemacs)
  3593. '(org-do-emphasis-faces (0 nil append))
  3594. '(org-do-emphasis-faces)))
  3595. ;; Checkboxes
  3596. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3597. 2 'bold prepend)
  3598. (if org-provide-checkbox-statistics
  3599. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3600. (0 (org-get-checkbox-statistics-face) t)))
  3601. ;; Description list items
  3602. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3603. 2 'bold prepend)
  3604. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3605. '(1 'org-archived prepend))
  3606. ;; Specials
  3607. '(org-do-latex-and-special-faces)
  3608. ;; Code
  3609. '(org-activate-code (1 'org-code t))
  3610. ;; COMMENT
  3611. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3612. "\\|" org-quote-string "\\)\\>")
  3613. '(1 'org-special-keyword t))
  3614. '("^#.*" (0 'font-lock-comment-face t))
  3615. )))
  3616. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3617. ;; Now set the full font-lock-keywords
  3618. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3619. (org-set-local 'font-lock-defaults
  3620. '(org-font-lock-keywords t nil nil backward-paragraph))
  3621. (kill-local-variable 'font-lock-keywords) nil))
  3622. (defvar org-m nil)
  3623. (defvar org-l nil)
  3624. (defvar org-f nil)
  3625. (defun org-get-level-face (n)
  3626. "Get the right face for match N in font-lock matching of headlines."
  3627. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3628. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3629. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3630. (cond
  3631. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3632. ((eq n 2) org-f)
  3633. (t (if org-level-color-stars-only nil org-f))))
  3634. (defun org-get-todo-face (kwd)
  3635. "Get the right face for a TODO keyword KWD.
  3636. If KWD is a number, get the corresponding match group."
  3637. (if (numberp kwd) (setq kwd (match-string kwd)))
  3638. (or (cdr (assoc kwd org-todo-keyword-faces))
  3639. (and (member kwd org-done-keywords) 'org-done)
  3640. 'org-todo))
  3641. (defun org-font-lock-add-tag-faces (limit)
  3642. "Add the special tag faces."
  3643. (when (and org-tag-faces org-tags-special-faces-re)
  3644. (while (re-search-forward org-tags-special-faces-re limit t)
  3645. (add-text-properties (match-beginning 1) (match-end 1)
  3646. (list 'face (org-get-tag-face 1)
  3647. 'font-lock-fontified t))
  3648. (backward-char 1))))
  3649. (defun org-get-tag-face (kwd)
  3650. "Get the right face for a TODO keyword KWD.
  3651. If KWD is a number, get the corresponding match group."
  3652. (if (numberp kwd) (setq kwd (match-string kwd)))
  3653. (or (cdr (assoc kwd org-tag-faces))
  3654. 'org-tag))
  3655. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3656. "Remove fontification and activation overlays from links."
  3657. (font-lock-default-unfontify-region beg end)
  3658. (let* ((buffer-undo-list t)
  3659. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3660. (inhibit-modification-hooks t)
  3661. deactivate-mark buffer-file-name buffer-file-truename)
  3662. (remove-text-properties beg end
  3663. '(mouse-face t keymap t org-linked-text t
  3664. invisible t intangible t))))
  3665. ;;;; Visibility cycling, including org-goto and indirect buffer
  3666. ;;; Cycling
  3667. (defvar org-cycle-global-status nil)
  3668. (make-variable-buffer-local 'org-cycle-global-status)
  3669. (defvar org-cycle-subtree-status nil)
  3670. (make-variable-buffer-local 'org-cycle-subtree-status)
  3671. ;;;###autoload
  3672. (defun org-cycle (&optional arg)
  3673. "Visibility cycling for Org-mode.
  3674. - When this function is called with a prefix argument, rotate the entire
  3675. buffer through 3 states (global cycling)
  3676. 1. OVERVIEW: Show only top-level headlines.
  3677. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3678. 3. SHOW ALL: Show everything.
  3679. When called with two C-u C-u prefixes, switch to the startup visibility,
  3680. determined by the variable `org-startup-folded', and by any VISIBILITY
  3681. properties in the buffer.
  3682. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3683. including drawers.
  3684. - When point is at the beginning of a headline, rotate the subtree started
  3685. by this line through 3 different states (local cycling)
  3686. 1. FOLDED: Only the main headline is shown.
  3687. 2. CHILDREN: The main headline and the direct children are shown.
  3688. From this state, you can move to one of the children
  3689. and zoom in further.
  3690. 3. SUBTREE: Show the entire subtree, including body text.
  3691. - When there is a numeric prefix, go up to a heading with level ARG, do
  3692. a `show-subtree' and return to the previous cursor position. If ARG
  3693. is negative, go up that many levels.
  3694. - When point is not at the beginning of a headline, execute the global
  3695. binding for TAB, which is re-indenting the line. See the option
  3696. `org-cycle-emulate-tab' for details.
  3697. - Special case: if point is at the beginning of the buffer and there is
  3698. no headline in line 1, this function will act as if called with prefix arg.
  3699. But only if also the variable `org-cycle-global-at-bob' is t."
  3700. (interactive "P")
  3701. (org-load-modules-maybe)
  3702. (let* ((outline-regexp
  3703. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3704. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3705. outline-regexp))
  3706. (bob-special (and org-cycle-global-at-bob (bobp)
  3707. (not (looking-at outline-regexp))))
  3708. (org-cycle-hook
  3709. (if bob-special
  3710. (delq 'org-optimize-window-after-visibility-change
  3711. (copy-sequence org-cycle-hook))
  3712. org-cycle-hook))
  3713. (pos (point)))
  3714. (if (or bob-special (equal arg '(4)))
  3715. ;; special case: use global cycling
  3716. (setq arg t))
  3717. (cond
  3718. ((equal arg '(16))
  3719. (org-set-startup-visibility)
  3720. (message "Startup visibility, plus VISIBILITY properties"))
  3721. ((equal arg '(64))
  3722. (show-all)
  3723. (message "Entire buffer visible, including drawers"))
  3724. ((org-at-table-p 'any)
  3725. ;; Enter the table or move to the next field in the table
  3726. (or (org-table-recognize-table.el)
  3727. (progn
  3728. (if arg (org-table-edit-field t)
  3729. (org-table-justify-field-maybe)
  3730. (call-interactively 'org-table-next-field)))))
  3731. ((eq arg t) ;; Global cycling
  3732. (cond
  3733. ((and (eq last-command this-command)
  3734. (eq org-cycle-global-status 'overview))
  3735. ;; We just created the overview - now do table of contents
  3736. ;; This can be slow in very large buffers, so indicate action
  3737. (message "CONTENTS...")
  3738. (org-content)
  3739. (message "CONTENTS...done")
  3740. (setq org-cycle-global-status 'contents)
  3741. (run-hook-with-args 'org-cycle-hook 'contents))
  3742. ((and (eq last-command this-command)
  3743. (eq org-cycle-global-status 'contents))
  3744. ;; We just showed the table of contents - now show everything
  3745. (show-all)
  3746. (message "SHOW ALL")
  3747. (setq org-cycle-global-status 'all)
  3748. (run-hook-with-args 'org-cycle-hook 'all))
  3749. (t
  3750. ;; Default action: go to overview
  3751. (org-overview)
  3752. (message "OVERVIEW")
  3753. (setq org-cycle-global-status 'overview)
  3754. (run-hook-with-args 'org-cycle-hook 'overview))))
  3755. ((and org-drawers org-drawer-regexp
  3756. (save-excursion
  3757. (beginning-of-line 1)
  3758. (looking-at org-drawer-regexp)))
  3759. ;; Toggle block visibility
  3760. (org-flag-drawer
  3761. (not (get-char-property (match-end 0) 'invisible))))
  3762. ((integerp arg)
  3763. ;; Show-subtree, ARG levels up from here.
  3764. (save-excursion
  3765. (org-back-to-heading)
  3766. (outline-up-heading (if (< arg 0) (- arg)
  3767. (- (funcall outline-level) arg)))
  3768. (org-show-subtree)))
  3769. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3770. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3771. ;; At a heading: rotate between three different views
  3772. (org-back-to-heading)
  3773. (let ((goal-column 0) eoh eol eos)
  3774. ;; First, some boundaries
  3775. (save-excursion
  3776. (org-back-to-heading)
  3777. (save-excursion
  3778. (beginning-of-line 2)
  3779. (while (and (not (eobp)) ;; this is like `next-line'
  3780. (get-char-property (1- (point)) 'invisible))
  3781. (beginning-of-line 2)) (setq eol (point)))
  3782. (outline-end-of-heading) (setq eoh (point))
  3783. (org-end-of-subtree t)
  3784. (unless (eobp)
  3785. (skip-chars-forward " \t\n")
  3786. (beginning-of-line 1) ; in case this is an item
  3787. )
  3788. (setq eos (1- (point))))
  3789. ;; Find out what to do next and set `this-command'
  3790. (cond
  3791. ((= eos eoh)
  3792. ;; Nothing is hidden behind this heading
  3793. (message "EMPTY ENTRY")
  3794. (setq org-cycle-subtree-status nil)
  3795. (save-excursion
  3796. (goto-char eos)
  3797. (outline-next-heading)
  3798. (if (org-invisible-p) (org-flag-heading nil))))
  3799. ((or (>= eol eos)
  3800. (not (string-match "\\S-" (buffer-substring eol eos))))
  3801. ;; Entire subtree is hidden in one line: open it
  3802. (org-show-entry)
  3803. (show-children)
  3804. (message "CHILDREN")
  3805. (save-excursion
  3806. (goto-char eos)
  3807. (outline-next-heading)
  3808. (if (org-invisible-p) (org-flag-heading nil)))
  3809. (setq org-cycle-subtree-status 'children)
  3810. (run-hook-with-args 'org-cycle-hook 'children))
  3811. ((and (eq last-command this-command)
  3812. (eq org-cycle-subtree-status 'children))
  3813. ;; We just showed the children, now show everything.
  3814. (org-show-subtree)
  3815. (message "SUBTREE")
  3816. (setq org-cycle-subtree-status 'subtree)
  3817. (run-hook-with-args 'org-cycle-hook 'subtree))
  3818. (t
  3819. ;; Default action: hide the subtree.
  3820. (hide-subtree)
  3821. (message "FOLDED")
  3822. (setq org-cycle-subtree-status 'folded)
  3823. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3824. ;; TAB emulation and template completion
  3825. (buffer-read-only (org-back-to-heading))
  3826. ((org-try-structure-completion))
  3827. ((org-try-cdlatex-tab))
  3828. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3829. (or (not (bolp))
  3830. (not (looking-at outline-regexp))))
  3831. (call-interactively (global-key-binding "\t")))
  3832. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3833. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3834. (or (and (eq org-cycle-emulate-tab 'white)
  3835. (= (match-end 0) (point-at-eol)))
  3836. (and (eq org-cycle-emulate-tab 'whitestart)
  3837. (>= (match-end 0) pos))))
  3838. t
  3839. (eq org-cycle-emulate-tab t))
  3840. (call-interactively (global-key-binding "\t")))
  3841. (t (save-excursion
  3842. (org-back-to-heading)
  3843. (org-cycle))))))
  3844. ;;;###autoload
  3845. (defun org-global-cycle (&optional arg)
  3846. "Cycle the global visibility. For details see `org-cycle'.
  3847. With C-u prefix arg, switch to startup visibility.
  3848. With a numeric prefix, show all headlines up to that level."
  3849. (interactive "P")
  3850. (let ((org-cycle-include-plain-lists
  3851. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3852. (cond
  3853. ((integerp arg)
  3854. (show-all)
  3855. (hide-sublevels arg)
  3856. (setq org-cycle-global-status 'contents))
  3857. ((equal arg '(4))
  3858. (org-set-startup-visibility)
  3859. (message "Startup visibility, plus VISIBILITY properties."))
  3860. (t
  3861. (org-cycle '(4))))))
  3862. (defun org-set-startup-visibility ()
  3863. "Set the visibility required by startup options and properties."
  3864. (cond
  3865. ((eq org-startup-folded t)
  3866. (org-cycle '(4)))
  3867. ((eq org-startup-folded 'content)
  3868. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3869. (org-cycle '(4)) (org-cycle '(4)))))
  3870. (org-set-visibility-according-to-property 'no-cleanup)
  3871. (org-cycle-hide-archived-subtrees 'all)
  3872. (org-cycle-hide-drawers 'all)
  3873. (org-cycle-show-empty-lines 'all))
  3874. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3875. "Switch subtree visibilities according to :VISIBILITY: property."
  3876. (interactive)
  3877. (let (state)
  3878. (save-excursion
  3879. (goto-char (point-min))
  3880. (while (re-search-forward
  3881. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3882. nil t)
  3883. (setq state (match-string 1))
  3884. (save-excursion
  3885. (org-back-to-heading t)
  3886. (hide-subtree)
  3887. (org-reveal)
  3888. (cond
  3889. ((equal state '("fold" "folded"))
  3890. (hide-subtree))
  3891. ((equal state "children")
  3892. (org-show-hidden-entry)
  3893. (show-children))
  3894. ((equal state "content")
  3895. (save-excursion
  3896. (save-restriction
  3897. (org-narrow-to-subtree)
  3898. (org-content))))
  3899. ((member state '("all" "showall"))
  3900. (show-subtree)))))
  3901. (unless no-cleanup
  3902. (org-cycle-hide-archived-subtrees 'all)
  3903. (org-cycle-hide-drawers 'all)
  3904. (org-cycle-show-empty-lines 'all)))))
  3905. (defun org-overview ()
  3906. "Switch to overview mode, showing only top-level headlines.
  3907. Really, this shows all headlines with level equal or greater than the level
  3908. of the first headline in the buffer. This is important, because if the
  3909. first headline is not level one, then (hide-sublevels 1) gives confusing
  3910. results."
  3911. (interactive)
  3912. (let ((level (save-excursion
  3913. (goto-char (point-min))
  3914. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3915. (progn
  3916. (goto-char (match-beginning 0))
  3917. (funcall outline-level))))))
  3918. (and level (hide-sublevels level))))
  3919. (defun org-content (&optional arg)
  3920. "Show all headlines in the buffer, like a table of contents.
  3921. With numerical argument N, show content up to level N."
  3922. (interactive "P")
  3923. (save-excursion
  3924. ;; Visit all headings and show their offspring
  3925. (and (integerp arg) (org-overview))
  3926. (goto-char (point-max))
  3927. (catch 'exit
  3928. (while (and (progn (condition-case nil
  3929. (outline-previous-visible-heading 1)
  3930. (error (goto-char (point-min))))
  3931. t)
  3932. (looking-at outline-regexp))
  3933. (if (integerp arg)
  3934. (show-children (1- arg))
  3935. (show-branches))
  3936. (if (bobp) (throw 'exit nil))))))
  3937. (defun org-optimize-window-after-visibility-change (state)
  3938. "Adjust the window after a change in outline visibility.
  3939. This function is the default value of the hook `org-cycle-hook'."
  3940. (when (get-buffer-window (current-buffer))
  3941. (cond
  3942. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3943. ; ((eq state 'overview) (org-beginning-of-line))
  3944. ((eq state 'content) nil)
  3945. ((eq state 'all) nil)
  3946. ((eq state 'folded) nil)
  3947. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3948. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3949. (defun org-compact-display-after-subtree-move ()
  3950. (let (beg end)
  3951. (save-excursion
  3952. (if (org-up-heading-safe)
  3953. (progn
  3954. (hide-subtree)
  3955. (show-entry)
  3956. (show-children)
  3957. (org-cycle-show-empty-lines 'children)
  3958. (org-cycle-hide-drawers 'children))
  3959. (org-overview)))))
  3960. (defun org-cycle-show-empty-lines (state)
  3961. "Show empty lines above all visible headlines.
  3962. The region to be covered depends on STATE when called through
  3963. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3964. entire buffer covered. Note that an empty line is only shown if there
  3965. are at least `org-cycle-separator-lines' empty lines before the headline."
  3966. (when (> org-cycle-separator-lines 0)
  3967. (save-excursion
  3968. (let* ((n org-cycle-separator-lines)
  3969. (re (cond
  3970. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3971. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3972. (t (let ((ns (number-to-string (- n 2))))
  3973. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3974. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3975. beg end)
  3976. (cond
  3977. ((memq state '(overview contents t))
  3978. (setq beg (point-min) end (point-max)))
  3979. ((memq state '(children folded))
  3980. (setq beg (point) end (progn (org-end-of-subtree t t)
  3981. (beginning-of-line 2)
  3982. (point)))))
  3983. (when beg
  3984. (goto-char beg)
  3985. (while (re-search-forward re end t)
  3986. (if (not (get-char-property (match-end 1) 'invisible))
  3987. (outline-flag-region
  3988. (match-beginning 1) (match-end 1) nil)))))))
  3989. ;; Never hide empty lines at the end of the file.
  3990. (save-excursion
  3991. (goto-char (point-max))
  3992. (outline-previous-heading)
  3993. (outline-end-of-heading)
  3994. (if (and (looking-at "[ \t\n]+")
  3995. (= (match-end 0) (point-max)))
  3996. (outline-flag-region (point) (match-end 0) nil))))
  3997. (defun org-show-empty-lines-in-parent ()
  3998. "Move to the parent and re-show empty lines before visible headlines."
  3999. (save-excursion
  4000. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4001. (org-cycle-show-empty-lines context))))
  4002. (defun org-cycle-hide-drawers (state)
  4003. "Re-hide all drawers after a visibility state change."
  4004. (when (and (org-mode-p)
  4005. (not (memq state '(overview folded))))
  4006. (save-excursion
  4007. (let* ((globalp (memq state '(contents all)))
  4008. (beg (if globalp (point-min) (point)))
  4009. (end (if globalp (point-max) (org-end-of-subtree t))))
  4010. (goto-char beg)
  4011. (while (re-search-forward org-drawer-regexp end t)
  4012. (org-flag-drawer t))))))
  4013. (defun org-flag-drawer (flag)
  4014. (save-excursion
  4015. (beginning-of-line 1)
  4016. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4017. (let ((b (match-end 0))
  4018. (outline-regexp org-outline-regexp))
  4019. (if (re-search-forward
  4020. "^[ \t]*:END:"
  4021. (save-excursion (outline-next-heading) (point)) t)
  4022. (outline-flag-region b (point-at-eol) flag)
  4023. (error ":END: line missing"))))))
  4024. (defun org-subtree-end-visible-p ()
  4025. "Is the end of the current subtree visible?"
  4026. (pos-visible-in-window-p
  4027. (save-excursion (org-end-of-subtree t) (point))))
  4028. (defun org-first-headline-recenter (&optional N)
  4029. "Move cursor to the first headline and recenter the headline.
  4030. Optional argument N means, put the headline into the Nth line of the window."
  4031. (goto-char (point-min))
  4032. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4033. (beginning-of-line)
  4034. (recenter (prefix-numeric-value N))))
  4035. ;;; Org-goto
  4036. (defvar org-goto-window-configuration nil)
  4037. (defvar org-goto-marker nil)
  4038. (defvar org-goto-map
  4039. (let ((map (make-sparse-keymap)))
  4040. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4041. (while (setq cmd (pop cmds))
  4042. (substitute-key-definition cmd cmd map global-map)))
  4043. (suppress-keymap map)
  4044. (org-defkey map "\C-m" 'org-goto-ret)
  4045. (org-defkey map [(return)] 'org-goto-ret)
  4046. (org-defkey map [(left)] 'org-goto-left)
  4047. (org-defkey map [(right)] 'org-goto-right)
  4048. (org-defkey map [(control ?g)] 'org-goto-quit)
  4049. (org-defkey map "\C-i" 'org-cycle)
  4050. (org-defkey map [(tab)] 'org-cycle)
  4051. (org-defkey map [(down)] 'outline-next-visible-heading)
  4052. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4053. (if org-goto-auto-isearch
  4054. (if (fboundp 'define-key-after)
  4055. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4056. nil)
  4057. (org-defkey map "q" 'org-goto-quit)
  4058. (org-defkey map "n" 'outline-next-visible-heading)
  4059. (org-defkey map "p" 'outline-previous-visible-heading)
  4060. (org-defkey map "f" 'outline-forward-same-level)
  4061. (org-defkey map "b" 'outline-backward-same-level)
  4062. (org-defkey map "u" 'outline-up-heading))
  4063. (org-defkey map "/" 'org-occur)
  4064. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4065. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4066. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4067. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4068. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4069. map))
  4070. (defconst org-goto-help
  4071. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4072. RET=jump to location [Q]uit and return to previous location
  4073. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4074. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4075. ;; FIXME: Docstring doe not mention both interfaces
  4076. (defun org-goto (&optional alternative-interface)
  4077. "Look up a different location in the current file, keeping current visibility.
  4078. When you want look-up or go to a different location in a document, the
  4079. fastest way is often to fold the entire buffer and then dive into the tree.
  4080. This method has the disadvantage, that the previous location will be folded,
  4081. which may not be what you want.
  4082. This command works around this by showing a copy of the current buffer
  4083. in an indirect buffer, in overview mode. You can dive into the tree in
  4084. that copy, use org-occur and incremental search to find a location.
  4085. When pressing RET or `Q', the command returns to the original buffer in
  4086. which the visibility is still unchanged. After RET is will also jump to
  4087. the location selected in the indirect buffer and expose the
  4088. the headline hierarchy above."
  4089. (interactive "P")
  4090. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4091. (org-refile-use-outline-path t)
  4092. (interface
  4093. (if (not alternative-interface)
  4094. org-goto-interface
  4095. (if (eq org-goto-interface 'outline)
  4096. 'outline-path-completion
  4097. 'outline)))
  4098. (org-goto-start-pos (point))
  4099. (selected-point
  4100. (if (eq interface 'outline)
  4101. (car (org-get-location (current-buffer) org-goto-help))
  4102. (nth 3 (org-refile-get-location "Goto: ")))))
  4103. (if selected-point
  4104. (progn
  4105. (org-mark-ring-push org-goto-start-pos)
  4106. (goto-char selected-point)
  4107. (if (or (org-invisible-p) (org-invisible-p2))
  4108. (org-show-context 'org-goto)))
  4109. (message "Quit"))))
  4110. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4111. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4112. (defvar org-goto-local-auto-isearch-map) ; defined below
  4113. (defun org-get-location (buf help)
  4114. "Let the user select a location in the Org-mode buffer BUF.
  4115. This function uses a recursive edit. It returns the selected position
  4116. or nil."
  4117. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4118. (isearch-hide-immediately nil)
  4119. (isearch-search-fun-function
  4120. (lambda () 'org-goto-local-search-headings))
  4121. (org-goto-selected-point org-goto-exit-command))
  4122. (save-excursion
  4123. (save-window-excursion
  4124. (delete-other-windows)
  4125. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4126. (switch-to-buffer
  4127. (condition-case nil
  4128. (make-indirect-buffer (current-buffer) "*org-goto*")
  4129. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4130. (with-output-to-temp-buffer "*Help*"
  4131. (princ help))
  4132. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4133. (setq buffer-read-only nil)
  4134. (let ((org-startup-truncated t)
  4135. (org-startup-folded nil)
  4136. (org-startup-align-all-tables nil))
  4137. (org-mode)
  4138. (org-overview))
  4139. (setq buffer-read-only t)
  4140. (if (and (boundp 'org-goto-start-pos)
  4141. (integer-or-marker-p org-goto-start-pos))
  4142. (let ((org-show-hierarchy-above t)
  4143. (org-show-siblings t)
  4144. (org-show-following-heading t))
  4145. (goto-char org-goto-start-pos)
  4146. (and (org-invisible-p) (org-show-context)))
  4147. (goto-char (point-min)))
  4148. (org-beginning-of-line)
  4149. (message "Select location and press RET")
  4150. (use-local-map org-goto-map)
  4151. (recursive-edit)
  4152. ))
  4153. (kill-buffer "*org-goto*")
  4154. (cons org-goto-selected-point org-goto-exit-command)))
  4155. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4156. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4157. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4158. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4159. (defun org-goto-local-search-headings (string bound noerror)
  4160. "Search and make sure that any matches are in headlines."
  4161. (catch 'return
  4162. (while (if isearch-forward
  4163. (search-forward string bound noerror)
  4164. (search-backward string bound noerror))
  4165. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4166. (and (member :headline context)
  4167. (not (member :tags context))))
  4168. (throw 'return (point))))))
  4169. (defun org-goto-local-auto-isearch ()
  4170. "Start isearch."
  4171. (interactive)
  4172. (goto-char (point-min))
  4173. (let ((keys (this-command-keys)))
  4174. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4175. (isearch-mode t)
  4176. (isearch-process-search-char (string-to-char keys)))))
  4177. (defun org-goto-ret (&optional arg)
  4178. "Finish `org-goto' by going to the new location."
  4179. (interactive "P")
  4180. (setq org-goto-selected-point (point)
  4181. org-goto-exit-command 'return)
  4182. (throw 'exit nil))
  4183. (defun org-goto-left ()
  4184. "Finish `org-goto' by going to the new location."
  4185. (interactive)
  4186. (if (org-on-heading-p)
  4187. (progn
  4188. (beginning-of-line 1)
  4189. (setq org-goto-selected-point (point)
  4190. org-goto-exit-command 'left)
  4191. (throw 'exit nil))
  4192. (error "Not on a heading")))
  4193. (defun org-goto-right ()
  4194. "Finish `org-goto' by going to the new location."
  4195. (interactive)
  4196. (if (org-on-heading-p)
  4197. (progn
  4198. (setq org-goto-selected-point (point)
  4199. org-goto-exit-command 'right)
  4200. (throw 'exit nil))
  4201. (error "Not on a heading")))
  4202. (defun org-goto-quit ()
  4203. "Finish `org-goto' without cursor motion."
  4204. (interactive)
  4205. (setq org-goto-selected-point nil)
  4206. (setq org-goto-exit-command 'quit)
  4207. (throw 'exit nil))
  4208. ;;; Indirect buffer display of subtrees
  4209. (defvar org-indirect-dedicated-frame nil
  4210. "This is the frame being used for indirect tree display.")
  4211. (defvar org-last-indirect-buffer nil)
  4212. (defun org-tree-to-indirect-buffer (&optional arg)
  4213. "Create indirect buffer and narrow it to current subtree.
  4214. With numerical prefix ARG, go up to this level and then take that tree.
  4215. If ARG is negative, go up that many levels.
  4216. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4217. indirect buffer previously made with this command, to avoid proliferation of
  4218. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4219. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4220. is kept so that you can work with several indirect buffers at the same time.
  4221. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4222. requests that a new frame be made for the new buffer, so that the dedicated
  4223. frame is not changed."
  4224. (interactive "P")
  4225. (let ((cbuf (current-buffer))
  4226. (cwin (selected-window))
  4227. (pos (point))
  4228. beg end level heading ibuf)
  4229. (save-excursion
  4230. (org-back-to-heading t)
  4231. (when (numberp arg)
  4232. (setq level (org-outline-level))
  4233. (if (< arg 0) (setq arg (+ level arg)))
  4234. (while (> (setq level (org-outline-level)) arg)
  4235. (outline-up-heading 1 t)))
  4236. (setq beg (point)
  4237. heading (org-get-heading))
  4238. (org-end-of-subtree t) (setq end (point)))
  4239. (if (and (buffer-live-p org-last-indirect-buffer)
  4240. (not (eq org-indirect-buffer-display 'new-frame))
  4241. (not arg))
  4242. (kill-buffer org-last-indirect-buffer))
  4243. (setq ibuf (org-get-indirect-buffer cbuf)
  4244. org-last-indirect-buffer ibuf)
  4245. (cond
  4246. ((or (eq org-indirect-buffer-display 'new-frame)
  4247. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4248. (select-frame (make-frame))
  4249. (delete-other-windows)
  4250. (switch-to-buffer ibuf)
  4251. (org-set-frame-title heading))
  4252. ((eq org-indirect-buffer-display 'dedicated-frame)
  4253. (raise-frame
  4254. (select-frame (or (and org-indirect-dedicated-frame
  4255. (frame-live-p org-indirect-dedicated-frame)
  4256. org-indirect-dedicated-frame)
  4257. (setq org-indirect-dedicated-frame (make-frame)))))
  4258. (delete-other-windows)
  4259. (switch-to-buffer ibuf)
  4260. (org-set-frame-title (concat "Indirect: " heading)))
  4261. ((eq org-indirect-buffer-display 'current-window)
  4262. (switch-to-buffer ibuf))
  4263. ((eq org-indirect-buffer-display 'other-window)
  4264. (pop-to-buffer ibuf))
  4265. (t (error "Invalid value.")))
  4266. (if (featurep 'xemacs)
  4267. (save-excursion (org-mode) (turn-on-font-lock)))
  4268. (narrow-to-region beg end)
  4269. (show-all)
  4270. (goto-char pos)
  4271. (and (window-live-p cwin) (select-window cwin))))
  4272. (defun org-get-indirect-buffer (&optional buffer)
  4273. (setq buffer (or buffer (current-buffer)))
  4274. (let ((n 1) (base (buffer-name buffer)) bname)
  4275. (while (buffer-live-p
  4276. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4277. (setq n (1+ n)))
  4278. (condition-case nil
  4279. (make-indirect-buffer buffer bname 'clone)
  4280. (error (make-indirect-buffer buffer bname)))))
  4281. (defun org-set-frame-title (title)
  4282. "Set the title of the current frame to the string TITLE."
  4283. ;; FIXME: how to name a single frame in XEmacs???
  4284. (unless (featurep 'xemacs)
  4285. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4286. ;;;; Structure editing
  4287. ;;; Inserting headlines
  4288. (defun org-insert-heading (&optional force-heading)
  4289. "Insert a new heading or item with same depth at point.
  4290. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4291. If point is at the beginning of a headline, insert a sibling before the
  4292. current headline. If point is not at the beginning, do not split the line,
  4293. but create the new headline after the current line."
  4294. (interactive "P")
  4295. (if (= (buffer-size) 0)
  4296. (insert "\n* ")
  4297. (when (or force-heading (not (org-insert-item)))
  4298. (let* ((head (save-excursion
  4299. (condition-case nil
  4300. (progn
  4301. (org-back-to-heading)
  4302. (match-string 0))
  4303. (error "*"))))
  4304. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4305. pos hide-previous previous-pos)
  4306. (cond
  4307. ((and (org-on-heading-p) (bolp)
  4308. (or (bobp)
  4309. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4310. ;; insert before the current line
  4311. (open-line (if blank 2 1)))
  4312. ((and (bolp)
  4313. (or (bobp)
  4314. (save-excursion
  4315. (backward-char 1) (not (org-invisible-p)))))
  4316. ;; insert right here
  4317. nil)
  4318. (t
  4319. ;; somewhere in the line
  4320. (save-excursion
  4321. (setq previous-pos (point-at-bol))
  4322. (end-of-line)
  4323. (setq hide-previous (org-invisible-p)))
  4324. (and org-insert-heading-respect-content (org-show-subtree))
  4325. (let ((split
  4326. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4327. (save-excursion
  4328. (let ((p (point)))
  4329. (goto-char (point-at-bol))
  4330. (and (looking-at org-complex-heading-regexp)
  4331. (> p (match-beginning 4)))))))
  4332. tags pos)
  4333. (cond
  4334. (org-insert-heading-respect-content
  4335. (org-end-of-subtree nil t)
  4336. (or (bolp) (newline))
  4337. (open-line 1))
  4338. ((org-on-heading-p)
  4339. (when hide-previous
  4340. (show-children)
  4341. (org-show-entry))
  4342. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4343. (setq tags (and (match-end 2) (match-string 2)))
  4344. (and (match-end 1)
  4345. (delete-region (match-beginning 1) (match-end 1)))
  4346. (setq pos (point-at-bol))
  4347. (or split (end-of-line 1))
  4348. (delete-horizontal-space)
  4349. (newline (if blank 2 1))
  4350. (when tags
  4351. (save-excursion
  4352. (goto-char pos)
  4353. (end-of-line 1)
  4354. (insert " " tags)
  4355. (org-set-tags nil 'align))))
  4356. (t
  4357. (or split (end-of-line 1))
  4358. (newline (if blank 2 1)))))))
  4359. (insert head) (just-one-space)
  4360. (setq pos (point))
  4361. (end-of-line 1)
  4362. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4363. (when (and org-insert-heading-respect-content hide-previous)
  4364. (save-excursion
  4365. (goto-char previous-pos)
  4366. (hide-subtree)))
  4367. (run-hooks 'org-insert-heading-hook)))))
  4368. (defun org-get-heading (&optional no-tags)
  4369. "Return the heading of the current entry, without the stars."
  4370. (save-excursion
  4371. (org-back-to-heading t)
  4372. (if (looking-at
  4373. (if no-tags
  4374. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4375. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4376. (match-string 1) "")))
  4377. (defun org-insert-heading-after-current ()
  4378. "Insert a new heading with same level as current, after current subtree."
  4379. (interactive)
  4380. (org-back-to-heading)
  4381. (org-insert-heading)
  4382. (org-move-subtree-down)
  4383. (end-of-line 1))
  4384. (defun org-insert-heading-respect-content ()
  4385. (interactive)
  4386. (let ((org-insert-heading-respect-content t))
  4387. (org-insert-heading t)))
  4388. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4389. (interactive "P")
  4390. (let ((org-insert-heading-respect-content t))
  4391. (org-insert-todo-heading force-state t)))
  4392. (defun org-insert-todo-heading (arg &optional force-heading)
  4393. "Insert a new heading with the same level and TODO state as current heading.
  4394. If the heading has no TODO state, or if the state is DONE, use the first
  4395. state (TODO by default). Also with prefix arg, force first state."
  4396. (interactive "P")
  4397. (when (or force-heading (not (org-insert-item 'checkbox)))
  4398. (org-insert-heading force-heading)
  4399. (save-excursion
  4400. (org-back-to-heading)
  4401. (outline-previous-heading)
  4402. (looking-at org-todo-line-regexp))
  4403. (if (or arg
  4404. (not (match-beginning 2))
  4405. (member (match-string 2) org-done-keywords))
  4406. (insert (car org-todo-keywords-1) " ")
  4407. (insert (match-string 2) " "))
  4408. (when org-provide-todo-statistics
  4409. (org-update-parent-todo-statistics))))
  4410. (defun org-insert-subheading (arg)
  4411. "Insert a new subheading and demote it.
  4412. Works for outline headings and for plain lists alike."
  4413. (interactive "P")
  4414. (org-insert-heading arg)
  4415. (cond
  4416. ((org-on-heading-p) (org-do-demote))
  4417. ((org-at-item-p) (org-indent-item 1))))
  4418. (defun org-insert-todo-subheading (arg)
  4419. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4420. Works for outline headings and for plain lists alike."
  4421. (interactive "P")
  4422. (org-insert-todo-heading arg)
  4423. (cond
  4424. ((org-on-heading-p) (org-do-demote))
  4425. ((org-at-item-p) (org-indent-item 1))))
  4426. ;;; Promotion and Demotion
  4427. (defun org-promote-subtree ()
  4428. "Promote the entire subtree.
  4429. See also `org-promote'."
  4430. (interactive)
  4431. (save-excursion
  4432. (org-map-tree 'org-promote))
  4433. (org-fix-position-after-promote))
  4434. (defun org-demote-subtree ()
  4435. "Demote the entire subtree. See `org-demote'.
  4436. See also `org-promote'."
  4437. (interactive)
  4438. (save-excursion
  4439. (org-map-tree 'org-demote))
  4440. (org-fix-position-after-promote))
  4441. (defun org-do-promote ()
  4442. "Promote the current heading higher up the tree.
  4443. If the region is active in `transient-mark-mode', promote all headings
  4444. in the region."
  4445. (interactive)
  4446. (save-excursion
  4447. (if (org-region-active-p)
  4448. (org-map-region 'org-promote (region-beginning) (region-end))
  4449. (org-promote)))
  4450. (org-fix-position-after-promote))
  4451. (defun org-do-demote ()
  4452. "Demote the current heading lower down the tree.
  4453. If the region is active in `transient-mark-mode', demote all headings
  4454. in the region."
  4455. (interactive)
  4456. (save-excursion
  4457. (if (org-region-active-p)
  4458. (org-map-region 'org-demote (region-beginning) (region-end))
  4459. (org-demote)))
  4460. (org-fix-position-after-promote))
  4461. (defun org-fix-position-after-promote ()
  4462. "Make sure that after pro/demotion cursor position is right."
  4463. (let ((pos (point)))
  4464. (when (save-excursion
  4465. (beginning-of-line 1)
  4466. (looking-at org-todo-line-regexp)
  4467. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4468. (cond ((eobp) (insert " "))
  4469. ((eolp) (insert " "))
  4470. ((equal (char-after) ?\ ) (forward-char 1))))))
  4471. (defun org-reduced-level (l)
  4472. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4473. (defun org-get-valid-level (level &optional change)
  4474. "Rectify a level change under the influence of `org-odd-levels-only'
  4475. LEVEL is a current level, CHANGE is by how much the level should be
  4476. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4477. even level numbers will become the next higher odd number."
  4478. (if org-odd-levels-only
  4479. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4480. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4481. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4482. (max 1 (+ level change))))
  4483. (if (boundp 'define-obsolete-function-alias)
  4484. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4485. (define-obsolete-function-alias 'org-get-legal-level
  4486. 'org-get-valid-level)
  4487. (define-obsolete-function-alias 'org-get-legal-level
  4488. 'org-get-valid-level "23.1")))
  4489. (defun org-promote ()
  4490. "Promote the current heading higher up the tree.
  4491. If the region is active in `transient-mark-mode', promote all headings
  4492. in the region."
  4493. (org-back-to-heading t)
  4494. (let* ((level (save-match-data (funcall outline-level)))
  4495. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4496. (diff (abs (- level (length up-head) -1))))
  4497. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4498. (replace-match up-head nil t)
  4499. ;; Fixup tag positioning
  4500. (and org-auto-align-tags (org-set-tags nil t))
  4501. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4502. (defun org-demote ()
  4503. "Demote the current heading lower down the tree.
  4504. If the region is active in `transient-mark-mode', demote all headings
  4505. in the region."
  4506. (org-back-to-heading t)
  4507. (let* ((level (save-match-data (funcall outline-level)))
  4508. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4509. (diff (abs (- level (length down-head) -1))))
  4510. (replace-match down-head nil t)
  4511. ;; Fixup tag positioning
  4512. (and org-auto-align-tags (org-set-tags nil t))
  4513. (if org-adapt-indentation (org-fixup-indentation diff))))
  4514. (defun org-map-tree (fun)
  4515. "Call FUN for every heading underneath the current one."
  4516. (org-back-to-heading)
  4517. (let ((level (funcall outline-level)))
  4518. (save-excursion
  4519. (funcall fun)
  4520. (while (and (progn
  4521. (outline-next-heading)
  4522. (> (funcall outline-level) level))
  4523. (not (eobp)))
  4524. (funcall fun)))))
  4525. (defun org-map-region (fun beg end)
  4526. "Call FUN for every heading between BEG and END."
  4527. (let ((org-ignore-region t))
  4528. (save-excursion
  4529. (setq end (copy-marker end))
  4530. (goto-char beg)
  4531. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4532. (< (point) end))
  4533. (funcall fun))
  4534. (while (and (progn
  4535. (outline-next-heading)
  4536. (< (point) end))
  4537. (not (eobp)))
  4538. (funcall fun)))))
  4539. (defun org-fixup-indentation (diff)
  4540. "Change the indentation in the current entry by DIFF
  4541. However, if any line in the current entry has no indentation, or if it
  4542. would end up with no indentation after the change, nothing at all is done."
  4543. (save-excursion
  4544. (let ((end (save-excursion (outline-next-heading)
  4545. (point-marker)))
  4546. (prohibit (if (> diff 0)
  4547. "^\\S-"
  4548. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4549. col)
  4550. (unless (save-excursion (end-of-line 1)
  4551. (re-search-forward prohibit end t))
  4552. (while (and (< (point) end)
  4553. (re-search-forward "^[ \t]+" end t))
  4554. (goto-char (match-end 0))
  4555. (setq col (current-column))
  4556. (if (< diff 0) (replace-match ""))
  4557. (org-indent-to-column (+ diff col))))
  4558. (move-marker end nil))))
  4559. (defun org-convert-to-odd-levels ()
  4560. "Convert an org-mode file with all levels allowed to one with odd levels.
  4561. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4562. level 5 etc."
  4563. (interactive)
  4564. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4565. (let ((org-odd-levels-only nil) n)
  4566. (save-excursion
  4567. (goto-char (point-min))
  4568. (while (re-search-forward "^\\*\\*+ " nil t)
  4569. (setq n (- (length (match-string 0)) 2))
  4570. (while (>= (setq n (1- n)) 0)
  4571. (org-demote))
  4572. (end-of-line 1))))))
  4573. (defun org-convert-to-oddeven-levels ()
  4574. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4575. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4576. section with an even level, conversion would destroy the structure of the file. An error
  4577. is signaled in this case."
  4578. (interactive)
  4579. (goto-char (point-min))
  4580. ;; First check if there are no even levels
  4581. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4582. (org-show-context t)
  4583. (error "Not all levels are odd in this file. Conversion not possible."))
  4584. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4585. (let ((org-odd-levels-only nil) n)
  4586. (save-excursion
  4587. (goto-char (point-min))
  4588. (while (re-search-forward "^\\*\\*+ " nil t)
  4589. (setq n (/ (1- (length (match-string 0))) 2))
  4590. (while (>= (setq n (1- n)) 0)
  4591. (org-promote))
  4592. (end-of-line 1))))))
  4593. (defun org-tr-level (n)
  4594. "Make N odd if required."
  4595. (if org-odd-levels-only (1+ (/ n 2)) n))
  4596. ;;; Vertical tree motion, cutting and pasting of subtrees
  4597. (defun org-move-subtree-up (&optional arg)
  4598. "Move the current subtree up past ARG headlines of the same level."
  4599. (interactive "p")
  4600. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4601. (defun org-move-subtree-down (&optional arg)
  4602. "Move the current subtree down past ARG headlines of the same level."
  4603. (interactive "p")
  4604. (setq arg (prefix-numeric-value arg))
  4605. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4606. 'outline-get-last-sibling))
  4607. (ins-point (make-marker))
  4608. (cnt (abs arg))
  4609. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4610. ;; Select the tree
  4611. (org-back-to-heading)
  4612. (setq beg0 (point))
  4613. (save-excursion
  4614. (setq ne-beg (org-back-over-empty-lines))
  4615. (setq beg (point)))
  4616. (save-match-data
  4617. (save-excursion (outline-end-of-heading)
  4618. (setq folded (org-invisible-p)))
  4619. (outline-end-of-subtree))
  4620. (outline-next-heading)
  4621. (setq ne-end (org-back-over-empty-lines))
  4622. (setq end (point))
  4623. (goto-char beg0)
  4624. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4625. ;; include less whitespace
  4626. (save-excursion
  4627. (goto-char beg)
  4628. (forward-line (- ne-beg ne-end))
  4629. (setq beg (point))))
  4630. ;; Find insertion point, with error handling
  4631. (while (> cnt 0)
  4632. (or (and (funcall movfunc) (looking-at outline-regexp))
  4633. (progn (goto-char beg0)
  4634. (error "Cannot move past superior level or buffer limit")))
  4635. (setq cnt (1- cnt)))
  4636. (if (> arg 0)
  4637. ;; Moving forward - still need to move over subtree
  4638. (progn (org-end-of-subtree t t)
  4639. (save-excursion
  4640. (org-back-over-empty-lines)
  4641. (or (bolp) (newline)))))
  4642. (setq ne-ins (org-back-over-empty-lines))
  4643. (move-marker ins-point (point))
  4644. (setq txt (buffer-substring beg end))
  4645. (org-save-markers-in-region beg end)
  4646. (delete-region beg end)
  4647. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4648. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4649. (let ((bbb (point)))
  4650. (insert-before-markers txt)
  4651. (org-reinstall-markers-in-region bbb)
  4652. (move-marker ins-point bbb))
  4653. (or (bolp) (insert "\n"))
  4654. (setq ins-end (point))
  4655. (goto-char ins-point)
  4656. (org-skip-whitespace)
  4657. (when (and (< arg 0)
  4658. (org-first-sibling-p)
  4659. (> ne-ins ne-beg))
  4660. ;; Move whitespace back to beginning
  4661. (save-excursion
  4662. (goto-char ins-end)
  4663. (let ((kill-whole-line t))
  4664. (kill-line (- ne-ins ne-beg)) (point)))
  4665. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4666. (move-marker ins-point nil)
  4667. (org-compact-display-after-subtree-move)
  4668. (org-show-empty-lines-in-parent)
  4669. (unless folded
  4670. (org-show-entry)
  4671. (show-children)
  4672. (org-cycle-hide-drawers 'children))))
  4673. (defvar org-subtree-clip ""
  4674. "Clipboard for cut and paste of subtrees.
  4675. This is actually only a copy of the kill, because we use the normal kill
  4676. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4677. (defvar org-subtree-clip-folded nil
  4678. "Was the last copied subtree folded?
  4679. This is used to fold the tree back after pasting.")
  4680. (defun org-cut-subtree (&optional n)
  4681. "Cut the current subtree into the clipboard.
  4682. With prefix arg N, cut this many sequential subtrees.
  4683. This is a short-hand for marking the subtree and then cutting it."
  4684. (interactive "p")
  4685. (org-copy-subtree n 'cut))
  4686. (defun org-copy-subtree (&optional n cut force-store-markers)
  4687. "Cut the current subtree into the clipboard.
  4688. With prefix arg N, cut this many sequential subtrees.
  4689. This is a short-hand for marking the subtree and then copying it.
  4690. If CUT is non-nil, actually cut the subtree.
  4691. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4692. of some markers in the region, even if CUT is non-nil. This is
  4693. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4694. (interactive "p")
  4695. (let (beg end folded (beg0 (point)))
  4696. (if (interactive-p)
  4697. (org-back-to-heading nil) ; take what looks like a subtree
  4698. (org-back-to-heading t)) ; take what is really there
  4699. (org-back-over-empty-lines)
  4700. (setq beg (point))
  4701. (skip-chars-forward " \t\r\n")
  4702. (save-match-data
  4703. (save-excursion (outline-end-of-heading)
  4704. (setq folded (org-invisible-p)))
  4705. (condition-case nil
  4706. (outline-forward-same-level (1- n))
  4707. (error nil))
  4708. (org-end-of-subtree t t))
  4709. (org-back-over-empty-lines)
  4710. (setq end (point))
  4711. (goto-char beg0)
  4712. (when (> end beg)
  4713. (setq org-subtree-clip-folded folded)
  4714. (when (or cut force-store-markers)
  4715. (org-save-markers-in-region beg end))
  4716. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4717. (setq org-subtree-clip (current-kill 0))
  4718. (message "%s: Subtree(s) with %d characters"
  4719. (if cut "Cut" "Copied")
  4720. (length org-subtree-clip)))))
  4721. (defun org-paste-subtree (&optional level tree for-yank)
  4722. "Paste the clipboard as a subtree, with modification of headline level.
  4723. The entire subtree is promoted or demoted in order to match a new headline
  4724. level.
  4725. If the cursor is at the beginning of a headline, the same level as
  4726. that headline is used to paste the tree
  4727. If not, the new level is derived from the *visible* headings
  4728. before and after the insertion point, and taken to be the inferior headline
  4729. level of the two. So if the previous visible heading is level 3 and the
  4730. next is level 4 (or vice versa), level 4 will be used for insertion.
  4731. This makes sure that the subtree remains an independent subtree and does
  4732. not swallow low level entries.
  4733. You can also force a different level, either by using a numeric prefix
  4734. argument, or by inserting the heading marker by hand. For example, if the
  4735. cursor is after \"*****\", then the tree will be shifted to level 5.
  4736. If optional TREE is given, use this text instead of the kill ring.
  4737. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4738. move back over whitespace before inserting, and move point to the end of
  4739. the inserted text when done."
  4740. (interactive "P")
  4741. (unless (org-kill-is-subtree-p tree)
  4742. (error "%s"
  4743. (substitute-command-keys
  4744. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4745. (let* ((visp (not (org-invisible-p)))
  4746. (txt (or tree (and kill-ring (current-kill 0))))
  4747. (^re (concat "^\\(" outline-regexp "\\)"))
  4748. (re (concat "\\(" outline-regexp "\\)"))
  4749. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4750. (old-level (if (string-match ^re txt)
  4751. (- (match-end 0) (match-beginning 0) 1)
  4752. -1))
  4753. (force-level (cond (level (prefix-numeric-value level))
  4754. ((and (looking-at "[ \t]*$")
  4755. (string-match
  4756. ^re_ (buffer-substring
  4757. (point-at-bol) (point))))
  4758. (- (match-end 1) (match-beginning 1)))
  4759. ((and (bolp)
  4760. (looking-at org-outline-regexp))
  4761. (- (match-end 0) (point) 1))
  4762. (t nil)))
  4763. (previous-level (save-excursion
  4764. (condition-case nil
  4765. (progn
  4766. (outline-previous-visible-heading 1)
  4767. (if (looking-at re)
  4768. (- (match-end 0) (match-beginning 0) 1)
  4769. 1))
  4770. (error 1))))
  4771. (next-level (save-excursion
  4772. (condition-case nil
  4773. (progn
  4774. (or (looking-at outline-regexp)
  4775. (outline-next-visible-heading 1))
  4776. (if (looking-at re)
  4777. (- (match-end 0) (match-beginning 0) 1)
  4778. 1))
  4779. (error 1))))
  4780. (new-level (or force-level (max previous-level next-level)))
  4781. (shift (if (or (= old-level -1)
  4782. (= new-level -1)
  4783. (= old-level new-level))
  4784. 0
  4785. (- new-level old-level)))
  4786. (delta (if (> shift 0) -1 1))
  4787. (func (if (> shift 0) 'org-demote 'org-promote))
  4788. (org-odd-levels-only nil)
  4789. beg end newend)
  4790. ;; Remove the forced level indicator
  4791. (if force-level
  4792. (delete-region (point-at-bol) (point)))
  4793. ;; Paste
  4794. (beginning-of-line 1)
  4795. (unless for-yank (org-back-over-empty-lines))
  4796. (setq beg (point))
  4797. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  4798. (insert-before-markers txt)
  4799. (unless (string-match "\n\\'" txt) (insert "\n"))
  4800. (setq newend (point))
  4801. (org-reinstall-markers-in-region beg)
  4802. (setq end (point))
  4803. (goto-char beg)
  4804. (skip-chars-forward " \t\n\r")
  4805. (setq beg (point))
  4806. (if (and (org-invisible-p) visp)
  4807. (save-excursion (outline-show-heading)))
  4808. ;; Shift if necessary
  4809. (unless (= shift 0)
  4810. (save-restriction
  4811. (narrow-to-region beg end)
  4812. (while (not (= shift 0))
  4813. (org-map-region func (point-min) (point-max))
  4814. (setq shift (+ delta shift)))
  4815. (goto-char (point-min))
  4816. (setq newend (point-max))))
  4817. (when (or (interactive-p) for-yank)
  4818. (message "Clipboard pasted as level %d subtree" new-level))
  4819. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4820. kill-ring
  4821. (eq org-subtree-clip (current-kill 0))
  4822. org-subtree-clip-folded)
  4823. ;; The tree was folded before it was killed/copied
  4824. (hide-subtree))
  4825. (and for-yank (goto-char newend))))
  4826. (defun org-kill-is-subtree-p (&optional txt)
  4827. "Check if the current kill is an outline subtree, or a set of trees.
  4828. Returns nil if kill does not start with a headline, or if the first
  4829. headline level is not the largest headline level in the tree.
  4830. So this will actually accept several entries of equal levels as well,
  4831. which is OK for `org-paste-subtree'.
  4832. If optional TXT is given, check this string instead of the current kill."
  4833. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4834. (start-level (and kill
  4835. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4836. org-outline-regexp "\\)")
  4837. kill)
  4838. (- (match-end 2) (match-beginning 2) 1)))
  4839. (re (concat "^" org-outline-regexp))
  4840. (start (1+ (or (match-beginning 2) -1))))
  4841. (if (not start-level)
  4842. (progn
  4843. nil) ;; does not even start with a heading
  4844. (catch 'exit
  4845. (while (setq start (string-match re kill (1+ start)))
  4846. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4847. (throw 'exit nil)))
  4848. t))))
  4849. (defvar org-markers-to-move nil
  4850. "Markers that should be moved with a cut-and-paste operation.
  4851. Those markers are stored together with their positions relative to
  4852. the start of the region.")
  4853. (defun org-save-markers-in-region (beg end)
  4854. "Check markers in region.
  4855. If these markers are between BEG and END, record their position relative
  4856. to BEG, so that after moving the block of text, we can put the markers back
  4857. into place.
  4858. This function gets called just before an entry or tree gets cut from the
  4859. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4860. called immediately, to move the markers with the entries."
  4861. (setq org-markers-to-move nil)
  4862. (when (featurep 'org-clock)
  4863. (org-clock-save-markers-for-cut-and-paste beg end))
  4864. (when (featurep 'org-agenda)
  4865. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4866. (defun org-check-and-save-marker (marker beg end)
  4867. "Check if MARKER is between BEG and END.
  4868. If yes, remember the marker and the distance to BEG."
  4869. (when (and (marker-buffer marker)
  4870. (equal (marker-buffer marker) (current-buffer)))
  4871. (if (and (>= marker beg) (< marker end))
  4872. (push (cons marker (- marker beg)) org-markers-to-move))))
  4873. (defun org-reinstall-markers-in-region (beg)
  4874. "Move all remembered markers to their position relative to BEG."
  4875. (mapc (lambda (x)
  4876. (move-marker (car x) (+ beg (cdr x))))
  4877. org-markers-to-move)
  4878. (setq org-markers-to-move nil))
  4879. (defun org-narrow-to-subtree ()
  4880. "Narrow buffer to the current subtree."
  4881. (interactive)
  4882. (save-excursion
  4883. (save-match-data
  4884. (narrow-to-region
  4885. (progn (org-back-to-heading) (point))
  4886. (progn (org-end-of-subtree t) (point))))))
  4887. ;;; Outline Sorting
  4888. (defun org-sort (with-case)
  4889. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4890. Optional argument WITH-CASE means sort case-sensitively."
  4891. (interactive "P")
  4892. (if (org-at-table-p)
  4893. (org-call-with-arg 'org-table-sort-lines with-case)
  4894. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4895. (defun org-sort-remove-invisible (s)
  4896. (remove-text-properties 0 (length s) org-rm-props s)
  4897. (while (string-match org-bracket-link-regexp s)
  4898. (setq s (replace-match (if (match-end 2)
  4899. (match-string 3 s)
  4900. (match-string 1 s)) t t s)))
  4901. s)
  4902. (defvar org-priority-regexp) ; defined later in the file
  4903. (defun org-sort-entries-or-items
  4904. (&optional with-case sorting-type getkey-func compare-func property)
  4905. "Sort entries on a certain level of an outline tree.
  4906. If there is an active region, the entries in the region are sorted.
  4907. Else, if the cursor is before the first entry, sort the top-level items.
  4908. Else, the children of the entry at point are sorted.
  4909. Sorting can be alphabetically, numerically, and by date/time as given by
  4910. the first time stamp in the entry. The command prompts for the sorting
  4911. type unless it has been given to the function through the SORTING-TYPE
  4912. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4913. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4914. called with point at the beginning of the record. It must return either
  4915. a string or a number that should serve as the sorting key for that record.
  4916. Comparing entries ignores case by default. However, with an optional argument
  4917. WITH-CASE, the sorting considers case as well."
  4918. (interactive "P")
  4919. (let ((case-func (if with-case 'identity 'downcase))
  4920. start beg end stars re re2
  4921. txt what tmp plain-list-p)
  4922. ;; Find beginning and end of region to sort
  4923. (cond
  4924. ((org-region-active-p)
  4925. ;; we will sort the region
  4926. (setq end (region-end)
  4927. what "region")
  4928. (goto-char (region-beginning))
  4929. (if (not (org-on-heading-p)) (outline-next-heading))
  4930. (setq start (point)))
  4931. ((org-at-item-p)
  4932. ;; we will sort this plain list
  4933. (org-beginning-of-item-list) (setq start (point))
  4934. (org-end-of-item-list) (setq end (point))
  4935. (goto-char start)
  4936. (setq plain-list-p t
  4937. what "plain list"))
  4938. ((or (org-on-heading-p)
  4939. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4940. ;; we will sort the children of the current headline
  4941. (org-back-to-heading)
  4942. (setq start (point)
  4943. end (progn (org-end-of-subtree t t)
  4944. (org-back-over-empty-lines)
  4945. (point))
  4946. what "children")
  4947. (goto-char start)
  4948. (show-subtree)
  4949. (outline-next-heading))
  4950. (t
  4951. ;; we will sort the top-level entries in this file
  4952. (goto-char (point-min))
  4953. (or (org-on-heading-p) (outline-next-heading))
  4954. (setq start (point) end (point-max) what "top-level")
  4955. (goto-char start)
  4956. (show-all)))
  4957. (setq beg (point))
  4958. (if (>= beg end) (error "Nothing to sort"))
  4959. (unless plain-list-p
  4960. (looking-at "\\(\\*+\\)")
  4961. (setq stars (match-string 1)
  4962. re (concat "^" (regexp-quote stars) " +")
  4963. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4964. txt (buffer-substring beg end))
  4965. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4966. (if (and (not (equal stars "*")) (string-match re2 txt))
  4967. (error "Region to sort contains a level above the first entry")))
  4968. (unless sorting-type
  4969. (message
  4970. (if plain-list-p
  4971. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4972. "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:")
  4973. what)
  4974. (setq sorting-type (read-char-exclusive))
  4975. (and (= (downcase sorting-type) ?f)
  4976. (setq getkey-func
  4977. (org-ido-completing-read "Sort using function: "
  4978. obarray 'fboundp t nil nil))
  4979. (setq getkey-func (intern getkey-func)))
  4980. (and (= (downcase sorting-type) ?r)
  4981. (setq property
  4982. (org-ido-completing-read "Property: "
  4983. (mapcar 'list (org-buffer-property-keys t))
  4984. nil t))))
  4985. (message "Sorting entries...")
  4986. (save-restriction
  4987. (narrow-to-region start end)
  4988. (let ((dcst (downcase sorting-type))
  4989. (now (current-time)))
  4990. (sort-subr
  4991. (/= dcst sorting-type)
  4992. ;; This function moves to the beginning character of the "record" to
  4993. ;; be sorted.
  4994. (if plain-list-p
  4995. (lambda nil
  4996. (if (org-at-item-p) t (goto-char (point-max))))
  4997. (lambda nil
  4998. (if (re-search-forward re nil t)
  4999. (goto-char (match-beginning 0))
  5000. (goto-char (point-max)))))
  5001. ;; This function moves to the last character of the "record" being
  5002. ;; sorted.
  5003. (if plain-list-p
  5004. 'org-end-of-item
  5005. (lambda nil
  5006. (save-match-data
  5007. (condition-case nil
  5008. (outline-forward-same-level 1)
  5009. (error
  5010. (goto-char (point-max)))))))
  5011. ;; This function returns the value that gets sorted against.
  5012. (if plain-list-p
  5013. (lambda nil
  5014. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5015. (cond
  5016. ((= dcst ?n)
  5017. (string-to-number (buffer-substring (match-end 0)
  5018. (point-at-eol))))
  5019. ((= dcst ?a)
  5020. (buffer-substring (match-end 0) (point-at-eol)))
  5021. ((= dcst ?t)
  5022. (if (re-search-forward org-ts-regexp
  5023. (point-at-eol) t)
  5024. (org-time-string-to-time (match-string 0))
  5025. now))
  5026. ((= dcst ?f)
  5027. (if getkey-func
  5028. (progn
  5029. (setq tmp (funcall getkey-func))
  5030. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5031. tmp)
  5032. (error "Invalid key function `%s'" getkey-func)))
  5033. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5034. (lambda nil
  5035. (cond
  5036. ((= dcst ?n)
  5037. (if (looking-at org-complex-heading-regexp)
  5038. (string-to-number (match-string 4))
  5039. nil))
  5040. ((= dcst ?a)
  5041. (if (looking-at org-complex-heading-regexp)
  5042. (funcall case-func (match-string 4))
  5043. nil))
  5044. ((= dcst ?t)
  5045. (if (re-search-forward org-ts-regexp
  5046. (save-excursion
  5047. (forward-line 2)
  5048. (point)) t)
  5049. (org-time-string-to-time (match-string 0))
  5050. now))
  5051. ((= dcst ?p)
  5052. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5053. (string-to-char (match-string 2))
  5054. org-default-priority))
  5055. ((= dcst ?r)
  5056. (or (org-entry-get nil property) ""))
  5057. ((= dcst ?o)
  5058. (if (looking-at org-complex-heading-regexp)
  5059. (- 9999 (length (member (match-string 2)
  5060. org-todo-keywords-1)))))
  5061. ((= dcst ?f)
  5062. (if getkey-func
  5063. (progn
  5064. (setq tmp (funcall getkey-func))
  5065. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5066. tmp)
  5067. (error "Invalid key function `%s'" getkey-func)))
  5068. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5069. nil
  5070. (cond
  5071. ((= dcst ?a) 'string<)
  5072. ((= dcst ?t) 'time-less-p)
  5073. ((= dcst ?f) compare-func)
  5074. (t nil)))))
  5075. (message "Sorting entries...done")))
  5076. (defun org-do-sort (table what &optional with-case sorting-type)
  5077. "Sort TABLE of WHAT according to SORTING-TYPE.
  5078. The user will be prompted for the SORTING-TYPE if the call to this
  5079. function does not specify it. WHAT is only for the prompt, to indicate
  5080. what is being sorted. The sorting key will be extracted from
  5081. the car of the elements of the table.
  5082. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5083. (unless sorting-type
  5084. (message
  5085. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5086. what)
  5087. (setq sorting-type (read-char-exclusive)))
  5088. (let ((dcst (downcase sorting-type))
  5089. extractfun comparefun)
  5090. ;; Define the appropriate functions
  5091. (cond
  5092. ((= dcst ?n)
  5093. (setq extractfun 'string-to-number
  5094. comparefun (if (= dcst sorting-type) '< '>)))
  5095. ((= dcst ?a)
  5096. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5097. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5098. comparefun (if (= dcst sorting-type)
  5099. 'string<
  5100. (lambda (a b) (and (not (string< a b))
  5101. (not (string= a b)))))))
  5102. ((= dcst ?t)
  5103. (setq extractfun
  5104. (lambda (x)
  5105. (if (string-match org-ts-regexp x)
  5106. (time-to-seconds
  5107. (org-time-string-to-time (match-string 0 x)))
  5108. 0))
  5109. comparefun (if (= dcst sorting-type) '< '>)))
  5110. (t (error "Invalid sorting type `%c'" sorting-type)))
  5111. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5112. table)
  5113. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5114. ;;; Editing source examples
  5115. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5116. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5117. (defvar org-edit-src-force-single-line nil)
  5118. (defvar org-edit-src-from-org-mode nil)
  5119. (defvar org-edit-src-picture nil)
  5120. (define-minor-mode org-exit-edit-mode
  5121. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5122. (defun org-edit-src-code ()
  5123. "Edit the source code example at point.
  5124. An indirect buffer is created, and that buffer is then narrowed to the
  5125. example at point and switched to the correct language mode. When done,
  5126. exit by killing the buffer with \\[org-edit-src-exit]."
  5127. (interactive)
  5128. (let ((line (org-current-line))
  5129. (case-fold-search t)
  5130. (msg (substitute-command-keys
  5131. "Edit, then exit with C-c ' (C-c and single quote)"))
  5132. (info (org-edit-src-find-region-and-lang))
  5133. (org-mode-p (eq major-mode 'org-mode))
  5134. beg end lang lang-f single)
  5135. (if (not info)
  5136. nil
  5137. (setq beg (nth 0 info)
  5138. end (nth 1 info)
  5139. lang (nth 2 info)
  5140. single (nth 3 info)
  5141. lang-f (intern (concat lang "-mode")))
  5142. (unless (functionp lang-f)
  5143. (error "No such language mode: %s" lang-f))
  5144. (goto-line line)
  5145. (if (get-buffer "*Org Edit Src Example*")
  5146. (kill-buffer "*Org Edit Src Example*"))
  5147. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5148. "*Org Edit Src Example*"))
  5149. (narrow-to-region beg end)
  5150. (remove-text-properties beg end '(display nil invisible nil
  5151. intangible nil))
  5152. (let ((org-inhibit-startup t))
  5153. (funcall lang-f))
  5154. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5155. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5156. (when org-mode-p
  5157. (goto-char (point-min))
  5158. (while (re-search-forward "^," nil t)
  5159. (replace-match "")))
  5160. (goto-line line)
  5161. (org-exit-edit-mode)
  5162. (org-set-local 'header-line-format msg)
  5163. (message "%s" msg)
  5164. t)))
  5165. (defun org-edit-fixed-width-region ()
  5166. "Edit the fixed-width ascii drawing at point.
  5167. This must be a region where each line starts with a colon followed by
  5168. a space character.
  5169. An indirect buffer is created, and that buffer is then narrowed to the
  5170. example at point and switched to artist-mode. When done,
  5171. exit by killing the buffer with \\[org-edit-src-exit]."
  5172. (interactive)
  5173. (let ((line (org-current-line))
  5174. (case-fold-search t)
  5175. (msg (substitute-command-keys
  5176. "Edit, then exit with C-c ' (C-c and single quote)"))
  5177. (org-mode-p (eq major-mode 'org-mode))
  5178. beg end lang lang-f)
  5179. (beginning-of-line 1)
  5180. (if (looking-at "[ \t]*[^:\n \t]")
  5181. nil
  5182. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5183. (setq beg (point) end beg)
  5184. (save-excursion
  5185. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5186. (setq beg (point-at-bol 2))
  5187. (setq beg (point))))
  5188. (save-excursion
  5189. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5190. (setq end (1- (match-beginning 0)))
  5191. (setq end (point))))
  5192. (goto-line line))
  5193. (if (get-buffer "*Org Edit Picture*")
  5194. (kill-buffer "*Org Edit Picture*"))
  5195. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5196. "*Org Edit Picture*"))
  5197. (narrow-to-region beg end)
  5198. (remove-text-properties beg end '(display nil invisible nil
  5199. intangible nil))
  5200. (when (fboundp 'font-lock-unfontify-region)
  5201. (font-lock-unfontify-region (point-min) (point-max)))
  5202. (cond
  5203. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5204. (fundamental-mode)
  5205. (artist-mode 1))
  5206. (t (funcall org-edit-fixed-width-region-mode)))
  5207. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5208. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5209. (set (make-local-variable 'org-edit-src-picture) t)
  5210. (goto-char (point-min))
  5211. (while (re-search-forward "^[ \t]*: ?" nil t)
  5212. (replace-match ""))
  5213. (goto-line line)
  5214. (org-exit-edit-mode)
  5215. (org-set-local 'header-line-format msg)
  5216. (message "%s" msg)
  5217. t)))
  5218. (defun org-edit-src-find-region-and-lang ()
  5219. "Find the region and language for a local edit.
  5220. Return a list with beginning and end of the region, a string representing
  5221. the language, a switch telling of the content should be in a single line."
  5222. (let ((re-list
  5223. (append
  5224. org-edit-src-region-extra
  5225. '(
  5226. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5227. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5228. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5229. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5230. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5231. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5232. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5233. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5234. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5235. ("^#\\+html:" "\n" "html" single-line)
  5236. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5237. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5238. ("^#\\+latex:" "\n" "latex" single-line)
  5239. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5240. ("^#\\+ascii:" "\n" "ascii" single-line)
  5241. )))
  5242. (pos (point))
  5243. re re1 re2 single beg end lang)
  5244. (catch 'exit
  5245. (while (setq entry (pop re-list))
  5246. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5247. single (nth 3 entry))
  5248. (save-excursion
  5249. (if (or (looking-at re1)
  5250. (re-search-backward re1 nil t))
  5251. (progn
  5252. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5253. (if (and (re-search-forward re2 nil t)
  5254. (>= (match-end 0) pos))
  5255. (throw 'exit (list beg (match-beginning 0) lang single))))
  5256. (if (or (looking-at re2)
  5257. (re-search-forward re2 nil t))
  5258. (progn
  5259. (setq end (match-beginning 0))
  5260. (if (and (re-search-backward re1 nil t)
  5261. (<= (match-beginning 0) pos))
  5262. (throw 'exit
  5263. (list (match-end 0) end
  5264. (org-edit-src-get-lang lang) single)))))))))))
  5265. (defun org-edit-src-get-lang (lang)
  5266. "Extract the src language."
  5267. (let ((m (match-string 0)))
  5268. (cond
  5269. ((stringp lang) lang)
  5270. ((integerp lang) (match-string lang))
  5271. ((and (eq lang 'lang)
  5272. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5273. (match-string 1 m))
  5274. ((and (eq lang 'style)
  5275. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5276. (match-string 1 m))
  5277. (t "fundamental"))))
  5278. (defun org-edit-src-exit ()
  5279. "Exit special edit and protect problematic lines."
  5280. (interactive)
  5281. (unless (buffer-base-buffer (current-buffer))
  5282. (error "This is not an indirect buffer, something is wrong..."))
  5283. (unless (> (point-min) 1)
  5284. (error "This buffer is not narrowed, something is wrong..."))
  5285. (goto-char (point-min))
  5286. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5287. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5288. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5289. (goto-char (point-min))
  5290. (while (re-search-forward "\n" nil t)
  5291. (replace-match " "))
  5292. (goto-char (point-min))
  5293. (if (looking-at "\\s-*") (replace-match " "))
  5294. (if (re-search-forward "\\s-+\\'" nil t)
  5295. (replace-match "")))
  5296. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5297. (goto-char (point-min))
  5298. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5299. (replace-match ",\\1"))
  5300. (when font-lock-mode
  5301. (font-lock-unfontify-region (point-min) (point-max)))
  5302. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5303. (when (org-bound-and-true-p org-edit-src-picture)
  5304. (untabify (point-min) (point-max))
  5305. (goto-char (point-min))
  5306. (while (re-search-forward "^" nil t)
  5307. (replace-match ": "))
  5308. (when font-lock-mode
  5309. (font-lock-unfontify-region (point-min) (point-max)))
  5310. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5311. (kill-buffer (current-buffer))
  5312. (and (org-mode-p) (org-restart-font-lock)))
  5313. ;;; The orgstruct minor mode
  5314. ;; Define a minor mode which can be used in other modes in order to
  5315. ;; integrate the org-mode structure editing commands.
  5316. ;; This is really a hack, because the org-mode structure commands use
  5317. ;; keys which normally belong to the major mode. Here is how it
  5318. ;; works: The minor mode defines all the keys necessary to operate the
  5319. ;; structure commands, but wraps the commands into a function which
  5320. ;; tests if the cursor is currently at a headline or a plain list
  5321. ;; item. If that is the case, the structure command is used,
  5322. ;; temporarily setting many Org-mode variables like regular
  5323. ;; expressions for filling etc. However, when any of those keys is
  5324. ;; used at a different location, function uses `key-binding' to look
  5325. ;; up if the key has an associated command in another currently active
  5326. ;; keymap (minor modes, major mode, global), and executes that
  5327. ;; command. There might be problems if any of the keys is otherwise
  5328. ;; used as a prefix key.
  5329. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5330. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5331. ;; addresses this by checking explicitly for both bindings.
  5332. (defvar orgstruct-mode-map (make-sparse-keymap)
  5333. "Keymap for the minor `orgstruct-mode'.")
  5334. (defvar org-local-vars nil
  5335. "List of local variables, for use by `orgstruct-mode'")
  5336. ;;;###autoload
  5337. (define-minor-mode orgstruct-mode
  5338. "Toggle the minor more `orgstruct-mode'.
  5339. This mode is for using Org-mode structure commands in other modes.
  5340. The following key behave as if Org-mode was active, if the cursor
  5341. is on a headline, or on a plain list item (both in the definition
  5342. of Org-mode).
  5343. M-up Move entry/item up
  5344. M-down Move entry/item down
  5345. M-left Promote
  5346. M-right Demote
  5347. M-S-up Move entry/item up
  5348. M-S-down Move entry/item down
  5349. M-S-left Promote subtree
  5350. M-S-right Demote subtree
  5351. M-q Fill paragraph and items like in Org-mode
  5352. C-c ^ Sort entries
  5353. C-c - Cycle list bullet
  5354. TAB Cycle item visibility
  5355. M-RET Insert new heading/item
  5356. S-M-RET Insert new TODO heading / Checkbox item
  5357. C-c C-c Set tags / toggle checkbox"
  5358. nil " OrgStruct" nil
  5359. (org-load-modules-maybe)
  5360. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5361. ;;;###autoload
  5362. (defun turn-on-orgstruct ()
  5363. "Unconditionally turn on `orgstruct-mode'."
  5364. (orgstruct-mode 1))
  5365. ;;;###autoload
  5366. (defun turn-on-orgstruct++ ()
  5367. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5368. In addition to setting orgstruct-mode, this also exports all indentation and
  5369. autofilling variables from org-mode into the buffer. Note that turning
  5370. off orgstruct-mode will *not* remove these additional settings."
  5371. (orgstruct-mode 1)
  5372. (let (var val)
  5373. (mapc
  5374. (lambda (x)
  5375. (when (string-match
  5376. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5377. (symbol-name (car x)))
  5378. (setq var (car x) val (nth 1 x))
  5379. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5380. org-local-vars)))
  5381. (defun orgstruct-error ()
  5382. "Error when there is no default binding for a structure key."
  5383. (interactive)
  5384. (error "This key has no function outside structure elements"))
  5385. (defun orgstruct-setup ()
  5386. "Setup orgstruct keymaps."
  5387. (let ((nfunc 0)
  5388. (bindings
  5389. (list
  5390. '([(meta up)] org-metaup)
  5391. '([(meta down)] org-metadown)
  5392. '([(meta left)] org-metaleft)
  5393. '([(meta right)] org-metaright)
  5394. '([(meta shift up)] org-shiftmetaup)
  5395. '([(meta shift down)] org-shiftmetadown)
  5396. '([(meta shift left)] org-shiftmetaleft)
  5397. '([(meta shift right)] org-shiftmetaright)
  5398. '([(shift up)] org-shiftup)
  5399. '([(shift down)] org-shiftdown)
  5400. '([(shift left)] org-shiftleft)
  5401. '([(shift right)] org-shiftright)
  5402. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5403. '("\M-q" fill-paragraph)
  5404. '("\C-c^" org-sort)
  5405. '("\C-c-" org-cycle-list-bullet)))
  5406. elt key fun cmd)
  5407. (while (setq elt (pop bindings))
  5408. (setq nfunc (1+ nfunc))
  5409. (setq key (org-key (car elt))
  5410. fun (nth 1 elt)
  5411. cmd (orgstruct-make-binding fun nfunc key))
  5412. (org-defkey orgstruct-mode-map key cmd))
  5413. ;; Special treatment needed for TAB and RET
  5414. (org-defkey orgstruct-mode-map [(tab)]
  5415. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5416. (org-defkey orgstruct-mode-map "\C-i"
  5417. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5418. (org-defkey orgstruct-mode-map "\M-\C-m"
  5419. (orgstruct-make-binding 'org-insert-heading 105
  5420. "\M-\C-m" [(meta return)]))
  5421. (org-defkey orgstruct-mode-map [(meta return)]
  5422. (orgstruct-make-binding 'org-insert-heading 106
  5423. [(meta return)] "\M-\C-m"))
  5424. (org-defkey orgstruct-mode-map [(shift meta return)]
  5425. (orgstruct-make-binding 'org-insert-todo-heading 107
  5426. [(meta return)] "\M-\C-m"))
  5427. (unless org-local-vars
  5428. (setq org-local-vars (org-get-local-variables)))
  5429. t))
  5430. (defun orgstruct-make-binding (fun n &rest keys)
  5431. "Create a function for binding in the structure minor mode.
  5432. FUN is the command to call inside a table. N is used to create a unique
  5433. command name. KEYS are keys that should be checked in for a command
  5434. to execute outside of tables."
  5435. (eval
  5436. (list 'defun
  5437. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5438. '(arg)
  5439. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5440. "Outside of structure, run the binding of `"
  5441. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5442. "'.")
  5443. '(interactive "p")
  5444. (list 'if
  5445. '(org-context-p 'headline 'item)
  5446. (list 'org-run-like-in-org-mode (list 'quote fun))
  5447. (list 'let '(orgstruct-mode)
  5448. (list 'call-interactively
  5449. (append '(or)
  5450. (mapcar (lambda (k)
  5451. (list 'key-binding k))
  5452. keys)
  5453. '('orgstruct-error))))))))
  5454. (defun org-context-p (&rest contexts)
  5455. "Check if local context is any of CONTEXTS.
  5456. Possible values in the list of contexts are `table', `headline', and `item'."
  5457. (let ((pos (point)))
  5458. (goto-char (point-at-bol))
  5459. (prog1 (or (and (memq 'table contexts)
  5460. (looking-at "[ \t]*|"))
  5461. (and (memq 'headline contexts)
  5462. ;;????????? (looking-at "\\*+"))
  5463. (looking-at outline-regexp))
  5464. (and (memq 'item contexts)
  5465. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5466. (goto-char pos))))
  5467. (defun org-get-local-variables ()
  5468. "Return a list of all local variables in an org-mode buffer."
  5469. (let (varlist)
  5470. (with-current-buffer (get-buffer-create "*Org tmp*")
  5471. (erase-buffer)
  5472. (org-mode)
  5473. (setq varlist (buffer-local-variables)))
  5474. (kill-buffer "*Org tmp*")
  5475. (delq nil
  5476. (mapcar
  5477. (lambda (x)
  5478. (setq x
  5479. (if (symbolp x)
  5480. (list x)
  5481. (list (car x) (list 'quote (cdr x)))))
  5482. (if (string-match
  5483. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5484. (symbol-name (car x)))
  5485. x nil))
  5486. varlist))))
  5487. ;;;###autoload
  5488. (defun org-run-like-in-org-mode (cmd)
  5489. (org-load-modules-maybe)
  5490. (unless org-local-vars
  5491. (setq org-local-vars (org-get-local-variables)))
  5492. (eval (list 'let org-local-vars
  5493. (list 'call-interactively (list 'quote cmd)))))
  5494. ;;;; Archiving
  5495. (defun org-get-category (&optional pos)
  5496. "Get the category applying to position POS."
  5497. (get-text-property (or pos (point)) 'org-category))
  5498. (defun org-refresh-category-properties ()
  5499. "Refresh category text properties in the buffer."
  5500. (let ((def-cat (cond
  5501. ((null org-category)
  5502. (if buffer-file-name
  5503. (file-name-sans-extension
  5504. (file-name-nondirectory buffer-file-name))
  5505. "???"))
  5506. ((symbolp org-category) (symbol-name org-category))
  5507. (t org-category)))
  5508. beg end cat pos optionp)
  5509. (org-unmodified
  5510. (save-excursion
  5511. (save-restriction
  5512. (widen)
  5513. (goto-char (point-min))
  5514. (put-text-property (point) (point-max) 'org-category def-cat)
  5515. (while (re-search-forward
  5516. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5517. (setq pos (match-end 0)
  5518. optionp (equal (char-after (match-beginning 0)) ?#)
  5519. cat (org-trim (match-string 2)))
  5520. (if optionp
  5521. (setq beg (point-at-bol) end (point-max))
  5522. (org-back-to-heading t)
  5523. (setq beg (point) end (org-end-of-subtree t t)))
  5524. (put-text-property beg end 'org-category cat)
  5525. (goto-char pos)))))))
  5526. ;;;; Link Stuff
  5527. ;;; Link abbreviations
  5528. (defun org-link-expand-abbrev (link)
  5529. "Apply replacements as defined in `org-link-abbrev-alist."
  5530. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5531. (let* ((key (match-string 1 link))
  5532. (as (or (assoc key org-link-abbrev-alist-local)
  5533. (assoc key org-link-abbrev-alist)))
  5534. (tag (and (match-end 2) (match-string 3 link)))
  5535. rpl)
  5536. (if (not as)
  5537. link
  5538. (setq rpl (cdr as))
  5539. (cond
  5540. ((symbolp rpl) (funcall rpl tag))
  5541. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5542. ((string-match "%h" rpl)
  5543. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5544. (t (concat rpl tag)))))
  5545. link))
  5546. ;;; Storing and inserting links
  5547. (defvar org-insert-link-history nil
  5548. "Minibuffer history for links inserted with `org-insert-link'.")
  5549. (defvar org-stored-links nil
  5550. "Contains the links stored with `org-store-link'.")
  5551. (defvar org-store-link-plist nil
  5552. "Plist with info about the most recently link created with `org-store-link'.")
  5553. (defvar org-link-protocols nil
  5554. "Link protocols added to Org-mode using `org-add-link-type'.")
  5555. (defvar org-store-link-functions nil
  5556. "List of functions that are called to create and store a link.
  5557. Each function will be called in turn until one returns a non-nil
  5558. value. Each function should check if it is responsible for creating
  5559. this link (for example by looking at the major mode).
  5560. If not, it must exit and return nil.
  5561. If yes, it should return a non-nil value after a calling
  5562. `org-store-link-props' with a list of properties and values.
  5563. Special properties are:
  5564. :type The link prefix. like \"http\". This must be given.
  5565. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5566. This is obligatory as well.
  5567. :description Optional default description for the second pair
  5568. of brackets in an Org-mode link. The user can still change
  5569. this when inserting this link into an Org-mode buffer.
  5570. In addition to these, any additional properties can be specified
  5571. and then used in remember templates.")
  5572. (defun org-add-link-type (type &optional follow export)
  5573. "Add TYPE to the list of `org-link-types'.
  5574. Re-compute all regular expressions depending on `org-link-types'
  5575. FOLLOW and EXPORT are two functions.
  5576. FOLLOW should take the link path as the single argument and do whatever
  5577. is necessary to follow the link, for example find a file or display
  5578. a mail message.
  5579. EXPORT should format the link path for export to one of the export formats.
  5580. It should be a function accepting three arguments:
  5581. path the path of the link, the text after the prefix (like \"http:\")
  5582. desc the description of the link, if any, nil if there was no description
  5583. format the export format, a symbol like `html' or `latex'.
  5584. The function may use the FORMAT information to return different values
  5585. depending on the format. The return value will be put literally into
  5586. the exported file.
  5587. Org-mode has a built-in default for exporting links. If you are happy with
  5588. this default, there is no need to define an export function for the link
  5589. type. For a simple example of an export function, see `org-bbdb.el'."
  5590. (add-to-list 'org-link-types type t)
  5591. (org-make-link-regexps)
  5592. (if (assoc type org-link-protocols)
  5593. (setcdr (assoc type org-link-protocols) (list follow export))
  5594. (push (list type follow export) org-link-protocols)))
  5595. ;;;###autoload
  5596. (defun org-store-link (arg)
  5597. "\\<org-mode-map>Store an org-link to the current location.
  5598. This link is added to `org-stored-links' and can later be inserted
  5599. into an org-buffer with \\[org-insert-link].
  5600. For some link types, a prefix arg is interpreted:
  5601. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5602. For file links, arg negates `org-context-in-file-links'."
  5603. (interactive "P")
  5604. (org-load-modules-maybe)
  5605. (setq org-store-link-plist nil) ; reset
  5606. (let (link cpltxt desc description search txt)
  5607. (cond
  5608. ((run-hook-with-args-until-success 'org-store-link-functions)
  5609. (setq link (plist-get org-store-link-plist :link)
  5610. desc (or (plist-get org-store-link-plist :description) link)))
  5611. ((equal (buffer-name) "*Org Edit Src Example*")
  5612. (let (label gc)
  5613. (while (or (not label)
  5614. (save-excursion
  5615. (save-restriction
  5616. (widen)
  5617. (goto-char (point-min))
  5618. (re-search-forward (concat "((" label "))") nil t))))
  5619. (when label (message "Label exists already") (sit-for 2))
  5620. (setq label (read-string "Code line label: " label)))
  5621. (end-of-line 1)
  5622. (setq link (format "((%s))" label))
  5623. (setq gc (- 79 (length link)))
  5624. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5625. (insert link)
  5626. (setq desc nil)))
  5627. ((eq major-mode 'calendar-mode)
  5628. (let ((cd (calendar-cursor-to-date)))
  5629. (setq link
  5630. (format-time-string
  5631. (car org-time-stamp-formats)
  5632. (apply 'encode-time
  5633. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5634. nil nil nil))))
  5635. (org-store-link-props :type "calendar" :date cd)))
  5636. ((eq major-mode 'w3-mode)
  5637. (setq cpltxt (url-view-url t)
  5638. link (org-make-link cpltxt))
  5639. (org-store-link-props :type "w3" :url (url-view-url t)))
  5640. ((eq major-mode 'w3m-mode)
  5641. (setq cpltxt (or w3m-current-title w3m-current-url)
  5642. link (org-make-link w3m-current-url))
  5643. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5644. ((setq search (run-hook-with-args-until-success
  5645. 'org-create-file-search-functions))
  5646. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5647. "::" search))
  5648. (setq cpltxt (or description link)))
  5649. ((eq major-mode 'image-mode)
  5650. (setq cpltxt (concat "file:"
  5651. (abbreviate-file-name buffer-file-name))
  5652. link (org-make-link cpltxt))
  5653. (org-store-link-props :type "image" :file buffer-file-name))
  5654. ((eq major-mode 'dired-mode)
  5655. ;; link to the file in the current line
  5656. (setq cpltxt (concat "file:"
  5657. (abbreviate-file-name
  5658. (expand-file-name
  5659. (dired-get-filename nil t))))
  5660. link (org-make-link cpltxt)))
  5661. ((and buffer-file-name (org-mode-p))
  5662. (cond
  5663. ((org-in-regexp "<<\\(.*?\\)>>")
  5664. (setq cpltxt
  5665. (concat "file:"
  5666. (abbreviate-file-name buffer-file-name)
  5667. "::" (match-string 1))
  5668. link (org-make-link cpltxt)))
  5669. ((and (featurep 'org-id)
  5670. (or (eq org-link-to-org-use-id t)
  5671. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5672. (interactive-p))
  5673. (and org-link-to-org-use-id
  5674. (condition-case nil
  5675. (org-entry-get nil "ID")
  5676. (error nil)))))
  5677. ;; We can make a link using the ID.
  5678. (setq link (condition-case nil
  5679. (prog1 (org-id-store-link)
  5680. (setq desc (plist-get org-store-link-plist
  5681. :description)))
  5682. (error
  5683. ;; probably before first headline, link to file only
  5684. (concat "file:"
  5685. (abbreviate-file-name buffer-file-name))))))
  5686. (t
  5687. ;; Just link to current headline
  5688. (setq cpltxt (concat "file:"
  5689. (abbreviate-file-name buffer-file-name)))
  5690. ;; Add a context search string
  5691. (when (org-xor org-context-in-file-links arg)
  5692. (setq txt (cond
  5693. ((org-on-heading-p) nil)
  5694. ((org-region-active-p)
  5695. (buffer-substring (region-beginning) (region-end)))
  5696. (t nil)))
  5697. (when (or (null txt) (string-match "\\S-" txt))
  5698. (setq cpltxt
  5699. (concat cpltxt "::"
  5700. (condition-case nil
  5701. (org-make-org-heading-search-string txt)
  5702. (error "")))
  5703. desc "NONE")))
  5704. (if (string-match "::\\'" cpltxt)
  5705. (setq cpltxt (substring cpltxt 0 -2)))
  5706. (setq link (org-make-link cpltxt)))))
  5707. ((buffer-file-name (buffer-base-buffer))
  5708. ;; Just link to this file here.
  5709. (setq cpltxt (concat "file:"
  5710. (abbreviate-file-name
  5711. (buffer-file-name (buffer-base-buffer)))))
  5712. ;; Add a context string
  5713. (when (org-xor org-context-in-file-links arg)
  5714. (setq txt (if (org-region-active-p)
  5715. (buffer-substring (region-beginning) (region-end))
  5716. (buffer-substring (point-at-bol) (point-at-eol))))
  5717. ;; Only use search option if there is some text.
  5718. (when (string-match "\\S-" txt)
  5719. (setq cpltxt
  5720. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5721. desc "NONE")))
  5722. (setq link (org-make-link cpltxt)))
  5723. ((interactive-p)
  5724. (error "Cannot link to a buffer which is not visiting a file"))
  5725. (t (setq link nil)))
  5726. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5727. (setq link (or link cpltxt)
  5728. desc (or desc cpltxt))
  5729. (if (equal desc "NONE") (setq desc nil))
  5730. (if (and (interactive-p) link)
  5731. (progn
  5732. (setq org-stored-links
  5733. (cons (list link desc) org-stored-links))
  5734. (message "Stored: %s" (or desc link)))
  5735. (and link (org-make-link-string link desc)))))
  5736. (defun org-store-link-props (&rest plist)
  5737. "Store link properties, extract names and addresses."
  5738. (let (x adr)
  5739. (when (setq x (plist-get plist :from))
  5740. (setq adr (mail-extract-address-components x))
  5741. (setq plist (plist-put plist :fromname (car adr)))
  5742. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5743. (when (setq x (plist-get plist :to))
  5744. (setq adr (mail-extract-address-components x))
  5745. (setq plist (plist-put plist :toname (car adr)))
  5746. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5747. (let ((from (plist-get plist :from))
  5748. (to (plist-get plist :to)))
  5749. (when (and from to org-from-is-user-regexp)
  5750. (setq plist
  5751. (plist-put plist :fromto
  5752. (if (string-match org-from-is-user-regexp from)
  5753. (concat "to %t")
  5754. (concat "from %f"))))))
  5755. (setq org-store-link-plist plist))
  5756. (defun org-add-link-props (&rest plist)
  5757. "Add these properties to the link property list."
  5758. (let (key value)
  5759. (while plist
  5760. (setq key (pop plist) value (pop plist))
  5761. (setq org-store-link-plist
  5762. (plist-put org-store-link-plist key value)))))
  5763. (defun org-email-link-description (&optional fmt)
  5764. "Return the description part of an email link.
  5765. This takes information from `org-store-link-plist' and formats it
  5766. according to FMT (default from `org-email-link-description-format')."
  5767. (setq fmt (or fmt org-email-link-description-format))
  5768. (let* ((p org-store-link-plist)
  5769. (to (plist-get p :toaddress))
  5770. (from (plist-get p :fromaddress))
  5771. (table
  5772. (list
  5773. (cons "%c" (plist-get p :fromto))
  5774. (cons "%F" (plist-get p :from))
  5775. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5776. (cons "%T" (plist-get p :to))
  5777. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5778. (cons "%s" (plist-get p :subject))
  5779. (cons "%m" (plist-get p :message-id)))))
  5780. (when (string-match "%c" fmt)
  5781. ;; Check if the user wrote this message
  5782. (if (and org-from-is-user-regexp from to
  5783. (save-match-data (string-match org-from-is-user-regexp from)))
  5784. (setq fmt (replace-match "to %t" t t fmt))
  5785. (setq fmt (replace-match "from %f" t t fmt))))
  5786. (org-replace-escapes fmt table)))
  5787. (defun org-make-org-heading-search-string (&optional string heading)
  5788. "Make search string for STRING or current headline."
  5789. (interactive)
  5790. (let ((s (or string (org-get-heading))))
  5791. (unless (and string (not heading))
  5792. ;; We are using a headline, clean up garbage in there.
  5793. (if (string-match org-todo-regexp s)
  5794. (setq s (replace-match "" t t s)))
  5795. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5796. (setq s (replace-match "" t t s)))
  5797. (setq s (org-trim s))
  5798. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5799. org-comment-string "\\)") s)
  5800. (setq s (replace-match "" t t s)))
  5801. (while (string-match org-ts-regexp s)
  5802. (setq s (replace-match "" t t s))))
  5803. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5804. (setq s (replace-match " " t t s)))
  5805. (or string (setq s (concat "*" s))) ; Add * for headlines
  5806. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5807. (defun org-make-link (&rest strings)
  5808. "Concatenate STRINGS."
  5809. (apply 'concat strings))
  5810. (defun org-make-link-string (link &optional description)
  5811. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5812. (unless (string-match "\\S-" link)
  5813. (error "Empty link"))
  5814. (when (stringp description)
  5815. ;; Remove brackets from the description, they are fatal.
  5816. (while (string-match "\\[" description)
  5817. (setq description (replace-match "{" t t description)))
  5818. (while (string-match "\\]" description)
  5819. (setq description (replace-match "}" t t description))))
  5820. (when (equal (org-link-escape link) description)
  5821. ;; No description needed, it is identical
  5822. (setq description nil))
  5823. (when (and (not description)
  5824. (not (equal link (org-link-escape link))))
  5825. (setq description (org-extract-attributes link)))
  5826. (concat "[[" (org-link-escape link) "]"
  5827. (if description (concat "[" description "]") "")
  5828. "]"))
  5829. (defconst org-link-escape-chars
  5830. '((?\ . "%20")
  5831. (?\[ . "%5B")
  5832. (?\] . "%5D")
  5833. (?\340 . "%E0") ; `a
  5834. (?\342 . "%E2") ; ^a
  5835. (?\347 . "%E7") ; ,c
  5836. (?\350 . "%E8") ; `e
  5837. (?\351 . "%E9") ; 'e
  5838. (?\352 . "%EA") ; ^e
  5839. (?\356 . "%EE") ; ^i
  5840. (?\364 . "%F4") ; ^o
  5841. (?\371 . "%F9") ; `u
  5842. (?\373 . "%FB") ; ^u
  5843. (?\; . "%3B")
  5844. (?? . "%3F")
  5845. (?= . "%3D")
  5846. (?+ . "%2B")
  5847. )
  5848. "Association list of escapes for some characters problematic in links.
  5849. This is the list that is used for internal purposes.")
  5850. (defconst org-link-escape-chars-browser
  5851. '((?\ . "%20")) ; 32 for the SPC char
  5852. "Association list of escapes for some characters problematic in links.
  5853. This is the list that is used before handing over to the browser.")
  5854. (defun org-link-escape (text &optional table)
  5855. "Escape characters in TEXT that are problematic for links."
  5856. (setq table (or table org-link-escape-chars))
  5857. (when text
  5858. (let ((re (mapconcat (lambda (x) (regexp-quote
  5859. (char-to-string (car x))))
  5860. table "\\|")))
  5861. (while (string-match re text)
  5862. (setq text
  5863. (replace-match
  5864. (cdr (assoc (string-to-char (match-string 0 text))
  5865. table))
  5866. t t text)))
  5867. text)))
  5868. (defun org-link-unescape (text &optional table)
  5869. "Reverse the action of `org-link-escape'."
  5870. (setq table (or table org-link-escape-chars))
  5871. (when text
  5872. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5873. table "\\|")))
  5874. (while (string-match re text)
  5875. (setq text
  5876. (replace-match
  5877. (char-to-string (car (rassoc (match-string 0 text) table)))
  5878. t t text)))
  5879. text)))
  5880. (defun org-xor (a b)
  5881. "Exclusive or."
  5882. (if a (not b) b))
  5883. (defun org-get-header (header)
  5884. "Find a header field in the current buffer."
  5885. (save-excursion
  5886. (goto-char (point-min))
  5887. (let ((case-fold-search t) s)
  5888. (cond
  5889. ((eq header 'from)
  5890. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5891. (setq s (match-string 1)))
  5892. (while (string-match "\"" s)
  5893. (setq s (replace-match "" t t s)))
  5894. (if (string-match "[<(].*" s)
  5895. (setq s (replace-match "" t t s))))
  5896. ((eq header 'message-id)
  5897. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5898. (setq s (match-string 1))))
  5899. ((eq header 'subject)
  5900. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5901. (setq s (match-string 1)))))
  5902. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5903. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5904. s)))
  5905. (defun org-fixup-message-id-for-http (s)
  5906. "Replace special characters in a message id, so it can be used in an http query."
  5907. (while (string-match "<" s)
  5908. (setq s (replace-match "%3C" t t s)))
  5909. (while (string-match ">" s)
  5910. (setq s (replace-match "%3E" t t s)))
  5911. (while (string-match "@" s)
  5912. (setq s (replace-match "%40" t t s)))
  5913. s)
  5914. ;;;###autoload
  5915. (defun org-insert-link-global ()
  5916. "Insert a link like Org-mode does.
  5917. This command can be called in any mode to insert a link in Org-mode syntax."
  5918. (interactive)
  5919. (org-load-modules-maybe)
  5920. (org-run-like-in-org-mode 'org-insert-link))
  5921. (defun org-insert-link (&optional complete-file link-location)
  5922. "Insert a link. At the prompt, enter the link.
  5923. Completion can be used to insert any of the link protocol prefixes like
  5924. http or ftp in use.
  5925. The history can be used to select a link previously stored with
  5926. `org-store-link'. When the empty string is entered (i.e. if you just
  5927. press RET at the prompt), the link defaults to the most recently
  5928. stored link. As SPC triggers completion in the minibuffer, you need to
  5929. use M-SPC or C-q SPC to force the insertion of a space character.
  5930. You will also be prompted for a description, and if one is given, it will
  5931. be displayed in the buffer instead of the link.
  5932. If there is already a link at point, this command will allow you to edit link
  5933. and description parts.
  5934. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5935. be selected using completion. The path to the file will be relative to the
  5936. current directory if the file is in the current directory or a subdirectory.
  5937. Otherwise, the link will be the absolute path as completed in the minibuffer
  5938. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  5939. option `org-link-file-path-type'.
  5940. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5941. the current directory or below.
  5942. With three \\[universal-argument] prefixes, negate the meaning of
  5943. `org-keep-stored-link-after-insertion'.
  5944. If `org-make-link-description-function' is non-nil, this function will be
  5945. called with the link target, and the result will be the default
  5946. link description.
  5947. If the LINK-LOCATION parameter is non-nil, this value will be
  5948. used as the link location instead of reading one interactively."
  5949. (interactive "P")
  5950. (let* ((wcf (current-window-configuration))
  5951. (region (if (org-region-active-p)
  5952. (buffer-substring (region-beginning) (region-end))))
  5953. (remove (and region (list (region-beginning) (region-end))))
  5954. (desc region)
  5955. tmphist ; byte-compile incorrectly complains about this
  5956. (link link-location)
  5957. entry file)
  5958. (cond
  5959. (link-location) ; specified by arg, just use it.
  5960. ((org-in-regexp org-bracket-link-regexp 1)
  5961. ;; We do have a link at point, and we are going to edit it.
  5962. (setq remove (list (match-beginning 0) (match-end 0)))
  5963. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5964. (setq link (read-string "Link: "
  5965. (org-link-unescape
  5966. (org-match-string-no-properties 1)))))
  5967. ((or (org-in-regexp org-angle-link-re)
  5968. (org-in-regexp org-plain-link-re))
  5969. ;; Convert to bracket link
  5970. (setq remove (list (match-beginning 0) (match-end 0))
  5971. link (read-string "Link: "
  5972. (org-remove-angle-brackets (match-string 0)))))
  5973. ((member complete-file '((4) (16)))
  5974. ;; Completing read for file names.
  5975. (setq file (read-file-name "File: "))
  5976. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5977. (pwd1 (file-name-as-directory (abbreviate-file-name
  5978. (expand-file-name ".")))))
  5979. (cond
  5980. ((equal complete-file '(16))
  5981. (setq link (org-make-link
  5982. "file:"
  5983. (abbreviate-file-name (expand-file-name file)))))
  5984. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5985. (setq link (org-make-link "file:" (match-string 1 file))))
  5986. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5987. (expand-file-name file))
  5988. (setq link (org-make-link
  5989. "file:" (match-string 1 (expand-file-name file)))))
  5990. (t (setq link (org-make-link "file:" file))))))
  5991. (t
  5992. ;; Read link, with completion for stored links.
  5993. (with-output-to-temp-buffer "*Org Links*"
  5994. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5995. (when org-stored-links
  5996. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5997. (princ (mapconcat
  5998. (lambda (x)
  5999. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6000. (reverse org-stored-links) "\n"))))
  6001. (let ((cw (selected-window)))
  6002. (select-window (get-buffer-window "*Org Links*"))
  6003. (setq truncate-lines t)
  6004. (org-fit-window-to-buffer)
  6005. (select-window cw))
  6006. ;; Fake a link history, containing the stored links.
  6007. (setq tmphist (append (mapcar 'car org-stored-links)
  6008. org-insert-link-history))
  6009. (unwind-protect
  6010. (setq link
  6011. (let ((org-completion-use-ido nil))
  6012. (org-completing-read
  6013. "Link: "
  6014. (append
  6015. (mapcar (lambda (x) (list (concat (car x) ":")))
  6016. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6017. (mapcar (lambda (x) (list (concat x ":")))
  6018. org-link-types))
  6019. nil nil nil
  6020. 'tmphist
  6021. (or (car (car org-stored-links))))))
  6022. (set-window-configuration wcf)
  6023. (kill-buffer "*Org Links*"))
  6024. (setq entry (assoc link org-stored-links))
  6025. (or entry (push link org-insert-link-history))
  6026. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6027. (not org-keep-stored-link-after-insertion))
  6028. (setq org-stored-links (delq (assoc link org-stored-links)
  6029. org-stored-links)))
  6030. (setq desc (or desc (nth 1 entry)))))
  6031. (if (string-match org-plain-link-re link)
  6032. ;; URL-like link, normalize the use of angular brackets.
  6033. (setq link (org-make-link (org-remove-angle-brackets link))))
  6034. ;; Check if we are linking to the current file with a search option
  6035. ;; If yes, simplify the link by using only the search option.
  6036. (when (and buffer-file-name
  6037. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6038. (let* ((path (match-string 1 link))
  6039. (case-fold-search nil)
  6040. (search (match-string 2 link)))
  6041. (save-match-data
  6042. (if (equal (file-truename buffer-file-name) (file-truename path))
  6043. ;; We are linking to this same file, with a search option
  6044. (setq link search)))))
  6045. ;; Check if we can/should use a relative path. If yes, simplify the link
  6046. (when (string-match "^file:\\(.*\\)" link)
  6047. (let* ((path (match-string 1 link))
  6048. (origpath path)
  6049. (case-fold-search nil))
  6050. (cond
  6051. ((or (eq org-link-file-path-type 'absolute)
  6052. (equal complete-file '(16)))
  6053. (setq path (abbreviate-file-name (expand-file-name path))))
  6054. ((eq org-link-file-path-type 'noabbrev)
  6055. (setq path (expand-file-name path)))
  6056. ((eq org-link-file-path-type 'relative)
  6057. (setq path (file-relative-name path)))
  6058. (t
  6059. (save-match-data
  6060. (if (string-match (concat "^" (regexp-quote
  6061. (file-name-as-directory
  6062. (expand-file-name "."))))
  6063. (expand-file-name path))
  6064. ;; We are linking a file with relative path name.
  6065. (setq path (substring (expand-file-name path)
  6066. (match-end 0)))
  6067. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6068. (setq link (concat "file:" path))
  6069. (if (equal desc origpath)
  6070. (setq desc path))))
  6071. (if org-make-link-description-function
  6072. (setq desc (funcall org-make-link-description-function link desc)))
  6073. (setq desc (read-string "Description: " desc))
  6074. (unless (string-match "\\S-" desc) (setq desc nil))
  6075. (if remove (apply 'delete-region remove))
  6076. (insert (org-make-link-string link desc))))
  6077. (defun org-completing-read (&rest args)
  6078. "Completing-read with SPACE being a normal character."
  6079. (let ((minibuffer-local-completion-map
  6080. (copy-keymap minibuffer-local-completion-map)))
  6081. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6082. (apply 'org-ido-completing-read args)))
  6083. (defun org-ido-completing-read (&rest args)
  6084. "Completing-read using `ido-mode' speedups if available"
  6085. (if (and org-completion-use-ido
  6086. (fboundp 'ido-completing-read)
  6087. (boundp 'ido-mode) ido-mode
  6088. (listp (second args)))
  6089. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6090. (apply 'completing-read args)))
  6091. (defun org-extract-attributes (s)
  6092. "Extract the attributes cookie from a string and set as text property."
  6093. (let (a attr (start 0) key value)
  6094. (save-match-data
  6095. (when (string-match "{{\\([^}]+\\)}}$" s)
  6096. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6097. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6098. (setq key (match-string 1 a) value (match-string 2 a)
  6099. start (match-end 0)
  6100. attr (plist-put attr (intern key) value))))
  6101. (org-add-props s nil 'org-attr attr))
  6102. s))
  6103. (defun org-attributes-to-string (plist)
  6104. "Format a property list into an HTML attribute list."
  6105. (let ((s "") key value)
  6106. (while plist
  6107. (setq key (pop plist) value (pop plist))
  6108. (and value
  6109. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6110. s))
  6111. ;;; Opening/following a link
  6112. (defvar org-link-search-failed nil)
  6113. (defun org-next-link ()
  6114. "Move forward to the next link.
  6115. If the link is in hidden text, expose it."
  6116. (interactive)
  6117. (when (and org-link-search-failed (eq this-command last-command))
  6118. (goto-char (point-min))
  6119. (message "Link search wrapped back to beginning of buffer"))
  6120. (setq org-link-search-failed nil)
  6121. (let* ((pos (point))
  6122. (ct (org-context))
  6123. (a (assoc :link ct)))
  6124. (if a (goto-char (nth 2 a)))
  6125. (if (re-search-forward org-any-link-re nil t)
  6126. (progn
  6127. (goto-char (match-beginning 0))
  6128. (if (org-invisible-p) (org-show-context)))
  6129. (goto-char pos)
  6130. (setq org-link-search-failed t)
  6131. (error "No further link found"))))
  6132. (defun org-previous-link ()
  6133. "Move backward to the previous link.
  6134. If the link is in hidden text, expose it."
  6135. (interactive)
  6136. (when (and org-link-search-failed (eq this-command last-command))
  6137. (goto-char (point-max))
  6138. (message "Link search wrapped back to end of buffer"))
  6139. (setq org-link-search-failed nil)
  6140. (let* ((pos (point))
  6141. (ct (org-context))
  6142. (a (assoc :link ct)))
  6143. (if a (goto-char (nth 1 a)))
  6144. (if (re-search-backward org-any-link-re nil t)
  6145. (progn
  6146. (goto-char (match-beginning 0))
  6147. (if (org-invisible-p) (org-show-context)))
  6148. (goto-char pos)
  6149. (setq org-link-search-failed t)
  6150. (error "No further link found"))))
  6151. (defun org-translate-link (s)
  6152. "Translate a link string if a translation function has been defined."
  6153. (if (and org-link-translation-function
  6154. (fboundp org-link-translation-function)
  6155. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6156. (progn
  6157. (setq s (funcall org-link-translation-function
  6158. (match-string 1) (match-string 2)))
  6159. (concat (car s) ":" (cdr s)))
  6160. s))
  6161. (defun org-translate-link-from-planner (type path)
  6162. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6163. This is still an experimental function, your mileage may vary."
  6164. (cond
  6165. ((member type '("http" "https" "news" "ftp"))
  6166. ;; standard Internet links are the same.
  6167. nil)
  6168. ((and (equal type "irc") (string-match "^//" path))
  6169. ;; Planner has two / at the beginning of an irc link, we have 1.
  6170. ;; We should have zero, actually....
  6171. (setq path (substring path 1)))
  6172. ((and (equal type "lisp") (string-match "^/" path))
  6173. ;; Planner has a slash, we do not.
  6174. (setq type "elisp" path (substring path 1)))
  6175. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6176. ;; A typical message link. Planner has the id after the fina slash,
  6177. ;; we separate it with a hash mark
  6178. (setq path (concat (match-string 1 path) "#"
  6179. (org-remove-angle-brackets (match-string 2 path)))))
  6180. )
  6181. (cons type path))
  6182. (defun org-find-file-at-mouse (ev)
  6183. "Open file link or URL at mouse."
  6184. (interactive "e")
  6185. (mouse-set-point ev)
  6186. (org-open-at-point 'in-emacs))
  6187. (defun org-open-at-mouse (ev)
  6188. "Open file link or URL at mouse."
  6189. (interactive "e")
  6190. (mouse-set-point ev)
  6191. (if (eq major-mode 'org-agenda-mode)
  6192. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6193. (org-open-at-point))
  6194. (defvar org-window-config-before-follow-link nil
  6195. "The window configuration before following a link.
  6196. This is saved in case the need arises to restore it.")
  6197. (defvar org-open-link-marker (make-marker)
  6198. "Marker pointing to the location where `org-open-at-point; was called.")
  6199. ;;;###autoload
  6200. (defun org-open-at-point-global ()
  6201. "Follow a link like Org-mode does.
  6202. This command can be called in any mode to follow a link that has
  6203. Org-mode syntax."
  6204. (interactive)
  6205. (org-run-like-in-org-mode 'org-open-at-point))
  6206. ;;;###autoload
  6207. (defun org-open-link-from-string (s &optional arg)
  6208. "Open a link in the string S, as if it was in Org-mode."
  6209. (interactive "sLink: \nP")
  6210. (with-temp-buffer
  6211. (let ((org-inhibit-startup t))
  6212. (org-mode)
  6213. (insert s)
  6214. (goto-char (point-min))
  6215. (org-open-at-point arg))))
  6216. (defun org-open-at-point (&optional in-emacs)
  6217. "Open link at or after point.
  6218. If there is no link at point, this function will search forward up to
  6219. the end of the current subtree.
  6220. Normally, files will be opened by an appropriate application. If the
  6221. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6222. With a double prefix argument, try to open outside of Emacs, in the
  6223. application the system uses for this file type."
  6224. (interactive "P")
  6225. (org-load-modules-maybe)
  6226. (move-marker org-open-link-marker (point))
  6227. (setq org-window-config-before-follow-link (current-window-configuration))
  6228. (org-remove-occur-highlights nil nil t)
  6229. (if (org-at-timestamp-p t)
  6230. (org-follow-timestamp-link)
  6231. (let (type path link line search (pos (point)))
  6232. (catch 'match
  6233. (save-excursion
  6234. (skip-chars-forward "^]\n\r")
  6235. (when (org-in-regexp org-bracket-link-regexp)
  6236. (setq link (org-extract-attributes
  6237. (org-link-unescape (org-match-string-no-properties 1))))
  6238. (while (string-match " *\n *" link)
  6239. (setq link (replace-match " " t t link)))
  6240. (setq link (org-link-expand-abbrev link))
  6241. (cond
  6242. ((or (file-name-absolute-p link)
  6243. (string-match "^\\.\\.?/" link))
  6244. (setq type "file" path link))
  6245. ((string-match org-link-re-with-space3 link)
  6246. (setq type (match-string 1 link) path (match-string 2 link)))
  6247. (t (setq type "thisfile" path link)))
  6248. (throw 'match t)))
  6249. (when (get-text-property (point) 'org-linked-text)
  6250. (setq type "thisfile"
  6251. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6252. (1+ (point)) (point))
  6253. path (buffer-substring
  6254. (previous-single-property-change pos 'org-linked-text)
  6255. (next-single-property-change pos 'org-linked-text)))
  6256. (throw 'match t))
  6257. (save-excursion
  6258. (when (or (org-in-regexp org-angle-link-re)
  6259. (org-in-regexp org-plain-link-re))
  6260. (setq type (match-string 1) path (match-string 2))
  6261. (throw 'match t)))
  6262. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6263. (setq type "tree-match"
  6264. path (match-string 1))
  6265. (throw 'match t))
  6266. (save-excursion
  6267. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6268. (setq type "tags"
  6269. path (match-string 1))
  6270. (while (string-match ":" path)
  6271. (setq path (replace-match "+" t t path)))
  6272. (throw 'match t))))
  6273. (unless path
  6274. (error "No link found"))
  6275. ;; Remove any trailing spaces in path
  6276. (if (string-match " +\\'" path)
  6277. (setq path (replace-match "" t t path)))
  6278. (if (and org-link-translation-function
  6279. (fboundp org-link-translation-function))
  6280. ;; Check if we need to translate the link
  6281. (let ((tmp (funcall org-link-translation-function type path)))
  6282. (setq type (car tmp) path (cdr tmp))))
  6283. (cond
  6284. ((assoc type org-link-protocols)
  6285. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6286. ((equal type "mailto")
  6287. (let ((cmd (car org-link-mailto-program))
  6288. (args (cdr org-link-mailto-program)) args1
  6289. (address path) (subject "") a)
  6290. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6291. (setq address (match-string 1 path)
  6292. subject (org-link-escape (match-string 2 path))))
  6293. (while args
  6294. (cond
  6295. ((not (stringp (car args))) (push (pop args) args1))
  6296. (t (setq a (pop args))
  6297. (if (string-match "%a" a)
  6298. (setq a (replace-match address t t a)))
  6299. (if (string-match "%s" a)
  6300. (setq a (replace-match subject t t a)))
  6301. (push a args1))))
  6302. (apply cmd (nreverse args1))))
  6303. ((member type '("http" "https" "ftp" "news"))
  6304. (browse-url (concat type ":" (org-link-escape
  6305. path org-link-escape-chars-browser))))
  6306. ((member type '("message"))
  6307. (browse-url (concat type ":" path)))
  6308. ((string= type "tags")
  6309. (org-tags-view in-emacs path))
  6310. ((string= type "thisfile")
  6311. (if in-emacs
  6312. (switch-to-buffer-other-window
  6313. (org-get-buffer-for-internal-link (current-buffer)))
  6314. (org-mark-ring-push))
  6315. (let ((cmd `(org-link-search
  6316. ,path
  6317. ,(cond ((equal in-emacs '(4)) 'occur)
  6318. ((equal in-emacs '(16)) 'org-occur)
  6319. (t nil))
  6320. ,pos)))
  6321. (condition-case nil (eval cmd)
  6322. (error (progn (widen) (eval cmd))))))
  6323. ((string= type "tree-match")
  6324. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6325. ((string= type "file")
  6326. (if (string-match "::\\([0-9]+\\)\\'" path)
  6327. (setq line (string-to-number (match-string 1 path))
  6328. path (substring path 0 (match-beginning 0)))
  6329. (if (string-match "::\\(.+\\)\\'" path)
  6330. (setq search (match-string 1 path)
  6331. path (substring path 0 (match-beginning 0)))))
  6332. (if (string-match "[*?{]" (file-name-nondirectory path))
  6333. (dired path)
  6334. (org-open-file path in-emacs line search)))
  6335. ((string= type "news")
  6336. (require 'org-gnus)
  6337. (org-gnus-follow-link path))
  6338. ((string= type "shell")
  6339. (let ((cmd path))
  6340. (if (or (not org-confirm-shell-link-function)
  6341. (funcall org-confirm-shell-link-function
  6342. (format "Execute \"%s\" in shell? "
  6343. (org-add-props cmd nil
  6344. 'face 'org-warning))))
  6345. (progn
  6346. (message "Executing %s" cmd)
  6347. (shell-command cmd))
  6348. (error "Abort"))))
  6349. ((string= type "elisp")
  6350. (let ((cmd path))
  6351. (if (or (not org-confirm-elisp-link-function)
  6352. (funcall org-confirm-elisp-link-function
  6353. (format "Execute \"%s\" as elisp? "
  6354. (org-add-props cmd nil
  6355. 'face 'org-warning))))
  6356. (message "%s => %s" cmd
  6357. (if (equal (string-to-char cmd) ?\()
  6358. (eval (read cmd))
  6359. (call-interactively (read cmd))))
  6360. (error "Abort"))))
  6361. (t
  6362. (browse-url-at-point)))))
  6363. (move-marker org-open-link-marker nil)
  6364. (run-hook-with-args 'org-follow-link-hook))
  6365. ;;;; Time estimates
  6366. (defun org-get-effort (&optional pom)
  6367. "Get the effort estimate for the current entry."
  6368. (org-entry-get pom org-effort-property))
  6369. ;;; File search
  6370. (defvar org-create-file-search-functions nil
  6371. "List of functions to construct the right search string for a file link.
  6372. These functions are called in turn with point at the location to
  6373. which the link should point.
  6374. A function in the hook should first test if it would like to
  6375. handle this file type, for example by checking the major-mode or
  6376. the file extension. If it decides not to handle this file, it
  6377. should just return nil to give other functions a chance. If it
  6378. does handle the file, it must return the search string to be used
  6379. when following the link. The search string will be part of the
  6380. file link, given after a double colon, and `org-open-at-point'
  6381. will automatically search for it. If special measures must be
  6382. taken to make the search successful, another function should be
  6383. added to the companion hook `org-execute-file-search-functions',
  6384. which see.
  6385. A function in this hook may also use `setq' to set the variable
  6386. `description' to provide a suggestion for the descriptive text to
  6387. be used for this link when it gets inserted into an Org-mode
  6388. buffer with \\[org-insert-link].")
  6389. (defvar org-execute-file-search-functions nil
  6390. "List of functions to execute a file search triggered by a link.
  6391. Functions added to this hook must accept a single argument, the
  6392. search string that was part of the file link, the part after the
  6393. double colon. The function must first check if it would like to
  6394. handle this search, for example by checking the major-mode or the
  6395. file extension. If it decides not to handle this search, it
  6396. should just return nil to give other functions a chance. If it
  6397. does handle the search, it must return a non-nil value to keep
  6398. other functions from trying.
  6399. Each function can access the current prefix argument through the
  6400. variable `current-prefix-argument'. Note that a single prefix is
  6401. used to force opening a link in Emacs, so it may be good to only
  6402. use a numeric or double prefix to guide the search function.
  6403. In case this is needed, a function in this hook can also restore
  6404. the window configuration before `org-open-at-point' was called using:
  6405. (set-window-configuration org-window-config-before-follow-link)")
  6406. (defun org-link-search (s &optional type avoid-pos)
  6407. "Search for a link search option.
  6408. If S is surrounded by forward slashes, it is interpreted as a
  6409. regular expression. In org-mode files, this will create an `org-occur'
  6410. sparse tree. In ordinary files, `occur' will be used to list matches.
  6411. If the current buffer is in `dired-mode', grep will be used to search
  6412. in all files. If AVOID-POS is given, ignore matches near that position."
  6413. (let ((case-fold-search t)
  6414. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6415. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6416. (append '(("") (" ") ("\t") ("\n"))
  6417. org-emphasis-alist)
  6418. "\\|") "\\)"))
  6419. (pos (point))
  6420. (pre nil) (post nil)
  6421. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6422. (cond
  6423. ;; First check if there are any special
  6424. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6425. ;; Now try the builtin stuff
  6426. ((save-excursion
  6427. (goto-char (point-min))
  6428. (and
  6429. (re-search-forward
  6430. (concat "<<" (regexp-quote s0) ">>") nil t)
  6431. (setq type 'dedicated
  6432. pos (match-beginning 0))))
  6433. ;; There is an exact target for this
  6434. (goto-char pos))
  6435. ((and (string-match "^((.*))$" s0)
  6436. (save-excursion
  6437. (goto-char (point-min))
  6438. (and
  6439. (re-search-forward (concat "[^[]" (regexp-quote s0)) nil t)
  6440. (setq type 'dedicated
  6441. pos (1+ (match-beginning 0))))))
  6442. ;; There is a coderef target for this
  6443. (goto-char pos))
  6444. ((string-match "^/\\(.*\\)/$" s)
  6445. ;; A regular expression
  6446. (cond
  6447. ((org-mode-p)
  6448. (org-occur (match-string 1 s)))
  6449. ;;((eq major-mode 'dired-mode)
  6450. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6451. (t (org-do-occur (match-string 1 s)))))
  6452. (t
  6453. ;; A normal search strings
  6454. (when (equal (string-to-char s) ?*)
  6455. ;; Anchor on headlines, post may include tags.
  6456. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6457. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6458. s (substring s 1)))
  6459. (remove-text-properties
  6460. 0 (length s)
  6461. '(face nil mouse-face nil keymap nil fontified nil) s)
  6462. ;; Make a series of regular expressions to find a match
  6463. (setq words (org-split-string s "[ \n\r\t]+")
  6464. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6465. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6466. "\\)" markers)
  6467. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6468. re2a (concat "[ \t\r\n]" re2a_)
  6469. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6470. re4 (concat "[^a-zA-Z_]" re4_)
  6471. re1 (concat pre re2 post)
  6472. re3 (concat pre (if pre re4_ re4) post)
  6473. re5 (concat pre ".*" re4)
  6474. re2 (concat pre re2)
  6475. re2a (concat pre (if pre re2a_ re2a))
  6476. re4 (concat pre (if pre re4_ re4))
  6477. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6478. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6479. re5 "\\)"
  6480. ))
  6481. (cond
  6482. ((eq type 'org-occur) (org-occur reall))
  6483. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6484. (t (goto-char (point-min))
  6485. (setq type 'fuzzy)
  6486. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6487. (org-search-not-self 1 re1 nil t)
  6488. (org-search-not-self 1 re2 nil t)
  6489. (org-search-not-self 1 re2a nil t)
  6490. (org-search-not-self 1 re3 nil t)
  6491. (org-search-not-self 1 re4 nil t)
  6492. (org-search-not-self 1 re5 nil t)
  6493. )
  6494. (goto-char (match-beginning 1))
  6495. (goto-char pos)
  6496. (error "No match")))))
  6497. (t
  6498. ;; Normal string-search
  6499. (goto-char (point-min))
  6500. (if (search-forward s nil t)
  6501. (goto-char (match-beginning 0))
  6502. (error "No match"))))
  6503. (and (org-mode-p) (org-show-context 'link-search))
  6504. type))
  6505. (defun org-search-not-self (group &rest args)
  6506. "Execute `re-search-forward', but only accept matches that do not
  6507. enclose the position of `org-open-link-marker'."
  6508. (let ((m org-open-link-marker))
  6509. (catch 'exit
  6510. (while (apply 're-search-forward args)
  6511. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6512. (goto-char (match-end group))
  6513. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6514. (> (match-beginning 0) (marker-position m))
  6515. (< (match-end 0) (marker-position m)))
  6516. (save-match-data
  6517. (or (not (org-in-regexp
  6518. org-bracket-link-analytic-regexp 1))
  6519. (not (match-end 4)) ; no description
  6520. (and (<= (match-beginning 4) (point))
  6521. (>= (match-end 4) (point))))))
  6522. (throw 'exit (point))))))))
  6523. (defun org-get-buffer-for-internal-link (buffer)
  6524. "Return a buffer to be used for displaying the link target of internal links."
  6525. (cond
  6526. ((not org-display-internal-link-with-indirect-buffer)
  6527. buffer)
  6528. ((string-match "(Clone)$" (buffer-name buffer))
  6529. (message "Buffer is already a clone, not making another one")
  6530. ;; we also do not modify visibility in this case
  6531. buffer)
  6532. (t ; make a new indirect buffer for displaying the link
  6533. (let* ((bn (buffer-name buffer))
  6534. (ibn (concat bn "(Clone)"))
  6535. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6536. (with-current-buffer ib (org-overview))
  6537. ib))))
  6538. (defun org-do-occur (regexp &optional cleanup)
  6539. "Call the Emacs command `occur'.
  6540. If CLEANUP is non-nil, remove the printout of the regular expression
  6541. in the *Occur* buffer. This is useful if the regex is long and not useful
  6542. to read."
  6543. (occur regexp)
  6544. (when cleanup
  6545. (let ((cwin (selected-window)) win beg end)
  6546. (when (setq win (get-buffer-window "*Occur*"))
  6547. (select-window win))
  6548. (goto-char (point-min))
  6549. (when (re-search-forward "match[a-z]+" nil t)
  6550. (setq beg (match-end 0))
  6551. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6552. (setq end (1- (match-beginning 0)))))
  6553. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6554. (goto-char (point-min))
  6555. (select-window cwin))))
  6556. ;;; The mark ring for links jumps
  6557. (defvar org-mark-ring nil
  6558. "Mark ring for positions before jumps in Org-mode.")
  6559. (defvar org-mark-ring-last-goto nil
  6560. "Last position in the mark ring used to go back.")
  6561. ;; Fill and close the ring
  6562. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6563. (loop for i from 1 to org-mark-ring-length do
  6564. (push (make-marker) org-mark-ring))
  6565. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6566. org-mark-ring)
  6567. (defun org-mark-ring-push (&optional pos buffer)
  6568. "Put the current position or POS into the mark ring and rotate it."
  6569. (interactive)
  6570. (setq pos (or pos (point)))
  6571. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6572. (move-marker (car org-mark-ring)
  6573. (or pos (point))
  6574. (or buffer (current-buffer)))
  6575. (message "%s"
  6576. (substitute-command-keys
  6577. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6578. (defun org-mark-ring-goto (&optional n)
  6579. "Jump to the previous position in the mark ring.
  6580. With prefix arg N, jump back that many stored positions. When
  6581. called several times in succession, walk through the entire ring.
  6582. Org-mode commands jumping to a different position in the current file,
  6583. or to another Org-mode file, automatically push the old position
  6584. onto the ring."
  6585. (interactive "p")
  6586. (let (p m)
  6587. (if (eq last-command this-command)
  6588. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6589. (setq p org-mark-ring))
  6590. (setq org-mark-ring-last-goto p)
  6591. (setq m (car p))
  6592. (switch-to-buffer (marker-buffer m))
  6593. (goto-char m)
  6594. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6595. (defun org-remove-angle-brackets (s)
  6596. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6597. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6598. s)
  6599. (defun org-add-angle-brackets (s)
  6600. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6601. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6602. s)
  6603. (defun org-remove-double-quotes (s)
  6604. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6605. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6606. s)
  6607. ;;; Following specific links
  6608. (defun org-follow-timestamp-link ()
  6609. (cond
  6610. ((org-at-date-range-p t)
  6611. (let ((org-agenda-start-on-weekday)
  6612. (t1 (match-string 1))
  6613. (t2 (match-string 2)))
  6614. (setq t1 (time-to-days (org-time-string-to-time t1))
  6615. t2 (time-to-days (org-time-string-to-time t2)))
  6616. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6617. ((org-at-timestamp-p t)
  6618. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6619. (substring (match-string 1) 0 10)))
  6620. 1))
  6621. (t (error "This should not happen"))))
  6622. ;;; Following file links
  6623. (defvar org-wait nil)
  6624. (defun org-open-file (path &optional in-emacs line search)
  6625. "Open the file at PATH.
  6626. First, this expands any special file name abbreviations. Then the
  6627. configuration variable `org-file-apps' is checked if it contains an
  6628. entry for this file type, and if yes, the corresponding command is launched.
  6629. If no application is found, Emacs simply visits the file.
  6630. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6631. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6632. and o use an external application to visit the file.
  6633. Optional LINE specifies a line to go to, optional SEARCH a string to
  6634. search for. If LINE or SEARCH is given, the file will always be
  6635. opened in Emacs.
  6636. If the file does not exist, an error is thrown."
  6637. (setq in-emacs (or in-emacs line search))
  6638. (let* ((file (if (equal path "")
  6639. buffer-file-name
  6640. (substitute-in-file-name (expand-file-name path))))
  6641. (apps (append org-file-apps (org-default-apps)))
  6642. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6643. (dirp (if remp nil (file-directory-p file)))
  6644. (file (if (and dirp org-open-directory-means-index-dot-org)
  6645. (concat (file-name-as-directory file) "index.org")
  6646. file))
  6647. (a-m-a-p (assq 'auto-mode apps))
  6648. (dfile (downcase file))
  6649. (old-buffer (current-buffer))
  6650. (old-pos (point))
  6651. (old-mode major-mode)
  6652. ext cmd)
  6653. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6654. (setq ext (match-string 1 dfile))
  6655. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6656. (setq ext (match-string 1 dfile))))
  6657. (cond
  6658. ((equal in-emacs '(16))
  6659. (setq cmd (cdr (assoc 'system apps))))
  6660. (in-emacs (setq cmd 'emacs))
  6661. (t
  6662. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6663. (and dirp (cdr (assoc 'directory apps)))
  6664. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6665. 'string-match)
  6666. (cdr (assoc ext apps))
  6667. (cdr (assoc t apps))))))
  6668. (when (eq cmd 'system)
  6669. (setq cmd (cdr (assoc 'system apps))))
  6670. (when (eq cmd 'default)
  6671. (setq cmd (cdr (assoc t apps))))
  6672. (when (eq cmd 'mailcap)
  6673. (require 'mailcap)
  6674. (mailcap-parse-mailcaps)
  6675. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6676. (command (mailcap-mime-info mime-type)))
  6677. (if (stringp command)
  6678. (setq cmd command)
  6679. (setq cmd 'emacs))))
  6680. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6681. (not (file-exists-p file))
  6682. (not org-open-non-existing-files))
  6683. (error "No such file: %s" file))
  6684. (cond
  6685. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6686. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6687. (while (string-match "['\"]%s['\"]" cmd)
  6688. (setq cmd (replace-match "%s" t t cmd)))
  6689. (while (string-match "%s" cmd)
  6690. (setq cmd (replace-match
  6691. (save-match-data
  6692. (shell-quote-argument
  6693. (convert-standard-filename file)))
  6694. t t cmd)))
  6695. (save-window-excursion
  6696. (start-process-shell-command cmd nil cmd)
  6697. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6698. ))
  6699. ((or (stringp cmd)
  6700. (eq cmd 'emacs))
  6701. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6702. (widen)
  6703. (if line (goto-line line)
  6704. (if search (org-link-search search))))
  6705. ((consp cmd)
  6706. (let ((file (convert-standard-filename file)))
  6707. (eval cmd)))
  6708. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6709. (and (org-mode-p) (eq old-mode 'org-mode)
  6710. (or (not (equal old-buffer (current-buffer)))
  6711. (not (equal old-pos (point))))
  6712. (org-mark-ring-push old-pos old-buffer))))
  6713. (defun org-default-apps ()
  6714. "Return the default applications for this operating system."
  6715. (cond
  6716. ((eq system-type 'darwin)
  6717. org-file-apps-defaults-macosx)
  6718. ((eq system-type 'windows-nt)
  6719. org-file-apps-defaults-windowsnt)
  6720. (t org-file-apps-defaults-gnu)))
  6721. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6722. "Convert extensions to regular expressions in the cars of LIST.
  6723. Also, weed out any non-string entries, because the return value is used
  6724. only for regexp matching.
  6725. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6726. point to the symbol `emacs', indicating that the file should
  6727. be opened in Emacs."
  6728. (append
  6729. (delq nil
  6730. (mapcar (lambda (x)
  6731. (if (not (stringp (car x)))
  6732. nil
  6733. (if (string-match "\\W" (car x))
  6734. x
  6735. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6736. list))
  6737. (if add-auto-mode
  6738. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6739. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6740. (defun org-file-remote-p (file)
  6741. "Test whether FILE specifies a location on a remote system.
  6742. Return non-nil if the location is indeed remote.
  6743. For example, the filename \"/user@host:/foo\" specifies a location
  6744. on the system \"/user@host:\"."
  6745. (cond ((fboundp 'file-remote-p)
  6746. (file-remote-p file))
  6747. ((fboundp 'tramp-handle-file-remote-p)
  6748. (tramp-handle-file-remote-p file))
  6749. ((and (boundp 'ange-ftp-name-format)
  6750. (string-match (car ange-ftp-name-format) file))
  6751. t)
  6752. (t nil)))
  6753. ;;;; Refiling
  6754. (defun org-get-org-file ()
  6755. "Read a filename, with default directory `org-directory'."
  6756. (let ((default (or org-default-notes-file remember-data-file)))
  6757. (read-file-name (format "File name [%s]: " default)
  6758. (file-name-as-directory org-directory)
  6759. default)))
  6760. (defun org-notes-order-reversed-p ()
  6761. "Check if the current file should receive notes in reversed order."
  6762. (cond
  6763. ((not org-reverse-note-order) nil)
  6764. ((eq t org-reverse-note-order) t)
  6765. ((not (listp org-reverse-note-order)) nil)
  6766. (t (catch 'exit
  6767. (let ((all org-reverse-note-order)
  6768. entry)
  6769. (while (setq entry (pop all))
  6770. (if (string-match (car entry) buffer-file-name)
  6771. (throw 'exit (cdr entry))))
  6772. nil)))))
  6773. (defvar org-refile-target-table nil
  6774. "The list of refile targets, created by `org-refile'.")
  6775. (defvar org-agenda-new-buffers nil
  6776. "Buffers created to visit agenda files.")
  6777. (defun org-get-refile-targets (&optional default-buffer)
  6778. "Produce a table with refile targets."
  6779. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6780. targets txt re files f desc descre fast-path-p level)
  6781. (message "Getting targets...")
  6782. (with-current-buffer (or default-buffer (current-buffer))
  6783. (while (setq entry (pop entries))
  6784. (setq files (car entry) desc (cdr entry))
  6785. (setq fast-path-p nil)
  6786. (cond
  6787. ((null files) (setq files (list (current-buffer))))
  6788. ((eq files 'org-agenda-files)
  6789. (setq files (org-agenda-files 'unrestricted)))
  6790. ((and (symbolp files) (fboundp files))
  6791. (setq files (funcall files)))
  6792. ((and (symbolp files) (boundp files))
  6793. (setq files (symbol-value files))))
  6794. (if (stringp files) (setq files (list files)))
  6795. (cond
  6796. ((eq (car desc) :tag)
  6797. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6798. ((eq (car desc) :todo)
  6799. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6800. ((eq (car desc) :regexp)
  6801. (setq descre (cdr desc)))
  6802. ((eq (car desc) :level)
  6803. (setq descre (concat "^\\*\\{" (number-to-string
  6804. (if org-odd-levels-only
  6805. (1- (* 2 (cdr desc)))
  6806. (cdr desc)))
  6807. "\\}[ \t]")))
  6808. ((eq (car desc) :maxlevel)
  6809. (setq fast-path-p t)
  6810. (setq descre (concat "^\\*\\{1," (number-to-string
  6811. (if org-odd-levels-only
  6812. (1- (* 2 (cdr desc)))
  6813. (cdr desc)))
  6814. "\\}[ \t]")))
  6815. (t (error "Bad refiling target description %s" desc)))
  6816. (while (setq f (pop files))
  6817. (save-excursion
  6818. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6819. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6820. (setq f (expand-file-name f))
  6821. (save-excursion
  6822. (save-restriction
  6823. (widen)
  6824. (goto-char (point-min))
  6825. (while (re-search-forward descre nil t)
  6826. (goto-char (point-at-bol))
  6827. (when (looking-at org-complex-heading-regexp)
  6828. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  6829. txt (org-link-display-format (match-string 4))
  6830. re (concat "^" (regexp-quote
  6831. (buffer-substring (match-beginning 1)
  6832. (match-end 4)))))
  6833. (if (match-end 5) (setq re (concat re "[ \t]+"
  6834. (regexp-quote
  6835. (match-string 5)))))
  6836. (setq re (concat re "[ \t]*$"))
  6837. (when org-refile-use-outline-path
  6838. (setq txt (mapconcat 'org-protect-slash
  6839. (append
  6840. (if (eq org-refile-use-outline-path 'file)
  6841. (list (file-name-nondirectory
  6842. (buffer-file-name (buffer-base-buffer))))
  6843. (if (eq org-refile-use-outline-path 'full-file-path)
  6844. (list (buffer-file-name (buffer-base-buffer)))))
  6845. (org-get-outline-path fast-path-p level txt)
  6846. (list txt))
  6847. "/")))
  6848. (push (list txt f re (point)) targets))
  6849. (goto-char (point-at-eol))))))))
  6850. (message "Getting targets...done")
  6851. (nreverse targets))))
  6852. (defun org-protect-slash (s)
  6853. (while (string-match "/" s)
  6854. (setq s (replace-match "\\" t t s)))
  6855. s)
  6856. (defvar org-olpa (make-vector 20 nil))
  6857. (defun org-get-outline-path (&optional fastp level heading)
  6858. "Return the outline path to the current entry, as a list."
  6859. (if fastp
  6860. (progn
  6861. (if (> level 19)
  6862. (error "Outline path failure, more than 19 levels."))
  6863. (loop for i from level upto 19 do
  6864. (aset org-olpa i nil))
  6865. (prog1
  6866. (delq nil (append org-olpa nil))
  6867. (aset org-olpa level heading)))
  6868. (let (rtn)
  6869. (save-excursion
  6870. (while (org-up-heading-safe)
  6871. (when (looking-at org-complex-heading-regexp)
  6872. (push (org-match-string-no-properties 4) rtn)))
  6873. rtn))))
  6874. (defvar org-refile-history nil
  6875. "History for refiling operations.")
  6876. (defun org-refile (&optional goto default-buffer)
  6877. "Move the entry at point to another heading.
  6878. The list of target headings is compiled using the information in
  6879. `org-refile-targets', which see. This list is created before each use
  6880. and will therefore always be up-to-date.
  6881. At the target location, the entry is filed as a subitem of the target heading.
  6882. Depending on `org-reverse-note-order', the new subitem will either be the
  6883. first or the last subitem.
  6884. If there is an active region, all entries in that region will be moved.
  6885. However, the region must fulfil the requirement that the first heading
  6886. is the first one sets the top-level of the moved text - at most siblings
  6887. below it are allowed.
  6888. With prefix arg GOTO, the command will only visit the target location,
  6889. not actually move anything.
  6890. With a double prefix `C-u C-u', go to the location where the last refiling
  6891. operation has put the subtree."
  6892. (interactive "P")
  6893. (let* ((cbuf (current-buffer))
  6894. (regionp (org-region-active-p))
  6895. (region-start (and regionp (region-beginning)))
  6896. (region-end (and regionp (region-end)))
  6897. (region-length (and regionp (- region-end region-start)))
  6898. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6899. pos it nbuf file re level reversed)
  6900. (when regionp (goto-char region-start)
  6901. (unless (org-kill-is-subtree-p
  6902. (buffer-substring region-start region-end))
  6903. (error "The region is not a (sequence of) subtree(s)")))
  6904. (if (equal goto '(16))
  6905. (org-refile-goto-last-stored)
  6906. (when (setq it (org-refile-get-location
  6907. (if goto "Goto: " "Refile to: ") default-buffer))
  6908. (setq file (nth 1 it)
  6909. re (nth 2 it)
  6910. pos (nth 3 it))
  6911. (if (and (equal (buffer-file-name) file)
  6912. (if regionp
  6913. (and (>= pos region-start)
  6914. (<= pos region-end))
  6915. (and (>= pos (point))
  6916. (< pos (save-excursion
  6917. (org-end-of-subtree t t))))))
  6918. (error "Cannot refile to position inside the tree or region"))
  6919. (setq nbuf (or (find-buffer-visiting file)
  6920. (find-file-noselect file)))
  6921. (if goto
  6922. (progn
  6923. (switch-to-buffer nbuf)
  6924. (goto-char pos)
  6925. (org-show-context 'org-goto))
  6926. (if regionp
  6927. (progn
  6928. (kill-new (buffer-substring region-start region-end))
  6929. (org-save-markers-in-region region-start region-end))
  6930. (org-copy-subtree 1 nil t))
  6931. (save-excursion
  6932. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6933. (find-file-noselect file))))
  6934. (setq reversed (org-notes-order-reversed-p))
  6935. (save-excursion
  6936. (save-restriction
  6937. (widen)
  6938. (goto-char pos)
  6939. (looking-at outline-regexp)
  6940. (setq level (org-get-valid-level (funcall outline-level) 1))
  6941. (goto-char
  6942. (if reversed
  6943. (or (outline-next-heading) (point-max))
  6944. (or (save-excursion (outline-get-next-sibling))
  6945. (org-end-of-subtree t t)
  6946. (point-max))))
  6947. (if (not (bolp)) (newline))
  6948. (bookmark-set "org-refile-last-stored")
  6949. (org-paste-subtree level))))
  6950. (if regionp
  6951. (delete-region (point) (+ (point) region-length))
  6952. (org-cut-subtree))
  6953. (setq org-markers-to-move nil)
  6954. (message "Refiled to \"%s\"" (car it)))))))
  6955. (defun org-refile-goto-last-stored ()
  6956. "Go to the location where the last refile was stored."
  6957. (interactive)
  6958. (bookmark-jump "org-refile-last-stored")
  6959. (message "This is the location of the last refile"))
  6960. (defun org-refile-get-location (&optional prompt default-buffer)
  6961. "Prompt the user for a refile location, using PROMPT."
  6962. (let ((org-refile-targets org-refile-targets)
  6963. (org-refile-use-outline-path org-refile-use-outline-path))
  6964. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6965. (unless org-refile-target-table
  6966. (error "No refile targets"))
  6967. (let* ((cbuf (current-buffer))
  6968. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  6969. (cfunc (if (and org-refile-use-outline-path
  6970. org-outline-path-complete-in-steps)
  6971. 'org-olpath-completing-read
  6972. 'org-ido-completing-read))
  6973. (extra (if org-refile-use-outline-path "/" ""))
  6974. (filename (and cfn (expand-file-name cfn)))
  6975. (tbl (mapcar
  6976. (lambda (x)
  6977. (if (not (equal filename (nth 1 x)))
  6978. (cons (concat (car x) extra " ("
  6979. (file-name-nondirectory (nth 1 x)) ")")
  6980. (cdr x))
  6981. (cons (concat (car x) extra) (cdr x))))
  6982. org-refile-target-table))
  6983. (completion-ignore-case t))
  6984. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6985. tbl)))
  6986. (defun org-olpath-completing-read (prompt collection &rest args)
  6987. "Read an outline path like a file name."
  6988. (let ((thetable collection))
  6989. (apply
  6990. 'org-ido-completing-read prompt
  6991. (lambda (string predicate &optional flag)
  6992. (let (rtn r s f (l (length string)))
  6993. (cond
  6994. ((eq flag nil)
  6995. ;; try completion
  6996. (try-completion string thetable))
  6997. ((eq flag t)
  6998. ;; all-completions
  6999. (setq rtn (all-completions string thetable predicate))
  7000. (mapcar
  7001. (lambda (x)
  7002. (setq r (substring x l))
  7003. (if (string-match " ([^)]*)$" x)
  7004. (setq f (match-string 0 x))
  7005. (setq f ""))
  7006. (if (string-match "/" r)
  7007. (concat string (substring r 0 (match-end 0)) f)
  7008. x))
  7009. rtn))
  7010. ((eq flag 'lambda)
  7011. ;; exact match?
  7012. (assoc string thetable)))
  7013. ))
  7014. args)))
  7015. ;;;; Dynamic blocks
  7016. (defun org-find-dblock (name)
  7017. "Find the first dynamic block with name NAME in the buffer.
  7018. If not found, stay at current position and return nil."
  7019. (let (pos)
  7020. (save-excursion
  7021. (goto-char (point-min))
  7022. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7023. nil t)
  7024. (match-beginning 0))))
  7025. (if pos (goto-char pos))
  7026. pos))
  7027. (defconst org-dblock-start-re
  7028. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7029. "Matches the startline of a dynamic block, with parameters.")
  7030. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7031. "Matches the end of a dynamic block.")
  7032. (defun org-create-dblock (plist)
  7033. "Create a dynamic block section, with parameters taken from PLIST.
  7034. PLIST must contain a :name entry which is used as name of the block."
  7035. (unless (bolp) (newline))
  7036. (let ((name (plist-get plist :name)))
  7037. (insert "#+BEGIN: " name)
  7038. (while plist
  7039. (if (eq (car plist) :name)
  7040. (setq plist (cddr plist))
  7041. (insert " " (prin1-to-string (pop plist)))))
  7042. (insert "\n\n#+END:\n")
  7043. (beginning-of-line -2)))
  7044. (defun org-prepare-dblock ()
  7045. "Prepare dynamic block for refresh.
  7046. This empties the block, puts the cursor at the insert position and returns
  7047. the property list including an extra property :name with the block name."
  7048. (unless (looking-at org-dblock-start-re)
  7049. (error "Not at a dynamic block"))
  7050. (let* ((begdel (1+ (match-end 0)))
  7051. (name (org-no-properties (match-string 1)))
  7052. (params (append (list :name name)
  7053. (read (concat "(" (match-string 3) ")")))))
  7054. (unless (re-search-forward org-dblock-end-re nil t)
  7055. (error "Dynamic block not terminated"))
  7056. (setq params
  7057. (append params
  7058. (list :content (buffer-substring
  7059. begdel (match-beginning 0)))))
  7060. (delete-region begdel (match-beginning 0))
  7061. (goto-char begdel)
  7062. (open-line 1)
  7063. params))
  7064. (defun org-map-dblocks (&optional command)
  7065. "Apply COMMAND to all dynamic blocks in the current buffer.
  7066. If COMMAND is not given, use `org-update-dblock'."
  7067. (let ((cmd (or command 'org-update-dblock))
  7068. pos)
  7069. (save-excursion
  7070. (goto-char (point-min))
  7071. (while (re-search-forward org-dblock-start-re nil t)
  7072. (goto-char (setq pos (match-beginning 0)))
  7073. (condition-case nil
  7074. (funcall cmd)
  7075. (error (message "Error during update of dynamic block")))
  7076. (goto-char pos)
  7077. (unless (re-search-forward org-dblock-end-re nil t)
  7078. (error "Dynamic block not terminated"))))))
  7079. (defun org-dblock-update (&optional arg)
  7080. "User command for updating dynamic blocks.
  7081. Update the dynamic block at point. With prefix ARG, update all dynamic
  7082. blocks in the buffer."
  7083. (interactive "P")
  7084. (if arg
  7085. (org-update-all-dblocks)
  7086. (or (looking-at org-dblock-start-re)
  7087. (org-beginning-of-dblock))
  7088. (org-update-dblock)))
  7089. (defun org-update-dblock ()
  7090. "Update the dynamic block at point
  7091. This means to empty the block, parse for parameters and then call
  7092. the correct writing function."
  7093. (save-window-excursion
  7094. (let* ((pos (point))
  7095. (line (org-current-line))
  7096. (params (org-prepare-dblock))
  7097. (name (plist-get params :name))
  7098. (cmd (intern (concat "org-dblock-write:" name))))
  7099. (message "Updating dynamic block `%s' at line %d..." name line)
  7100. (funcall cmd params)
  7101. (message "Updating dynamic block `%s' at line %d...done" name line)
  7102. (goto-char pos))))
  7103. (defun org-beginning-of-dblock ()
  7104. "Find the beginning of the dynamic block at point.
  7105. Error if there is no such block at point."
  7106. (let ((pos (point))
  7107. beg)
  7108. (end-of-line 1)
  7109. (if (and (re-search-backward org-dblock-start-re nil t)
  7110. (setq beg (match-beginning 0))
  7111. (re-search-forward org-dblock-end-re nil t)
  7112. (> (match-end 0) pos))
  7113. (goto-char beg)
  7114. (goto-char pos)
  7115. (error "Not in a dynamic block"))))
  7116. (defun org-update-all-dblocks ()
  7117. "Update all dynamic blocks in the buffer.
  7118. This function can be used in a hook."
  7119. (when (org-mode-p)
  7120. (org-map-dblocks 'org-update-dblock)))
  7121. ;;;; Completion
  7122. (defconst org-additional-option-like-keywords
  7123. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7124. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7125. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7126. "BEGIN_QUOTE" "END_QUOTE"
  7127. "BEGIN_VERSE" "END_VERSE"
  7128. "BEGIN_SRC" "END_SRC"
  7129. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7130. (defcustom org-structure-template-alist
  7131. '(
  7132. ("s" "#+begin_src ?\n\n#+end_src"
  7133. "<src lang=\"?\">\n\n</src>")
  7134. ("e" "#+begin_example\n?\n#+end_example"
  7135. "<example>\n?\n</example>")
  7136. ("q" "#+begin_quote\n?\n#+end_quote"
  7137. "<quote>\n?\n</quote>")
  7138. ("v" "#+begin_verse\n?\n#+end_verse"
  7139. "<verse>\n?\n/verse>")
  7140. ("l" "#+begin_latex\n?\n#+end_latex"
  7141. "<literal style=\"latex\">\n?\n</literal>")
  7142. ("L" "#+latex: "
  7143. "<literal style=\"latex\">?</literal>")
  7144. ("h" "#+begin_html\n?\n#+end_html"
  7145. "<literal style=\"html\">\n?\n</literal>")
  7146. ("H" "#+html: "
  7147. "<literal style=\"html\">?</literal>")
  7148. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7149. ("A" "#+ascii: ")
  7150. ("i" "#+include %file ?"
  7151. "<include file=%file markup=\"?\">")
  7152. )
  7153. "Structure completion elements.
  7154. This is a list of abbreviation keys and values. The value gets inserted
  7155. it you type @samp{.} followed by the key and then the completion key,
  7156. usually `M-TAB'. %file will be replaced by a file name after prompting
  7157. for the file using completion.
  7158. There are two templates for each key, the first uses the original Org syntax,
  7159. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7160. the default when the /org-mtags.el/ module has been loaded. See also the
  7161. variable `org-mtags-prefer-muse-templates'.
  7162. This is an experimental feature, it is undecided if it is going to stay in."
  7163. :group 'org-completion
  7164. :type '(repeat
  7165. (string :tag "Key")
  7166. (string :tag "Template")
  7167. (string :tag "Muse Template")))
  7168. (defun org-try-structure-completion ()
  7169. "Try to complete a structure template before point.
  7170. This looks for strings like \"<e\" on an otherwise empty line and
  7171. expands them."
  7172. (let ((l (buffer-substring (point-at-bol) (point)))
  7173. a)
  7174. (when (and (looking-at "[ \t]*$")
  7175. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7176. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7177. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7178. (match-beginning 1)) a)
  7179. t)))
  7180. (defun org-complete-expand-structure-template (start cell)
  7181. "Expand a structure template."
  7182. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7183. (rpl (nth (if musep 2 1) cell)))
  7184. (delete-region start (point))
  7185. (when (string-match "\\`#\\+" rpl)
  7186. (cond
  7187. ((bolp))
  7188. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7189. (delete-region (point-at-bol) (point)))
  7190. (t (newline))))
  7191. (setq start (point))
  7192. (if (string-match "%file" rpl)
  7193. (setq rpl (replace-match
  7194. (concat
  7195. "\""
  7196. (save-match-data
  7197. (abbreviate-file-name (read-file-name "Include file: ")))
  7198. "\"")
  7199. t t rpl)))
  7200. (insert rpl)
  7201. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7202. (defun org-complete (&optional arg)
  7203. "Perform completion on word at point.
  7204. At the beginning of a headline, this completes TODO keywords as given in
  7205. `org-todo-keywords'.
  7206. If the current word is preceded by a backslash, completes the TeX symbols
  7207. that are supported for HTML support.
  7208. If the current word is preceded by \"#+\", completes special words for
  7209. setting file options.
  7210. In the line after \"#+STARTUP:, complete valid keywords.\"
  7211. At all other locations, this simply calls the value of
  7212. `org-completion-fallback-command'."
  7213. (interactive "P")
  7214. (org-without-partial-completion
  7215. (catch 'exit
  7216. (let* ((a nil)
  7217. (end (point))
  7218. (beg1 (save-excursion
  7219. (skip-chars-backward (org-re "[:alnum:]_@"))
  7220. (point)))
  7221. (beg (save-excursion
  7222. (skip-chars-backward "a-zA-Z0-9_:$")
  7223. (point)))
  7224. (confirm (lambda (x) (stringp (car x))))
  7225. (searchhead (equal (char-before beg) ?*))
  7226. (struct
  7227. (when (and (member (char-before beg1) '(?. ?<))
  7228. (setq a (assoc (buffer-substring beg1 (point))
  7229. org-structure-template-alist)))
  7230. (org-complete-expand-structure-template (1- beg1) a)
  7231. (throw 'exit t)))
  7232. (tag (and (equal (char-before beg1) ?:)
  7233. (equal (char-after (point-at-bol)) ?*)))
  7234. (prop (and (equal (char-before beg1) ?:)
  7235. (not (equal (char-after (point-at-bol)) ?*))))
  7236. (texp (equal (char-before beg) ?\\))
  7237. (link (equal (char-before beg) ?\[))
  7238. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7239. beg)
  7240. "#+"))
  7241. (startup (string-match "^#\\+STARTUP:.*"
  7242. (buffer-substring (point-at-bol) (point))))
  7243. (completion-ignore-case opt)
  7244. (type nil)
  7245. (tbl nil)
  7246. (table (cond
  7247. (opt
  7248. (setq type :opt)
  7249. (require 'org-exp)
  7250. (append
  7251. (mapcar
  7252. (lambda (x)
  7253. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7254. (cons (match-string 2 x) (match-string 1 x)))
  7255. (org-split-string (org-get-current-options) "\n"))
  7256. (mapcar 'list org-additional-option-like-keywords)))
  7257. (startup
  7258. (setq type :startup)
  7259. org-startup-options)
  7260. (link (append org-link-abbrev-alist-local
  7261. org-link-abbrev-alist))
  7262. (texp
  7263. (setq type :tex)
  7264. org-html-entities)
  7265. ((string-match "\\`\\*+[ \t]+\\'"
  7266. (buffer-substring (point-at-bol) beg))
  7267. (setq type :todo)
  7268. (mapcar 'list org-todo-keywords-1))
  7269. (searchhead
  7270. (setq type :searchhead)
  7271. (save-excursion
  7272. (goto-char (point-min))
  7273. (while (re-search-forward org-todo-line-regexp nil t)
  7274. (push (list
  7275. (org-make-org-heading-search-string
  7276. (match-string 3) t))
  7277. tbl)))
  7278. tbl)
  7279. (tag (setq type :tag beg beg1)
  7280. (or org-tag-alist (org-get-buffer-tags)))
  7281. (prop (setq type :prop beg beg1)
  7282. (mapcar 'list (org-buffer-property-keys nil t t)))
  7283. (t (progn
  7284. (call-interactively org-completion-fallback-command)
  7285. (throw 'exit nil)))))
  7286. (pattern (buffer-substring-no-properties beg end))
  7287. (completion (try-completion pattern table confirm)))
  7288. (cond ((eq completion t)
  7289. (if (not (assoc (upcase pattern) table))
  7290. (message "Already complete")
  7291. (if (and (equal type :opt)
  7292. (not (member (car (assoc (upcase pattern) table))
  7293. org-additional-option-like-keywords)))
  7294. (insert (substring (cdr (assoc (upcase pattern) table))
  7295. (length pattern)))
  7296. (if (memq type '(:tag :prop)) (insert ":")))))
  7297. ((null completion)
  7298. (message "Can't find completion for \"%s\"" pattern)
  7299. (ding))
  7300. ((not (string= pattern completion))
  7301. (delete-region beg end)
  7302. (if (string-match " +$" completion)
  7303. (setq completion (replace-match "" t t completion)))
  7304. (insert completion)
  7305. (if (get-buffer-window "*Completions*")
  7306. (delete-window (get-buffer-window "*Completions*")))
  7307. (if (assoc completion table)
  7308. (if (eq type :todo) (insert " ")
  7309. (if (memq type '(:tag :prop)) (insert ":"))))
  7310. (if (and (equal type :opt) (assoc completion table))
  7311. (message "%s" (substitute-command-keys
  7312. "Press \\[org-complete] again to insert example settings"))))
  7313. (t
  7314. (message "Making completion list...")
  7315. (let ((list (sort (all-completions pattern table confirm)
  7316. 'string<)))
  7317. (with-output-to-temp-buffer "*Completions*"
  7318. (condition-case nil
  7319. ;; Protection needed for XEmacs and emacs 21
  7320. (display-completion-list list pattern)
  7321. (error (display-completion-list list)))))
  7322. (message "Making completion list...%s" "done")))))))
  7323. ;;;; TODO, DEADLINE, Comments
  7324. (defun org-toggle-comment ()
  7325. "Change the COMMENT state of an entry."
  7326. (interactive)
  7327. (save-excursion
  7328. (org-back-to-heading)
  7329. (let (case-fold-search)
  7330. (if (looking-at (concat outline-regexp
  7331. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7332. (replace-match "" t t nil 1)
  7333. (if (looking-at outline-regexp)
  7334. (progn
  7335. (goto-char (match-end 0))
  7336. (insert org-comment-string " ")))))))
  7337. (defvar org-last-todo-state-is-todo nil
  7338. "This is non-nil when the last TODO state change led to a TODO state.
  7339. If the last change removed the TODO tag or switched to DONE, then
  7340. this is nil.")
  7341. (defvar org-setting-tags nil) ; dynamically skipped
  7342. (defun org-parse-local-options (string var)
  7343. "Parse STRING for startup setting relevant for variable VAR."
  7344. (let ((rtn (symbol-value var))
  7345. e opts)
  7346. (save-match-data
  7347. (if (or (not string) (not (string-match "\\S-" string)))
  7348. rtn
  7349. (setq opts (delq nil (mapcar (lambda (x)
  7350. (setq e (assoc x org-startup-options))
  7351. (if (eq (nth 1 e) var) e nil))
  7352. (org-split-string string "[ \t]+"))))
  7353. (if (not opts)
  7354. rtn
  7355. (setq rtn nil)
  7356. (while (setq e (pop opts))
  7357. (if (not (nth 3 e))
  7358. (setq rtn (nth 2 e))
  7359. (if (not (listp rtn)) (setq rtn nil))
  7360. (push (nth 2 e) rtn)))
  7361. rtn)))))
  7362. (defvar org-blocker-hook nil
  7363. "Hook for functions that are allowed to block a state change.
  7364. Each function gets as its single argument a property list, see
  7365. `org-trigger-hook' for more information about this list.
  7366. If any of the functions in this hook returns nil, the state change
  7367. is blocked.")
  7368. (defvar org-trigger-hook nil
  7369. "Hook for functions that are triggered by a state change.
  7370. Each function gets as its single argument a property list with at least
  7371. the following elements:
  7372. (:type type-of-change :position pos-at-entry-start
  7373. :from old-state :to new-state)
  7374. Depending on the type, more properties may be present.
  7375. This mechanism is currently implemented for:
  7376. TODO state changes
  7377. ------------------
  7378. :type todo-state-change
  7379. :from previous state (keyword as a string), or nil
  7380. :to new state (keyword as a string), or nil")
  7381. (defvar org-agenda-headline-snapshot-before-repeat)
  7382. (defun org-todo (&optional arg)
  7383. "Change the TODO state of an item.
  7384. The state of an item is given by a keyword at the start of the heading,
  7385. like
  7386. *** TODO Write paper
  7387. *** DONE Call mom
  7388. The different keywords are specified in the variable `org-todo-keywords'.
  7389. By default the available states are \"TODO\" and \"DONE\".
  7390. So for this example: when the item starts with TODO, it is changed to DONE.
  7391. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7392. DONE are present, add TODO at the beginning of the heading.
  7393. With C-u prefix arg, use completion to determine the new state.
  7394. With numeric prefix arg, switch to that state.
  7395. For calling through lisp, arg is also interpreted in the following way:
  7396. 'none -> empty state
  7397. \"\"(empty string) -> switch to empty state
  7398. 'done -> switch to DONE
  7399. 'nextset -> switch to the next set of keywords
  7400. 'previousset -> switch to the previous set of keywords
  7401. \"WAITING\" -> switch to the specified keyword, but only if it
  7402. really is a member of `org-todo-keywords'."
  7403. (interactive "P")
  7404. (save-excursion
  7405. (catch 'exit
  7406. (org-back-to-heading)
  7407. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7408. (or (looking-at (concat " +" org-todo-regexp " *"))
  7409. (looking-at " *"))
  7410. (let* ((match-data (match-data))
  7411. (startpos (point-at-bol))
  7412. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7413. (org-log-done org-log-done)
  7414. (org-log-repeat org-log-repeat)
  7415. (org-todo-log-states org-todo-log-states)
  7416. (this (match-string 1))
  7417. (hl-pos (match-beginning 0))
  7418. (head (org-get-todo-sequence-head this))
  7419. (ass (assoc head org-todo-kwd-alist))
  7420. (interpret (nth 1 ass))
  7421. (done-word (nth 3 ass))
  7422. (final-done-word (nth 4 ass))
  7423. (last-state (or this ""))
  7424. (completion-ignore-case t)
  7425. (member (member this org-todo-keywords-1))
  7426. (tail (cdr member))
  7427. (state (cond
  7428. ((and org-todo-key-trigger
  7429. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7430. (and (not arg) org-use-fast-todo-selection
  7431. (not (eq org-use-fast-todo-selection 'prefix)))))
  7432. ;; Use fast selection
  7433. (org-fast-todo-selection))
  7434. ((and (equal arg '(4))
  7435. (or (not org-use-fast-todo-selection)
  7436. (not org-todo-key-trigger)))
  7437. ;; Read a state with completion
  7438. (org-ido-completing-read "State: " (mapcar (lambda(x) (list x))
  7439. org-todo-keywords-1)
  7440. nil t))
  7441. ((eq arg 'right)
  7442. (if this
  7443. (if tail (car tail) nil)
  7444. (car org-todo-keywords-1)))
  7445. ((eq arg 'left)
  7446. (if (equal member org-todo-keywords-1)
  7447. nil
  7448. (if this
  7449. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7450. org-todo-keywords-1)
  7451. (org-last org-todo-keywords-1))))
  7452. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7453. (setq arg nil))) ; hack to fall back to cycling
  7454. (arg
  7455. ;; user or caller requests a specific state
  7456. (cond
  7457. ((equal arg "") nil)
  7458. ((eq arg 'none) nil)
  7459. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7460. ((eq arg 'nextset)
  7461. (or (car (cdr (member head org-todo-heads)))
  7462. (car org-todo-heads)))
  7463. ((eq arg 'previousset)
  7464. (let ((org-todo-heads (reverse org-todo-heads)))
  7465. (or (car (cdr (member head org-todo-heads)))
  7466. (car org-todo-heads))))
  7467. ((car (member arg org-todo-keywords-1)))
  7468. ((nth (1- (prefix-numeric-value arg))
  7469. org-todo-keywords-1))))
  7470. ((null member) (or head (car org-todo-keywords-1)))
  7471. ((equal this final-done-word) nil) ;; -> make empty
  7472. ((null tail) nil) ;; -> first entry
  7473. ((eq interpret 'sequence)
  7474. (car tail))
  7475. ((memq interpret '(type priority))
  7476. (if (eq this-command last-command)
  7477. (car tail)
  7478. (if (> (length tail) 0)
  7479. (or done-word (car org-done-keywords))
  7480. nil)))
  7481. (t nil)))
  7482. (next (if state (concat " " state " ") " "))
  7483. (change-plist (list :type 'todo-state-change :from this :to state
  7484. :position startpos))
  7485. dolog now-done-p)
  7486. (when org-blocker-hook
  7487. (unless (save-excursion
  7488. (save-match-data
  7489. (run-hook-with-args-until-failure
  7490. 'org-blocker-hook change-plist)))
  7491. (if (interactive-p)
  7492. (error "TODO state change from %s to %s blocked" this state)
  7493. ;; fail silently
  7494. (message "TODO state change from %s to %s blocked" this state)
  7495. (throw 'exit nil))))
  7496. (store-match-data match-data)
  7497. (replace-match next t t)
  7498. (unless (pos-visible-in-window-p hl-pos)
  7499. (message "TODO state changed to %s" (org-trim next)))
  7500. (unless head
  7501. (setq head (org-get-todo-sequence-head state)
  7502. ass (assoc head org-todo-kwd-alist)
  7503. interpret (nth 1 ass)
  7504. done-word (nth 3 ass)
  7505. final-done-word (nth 4 ass)))
  7506. (when (memq arg '(nextset previousset))
  7507. (message "Keyword-Set %d/%d: %s"
  7508. (- (length org-todo-sets) -1
  7509. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7510. (length org-todo-sets)
  7511. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7512. (setq org-last-todo-state-is-todo
  7513. (not (member state org-done-keywords)))
  7514. (setq now-done-p (and (member state org-done-keywords)
  7515. (not (member this org-done-keywords))))
  7516. (and logging (org-local-logging logging))
  7517. (when (and (or org-todo-log-states org-log-done)
  7518. (not (memq arg '(nextset previousset))))
  7519. ;; we need to look at recording a time and note
  7520. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7521. (nth 2 (assoc this org-todo-log-states))))
  7522. (when (and state
  7523. (member state org-not-done-keywords)
  7524. (not (member this org-not-done-keywords)))
  7525. ;; This is now a todo state and was not one before
  7526. ;; If there was a CLOSED time stamp, get rid of it.
  7527. (org-add-planning-info nil nil 'closed))
  7528. (when (and now-done-p org-log-done)
  7529. ;; It is now done, and it was not done before
  7530. (org-add-planning-info 'closed (org-current-time))
  7531. (if (and (not dolog) (eq 'note org-log-done))
  7532. (org-add-log-setup 'done state 'findpos 'note)))
  7533. (when (and state dolog)
  7534. ;; This is a non-nil state, and we need to log it
  7535. (org-add-log-setup 'state state 'findpos dolog)))
  7536. ;; Fixup tag positioning
  7537. (org-todo-trigger-tag-changes state)
  7538. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7539. (when org-provide-todo-statistics
  7540. (org-update-parent-todo-statistics))
  7541. (run-hooks 'org-after-todo-state-change-hook)
  7542. (if (and arg (not (member state org-done-keywords)))
  7543. (setq head (org-get-todo-sequence-head state)))
  7544. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7545. ;; Do we need to trigger a repeat?
  7546. (when now-done-p
  7547. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7548. ;; This is for the agenda, take a snapshot of the headline.
  7549. (save-match-data
  7550. (setq org-agenda-headline-snapshot-before-repeat
  7551. (org-get-heading))))
  7552. (org-auto-repeat-maybe state))
  7553. ;; Fixup cursor location if close to the keyword
  7554. (if (and (outline-on-heading-p)
  7555. (not (bolp))
  7556. (save-excursion (beginning-of-line 1)
  7557. (looking-at org-todo-line-regexp))
  7558. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7559. (progn
  7560. (goto-char (or (match-end 2) (match-end 1)))
  7561. (just-one-space)))
  7562. (when org-trigger-hook
  7563. (save-excursion
  7564. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7565. (defun org-update-parent-todo-statistics ()
  7566. "Update any statistics cookie in the parent of the current headline."
  7567. (interactive)
  7568. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7569. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7570. (catch 'exit
  7571. (save-excursion
  7572. (setq level (org-up-heading-safe))
  7573. (unless (and level
  7574. (re-search-forward box-re (point-at-eol) t))
  7575. (throw 'exit nil))
  7576. (setq is-percent (match-end 2))
  7577. (save-match-data
  7578. (unless (outline-next-heading) (throw 'exit nil))
  7579. (while (looking-at org-todo-line-regexp)
  7580. (setq kwd (match-string 2))
  7581. (and kwd (setq cnt-all (1+ cnt-all)))
  7582. (and (member kwd org-done-keywords)
  7583. (setq cnt-done (1+ cnt-done)))
  7584. (condition-case nil
  7585. (org-forward-same-level 1)
  7586. (error (end-of-line 1)))))
  7587. (replace-match
  7588. (if is-percent
  7589. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7590. (format "[%d/%d]" cnt-done cnt-all)))
  7591. (run-hook-with-args 'org-after-todo-statistics-hook
  7592. cnt-done (- cnt-all cnt-done))))))
  7593. (defvar org-after-todo-statistics-hook nil
  7594. "Hook that is called after a TODO statistics cookie has been updated.
  7595. Each function is called with two arguments: the number of not-done entries
  7596. and the number of done entries.
  7597. For example, the following function, when added to this hook, will switch
  7598. an entry to DONE when all children are done, and back to TODO when new
  7599. entries are set to a TODO status. Note that this hook is only called
  7600. when there is a statistics cookie in the headline!
  7601. (defun org-summary-todo (n-done n-not-done)
  7602. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7603. (let (org-log-done org-log-states) ; turn off logging
  7604. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7605. ")
  7606. (defun org-todo-trigger-tag-changes (state)
  7607. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7608. (let ((l org-todo-state-tags-triggers)
  7609. changes)
  7610. (when (or (not state) (equal state ""))
  7611. (setq changes (append changes (cdr (assoc "" l)))))
  7612. (when (and (stringp state) (> (length state) 0))
  7613. (setq changes (append changes (cdr (assoc state l)))))
  7614. (when (member state org-not-done-keywords)
  7615. (setq changes (append changes (cdr (assoc 'todo l)))))
  7616. (when (member state org-done-keywords)
  7617. (setq changes (append changes (cdr (assoc 'done l)))))
  7618. (dolist (c changes)
  7619. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7620. (defun org-local-logging (value)
  7621. "Get logging settings from a property VALUE."
  7622. (let* (words w a)
  7623. ;; directly set the variables, they are already local.
  7624. (setq org-log-done nil
  7625. org-log-repeat nil
  7626. org-todo-log-states nil)
  7627. (setq words (org-split-string value))
  7628. (while (setq w (pop words))
  7629. (cond
  7630. ((setq a (assoc w org-startup-options))
  7631. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7632. (set (nth 1 a) (nth 2 a))))
  7633. ((setq a (org-extract-log-state-settings w))
  7634. (and (member (car a) org-todo-keywords-1)
  7635. (push a org-todo-log-states)))))))
  7636. (defun org-get-todo-sequence-head (kwd)
  7637. "Return the head of the TODO sequence to which KWD belongs.
  7638. If KWD is not set, check if there is a text property remembering the
  7639. right sequence."
  7640. (let (p)
  7641. (cond
  7642. ((not kwd)
  7643. (or (get-text-property (point-at-bol) 'org-todo-head)
  7644. (progn
  7645. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7646. nil (point-at-eol)))
  7647. (get-text-property p 'org-todo-head))))
  7648. ((not (member kwd org-todo-keywords-1))
  7649. (car org-todo-keywords-1))
  7650. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7651. (defun org-fast-todo-selection ()
  7652. "Fast TODO keyword selection with single keys.
  7653. Returns the new TODO keyword, or nil if no state change should occur."
  7654. (let* ((fulltable org-todo-key-alist)
  7655. (done-keywords org-done-keywords) ;; needed for the faces.
  7656. (maxlen (apply 'max (mapcar
  7657. (lambda (x)
  7658. (if (stringp (car x)) (string-width (car x)) 0))
  7659. fulltable)))
  7660. (expert nil)
  7661. (fwidth (+ maxlen 3 1 3))
  7662. (ncol (/ (- (window-width) 4) fwidth))
  7663. tg cnt e c tbl
  7664. groups ingroup)
  7665. (save-window-excursion
  7666. (if expert
  7667. (set-buffer (get-buffer-create " *Org todo*"))
  7668. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7669. (erase-buffer)
  7670. (org-set-local 'org-done-keywords done-keywords)
  7671. (setq tbl fulltable cnt 0)
  7672. (while (setq e (pop tbl))
  7673. (cond
  7674. ((equal e '(:startgroup))
  7675. (push '() groups) (setq ingroup t)
  7676. (when (not (= cnt 0))
  7677. (setq cnt 0)
  7678. (insert "\n"))
  7679. (insert "{ "))
  7680. ((equal e '(:endgroup))
  7681. (setq ingroup nil cnt 0)
  7682. (insert "}\n"))
  7683. (t
  7684. (setq tg (car e) c (cdr e))
  7685. (if ingroup (push tg (car groups)))
  7686. (setq tg (org-add-props tg nil 'face
  7687. (org-get-todo-face tg)))
  7688. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7689. (insert "[" c "] " tg (make-string
  7690. (- fwidth 4 (length tg)) ?\ ))
  7691. (when (= (setq cnt (1+ cnt)) ncol)
  7692. (insert "\n")
  7693. (if ingroup (insert " "))
  7694. (setq cnt 0)))))
  7695. (insert "\n")
  7696. (goto-char (point-min))
  7697. (if (not expert) (org-fit-window-to-buffer))
  7698. (message "[a-z..]:Set [SPC]:clear")
  7699. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7700. (cond
  7701. ((or (= c ?\C-g)
  7702. (and (= c ?q) (not (rassoc c fulltable))))
  7703. (setq quit-flag t))
  7704. ((= c ?\ ) nil)
  7705. ((setq e (rassoc c fulltable) tg (car e))
  7706. tg)
  7707. (t (setq quit-flag t))))))
  7708. (defun org-entry-is-todo-p ()
  7709. (member (org-get-todo-state) org-not-done-keywords))
  7710. (defun org-entry-is-done-p ()
  7711. (member (org-get-todo-state) org-done-keywords))
  7712. (defun org-get-todo-state ()
  7713. (save-excursion
  7714. (org-back-to-heading t)
  7715. (and (looking-at org-todo-line-regexp)
  7716. (match-end 2)
  7717. (match-string 2))))
  7718. (defun org-at-date-range-p (&optional inactive-ok)
  7719. "Is the cursor inside a date range?"
  7720. (interactive)
  7721. (save-excursion
  7722. (catch 'exit
  7723. (let ((pos (point)))
  7724. (skip-chars-backward "^[<\r\n")
  7725. (skip-chars-backward "<[")
  7726. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7727. (>= (match-end 0) pos)
  7728. (throw 'exit t))
  7729. (skip-chars-backward "^<[\r\n")
  7730. (skip-chars-backward "<[")
  7731. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7732. (>= (match-end 0) pos)
  7733. (throw 'exit t)))
  7734. nil)))
  7735. (defun org-get-repeat ()
  7736. "Check if there is a deadline/schedule with repeater in this entry."
  7737. (save-match-data
  7738. (save-excursion
  7739. (org-back-to-heading t)
  7740. (if (re-search-forward
  7741. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7742. (match-string 1)))))
  7743. (defvar org-last-changed-timestamp)
  7744. (defvar org-last-inserted-timestamp)
  7745. (defvar org-log-post-message)
  7746. (defvar org-log-note-purpose)
  7747. (defvar org-log-note-how)
  7748. (defvar org-log-note-extra)
  7749. (defun org-auto-repeat-maybe (done-word)
  7750. "Check if the current headline contains a repeated deadline/schedule.
  7751. If yes, set TODO state back to what it was and change the base date
  7752. of repeating deadline/scheduled time stamps to new date.
  7753. This function is run automatically after each state change to a DONE state."
  7754. ;; last-state is dynamically scoped into this function
  7755. (let* ((repeat (org-get-repeat))
  7756. (aa (assoc last-state org-todo-kwd-alist))
  7757. (interpret (nth 1 aa))
  7758. (head (nth 2 aa))
  7759. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7760. (msg "Entry repeats: ")
  7761. (org-log-done nil)
  7762. (org-todo-log-states nil)
  7763. (nshiftmax 10) (nshift 0)
  7764. re type n what ts mb0 time)
  7765. (when repeat
  7766. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7767. (org-todo (if (eq interpret 'type) last-state head))
  7768. (when org-log-repeat
  7769. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7770. (memq 'org-add-log-note post-command-hook))
  7771. ;; OK, we are already setup for some record
  7772. (if (eq org-log-repeat 'note)
  7773. ;; make sure we take a note, not only a time stamp
  7774. (setq org-log-note-how 'note))
  7775. ;; Set up for taking a record
  7776. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7777. 'findpos org-log-repeat)))
  7778. (org-back-to-heading t)
  7779. (org-add-planning-info nil nil 'closed)
  7780. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7781. org-deadline-time-regexp "\\)\\|\\("
  7782. org-ts-regexp "\\)"))
  7783. (while (re-search-forward
  7784. re (save-excursion (outline-next-heading) (point)) t)
  7785. (setq type (if (match-end 1) org-scheduled-string
  7786. (if (match-end 3) org-deadline-string "Plain:"))
  7787. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7788. mb0 (match-beginning 0))
  7789. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7790. (setq n (string-to-number (match-string 2 ts))
  7791. what (match-string 3 ts))
  7792. (if (equal what "w") (setq n (* n 7) what "d"))
  7793. ;; Preparation, see if we need to modify the start date for the change
  7794. (when (match-end 1)
  7795. (setq time (save-match-data (org-time-string-to-time ts)))
  7796. (cond
  7797. ((equal (match-string 1 ts) ".")
  7798. ;; Shift starting date to today
  7799. (org-timestamp-change
  7800. (- (time-to-days (current-time)) (time-to-days time))
  7801. 'day))
  7802. ((equal (match-string 1 ts) "+")
  7803. (while (or (= nshift 0)
  7804. (<= (time-to-days time) (time-to-days (current-time))))
  7805. (when (= (incf nshift) nshiftmax)
  7806. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7807. (error "Abort")))
  7808. (org-timestamp-change n (cdr (assoc what whata)))
  7809. (org-at-timestamp-p t)
  7810. (setq ts (match-string 1))
  7811. (setq time (save-match-data (org-time-string-to-time ts))))
  7812. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7813. ;; rematch, so that we have everything in place for the real shift
  7814. (org-at-timestamp-p t)
  7815. (setq ts (match-string 1))
  7816. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7817. (org-timestamp-change n (cdr (assoc what whata)))
  7818. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7819. (setq org-log-post-message msg)
  7820. (message "%s" msg))))
  7821. (defun org-show-todo-tree (arg)
  7822. "Make a compact tree which shows all headlines marked with TODO.
  7823. The tree will show the lines where the regexp matches, and all higher
  7824. headlines above the match.
  7825. With a \\[universal-argument] prefix, also show the DONE entries.
  7826. With a numeric prefix N, construct a sparse tree for the Nth element
  7827. of `org-todo-keywords-1'."
  7828. (interactive "P")
  7829. (let ((case-fold-search nil)
  7830. (kwd-re
  7831. (cond ((null arg) org-not-done-regexp)
  7832. ((equal arg '(4))
  7833. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  7834. (mapcar 'list org-todo-keywords-1))))
  7835. (concat "\\("
  7836. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7837. "\\)\\>")))
  7838. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7839. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7840. org-todo-keywords-1)))
  7841. (t (error "Invalid prefix argument: %s" arg)))))
  7842. (message "%d TODO entries found"
  7843. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7844. (defun org-deadline (&optional remove time)
  7845. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7846. With argument REMOVE, remove any deadline from the item.
  7847. When TIME is set, it should be an internal time specification, and the
  7848. scheduling will use the corresponding date."
  7849. (interactive "P")
  7850. (if remove
  7851. (progn
  7852. (org-remove-timestamp-with-keyword org-deadline-string)
  7853. (message "Item no longer has a deadline."))
  7854. (if (org-get-repeat)
  7855. (error "Cannot change deadline on task with repeater, please do that by hand")
  7856. (org-add-planning-info 'deadline time 'closed)
  7857. (message "Deadline on %s" org-last-inserted-timestamp))))
  7858. (defun org-schedule (&optional remove time)
  7859. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7860. With argument REMOVE, remove any scheduling date from the item.
  7861. When TIME is set, it should be an internal time specification, and the
  7862. scheduling will use the corresponding date."
  7863. (interactive "P")
  7864. (if remove
  7865. (progn
  7866. (org-remove-timestamp-with-keyword org-scheduled-string)
  7867. (message "Item is no longer scheduled."))
  7868. (if (org-get-repeat)
  7869. (error "Cannot reschedule task with repeater, please do that by hand")
  7870. (org-add-planning-info 'scheduled time 'closed)
  7871. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7872. (defun org-remove-timestamp-with-keyword (keyword)
  7873. "Remove all time stamps with KEYWORD in the current entry."
  7874. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7875. beg)
  7876. (save-excursion
  7877. (org-back-to-heading t)
  7878. (setq beg (point))
  7879. (org-end-of-subtree t t)
  7880. (while (re-search-backward re beg t)
  7881. (replace-match "")
  7882. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7883. (equal (char-before) ?\ ))
  7884. (backward-delete-char 1)
  7885. (if (string-match "^[ \t]*$" (buffer-substring
  7886. (point-at-bol) (point-at-eol)))
  7887. (delete-region (point-at-bol)
  7888. (min (point-max) (1+ (point-at-eol))))))))))
  7889. (defun org-add-planning-info (what &optional time &rest remove)
  7890. "Insert new timestamp with keyword in the line directly after the headline.
  7891. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7892. If non is given, the user is prompted for a date.
  7893. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7894. be removed."
  7895. (interactive)
  7896. (let (org-time-was-given org-end-time-was-given ts
  7897. end default-time default-input)
  7898. (when (and (not time) (memq what '(scheduled deadline)))
  7899. ;; Try to get a default date/time from existing timestamp
  7900. (save-excursion
  7901. (org-back-to-heading t)
  7902. (setq end (save-excursion (outline-next-heading) (point)))
  7903. (when (re-search-forward (if (eq what 'scheduled)
  7904. org-scheduled-time-regexp
  7905. org-deadline-time-regexp)
  7906. end t)
  7907. (setq ts (match-string 1)
  7908. default-time
  7909. (apply 'encode-time (org-parse-time-string ts))
  7910. default-input (and ts (org-get-compact-tod ts))))))
  7911. (when what
  7912. ;; If necessary, get the time from the user
  7913. (setq time (or time (org-read-date nil 'to-time nil nil
  7914. default-time default-input))))
  7915. (when (and org-insert-labeled-timestamps-at-point
  7916. (member what '(scheduled deadline)))
  7917. (insert
  7918. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7919. (org-insert-time-stamp time org-time-was-given
  7920. nil nil nil (list org-end-time-was-given))
  7921. (setq what nil))
  7922. (save-excursion
  7923. (save-restriction
  7924. (let (col list elt ts buffer-invisibility-spec)
  7925. (org-back-to-heading t)
  7926. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7927. (goto-char (match-end 1))
  7928. (setq col (current-column))
  7929. (goto-char (match-end 0))
  7930. (if (eobp) (insert "\n") (forward-char 1))
  7931. (if (and (not (looking-at outline-regexp))
  7932. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7933. "[^\r\n]*"))
  7934. (not (equal (match-string 1) org-clock-string)))
  7935. (narrow-to-region (match-beginning 0) (match-end 0))
  7936. (insert-before-markers "\n")
  7937. (backward-char 1)
  7938. (narrow-to-region (point) (point))
  7939. (and org-adapt-indentation (org-indent-to-column col)))
  7940. ;; Check if we have to remove something.
  7941. (setq list (cons what remove))
  7942. (while list
  7943. (setq elt (pop list))
  7944. (goto-char (point-min))
  7945. (when (or (and (eq elt 'scheduled)
  7946. (re-search-forward org-scheduled-time-regexp nil t))
  7947. (and (eq elt 'deadline)
  7948. (re-search-forward org-deadline-time-regexp nil t))
  7949. (and (eq elt 'closed)
  7950. (re-search-forward org-closed-time-regexp nil t)))
  7951. (replace-match "")
  7952. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7953. (if (looking-at " +") (replace-match ""))))
  7954. (goto-char (point-max))
  7955. (when what
  7956. (insert
  7957. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7958. (cond ((eq what 'scheduled) org-scheduled-string)
  7959. ((eq what 'deadline) org-deadline-string)
  7960. ((eq what 'closed) org-closed-string))
  7961. " ")
  7962. (setq ts (org-insert-time-stamp
  7963. time
  7964. (or org-time-was-given
  7965. (and (eq what 'closed) org-log-done-with-time))
  7966. (eq what 'closed)
  7967. nil nil (list org-end-time-was-given)))
  7968. (end-of-line 1))
  7969. (goto-char (point-min))
  7970. (widen)
  7971. (if (and (looking-at "[ \t]+\n")
  7972. (equal (char-before) ?\n))
  7973. (delete-region (1- (point)) (point-at-eol)))
  7974. ts)))))
  7975. (defvar org-log-note-marker (make-marker))
  7976. (defvar org-log-note-purpose nil)
  7977. (defvar org-log-note-state nil)
  7978. (defvar org-log-note-how nil)
  7979. (defvar org-log-note-extra nil)
  7980. (defvar org-log-note-window-configuration nil)
  7981. (defvar org-log-note-return-to (make-marker))
  7982. (defvar org-log-post-message nil
  7983. "Message to be displayed after a log note has been stored.
  7984. The auto-repeater uses this.")
  7985. (defun org-add-note ()
  7986. "Add a note to the current entry.
  7987. This is done in the same way as adding a state change note."
  7988. (interactive)
  7989. (org-add-log-setup 'note nil 'findpos nil))
  7990. (defvar org-property-end-re)
  7991. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  7992. "Set up the post command hook to take a note.
  7993. If this is about to TODO state change, the new state is expected in STATE.
  7994. When FINDPOS is non-nil, find the correct position for the note in
  7995. the current entry. If not, assume that it can be inserted at point.
  7996. HOW is an indicator what kind of note should be created.
  7997. EXTRA is additional text that will be inserted into the notes buffer."
  7998. (save-restriction
  7999. (save-excursion
  8000. (when findpos
  8001. (org-back-to-heading t)
  8002. (narrow-to-region (point) (save-excursion
  8003. (outline-next-heading) (point)))
  8004. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8005. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8006. "[^\r\n]*\\)?"))
  8007. (goto-char (match-end 0))
  8008. (when (and org-log-state-notes-insert-after-drawers
  8009. (save-excursion
  8010. (forward-line) (looking-at org-drawer-regexp)))
  8011. (progn (forward-line)
  8012. (while (looking-at org-drawer-regexp)
  8013. (goto-char (match-end 0))
  8014. (re-search-forward org-property-end-re (point-max) t)
  8015. (forward-line))
  8016. (forward-line -1)))
  8017. (unless org-log-states-order-reversed
  8018. (and (= (char-after) ?\n) (forward-char 1))
  8019. (org-skip-over-state-notes)
  8020. (skip-chars-backward " \t\n\r")))
  8021. (move-marker org-log-note-marker (point))
  8022. (setq org-log-note-purpose purpose
  8023. org-log-note-state state
  8024. org-log-note-how how
  8025. org-log-note-extra extra)
  8026. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  8027. (defun org-skip-over-state-notes ()
  8028. "Skip past the list of State notes in an entry."
  8029. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8030. (while (looking-at "[ \t]*- State")
  8031. (condition-case nil
  8032. (org-next-item)
  8033. (error (org-end-of-item)))))
  8034. (defun org-add-log-note (&optional purpose)
  8035. "Pop up a window for taking a note, and add this note later at point."
  8036. (remove-hook 'post-command-hook 'org-add-log-note)
  8037. (setq org-log-note-window-configuration (current-window-configuration))
  8038. (delete-other-windows)
  8039. (move-marker org-log-note-return-to (point))
  8040. (switch-to-buffer (marker-buffer org-log-note-marker))
  8041. (goto-char org-log-note-marker)
  8042. (org-switch-to-buffer-other-window "*Org Note*")
  8043. (erase-buffer)
  8044. (if (memq org-log-note-how '(time state))
  8045. (let (current-prefix-arg) (org-store-log-note))
  8046. (let ((org-inhibit-startup t)) (org-mode))
  8047. (insert (format "# Insert note for %s.
  8048. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8049. (cond
  8050. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8051. ((eq org-log-note-purpose 'done) "closed todo item")
  8052. ((eq org-log-note-purpose 'state)
  8053. (format "state change to \"%s\"" org-log-note-state))
  8054. ((eq org-log-note-purpose 'note)
  8055. "this entry")
  8056. (t (error "This should not happen")))))
  8057. (if org-log-note-extra (insert org-log-note-extra))
  8058. (org-set-local 'org-finish-function 'org-store-log-note)))
  8059. (defvar org-note-abort nil) ; dynamically scoped
  8060. (defun org-store-log-note ()
  8061. "Finish taking a log note, and insert it to where it belongs."
  8062. (let ((txt (buffer-string))
  8063. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8064. lines ind)
  8065. (kill-buffer (current-buffer))
  8066. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8067. (setq txt (replace-match "" t t txt)))
  8068. (if (string-match "\\s-+\\'" txt)
  8069. (setq txt (replace-match "" t t txt)))
  8070. (setq lines (org-split-string txt "\n"))
  8071. (when (and note (string-match "\\S-" note))
  8072. (setq note
  8073. (org-replace-escapes
  8074. note
  8075. (list (cons "%u" (user-login-name))
  8076. (cons "%U" user-full-name)
  8077. (cons "%t" (format-time-string
  8078. (org-time-stamp-format 'long 'inactive)
  8079. (current-time)))
  8080. (cons "%s" (if org-log-note-state
  8081. (concat "\"" org-log-note-state "\"")
  8082. "")))))
  8083. (if lines (setq note (concat note " \\\\")))
  8084. (push note lines))
  8085. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8086. (when lines
  8087. (save-excursion
  8088. (set-buffer (marker-buffer org-log-note-marker))
  8089. (save-excursion
  8090. (goto-char org-log-note-marker)
  8091. (move-marker org-log-note-marker nil)
  8092. (end-of-line 1)
  8093. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8094. (indent-relative nil)
  8095. (insert "- " (pop lines))
  8096. (org-indent-line-function)
  8097. (beginning-of-line 1)
  8098. (looking-at "[ \t]*")
  8099. (setq ind (concat (match-string 0) " "))
  8100. (end-of-line 1)
  8101. (while lines (insert "\n" ind (pop lines)))))))
  8102. (set-window-configuration org-log-note-window-configuration)
  8103. (with-current-buffer (marker-buffer org-log-note-return-to)
  8104. (goto-char org-log-note-return-to))
  8105. (move-marker org-log-note-return-to nil)
  8106. (and org-log-post-message (message "%s" org-log-post-message)))
  8107. (defun org-sparse-tree (&optional arg)
  8108. "Create a sparse tree, prompt for the details.
  8109. This command can create sparse trees. You first need to select the type
  8110. of match used to create the tree:
  8111. t Show entries with a specific TODO keyword.
  8112. T Show entries selected by a tags match.
  8113. p Enter a property name and its value (both with completion on existing
  8114. names/values) and show entries with that property.
  8115. r Show entries matching a regular expression
  8116. d Show deadlines due within `org-deadline-warning-days'."
  8117. (interactive "P")
  8118. (let (ans kwd value)
  8119. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8120. (setq ans (read-char-exclusive))
  8121. (cond
  8122. ((equal ans ?d)
  8123. (call-interactively 'org-check-deadlines))
  8124. ((equal ans ?b)
  8125. (call-interactively 'org-check-before-date))
  8126. ((equal ans ?t)
  8127. (org-show-todo-tree '(4)))
  8128. ((equal ans ?T)
  8129. (call-interactively 'org-tags-sparse-tree))
  8130. ((member ans '(?p ?P))
  8131. (setq kwd (org-ido-completing-read "Property: "
  8132. (mapcar 'list (org-buffer-property-keys))))
  8133. (setq value (org-ido-completing-read "Value: "
  8134. (mapcar 'list (org-property-values kwd))))
  8135. (unless (string-match "\\`{.*}\\'" value)
  8136. (setq value (concat "\"" value "\"")))
  8137. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8138. ((member ans '(?r ?R ?/))
  8139. (call-interactively 'org-occur))
  8140. (t (error "No such sparse tree command \"%c\"" ans)))))
  8141. (defvar org-occur-highlights nil
  8142. "List of overlays used for occur matches.")
  8143. (make-variable-buffer-local 'org-occur-highlights)
  8144. (defvar org-occur-parameters nil
  8145. "Parameters of the active org-occur calls.
  8146. This is a list, each call to org-occur pushes as cons cell,
  8147. containing the regular expression and the callback, onto the list.
  8148. The list can contain several entries if `org-occur' has been called
  8149. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8150. will only contain one set of parameters. When the highlights are
  8151. removed (for example with `C-c C-c', or with the next edit (depending
  8152. on `org-remove-highlights-with-change'), this variable is emptied
  8153. as well.")
  8154. (make-variable-buffer-local 'org-occur-parameters)
  8155. (defun org-occur (regexp &optional keep-previous callback)
  8156. "Make a compact tree which shows all matches of REGEXP.
  8157. The tree will show the lines where the regexp matches, and all higher
  8158. headlines above the match. It will also show the heading after the match,
  8159. to make sure editing the matching entry is easy.
  8160. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8161. call to `org-occur' will be kept, to allow stacking of calls to this
  8162. command.
  8163. If CALLBACK is non-nil, it is a function which is called to confirm
  8164. that the match should indeed be shown."
  8165. (interactive "sRegexp: \nP")
  8166. (unless keep-previous
  8167. (org-remove-occur-highlights nil nil t))
  8168. (push (cons regexp callback) org-occur-parameters)
  8169. (let ((cnt 0))
  8170. (save-excursion
  8171. (goto-char (point-min))
  8172. (if (or (not keep-previous) ; do not want to keep
  8173. (not org-occur-highlights)) ; no previous matches
  8174. ;; hide everything
  8175. (org-overview))
  8176. (while (re-search-forward regexp nil t)
  8177. (when (or (not callback)
  8178. (save-match-data (funcall callback)))
  8179. (setq cnt (1+ cnt))
  8180. (when org-highlight-sparse-tree-matches
  8181. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8182. (org-show-context 'occur-tree))))
  8183. (when org-remove-highlights-with-change
  8184. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8185. nil 'local))
  8186. (unless org-sparse-tree-open-archived-trees
  8187. (org-hide-archived-subtrees (point-min) (point-max)))
  8188. (run-hooks 'org-occur-hook)
  8189. (if (interactive-p)
  8190. (message "%d match(es) for regexp %s" cnt regexp))
  8191. cnt))
  8192. (defun org-show-context (&optional key)
  8193. "Make sure point and context and visible.
  8194. How much context is shown depends upon the variables
  8195. `org-show-hierarchy-above', `org-show-following-heading'. and
  8196. `org-show-siblings'."
  8197. (let ((heading-p (org-on-heading-p t))
  8198. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8199. (following-p (org-get-alist-option org-show-following-heading key))
  8200. (entry-p (org-get-alist-option org-show-entry-below key))
  8201. (siblings-p (org-get-alist-option org-show-siblings key)))
  8202. (catch 'exit
  8203. ;; Show heading or entry text
  8204. (if (and heading-p (not entry-p))
  8205. (org-flag-heading nil) ; only show the heading
  8206. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8207. (org-show-hidden-entry))) ; show entire entry
  8208. (when following-p
  8209. ;; Show next sibling, or heading below text
  8210. (save-excursion
  8211. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8212. (org-flag-heading nil))))
  8213. (when siblings-p (org-show-siblings))
  8214. (when hierarchy-p
  8215. ;; show all higher headings, possibly with siblings
  8216. (save-excursion
  8217. (while (and (condition-case nil
  8218. (progn (org-up-heading-all 1) t)
  8219. (error nil))
  8220. (not (bobp)))
  8221. (org-flag-heading nil)
  8222. (when siblings-p (org-show-siblings))))))))
  8223. (defun org-reveal (&optional siblings)
  8224. "Show current entry, hierarchy above it, and the following headline.
  8225. This can be used to show a consistent set of context around locations
  8226. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8227. not t for the search context.
  8228. With optional argument SIBLINGS, on each level of the hierarchy all
  8229. siblings are shown. This repairs the tree structure to what it would
  8230. look like when opened with hierarchical calls to `org-cycle'."
  8231. (interactive "P")
  8232. (let ((org-show-hierarchy-above t)
  8233. (org-show-following-heading t)
  8234. (org-show-siblings (if siblings t org-show-siblings)))
  8235. (org-show-context nil)))
  8236. (defun org-highlight-new-match (beg end)
  8237. "Highlight from BEG to END and mark the highlight is an occur headline."
  8238. (let ((ov (org-make-overlay beg end)))
  8239. (org-overlay-put ov 'face 'secondary-selection)
  8240. (push ov org-occur-highlights)))
  8241. (defun org-remove-occur-highlights (&optional beg end noremove)
  8242. "Remove the occur highlights from the buffer.
  8243. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8244. from the `before-change-functions' in the current buffer."
  8245. (interactive)
  8246. (unless org-inhibit-highlight-removal
  8247. (mapc 'org-delete-overlay org-occur-highlights)
  8248. (setq org-occur-highlights nil)
  8249. (setq org-occur-parameters nil)
  8250. (unless noremove
  8251. (remove-hook 'before-change-functions
  8252. 'org-remove-occur-highlights 'local))))
  8253. ;;;; Priorities
  8254. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8255. "Regular expression matching the priority indicator.")
  8256. (defvar org-remove-priority-next-time nil)
  8257. (defun org-priority-up ()
  8258. "Increase the priority of the current item."
  8259. (interactive)
  8260. (org-priority 'up))
  8261. (defun org-priority-down ()
  8262. "Decrease the priority of the current item."
  8263. (interactive)
  8264. (org-priority 'down))
  8265. (defun org-priority (&optional action)
  8266. "Change the priority of an item by ARG.
  8267. ACTION can be `set', `up', `down', or a character."
  8268. (interactive)
  8269. (setq action (or action 'set))
  8270. (let (current new news have remove)
  8271. (save-excursion
  8272. (org-back-to-heading)
  8273. (if (looking-at org-priority-regexp)
  8274. (setq current (string-to-char (match-string 2))
  8275. have t)
  8276. (setq current org-default-priority))
  8277. (cond
  8278. ((or (eq action 'set)
  8279. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8280. (if (not (eq action 'set))
  8281. (setq new action)
  8282. (message "Priority %c-%c, SPC to remove: "
  8283. org-highest-priority org-lowest-priority)
  8284. (setq new (read-char-exclusive)))
  8285. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8286. (= (upcase org-lowest-priority) org-lowest-priority))
  8287. (setq new (upcase new)))
  8288. (cond ((equal new ?\ ) (setq remove t))
  8289. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8290. (error "Priority must be between `%c' and `%c'"
  8291. org-highest-priority org-lowest-priority))))
  8292. ((eq action 'up)
  8293. (if (and (not have) (eq last-command this-command))
  8294. (setq new org-lowest-priority)
  8295. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8296. org-default-priority (1- current)))))
  8297. ((eq action 'down)
  8298. (if (and (not have) (eq last-command this-command))
  8299. (setq new org-highest-priority)
  8300. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8301. org-default-priority (1+ current)))))
  8302. (t (error "Invalid action")))
  8303. (if (or (< (upcase new) org-highest-priority)
  8304. (> (upcase new) org-lowest-priority))
  8305. (setq remove t))
  8306. (setq news (format "%c" new))
  8307. (if have
  8308. (if remove
  8309. (replace-match "" t t nil 1)
  8310. (replace-match news t t nil 2))
  8311. (if remove
  8312. (error "No priority cookie found in line")
  8313. (looking-at org-todo-line-regexp)
  8314. (if (match-end 2)
  8315. (progn
  8316. (goto-char (match-end 2))
  8317. (insert " [#" news "]"))
  8318. (goto-char (match-beginning 3))
  8319. (insert "[#" news "] ")))))
  8320. (org-preserve-lc (org-set-tags nil 'align))
  8321. (if remove
  8322. (message "Priority removed")
  8323. (message "Priority of current item set to %s" news))))
  8324. (defun org-get-priority (s)
  8325. "Find priority cookie and return priority."
  8326. (save-match-data
  8327. (if (not (string-match org-priority-regexp s))
  8328. (* 1000 (- org-lowest-priority org-default-priority))
  8329. (* 1000 (- org-lowest-priority
  8330. (string-to-char (match-string 2 s)))))))
  8331. ;;;; Tags
  8332. (defvar org-agenda-archives-mode)
  8333. (defun org-scan-tags (action matcher &optional todo-only)
  8334. "Scan headline tags with inheritance and produce output ACTION.
  8335. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8336. or `agenda' to produce an entry list for an agenda view. It can also be
  8337. a Lisp form or a function that should be called at each matched headline, in
  8338. this case the return value is a list of all return values from these calls.
  8339. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8340. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8341. only lines with a TODO keyword are included in the output."
  8342. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8343. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8344. (org-re
  8345. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8346. (props (list 'face 'default
  8347. 'done-face 'org-done
  8348. 'undone-face 'default
  8349. 'mouse-face 'highlight
  8350. 'org-not-done-regexp org-not-done-regexp
  8351. 'org-todo-regexp org-todo-regexp
  8352. 'keymap org-agenda-keymap
  8353. 'help-echo
  8354. (format "mouse-2 or RET jump to org file %s"
  8355. (abbreviate-file-name
  8356. (or (buffer-file-name (buffer-base-buffer))
  8357. (buffer-name (buffer-base-buffer)))))))
  8358. (case-fold-search nil)
  8359. lspos tags tags-list
  8360. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8361. (llast 0) rtn rtn1 level category i txt
  8362. todo marker entry priority)
  8363. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8364. (setq action (list 'lambda nil action)))
  8365. (save-excursion
  8366. (goto-char (point-min))
  8367. (when (eq action 'sparse-tree)
  8368. (org-overview)
  8369. (org-remove-occur-highlights))
  8370. (while (re-search-forward re nil t)
  8371. (catch :skip
  8372. (setq todo (if (match-end 1) (match-string 2))
  8373. tags (if (match-end 4) (match-string 4)))
  8374. (goto-char (setq lspos (1+ (match-beginning 0))))
  8375. (setq level (org-reduced-level (funcall outline-level))
  8376. category (org-get-category))
  8377. (setq i llast llast level)
  8378. ;; remove tag lists from same and sublevels
  8379. (while (>= i level)
  8380. (when (setq entry (assoc i tags-alist))
  8381. (setq tags-alist (delete entry tags-alist)))
  8382. (setq i (1- i)))
  8383. ;; add the next tags
  8384. (when tags
  8385. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8386. tags-alist
  8387. (cons (cons level tags) tags-alist)))
  8388. ;; compile tags for current headline
  8389. (setq tags-list
  8390. (if org-use-tag-inheritance
  8391. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8392. tags))
  8393. (when org-use-tag-inheritance
  8394. (setcdr (car tags-alist)
  8395. (mapcar (lambda (x)
  8396. (setq x (copy-sequence x))
  8397. (org-add-prop-inherited x))
  8398. (cdar tags-alist))))
  8399. (when (and tags org-use-tag-inheritance
  8400. (not (eq t org-use-tag-inheritance)))
  8401. ;; selective inheritance, remove uninherited ones
  8402. (setcdr (car tags-alist)
  8403. (org-remove-uniherited-tags (cdar tags-alist))))
  8404. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8405. (let ((case-fold-search t)) (eval matcher))
  8406. (or
  8407. (not (member org-archive-tag tags-list))
  8408. ;; we have an archive tag, should we use this anyway?
  8409. (or (not org-agenda-skip-archived-trees)
  8410. (and (eq action 'agenda) org-agenda-archives-mode))))
  8411. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8412. ;; select this headline
  8413. (cond
  8414. ((eq action 'sparse-tree)
  8415. (and org-highlight-sparse-tree-matches
  8416. (org-get-heading) (match-end 0)
  8417. (org-highlight-new-match
  8418. (match-beginning 0) (match-beginning 1)))
  8419. (org-show-context 'tags-tree))
  8420. ((eq action 'agenda)
  8421. (setq txt (org-format-agenda-item
  8422. ""
  8423. (concat
  8424. (if org-tags-match-list-sublevels
  8425. (make-string (1- level) ?.) "")
  8426. (org-get-heading))
  8427. category (org-get-tags-at))
  8428. priority (org-get-priority txt))
  8429. (goto-char lspos)
  8430. (setq marker (org-agenda-new-marker))
  8431. (org-add-props txt props
  8432. 'org-marker marker 'org-hd-marker marker 'org-category category
  8433. 'priority priority 'type "tagsmatch")
  8434. (push txt rtn))
  8435. ((functionp action)
  8436. (save-excursion
  8437. (setq rtn1 (funcall action))
  8438. (push rtn1 rtn))
  8439. (goto-char (point-at-eol)))
  8440. (t (error "Invalid action")))
  8441. ;; if we are to skip sublevels, jump to end of subtree
  8442. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8443. (when (and (eq action 'sparse-tree)
  8444. (not org-sparse-tree-open-archived-trees))
  8445. (org-hide-archived-subtrees (point-min) (point-max)))
  8446. (nreverse rtn)))
  8447. (defun org-remove-uniherited-tags (tags)
  8448. "Remove all tags that are not inherited from the list TAGS."
  8449. (cond
  8450. ((eq org-use-tag-inheritance t)
  8451. (if org-tags-exclude-from-inheritance
  8452. (org-delete-all org-tags-exclude-from-inheritance tags)
  8453. tags))
  8454. ((not org-use-tag-inheritance) nil)
  8455. ((stringp org-use-tag-inheritance)
  8456. (delq nil (mapcar
  8457. (lambda (x)
  8458. (if (and (string-match org-use-tag-inheritance x)
  8459. (not (member x org-tags-exclude-from-inheritance)))
  8460. x nil))
  8461. tags)))
  8462. ((listp org-use-tag-inheritance)
  8463. (delq nil (mapcar
  8464. (lambda (x)
  8465. (if (member x org-use-tag-inheritance) x nil))
  8466. tags)))))
  8467. (defvar todo-only) ;; dynamically scoped
  8468. (defun org-tags-sparse-tree (&optional todo-only match)
  8469. "Create a sparse tree according to tags string MATCH.
  8470. MATCH can contain positive and negative selection of tags, like
  8471. \"+WORK+URGENT-WITHBOSS\".
  8472. If optional argument TODO-ONLY is non-nil, only select lines that are
  8473. also TODO lines."
  8474. (interactive "P")
  8475. (org-prepare-agenda-buffers (list (current-buffer)))
  8476. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8477. (defvar org-cached-props nil)
  8478. (defun org-cached-entry-get (pom property)
  8479. (if (or (eq t org-use-property-inheritance)
  8480. (and (stringp org-use-property-inheritance)
  8481. (string-match org-use-property-inheritance property))
  8482. (and (listp org-use-property-inheritance)
  8483. (member property org-use-property-inheritance)))
  8484. ;; Caching is not possible, check it directly
  8485. (org-entry-get pom property 'inherit)
  8486. ;; Get all properties, so that we can do complicated checks easily
  8487. (cdr (assoc property (or org-cached-props
  8488. (setq org-cached-props
  8489. (org-entry-properties pom)))))))
  8490. (defun org-global-tags-completion-table (&optional files)
  8491. "Return the list of all tags in all agenda buffer/files."
  8492. (save-excursion
  8493. (org-uniquify
  8494. (delq nil
  8495. (apply 'append
  8496. (mapcar
  8497. (lambda (file)
  8498. (set-buffer (find-file-noselect file))
  8499. (append (org-get-buffer-tags)
  8500. (mapcar (lambda (x) (if (stringp (car-safe x))
  8501. (list (car-safe x)) nil))
  8502. org-tag-alist)))
  8503. (if (and files (car files))
  8504. files
  8505. (org-agenda-files))))))))
  8506. (defun org-make-tags-matcher (match)
  8507. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8508. ;; todo-only is scoped dynamically into this function, and the function
  8509. ;; may change it if the matcher asks for it.
  8510. (unless match
  8511. ;; Get a new match request, with completion
  8512. (let ((org-last-tags-completion-table
  8513. (org-global-tags-completion-table)))
  8514. (setq match (org-ido-completing-read
  8515. "Match: " 'org-tags-completion-function nil nil nil
  8516. 'org-tags-history))))
  8517. ;; Parse the string and create a lisp form
  8518. (let ((match0 match)
  8519. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8520. minus tag mm
  8521. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8522. orterms term orlist re-p str-p level-p level-op time-p
  8523. prop-p pn pv po cat-p gv rest)
  8524. (if (string-match "/+" match)
  8525. ;; match contains also a todo-matching request
  8526. (progn
  8527. (setq tagsmatch (substring match 0 (match-beginning 0))
  8528. todomatch (substring match (match-end 0)))
  8529. (if (string-match "^!" todomatch)
  8530. (setq todo-only t todomatch (substring todomatch 1)))
  8531. (if (string-match "^\\s-*$" todomatch)
  8532. (setq todomatch nil)))
  8533. ;; only matching tags
  8534. (setq tagsmatch match todomatch nil))
  8535. ;; Make the tags matcher
  8536. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8537. (setq tagsmatcher t)
  8538. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8539. (while (setq term (pop orterms))
  8540. (while (and (equal (substring term -1) "\\") orterms)
  8541. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8542. (while (string-match re term)
  8543. (setq rest (substring term (match-end 0))
  8544. minus (and (match-end 1)
  8545. (equal (match-string 1 term) "-"))
  8546. tag (match-string 2 term)
  8547. re-p (equal (string-to-char tag) ?{)
  8548. level-p (match-end 4)
  8549. prop-p (match-end 5)
  8550. mm (cond
  8551. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8552. (level-p
  8553. (setq level-op (org-op-to-function (match-string 3 term)))
  8554. `(,level-op level ,(string-to-number
  8555. (match-string 4 term))))
  8556. (prop-p
  8557. (setq pn (match-string 5 term)
  8558. po (match-string 6 term)
  8559. pv (match-string 7 term)
  8560. cat-p (equal pn "CATEGORY")
  8561. re-p (equal (string-to-char pv) ?{)
  8562. str-p (equal (string-to-char pv) ?\")
  8563. time-p (save-match-data
  8564. (string-match "^\"[[<].*[]>]\"$" pv))
  8565. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8566. (if time-p (setq pv (org-matcher-time pv)))
  8567. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8568. (cond
  8569. ((equal pn "CATEGORY")
  8570. (setq gv '(get-text-property (point) 'org-category)))
  8571. ((equal pn "TODO")
  8572. (setq gv 'todo))
  8573. (t
  8574. (setq gv `(org-cached-entry-get nil ,pn))))
  8575. (if re-p
  8576. (if (eq po 'org<>)
  8577. `(not (string-match ,pv (or ,gv "")))
  8578. `(string-match ,pv (or ,gv "")))
  8579. (if str-p
  8580. `(,po (or ,gv "") ,pv)
  8581. `(,po (string-to-number (or ,gv ""))
  8582. ,(string-to-number pv) ))))
  8583. (t `(member ,(downcase tag) tags-list)))
  8584. mm (if minus (list 'not mm) mm)
  8585. term rest)
  8586. (push mm tagsmatcher))
  8587. (push (if (> (length tagsmatcher) 1)
  8588. (cons 'and tagsmatcher)
  8589. (car tagsmatcher))
  8590. orlist)
  8591. (setq tagsmatcher nil))
  8592. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8593. (setq tagsmatcher
  8594. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8595. ;; Make the todo matcher
  8596. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8597. (setq todomatcher t)
  8598. (setq orterms (org-split-string todomatch "|") orlist nil)
  8599. (while (setq term (pop orterms))
  8600. (while (string-match re term)
  8601. (setq minus (and (match-end 1)
  8602. (equal (match-string 1 term) "-"))
  8603. kwd (match-string 2 term)
  8604. re-p (equal (string-to-char kwd) ?{)
  8605. term (substring term (match-end 0))
  8606. mm (if re-p
  8607. `(string-match ,(substring kwd 1 -1) todo)
  8608. (list 'equal 'todo kwd))
  8609. mm (if minus (list 'not mm) mm))
  8610. (push mm todomatcher))
  8611. (push (if (> (length todomatcher) 1)
  8612. (cons 'and todomatcher)
  8613. (car todomatcher))
  8614. orlist)
  8615. (setq todomatcher nil))
  8616. (setq todomatcher (if (> (length orlist) 1)
  8617. (cons 'or orlist) (car orlist))))
  8618. ;; Return the string and lisp forms of the matcher
  8619. (setq matcher (if todomatcher
  8620. (list 'and tagsmatcher todomatcher)
  8621. tagsmatcher))
  8622. (cons match0 matcher)))
  8623. (defun org-op-to-function (op &optional stringp)
  8624. "Turn an operator into the appropriate function."
  8625. (setq op
  8626. (cond
  8627. ((equal op "<" ) '(< string< org-time<))
  8628. ((equal op ">" ) '(> org-string> org-time>))
  8629. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8630. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8631. ((member op '("=" "==")) '(= string= org-time=))
  8632. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8633. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8634. (defun org<> (a b) (not (= a b)))
  8635. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8636. (defun org-string>= (a b) (not (string< a b)))
  8637. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8638. (defun org-string<> (a b) (not (string= a b)))
  8639. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8640. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8641. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8642. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8643. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8644. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8645. (defun org-2ft (s)
  8646. "Convert S to a floating point time.
  8647. If S is already a number, just return it. If it is a string, parse
  8648. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8649. (cond
  8650. ((numberp s) s)
  8651. ((stringp s)
  8652. (condition-case nil
  8653. (float-time (apply 'encode-time (org-parse-time-string s)))
  8654. (error 0.)))
  8655. (t 0.)))
  8656. (defun org-time-today ()
  8657. "Time in seconds today at 0:00.
  8658. Returns the float number of seconds since the beginning of the
  8659. epoch to the beginning of today (00:00)."
  8660. (float-time (apply 'encode-time
  8661. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  8662. (defun org-matcher-time (s)
  8663. "Interpret a time comparison value."
  8664. (save-match-data
  8665. (cond
  8666. ((string= s "<now>") (float-time))
  8667. ((string= s "<today>") (org-time-today))
  8668. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  8669. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  8670. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  8671. (+ (org-time-today)
  8672. (* (string-to-number (match-string 1 s))
  8673. (cdr (assoc (match-string 2 s)
  8674. '(("d" . 86400.0) ("w" . 604800.0)
  8675. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  8676. (t (org-2ft s)))))
  8677. (defun org-match-any-p (re list)
  8678. "Does re match any element of list?"
  8679. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8680. (delq nil list))
  8681. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  8682. (defvar org-tags-overlay (org-make-overlay 1 1))
  8683. (org-detach-overlay org-tags-overlay)
  8684. (defun org-get-local-tags-at (&optional pos)
  8685. "Get a list of tags defined in the current headline."
  8686. (org-get-tags-at pos 'local))
  8687. (defun org-get-local-tags ()
  8688. "Get a list of tags defined in the current headline."
  8689. (org-get-tags-at nil 'local))
  8690. (defun org-get-tags-at (&optional pos local)
  8691. "Get a list of all headline tags applicable at POS.
  8692. POS defaults to point. If tags are inherited, the list contains
  8693. the targets in the same sequence as the headlines appear, i.e.
  8694. the tags of the current headline come last.
  8695. When LOCAL is non-nil, only return tags from the current headline,
  8696. ignore inherited ones."
  8697. (interactive)
  8698. (let (tags ltags lastpos parent)
  8699. (save-excursion
  8700. (save-restriction
  8701. (widen)
  8702. (goto-char (or pos (point)))
  8703. (save-match-data
  8704. (catch 'done
  8705. (condition-case nil
  8706. (progn
  8707. (org-back-to-heading t)
  8708. (while (not (equal lastpos (point)))
  8709. (setq lastpos (point))
  8710. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8711. (setq ltags (org-split-string
  8712. (org-match-string-no-properties 1) ":"))
  8713. (when parent
  8714. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  8715. (setq tags (append
  8716. (if parent
  8717. (org-remove-uniherited-tags ltags)
  8718. ltags)
  8719. tags)))
  8720. (or org-use-tag-inheritance (throw 'done t))
  8721. (if local (throw 'done t))
  8722. (org-up-heading-all 1)
  8723. (setq parent t)))
  8724. (error nil)))))
  8725. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8726. (defun org-add-prop-inherited (s)
  8727. (add-text-properties 0 (length s) '(inherited t) s)
  8728. s)
  8729. (defun org-toggle-tag (tag &optional onoff)
  8730. "Toggle the tag TAG for the current line.
  8731. If ONOFF is `on' or `off', don't toggle but set to this state."
  8732. (let (res current)
  8733. (save-excursion
  8734. (org-back-to-heading t)
  8735. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8736. (point-at-eol) t)
  8737. (progn
  8738. (setq current (match-string 1))
  8739. (replace-match ""))
  8740. (setq current ""))
  8741. (setq current (nreverse (org-split-string current ":")))
  8742. (cond
  8743. ((eq onoff 'on)
  8744. (setq res t)
  8745. (or (member tag current) (push tag current)))
  8746. ((eq onoff 'off)
  8747. (or (not (member tag current)) (setq current (delete tag current))))
  8748. (t (if (member tag current)
  8749. (setq current (delete tag current))
  8750. (setq res t)
  8751. (push tag current))))
  8752. (end-of-line 1)
  8753. (if current
  8754. (progn
  8755. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8756. (org-set-tags nil t))
  8757. (delete-horizontal-space))
  8758. (run-hooks 'org-after-tags-change-hook))
  8759. res))
  8760. (defun org-align-tags-here (to-col)
  8761. ;; Assumes that this is a headline
  8762. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8763. (beginning-of-line 1)
  8764. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8765. (< pos (match-beginning 2)))
  8766. (progn
  8767. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8768. (goto-char (match-beginning 1))
  8769. (insert " ")
  8770. (delete-region (point) (1+ (match-beginning 2)))
  8771. (setq ncol (max (1+ (current-column))
  8772. (1+ col)
  8773. (if (> to-col 0)
  8774. to-col
  8775. (- (abs to-col) tags-l))))
  8776. (setq p (point))
  8777. (insert (make-string (- ncol (current-column)) ?\ ))
  8778. (setq ncol (current-column))
  8779. (when indent-tabs-mode (tabify p (point-at-eol)))
  8780. (org-move-to-column (min ncol col) t))
  8781. (goto-char pos))))
  8782. (defun org-set-tags-command (&optional arg just-align)
  8783. "Call the set-tags command for the current entry."
  8784. (interactive "P")
  8785. (if (org-on-heading-p)
  8786. (org-set-tags arg just-align)
  8787. (save-excursion
  8788. (org-back-to-heading t)
  8789. (org-set-tags arg just-align))))
  8790. (defun org-set-tags (&optional arg just-align)
  8791. "Set the tags for the current headline.
  8792. With prefix ARG, realign all tags in headings in the current buffer."
  8793. (interactive "P")
  8794. (let* ((re (concat "^" outline-regexp))
  8795. (current (org-get-tags-string))
  8796. (col (current-column))
  8797. (org-setting-tags t)
  8798. table current-tags inherited-tags ; computed below when needed
  8799. tags p0 c0 c1 rpl)
  8800. (if arg
  8801. (save-excursion
  8802. (goto-char (point-min))
  8803. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8804. (while (re-search-forward re nil t)
  8805. (org-set-tags nil t)
  8806. (end-of-line 1)))
  8807. (message "All tags realigned to column %d" org-tags-column))
  8808. (if just-align
  8809. (setq tags current)
  8810. ;; Get a new set of tags from the user
  8811. (save-excursion
  8812. (setq table (or org-tag-alist (org-get-buffer-tags))
  8813. org-last-tags-completion-table table
  8814. current-tags (org-split-string current ":")
  8815. inherited-tags (nreverse
  8816. (nthcdr (length current-tags)
  8817. (nreverse (org-get-tags-at))))
  8818. tags
  8819. (if (or (eq t org-use-fast-tag-selection)
  8820. (and org-use-fast-tag-selection
  8821. (delq nil (mapcar 'cdr table))))
  8822. (org-fast-tag-selection
  8823. current-tags inherited-tags table
  8824. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8825. (let ((org-add-colon-after-tag-completion t))
  8826. (org-trim
  8827. (org-without-partial-completion
  8828. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  8829. nil nil current 'org-tags-history)))))))
  8830. (while (string-match "[-+&]+" tags)
  8831. ;; No boolean logic, just a list
  8832. (setq tags (replace-match ":" t t tags))))
  8833. (if (string-match "\\`[\t ]*\\'" tags)
  8834. (setq tags "")
  8835. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8836. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8837. ;; Insert new tags at the correct column
  8838. (beginning-of-line 1)
  8839. (cond
  8840. ((and (equal current "") (equal tags "")))
  8841. ((re-search-forward
  8842. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8843. (point-at-eol) t)
  8844. (if (equal tags "")
  8845. (setq rpl "")
  8846. (goto-char (match-beginning 0))
  8847. (setq c0 (current-column) p0 (point)
  8848. c1 (max (1+ c0) (if (> org-tags-column 0)
  8849. org-tags-column
  8850. (- (- org-tags-column) (length tags))))
  8851. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8852. (replace-match rpl t t)
  8853. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8854. tags)
  8855. (t (error "Tags alignment failed")))
  8856. (org-move-to-column col)
  8857. (unless just-align
  8858. (run-hooks 'org-after-tags-change-hook)))))
  8859. (defun org-change-tag-in-region (beg end tag off)
  8860. "Add or remove TAG for each entry in the region.
  8861. This works in the agenda, and also in an org-mode buffer."
  8862. (interactive
  8863. (list (region-beginning) (region-end)
  8864. (let ((org-last-tags-completion-table
  8865. (if (org-mode-p)
  8866. (org-get-buffer-tags)
  8867. (org-global-tags-completion-table))))
  8868. (org-ido-completing-read
  8869. "Tag: " 'org-tags-completion-function nil nil nil
  8870. 'org-tags-history))
  8871. (progn
  8872. (message "[s]et or [r]emove? ")
  8873. (equal (read-char-exclusive) ?r))))
  8874. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8875. (let ((agendap (equal major-mode 'org-agenda-mode))
  8876. l1 l2 m buf pos newhead (cnt 0))
  8877. (goto-char end)
  8878. (setq l2 (1- (org-current-line)))
  8879. (goto-char beg)
  8880. (setq l1 (org-current-line))
  8881. (loop for l from l1 to l2 do
  8882. (goto-line l)
  8883. (setq m (get-text-property (point) 'org-hd-marker))
  8884. (when (or (and (org-mode-p) (org-on-heading-p))
  8885. (and agendap m))
  8886. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8887. pos (if agendap m (point)))
  8888. (with-current-buffer buf
  8889. (save-excursion
  8890. (save-restriction
  8891. (goto-char pos)
  8892. (setq cnt (1+ cnt))
  8893. (org-toggle-tag tag (if off 'off 'on))
  8894. (setq newhead (org-get-heading)))))
  8895. (and agendap (org-agenda-change-all-lines newhead m))))
  8896. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8897. (defun org-tags-completion-function (string predicate &optional flag)
  8898. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8899. (confirm (lambda (x) (stringp (car x)))))
  8900. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8901. (setq s1 (match-string 1 string)
  8902. s2 (match-string 2 string))
  8903. (setq s1 "" s2 string))
  8904. (cond
  8905. ((eq flag nil)
  8906. ;; try completion
  8907. (setq rtn (try-completion s2 ctable confirm))
  8908. (if (stringp rtn)
  8909. (setq rtn
  8910. (concat s1 s2 (substring rtn (length s2))
  8911. (if (and org-add-colon-after-tag-completion
  8912. (assoc rtn ctable))
  8913. ":" ""))))
  8914. rtn)
  8915. ((eq flag t)
  8916. ;; all-completions
  8917. (all-completions s2 ctable confirm)
  8918. )
  8919. ((eq flag 'lambda)
  8920. ;; exact match?
  8921. (assoc s2 ctable)))
  8922. ))
  8923. (defun org-fast-tag-insert (kwd tags face &optional end)
  8924. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  8925. (insert (format "%-12s" (concat kwd ":"))
  8926. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8927. (or end "")))
  8928. (defun org-fast-tag-show-exit (flag)
  8929. (save-excursion
  8930. (goto-line 3)
  8931. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8932. (replace-match ""))
  8933. (when flag
  8934. (end-of-line 1)
  8935. (org-move-to-column (- (window-width) 19) t)
  8936. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8937. (defun org-set-current-tags-overlay (current prefix)
  8938. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8939. (if (featurep 'xemacs)
  8940. (org-overlay-display org-tags-overlay (concat prefix s)
  8941. 'secondary-selection)
  8942. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8943. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8944. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8945. "Fast tag selection with single keys.
  8946. CURRENT is the current list of tags in the headline, INHERITED is the
  8947. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8948. possibly with grouping information. TODO-TABLE is a similar table with
  8949. TODO keywords, should these have keys assigned to them.
  8950. If the keys are nil, a-z are automatically assigned.
  8951. Returns the new tags string, or nil to not change the current settings."
  8952. (let* ((fulltable (append table todo-table))
  8953. (maxlen (apply 'max (mapcar
  8954. (lambda (x)
  8955. (if (stringp (car x)) (string-width (car x)) 0))
  8956. fulltable)))
  8957. (buf (current-buffer))
  8958. (expert (eq org-fast-tag-selection-single-key 'expert))
  8959. (buffer-tags nil)
  8960. (fwidth (+ maxlen 3 1 3))
  8961. (ncol (/ (- (window-width) 4) fwidth))
  8962. (i-face 'org-done)
  8963. (c-face 'org-todo)
  8964. tg cnt e c char c1 c2 ntable tbl rtn
  8965. ov-start ov-end ov-prefix
  8966. (exit-after-next org-fast-tag-selection-single-key)
  8967. (done-keywords org-done-keywords)
  8968. groups ingroup)
  8969. (save-excursion
  8970. (beginning-of-line 1)
  8971. (if (looking-at
  8972. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8973. (setq ov-start (match-beginning 1)
  8974. ov-end (match-end 1)
  8975. ov-prefix "")
  8976. (setq ov-start (1- (point-at-eol))
  8977. ov-end (1+ ov-start))
  8978. (skip-chars-forward "^\n\r")
  8979. (setq ov-prefix
  8980. (concat
  8981. (buffer-substring (1- (point)) (point))
  8982. (if (> (current-column) org-tags-column)
  8983. " "
  8984. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8985. (org-move-overlay org-tags-overlay ov-start ov-end)
  8986. (save-window-excursion
  8987. (if expert
  8988. (set-buffer (get-buffer-create " *Org tags*"))
  8989. (delete-other-windows)
  8990. (split-window-vertically)
  8991. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8992. (erase-buffer)
  8993. (org-set-local 'org-done-keywords done-keywords)
  8994. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8995. (org-fast-tag-insert "Current" current c-face "\n\n")
  8996. (org-fast-tag-show-exit exit-after-next)
  8997. (org-set-current-tags-overlay current ov-prefix)
  8998. (setq tbl fulltable char ?a cnt 0)
  8999. (while (setq e (pop tbl))
  9000. (cond
  9001. ((equal e '(:startgroup))
  9002. (push '() groups) (setq ingroup t)
  9003. (when (not (= cnt 0))
  9004. (setq cnt 0)
  9005. (insert "\n"))
  9006. (insert "{ "))
  9007. ((equal e '(:endgroup))
  9008. (setq ingroup nil cnt 0)
  9009. (insert "}\n"))
  9010. (t
  9011. (setq tg (car e) c2 nil)
  9012. (if (cdr e)
  9013. (setq c (cdr e))
  9014. ;; automatically assign a character.
  9015. (setq c1 (string-to-char
  9016. (downcase (substring
  9017. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9018. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9019. (while (or (rassoc char ntable) (rassoc char table))
  9020. (setq char (1+ char)))
  9021. (setq c2 c1))
  9022. (setq c (or c2 char)))
  9023. (if ingroup (push tg (car groups)))
  9024. (setq tg (org-add-props tg nil 'face
  9025. (cond
  9026. ((not (assoc tg table))
  9027. (org-get-todo-face tg))
  9028. ((member tg current) c-face)
  9029. ((member tg inherited) i-face)
  9030. (t nil))))
  9031. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9032. (insert "[" c "] " tg (make-string
  9033. (- fwidth 4 (length tg)) ?\ ))
  9034. (push (cons tg c) ntable)
  9035. (when (= (setq cnt (1+ cnt)) ncol)
  9036. (insert "\n")
  9037. (if ingroup (insert " "))
  9038. (setq cnt 0)))))
  9039. (setq ntable (nreverse ntable))
  9040. (insert "\n")
  9041. (goto-char (point-min))
  9042. (if (not expert) (org-fit-window-to-buffer))
  9043. (setq rtn
  9044. (catch 'exit
  9045. (while t
  9046. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9047. (if groups " [!] no groups" " [!]groups")
  9048. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9049. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9050. (cond
  9051. ((= c ?\r) (throw 'exit t))
  9052. ((= c ?!)
  9053. (setq groups (not groups))
  9054. (goto-char (point-min))
  9055. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9056. ((= c ?\C-c)
  9057. (if (not expert)
  9058. (org-fast-tag-show-exit
  9059. (setq exit-after-next (not exit-after-next)))
  9060. (setq expert nil)
  9061. (delete-other-windows)
  9062. (split-window-vertically)
  9063. (org-switch-to-buffer-other-window " *Org tags*")
  9064. (org-fit-window-to-buffer)))
  9065. ((or (= c ?\C-g)
  9066. (and (= c ?q) (not (rassoc c ntable))))
  9067. (org-detach-overlay org-tags-overlay)
  9068. (setq quit-flag t))
  9069. ((= c ?\ )
  9070. (setq current nil)
  9071. (if exit-after-next (setq exit-after-next 'now)))
  9072. ((= c ?\t)
  9073. (condition-case nil
  9074. (setq tg (org-ido-completing-read
  9075. "Tag: "
  9076. (or buffer-tags
  9077. (with-current-buffer buf
  9078. (org-get-buffer-tags)))))
  9079. (quit (setq tg "")))
  9080. (when (string-match "\\S-" tg)
  9081. (add-to-list 'buffer-tags (list tg))
  9082. (if (member tg current)
  9083. (setq current (delete tg current))
  9084. (push tg current)))
  9085. (if exit-after-next (setq exit-after-next 'now)))
  9086. ((setq e (rassoc c todo-table) tg (car e))
  9087. (with-current-buffer buf
  9088. (save-excursion (org-todo tg)))
  9089. (if exit-after-next (setq exit-after-next 'now)))
  9090. ((setq e (rassoc c ntable) tg (car e))
  9091. (if (member tg current)
  9092. (setq current (delete tg current))
  9093. (loop for g in groups do
  9094. (if (member tg g)
  9095. (mapc (lambda (x)
  9096. (setq current (delete x current)))
  9097. g)))
  9098. (push tg current))
  9099. (if exit-after-next (setq exit-after-next 'now))))
  9100. ;; Create a sorted list
  9101. (setq current
  9102. (sort current
  9103. (lambda (a b)
  9104. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9105. (if (eq exit-after-next 'now) (throw 'exit t))
  9106. (goto-char (point-min))
  9107. (beginning-of-line 2)
  9108. (delete-region (point) (point-at-eol))
  9109. (org-fast-tag-insert "Current" current c-face)
  9110. (org-set-current-tags-overlay current ov-prefix)
  9111. (while (re-search-forward
  9112. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9113. (setq tg (match-string 1))
  9114. (add-text-properties
  9115. (match-beginning 1) (match-end 1)
  9116. (list 'face
  9117. (cond
  9118. ((member tg current) c-face)
  9119. ((member tg inherited) i-face)
  9120. (t (get-text-property (match-beginning 1) 'face))))))
  9121. (goto-char (point-min)))))
  9122. (org-detach-overlay org-tags-overlay)
  9123. (if rtn
  9124. (mapconcat 'identity current ":")
  9125. nil))))
  9126. (defun org-get-tags-string ()
  9127. "Get the TAGS string in the current headline."
  9128. (unless (org-on-heading-p t)
  9129. (error "Not on a heading"))
  9130. (save-excursion
  9131. (beginning-of-line 1)
  9132. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9133. (org-match-string-no-properties 1)
  9134. "")))
  9135. (defun org-get-tags ()
  9136. "Get the list of tags specified in the current headline."
  9137. (org-split-string (org-get-tags-string) ":"))
  9138. (defun org-get-buffer-tags ()
  9139. "Get a table of all tags used in the buffer, for completion."
  9140. (let (tags)
  9141. (save-excursion
  9142. (goto-char (point-min))
  9143. (while (re-search-forward
  9144. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9145. (when (equal (char-after (point-at-bol 0)) ?*)
  9146. (mapc (lambda (x) (add-to-list 'tags x))
  9147. (org-split-string (org-match-string-no-properties 1) ":")))))
  9148. (mapcar 'list tags)))
  9149. ;;;; The mapping API
  9150. ;;;###autoload
  9151. (defun org-map-entries (func &optional match scope &rest skip)
  9152. "Call FUNC at each headline selected by MATCH in SCOPE.
  9153. FUNC is a function or a lisp form. The function will be called without
  9154. arguments, with the cursor positioned at the beginning of the headline.
  9155. The return values of all calls to the function will be collected and
  9156. returned as a list.
  9157. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9158. Only headlines that are matched by this query will be considered during
  9159. the iteration. When MATCH is nil or t, all headlines will be
  9160. visited by the iteration.
  9161. SCOPE determines the scope of this command. It can be any of:
  9162. nil The current buffer, respecting the restriction if any
  9163. tree The subtree started with the entry at point
  9164. file The current buffer, without restriction
  9165. file-with-archives
  9166. The current buffer, and any archives associated with it
  9167. agenda All agenda files
  9168. agenda-with-archives
  9169. All agenda files with any archive files associated with them
  9170. \(file1 file2 ...)
  9171. If this is a list, all files in the list will be scanned
  9172. The remaining args are treated as settings for the skipping facilities of
  9173. the scanner. The following items can be given here:
  9174. archive skip trees with the archive tag.
  9175. comment skip trees with the COMMENT keyword
  9176. function or Emacs Lisp form:
  9177. will be used as value for `org-agenda-skip-function', so whenever
  9178. the the function returns t, FUNC will not be called for that
  9179. entry and search will continue from the point where the
  9180. function leaves it."
  9181. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9182. (org-agenda-skip-archived-trees (memq 'archive skip))
  9183. (org-agenda-skip-comment-trees (memq 'comment skip))
  9184. (org-agenda-skip-function
  9185. (car (org-delete-all '(comment archive) skip)))
  9186. (org-tags-match-list-sublevels t)
  9187. matcher pos file res
  9188. org-todo-keywords-for-agenda
  9189. org-done-keywords-for-agenda
  9190. org-todo-keyword-alist-for-agenda
  9191. org-tag-alist-for-agenda)
  9192. (cond
  9193. ((eq match t) (setq matcher t))
  9194. ((eq match nil) (setq matcher t))
  9195. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9196. (when (eq scope 'tree)
  9197. (org-back-to-heading t)
  9198. (org-narrow-to-subtree)
  9199. (setq scope nil))
  9200. (if (not scope)
  9201. (progn
  9202. (org-prepare-agenda-buffers
  9203. (list (buffer-file-name (current-buffer))))
  9204. (org-scan-tags func matcher))
  9205. ;; Get the right scope
  9206. (setq pos (point))
  9207. (cond
  9208. ((and scope (listp scope) (symbolp (car scope)))
  9209. (setq scope (eval scope)))
  9210. ((eq scope 'agenda)
  9211. (setq scope (org-agenda-files t)))
  9212. ((eq scope 'agenda-with-archives)
  9213. (setq scope (org-agenda-files t))
  9214. (setq scope (org-add-archive-files scope)))
  9215. ((eq scope 'file)
  9216. (setq scope (list (buffer-file-name))))
  9217. ((eq scope 'file-with-archives)
  9218. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9219. (org-prepare-agenda-buffers scope)
  9220. (while (setq file (pop scope))
  9221. (with-current-buffer (org-find-base-buffer-visiting file)
  9222. (save-excursion
  9223. (save-restriction
  9224. (widen)
  9225. (goto-char (point-min))
  9226. (setq res (append res (org-scan-tags func matcher)))))))
  9227. res)))
  9228. ;;;; Properties
  9229. ;;; Setting and retrieving properties
  9230. (defconst org-special-properties
  9231. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9232. "TIMESTAMP" "TIMESTAMP_IA")
  9233. "The special properties valid in Org-mode.
  9234. These are properties that are not defined in the property drawer,
  9235. but in some other way.")
  9236. (defconst org-default-properties
  9237. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9238. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9239. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9240. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9241. "Some properties that are used by Org-mode for various purposes.
  9242. Being in this list makes sure that they are offered for completion.")
  9243. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9244. "Regular expression matching the first line of a property drawer.")
  9245. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9246. "Regular expression matching the first line of a property drawer.")
  9247. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9248. "Regular expression matching the first line of a property drawer.")
  9249. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9250. "Regular expression matching the first line of a property drawer.")
  9251. (defconst org-property-drawer-re
  9252. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9253. org-property-end-re "\\)\n?")
  9254. "Matches an entire property drawer.")
  9255. (defconst org-clock-drawer-re
  9256. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9257. org-property-end-re "\\)\n?")
  9258. "Matches an entire clock drawer.")
  9259. (defun org-property-action ()
  9260. "Do an action on properties."
  9261. (interactive)
  9262. (let (c)
  9263. (org-at-property-p)
  9264. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9265. (setq c (read-char-exclusive))
  9266. (cond
  9267. ((equal c ?s)
  9268. (call-interactively 'org-set-property))
  9269. ((equal c ?d)
  9270. (call-interactively 'org-delete-property))
  9271. ((equal c ?D)
  9272. (call-interactively 'org-delete-property-globally))
  9273. ((equal c ?c)
  9274. (call-interactively 'org-compute-property-at-point))
  9275. (t (error "No such property action %c" c)))))
  9276. (defun org-at-property-p ()
  9277. "Is the cursor in a property line?"
  9278. ;; FIXME: Does not check if we are actually in the drawer.
  9279. ;; FIXME: also returns true on any drawers.....
  9280. ;; This is used by C-c C-c for property action.
  9281. (save-excursion
  9282. (beginning-of-line 1)
  9283. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9284. (defun org-get-property-block (&optional beg end force)
  9285. "Return the (beg . end) range of the body of the property drawer.
  9286. BEG and END can be beginning and end of subtree, if not given
  9287. they will be found.
  9288. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9289. (catch 'exit
  9290. (save-excursion
  9291. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9292. (end (or end (progn (outline-next-heading) (point)))))
  9293. (goto-char beg)
  9294. (if (re-search-forward org-property-start-re end t)
  9295. (setq beg (1+ (match-end 0)))
  9296. (if force
  9297. (save-excursion
  9298. (org-insert-property-drawer)
  9299. (setq end (progn (outline-next-heading) (point))))
  9300. (throw 'exit nil))
  9301. (goto-char beg)
  9302. (if (re-search-forward org-property-start-re end t)
  9303. (setq beg (1+ (match-end 0)))))
  9304. (if (re-search-forward org-property-end-re end t)
  9305. (setq end (match-beginning 0))
  9306. (or force (throw 'exit nil))
  9307. (goto-char beg)
  9308. (setq end beg)
  9309. (org-indent-line-function)
  9310. (insert ":END:\n"))
  9311. (cons beg end)))))
  9312. (defun org-entry-properties (&optional pom which)
  9313. "Get all properties of the entry at point-or-marker POM.
  9314. This includes the TODO keyword, the tags, time strings for deadline,
  9315. scheduled, and clocking, and any additional properties defined in the
  9316. entry. The return value is an alist, keys may occur multiple times
  9317. if the property key was used several times.
  9318. POM may also be nil, in which case the current entry is used.
  9319. If WHICH is nil or `all', get all properties. If WHICH is
  9320. `special' or `standard', only get that subclass."
  9321. (setq which (or which 'all))
  9322. (org-with-point-at pom
  9323. (let ((clockstr (substring org-clock-string 0 -1))
  9324. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9325. beg end range props sum-props key value string clocksum)
  9326. (save-excursion
  9327. (when (condition-case nil (org-back-to-heading t) (error nil))
  9328. (setq beg (point))
  9329. (setq sum-props (get-text-property (point) 'org-summaries))
  9330. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9331. (outline-next-heading)
  9332. (setq end (point))
  9333. (when (memq which '(all special))
  9334. ;; Get the special properties, like TODO and tags
  9335. (goto-char beg)
  9336. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9337. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9338. (when (looking-at org-priority-regexp)
  9339. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9340. (when (and (setq value (org-get-tags-string))
  9341. (string-match "\\S-" value))
  9342. (push (cons "TAGS" value) props))
  9343. (when (setq value (org-get-tags-at))
  9344. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9345. props))
  9346. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9347. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9348. string (if (equal key clockstr)
  9349. (org-no-properties
  9350. (org-trim
  9351. (buffer-substring
  9352. (match-beginning 3) (goto-char (point-at-eol)))))
  9353. (substring (org-match-string-no-properties 3) 1 -1)))
  9354. (unless key
  9355. (if (= (char-after (match-beginning 3)) ?\[)
  9356. (setq key "TIMESTAMP_IA")
  9357. (setq key "TIMESTAMP")))
  9358. (when (or (equal key clockstr) (not (assoc key props)))
  9359. (push (cons key string) props)))
  9360. )
  9361. (when (memq which '(all standard))
  9362. ;; Get the standard properties, like :PORP: ...
  9363. (setq range (org-get-property-block beg end))
  9364. (when range
  9365. (goto-char (car range))
  9366. (while (re-search-forward
  9367. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9368. (cdr range) t)
  9369. (setq key (org-match-string-no-properties 1)
  9370. value (org-trim (or (org-match-string-no-properties 2) "")))
  9371. (unless (member key excluded)
  9372. (push (cons key (or value "")) props)))))
  9373. (if clocksum
  9374. (push (cons "CLOCKSUM"
  9375. (org-columns-number-to-string (/ (float clocksum) 60.)
  9376. 'add_times))
  9377. props))
  9378. (unless (assoc "CATEGORY" props)
  9379. (setq value (or (org-get-category)
  9380. (progn (org-refresh-category-properties)
  9381. (org-get-category))))
  9382. (push (cons "CATEGORY" value) props))
  9383. (append sum-props (nreverse props)))))))
  9384. (defun org-entry-get (pom property &optional inherit)
  9385. "Get value of PROPERTY for entry at point-or-marker POM.
  9386. If INHERIT is non-nil and the entry does not have the property,
  9387. then also check higher levels of the hierarchy.
  9388. If INHERIT is the symbol `selective', use inheritance only if the setting
  9389. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9390. If the property is present but empty, the return value is the empty string.
  9391. If the property is not present at all, nil is returned."
  9392. (org-with-point-at pom
  9393. (if (and inherit (if (eq inherit 'selective)
  9394. (org-property-inherit-p property)
  9395. t))
  9396. (org-entry-get-with-inheritance property)
  9397. (if (member property org-special-properties)
  9398. ;; We need a special property. Use brute force, get all properties.
  9399. (cdr (assoc property (org-entry-properties nil 'special)))
  9400. (let ((range (org-get-property-block)))
  9401. (if (and range
  9402. (goto-char (car range))
  9403. (re-search-forward
  9404. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9405. (cdr range) t))
  9406. ;; Found the property, return it.
  9407. (if (match-end 1)
  9408. (org-match-string-no-properties 1)
  9409. "")))))))
  9410. (defun org-property-or-variable-value (var &optional inherit)
  9411. "Check if there is a property fixing the value of VAR.
  9412. If yes, return this value. If not, return the current value of the variable."
  9413. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9414. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9415. (read prop)
  9416. (symbol-value var))))
  9417. (defun org-entry-delete (pom property)
  9418. "Delete the property PROPERTY from entry at point-or-marker POM."
  9419. (org-with-point-at pom
  9420. (if (member property org-special-properties)
  9421. nil ; cannot delete these properties.
  9422. (let ((range (org-get-property-block)))
  9423. (if (and range
  9424. (goto-char (car range))
  9425. (re-search-forward
  9426. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9427. (cdr range) t))
  9428. (progn
  9429. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9430. t)
  9431. nil)))))
  9432. ;; Multi-values properties are properties that contain multiple values
  9433. ;; These values are assumed to be single words, separated by whitespace.
  9434. (defun org-entry-add-to-multivalued-property (pom property value)
  9435. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9436. (let* ((old (org-entry-get pom property))
  9437. (values (and old (org-split-string old "[ \t]"))))
  9438. (setq value (org-entry-protect-space value))
  9439. (unless (member value values)
  9440. (setq values (cons value values))
  9441. (org-entry-put pom property
  9442. (mapconcat 'identity values " ")))))
  9443. (defun org-entry-remove-from-multivalued-property (pom property value)
  9444. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9445. (let* ((old (org-entry-get pom property))
  9446. (values (and old (org-split-string old "[ \t]"))))
  9447. (setq value (org-entry-protect-space value))
  9448. (when (member value values)
  9449. (setq values (delete value values))
  9450. (org-entry-put pom property
  9451. (mapconcat 'identity values " ")))))
  9452. (defun org-entry-member-in-multivalued-property (pom property value)
  9453. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9454. (let* ((old (org-entry-get pom property))
  9455. (values (and old (org-split-string old "[ \t]"))))
  9456. (setq value (org-entry-protect-space value))
  9457. (member value values)))
  9458. (defun org-entry-get-multivalued-property (pom property)
  9459. "Return a list of values in a multivalued property."
  9460. (let* ((value (org-entry-get pom property))
  9461. (values (and value (org-split-string value "[ \t]"))))
  9462. (mapcar 'org-entry-restore-space values)))
  9463. (defun org-entry-put-multivalued-property (pom property &rest values)
  9464. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9465. VALUES should be a list of strings. Spaces will be protected."
  9466. (org-entry-put pom property
  9467. (mapconcat 'org-entry-protect-space values " "))
  9468. (let* ((value (org-entry-get pom property))
  9469. (values (and value (org-split-string value "[ \t]"))))
  9470. (mapcar 'org-entry-restore-space values)))
  9471. (defun org-entry-protect-space (s)
  9472. "Protect spaces and newline in string S."
  9473. (while (string-match " " s)
  9474. (setq s (replace-match "%20" t t s)))
  9475. (while (string-match "\n" s)
  9476. (setq s (replace-match "%0A" t t s)))
  9477. s)
  9478. (defun org-entry-restore-space (s)
  9479. "Restore spaces and newline in string S."
  9480. (while (string-match "%20" s)
  9481. (setq s (replace-match " " t t s)))
  9482. (while (string-match "%0A" s)
  9483. (setq s (replace-match "\n" t t s)))
  9484. s)
  9485. (defvar org-entry-property-inherited-from (make-marker)
  9486. "Marker pointing to the entry from where a property was inherited.
  9487. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9488. location of the entry where the inheritance search matched. If there was
  9489. no match, the marker will point nowhere.
  9490. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9491. is set.")
  9492. (defun org-entry-get-with-inheritance (property)
  9493. "Get entry property, and search higher levels if not present."
  9494. (move-marker org-entry-property-inherited-from nil)
  9495. (let (tmp)
  9496. (save-excursion
  9497. (save-restriction
  9498. (widen)
  9499. (catch 'ex
  9500. (while t
  9501. (when (setq tmp (org-entry-get nil property))
  9502. (org-back-to-heading t)
  9503. (move-marker org-entry-property-inherited-from (point))
  9504. (throw 'ex tmp))
  9505. (or (org-up-heading-safe) (throw 'ex nil)))))
  9506. (or tmp
  9507. (cdr (assoc property org-file-properties))
  9508. (cdr (assoc property org-global-properties))
  9509. (cdr (assoc property org-global-properties-fixed))))))
  9510. (defun org-entry-put (pom property value)
  9511. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9512. (org-with-point-at pom
  9513. (org-back-to-heading t)
  9514. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9515. range)
  9516. (cond
  9517. ((equal property "TODO")
  9518. (when (and (stringp value) (string-match "\\S-" value)
  9519. (not (member value org-todo-keywords-1)))
  9520. (error "\"%s\" is not a valid TODO state" value))
  9521. (if (or (not value)
  9522. (not (string-match "\\S-" value)))
  9523. (setq value 'none))
  9524. (org-todo value)
  9525. (org-set-tags nil 'align))
  9526. ((equal property "PRIORITY")
  9527. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9528. (string-to-char value) ?\ ))
  9529. (org-set-tags nil 'align))
  9530. ((equal property "SCHEDULED")
  9531. (if (re-search-forward org-scheduled-time-regexp end t)
  9532. (cond
  9533. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9534. ((eq value 'later) (org-timestamp-change 1 'day))
  9535. (t (call-interactively 'org-schedule)))
  9536. (call-interactively 'org-schedule)))
  9537. ((equal property "DEADLINE")
  9538. (if (re-search-forward org-deadline-time-regexp end t)
  9539. (cond
  9540. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9541. ((eq value 'later) (org-timestamp-change 1 'day))
  9542. (t (call-interactively 'org-deadline)))
  9543. (call-interactively 'org-deadline)))
  9544. ((member property org-special-properties)
  9545. (error "The %s property can not yet be set with `org-entry-put'"
  9546. property))
  9547. (t ; a non-special property
  9548. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9549. (setq range (org-get-property-block beg end 'force))
  9550. (goto-char (car range))
  9551. (if (re-search-forward
  9552. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9553. (progn
  9554. (delete-region (match-beginning 1) (match-end 1))
  9555. (goto-char (match-beginning 1)))
  9556. (goto-char (cdr range))
  9557. (insert "\n")
  9558. (backward-char 1)
  9559. (org-indent-line-function)
  9560. (insert ":" property ":"))
  9561. (and value (insert " " value))
  9562. (org-indent-line-function)))))))
  9563. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9564. "Get all property keys in the current buffer.
  9565. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9566. like tags and TODO state.
  9567. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9568. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9569. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9570. formats in the current buffer."
  9571. (let (rtn range cfmt cols s p)
  9572. (save-excursion
  9573. (save-restriction
  9574. (widen)
  9575. (goto-char (point-min))
  9576. (while (re-search-forward org-property-start-re nil t)
  9577. (setq range (org-get-property-block))
  9578. (goto-char (car range))
  9579. (while (re-search-forward
  9580. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9581. (cdr range) t)
  9582. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9583. (outline-next-heading))))
  9584. (when include-specials
  9585. (setq rtn (append org-special-properties rtn)))
  9586. (when include-defaults
  9587. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9588. (when include-columns
  9589. (save-excursion
  9590. (save-restriction
  9591. (widen)
  9592. (goto-char (point-min))
  9593. (while (re-search-forward
  9594. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9595. nil t)
  9596. (setq cfmt (match-string 2) s 0)
  9597. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9598. cfmt s)
  9599. (setq s (match-end 0)
  9600. p (match-string 1 cfmt))
  9601. (unless (or (equal p "ITEM")
  9602. (member p org-special-properties))
  9603. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9604. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9605. (defun org-property-values (key)
  9606. "Return a list of all values of property KEY."
  9607. (save-excursion
  9608. (save-restriction
  9609. (widen)
  9610. (goto-char (point-min))
  9611. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9612. values)
  9613. (while (re-search-forward re nil t)
  9614. (add-to-list 'values (org-trim (match-string 1))))
  9615. (delete "" values)))))
  9616. (defun org-insert-property-drawer ()
  9617. "Insert a property drawer into the current entry."
  9618. (interactive)
  9619. (org-back-to-heading t)
  9620. (looking-at outline-regexp)
  9621. (let ((indent (- (match-end 0)(match-beginning 0)))
  9622. (beg (point))
  9623. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9624. end hiddenp)
  9625. (outline-next-heading)
  9626. (setq end (point))
  9627. (goto-char beg)
  9628. (while (re-search-forward re end t))
  9629. (setq hiddenp (org-invisible-p))
  9630. (end-of-line 1)
  9631. (and (equal (char-after) ?\n) (forward-char 1))
  9632. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9633. (beginning-of-line 2))
  9634. (org-skip-over-state-notes)
  9635. (skip-chars-backward " \t\n\r")
  9636. (if (eq (char-before) ?*) (forward-char 1))
  9637. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9638. (beginning-of-line 0)
  9639. (org-indent-to-column indent)
  9640. (beginning-of-line 2)
  9641. (org-indent-to-column indent)
  9642. (beginning-of-line 0)
  9643. (if hiddenp
  9644. (save-excursion
  9645. (org-back-to-heading t)
  9646. (hide-entry))
  9647. (org-flag-drawer t))))
  9648. (defun org-set-property (property value)
  9649. "In the current entry, set PROPERTY to VALUE.
  9650. When called interactively, this will prompt for a property name, offering
  9651. completion on existing and default properties. And then it will prompt
  9652. for a value, offering completion either on allowed values (via an inherited
  9653. xxx_ALL property) or on existing values in other instances of this property
  9654. in the current file."
  9655. (interactive
  9656. (let* ((completion-ignore-case t)
  9657. (keys (org-buffer-property-keys nil t t))
  9658. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  9659. (prop (if (member prop0 keys)
  9660. prop0
  9661. (or (cdr (assoc (downcase prop0)
  9662. (mapcar (lambda (x) (cons (downcase x) x))
  9663. keys)))
  9664. prop0)))
  9665. (cur (org-entry-get nil prop))
  9666. (allowed (org-property-get-allowed-values nil prop 'table))
  9667. (existing (mapcar 'list (org-property-values prop)))
  9668. (val (if allowed
  9669. (org-completing-read "Value: " allowed nil 'req-match)
  9670. (org-completing-read
  9671. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9672. (concat "[" cur "]") "")
  9673. ": ")
  9674. existing nil nil "" nil cur))))
  9675. (list prop (if (equal val "") cur val))))
  9676. (unless (equal (org-entry-get nil property) value)
  9677. (org-entry-put nil property value)))
  9678. (defun org-delete-property (property)
  9679. "In the current entry, delete PROPERTY."
  9680. (interactive
  9681. (let* ((completion-ignore-case t)
  9682. (prop (org-ido-completing-read
  9683. "Property: " (org-entry-properties nil 'standard))))
  9684. (list prop)))
  9685. (message "Property %s %s" property
  9686. (if (org-entry-delete nil property)
  9687. "deleted"
  9688. "was not present in the entry")))
  9689. (defun org-delete-property-globally (property)
  9690. "Remove PROPERTY globally, from all entries."
  9691. (interactive
  9692. (let* ((completion-ignore-case t)
  9693. (prop (org-ido-completing-read
  9694. "Globally remove property: "
  9695. (mapcar 'list (org-buffer-property-keys)))))
  9696. (list prop)))
  9697. (save-excursion
  9698. (save-restriction
  9699. (widen)
  9700. (goto-char (point-min))
  9701. (let ((cnt 0))
  9702. (while (re-search-forward
  9703. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9704. nil t)
  9705. (setq cnt (1+ cnt))
  9706. (replace-match ""))
  9707. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9708. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9709. (defun org-compute-property-at-point ()
  9710. "Compute the property at point.
  9711. This looks for an enclosing column format, extracts the operator and
  9712. then applies it to the property in the column format's scope."
  9713. (interactive)
  9714. (unless (org-at-property-p)
  9715. (error "Not at a property"))
  9716. (let ((prop (org-match-string-no-properties 2)))
  9717. (org-columns-get-format-and-top-level)
  9718. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9719. (error "No operator defined for property %s" prop))
  9720. (org-columns-compute prop)))
  9721. (defun org-property-get-allowed-values (pom property &optional table)
  9722. "Get allowed values for the property PROPERTY.
  9723. When TABLE is non-nil, return an alist that can directly be used for
  9724. completion."
  9725. (let (vals)
  9726. (cond
  9727. ((equal property "TODO")
  9728. (setq vals (org-with-point-at pom
  9729. (append org-todo-keywords-1 '("")))))
  9730. ((equal property "PRIORITY")
  9731. (let ((n org-lowest-priority))
  9732. (while (>= n org-highest-priority)
  9733. (push (char-to-string n) vals)
  9734. (setq n (1- n)))))
  9735. ((member property org-special-properties))
  9736. (t
  9737. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9738. (when (and vals (string-match "\\S-" vals))
  9739. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9740. (setq vals (mapcar (lambda (x)
  9741. (cond ((stringp x) x)
  9742. ((numberp x) (number-to-string x))
  9743. ((symbolp x) (symbol-name x))
  9744. (t "???")))
  9745. vals)))))
  9746. (if table (mapcar 'list vals) vals)))
  9747. (defun org-property-previous-allowed-value (&optional previous)
  9748. "Switch to the next allowed value for this property."
  9749. (interactive)
  9750. (org-property-next-allowed-value t))
  9751. (defun org-property-next-allowed-value (&optional previous)
  9752. "Switch to the next allowed value for this property."
  9753. (interactive)
  9754. (unless (org-at-property-p)
  9755. (error "Not at a property"))
  9756. (let* ((key (match-string 2))
  9757. (value (match-string 3))
  9758. (allowed (or (org-property-get-allowed-values (point) key)
  9759. (and (member value '("[ ]" "[-]" "[X]"))
  9760. '("[ ]" "[X]"))))
  9761. nval)
  9762. (unless allowed
  9763. (error "Allowed values for this property have not been defined"))
  9764. (if previous (setq allowed (reverse allowed)))
  9765. (if (member value allowed)
  9766. (setq nval (car (cdr (member value allowed)))))
  9767. (setq nval (or nval (car allowed)))
  9768. (if (equal nval value)
  9769. (error "Only one allowed value for this property"))
  9770. (org-at-property-p)
  9771. (replace-match (concat " :" key ": " nval) t t)
  9772. (org-indent-line-function)
  9773. (beginning-of-line 1)
  9774. (skip-chars-forward " \t")))
  9775. (defun org-find-entry-with-id (ident)
  9776. "Locate the entry that contains the ID property with exact value IDENT.
  9777. IDENT can be a string, a symbol or a number, this function will search for
  9778. the string representation of it.
  9779. Return the position where this entry starts, or nil if there is no such entry."
  9780. (interactive "sID: ")
  9781. (let ((id (cond
  9782. ((stringp ident) ident)
  9783. ((symbol-name ident) (symbol-name ident))
  9784. ((numberp ident) (number-to-string ident))
  9785. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9786. (case-fold-search nil))
  9787. (save-excursion
  9788. (save-restriction
  9789. (widen)
  9790. (goto-char (point-min))
  9791. (when (re-search-forward
  9792. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9793. nil t)
  9794. (org-back-to-heading)
  9795. (point))))))
  9796. ;;;; Timestamps
  9797. (defvar org-last-changed-timestamp nil)
  9798. (defvar org-last-inserted-timestamp nil
  9799. "The last time stamp inserted with `org-insert-time-stamp'.")
  9800. (defvar org-time-was-given) ; dynamically scoped parameter
  9801. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9802. (defvar org-ts-what) ; dynamically scoped parameter
  9803. (defun org-time-stamp (arg &optional inactive)
  9804. "Prompt for a date/time and insert a time stamp.
  9805. If the user specifies a time like HH:MM, or if this command is called
  9806. with a prefix argument, the time stamp will contain date and time.
  9807. Otherwise, only the date will be included. All parts of a date not
  9808. specified by the user will be filled in from the current date/time.
  9809. So if you press just return without typing anything, the time stamp
  9810. will represent the current date/time. If there is already a timestamp
  9811. at the cursor, it will be modified."
  9812. (interactive "P")
  9813. (let* ((ts nil)
  9814. (default-time
  9815. ;; Default time is either today, or, when entering a range,
  9816. ;; the range start.
  9817. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9818. (save-excursion
  9819. (re-search-backward
  9820. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9821. (- (point) 20) t)))
  9822. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9823. (current-time)))
  9824. (default-input (and ts (org-get-compact-tod ts)))
  9825. org-time-was-given org-end-time-was-given time)
  9826. (cond
  9827. ((and (org-at-timestamp-p t)
  9828. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9829. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9830. (insert "--")
  9831. (setq time (let ((this-command this-command))
  9832. (org-read-date arg 'totime nil nil
  9833. default-time default-input)))
  9834. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9835. ((org-at-timestamp-p t)
  9836. (setq time (let ((this-command this-command))
  9837. (org-read-date arg 'totime nil nil default-time default-input)))
  9838. (when (org-at-timestamp-p t) ; just to get the match data
  9839. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9840. (replace-match "")
  9841. (setq org-last-changed-timestamp
  9842. (org-insert-time-stamp
  9843. time (or org-time-was-given arg)
  9844. inactive nil nil (list org-end-time-was-given))))
  9845. (message "Timestamp updated"))
  9846. (t
  9847. (setq time (let ((this-command this-command))
  9848. (org-read-date arg 'totime nil nil default-time default-input)))
  9849. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9850. nil nil (list org-end-time-was-given))))))
  9851. ;; FIXME: can we use this for something else, like computing time differences?
  9852. (defun org-get-compact-tod (s)
  9853. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9854. (let* ((t1 (match-string 1 s))
  9855. (h1 (string-to-number (match-string 2 s)))
  9856. (m1 (string-to-number (match-string 3 s)))
  9857. (t2 (and (match-end 4) (match-string 5 s)))
  9858. (h2 (and t2 (string-to-number (match-string 6 s))))
  9859. (m2 (and t2 (string-to-number (match-string 7 s))))
  9860. dh dm)
  9861. (if (not t2)
  9862. t1
  9863. (setq dh (- h2 h1) dm (- m2 m1))
  9864. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9865. (concat t1 "+" (number-to-string dh)
  9866. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9867. (defun org-time-stamp-inactive (&optional arg)
  9868. "Insert an inactive time stamp.
  9869. An inactive time stamp is enclosed in square brackets instead of angle
  9870. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9871. does not link to the calendar and cannot be changed with the S-cursor keys.
  9872. So these are more for recording a certain time/date."
  9873. (interactive "P")
  9874. (org-time-stamp arg 'inactive))
  9875. (defvar org-date-ovl (org-make-overlay 1 1))
  9876. (org-overlay-put org-date-ovl 'face 'org-warning)
  9877. (org-detach-overlay org-date-ovl)
  9878. (defvar org-ans1) ; dynamically scoped parameter
  9879. (defvar org-ans2) ; dynamically scoped parameter
  9880. (defvar org-plain-time-of-day-regexp) ; defined below
  9881. (defvar org-overriding-default-time nil) ; dynamically scoped
  9882. (defvar org-read-date-overlay nil)
  9883. (defvar org-dcst nil) ; dynamically scoped
  9884. (defun org-read-date (&optional with-time to-time from-string prompt
  9885. default-time default-input)
  9886. "Read a date, possibly a time, and make things smooth for the user.
  9887. The prompt will suggest to enter an ISO date, but you can also enter anything
  9888. which will at least partially be understood by `parse-time-string'.
  9889. Unrecognized parts of the date will default to the current day, month, year,
  9890. hour and minute. If this command is called to replace a timestamp at point,
  9891. of to enter the second timestamp of a range, the default time is taken from the
  9892. existing stamp. For example,
  9893. 3-2-5 --> 2003-02-05
  9894. feb 15 --> currentyear-02-15
  9895. sep 12 9 --> 2009-09-12
  9896. 12:45 --> today 12:45
  9897. 22 sept 0:34 --> currentyear-09-22 0:34
  9898. 12 --> currentyear-currentmonth-12
  9899. Fri --> nearest Friday (today or later)
  9900. etc.
  9901. Furthermore you can specify a relative date by giving, as the *first* thing
  9902. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9903. change in days weeks, months, years.
  9904. With a single plus or minus, the date is relative to today. With a double
  9905. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9906. +4d --> four days from today
  9907. +4 --> same as above
  9908. +2w --> two weeks from today
  9909. ++5 --> five days from default date
  9910. The function understands only English month and weekday abbreviations,
  9911. but this can be configured with the variables `parse-time-months' and
  9912. `parse-time-weekdays'.
  9913. While prompting, a calendar is popped up - you can also select the
  9914. date with the mouse (button 1). The calendar shows a period of three
  9915. months. To scroll it to other months, use the keys `>' and `<'.
  9916. If you don't like the calendar, turn it off with
  9917. \(setq org-read-date-popup-calendar nil)
  9918. With optional argument TO-TIME, the date will immediately be converted
  9919. to an internal time.
  9920. With an optional argument WITH-TIME, the prompt will suggest to also
  9921. insert a time. Note that when WITH-TIME is not set, you can still
  9922. enter a time, and this function will inform the calling routine about
  9923. this change. The calling routine may then choose to change the format
  9924. used to insert the time stamp into the buffer to include the time.
  9925. With optional argument FROM-STRING, read from this string instead from
  9926. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9927. the time/date that is used for everything that is not specified by the
  9928. user."
  9929. (require 'parse-time)
  9930. (let* ((org-time-stamp-rounding-minutes
  9931. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9932. (org-dcst org-display-custom-times)
  9933. (ct (org-current-time))
  9934. (def (or org-overriding-default-time default-time ct))
  9935. (defdecode (decode-time def))
  9936. (dummy (progn
  9937. (when (< (nth 2 defdecode) org-extend-today-until)
  9938. (setcar (nthcdr 2 defdecode) -1)
  9939. (setcar (nthcdr 1 defdecode) 59)
  9940. (setq def (apply 'encode-time defdecode)
  9941. defdecode (decode-time def)))))
  9942. (calendar-move-hook nil)
  9943. (calendar-view-diary-initially-flag nil)
  9944. (view-diary-entries-initially nil)
  9945. (calendar-view-holidays-initially-flag nil)
  9946. (view-calendar-holidays-initially nil)
  9947. (timestr (format-time-string
  9948. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9949. (prompt (concat (if prompt (concat prompt " ") "")
  9950. (format "Date+time [%s]: " timestr)))
  9951. ans (org-ans0 "") org-ans1 org-ans2 final)
  9952. (cond
  9953. (from-string (setq ans from-string))
  9954. (org-read-date-popup-calendar
  9955. (save-excursion
  9956. (save-window-excursion
  9957. (calendar)
  9958. (calendar-forward-day (- (time-to-days def)
  9959. (calendar-absolute-from-gregorian
  9960. (calendar-current-date))))
  9961. (org-eval-in-calendar nil t)
  9962. (let* ((old-map (current-local-map))
  9963. (map (copy-keymap calendar-mode-map))
  9964. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9965. (org-defkey map (kbd "RET") 'org-calendar-select)
  9966. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9967. 'org-calendar-select-mouse)
  9968. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9969. 'org-calendar-select-mouse)
  9970. (org-defkey minibuffer-local-map [(meta shift left)]
  9971. (lambda () (interactive)
  9972. (org-eval-in-calendar '(calendar-backward-month 1))))
  9973. (org-defkey minibuffer-local-map [(meta shift right)]
  9974. (lambda () (interactive)
  9975. (org-eval-in-calendar '(calendar-forward-month 1))))
  9976. (org-defkey minibuffer-local-map [(meta shift up)]
  9977. (lambda () (interactive)
  9978. (org-eval-in-calendar '(calendar-backward-year 1))))
  9979. (org-defkey minibuffer-local-map [(meta shift down)]
  9980. (lambda () (interactive)
  9981. (org-eval-in-calendar '(calendar-forward-year 1))))
  9982. (org-defkey minibuffer-local-map [(shift up)]
  9983. (lambda () (interactive)
  9984. (org-eval-in-calendar '(calendar-backward-week 1))))
  9985. (org-defkey minibuffer-local-map [(shift down)]
  9986. (lambda () (interactive)
  9987. (org-eval-in-calendar '(calendar-forward-week 1))))
  9988. (org-defkey minibuffer-local-map [(shift left)]
  9989. (lambda () (interactive)
  9990. (org-eval-in-calendar '(calendar-backward-day 1))))
  9991. (org-defkey minibuffer-local-map [(shift right)]
  9992. (lambda () (interactive)
  9993. (org-eval-in-calendar '(calendar-forward-day 1))))
  9994. (org-defkey minibuffer-local-map ">"
  9995. (lambda () (interactive)
  9996. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9997. (org-defkey minibuffer-local-map "<"
  9998. (lambda () (interactive)
  9999. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10000. (unwind-protect
  10001. (progn
  10002. (use-local-map map)
  10003. (add-hook 'post-command-hook 'org-read-date-display)
  10004. (setq org-ans0 (read-string prompt default-input nil nil))
  10005. ;; org-ans0: from prompt
  10006. ;; org-ans1: from mouse click
  10007. ;; org-ans2: from calendar motion
  10008. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10009. (remove-hook 'post-command-hook 'org-read-date-display)
  10010. (use-local-map old-map)
  10011. (when org-read-date-overlay
  10012. (org-delete-overlay org-read-date-overlay)
  10013. (setq org-read-date-overlay nil)))))))
  10014. (t ; Naked prompt only
  10015. (unwind-protect
  10016. (setq ans (read-string prompt default-input nil timestr))
  10017. (when org-read-date-overlay
  10018. (org-delete-overlay org-read-date-overlay)
  10019. (setq org-read-date-overlay nil)))))
  10020. (setq final (org-read-date-analyze ans def defdecode))
  10021. (if to-time
  10022. (apply 'encode-time final)
  10023. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10024. (format "%04d-%02d-%02d %02d:%02d"
  10025. (nth 5 final) (nth 4 final) (nth 3 final)
  10026. (nth 2 final) (nth 1 final))
  10027. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10028. (defvar def)
  10029. (defvar defdecode)
  10030. (defvar with-time)
  10031. (defun org-read-date-display ()
  10032. "Display the current date prompt interpretation in the minibuffer."
  10033. (when org-read-date-display-live
  10034. (when org-read-date-overlay
  10035. (org-delete-overlay org-read-date-overlay))
  10036. (let ((p (point)))
  10037. (end-of-line 1)
  10038. (while (not (equal (buffer-substring
  10039. (max (point-min) (- (point) 4)) (point))
  10040. " "))
  10041. (insert " "))
  10042. (goto-char p))
  10043. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10044. " " (or org-ans1 org-ans2)))
  10045. (org-end-time-was-given nil)
  10046. (f (org-read-date-analyze ans def defdecode))
  10047. (fmts (if org-dcst
  10048. org-time-stamp-custom-formats
  10049. org-time-stamp-formats))
  10050. (fmt (if (or with-time
  10051. (and (boundp 'org-time-was-given) org-time-was-given))
  10052. (cdr fmts)
  10053. (car fmts)))
  10054. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10055. (when (and org-end-time-was-given
  10056. (string-match org-plain-time-of-day-regexp txt))
  10057. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10058. org-end-time-was-given
  10059. (substring txt (match-end 0)))))
  10060. (setq org-read-date-overlay
  10061. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10062. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10063. (defun org-read-date-analyze (ans def defdecode)
  10064. "Analyse the combined answer of the date prompt."
  10065. ;; FIXME: cleanup and comment
  10066. (let (delta deltan deltaw deltadef year month day
  10067. hour minute second wday pm h2 m2 tl wday1
  10068. iso-year iso-weekday iso-week iso-year iso-date)
  10069. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10070. (setq ans "+0"))
  10071. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10072. (setq ans (replace-match "" t t ans)
  10073. deltan (car delta)
  10074. deltaw (nth 1 delta)
  10075. deltadef (nth 2 delta)))
  10076. ;; Check if there is an iso week date in there
  10077. ;; If yes, sore the info and postpone interpreting it until the rest
  10078. ;; of the parsing is done
  10079. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10080. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10081. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10082. iso-week (string-to-number (match-string 2 ans)))
  10083. (setq ans (replace-match "" t t ans)))
  10084. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10085. (when (string-match
  10086. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10087. (setq year (if (match-end 2)
  10088. (string-to-number (match-string 2 ans))
  10089. (string-to-number (format-time-string "%Y")))
  10090. month (string-to-number (match-string 3 ans))
  10091. day (string-to-number (match-string 4 ans)))
  10092. (if (< year 100) (setq year (+ 2000 year)))
  10093. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10094. t nil ans)))
  10095. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10096. ;; If there is a time with am/pm, and *no* time without it, we convert
  10097. ;; so that matching will be successful.
  10098. (loop for i from 1 to 2 do ; twice, for end time as well
  10099. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10100. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10101. (setq hour (string-to-number (match-string 1 ans))
  10102. minute (if (match-end 3)
  10103. (string-to-number (match-string 3 ans))
  10104. 0)
  10105. pm (equal ?p
  10106. (string-to-char (downcase (match-string 4 ans)))))
  10107. (if (and (= hour 12) (not pm))
  10108. (setq hour 0)
  10109. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10110. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10111. t t ans))))
  10112. ;; Check if a time range is given as a duration
  10113. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10114. (setq hour (string-to-number (match-string 1 ans))
  10115. h2 (+ hour (string-to-number (match-string 3 ans)))
  10116. minute (string-to-number (match-string 2 ans))
  10117. m2 (+ minute (if (match-end 5) (string-to-number
  10118. (match-string 5 ans))0)))
  10119. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10120. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10121. t t ans)))
  10122. ;; Check if there is a time range
  10123. (when (boundp 'org-end-time-was-given)
  10124. (setq org-time-was-given nil)
  10125. (when (and (string-match org-plain-time-of-day-regexp ans)
  10126. (match-end 8))
  10127. (setq org-end-time-was-given (match-string 8 ans))
  10128. (setq ans (concat (substring ans 0 (match-beginning 7))
  10129. (substring ans (match-end 7))))))
  10130. (setq tl (parse-time-string ans)
  10131. day (or (nth 3 tl) (nth 3 defdecode))
  10132. month (or (nth 4 tl)
  10133. (if (and org-read-date-prefer-future
  10134. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10135. (1+ (nth 4 defdecode))
  10136. (nth 4 defdecode)))
  10137. year (or (nth 5 tl)
  10138. (if (and org-read-date-prefer-future
  10139. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10140. (1+ (nth 5 defdecode))
  10141. (nth 5 defdecode)))
  10142. hour (or (nth 2 tl) (nth 2 defdecode))
  10143. minute (or (nth 1 tl) (nth 1 defdecode))
  10144. second (or (nth 0 tl) 0)
  10145. wday (nth 6 tl))
  10146. ;; Special date definitions below
  10147. (cond
  10148. (iso-week
  10149. ;; There was an iso week
  10150. (setq year (or iso-year year)
  10151. day (or iso-weekday wday 1)
  10152. wday nil ; to make sure that the trigger below does not match
  10153. iso-date (calendar-gregorian-from-absolute
  10154. (calendar-absolute-from-iso
  10155. (list iso-week day year))))
  10156. ; FIXME: Should we also push ISO weeks into the future?
  10157. ; (when (and org-read-date-prefer-future
  10158. ; (not iso-year)
  10159. ; (< (calendar-absolute-from-gregorian iso-date)
  10160. ; (time-to-days (current-time))))
  10161. ; (setq year (1+ year)
  10162. ; iso-date (calendar-gregorian-from-absolute
  10163. ; (calendar-absolute-from-iso
  10164. ; (list iso-week day year)))))
  10165. (setq month (car iso-date)
  10166. year (nth 2 iso-date)
  10167. day (nth 1 iso-date)))
  10168. (deltan
  10169. (unless deltadef
  10170. (let ((now (decode-time (current-time))))
  10171. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10172. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10173. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10174. ((equal deltaw "m") (setq month (+ month deltan)))
  10175. ((equal deltaw "y") (setq year (+ year deltan)))))
  10176. ((and wday (not (nth 3 tl)))
  10177. ;; Weekday was given, but no day, so pick that day in the week
  10178. ;; on or after the derived date.
  10179. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10180. (unless (equal wday wday1)
  10181. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10182. (if (and (boundp 'org-time-was-given)
  10183. (nth 2 tl))
  10184. (setq org-time-was-given t))
  10185. (if (< year 100) (setq year (+ 2000 year)))
  10186. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10187. (list second minute hour day month year)))
  10188. (defvar parse-time-weekdays)
  10189. (defun org-read-date-get-relative (s today default)
  10190. "Check string S for special relative date string.
  10191. TODAY and DEFAULT are internal times, for today and for a default.
  10192. Return shift list (N what def-flag)
  10193. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10194. N is the number of WHATs to shift.
  10195. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10196. the DEFAULT date rather than TODAY."
  10197. (when (and
  10198. (string-match
  10199. (concat
  10200. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10201. "\\([0-9]+\\)?"
  10202. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10203. "\\([ \t]\\|$\\)") s)
  10204. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10205. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10206. (string-to-char (substring (match-string 1 s) -1))
  10207. ?+))
  10208. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10209. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10210. (what (if (match-end 3) (match-string 3 s) "d"))
  10211. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10212. (date (if rel default today))
  10213. (wday (nth 6 (decode-time date)))
  10214. delta)
  10215. (if wday1
  10216. (progn
  10217. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10218. (if (= dir ?-) (setq delta (- delta 7)))
  10219. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10220. (list delta "d" rel))
  10221. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10222. (defun org-eval-in-calendar (form &optional keepdate)
  10223. "Eval FORM in the calendar window and return to current window.
  10224. Also, store the cursor date in variable org-ans2."
  10225. (let ((sw (selected-window)))
  10226. (select-window (get-buffer-window "*Calendar*"))
  10227. (eval form)
  10228. (when (and (not keepdate) (calendar-cursor-to-date))
  10229. (let* ((date (calendar-cursor-to-date))
  10230. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10231. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10232. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10233. (select-window sw)))
  10234. (defun org-calendar-select ()
  10235. "Return to `org-read-date' with the date currently selected.
  10236. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10237. (interactive)
  10238. (when (calendar-cursor-to-date)
  10239. (let* ((date (calendar-cursor-to-date))
  10240. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10241. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10242. (if (active-minibuffer-window) (exit-minibuffer))))
  10243. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10244. "Insert a date stamp for the date given by the internal TIME.
  10245. WITH-HM means, use the stamp format that includes the time of the day.
  10246. INACTIVE means use square brackets instead of angular ones, so that the
  10247. stamp will not contribute to the agenda.
  10248. PRE and POST are optional strings to be inserted before and after the
  10249. stamp.
  10250. The command returns the inserted time stamp."
  10251. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10252. stamp)
  10253. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10254. (insert-before-markers (or pre ""))
  10255. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10256. (when (listp extra)
  10257. (setq extra (car extra))
  10258. (if (and (stringp extra)
  10259. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10260. (setq extra (format "-%02d:%02d"
  10261. (string-to-number (match-string 1 extra))
  10262. (string-to-number (match-string 2 extra))))
  10263. (setq extra nil)))
  10264. (when extra
  10265. (backward-char 1)
  10266. (insert-before-markers extra)
  10267. (forward-char 1))
  10268. (insert-before-markers (or post ""))
  10269. (setq org-last-inserted-timestamp stamp)))
  10270. (defun org-toggle-time-stamp-overlays ()
  10271. "Toggle the use of custom time stamp formats."
  10272. (interactive)
  10273. (setq org-display-custom-times (not org-display-custom-times))
  10274. (unless org-display-custom-times
  10275. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10276. (while (setq p (next-single-property-change p 'display))
  10277. (if (and (get-text-property p 'display)
  10278. (eq (get-text-property p 'face) 'org-date))
  10279. (remove-text-properties
  10280. p (setq p (next-single-property-change p 'display))
  10281. '(display t))))
  10282. (set-buffer-modified-p bmp)))
  10283. (if (featurep 'xemacs)
  10284. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10285. (org-restart-font-lock)
  10286. (setq org-table-may-need-update t)
  10287. (if org-display-custom-times
  10288. (message "Time stamps are overlayed with custom format")
  10289. (message "Time stamp overlays removed")))
  10290. (defun org-display-custom-time (beg end)
  10291. "Overlay modified time stamp format over timestamp between BEG and END."
  10292. (let* ((ts (buffer-substring beg end))
  10293. t1 w1 with-hm tf time str w2 (off 0))
  10294. (save-match-data
  10295. (setq t1 (org-parse-time-string ts t))
  10296. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10297. (setq off (- (match-end 0) (match-beginning 0)))))
  10298. (setq end (- end off))
  10299. (setq w1 (- end beg)
  10300. with-hm (and (nth 1 t1) (nth 2 t1))
  10301. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10302. time (org-fix-decoded-time t1)
  10303. str (org-add-props
  10304. (format-time-string
  10305. (substring tf 1 -1) (apply 'encode-time time))
  10306. nil 'mouse-face 'highlight)
  10307. w2 (length str))
  10308. (if (not (= w2 w1))
  10309. (add-text-properties (1+ beg) (+ 2 beg)
  10310. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10311. (if (featurep 'xemacs)
  10312. (progn
  10313. (put-text-property beg end 'invisible t)
  10314. (put-text-property beg end 'end-glyph (make-glyph str)))
  10315. (put-text-property beg end 'display str))))
  10316. (defun org-translate-time (string)
  10317. "Translate all timestamps in STRING to custom format.
  10318. But do this only if the variable `org-display-custom-times' is set."
  10319. (when org-display-custom-times
  10320. (save-match-data
  10321. (let* ((start 0)
  10322. (re org-ts-regexp-both)
  10323. t1 with-hm inactive tf time str beg end)
  10324. (while (setq start (string-match re string start))
  10325. (setq beg (match-beginning 0)
  10326. end (match-end 0)
  10327. t1 (save-match-data
  10328. (org-parse-time-string (substring string beg end) t))
  10329. with-hm (and (nth 1 t1) (nth 2 t1))
  10330. inactive (equal (substring string beg (1+ beg)) "[")
  10331. tf (funcall (if with-hm 'cdr 'car)
  10332. org-time-stamp-custom-formats)
  10333. time (org-fix-decoded-time t1)
  10334. str (format-time-string
  10335. (concat
  10336. (if inactive "[" "<") (substring tf 1 -1)
  10337. (if inactive "]" ">"))
  10338. (apply 'encode-time time))
  10339. string (replace-match str t t string)
  10340. start (+ start (length str)))))))
  10341. string)
  10342. (defun org-fix-decoded-time (time)
  10343. "Set 0 instead of nil for the first 6 elements of time.
  10344. Don't touch the rest."
  10345. (let ((n 0))
  10346. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10347. (defun org-days-to-time (timestamp-string)
  10348. "Difference between TIMESTAMP-STRING and now in days."
  10349. (- (time-to-days (org-time-string-to-time timestamp-string))
  10350. (time-to-days (current-time))))
  10351. (defun org-deadline-close (timestamp-string &optional ndays)
  10352. "Is the time in TIMESTAMP-STRING close to the current date?"
  10353. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10354. (and (< (org-days-to-time timestamp-string) ndays)
  10355. (not (org-entry-is-done-p))))
  10356. (defun org-get-wdays (ts)
  10357. "Get the deadline lead time appropriate for timestring TS."
  10358. (cond
  10359. ((<= org-deadline-warning-days 0)
  10360. ;; 0 or negative, enforce this value no matter what
  10361. (- org-deadline-warning-days))
  10362. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10363. ;; lead time is specified.
  10364. (floor (* (string-to-number (match-string 1 ts))
  10365. (cdr (assoc (match-string 2 ts)
  10366. '(("d" . 1) ("w" . 7)
  10367. ("m" . 30.4) ("y" . 365.25)))))))
  10368. ;; go for the default.
  10369. (t org-deadline-warning-days)))
  10370. (defun org-calendar-select-mouse (ev)
  10371. "Return to `org-read-date' with the date currently selected.
  10372. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10373. (interactive "e")
  10374. (mouse-set-point ev)
  10375. (when (calendar-cursor-to-date)
  10376. (let* ((date (calendar-cursor-to-date))
  10377. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10378. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10379. (if (active-minibuffer-window) (exit-minibuffer))))
  10380. (defun org-check-deadlines (ndays)
  10381. "Check if there are any deadlines due or past due.
  10382. A deadline is considered due if it happens within `org-deadline-warning-days'
  10383. days from today's date. If the deadline appears in an entry marked DONE,
  10384. it is not shown. The prefix arg NDAYS can be used to test that many
  10385. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10386. (interactive "P")
  10387. (let* ((org-warn-days
  10388. (cond
  10389. ((equal ndays '(4)) 100000)
  10390. (ndays (prefix-numeric-value ndays))
  10391. (t (abs org-deadline-warning-days))))
  10392. (case-fold-search nil)
  10393. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10394. (callback
  10395. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10396. (message "%d deadlines past-due or due within %d days"
  10397. (org-occur regexp nil callback)
  10398. org-warn-days)))
  10399. (defun org-check-before-date (date)
  10400. "Check if there are deadlines or scheduled entries before DATE."
  10401. (interactive (list (org-read-date)))
  10402. (let ((case-fold-search nil)
  10403. (regexp (concat "\\<\\(" org-deadline-string
  10404. "\\|" org-scheduled-string
  10405. "\\) *<\\([^>]+\\)>"))
  10406. (callback
  10407. (lambda () (time-less-p
  10408. (org-time-string-to-time (match-string 2))
  10409. (org-time-string-to-time date)))))
  10410. (message "%d entries before %s"
  10411. (org-occur regexp nil callback) date)))
  10412. (defun org-evaluate-time-range (&optional to-buffer)
  10413. "Evaluate a time range by computing the difference between start and end.
  10414. Normally the result is just printed in the echo area, but with prefix arg
  10415. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10416. If the time range is actually in a table, the result is inserted into the
  10417. next column.
  10418. For time difference computation, a year is assumed to be exactly 365
  10419. days in order to avoid rounding problems."
  10420. (interactive "P")
  10421. (or
  10422. (org-clock-update-time-maybe)
  10423. (save-excursion
  10424. (unless (org-at-date-range-p t)
  10425. (goto-char (point-at-bol))
  10426. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10427. (if (not (org-at-date-range-p t))
  10428. (error "Not at a time-stamp range, and none found in current line")))
  10429. (let* ((ts1 (match-string 1))
  10430. (ts2 (match-string 2))
  10431. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10432. (match-end (match-end 0))
  10433. (time1 (org-time-string-to-time ts1))
  10434. (time2 (org-time-string-to-time ts2))
  10435. (t1 (time-to-seconds time1))
  10436. (t2 (time-to-seconds time2))
  10437. (diff (abs (- t2 t1)))
  10438. (negative (< (- t2 t1) 0))
  10439. ;; (ys (floor (* 365 24 60 60)))
  10440. (ds (* 24 60 60))
  10441. (hs (* 60 60))
  10442. (fy "%dy %dd %02d:%02d")
  10443. (fy1 "%dy %dd")
  10444. (fd "%dd %02d:%02d")
  10445. (fd1 "%dd")
  10446. (fh "%02d:%02d")
  10447. y d h m align)
  10448. (if havetime
  10449. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10450. y 0
  10451. d (floor (/ diff ds)) diff (mod diff ds)
  10452. h (floor (/ diff hs)) diff (mod diff hs)
  10453. m (floor (/ diff 60)))
  10454. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10455. y 0
  10456. d (floor (+ (/ diff ds) 0.5))
  10457. h 0 m 0))
  10458. (if (not to-buffer)
  10459. (message "%s" (org-make-tdiff-string y d h m))
  10460. (if (org-at-table-p)
  10461. (progn
  10462. (goto-char match-end)
  10463. (setq align t)
  10464. (and (looking-at " *|") (goto-char (match-end 0))))
  10465. (goto-char match-end))
  10466. (if (looking-at
  10467. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10468. (replace-match ""))
  10469. (if negative (insert " -"))
  10470. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10471. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10472. (insert " " (format fh h m))))
  10473. (if align (org-table-align))
  10474. (message "Time difference inserted")))))
  10475. (defun org-make-tdiff-string (y d h m)
  10476. (let ((fmt "")
  10477. (l nil))
  10478. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10479. l (push y l)))
  10480. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10481. l (push d l)))
  10482. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10483. l (push h l)))
  10484. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10485. l (push m l)))
  10486. (apply 'format fmt (nreverse l))))
  10487. (defun org-time-string-to-time (s)
  10488. (apply 'encode-time (org-parse-time-string s)))
  10489. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10490. "Convert a time stamp to an absolute day number.
  10491. If there is a specifyer for a cyclic time stamp, get the closest date to
  10492. DAYNR.
  10493. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10494. (cond
  10495. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10496. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10497. daynr
  10498. (+ daynr 1000)))
  10499. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10500. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10501. (time-to-days (current-time))) (match-string 0 s)
  10502. prefer show-all))
  10503. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10504. (defun org-days-to-iso-week (days)
  10505. "Return the iso week number."
  10506. (require 'cal-iso)
  10507. (car (calendar-iso-from-absolute days)))
  10508. (defun org-small-year-to-year (year)
  10509. "Convert 2-digit years into 4-digit years.
  10510. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10511. The year 2000 cannot be abbreviated. Any year larger than 99
  10512. is returned unchanged."
  10513. (if (< year 38)
  10514. (setq year (+ 2000 year))
  10515. (if (< year 100)
  10516. (setq year (+ 1900 year))))
  10517. year)
  10518. (defun org-time-from-absolute (d)
  10519. "Return the time corresponding to date D.
  10520. D may be an absolute day number, or a calendar-type list (month day year)."
  10521. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10522. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10523. (defun org-calendar-holiday ()
  10524. "List of holidays, for Diary display in Org-mode."
  10525. (require 'holidays)
  10526. (let ((hl (funcall
  10527. (if (fboundp 'calendar-check-holidays)
  10528. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10529. (if hl (mapconcat 'identity hl "; "))))
  10530. (defun org-diary-sexp-entry (sexp entry date)
  10531. "Process a SEXP diary ENTRY for DATE."
  10532. (require 'diary-lib)
  10533. (let ((result (if calendar-debug-sexp
  10534. (let ((stack-trace-on-error t))
  10535. (eval (car (read-from-string sexp))))
  10536. (condition-case nil
  10537. (eval (car (read-from-string sexp)))
  10538. (error
  10539. (beep)
  10540. (message "Bad sexp at line %d in %s: %s"
  10541. (org-current-line)
  10542. (buffer-file-name) sexp)
  10543. (sleep-for 2))))))
  10544. (cond ((stringp result) result)
  10545. ((and (consp result)
  10546. (stringp (cdr result))) (cdr result))
  10547. (result entry)
  10548. (t nil))))
  10549. (defun org-diary-to-ical-string (frombuf)
  10550. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10551. This uses the icalendar.el library."
  10552. (let* ((tmpdir (if (featurep 'xemacs)
  10553. (temp-directory)
  10554. temporary-file-directory))
  10555. (tmpfile (make-temp-name
  10556. (expand-file-name "orgics" tmpdir)))
  10557. buf rtn b e)
  10558. (save-excursion
  10559. (set-buffer frombuf)
  10560. (icalendar-export-region (point-min) (point-max) tmpfile)
  10561. (setq buf (find-buffer-visiting tmpfile))
  10562. (set-buffer buf)
  10563. (goto-char (point-min))
  10564. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10565. (setq b (match-beginning 0)))
  10566. (goto-char (point-max))
  10567. (if (re-search-backward "^END:VEVENT" nil t)
  10568. (setq e (match-end 0)))
  10569. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10570. (kill-buffer buf)
  10571. (delete-file tmpfile)
  10572. rtn))
  10573. (defun org-closest-date (start current change prefer show-all)
  10574. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10575. When PREFER is `past' return a date that is either CURRENT or past.
  10576. When PREFER is `future', return a date that is either CURRENT or future.
  10577. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10578. ;; Make the proper lists from the dates
  10579. (catch 'exit
  10580. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10581. dn dw sday cday n1 n2
  10582. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10583. (setq start (org-date-to-gregorian start)
  10584. current (org-date-to-gregorian
  10585. (if show-all
  10586. current
  10587. (time-to-days (current-time))))
  10588. sday (calendar-absolute-from-gregorian start)
  10589. cday (calendar-absolute-from-gregorian current))
  10590. (if (<= cday sday) (throw 'exit sday))
  10591. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10592. (setq dn (string-to-number (match-string 1 change))
  10593. dw (cdr (assoc (match-string 2 change) a1)))
  10594. (error "Invalid change specifyer: %s" change))
  10595. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10596. (cond
  10597. ((eq dw 'day)
  10598. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10599. n2 (+ n1 dn)))
  10600. ((eq dw 'year)
  10601. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10602. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10603. (setq date1 (list m d y1)
  10604. n1 (calendar-absolute-from-gregorian date1)
  10605. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10606. n2 (calendar-absolute-from-gregorian date2)))
  10607. ((eq dw 'month)
  10608. ;; approx number of month between the two dates
  10609. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10610. ;; How often does dn fit in there?
  10611. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10612. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10613. m (+ m nm)
  10614. ny (floor (/ m 12))
  10615. y (+ y ny)
  10616. m (- m (* ny 12)))
  10617. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10618. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10619. (setq m2 (+ m dn) y2 y)
  10620. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10621. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10622. (while (<= n2 cday)
  10623. (setq n1 n2 m m2 y y2)
  10624. (setq m2 (+ m dn) y2 y)
  10625. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10626. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10627. (if show-all
  10628. (cond
  10629. ((eq prefer 'past) n1)
  10630. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10631. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10632. (cond
  10633. ((eq prefer 'past) n1)
  10634. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10635. (t (if (= cday n1) n1 n2)))))))
  10636. (defun org-date-to-gregorian (date)
  10637. "Turn any specification of DATE into a gregorian date for the calendar."
  10638. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10639. ((and (listp date) (= (length date) 3)) date)
  10640. ((stringp date)
  10641. (setq date (org-parse-time-string date))
  10642. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10643. ((listp date)
  10644. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10645. (defun org-parse-time-string (s &optional nodefault)
  10646. "Parse the standard Org-mode time string.
  10647. This should be a lot faster than the normal `parse-time-string'.
  10648. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10649. hour and minute fields will be nil if not given."
  10650. (if (string-match org-ts-regexp0 s)
  10651. (list 0
  10652. (if (or (match-beginning 8) (not nodefault))
  10653. (string-to-number (or (match-string 8 s) "0")))
  10654. (if (or (match-beginning 7) (not nodefault))
  10655. (string-to-number (or (match-string 7 s) "0")))
  10656. (string-to-number (match-string 4 s))
  10657. (string-to-number (match-string 3 s))
  10658. (string-to-number (match-string 2 s))
  10659. nil nil nil)
  10660. (make-list 9 0)))
  10661. (defun org-timestamp-up (&optional arg)
  10662. "Increase the date item at the cursor by one.
  10663. If the cursor is on the year, change the year. If it is on the month or
  10664. the day, change that.
  10665. With prefix ARG, change by that many units."
  10666. (interactive "p")
  10667. (org-timestamp-change (prefix-numeric-value arg)))
  10668. (defun org-timestamp-down (&optional arg)
  10669. "Decrease the date item at the cursor by one.
  10670. If the cursor is on the year, change the year. If it is on the month or
  10671. the day, change that.
  10672. With prefix ARG, change by that many units."
  10673. (interactive "p")
  10674. (org-timestamp-change (- (prefix-numeric-value arg))))
  10675. (defun org-timestamp-up-day (&optional arg)
  10676. "Increase the date in the time stamp by one day.
  10677. With prefix ARG, change that many days."
  10678. (interactive "p")
  10679. (if (and (not (org-at-timestamp-p t))
  10680. (org-on-heading-p))
  10681. (org-todo 'up)
  10682. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10683. (defun org-timestamp-down-day (&optional arg)
  10684. "Decrease the date in the time stamp by one day.
  10685. With prefix ARG, change that many days."
  10686. (interactive "p")
  10687. (if (and (not (org-at-timestamp-p t))
  10688. (org-on-heading-p))
  10689. (org-todo 'down)
  10690. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10691. (defun org-at-timestamp-p (&optional inactive-ok)
  10692. "Determine if the cursor is in or at a timestamp."
  10693. (interactive)
  10694. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10695. (pos (point))
  10696. (ans (or (looking-at tsr)
  10697. (save-excursion
  10698. (skip-chars-backward "^[<\n\r\t")
  10699. (if (> (point) (point-min)) (backward-char 1))
  10700. (and (looking-at tsr)
  10701. (> (- (match-end 0) pos) -1))))))
  10702. (and ans
  10703. (boundp 'org-ts-what)
  10704. (setq org-ts-what
  10705. (cond
  10706. ((= pos (match-beginning 0)) 'bracket)
  10707. ((= pos (1- (match-end 0))) 'bracket)
  10708. ((org-pos-in-match-range pos 2) 'year)
  10709. ((org-pos-in-match-range pos 3) 'month)
  10710. ((org-pos-in-match-range pos 7) 'hour)
  10711. ((org-pos-in-match-range pos 8) 'minute)
  10712. ((or (org-pos-in-match-range pos 4)
  10713. (org-pos-in-match-range pos 5)) 'day)
  10714. ((and (> pos (or (match-end 8) (match-end 5)))
  10715. (< pos (match-end 0)))
  10716. (- pos (or (match-end 8) (match-end 5))))
  10717. (t 'day))))
  10718. ans))
  10719. (defun org-toggle-timestamp-type ()
  10720. "Toggle the type (<active> or [inactive]) of a time stamp."
  10721. (interactive)
  10722. (when (org-at-timestamp-p t)
  10723. (let ((beg (match-beginning 0)) (end (match-end 0))
  10724. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10725. (save-excursion
  10726. (goto-char beg)
  10727. (while (re-search-forward "[][<>]" end t)
  10728. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10729. t t)))
  10730. (message "Timestamp is now %sactive"
  10731. (if (equal (char-after beg) ?<) "" "in")))))
  10732. (defun org-timestamp-change (n &optional what)
  10733. "Change the date in the time stamp at point.
  10734. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10735. `year', `minute', `second'. If WHAT is not given, the cursor position
  10736. in the timestamp determines what will be changed."
  10737. (let ((pos (point))
  10738. with-hm inactive
  10739. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10740. org-ts-what
  10741. extra rem
  10742. ts time time0)
  10743. (if (not (org-at-timestamp-p t))
  10744. (error "Not at a timestamp"))
  10745. (if (and (not what) (eq org-ts-what 'bracket))
  10746. (org-toggle-timestamp-type)
  10747. (if (and (not what) (not (eq org-ts-what 'day))
  10748. org-display-custom-times
  10749. (get-text-property (point) 'display)
  10750. (not (get-text-property (1- (point)) 'display)))
  10751. (setq org-ts-what 'day))
  10752. (setq org-ts-what (or what org-ts-what)
  10753. inactive (= (char-after (match-beginning 0)) ?\[)
  10754. ts (match-string 0))
  10755. (replace-match "")
  10756. (if (string-match
  10757. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10758. ts)
  10759. (setq extra (match-string 1 ts)))
  10760. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10761. (setq with-hm t))
  10762. (setq time0 (org-parse-time-string ts))
  10763. (when (and (eq org-ts-what 'minute)
  10764. (eq current-prefix-arg nil))
  10765. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10766. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10767. (setcar (cdr time0) (+ (nth 1 time0)
  10768. (if (> n 0) (- rem) (- dm rem))))))
  10769. (setq time
  10770. (encode-time (or (car time0) 0)
  10771. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10772. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10773. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10774. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10775. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10776. (nthcdr 6 time0)))
  10777. (when (integerp org-ts-what)
  10778. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10779. (if (eq what 'calendar)
  10780. (let ((cal-date (org-get-date-from-calendar)))
  10781. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10782. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10783. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10784. (setcar time0 (or (car time0) 0))
  10785. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10786. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10787. (setq time (apply 'encode-time time0))))
  10788. (setq org-last-changed-timestamp
  10789. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10790. (org-clock-update-time-maybe)
  10791. (goto-char pos)
  10792. ;; Try to recenter the calendar window, if any
  10793. (if (and org-calendar-follow-timestamp-change
  10794. (get-buffer-window "*Calendar*" t)
  10795. (memq org-ts-what '(day month year)))
  10796. (org-recenter-calendar (time-to-days time))))))
  10797. (defun org-modify-ts-extra (s pos n dm)
  10798. "Change the different parts of the lead-time and repeat fields in timestamp."
  10799. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10800. ng h m new rem)
  10801. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10802. (cond
  10803. ((or (org-pos-in-match-range pos 2)
  10804. (org-pos-in-match-range pos 3))
  10805. (setq m (string-to-number (match-string 3 s))
  10806. h (string-to-number (match-string 2 s)))
  10807. (if (org-pos-in-match-range pos 2)
  10808. (setq h (+ h n))
  10809. (setq n (* dm (org-no-warnings (signum n))))
  10810. (when (not (= 0 (setq rem (% m dm))))
  10811. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10812. (setq m (+ m n)))
  10813. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10814. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10815. (setq h (min 24 (max 0 h)))
  10816. (setq ng 1 new (format "-%02d:%02d" h m)))
  10817. ((org-pos-in-match-range pos 6)
  10818. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10819. ((org-pos-in-match-range pos 5)
  10820. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10821. ((org-pos-in-match-range pos 9)
  10822. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10823. ((org-pos-in-match-range pos 8)
  10824. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10825. (when ng
  10826. (setq s (concat
  10827. (substring s 0 (match-beginning ng))
  10828. new
  10829. (substring s (match-end ng))))))
  10830. s))
  10831. (defun org-recenter-calendar (date)
  10832. "If the calendar is visible, recenter it to DATE."
  10833. (let* ((win (selected-window))
  10834. (cwin (get-buffer-window "*Calendar*" t))
  10835. (calendar-move-hook nil))
  10836. (when cwin
  10837. (select-window cwin)
  10838. (calendar-goto-date (if (listp date) date
  10839. (calendar-gregorian-from-absolute date)))
  10840. (select-window win))))
  10841. (defun org-goto-calendar (&optional arg)
  10842. "Go to the Emacs calendar at the current date.
  10843. If there is a time stamp in the current line, go to that date.
  10844. A prefix ARG can be used to force the current date."
  10845. (interactive "P")
  10846. (let ((tsr org-ts-regexp) diff
  10847. (calendar-move-hook nil)
  10848. (calendar-view-holidays-initially-flag nil)
  10849. (view-calendar-holidays-initially nil)
  10850. (calendar-view-diary-initially-flag nil)
  10851. (view-diary-entries-initially nil))
  10852. (if (or (org-at-timestamp-p)
  10853. (save-excursion
  10854. (beginning-of-line 1)
  10855. (looking-at (concat ".*" tsr))))
  10856. (let ((d1 (time-to-days (current-time)))
  10857. (d2 (time-to-days
  10858. (org-time-string-to-time (match-string 1)))))
  10859. (setq diff (- d2 d1))))
  10860. (calendar)
  10861. (calendar-goto-today)
  10862. (if (and diff (not arg)) (calendar-forward-day diff))))
  10863. (defun org-get-date-from-calendar ()
  10864. "Return a list (month day year) of date at point in calendar."
  10865. (with-current-buffer "*Calendar*"
  10866. (save-match-data
  10867. (calendar-cursor-to-date))))
  10868. (defun org-date-from-calendar ()
  10869. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10870. If there is already a time stamp at the cursor position, update it."
  10871. (interactive)
  10872. (if (org-at-timestamp-p t)
  10873. (org-timestamp-change 0 'calendar)
  10874. (let ((cal-date (org-get-date-from-calendar)))
  10875. (org-insert-time-stamp
  10876. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10877. (defun org-minutes-to-hh:mm-string (m)
  10878. "Compute H:MM from a number of minutes."
  10879. (let ((h (/ m 60)))
  10880. (setq m (- m (* 60 h)))
  10881. (format org-time-clocksum-format h m)))
  10882. (defun org-hh:mm-string-to-minutes (s)
  10883. "Convert a string H:MM to a number of minutes."
  10884. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10885. (+ (* (string-to-number (match-string 1 s)) 60)
  10886. (string-to-number (match-string 2 s)))
  10887. 0))
  10888. ;;;; Agenda files
  10889. ;;;###autoload
  10890. (defun org-iswitchb (&optional arg)
  10891. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10892. With a prefix argument, restrict available to files.
  10893. With two prefix arguments, restrict available buffers to agenda files.
  10894. Due to some yet unresolved reason, the global function
  10895. `iswitchb-mode' needs to be active for this function to work."
  10896. (interactive "P")
  10897. (require 'iswitchb)
  10898. (let ((enabled iswitchb-mode) blist)
  10899. (or enabled (iswitchb-mode 1))
  10900. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10901. ((equal arg '(16)) (org-buffer-list 'agenda))
  10902. (t (org-buffer-list))))
  10903. (unwind-protect
  10904. (let ((iswitchb-make-buflist-hook
  10905. (lambda ()
  10906. (setq iswitchb-temp-buflist
  10907. (mapcar 'buffer-name blist)))))
  10908. (switch-to-buffer
  10909. (iswitchb-read-buffer
  10910. "Switch-to: " nil t))
  10911. (or enabled (iswitchb-mode -1))))))
  10912. ;;;###autoload
  10913. (defun org-ido-switchb (&optional arg)
  10914. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  10915. With a prefix argument, restrict available to files.
  10916. With two prefix arguments, restrict available buffers to agenda files."
  10917. (interactive "P")
  10918. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10919. ((equal arg '(16)) (org-buffer-list 'agenda))
  10920. (t (org-buffer-list)))))
  10921. (switch-to-buffer
  10922. (org-ido-completing-read "Org buffer: "
  10923. (mapcar 'buffer-name blist)
  10924. nil t))))
  10925. (defun org-buffer-list (&optional predicate exclude-tmp)
  10926. "Return a list of Org buffers.
  10927. PREDICATE can be `export', `files' or `agenda'.
  10928. export restrict the list to Export buffers.
  10929. files restrict the list to buffers visiting Org files.
  10930. agenda restrict the list to buffers visiting agenda files.
  10931. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10932. (let* ((bfn nil)
  10933. (agenda-files (and (eq predicate 'agenda)
  10934. (mapcar 'file-truename (org-agenda-files t))))
  10935. (filter
  10936. (cond
  10937. ((eq predicate 'files)
  10938. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10939. ((eq predicate 'export)
  10940. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  10941. ((eq predicate 'agenda)
  10942. (lambda (b)
  10943. (with-current-buffer b
  10944. (and (eq major-mode 'org-mode)
  10945. (setq bfn (buffer-file-name b))
  10946. (member (file-truename bfn) agenda-files)))))
  10947. (t (lambda (b) (with-current-buffer b
  10948. (or (eq major-mode 'org-mode)
  10949. (string-match "\*Org .*Export"
  10950. (buffer-name b)))))))))
  10951. (delq nil
  10952. (mapcar
  10953. (lambda(b)
  10954. (if (and (funcall filter b)
  10955. (or (not exclude-tmp)
  10956. (not (string-match "tmp" (buffer-name b)))))
  10957. b
  10958. nil))
  10959. (buffer-list)))))
  10960. (defun org-agenda-files (&optional unrestricted archives)
  10961. "Get the list of agenda files.
  10962. Optional UNRESTRICTED means return the full list even if a restriction
  10963. is currently in place.
  10964. When ARCHIVES is t, include all archive files hat are really being
  10965. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10966. `org-agenda-archives-mode' is t."
  10967. (let ((files
  10968. (cond
  10969. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10970. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10971. ((listp org-agenda-files) org-agenda-files)
  10972. (t (error "Invalid value of `org-agenda-files'")))))
  10973. (setq files (apply 'append
  10974. (mapcar (lambda (f)
  10975. (if (file-directory-p f)
  10976. (directory-files
  10977. f t org-agenda-file-regexp)
  10978. (list f)))
  10979. files)))
  10980. (when org-agenda-skip-unavailable-files
  10981. (setq files (delq nil
  10982. (mapcar (function
  10983. (lambda (file)
  10984. (and (file-readable-p file) file)))
  10985. files))))
  10986. (when (or (eq archives t)
  10987. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  10988. (setq files (org-add-archive-files files)))
  10989. files))
  10990. (defun org-edit-agenda-file-list ()
  10991. "Edit the list of agenda files.
  10992. Depending on setup, this either uses customize to edit the variable
  10993. `org-agenda-files', or it visits the file that is holding the list. In the
  10994. latter case, the buffer is set up in a way that saving it automatically kills
  10995. the buffer and restores the previous window configuration."
  10996. (interactive)
  10997. (if (stringp org-agenda-files)
  10998. (let ((cw (current-window-configuration)))
  10999. (find-file org-agenda-files)
  11000. (org-set-local 'org-window-configuration cw)
  11001. (org-add-hook 'after-save-hook
  11002. (lambda ()
  11003. (set-window-configuration
  11004. (prog1 org-window-configuration
  11005. (kill-buffer (current-buffer))))
  11006. (org-install-agenda-files-menu)
  11007. (message "New agenda file list installed"))
  11008. nil 'local)
  11009. (message "%s" (substitute-command-keys
  11010. "Edit list and finish with \\[save-buffer]")))
  11011. (customize-variable 'org-agenda-files)))
  11012. (defun org-store-new-agenda-file-list (list)
  11013. "Set new value for the agenda file list and save it correctly."
  11014. (if (stringp org-agenda-files)
  11015. (let ((f org-agenda-files) b)
  11016. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11017. (with-temp-file f
  11018. (insert (mapconcat 'identity list "\n") "\n")))
  11019. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11020. (setq org-agenda-files list)
  11021. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11022. (defun org-read-agenda-file-list ()
  11023. "Read the list of agenda files from a file."
  11024. (when (file-directory-p org-agenda-files)
  11025. (error "`org-agenda-files' cannot be a single directory"))
  11026. (when (stringp org-agenda-files)
  11027. (with-temp-buffer
  11028. (insert-file-contents org-agenda-files)
  11029. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11030. ;;;###autoload
  11031. (defun org-cycle-agenda-files ()
  11032. "Cycle through the files in `org-agenda-files'.
  11033. If the current buffer visits an agenda file, find the next one in the list.
  11034. If the current buffer does not, find the first agenda file."
  11035. (interactive)
  11036. (let* ((fs (org-agenda-files t))
  11037. (files (append fs (list (car fs))))
  11038. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11039. file)
  11040. (unless files (error "No agenda files"))
  11041. (catch 'exit
  11042. (while (setq file (pop files))
  11043. (if (equal (file-truename file) tcf)
  11044. (when (car files)
  11045. (find-file (car files))
  11046. (throw 'exit t))))
  11047. (find-file (car fs)))
  11048. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11049. (defun org-agenda-file-to-front (&optional to-end)
  11050. "Move/add the current file to the top of the agenda file list.
  11051. If the file is not present in the list, it is added to the front. If it is
  11052. present, it is moved there. With optional argument TO-END, add/move to the
  11053. end of the list."
  11054. (interactive "P")
  11055. (let ((org-agenda-skip-unavailable-files nil)
  11056. (file-alist (mapcar (lambda (x)
  11057. (cons (file-truename x) x))
  11058. (org-agenda-files t)))
  11059. (ctf (file-truename buffer-file-name))
  11060. x had)
  11061. (setq x (assoc ctf file-alist) had x)
  11062. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11063. (if to-end
  11064. (setq file-alist (append (delq x file-alist) (list x)))
  11065. (setq file-alist (cons x (delq x file-alist))))
  11066. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11067. (org-install-agenda-files-menu)
  11068. (message "File %s to %s of agenda file list"
  11069. (if had "moved" "added") (if to-end "end" "front"))))
  11070. (defun org-remove-file (&optional file)
  11071. "Remove current file from the list of files in variable `org-agenda-files'.
  11072. These are the files which are being checked for agenda entries.
  11073. Optional argument FILE means, use this file instead of the current."
  11074. (interactive)
  11075. (let* ((org-agenda-skip-unavailable-files nil)
  11076. (file (or file buffer-file-name))
  11077. (true-file (file-truename file))
  11078. (afile (abbreviate-file-name file))
  11079. (files (delq nil (mapcar
  11080. (lambda (x)
  11081. (if (equal true-file
  11082. (file-truename x))
  11083. nil x))
  11084. (org-agenda-files t)))))
  11085. (if (not (= (length files) (length (org-agenda-files t))))
  11086. (progn
  11087. (org-store-new-agenda-file-list files)
  11088. (org-install-agenda-files-menu)
  11089. (message "Removed file: %s" afile))
  11090. (message "File was not in list: %s (not removed)" afile))))
  11091. (defun org-file-menu-entry (file)
  11092. (vector file (list 'find-file file) t))
  11093. (defun org-check-agenda-file (file)
  11094. "Make sure FILE exists. If not, ask user what to do."
  11095. (when (not (file-exists-p file))
  11096. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11097. (abbreviate-file-name file))
  11098. (let ((r (downcase (read-char-exclusive))))
  11099. (cond
  11100. ((equal r ?r)
  11101. (org-remove-file file)
  11102. (throw 'nextfile t))
  11103. (t (error "Abort"))))))
  11104. (defun org-get-agenda-file-buffer (file)
  11105. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11106. it to the list of buffers which might be released later."
  11107. (let ((buf (org-find-base-buffer-visiting file)))
  11108. (if buf
  11109. buf ; just return it
  11110. ;; Make a new buffer and remember it
  11111. (setq buf (find-file-noselect file))
  11112. (if buf (push buf org-agenda-new-buffers))
  11113. buf)))
  11114. (defun org-release-buffers (blist)
  11115. "Release all buffers in list, asking the user for confirmation when needed.
  11116. When a buffer is unmodified, it is just killed. When modified, it is saved
  11117. \(if the user agrees) and then killed."
  11118. (let (buf file)
  11119. (while (setq buf (pop blist))
  11120. (setq file (buffer-file-name buf))
  11121. (when (and (buffer-modified-p buf)
  11122. file
  11123. (y-or-n-p (format "Save file %s? " file)))
  11124. (with-current-buffer buf (save-buffer)))
  11125. (kill-buffer buf))))
  11126. (defun org-prepare-agenda-buffers (files)
  11127. "Create buffers for all agenda files, protect archived trees and comments."
  11128. (interactive)
  11129. (let ((pa '(:org-archived t))
  11130. (pc '(:org-comment t))
  11131. (pall '(:org-archived t :org-comment t))
  11132. (inhibit-read-only t)
  11133. (rea (concat ":" org-archive-tag ":"))
  11134. bmp file re)
  11135. (save-excursion
  11136. (save-restriction
  11137. (while (setq file (pop files))
  11138. (if (bufferp file)
  11139. (set-buffer file)
  11140. (org-check-agenda-file file)
  11141. (set-buffer (org-get-agenda-file-buffer file)))
  11142. (widen)
  11143. (setq bmp (buffer-modified-p))
  11144. (org-refresh-category-properties)
  11145. (setq org-todo-keywords-for-agenda
  11146. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11147. (setq org-done-keywords-for-agenda
  11148. (append org-done-keywords-for-agenda org-done-keywords))
  11149. (setq org-todo-keyword-alist-for-agenda
  11150. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11151. (setq org-tag-alist-for-agenda
  11152. (append org-tag-alist-for-agenda org-tag-alist))
  11153. (save-excursion
  11154. (remove-text-properties (point-min) (point-max) pall)
  11155. (when org-agenda-skip-archived-trees
  11156. (goto-char (point-min))
  11157. (while (re-search-forward rea nil t)
  11158. (if (org-on-heading-p t)
  11159. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11160. (goto-char (point-min))
  11161. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11162. (while (re-search-forward re nil t)
  11163. (add-text-properties
  11164. (match-beginning 0) (org-end-of-subtree t) pc)))
  11165. (set-buffer-modified-p bmp))))
  11166. (setq org-todo-keyword-alist-for-agenda
  11167. (org-uniquify org-todo-keyword-alist-for-agenda)
  11168. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11169. ;;;; Embedded LaTeX
  11170. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11171. "Keymap for the minor `org-cdlatex-mode'.")
  11172. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11173. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11174. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11175. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11176. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11177. (defvar org-cdlatex-texmathp-advice-is-done nil
  11178. "Flag remembering if we have applied the advice to texmathp already.")
  11179. (define-minor-mode org-cdlatex-mode
  11180. "Toggle the minor `org-cdlatex-mode'.
  11181. This mode supports entering LaTeX environment and math in LaTeX fragments
  11182. in Org-mode.
  11183. \\{org-cdlatex-mode-map}"
  11184. nil " OCDL" nil
  11185. (when org-cdlatex-mode (require 'cdlatex))
  11186. (unless org-cdlatex-texmathp-advice-is-done
  11187. (setq org-cdlatex-texmathp-advice-is-done t)
  11188. (defadvice texmathp (around org-math-always-on activate)
  11189. "Always return t in org-mode buffers.
  11190. This is because we want to insert math symbols without dollars even outside
  11191. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11192. an embedded LaTeX fragment, let texmathp do its job.
  11193. \\[org-cdlatex-mode-map]"
  11194. (interactive)
  11195. (let (p)
  11196. (cond
  11197. ((not (org-mode-p)) ad-do-it)
  11198. ((eq this-command 'cdlatex-math-symbol)
  11199. (setq ad-return-value t
  11200. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11201. (t
  11202. (let ((p (org-inside-LaTeX-fragment-p)))
  11203. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11204. (setq ad-return-value t
  11205. texmathp-why '("Org-mode embedded math" . 0))
  11206. (if p ad-do-it)))))))))
  11207. (defun turn-on-org-cdlatex ()
  11208. "Unconditionally turn on `org-cdlatex-mode'."
  11209. (org-cdlatex-mode 1))
  11210. (defun org-inside-LaTeX-fragment-p ()
  11211. "Test if point is inside a LaTeX fragment.
  11212. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11213. sequence appearing also before point.
  11214. Even though the matchers for math are configurable, this function assumes
  11215. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11216. delimiters are skipped when they have been removed by customization.
  11217. The return value is nil, or a cons cell with the delimiter and
  11218. and the position of this delimiter.
  11219. This function does a reasonably good job, but can locally be fooled by
  11220. for example currency specifications. For example it will assume being in
  11221. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11222. fragments that are properly closed, but during editing, we have to live
  11223. with the uncertainty caused by missing closing delimiters. This function
  11224. looks only before point, not after."
  11225. (catch 'exit
  11226. (let ((pos (point))
  11227. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11228. (lim (progn
  11229. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11230. (point)))
  11231. dd-on str (start 0) m re)
  11232. (goto-char pos)
  11233. (when dodollar
  11234. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11235. re (nth 1 (assoc "$" org-latex-regexps)))
  11236. (while (string-match re str start)
  11237. (cond
  11238. ((= (match-end 0) (length str))
  11239. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11240. ((= (match-end 0) (- (length str) 5))
  11241. (throw 'exit nil))
  11242. (t (setq start (match-end 0))))))
  11243. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11244. (goto-char pos)
  11245. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11246. (and (match-beginning 2) (throw 'exit nil))
  11247. ;; count $$
  11248. (while (re-search-backward "\\$\\$" lim t)
  11249. (setq dd-on (not dd-on)))
  11250. (goto-char pos)
  11251. (if dd-on (cons "$$" m))))))
  11252. (defun org-try-cdlatex-tab ()
  11253. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11254. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11255. - inside a LaTeX fragment, or
  11256. - after the first word in a line, where an abbreviation expansion could
  11257. insert a LaTeX environment."
  11258. (when org-cdlatex-mode
  11259. (cond
  11260. ((save-excursion
  11261. (skip-chars-backward "a-zA-Z0-9*")
  11262. (skip-chars-backward " \t")
  11263. (bolp))
  11264. (cdlatex-tab) t)
  11265. ((org-inside-LaTeX-fragment-p)
  11266. (cdlatex-tab) t)
  11267. (t nil))))
  11268. (defun org-cdlatex-underscore-caret (&optional arg)
  11269. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11270. Revert to the normal definition outside of these fragments."
  11271. (interactive "P")
  11272. (if (org-inside-LaTeX-fragment-p)
  11273. (call-interactively 'cdlatex-sub-superscript)
  11274. (let (org-cdlatex-mode)
  11275. (call-interactively (key-binding (vector last-input-event))))))
  11276. (defun org-cdlatex-math-modify (&optional arg)
  11277. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11278. Revert to the normal definition outside of these fragments."
  11279. (interactive "P")
  11280. (if (org-inside-LaTeX-fragment-p)
  11281. (call-interactively 'cdlatex-math-modify)
  11282. (let (org-cdlatex-mode)
  11283. (call-interactively (key-binding (vector last-input-event))))))
  11284. (defvar org-latex-fragment-image-overlays nil
  11285. "List of overlays carrying the images of latex fragments.")
  11286. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11287. (defun org-remove-latex-fragment-image-overlays ()
  11288. "Remove all overlays with LaTeX fragment images in current buffer."
  11289. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11290. (setq org-latex-fragment-image-overlays nil))
  11291. (defun org-preview-latex-fragment (&optional subtree)
  11292. "Preview the LaTeX fragment at point, or all locally or globally.
  11293. If the cursor is in a LaTeX fragment, create the image and overlay
  11294. it over the source code. If there is no fragment at point, display
  11295. all fragments in the current text, from one headline to the next. With
  11296. prefix SUBTREE, display all fragments in the current subtree. With a
  11297. double prefix `C-u C-u', or when the cursor is before the first headline,
  11298. display all fragments in the buffer.
  11299. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11300. (interactive "P")
  11301. (org-remove-latex-fragment-image-overlays)
  11302. (save-excursion
  11303. (save-restriction
  11304. (let (beg end at msg)
  11305. (cond
  11306. ((or (equal subtree '(16))
  11307. (not (save-excursion
  11308. (re-search-backward (concat "^" outline-regexp) nil t))))
  11309. (setq beg (point-min) end (point-max)
  11310. msg "Creating images for buffer...%s"))
  11311. ((equal subtree '(4))
  11312. (org-back-to-heading)
  11313. (setq beg (point) end (org-end-of-subtree t)
  11314. msg "Creating images for subtree...%s"))
  11315. (t
  11316. (if (setq at (org-inside-LaTeX-fragment-p))
  11317. (goto-char (max (point-min) (- (cdr at) 2)))
  11318. (org-back-to-heading))
  11319. (setq beg (point) end (progn (outline-next-heading) (point))
  11320. msg (if at "Creating image...%s"
  11321. "Creating images for entry...%s"))))
  11322. (message msg "")
  11323. (narrow-to-region beg end)
  11324. (goto-char beg)
  11325. (org-format-latex
  11326. (concat "ltxpng/" (file-name-sans-extension
  11327. (file-name-nondirectory
  11328. buffer-file-name)))
  11329. default-directory 'overlays msg at 'forbuffer)
  11330. (message msg "done. Use `C-c C-c' to remove images.")))))
  11331. (defvar org-latex-regexps
  11332. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11333. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11334. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11335. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11336. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11337. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11338. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11339. "Regular expressions for matching embedded LaTeX.")
  11340. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11341. "Replace LaTeX fragments with links to an image, and produce images."
  11342. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11343. (let* ((prefixnodir (file-name-nondirectory prefix))
  11344. (absprefix (expand-file-name prefix dir))
  11345. (todir (file-name-directory absprefix))
  11346. (opt org-format-latex-options)
  11347. (matchers (plist-get opt :matchers))
  11348. (re-list org-latex-regexps)
  11349. (cnt 0) txt link beg end re e checkdir
  11350. m n block linkfile movefile ov)
  11351. ;; Check if there are old images files with this prefix, and remove them
  11352. (when (file-directory-p todir)
  11353. (mapc 'delete-file
  11354. (directory-files
  11355. todir 'full
  11356. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11357. ;; Check the different regular expressions
  11358. (while (setq e (pop re-list))
  11359. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11360. block (if (nth 3 e) "\n\n" ""))
  11361. (when (member m matchers)
  11362. (goto-char (point-min))
  11363. (while (re-search-forward re nil t)
  11364. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11365. (setq txt (match-string n)
  11366. beg (match-beginning n) end (match-end n)
  11367. cnt (1+ cnt)
  11368. linkfile (format "%s_%04d.png" prefix cnt)
  11369. movefile (format "%s_%04d.png" absprefix cnt)
  11370. link (concat block "[[file:" linkfile "]]" block))
  11371. (if msg (message msg cnt))
  11372. (goto-char beg)
  11373. (unless checkdir ; make sure the directory exists
  11374. (setq checkdir t)
  11375. (or (file-directory-p todir) (make-directory todir)))
  11376. (org-create-formula-image
  11377. txt movefile opt forbuffer)
  11378. (if overlays
  11379. (progn
  11380. (setq ov (org-make-overlay beg end))
  11381. (if (featurep 'xemacs)
  11382. (progn
  11383. (org-overlay-put ov 'invisible t)
  11384. (org-overlay-put
  11385. ov 'end-glyph
  11386. (make-glyph (vector 'png :file movefile))))
  11387. (org-overlay-put
  11388. ov 'display
  11389. (list 'image :type 'png :file movefile :ascent 'center)))
  11390. (push ov org-latex-fragment-image-overlays)
  11391. (goto-char end))
  11392. (delete-region beg end)
  11393. (insert link))))))))
  11394. ;; This function borrows from Ganesh Swami's latex2png.el
  11395. (defun org-create-formula-image (string tofile options buffer)
  11396. (let* ((tmpdir (if (featurep 'xemacs)
  11397. (temp-directory)
  11398. temporary-file-directory))
  11399. (texfilebase (make-temp-name
  11400. (expand-file-name "orgtex" tmpdir)))
  11401. (texfile (concat texfilebase ".tex"))
  11402. (dvifile (concat texfilebase ".dvi"))
  11403. (pngfile (concat texfilebase ".png"))
  11404. (fnh (if (featurep 'xemacs)
  11405. (font-height (get-face-font 'default))
  11406. (face-attribute 'default :height nil)))
  11407. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11408. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11409. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11410. "Black"))
  11411. (bg (or (plist-get options (if buffer :background :html-background))
  11412. "Transparent")))
  11413. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11414. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11415. (with-temp-file texfile
  11416. (insert org-format-latex-header
  11417. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11418. (let ((dir default-directory))
  11419. (condition-case nil
  11420. (progn
  11421. (cd tmpdir)
  11422. (call-process "latex" nil nil nil texfile))
  11423. (error nil))
  11424. (cd dir))
  11425. (if (not (file-exists-p dvifile))
  11426. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11427. (condition-case nil
  11428. (call-process "dvipng" nil nil nil
  11429. "-E" "-fg" fg "-bg" bg
  11430. "-D" dpi
  11431. ;;"-x" scale "-y" scale
  11432. "-T" "tight"
  11433. "-o" pngfile
  11434. dvifile)
  11435. (error nil))
  11436. (if (not (file-exists-p pngfile))
  11437. (progn (message "Failed to create png file from %s" texfile) nil)
  11438. ;; Use the requested file name and clean up
  11439. (copy-file pngfile tofile 'replace)
  11440. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11441. (delete-file (concat texfilebase e)))
  11442. pngfile))))
  11443. (defun org-dvipng-color (attr)
  11444. "Return an rgb color specification for dvipng."
  11445. (apply 'format "rgb %s %s %s"
  11446. (mapcar 'org-normalize-color
  11447. (color-values (face-attribute 'default attr nil)))))
  11448. (defun org-normalize-color (value)
  11449. "Return string to be used as color value for an RGB component."
  11450. (format "%g" (/ value 65535.0)))
  11451. ;;;; Key bindings
  11452. ;; Make `C-c C-x' a prefix key
  11453. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11454. ;; TAB key with modifiers
  11455. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11456. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11457. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11458. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11459. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11460. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11461. ;; The following line is necessary under Suse GNU/Linux
  11462. (unless (featurep 'xemacs)
  11463. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11464. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11465. (define-key org-mode-map [backtab] 'org-shifttab)
  11466. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11467. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11468. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11469. ;; Cursor keys with modifiers
  11470. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11471. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11472. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11473. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11474. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11475. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11476. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11477. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11478. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11479. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11480. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11481. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11482. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11483. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11484. ;;; Extra keys for tty access.
  11485. ;; We only set them when really needed because otherwise the
  11486. ;; menus don't show the simple keys
  11487. (when (or org-use-extra-keys
  11488. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11489. (not window-system))
  11490. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11491. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11492. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11493. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11494. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11495. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11496. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11497. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11498. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11499. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11500. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11501. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11502. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11503. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11504. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11505. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11506. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11507. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11508. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11509. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11510. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11511. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11512. ;; All the other keys
  11513. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11514. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11515. (if (boundp 'narrow-map)
  11516. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11517. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11518. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11519. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11520. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11521. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11522. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11523. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11524. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11525. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11526. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11527. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11528. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11529. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11530. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11531. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11532. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11533. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11534. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11535. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11536. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11537. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11538. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11539. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11540. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11541. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11542. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11543. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11544. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11545. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11546. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11547. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11548. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11549. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11550. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11551. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11552. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11553. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11554. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11555. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11556. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11557. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11558. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11559. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11560. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11561. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11562. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11563. (org-defkey org-mode-map "\C-m" 'org-return)
  11564. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11565. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11566. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11567. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11568. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11569. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11570. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11571. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11572. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11573. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11574. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11575. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11576. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11577. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11578. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11579. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11580. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  11581. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11582. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11583. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11584. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11585. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11586. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11587. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11588. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11589. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11590. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11591. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11592. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11593. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11594. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11595. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11596. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11597. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  11598. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  11599. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  11600. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11601. (when (featurep 'xemacs)
  11602. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11603. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11604. (defun org-self-insert-command (N)
  11605. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11606. If the cursor is in a table looking at whitespace, the whitespace is
  11607. overwritten, and the table is not marked as requiring realignment."
  11608. (interactive "p")
  11609. (if (and (org-table-p)
  11610. (progn
  11611. ;; check if we blank the field, and if that triggers align
  11612. (and (featurep 'org-table) org-table-auto-blank-field
  11613. (member last-command
  11614. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11615. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11616. ;; got extra space, this field does not determine column width
  11617. (let (org-table-may-need-update) (org-table-blank-field))
  11618. ;; no extra space, this field may determine column width
  11619. (org-table-blank-field)))
  11620. t)
  11621. (eq N 1)
  11622. (looking-at "[^|\n]* |"))
  11623. (let (org-table-may-need-update)
  11624. (goto-char (1- (match-end 0)))
  11625. (delete-backward-char 1)
  11626. (goto-char (match-beginning 0))
  11627. (self-insert-command N))
  11628. (setq org-table-may-need-update t)
  11629. (self-insert-command N)
  11630. (org-fix-tags-on-the-fly)))
  11631. (defun org-fix-tags-on-the-fly ()
  11632. (when (and (equal (char-after (point-at-bol)) ?*)
  11633. (org-on-heading-p))
  11634. (org-align-tags-here org-tags-column)))
  11635. (defun org-delete-backward-char (N)
  11636. "Like `delete-backward-char', insert whitespace at field end in tables.
  11637. When deleting backwards, in tables this function will insert whitespace in
  11638. front of the next \"|\" separator, to keep the table aligned. The table will
  11639. still be marked for re-alignment if the field did fill the entire column,
  11640. because, in this case the deletion might narrow the column."
  11641. (interactive "p")
  11642. (if (and (org-table-p)
  11643. (eq N 1)
  11644. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11645. (looking-at ".*?|"))
  11646. (let ((pos (point))
  11647. (noalign (looking-at "[^|\n\r]* |"))
  11648. (c org-table-may-need-update))
  11649. (backward-delete-char N)
  11650. (skip-chars-forward "^|")
  11651. (insert " ")
  11652. (goto-char (1- pos))
  11653. ;; noalign: if there were two spaces at the end, this field
  11654. ;; does not determine the width of the column.
  11655. (if noalign (setq org-table-may-need-update c)))
  11656. (backward-delete-char N)
  11657. (org-fix-tags-on-the-fly)))
  11658. (defun org-delete-char (N)
  11659. "Like `delete-char', but insert whitespace at field end in tables.
  11660. When deleting characters, in tables this function will insert whitespace in
  11661. front of the next \"|\" separator, to keep the table aligned. The table will
  11662. still be marked for re-alignment if the field did fill the entire column,
  11663. because, in this case the deletion might narrow the column."
  11664. (interactive "p")
  11665. (if (and (org-table-p)
  11666. (not (bolp))
  11667. (not (= (char-after) ?|))
  11668. (eq N 1))
  11669. (if (looking-at ".*?|")
  11670. (let ((pos (point))
  11671. (noalign (looking-at "[^|\n\r]* |"))
  11672. (c org-table-may-need-update))
  11673. (replace-match (concat
  11674. (substring (match-string 0) 1 -1)
  11675. " |"))
  11676. (goto-char pos)
  11677. ;; noalign: if there were two spaces at the end, this field
  11678. ;; does not determine the width of the column.
  11679. (if noalign (setq org-table-may-need-update c)))
  11680. (delete-char N))
  11681. (delete-char N)
  11682. (org-fix-tags-on-the-fly)))
  11683. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11684. (put 'org-self-insert-command 'delete-selection t)
  11685. (put 'orgtbl-self-insert-command 'delete-selection t)
  11686. (put 'org-delete-char 'delete-selection 'supersede)
  11687. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11688. ;; Make `flyspell-mode' delay after some commands
  11689. (put 'org-self-insert-command 'flyspell-delayed t)
  11690. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11691. (put 'org-delete-char 'flyspell-delayed t)
  11692. (put 'org-delete-backward-char 'flyspell-delayed t)
  11693. ;; Make pabbrev-mode expand after org-mode commands
  11694. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11695. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  11696. ;; How to do this: Measure non-white length of current string
  11697. ;; If equal to column width, we should realign.
  11698. (defun org-remap (map &rest commands)
  11699. "In MAP, remap the functions given in COMMANDS.
  11700. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11701. (let (new old)
  11702. (while commands
  11703. (setq old (pop commands) new (pop commands))
  11704. (if (fboundp 'command-remapping)
  11705. (org-defkey map (vector 'remap old) new)
  11706. (substitute-key-definition old new map global-map)))))
  11707. (when (eq org-enable-table-editor 'optimized)
  11708. ;; If the user wants maximum table support, we need to hijack
  11709. ;; some standard editing functions
  11710. (org-remap org-mode-map
  11711. 'self-insert-command 'org-self-insert-command
  11712. 'delete-char 'org-delete-char
  11713. 'delete-backward-char 'org-delete-backward-char)
  11714. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11715. (defun org-shiftcursor-error ()
  11716. "Throw an error because Shift-Cursor command was applied in wrong context."
  11717. (error "This command is active in special context like tables, headlines or timestamps"))
  11718. (defun org-shifttab (&optional arg)
  11719. "Global visibility cycling or move to previous table field.
  11720. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11721. on context.
  11722. See the individual commands for more information."
  11723. (interactive "P")
  11724. (cond
  11725. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11726. ((integerp arg)
  11727. (message "Content view to level: %d" arg)
  11728. (org-content (prefix-numeric-value arg))
  11729. (setq org-cycle-global-status 'overview))
  11730. (t (call-interactively 'org-global-cycle))))
  11731. (defun org-shiftmetaleft ()
  11732. "Promote subtree or delete table column.
  11733. Calls `org-promote-subtree', `org-outdent-item',
  11734. or `org-table-delete-column', depending on context.
  11735. See the individual commands for more information."
  11736. (interactive)
  11737. (cond
  11738. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11739. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11740. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11741. (t (org-shiftcursor-error))))
  11742. (defun org-shiftmetaright ()
  11743. "Demote subtree or insert table column.
  11744. Calls `org-demote-subtree', `org-indent-item',
  11745. or `org-table-insert-column', depending on context.
  11746. See the individual commands for more information."
  11747. (interactive)
  11748. (cond
  11749. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11750. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11751. ((org-at-item-p) (call-interactively 'org-indent-item))
  11752. (t (org-shiftcursor-error))))
  11753. (defun org-shiftmetaup (&optional arg)
  11754. "Move subtree up or kill table row.
  11755. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11756. `org-move-item-up' depending on context. See the individual commands
  11757. for more information."
  11758. (interactive "P")
  11759. (cond
  11760. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11761. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11762. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11763. (t (org-shiftcursor-error))))
  11764. (defun org-shiftmetadown (&optional arg)
  11765. "Move subtree down or insert table row.
  11766. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11767. `org-move-item-down', depending on context. See the individual
  11768. commands for more information."
  11769. (interactive "P")
  11770. (cond
  11771. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11772. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11773. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11774. (t (org-shiftcursor-error))))
  11775. (defun org-metaleft (&optional arg)
  11776. "Promote heading or move table column to left.
  11777. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11778. With no specific context, calls the Emacs default `backward-word'.
  11779. See the individual commands for more information."
  11780. (interactive "P")
  11781. (cond
  11782. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11783. ((or (org-on-heading-p) (org-region-active-p))
  11784. (call-interactively 'org-do-promote))
  11785. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11786. (t (call-interactively 'backward-word))))
  11787. (defun org-metaright (&optional arg)
  11788. "Demote subtree or move table column to right.
  11789. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11790. With no specific context, calls the Emacs default `forward-word'.
  11791. See the individual commands for more information."
  11792. (interactive "P")
  11793. (cond
  11794. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11795. ((or (org-on-heading-p) (org-region-active-p))
  11796. (call-interactively 'org-do-demote))
  11797. ((org-at-item-p) (call-interactively 'org-indent-item))
  11798. (t (call-interactively 'forward-word))))
  11799. (defun org-metaup (&optional arg)
  11800. "Move subtree up or move table row up.
  11801. Calls `org-move-subtree-up' or `org-table-move-row' or
  11802. `org-move-item-up', depending on context. See the individual commands
  11803. for more information."
  11804. (interactive "P")
  11805. (cond
  11806. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11807. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11808. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11809. (t (transpose-lines 1) (beginning-of-line -1))))
  11810. (defun org-metadown (&optional arg)
  11811. "Move subtree down or move table row down.
  11812. Calls `org-move-subtree-down' or `org-table-move-row' or
  11813. `org-move-item-down', depending on context. See the individual
  11814. commands for more information."
  11815. (interactive "P")
  11816. (cond
  11817. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11818. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11819. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11820. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11821. (defun org-shiftup (&optional arg)
  11822. "Increase item in timestamp or increase priority of current headline.
  11823. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11824. depending on context. See the individual commands for more information."
  11825. (interactive "P")
  11826. (cond
  11827. ((org-at-timestamp-p t)
  11828. (call-interactively (if org-edit-timestamp-down-means-later
  11829. 'org-timestamp-down 'org-timestamp-up)))
  11830. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11831. ((org-at-item-p) (call-interactively 'org-previous-item))
  11832. ((org-clocktable-try-shift 'up arg))
  11833. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11834. (defun org-shiftdown (&optional arg)
  11835. "Decrease item in timestamp or decrease priority of current headline.
  11836. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11837. depending on context. See the individual commands for more information."
  11838. (interactive "P")
  11839. (cond
  11840. ((org-at-timestamp-p t)
  11841. (call-interactively (if org-edit-timestamp-down-means-later
  11842. 'org-timestamp-up 'org-timestamp-down)))
  11843. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11844. ((org-clocktable-try-shift 'down arg))
  11845. (t (call-interactively 'org-next-item))))
  11846. (defun org-shiftright (&optional arg)
  11847. "Cycle the thing at point or in the current line, depending on context.
  11848. Depending on context, this does one of the following:
  11849. - switch a timestamp at point one day into the future
  11850. - on a headline, switch to the next TODO keyword.
  11851. - on an item, switch entire list to the next bullet type
  11852. - on a property line, switch to the next allowed value
  11853. - on a clocktable definition line, move time block into the future"
  11854. (interactive "P")
  11855. (cond
  11856. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11857. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11858. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11859. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11860. ((org-clocktable-try-shift 'right arg))
  11861. (t (org-shiftcursor-error))))
  11862. (defun org-shiftleft (&optional arg)
  11863. "Cycle the thing at point or in the current line, depending on context.
  11864. Depending on context, this does one of the following:
  11865. - switch a timestamp at point one day into the past
  11866. - on a headline, switch to the previous TODO keyword.
  11867. - on an item, switch entire list to the previous bullet type
  11868. - on a property line, switch to the previous allowed value
  11869. - on a clocktable definition line, move time block into the past"
  11870. (interactive "P")
  11871. (cond
  11872. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11873. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11874. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11875. ((org-at-property-p)
  11876. (call-interactively 'org-property-previous-allowed-value))
  11877. ((org-clocktable-try-shift 'left arg))
  11878. (t (org-shiftcursor-error))))
  11879. (defun org-shiftcontrolright ()
  11880. "Switch to next TODO set."
  11881. (interactive)
  11882. (cond
  11883. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11884. (t (org-shiftcursor-error))))
  11885. (defun org-shiftcontrolleft ()
  11886. "Switch to previous TODO set."
  11887. (interactive)
  11888. (cond
  11889. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11890. (t (org-shiftcursor-error))))
  11891. (defun org-ctrl-c-ret ()
  11892. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11893. (interactive)
  11894. (cond
  11895. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11896. (t (call-interactively 'org-insert-heading))))
  11897. (defun org-copy-special ()
  11898. "Copy region in table or copy current subtree.
  11899. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11900. See the individual commands for more information."
  11901. (interactive)
  11902. (call-interactively
  11903. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11904. (defun org-cut-special ()
  11905. "Cut region in table or cut current subtree.
  11906. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11907. See the individual commands for more information."
  11908. (interactive)
  11909. (call-interactively
  11910. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11911. (defun org-paste-special (arg)
  11912. "Paste rectangular region into table, or past subtree relative to level.
  11913. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11914. See the individual commands for more information."
  11915. (interactive "P")
  11916. (if (org-at-table-p)
  11917. (org-table-paste-rectangle)
  11918. (org-paste-subtree arg)))
  11919. (defun org-edit-special ()
  11920. "Call a special editor for the stuff at point.
  11921. When at a table, call the formula editor with `org-table-edit-formulas'.
  11922. When at the first line of an src example, call `org-edit-src-code'.
  11923. When in an #+include line, visit the include file. Otherwise call
  11924. `ffap' to visit the file at point."
  11925. (interactive)
  11926. (cond
  11927. ((org-at-table-p)
  11928. (call-interactively 'org-table-edit-formulas))
  11929. ((save-excursion
  11930. (beginning-of-line 1)
  11931. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11932. (find-file (org-trim (match-string 1))))
  11933. ((org-edit-src-code))
  11934. ((org-edit-fixed-width-region))
  11935. (t (call-interactively 'ffap))))
  11936. (defun org-ctrl-c-ctrl-c (&optional arg)
  11937. "Set tags in headline, or update according to changed information at point.
  11938. This command does many different things, depending on context:
  11939. - If the cursor is in a headline, prompt for tags and insert them
  11940. into the current line, aligned to `org-tags-column'. When called
  11941. with prefix arg, realign all tags in the current buffer.
  11942. - If the cursor is in one of the special #+KEYWORD lines, this
  11943. triggers scanning the buffer for these lines and updating the
  11944. information.
  11945. - If the cursor is inside a table, realign the table. This command
  11946. works even if the automatic table editor has been turned off.
  11947. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11948. the entire table.
  11949. - If the cursor is at a footnote reference or definition, jump to
  11950. the corresponding definition or references, respectively.
  11951. - If the cursor is a the beginning of a dynamic block, update it.
  11952. - If the cursor is inside a table created by the table.el package,
  11953. activate that table.
  11954. - If the current buffer is a remember buffer, close note and file
  11955. it. A prefix argument of 1 files to the default location
  11956. without further interaction. A prefix argument of 2 files to
  11957. the currently clocking task.
  11958. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11959. links in this buffer.
  11960. - If the cursor is on a numbered item in a plain list, renumber the
  11961. ordered list.
  11962. - If the cursor is on a checkbox, toggle it."
  11963. (interactive "P")
  11964. (let ((org-enable-table-editor t))
  11965. (cond
  11966. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11967. org-occur-highlights
  11968. org-latex-fragment-image-overlays)
  11969. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11970. (org-remove-occur-highlights)
  11971. (org-remove-latex-fragment-image-overlays)
  11972. (message "Temporary highlights/overlays removed from current buffer"))
  11973. ((and (local-variable-p 'org-finish-function (current-buffer))
  11974. (fboundp org-finish-function))
  11975. (funcall org-finish-function))
  11976. ((org-at-property-p)
  11977. (call-interactively 'org-property-action))
  11978. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11979. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11980. ((org-at-table.el-p)
  11981. (require 'table)
  11982. (beginning-of-line 1)
  11983. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11984. (call-interactively 'table-recognize-table))
  11985. ((org-at-table-p)
  11986. (org-table-maybe-eval-formula)
  11987. (if arg
  11988. (call-interactively 'org-table-recalculate)
  11989. (org-table-maybe-recalculate-line))
  11990. (call-interactively 'org-table-align))
  11991. ((or (org-footnote-at-reference-p)
  11992. (org-footnote-at-definition-p))
  11993. (call-interactively 'org-footnote-action))
  11994. ((org-at-item-checkbox-p)
  11995. (call-interactively 'org-toggle-checkbox))
  11996. ((org-at-item-p)
  11997. (call-interactively 'org-maybe-renumber-ordered-list))
  11998. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11999. ;; Dynamic block
  12000. (beginning-of-line 1)
  12001. (save-excursion (org-update-dblock)))
  12002. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12003. (cond
  12004. ((equal (match-string 1) "TBLFM")
  12005. ;; Recalculate the table before this line
  12006. (save-excursion
  12007. (beginning-of-line 1)
  12008. (skip-chars-backward " \r\n\t")
  12009. (if (org-at-table-p)
  12010. (org-call-with-arg 'org-table-recalculate t))))
  12011. (t
  12012. ; (org-set-regexps-and-options)
  12013. ; (org-restart-font-lock)
  12014. (let ((org-inhibit-startup t)) (org-mode-restart))
  12015. (message "Local setup has been refreshed"))))
  12016. (t (error "C-c C-c can do nothing useful at this location.")))))
  12017. (defun org-mode-restart ()
  12018. "Restart Org-mode, to scan again for special lines.
  12019. Also updates the keyword regular expressions."
  12020. (interactive)
  12021. (org-mode)
  12022. (message "Org-mode restarted"))
  12023. (defun org-kill-note-or-show-branches ()
  12024. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12025. (interactive)
  12026. (if (not org-finish-function)
  12027. (call-interactively 'show-branches)
  12028. (let ((org-note-abort t))
  12029. (funcall org-finish-function))))
  12030. (defun org-return (&optional indent)
  12031. "Goto next table row or insert a newline.
  12032. Calls `org-table-next-row' or `newline', depending on context.
  12033. See the individual commands for more information."
  12034. (interactive)
  12035. (cond
  12036. ((bobp) (if indent (newline-and-indent) (newline)))
  12037. ((and (org-at-heading-p)
  12038. (looking-at
  12039. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12040. (org-show-entry)
  12041. (end-of-line 1)
  12042. (newline))
  12043. ((org-at-table-p)
  12044. (org-table-justify-field-maybe)
  12045. (call-interactively 'org-table-next-row))
  12046. (t (if indent (newline-and-indent) (newline)))))
  12047. (defun org-return-indent ()
  12048. "Goto next table row or insert a newline and indent.
  12049. Calls `org-table-next-row' or `newline-and-indent', depending on
  12050. context. See the individual commands for more information."
  12051. (interactive)
  12052. (org-return t))
  12053. (defun org-ctrl-c-star ()
  12054. "Compute table, or change heading status of lines.
  12055. Calls `org-table-recalculate' or `org-toggle-region-headings',
  12056. depending on context. This will also turn a plain list item or a normal
  12057. line into a subheading."
  12058. (interactive)
  12059. (cond
  12060. ((org-at-table-p)
  12061. (call-interactively 'org-table-recalculate))
  12062. ((org-region-active-p)
  12063. ;; Convert all lines in region to list items
  12064. (call-interactively 'org-toggle-region-headings))
  12065. ((org-on-heading-p)
  12066. (org-toggle-region-headings (point-at-bol)
  12067. (min (1+ (point-at-eol)) (point-max))))
  12068. ((org-at-item-p)
  12069. ;; Convert to heading
  12070. (let ((level (save-match-data
  12071. (save-excursion
  12072. (condition-case nil
  12073. (progn
  12074. (org-back-to-heading t)
  12075. (funcall outline-level))
  12076. (error 0))))))
  12077. (replace-match
  12078. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  12079. (t (org-toggle-region-headings (point-at-bol)
  12080. (min (1+ (point-at-eol)) (point-max))))))
  12081. (defun org-ctrl-c-minus ()
  12082. "Insert separator line in table or modify bullet status of line.
  12083. Also turns a plain line or a region of lines into list items.
  12084. Calls `org-table-insert-hline', `org-toggle-region-items', or
  12085. `org-cycle-list-bullet', depending on context."
  12086. (interactive)
  12087. (cond
  12088. ((org-at-table-p)
  12089. (call-interactively 'org-table-insert-hline))
  12090. ((org-on-heading-p)
  12091. ;; Convert to item
  12092. (save-excursion
  12093. (beginning-of-line 1)
  12094. (if (looking-at "\\*+ ")
  12095. (replace-match
  12096. (concat (make-string
  12097. (- (match-end 0) (point) (if org-odd-levels-only 2 1)) ?\ )
  12098. "- ")))))
  12099. ((org-region-active-p)
  12100. ;; Convert all lines in region to list items
  12101. (call-interactively 'org-toggle-region-items))
  12102. ((org-in-item-p)
  12103. (call-interactively 'org-cycle-list-bullet))
  12104. (t (org-toggle-region-items (point-at-bol)
  12105. (min (1+ (point-at-eol)) (point-max))))))
  12106. (defun org-toggle-region-items (beg end)
  12107. "Convert all lines in region to list items.
  12108. If the first line is already an item, convert all list items in the region
  12109. to normal lines."
  12110. (interactive "r")
  12111. (let (l2 l)
  12112. (save-excursion
  12113. (goto-char end)
  12114. (setq l2 (org-current-line))
  12115. (goto-char beg)
  12116. (beginning-of-line 1)
  12117. (setq l (1- (org-current-line)))
  12118. (if (org-at-item-p)
  12119. ;; We already have items, de-itemize
  12120. (while (< (setq l (1+ l)) l2)
  12121. (when (org-at-item-p)
  12122. (goto-char (match-beginning 2))
  12123. (delete-region (match-beginning 2) (match-end 2))
  12124. (and (looking-at "[ \t]+") (replace-match "")))
  12125. (beginning-of-line 2))
  12126. (while (< (setq l (1+ l)) l2)
  12127. (unless (org-at-item-p)
  12128. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12129. (replace-match "\\1- \\2")))
  12130. (beginning-of-line 2))))))
  12131. (defun org-toggle-region-headings (beg end)
  12132. "Convert all lines in region to list items.
  12133. If the first line is already an item, convert all list items in the region
  12134. to normal lines."
  12135. (interactive "r")
  12136. (let (l2 l)
  12137. (save-excursion
  12138. (goto-char end)
  12139. (setq l2 (org-current-line))
  12140. (goto-char beg)
  12141. (beginning-of-line 1)
  12142. (setq l (1- (org-current-line)))
  12143. (if (org-on-heading-p)
  12144. ;; We already have headlines, de-star them
  12145. (while (< (setq l (1+ l)) l2)
  12146. (when (org-on-heading-p t)
  12147. (and (looking-at outline-regexp) (replace-match "")))
  12148. (beginning-of-line 2))
  12149. (let* ((stars (save-excursion
  12150. (re-search-backward org-complex-heading-regexp nil t)
  12151. (or (match-string 1) "*")))
  12152. (add-stars (if org-odd-levels-only "**" "*"))
  12153. (rpl (concat stars add-stars " \\2")))
  12154. (while (< (setq l (1+ l)) l2)
  12155. (unless (org-on-heading-p)
  12156. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12157. (replace-match rpl)))
  12158. (beginning-of-line 2)))))))
  12159. (defun org-meta-return (&optional arg)
  12160. "Insert a new heading or wrap a region in a table.
  12161. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12162. See the individual commands for more information."
  12163. (interactive "P")
  12164. (cond
  12165. ((org-at-table-p)
  12166. (call-interactively 'org-table-wrap-region))
  12167. (t (call-interactively 'org-insert-heading))))
  12168. ;;; Menu entries
  12169. ;; Define the Org-mode menus
  12170. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12171. '("Tbl"
  12172. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12173. ["Next Field" org-cycle (org-at-table-p)]
  12174. ["Previous Field" org-shifttab (org-at-table-p)]
  12175. ["Next Row" org-return (org-at-table-p)]
  12176. "--"
  12177. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12178. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12179. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12180. "--"
  12181. ("Column"
  12182. ["Move Column Left" org-metaleft (org-at-table-p)]
  12183. ["Move Column Right" org-metaright (org-at-table-p)]
  12184. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12185. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12186. ("Row"
  12187. ["Move Row Up" org-metaup (org-at-table-p)]
  12188. ["Move Row Down" org-metadown (org-at-table-p)]
  12189. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12190. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12191. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12192. "--"
  12193. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12194. ("Rectangle"
  12195. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12196. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12197. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12198. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12199. "--"
  12200. ("Calculate"
  12201. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12202. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12203. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12204. "--"
  12205. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12206. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12207. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12208. "--"
  12209. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12210. "--"
  12211. ["Sum Column/Rectangle" org-table-sum
  12212. (or (org-at-table-p) (org-region-active-p))]
  12213. ["Which Column?" org-table-current-column (org-at-table-p)])
  12214. ["Debug Formulas"
  12215. org-table-toggle-formula-debugger
  12216. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12217. ["Show Col/Row Numbers"
  12218. org-table-toggle-coordinate-overlays
  12219. :style toggle
  12220. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12221. "--"
  12222. ["Create" org-table-create (and (not (org-at-table-p))
  12223. org-enable-table-editor)]
  12224. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12225. ["Import from File" org-table-import (not (org-at-table-p))]
  12226. ["Export to File" org-table-export (org-at-table-p)]
  12227. "--"
  12228. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12229. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12230. '("Org"
  12231. ("Show/Hide"
  12232. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12233. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12234. ["Sparse Tree..." org-sparse-tree t]
  12235. ["Reveal Context" org-reveal t]
  12236. ["Show All" show-all t]
  12237. "--"
  12238. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12239. "--"
  12240. ["New Heading" org-insert-heading t]
  12241. ("Navigate Headings"
  12242. ["Up" outline-up-heading t]
  12243. ["Next" outline-next-visible-heading t]
  12244. ["Previous" outline-previous-visible-heading t]
  12245. ["Next Same Level" outline-forward-same-level t]
  12246. ["Previous Same Level" outline-backward-same-level t]
  12247. "--"
  12248. ["Jump" org-goto t])
  12249. ("Edit Structure"
  12250. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12251. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12252. "--"
  12253. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12254. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12255. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12256. "--"
  12257. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12258. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12259. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12260. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12261. "--"
  12262. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12263. "--"
  12264. ["Convert to odd levels" org-convert-to-odd-levels t]
  12265. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12266. ("Editing"
  12267. ["Emphasis..." org-emphasize t]
  12268. ["Edit Source Example" org-edit-special t]
  12269. "--"
  12270. ["Footnote new/jump" org-footnote-action t]
  12271. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12272. ("Archive"
  12273. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12274. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12275. ; :active t :keys "C-u C-c C-x C-a"]
  12276. ["Sparse trees open ARCHIVE trees"
  12277. (setq org-sparse-tree-open-archived-trees
  12278. (not org-sparse-tree-open-archived-trees))
  12279. :style toggle :selected org-sparse-tree-open-archived-trees]
  12280. ["Cycling opens ARCHIVE trees"
  12281. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12282. :style toggle :selected org-cycle-open-archived-trees]
  12283. "--"
  12284. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12285. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12286. ; ["Check and Move Children" (org-archive-subtree '(4))
  12287. ; :active t :keys "C-u C-c C-x C-s"]
  12288. )
  12289. "--"
  12290. ("TODO Lists"
  12291. ["TODO/DONE/-" org-todo t]
  12292. ("Select keyword"
  12293. ["Next keyword" org-shiftright (org-on-heading-p)]
  12294. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12295. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12296. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12297. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12298. ["Show TODO Tree" org-show-todo-tree t]
  12299. ["Global TODO list" org-todo-list t]
  12300. "--"
  12301. ["Set Priority" org-priority t]
  12302. ["Priority Up" org-shiftup t]
  12303. ["Priority Down" org-shiftdown t])
  12304. ("TAGS and Properties"
  12305. ["Set Tags" org-set-tags-command t]
  12306. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12307. "--"
  12308. ["Set property" org-set-property t]
  12309. ["Column view of properties" org-columns t]
  12310. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12311. ("Dates and Scheduling"
  12312. ["Timestamp" org-time-stamp t]
  12313. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12314. ("Change Date"
  12315. ["1 Day Later" org-shiftright t]
  12316. ["1 Day Earlier" org-shiftleft t]
  12317. ["1 ... Later" org-shiftup t]
  12318. ["1 ... Earlier" org-shiftdown t])
  12319. ["Compute Time Range" org-evaluate-time-range t]
  12320. ["Schedule Item" org-schedule t]
  12321. ["Deadline" org-deadline t]
  12322. "--"
  12323. ["Custom time format" org-toggle-time-stamp-overlays
  12324. :style radio :selected org-display-custom-times]
  12325. "--"
  12326. ["Goto Calendar" org-goto-calendar t]
  12327. ["Date from Calendar" org-date-from-calendar t]
  12328. "--"
  12329. ["Start/restart timer" org-timer-start t]
  12330. ["Insert timer string" org-timer t]
  12331. ["Insert timer item" org-timer-item t])
  12332. ("Logging work"
  12333. ["Clock in" org-clock-in t]
  12334. ["Clock out" org-clock-out t]
  12335. ["Clock cancel" org-clock-cancel t]
  12336. ["Goto running clock" org-clock-goto t]
  12337. ["Display times" org-clock-display t]
  12338. ["Create clock table" org-clock-report t]
  12339. "--"
  12340. ["Record DONE time"
  12341. (progn (setq org-log-done (not org-log-done))
  12342. (message "Switching to %s will %s record a timestamp"
  12343. (car org-done-keywords)
  12344. (if org-log-done "automatically" "not")))
  12345. :style toggle :selected org-log-done])
  12346. "--"
  12347. ["Agenda Command..." org-agenda t]
  12348. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12349. ("File List for Agenda")
  12350. ("Special views current file"
  12351. ["TODO Tree" org-show-todo-tree t]
  12352. ["Check Deadlines" org-check-deadlines t]
  12353. ["Timeline" org-timeline t]
  12354. ["Tags Tree" org-tags-sparse-tree t])
  12355. "--"
  12356. ("Hyperlinks"
  12357. ["Store Link (Global)" org-store-link t]
  12358. ["Insert Link" org-insert-link t]
  12359. ["Follow Link" org-open-at-point t]
  12360. "--"
  12361. ["Next link" org-next-link t]
  12362. ["Previous link" org-previous-link t]
  12363. "--"
  12364. ["Descriptive Links"
  12365. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12366. :style radio
  12367. :selected (member '(org-link) buffer-invisibility-spec)]
  12368. ["Literal Links"
  12369. (progn
  12370. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12371. :style radio
  12372. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12373. "--"
  12374. ["Export/Publish..." org-export t]
  12375. ("LaTeX"
  12376. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12377. :selected org-cdlatex-mode]
  12378. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12379. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12380. ["Modify math symbol" org-cdlatex-math-modify
  12381. (org-inside-LaTeX-fragment-p)]
  12382. ["Export LaTeX fragments as images"
  12383. (if (featurep 'org-exp)
  12384. (setq org-export-with-LaTeX-fragments
  12385. (not org-export-with-LaTeX-fragments))
  12386. (require 'org-exp))
  12387. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12388. org-export-with-LaTeX-fragments)])
  12389. "--"
  12390. ("Documentation"
  12391. ["Show Version" org-version t]
  12392. ["Info Documentation" org-info t])
  12393. ("Customize"
  12394. ["Browse Org Group" org-customize t]
  12395. "--"
  12396. ["Expand This Menu" org-create-customize-menu
  12397. (fboundp 'customize-menu-create)])
  12398. "--"
  12399. ["Refresh setup" org-mode-restart t]
  12400. ))
  12401. (defun org-info (&optional node)
  12402. "Read documentation for Org-mode in the info system.
  12403. With optional NODE, go directly to that node."
  12404. (interactive)
  12405. (info (format "(org)%s" (or node ""))))
  12406. (defun org-install-agenda-files-menu ()
  12407. (let ((bl (buffer-list)))
  12408. (save-excursion
  12409. (while bl
  12410. (set-buffer (pop bl))
  12411. (if (org-mode-p) (setq bl nil)))
  12412. (when (org-mode-p)
  12413. (easy-menu-change
  12414. '("Org") "File List for Agenda"
  12415. (append
  12416. (list
  12417. ["Edit File List" (org-edit-agenda-file-list) t]
  12418. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12419. ["Remove Current File from List" org-remove-file t]
  12420. ["Cycle through agenda files" org-cycle-agenda-files t]
  12421. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12422. "--")
  12423. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12424. ;;;; Documentation
  12425. ;;;###autoload
  12426. (defun org-require-autoloaded-modules ()
  12427. (interactive)
  12428. (mapc 'require
  12429. '(org-agenda org-archive org-clock org-colview
  12430. org-exp org-id org-export-latex org-publish
  12431. org-remember org-table)))
  12432. ;;;###autoload
  12433. (defun org-customize ()
  12434. "Call the customize function with org as argument."
  12435. (interactive)
  12436. (org-load-modules-maybe)
  12437. (org-require-autoloaded-modules)
  12438. (customize-browse 'org))
  12439. (defun org-create-customize-menu ()
  12440. "Create a full customization menu for Org-mode, insert it into the menu."
  12441. (interactive)
  12442. (org-load-modules-maybe)
  12443. (org-require-autoloaded-modules)
  12444. (if (fboundp 'customize-menu-create)
  12445. (progn
  12446. (easy-menu-change
  12447. '("Org") "Customize"
  12448. `(["Browse Org group" org-customize t]
  12449. "--"
  12450. ,(customize-menu-create 'org)
  12451. ["Set" Custom-set t]
  12452. ["Save" Custom-save t]
  12453. ["Reset to Current" Custom-reset-current t]
  12454. ["Reset to Saved" Custom-reset-saved t]
  12455. ["Reset to Standard Settings" Custom-reset-standard t]))
  12456. (message "\"Org\"-menu now contains full customization menu"))
  12457. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12458. ;;;; Miscellaneous stuff
  12459. ;;; Generally useful functions
  12460. (defun org-find-text-property-in-string (prop s)
  12461. "Return the first non-nil value of property PROP in string S."
  12462. (or (get-text-property 0 prop s)
  12463. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12464. prop s)))
  12465. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12466. "Display the given MESSAGE as a warning."
  12467. (if (fboundp 'display-warning)
  12468. (display-warning 'org message
  12469. (if (featurep 'xemacs)
  12470. 'warning
  12471. :warning))
  12472. (let ((buf (get-buffer-create "*Org warnings*")))
  12473. (with-current-buffer buf
  12474. (goto-char (point-max))
  12475. (insert "Warning (Org): " message)
  12476. (unless (bolp)
  12477. (newline)))
  12478. (display-buffer buf)
  12479. (sit-for 0))))
  12480. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12481. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12482. (if (and marker (marker-buffer marker)
  12483. (buffer-live-p (marker-buffer marker)))
  12484. (progn
  12485. (switch-to-buffer (marker-buffer marker))
  12486. (if (or (> marker (point-max)) (< marker (point-min)))
  12487. (widen))
  12488. (goto-char marker))
  12489. (if bookmark
  12490. (bookmark-jump bookmark)
  12491. (error "Cannot find location"))))
  12492. (defun org-quote-csv-field (s)
  12493. "Quote field for inclusion in CSV material."
  12494. (if (string-match "[\",]" s)
  12495. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12496. s))
  12497. (defun org-plist-delete (plist property)
  12498. "Delete PROPERTY from PLIST.
  12499. This is in contrast to merely setting it to 0."
  12500. (let (p)
  12501. (while plist
  12502. (if (not (eq property (car plist)))
  12503. (setq p (plist-put p (car plist) (nth 1 plist))))
  12504. (setq plist (cddr plist)))
  12505. p))
  12506. (defun org-force-self-insert (N)
  12507. "Needed to enforce self-insert under remapping."
  12508. (interactive "p")
  12509. (self-insert-command N))
  12510. (defun org-string-width (s)
  12511. "Compute width of string, ignoring invisible characters.
  12512. This ignores character with invisibility property `org-link', and also
  12513. characters with property `org-cwidth', because these will become invisible
  12514. upon the next fontification round."
  12515. (let (b l)
  12516. (when (or (eq t buffer-invisibility-spec)
  12517. (assq 'org-link buffer-invisibility-spec))
  12518. (while (setq b (text-property-any 0 (length s)
  12519. 'invisible 'org-link s))
  12520. (setq s (concat (substring s 0 b)
  12521. (substring s (or (next-single-property-change
  12522. b 'invisible s) (length s)))))))
  12523. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12524. (setq s (concat (substring s 0 b)
  12525. (substring s (or (next-single-property-change
  12526. b 'org-cwidth s) (length s))))))
  12527. (setq l (string-width s) b -1)
  12528. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12529. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12530. l))
  12531. (defun org-get-indentation (&optional line)
  12532. "Get the indentation of the current line, interpreting tabs.
  12533. When LINE is given, assume it represents a line and compute its indentation."
  12534. (if line
  12535. (if (string-match "^ *" (org-remove-tabs line))
  12536. (match-end 0))
  12537. (save-excursion
  12538. (beginning-of-line 1)
  12539. (skip-chars-forward " \t")
  12540. (current-column))))
  12541. (defun org-remove-tabs (s &optional width)
  12542. "Replace tabulators in S with spaces.
  12543. Assumes that s is a single line, starting in column 0."
  12544. (setq width (or width tab-width))
  12545. (while (string-match "\t" s)
  12546. (setq s (replace-match
  12547. (make-string
  12548. (- (* width (/ (+ (match-beginning 0) width) width))
  12549. (match-beginning 0)) ?\ )
  12550. t t s)))
  12551. s)
  12552. (defun org-fix-indentation (line ind)
  12553. "Fix indentation in LINE.
  12554. IND is a cons cell with target and minimum indentation.
  12555. If the current indentation in LINE is smaller than the minimum,
  12556. leave it alone. If it is larger than ind, set it to the target."
  12557. (let* ((l (org-remove-tabs line))
  12558. (i (org-get-indentation l))
  12559. (i1 (car ind)) (i2 (cdr ind)))
  12560. (if (>= i i2) (setq l (substring line i2)))
  12561. (if (> i1 0)
  12562. (concat (make-string i1 ?\ ) l)
  12563. l)))
  12564. (defun org-base-buffer (buffer)
  12565. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12566. (if (not buffer)
  12567. buffer
  12568. (or (buffer-base-buffer buffer)
  12569. buffer)))
  12570. (defun org-trim (s)
  12571. "Remove whitespace at beginning and end of string."
  12572. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12573. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12574. s)
  12575. (defun org-wrap (string &optional width lines)
  12576. "Wrap string to either a number of lines, or a width in characters.
  12577. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12578. that costs. If there is a word longer than WIDTH, the text is actually
  12579. wrapped to the length of that word.
  12580. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12581. many lines, whatever width that takes.
  12582. The return value is a list of lines, without newlines at the end."
  12583. (let* ((words (org-split-string string "[ \t\n]+"))
  12584. (maxword (apply 'max (mapcar 'org-string-width words)))
  12585. w ll)
  12586. (cond (width
  12587. (org-do-wrap words (max maxword width)))
  12588. (lines
  12589. (setq w maxword)
  12590. (setq ll (org-do-wrap words maxword))
  12591. (if (<= (length ll) lines)
  12592. ll
  12593. (setq ll words)
  12594. (while (> (length ll) lines)
  12595. (setq w (1+ w))
  12596. (setq ll (org-do-wrap words w)))
  12597. ll))
  12598. (t (error "Cannot wrap this")))))
  12599. (defun org-do-wrap (words width)
  12600. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12601. (let (lines line)
  12602. (while words
  12603. (setq line (pop words))
  12604. (while (and words (< (+ (length line) (length (car words))) width))
  12605. (setq line (concat line " " (pop words))))
  12606. (setq lines (push line lines)))
  12607. (nreverse lines)))
  12608. (defun org-split-string (string &optional separators)
  12609. "Splits STRING into substrings at SEPARATORS.
  12610. No empty strings are returned if there are matches at the beginning
  12611. and end of string."
  12612. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12613. (start 0)
  12614. notfirst
  12615. (list nil))
  12616. (while (and (string-match rexp string
  12617. (if (and notfirst
  12618. (= start (match-beginning 0))
  12619. (< start (length string)))
  12620. (1+ start) start))
  12621. (< (match-beginning 0) (length string)))
  12622. (setq notfirst t)
  12623. (or (eq (match-beginning 0) 0)
  12624. (and (eq (match-beginning 0) (match-end 0))
  12625. (eq (match-beginning 0) start))
  12626. (setq list
  12627. (cons (substring string start (match-beginning 0))
  12628. list)))
  12629. (setq start (match-end 0)))
  12630. (or (eq start (length string))
  12631. (setq list
  12632. (cons (substring string start)
  12633. list)))
  12634. (nreverse list)))
  12635. (defun org-context ()
  12636. "Return a list of contexts of the current cursor position.
  12637. If several contexts apply, all are returned.
  12638. Each context entry is a list with a symbol naming the context, and
  12639. two positions indicating start and end of the context. Possible
  12640. contexts are:
  12641. :headline anywhere in a headline
  12642. :headline-stars on the leading stars in a headline
  12643. :todo-keyword on a TODO keyword (including DONE) in a headline
  12644. :tags on the TAGS in a headline
  12645. :priority on the priority cookie in a headline
  12646. :item on the first line of a plain list item
  12647. :item-bullet on the bullet/number of a plain list item
  12648. :checkbox on the checkbox in a plain list item
  12649. :table in an org-mode table
  12650. :table-special on a special filed in a table
  12651. :table-table in a table.el table
  12652. :link on a hyperlink
  12653. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12654. :target on a <<target>>
  12655. :radio-target on a <<<radio-target>>>
  12656. :latex-fragment on a LaTeX fragment
  12657. :latex-preview on a LaTeX fragment with overlayed preview image
  12658. This function expects the position to be visible because it uses font-lock
  12659. faces as a help to recognize the following contexts: :table-special, :link,
  12660. and :keyword."
  12661. (let* ((f (get-text-property (point) 'face))
  12662. (faces (if (listp f) f (list f)))
  12663. (p (point)) clist o)
  12664. ;; First the large context
  12665. (cond
  12666. ((org-on-heading-p t)
  12667. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12668. (when (progn
  12669. (beginning-of-line 1)
  12670. (looking-at org-todo-line-tags-regexp))
  12671. (push (org-point-in-group p 1 :headline-stars) clist)
  12672. (push (org-point-in-group p 2 :todo-keyword) clist)
  12673. (push (org-point-in-group p 4 :tags) clist))
  12674. (goto-char p)
  12675. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12676. (if (looking-at "\\[#[A-Z0-9]\\]")
  12677. (push (org-point-in-group p 0 :priority) clist)))
  12678. ((org-at-item-p)
  12679. (push (org-point-in-group p 2 :item-bullet) clist)
  12680. (push (list :item (point-at-bol)
  12681. (save-excursion (org-end-of-item) (point)))
  12682. clist)
  12683. (and (org-at-item-checkbox-p)
  12684. (push (org-point-in-group p 0 :checkbox) clist)))
  12685. ((org-at-table-p)
  12686. (push (list :table (org-table-begin) (org-table-end)) clist)
  12687. (if (memq 'org-formula faces)
  12688. (push (list :table-special
  12689. (previous-single-property-change p 'face)
  12690. (next-single-property-change p 'face)) clist)))
  12691. ((org-at-table-p 'any)
  12692. (push (list :table-table) clist)))
  12693. (goto-char p)
  12694. ;; Now the small context
  12695. (cond
  12696. ((org-at-timestamp-p)
  12697. (push (org-point-in-group p 0 :timestamp) clist))
  12698. ((memq 'org-link faces)
  12699. (push (list :link
  12700. (previous-single-property-change p 'face)
  12701. (next-single-property-change p 'face)) clist))
  12702. ((memq 'org-special-keyword faces)
  12703. (push (list :keyword
  12704. (previous-single-property-change p 'face)
  12705. (next-single-property-change p 'face)) clist))
  12706. ((org-on-target-p)
  12707. (push (org-point-in-group p 0 :target) clist)
  12708. (goto-char (1- (match-beginning 0)))
  12709. (if (looking-at org-radio-target-regexp)
  12710. (push (org-point-in-group p 0 :radio-target) clist))
  12711. (goto-char p))
  12712. ((setq o (car (delq nil
  12713. (mapcar
  12714. (lambda (x)
  12715. (if (memq x org-latex-fragment-image-overlays) x))
  12716. (org-overlays-at (point))))))
  12717. (push (list :latex-fragment
  12718. (org-overlay-start o) (org-overlay-end o)) clist)
  12719. (push (list :latex-preview
  12720. (org-overlay-start o) (org-overlay-end o)) clist))
  12721. ((org-inside-LaTeX-fragment-p)
  12722. ;; FIXME: positions wrong.
  12723. (push (list :latex-fragment (point) (point)) clist)))
  12724. (setq clist (nreverse (delq nil clist)))
  12725. clist))
  12726. ;; FIXME: Compare with at-regexp-p Do we need both?
  12727. (defun org-in-regexp (re &optional nlines visually)
  12728. "Check if point is inside a match of regexp.
  12729. Normally only the current line is checked, but you can include NLINES extra
  12730. lines both before and after point into the search.
  12731. If VISUALLY is set, require that the cursor is not after the match but
  12732. really on, so that the block visually is on the match."
  12733. (catch 'exit
  12734. (let ((pos (point))
  12735. (eol (point-at-eol (+ 1 (or nlines 0))))
  12736. (inc (if visually 1 0)))
  12737. (save-excursion
  12738. (beginning-of-line (- 1 (or nlines 0)))
  12739. (while (re-search-forward re eol t)
  12740. (if (and (<= (match-beginning 0) pos)
  12741. (>= (+ inc (match-end 0)) pos))
  12742. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12743. (defun org-at-regexp-p (regexp)
  12744. "Is point inside a match of REGEXP in the current line?"
  12745. (catch 'exit
  12746. (save-excursion
  12747. (let ((pos (point)) (end (point-at-eol)))
  12748. (beginning-of-line 1)
  12749. (while (re-search-forward regexp end t)
  12750. (if (and (<= (match-beginning 0) pos)
  12751. (>= (match-end 0) pos))
  12752. (throw 'exit t)))
  12753. nil))))
  12754. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12755. "Call `multi-occur' with buffers for all agenda files."
  12756. (interactive "sOrg-files matching: \np")
  12757. (let* ((files (org-agenda-files))
  12758. (tnames (mapcar 'file-truename files))
  12759. (extra org-agenda-text-search-extra-files)
  12760. f)
  12761. (when (eq (car extra) 'agenda-archives)
  12762. (setq extra (cdr extra))
  12763. (setq files (org-add-archive-files files)))
  12764. (while (setq f (pop extra))
  12765. (unless (member (file-truename f) tnames)
  12766. (add-to-list 'files f 'append)
  12767. (add-to-list 'tnames (file-truename f) 'append)))
  12768. (multi-occur
  12769. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12770. regexp)))
  12771. (if (boundp 'occur-mode-find-occurrence-hook)
  12772. ;; Emacs 23
  12773. (add-hook 'occur-mode-find-occurrence-hook
  12774. (lambda ()
  12775. (when (org-mode-p)
  12776. (org-reveal))))
  12777. ;; Emacs 22
  12778. (defadvice occur-mode-goto-occurrence
  12779. (after org-occur-reveal activate)
  12780. (and (org-mode-p) (org-reveal)))
  12781. (defadvice occur-mode-goto-occurrence-other-window
  12782. (after org-occur-reveal activate)
  12783. (and (org-mode-p) (org-reveal)))
  12784. (defadvice occur-mode-display-occurrence
  12785. (after org-occur-reveal activate)
  12786. (when (org-mode-p)
  12787. (let ((pos (occur-mode-find-occurrence)))
  12788. (with-current-buffer (marker-buffer pos)
  12789. (save-excursion
  12790. (goto-char pos)
  12791. (org-reveal)))))))
  12792. (defun org-uniquify (list)
  12793. "Remove duplicate elements from LIST."
  12794. (let (res)
  12795. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12796. res))
  12797. (defun org-delete-all (elts list)
  12798. "Remove all elements in ELTS from LIST."
  12799. (while elts
  12800. (setq list (delete (pop elts) list)))
  12801. list)
  12802. (defun org-back-over-empty-lines ()
  12803. "Move backwards over whitespace, to the beginning of the first empty line.
  12804. Returns the number of empty lines passed."
  12805. (let ((pos (point)))
  12806. (skip-chars-backward " \t\n\r")
  12807. (beginning-of-line 2)
  12808. (goto-char (min (point) pos))
  12809. (count-lines (point) pos)))
  12810. (defun org-skip-whitespace ()
  12811. (skip-chars-forward " \t\n\r"))
  12812. (defun org-point-in-group (point group &optional context)
  12813. "Check if POINT is in match-group GROUP.
  12814. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12815. match. If the match group does ot exist or point is not inside it,
  12816. return nil."
  12817. (and (match-beginning group)
  12818. (>= point (match-beginning group))
  12819. (<= point (match-end group))
  12820. (if context
  12821. (list context (match-beginning group) (match-end group))
  12822. t)))
  12823. (defun org-switch-to-buffer-other-window (&rest args)
  12824. "Switch to buffer in a second window on the current frame.
  12825. In particular, do not allow pop-up frames."
  12826. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12827. special-display-function)
  12828. (apply 'switch-to-buffer-other-window args)))
  12829. (defun org-combine-plists (&rest plists)
  12830. "Create a single property list from all plists in PLISTS.
  12831. The process starts by copying the first list, and then setting properties
  12832. from the other lists. Settings in the last list are the most significant
  12833. ones and overrule settings in the other lists."
  12834. (let ((rtn (copy-sequence (pop plists)))
  12835. p v ls)
  12836. (while plists
  12837. (setq ls (pop plists))
  12838. (while ls
  12839. (setq p (pop ls) v (pop ls))
  12840. (setq rtn (plist-put rtn p v))))
  12841. rtn))
  12842. (defun org-move-line-down (arg)
  12843. "Move the current line down. With prefix argument, move it past ARG lines."
  12844. (interactive "p")
  12845. (let ((col (current-column))
  12846. beg end pos)
  12847. (beginning-of-line 1) (setq beg (point))
  12848. (beginning-of-line 2) (setq end (point))
  12849. (beginning-of-line (+ 1 arg))
  12850. (setq pos (move-marker (make-marker) (point)))
  12851. (insert (delete-and-extract-region beg end))
  12852. (goto-char pos)
  12853. (org-move-to-column col)))
  12854. (defun org-move-line-up (arg)
  12855. "Move the current line up. With prefix argument, move it past ARG lines."
  12856. (interactive "p")
  12857. (let ((col (current-column))
  12858. beg end pos)
  12859. (beginning-of-line 1) (setq beg (point))
  12860. (beginning-of-line 2) (setq end (point))
  12861. (beginning-of-line (- arg))
  12862. (setq pos (move-marker (make-marker) (point)))
  12863. (insert (delete-and-extract-region beg end))
  12864. (goto-char pos)
  12865. (org-move-to-column col)))
  12866. (defun org-replace-escapes (string table)
  12867. "Replace %-escapes in STRING with values in TABLE.
  12868. TABLE is an association list with keys like \"%a\" and string values.
  12869. The sequences in STRING may contain normal field width and padding information,
  12870. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12871. so values can contain further %-escapes if they are define later in TABLE."
  12872. (let ((case-fold-search nil)
  12873. e re rpl)
  12874. (while (setq e (pop table))
  12875. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12876. (while (string-match re string)
  12877. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12878. (cdr e)))
  12879. (setq string (replace-match rpl t t string))))
  12880. string))
  12881. (defun org-sublist (list start end)
  12882. "Return a section of LIST, from START to END.
  12883. Counting starts at 1."
  12884. (let (rtn (c start))
  12885. (setq list (nthcdr (1- start) list))
  12886. (while (and list (<= c end))
  12887. (push (pop list) rtn)
  12888. (setq c (1+ c)))
  12889. (nreverse rtn)))
  12890. (defun org-find-base-buffer-visiting (file)
  12891. "Like `find-buffer-visiting' but alway return the base buffer and
  12892. not an indirect buffer."
  12893. (let ((buf (find-buffer-visiting file)))
  12894. (if buf
  12895. (or (buffer-base-buffer buf) buf)
  12896. nil)))
  12897. (defun org-image-file-name-regexp ()
  12898. "Return regexp matching the file names of images."
  12899. (if (fboundp 'image-file-name-regexp)
  12900. (image-file-name-regexp)
  12901. (let ((image-file-name-extensions
  12902. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12903. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12904. (concat "\\."
  12905. (regexp-opt (nconc (mapcar 'upcase
  12906. image-file-name-extensions)
  12907. image-file-name-extensions)
  12908. t)
  12909. "\\'"))))
  12910. (defun org-file-image-p (file)
  12911. "Return non-nil if FILE is an image."
  12912. (save-match-data
  12913. (string-match (org-image-file-name-regexp) file)))
  12914. (defun org-get-cursor-date ()
  12915. "Return the date at cursor in as a time.
  12916. This works in the calendar and in the agenda, anywhere else it just
  12917. returns the current time."
  12918. (let (date day defd)
  12919. (cond
  12920. ((eq major-mode 'calendar-mode)
  12921. (setq date (calendar-cursor-to-date)
  12922. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12923. ((eq major-mode 'org-agenda-mode)
  12924. (setq day (get-text-property (point) 'day))
  12925. (if day
  12926. (setq date (calendar-gregorian-from-absolute day)
  12927. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12928. (nth 2 date))))))
  12929. (or defd (current-time))))
  12930. (defvar org-agenda-action-marker (make-marker)
  12931. "Marker pointing to the entry for the next agenda action.")
  12932. (defun org-mark-entry-for-agenda-action ()
  12933. "Mark the current entry as target of an agenda action.
  12934. Agenda actions are actions executed from the agenda with the key `k',
  12935. which make use of the date at the cursor."
  12936. (interactive)
  12937. (move-marker org-agenda-action-marker
  12938. (save-excursion (org-back-to-heading t) (point))
  12939. (current-buffer))
  12940. (message
  12941. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12942. ;;; Paragraph filling stuff.
  12943. ;; We want this to be just right, so use the full arsenal.
  12944. (defun org-indent-line-function ()
  12945. "Indent line like previous, but further if previous was headline or item."
  12946. (interactive)
  12947. (let* ((pos (point))
  12948. (itemp (org-at-item-p))
  12949. column bpos bcol tpos tcol bullet btype bullet-type)
  12950. ;; Find the previous relevant line
  12951. (beginning-of-line 1)
  12952. (cond
  12953. ((looking-at "#") (setq column 0))
  12954. ((looking-at "\\*+ ") (setq column 0))
  12955. (t
  12956. (beginning-of-line 0)
  12957. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12958. (beginning-of-line 0))
  12959. (cond
  12960. ((looking-at "\\*+[ \t]+")
  12961. (if (not org-adapt-indentation)
  12962. (setq column 0)
  12963. (goto-char (match-end 0))
  12964. (setq column (current-column))))
  12965. ((org-in-item-p)
  12966. (org-beginning-of-item)
  12967. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12968. (setq bpos (match-beginning 1) tpos (match-end 0)
  12969. bcol (progn (goto-char bpos) (current-column))
  12970. tcol (progn (goto-char tpos) (current-column))
  12971. bullet (match-string 1)
  12972. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12973. (if (> tcol (+ bcol org-description-max-indent))
  12974. (setq tcol (+ bcol 5)))
  12975. (if (not itemp)
  12976. (setq column tcol)
  12977. (goto-char pos)
  12978. (beginning-of-line 1)
  12979. (if (looking-at "\\S-")
  12980. (progn
  12981. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12982. (setq bullet (match-string 1)
  12983. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12984. (setq column (if (equal btype bullet-type) bcol tcol)))
  12985. (setq column (org-get-indentation)))))
  12986. (t (setq column (org-get-indentation))))))
  12987. (goto-char pos)
  12988. (if (<= (current-column) (current-indentation))
  12989. (org-indent-line-to column)
  12990. (save-excursion (org-indent-line-to column)))
  12991. (setq column (current-column))
  12992. (beginning-of-line 1)
  12993. (if (looking-at
  12994. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12995. (replace-match (concat "\\1" (format org-property-format
  12996. (match-string 2) (match-string 3)))
  12997. t nil))
  12998. (org-move-to-column column)))
  12999. (defun org-set-autofill-regexps ()
  13000. (interactive)
  13001. ;; In the paragraph separator we include headlines, because filling
  13002. ;; text in a line directly attached to a headline would otherwise
  13003. ;; fill the headline as well.
  13004. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13005. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13006. ;; The paragraph starter includes hand-formatted lists.
  13007. (org-set-local 'paragraph-start
  13008. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13009. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13010. ;; But only if the user has not turned off tables or fixed-width regions
  13011. (org-set-local
  13012. 'auto-fill-inhibit-regexp
  13013. (concat "\\*+ \\|#\\+"
  13014. "\\|[ \t]*" org-keyword-time-regexp
  13015. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13016. (concat
  13017. "\\|[ \t]*["
  13018. (if org-enable-table-editor "|" "")
  13019. (if org-enable-fixed-width-editor ":" "")
  13020. "]"))))
  13021. ;; We use our own fill-paragraph function, to make sure that tables
  13022. ;; and fixed-width regions are not wrapped. That function will pass
  13023. ;; through to `fill-paragraph' when appropriate.
  13024. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13025. ; Adaptive filling: To get full control, first make sure that
  13026. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13027. (org-set-local 'adaptive-fill-regexp "\000")
  13028. (org-set-local 'adaptive-fill-function
  13029. 'org-adaptive-fill-function)
  13030. (org-set-local
  13031. 'align-mode-rules-list
  13032. '((org-in-buffer-settings
  13033. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13034. (modes . '(org-mode))))))
  13035. (defun org-fill-paragraph (&optional justify)
  13036. "Re-align a table, pass through to fill-paragraph if no table."
  13037. (let ((table-p (org-at-table-p))
  13038. (table.el-p (org-at-table.el-p)))
  13039. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13040. (save-excursion (goto-char (point-at-bol))
  13041. (looking-at outline-regexp)))
  13042. t) ; skip headlines
  13043. (table.el-p t) ; skip table.el tables
  13044. (table-p (org-table-align) t) ; align org-mode tables
  13045. (t nil)))) ; call paragraph-fill
  13046. ;; For reference, this is the default value of adaptive-fill-regexp
  13047. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13048. (defun org-adaptive-fill-function ()
  13049. "Return a fill prefix for org-mode files.
  13050. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13051. work correctly."
  13052. (cond ((looking-at "#[ \t]+")
  13053. (match-string 0))
  13054. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13055. (save-excursion
  13056. (if (> (match-end 1) (+ (match-beginning 1)
  13057. org-description-max-indent))
  13058. (goto-char (+ (match-beginning 1) 5))
  13059. (goto-char (match-end 0)))
  13060. (make-string (current-column) ?\ )))
  13061. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13062. (save-excursion
  13063. (goto-char (match-end 0))
  13064. (make-string (current-column) ?\ )))
  13065. (t nil)))
  13066. ;;; Other stuff.
  13067. (defun org-toggle-fixed-width-section (arg)
  13068. "Toggle the fixed-width export.
  13069. If there is no active region, the QUOTE keyword at the current headline is
  13070. inserted or removed. When present, it causes the text between this headline
  13071. and the next to be exported as fixed-width text, and unmodified.
  13072. If there is an active region, this command adds or removes a colon as the
  13073. first character of this line. If the first character of a line is a colon,
  13074. this line is also exported in fixed-width font."
  13075. (interactive "P")
  13076. (let* ((cc 0)
  13077. (regionp (org-region-active-p))
  13078. (beg (if regionp (region-beginning) (point)))
  13079. (end (if regionp (region-end)))
  13080. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13081. (case-fold-search nil)
  13082. (re "[ \t]*\\(:\\)")
  13083. off)
  13084. (if regionp
  13085. (save-excursion
  13086. (goto-char beg)
  13087. (setq cc (current-column))
  13088. (beginning-of-line 1)
  13089. (setq off (looking-at re))
  13090. (while (> nlines 0)
  13091. (setq nlines (1- nlines))
  13092. (beginning-of-line 1)
  13093. (cond
  13094. (arg
  13095. (org-move-to-column cc t)
  13096. (insert ":\n")
  13097. (forward-line -1))
  13098. ((and off (looking-at re))
  13099. (replace-match "" t t nil 1))
  13100. ((not off) (org-move-to-column cc t) (insert ":")))
  13101. (forward-line 1)))
  13102. (save-excursion
  13103. (org-back-to-heading)
  13104. (if (looking-at (concat outline-regexp
  13105. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13106. (replace-match "" t t nil 1)
  13107. (if (looking-at outline-regexp)
  13108. (progn
  13109. (goto-char (match-end 0))
  13110. (insert org-quote-string " "))))))))
  13111. ;;;; Functions extending outline functionality
  13112. (defun org-beginning-of-line (&optional arg)
  13113. "Go to the beginning of the current line. If that is invisible, continue
  13114. to a visible line beginning. This makes the function of C-a more intuitive.
  13115. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13116. first attempt, and only move to after the tags when the cursor is already
  13117. beyond the end of the headline."
  13118. (interactive "P")
  13119. (let ((pos (point)) refpos)
  13120. (beginning-of-line 1)
  13121. (if (bobp)
  13122. nil
  13123. (backward-char 1)
  13124. (if (org-invisible-p)
  13125. (while (and (not (bobp)) (org-invisible-p))
  13126. (backward-char 1)
  13127. (beginning-of-line 1))
  13128. (forward-char 1)))
  13129. (when org-special-ctrl-a/e
  13130. (cond
  13131. ((and (looking-at org-complex-heading-regexp)
  13132. (= (char-after (match-end 1)) ?\ ))
  13133. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13134. (point-at-eol)))
  13135. (goto-char
  13136. (if (eq org-special-ctrl-a/e t)
  13137. (cond ((> pos refpos) refpos)
  13138. ((= pos (point)) refpos)
  13139. (t (point)))
  13140. (cond ((> pos (point)) (point))
  13141. ((not (eq last-command this-command)) (point))
  13142. (t refpos)))))
  13143. ((org-at-item-p)
  13144. (goto-char
  13145. (if (eq org-special-ctrl-a/e t)
  13146. (cond ((> pos (match-end 4)) (match-end 4))
  13147. ((= pos (point)) (match-end 4))
  13148. (t (point)))
  13149. (cond ((> pos (point)) (point))
  13150. ((not (eq last-command this-command)) (point))
  13151. (t (match-end 4))))))))
  13152. (org-no-warnings
  13153. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13154. (defun org-end-of-line (&optional arg)
  13155. "Go to the end of the line.
  13156. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13157. first attempt, and only move to after the tags when the cursor is already
  13158. beyond the end of the headline."
  13159. (interactive "P")
  13160. (if (or (not org-special-ctrl-a/e)
  13161. (not (org-on-heading-p)))
  13162. (end-of-line arg)
  13163. (let ((pos (point)))
  13164. (beginning-of-line 1)
  13165. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13166. (if (eq org-special-ctrl-a/e t)
  13167. (if (or (< pos (match-beginning 1))
  13168. (= pos (match-end 0)))
  13169. (goto-char (match-beginning 1))
  13170. (goto-char (match-end 0)))
  13171. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13172. (goto-char (match-end 0))
  13173. (goto-char (match-beginning 1))))
  13174. (end-of-line arg))))
  13175. (org-no-warnings
  13176. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13177. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13178. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13179. (defun org-kill-line (&optional arg)
  13180. "Kill line, to tags or end of line."
  13181. (interactive "P")
  13182. (cond
  13183. ((or (not org-special-ctrl-k)
  13184. (bolp)
  13185. (not (org-on-heading-p)))
  13186. (call-interactively 'kill-line))
  13187. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13188. (kill-region (point) (match-beginning 1))
  13189. (org-set-tags nil t))
  13190. (t (kill-region (point) (point-at-eol)))))
  13191. (define-key org-mode-map "\C-k" 'org-kill-line)
  13192. (defun org-yank (&optional arg)
  13193. "Yank. If the kill is a subtree, treat it specially.
  13194. This command will look at the current kill and check if is a single
  13195. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13196. cursor is at the beginning of a line or after the stars of a currently
  13197. empty headline, then the yank is handled specially. How exactly depends
  13198. on the value of the following variables, both set by default.
  13199. org-yank-folded-subtrees
  13200. When set, the subtree(s) will be folded after insertion, but only
  13201. if doing so would now swallow text after the yanked text.
  13202. org-yank-adjusted-subtrees
  13203. When set, the subtree will be promoted or demoted in order to
  13204. fit into the local outline tree structure, which means that the level
  13205. will be adjusted so that it becomes the smaller one of the two
  13206. *visible* surrounding headings.
  13207. Any prefix to this command will cause `yank' to be called directly with
  13208. no special treatment. In particular, a simple `C-u' prefix will just
  13209. plainly yank the text as it is.
  13210. \[1] Basically, the test checks if the first non-white line is a heading
  13211. and if there are no other headings with fewer stars."
  13212. (interactive "P")
  13213. (setq this-command 'yank)
  13214. (if arg
  13215. (call-interactively 'yank)
  13216. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13217. (and (org-kill-is-subtree-p)
  13218. (or (bolp)
  13219. (and (looking-at "[ \t]*$")
  13220. (string-match
  13221. "\\`\\*+\\'"
  13222. (buffer-substring (point-at-bol) (point)))))))
  13223. swallowp)
  13224. (cond
  13225. ((and subtreep org-yank-folded-subtrees)
  13226. (let ((beg (point))
  13227. end)
  13228. (if (and subtreep org-yank-adjusted-subtrees)
  13229. (org-paste-subtree nil nil 'for-yank)
  13230. (call-interactively 'yank))
  13231. (setq end (point))
  13232. (goto-char beg)
  13233. (when (and (bolp) subtreep
  13234. (not (setq swallowp
  13235. (org-yank-folding-would-swallow-text beg end))))
  13236. (or (looking-at outline-regexp)
  13237. (re-search-forward (concat "^" outline-regexp) end t))
  13238. (while (and (< (point) end) (looking-at outline-regexp))
  13239. (hide-subtree)
  13240. (org-cycle-show-empty-lines 'folded)
  13241. (condition-case nil
  13242. (outline-forward-same-level 1)
  13243. (error (goto-char end)))))
  13244. (when swallowp
  13245. (message
  13246. "Yanked text not folded because that would swallow text"))
  13247. (goto-char end)
  13248. (skip-chars-forward " \t\n\r")
  13249. (beginning-of-line 1)
  13250. (push-mark beg 'nomsg)))
  13251. ((and subtreep org-yank-adjusted-subtrees)
  13252. (let ((beg (point-at-bol)))
  13253. (org-paste-subtree nil nil 'for-yank)
  13254. (push-mark beg 'nomsg)))
  13255. (t
  13256. (call-interactively 'yank))))))
  13257. (defun org-yank-folding-would-swallow-text (beg end)
  13258. "Would hide-subtree at BEG swallow any text after END?"
  13259. (let (level)
  13260. (save-excursion
  13261. (goto-char beg)
  13262. (when (or (looking-at outline-regexp)
  13263. (re-search-forward (concat "^" outline-regexp) end t))
  13264. (setq level (org-outline-level)))
  13265. (goto-char end)
  13266. (skip-chars-forward " \t\r\n\v\f")
  13267. (if (or (eobp)
  13268. (and (bolp) (looking-at org-outline-regexp)
  13269. (<= (org-outline-level) level)))
  13270. nil ; Nothing would be swallowed
  13271. t)))) ; something would swallow
  13272. (define-key org-mode-map "\C-y" 'org-yank)
  13273. (defun org-invisible-p ()
  13274. "Check if point is at a character currently not visible."
  13275. ;; Early versions of noutline don't have `outline-invisible-p'.
  13276. (if (fboundp 'outline-invisible-p)
  13277. (outline-invisible-p)
  13278. (get-char-property (point) 'invisible)))
  13279. (defun org-invisible-p2 ()
  13280. "Check if point is at a character currently not visible."
  13281. (save-excursion
  13282. (if (and (eolp) (not (bobp))) (backward-char 1))
  13283. ;; Early versions of noutline don't have `outline-invisible-p'.
  13284. (if (fboundp 'outline-invisible-p)
  13285. (outline-invisible-p)
  13286. (get-char-property (point) 'invisible))))
  13287. (defun org-back-to-heading (&optional invisible-ok)
  13288. "Call `outline-back-to-heading', but provide a better error message."
  13289. (condition-case nil
  13290. (outline-back-to-heading invisible-ok)
  13291. (error (error "Before first headline at position %d in buffer %s"
  13292. (point) (current-buffer)))))
  13293. (defun org-before-first-heading-p ()
  13294. "Before first heading?"
  13295. (save-excursion
  13296. (null (re-search-backward "^\\*+ " nil t))))
  13297. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13298. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13299. (defun org-at-heading-or-item-p ()
  13300. (or (org-on-heading-p) (org-at-item-p)))
  13301. (defun org-on-target-p ()
  13302. (or (org-in-regexp org-radio-target-regexp)
  13303. (org-in-regexp org-target-regexp)))
  13304. (defun org-up-heading-all (arg)
  13305. "Move to the heading line of which the present line is a subheading.
  13306. This function considers both visible and invisible heading lines.
  13307. With argument, move up ARG levels."
  13308. (if (fboundp 'outline-up-heading-all)
  13309. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13310. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13311. (defun org-up-heading-safe ()
  13312. "Move to the heading line of which the present line is a subheading.
  13313. This version will not throw an error. It will return the level of the
  13314. headline found, or nil if no higher level is found."
  13315. (let (start-level re)
  13316. (org-back-to-heading t)
  13317. (setq start-level (funcall outline-level))
  13318. (if (equal start-level 1)
  13319. nil
  13320. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13321. (if (re-search-backward re nil t)
  13322. (funcall outline-level)))))
  13323. (defun org-first-sibling-p ()
  13324. "Is this heading the first child of its parents?"
  13325. (interactive)
  13326. (let ((re (concat "^" outline-regexp))
  13327. level l)
  13328. (unless (org-at-heading-p t)
  13329. (error "Not at a heading"))
  13330. (setq level (funcall outline-level))
  13331. (save-excursion
  13332. (if (not (re-search-backward re nil t))
  13333. t
  13334. (setq l (funcall outline-level))
  13335. (< l level)))))
  13336. (defun org-goto-sibling (&optional previous)
  13337. "Goto the next sibling, even if it is invisible.
  13338. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13339. when a sibling was found. When none is found, return nil and don't
  13340. move point."
  13341. (let ((fun (if previous 're-search-backward 're-search-forward))
  13342. (pos (point))
  13343. (re (concat "^" outline-regexp))
  13344. level l)
  13345. (when (condition-case nil (org-back-to-heading t) (error nil))
  13346. (setq level (funcall outline-level))
  13347. (catch 'exit
  13348. (or previous (forward-char 1))
  13349. (while (funcall fun re nil t)
  13350. (setq l (funcall outline-level))
  13351. (when (< l level) (goto-char pos) (throw 'exit nil))
  13352. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13353. (goto-char pos)
  13354. nil))))
  13355. (defun org-show-siblings ()
  13356. "Show all siblings of the current headline."
  13357. (save-excursion
  13358. (while (org-goto-sibling) (org-flag-heading nil)))
  13359. (save-excursion
  13360. (while (org-goto-sibling 'previous)
  13361. (org-flag-heading nil))))
  13362. (defun org-show-hidden-entry ()
  13363. "Show an entry where even the heading is hidden."
  13364. (save-excursion
  13365. (org-show-entry)))
  13366. (defun org-flag-heading (flag &optional entry)
  13367. "Flag the current heading. FLAG non-nil means make invisible.
  13368. When ENTRY is non-nil, show the entire entry."
  13369. (save-excursion
  13370. (org-back-to-heading t)
  13371. ;; Check if we should show the entire entry
  13372. (if entry
  13373. (progn
  13374. (org-show-entry)
  13375. (save-excursion
  13376. (and (outline-next-heading)
  13377. (org-flag-heading nil))))
  13378. (outline-flag-region (max (point-min) (1- (point)))
  13379. (save-excursion (outline-end-of-heading) (point))
  13380. flag))))
  13381. (defun org-forward-same-level (arg)
  13382. "Move forward to the ARG'th subheading at same level as this one.
  13383. Stop at the first and last subheadings of a superior heading.
  13384. This is like outline-forward-same-level, but invisible headings are ok."
  13385. (interactive "p")
  13386. (org-back-to-heading t)
  13387. (while (> arg 0)
  13388. (let ((point-to-move-to (save-excursion
  13389. (org-get-next-sibling))))
  13390. (if point-to-move-to
  13391. (progn
  13392. (goto-char point-to-move-to)
  13393. (setq arg (1- arg)))
  13394. (progn
  13395. (setq arg 0)
  13396. (error "No following same-level heading"))))))
  13397. (defun org-get-next-sibling ()
  13398. "Move to next heading of the same level, and return point.
  13399. If there is no such heading, return nil.
  13400. This is like outline-next-sibling, but invisible headings are ok."
  13401. (let ((level (funcall outline-level)))
  13402. (outline-next-heading)
  13403. (while (and (not (eobp)) (> (funcall outline-level) level))
  13404. (outline-next-heading))
  13405. (if (or (eobp) (< (funcall outline-level) level))
  13406. nil
  13407. (point))))
  13408. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13409. ;; This is an exact copy of the original function, but it uses
  13410. ;; `org-back-to-heading', to make it work also in invisible
  13411. ;; trees. And is uses an invisible-OK argument.
  13412. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13413. (org-back-to-heading invisible-OK)
  13414. (let ((first t)
  13415. (level (funcall outline-level)))
  13416. (while (and (not (eobp))
  13417. (or first (> (funcall outline-level) level)))
  13418. (setq first nil)
  13419. (outline-next-heading))
  13420. (unless to-heading
  13421. (if (memq (preceding-char) '(?\n ?\^M))
  13422. (progn
  13423. ;; Go to end of line before heading
  13424. (forward-char -1)
  13425. (if (memq (preceding-char) '(?\n ?\^M))
  13426. ;; leave blank line before heading
  13427. (forward-char -1))))))
  13428. (point))
  13429. (defun org-show-subtree ()
  13430. "Show everything after this heading at deeper levels."
  13431. (outline-flag-region
  13432. (point)
  13433. (save-excursion
  13434. (outline-end-of-subtree) (outline-next-heading) (point))
  13435. nil))
  13436. (defun org-show-entry ()
  13437. "Show the body directly following this heading.
  13438. Show the heading too, if it is currently invisible."
  13439. (interactive)
  13440. (save-excursion
  13441. (condition-case nil
  13442. (progn
  13443. (org-back-to-heading t)
  13444. (outline-flag-region
  13445. (max (point-min) (1- (point)))
  13446. (save-excursion
  13447. (re-search-forward
  13448. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13449. (or (match-beginning 1) (point-max)))
  13450. nil))
  13451. (error nil))))
  13452. (defun org-make-options-regexp (kwds)
  13453. "Make a regular expression for keyword lines."
  13454. (concat
  13455. "^"
  13456. "#?[ \t]*\\+\\("
  13457. (mapconcat 'regexp-quote kwds "\\|")
  13458. "\\):[ \t]*"
  13459. "\\(.+\\)"))
  13460. ;; Make isearch reveal the necessary context
  13461. (defun org-isearch-end ()
  13462. "Reveal context after isearch exits."
  13463. (when isearch-success ; only if search was successful
  13464. (if (featurep 'xemacs)
  13465. ;; Under XEmacs, the hook is run in the correct place,
  13466. ;; we directly show the context.
  13467. (org-show-context 'isearch)
  13468. ;; In Emacs the hook runs *before* restoring the overlays.
  13469. ;; So we have to use a one-time post-command-hook to do this.
  13470. ;; (Emacs 22 has a special variable, see function `org-mode')
  13471. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13472. isearch-mode-end-hook-quit)
  13473. ;; Only when the isearch was not quitted.
  13474. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13475. 'append 'local)))))
  13476. (defun org-isearch-post-command ()
  13477. "Remove self from hook, and show context."
  13478. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13479. (org-show-context 'isearch))
  13480. ;;;; Integration with and fixes for other packages
  13481. ;;; Imenu support
  13482. (defvar org-imenu-markers nil
  13483. "All markers currently used by Imenu.")
  13484. (make-variable-buffer-local 'org-imenu-markers)
  13485. (defun org-imenu-new-marker (&optional pos)
  13486. "Return a new marker for use by Imenu, and remember the marker."
  13487. (let ((m (make-marker)))
  13488. (move-marker m (or pos (point)))
  13489. (push m org-imenu-markers)
  13490. m))
  13491. (defun org-imenu-get-tree ()
  13492. "Produce the index for Imenu."
  13493. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13494. (setq org-imenu-markers nil)
  13495. (let* ((n org-imenu-depth)
  13496. (re (concat "^" outline-regexp))
  13497. (subs (make-vector (1+ n) nil))
  13498. (last-level 0)
  13499. m tree level head)
  13500. (save-excursion
  13501. (save-restriction
  13502. (widen)
  13503. (goto-char (point-max))
  13504. (while (re-search-backward re nil t)
  13505. (setq level (org-reduced-level (funcall outline-level)))
  13506. (when (<= level n)
  13507. (looking-at org-complex-heading-regexp)
  13508. (setq head (org-link-display-format
  13509. (org-match-string-no-properties 4))
  13510. m (org-imenu-new-marker))
  13511. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13512. (if (>= level last-level)
  13513. (push (cons head m) (aref subs level))
  13514. (push (cons head (aref subs (1+ level))) (aref subs level))
  13515. (loop for i from (1+ level) to n do (aset subs i nil)))
  13516. (setq last-level level)))))
  13517. (aref subs 1)))
  13518. (eval-after-load "imenu"
  13519. '(progn
  13520. (add-hook 'imenu-after-jump-hook
  13521. (lambda ()
  13522. (if (eq major-mode 'org-mode)
  13523. (org-show-context 'org-goto))))))
  13524. (defun org-link-display-format (link)
  13525. "Replace a link with either the description, or the link target
  13526. if no description is present"
  13527. (save-match-data
  13528. (if (string-match org-bracket-link-analytic-regexp link)
  13529. (replace-match (or (match-string 5 link)
  13530. (concat (match-string 1 link)
  13531. (match-string 3 link)))
  13532. nil nil link)
  13533. link)))
  13534. ;; Speedbar support
  13535. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13536. "Overlay marking the agenda restriction line in speedbar.")
  13537. (org-overlay-put org-speedbar-restriction-lock-overlay
  13538. 'face 'org-agenda-restriction-lock)
  13539. (org-overlay-put org-speedbar-restriction-lock-overlay
  13540. 'help-echo "Agendas are currently limited to this item.")
  13541. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13542. (defun org-speedbar-set-agenda-restriction ()
  13543. "Restrict future agenda commands to the location at point in speedbar.
  13544. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13545. (interactive)
  13546. (require 'org-agenda)
  13547. (let (p m tp np dir txt w)
  13548. (cond
  13549. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13550. 'org-imenu t))
  13551. (setq m (get-text-property p 'org-imenu-marker))
  13552. (save-excursion
  13553. (save-restriction
  13554. (set-buffer (marker-buffer m))
  13555. (goto-char m)
  13556. (org-agenda-set-restriction-lock 'subtree))))
  13557. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13558. 'speedbar-function 'speedbar-find-file))
  13559. (setq tp (previous-single-property-change
  13560. (1+ p) 'speedbar-function)
  13561. np (next-single-property-change
  13562. tp 'speedbar-function)
  13563. dir (speedbar-line-directory)
  13564. txt (buffer-substring-no-properties (or tp (point-min))
  13565. (or np (point-max))))
  13566. (save-excursion
  13567. (save-restriction
  13568. (set-buffer (find-file-noselect
  13569. (let ((default-directory dir))
  13570. (expand-file-name txt))))
  13571. (unless (org-mode-p)
  13572. (error "Cannot restrict to non-Org-mode file"))
  13573. (org-agenda-set-restriction-lock 'file))))
  13574. (t (error "Don't know how to restrict Org-mode's agenda")))
  13575. (org-move-overlay org-speedbar-restriction-lock-overlay
  13576. (point-at-bol) (point-at-eol))
  13577. (setq current-prefix-arg nil)
  13578. (org-agenda-maybe-redo)))
  13579. (eval-after-load "speedbar"
  13580. '(progn
  13581. (speedbar-add-supported-extension ".org")
  13582. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13583. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13584. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13585. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13586. (add-hook 'speedbar-visiting-tag-hook
  13587. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  13588. ;;; Fixes and Hacks for problems with other packages
  13589. ;; Make flyspell not check words in links, to not mess up our keymap
  13590. (defun org-mode-flyspell-verify ()
  13591. "Don't let flyspell put overlays at active buttons."
  13592. (not (get-text-property (point) 'keymap)))
  13593. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13594. (eval-after-load "bookmark"
  13595. '(if (boundp 'bookmark-after-jump-hook)
  13596. ;; We can use the hook
  13597. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13598. ;; Hook not available, use advice
  13599. (defadvice bookmark-jump (after org-make-visible activate)
  13600. "Make the position visible."
  13601. (org-bookmark-jump-unhide))))
  13602. ;; Make sure saveplace show the location if it was hidden
  13603. (eval-after-load "saveplace"
  13604. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13605. "Make the position visible."
  13606. (org-bookmark-jump-unhide)))
  13607. (defun org-bookmark-jump-unhide ()
  13608. "Unhide the current position, to show the bookmark location."
  13609. (and (org-mode-p)
  13610. (or (org-invisible-p)
  13611. (save-excursion (goto-char (max (point-min) (1- (point))))
  13612. (org-invisible-p)))
  13613. (org-show-context 'bookmark-jump)))
  13614. ;; Make session.el ignore our circular variable
  13615. (eval-after-load "session"
  13616. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13617. ;;;; Experimental code
  13618. (defun org-closed-in-range ()
  13619. "Sparse tree of items closed in a certain time range.
  13620. Still experimental, may disappear in the future."
  13621. (interactive)
  13622. ;; Get the time interval from the user.
  13623. (let* ((time1 (time-to-seconds
  13624. (org-read-date nil 'to-time nil "Starting date: ")))
  13625. (time2 (time-to-seconds
  13626. (org-read-date nil 'to-time nil "End date:")))
  13627. ;; callback function
  13628. (callback (lambda ()
  13629. (let ((time
  13630. (time-to-seconds
  13631. (apply 'encode-time
  13632. (org-parse-time-string
  13633. (match-string 1))))))
  13634. ;; check if time in interval
  13635. (and (>= time time1) (<= time time2))))))
  13636. ;; make tree, check each match with the callback
  13637. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13638. ;;;; Finish up
  13639. (provide 'org)
  13640. (run-hooks 'org-load-hook)
  13641. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13642. ;;; org.el ends here