org.el 553 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913
  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 footnote)
  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. footnote Footnote labels.
  791. Changing this variable requires a restart of Emacs to become effective."
  792. :group 'org-link
  793. :type '(set :greedy t
  794. (const :tag "Double bracket links (new style)" bracket)
  795. (const :tag "Angular bracket links (old style)" angular)
  796. (const :tag "Plain text links" plain)
  797. (const :tag "Radio target matches" radio)
  798. (const :tag "Tags" tag)
  799. (const :tag "Timestamps" date)
  800. (const :tag "Footnotes" footnote)))
  801. (defcustom org-make-link-description-function nil
  802. "Function to use to generate link descriptions from links. If
  803. nil the link location will be used. This function must take two
  804. parameters; the first is the link and the second the description
  805. org-insert-link has generated, and should return the description
  806. to use."
  807. :group 'org-link
  808. :type 'function)
  809. (defgroup org-link-store nil
  810. "Options concerning storing links in Org-mode."
  811. :tag "Org Store Link"
  812. :group 'org-link)
  813. (defcustom org-email-link-description-format "Email %c: %.30s"
  814. "Format of the description part of a link to an email or usenet message.
  815. The following %-escapes will be replaced by corresponding information:
  816. %F full \"From\" field
  817. %f name, taken from \"From\" field, address if no name
  818. %T full \"To\" field
  819. %t first name in \"To\" field, address if no name
  820. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  821. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  822. %s subject
  823. %m message-id.
  824. You may use normal field width specification between the % and the letter.
  825. This is for example useful to limit the length of the subject.
  826. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  827. :group 'org-link-store
  828. :type 'string)
  829. (defcustom org-from-is-user-regexp
  830. (let (r1 r2)
  831. (when (and user-mail-address (not (string= user-mail-address "")))
  832. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  833. (when (and user-full-name (not (string= user-full-name "")))
  834. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  835. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  836. "Regexp matched against the \"From:\" header of an email or usenet message.
  837. It should match if the message is from the user him/herself."
  838. :group 'org-link-store
  839. :type 'regexp)
  840. (defcustom org-link-to-org-use-id 'create-if-interactive
  841. "Non-nil means, storing a link to an Org file will use entry IDs.
  842. Note that before this variable is even considered, org-id must be loaded,
  843. to please customize `org-modules' and turn it on.
  844. The variable can have the following values:
  845. t Create an ID if needed to make a link to the current entry.
  846. create-if-interactive
  847. If `org-store-link' is called directly (interactively, as a user
  848. command), do create an ID to support the link. But when doing the
  849. job for remember, only use the ID if it already exists. The
  850. purpose of this setting is to avoid proliferation of unwanted
  851. IDs, just because you happen to be in an Org file when you
  852. call `org-remember' that automatically and preemptively
  853. creates a link. If you do want to get an ID link in a remember
  854. template to an entry not having an ID, create it first by
  855. explicitly creating a link to it, using `C-c C-l' first.
  856. use-existing
  857. Use existing ID, do not create one.
  858. nil Never use an ID to make a link, instead link using a text search for
  859. the headline text."
  860. :group 'org-link-store
  861. :type '(choice
  862. (const :tag "Create ID to make link" t)
  863. (const :tag "Create if string link interactively"
  864. 'create-if-interactive)
  865. (const :tag "Only use existing" 'use-existing)
  866. (const :tag "Do not use ID to create link" nil)))
  867. (defcustom org-context-in-file-links t
  868. "Non-nil means, file links from `org-store-link' contain context.
  869. A search string will be added to the file name with :: as separator and
  870. used to find the context when the link is activated by the command
  871. `org-open-at-point'.
  872. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  873. negates this setting for the duration of the command."
  874. :group 'org-link-store
  875. :type 'boolean)
  876. (defcustom org-keep-stored-link-after-insertion nil
  877. "Non-nil means, keep link in list for entire session.
  878. The command `org-store-link' adds a link pointing to the current
  879. location to an internal list. These links accumulate during a session.
  880. The command `org-insert-link' can be used to insert links into any
  881. Org-mode file (offering completion for all stored links). When this
  882. option is nil, every link which has been inserted once using \\[org-insert-link]
  883. will be removed from the list, to make completing the unused links
  884. more efficient."
  885. :group 'org-link-store
  886. :type 'boolean)
  887. (defgroup org-link-follow nil
  888. "Options concerning following links in Org-mode."
  889. :tag "Org Follow Link"
  890. :group 'org-link)
  891. (defcustom org-link-translation-function nil
  892. "Function to translate links with different syntax to Org syntax.
  893. This can be used to translate links created for example by the Planner
  894. or emacs-wiki packages to Org syntax.
  895. The function must accept two parameters, a TYPE containing the link
  896. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  897. which is everything after the link protocol. It should return a cons
  898. with possibly modified values of type and path.
  899. Org contains a function for this, so if you set this variable to
  900. `org-translate-link-from-planner', you should be able follow many
  901. links created by planner."
  902. :group 'org-link-follow
  903. :type 'function)
  904. (defcustom org-follow-link-hook nil
  905. "Hook that is run after a link has been followed."
  906. :group 'org-link-follow
  907. :type 'hook)
  908. (defcustom org-tab-follows-link nil
  909. "Non-nil means, on links TAB will follow the link.
  910. Needs to be set before org.el is loaded."
  911. :group 'org-link-follow
  912. :type 'boolean)
  913. (defcustom org-return-follows-link nil
  914. "Non-nil means, on links RET will follow the link.
  915. Needs to be set before org.el is loaded."
  916. :group 'org-link-follow
  917. :type 'boolean)
  918. (defcustom org-mouse-1-follows-link
  919. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  920. "Non-nil means, mouse-1 on a link will follow the link.
  921. A longer mouse click will still set point. Does not work on XEmacs.
  922. Needs to be set before org.el is loaded."
  923. :group 'org-link-follow
  924. :type 'boolean)
  925. (defcustom org-mark-ring-length 4
  926. "Number of different positions to be recorded in the ring
  927. Changing this requires a restart of Emacs to work correctly."
  928. :group 'org-link-follow
  929. :type 'integer)
  930. (defcustom org-link-frame-setup
  931. '((vm . vm-visit-folder-other-frame)
  932. (gnus . gnus-other-frame)
  933. (file . find-file-other-window))
  934. "Setup the frame configuration for following links.
  935. When following a link with Emacs, it may often be useful to display
  936. this link in another window or frame. This variable can be used to
  937. set this up for the different types of links.
  938. For VM, use any of
  939. `vm-visit-folder'
  940. `vm-visit-folder-other-frame'
  941. For Gnus, use any of
  942. `gnus'
  943. `gnus-other-frame'
  944. `org-gnus-no-new-news'
  945. For FILE, use any of
  946. `find-file'
  947. `find-file-other-window'
  948. `find-file-other-frame'
  949. For the calendar, use the variable `calendar-setup'.
  950. For BBDB, it is currently only possible to display the matches in
  951. another window."
  952. :group 'org-link-follow
  953. :type '(list
  954. (cons (const vm)
  955. (choice
  956. (const vm-visit-folder)
  957. (const vm-visit-folder-other-window)
  958. (const vm-visit-folder-other-frame)))
  959. (cons (const gnus)
  960. (choice
  961. (const gnus)
  962. (const gnus-other-frame)
  963. (const org-gnus-no-new-news)))
  964. (cons (const file)
  965. (choice
  966. (const find-file)
  967. (const find-file-other-window)
  968. (const find-file-other-frame)))))
  969. (defcustom org-display-internal-link-with-indirect-buffer nil
  970. "Non-nil means, use indirect buffer to display infile links.
  971. Activating internal links (from one location in a file to another location
  972. in the same file) normally just jumps to the location. When the link is
  973. activated with a C-u prefix (or with mouse-3), the link is displayed in
  974. another window. When this option is set, the other window actually displays
  975. an indirect buffer clone of the current buffer, to avoid any visibility
  976. changes to the current buffer."
  977. :group 'org-link-follow
  978. :type 'boolean)
  979. (defcustom org-open-non-existing-files nil
  980. "Non-nil means, `org-open-file' will open non-existing files.
  981. When nil, an error will be generated."
  982. :group 'org-link-follow
  983. :type 'boolean)
  984. (defcustom org-open-directory-means-index-dot-org nil
  985. "Non-nil means, a link to a directory really means to index.org.
  986. When nil, following a directory link will run dired or open a finder/explorer
  987. window on that directory."
  988. :group 'org-link-follow
  989. :type 'boolean)
  990. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  991. "Function and arguments to call for following mailto links.
  992. This is a list with the first element being a lisp function, and the
  993. remaining elements being arguments to the function. In string arguments,
  994. %a will be replaced by the address, and %s will be replaced by the subject
  995. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  996. :group 'org-link-follow
  997. :type '(choice
  998. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  999. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1000. (const :tag "message-mail" (message-mail "%a" "%s"))
  1001. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1002. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1003. "Non-nil means, ask for confirmation before executing shell links.
  1004. Shell links can be dangerous: just think about a link
  1005. [[shell:rm -rf ~/*][Google Search]]
  1006. This link would show up in your Org-mode document as \"Google Search\",
  1007. but really it would remove your entire home directory.
  1008. Therefore we advise against setting this variable to nil.
  1009. Just change it to `y-or-n-p' of you want to confirm with a
  1010. single keystroke rather than having to type \"yes\"."
  1011. :group 'org-link-follow
  1012. :type '(choice
  1013. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1014. (const :tag "with y-or-n (faster)" y-or-n-p)
  1015. (const :tag "no confirmation (dangerous)" nil)))
  1016. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1017. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1018. Elisp links can be dangerous: just think about a link
  1019. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1020. This link would show up in your Org-mode document as \"Google Search\",
  1021. but really it would remove your entire home directory.
  1022. Therefore we advise against setting this variable to nil.
  1023. Just change it to `y-or-n-p' of you want to confirm with a
  1024. single keystroke rather than having to type \"yes\"."
  1025. :group 'org-link-follow
  1026. :type '(choice
  1027. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1028. (const :tag "with y-or-n (faster)" y-or-n-p)
  1029. (const :tag "no confirmation (dangerous)" nil)))
  1030. (defconst org-file-apps-defaults-gnu
  1031. '((remote . emacs)
  1032. (system . mailcap)
  1033. (t . mailcap))
  1034. "Default file applications on a UNIX or GNU/Linux system.
  1035. See `org-file-apps'.")
  1036. (defconst org-file-apps-defaults-macosx
  1037. '((remote . emacs)
  1038. (t . "open %s")
  1039. (system . "open %s")
  1040. ("ps.gz" . "gv %s")
  1041. ("eps.gz" . "gv %s")
  1042. ("dvi" . "xdvi %s")
  1043. ("fig" . "xfig %s"))
  1044. "Default file applications on a MacOS X system.
  1045. The system \"open\" is known as a default, but we use X11 applications
  1046. for some files for which the OS does not have a good default.
  1047. See `org-file-apps'.")
  1048. (defconst org-file-apps-defaults-windowsnt
  1049. (list
  1050. '(remote . emacs)
  1051. (cons t
  1052. (list (if (featurep 'xemacs)
  1053. 'mswindows-shell-execute
  1054. 'w32-shell-execute)
  1055. "open" 'file))
  1056. (cons 'system
  1057. (list (if (featurep 'xemacs)
  1058. 'mswindows-shell-execute
  1059. 'w32-shell-execute)
  1060. "open" 'file)))
  1061. "Default file applications on a Windows NT system.
  1062. The system \"open\" is used for most files.
  1063. See `org-file-apps'.")
  1064. (defcustom org-file-apps
  1065. '(
  1066. (auto-mode . emacs)
  1067. ("\\.x?html?\\'" . default)
  1068. ("\\.pdf\\'" . default)
  1069. )
  1070. "External applications for opening `file:path' items in a document.
  1071. Org-mode uses system defaults for different file types, but
  1072. you can use this variable to set the application for a given file
  1073. extension. The entries in this list are cons cells where the car identifies
  1074. files and the cdr the corresponding command. Possible values for the
  1075. file identifier are
  1076. \"regex\" Regular expression matched against the file name. For backward
  1077. compatibility, this can also be a string with only alphanumeric
  1078. characters, which is then interpreted as an extension.
  1079. `directory' Matches a directory
  1080. `remote' Matches a remote file, accessible through tramp or efs.
  1081. Remote files most likely should be visited through Emacs
  1082. because external applications cannot handle such paths.
  1083. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1084. so all files Emacs knows how to handle. Using this with
  1085. command `emacs' will open most files in Emacs. Beware that this
  1086. will also open html files inside Emacs, unless you add
  1087. (\"html\" . default) to the list as well.
  1088. t Default for files not matched by any of the other options.
  1089. `system' The system command to open files, like `open' on Windows
  1090. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1091. that will be selected if you call `C-c C-o' with a double
  1092. `C-u C-u' prefix.
  1093. Possible values for the command are:
  1094. `emacs' The file will be visited by the current Emacs process.
  1095. `default' Use the default application for this file type, which is the
  1096. association for t in the list, most likely in the system-specific
  1097. part.
  1098. This can be used to overrule an unwanted setting in the
  1099. system-specific variable.
  1100. `system' Use the system command for opening files, like \"open\".
  1101. This command is specified by the entry whose car is `system'.
  1102. Most likely, the system-specific version of this variable
  1103. does define this command, but you can overrule/replace it
  1104. here.
  1105. string A command to be executed by a shell; %s will be replaced
  1106. by the path to the file.
  1107. sexp A Lisp form which will be evaluated. The file path will
  1108. be available in the Lisp variable `file'.
  1109. For more examples, see the system specific constants
  1110. `org-file-apps-defaults-macosx'
  1111. `org-file-apps-defaults-windowsnt'
  1112. `org-file-apps-defaults-gnu'."
  1113. :group 'org-link-follow
  1114. :type '(repeat
  1115. (cons (choice :value ""
  1116. (string :tag "Extension")
  1117. (const :tag "System command to open files" system)
  1118. (const :tag "Default for unrecognized files" t)
  1119. (const :tag "Remote file" remote)
  1120. (const :tag "Links to a directory" directory)
  1121. (const :tag "Any files that have Emacs modes"
  1122. auto-mode))
  1123. (choice :value ""
  1124. (const :tag "Visit with Emacs" emacs)
  1125. (const :tag "Use default" default)
  1126. (const :tag "Use the system command" system)
  1127. (string :tag "Command")
  1128. (sexp :tag "Lisp form")))))
  1129. (defgroup org-refile nil
  1130. "Options concerning refiling entries in Org-mode."
  1131. :tag "Org Refile"
  1132. :group 'org)
  1133. (defcustom org-directory "~/org"
  1134. "Directory with org files.
  1135. This directory will be used as default to prompt for org files.
  1136. Used by the hooks for remember.el."
  1137. :group 'org-refile
  1138. :group 'org-remember
  1139. :type 'directory)
  1140. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1141. "Default target for storing notes.
  1142. Used by the hooks for remember.el. This can be a string, or nil to mean
  1143. the value of `remember-data-file'.
  1144. You can set this on a per-template basis with the variable
  1145. `org-remember-templates'."
  1146. :group 'org-refile
  1147. :group 'org-remember
  1148. :type '(choice
  1149. (const :tag "Default from remember-data-file" nil)
  1150. file))
  1151. (defcustom org-goto-interface 'outline
  1152. "The default interface to be used for `org-goto'.
  1153. Allowed values are:
  1154. outline The interface shows an outline of the relevant file
  1155. and the correct heading is found by moving through
  1156. the outline or by searching with incremental search.
  1157. outline-path-completion Headlines in the current buffer are offered via
  1158. completion. This is the interface also used by
  1159. the refile command."
  1160. :group 'org-refile
  1161. :type '(choice
  1162. (const :tag "Outline" outline)
  1163. (const :tag "Outline-path-completion" outline-path-completion)))
  1164. (defcustom org-goto-max-level 5
  1165. "Maximum level to be considered when running org-goto with refile interface."
  1166. :group 'org-refile
  1167. :type 'number)
  1168. (defcustom org-reverse-note-order nil
  1169. "Non-nil means, store new notes at the beginning of a file or entry.
  1170. When nil, new notes will be filed to the end of a file or entry.
  1171. This can also be a list with cons cells of regular expressions that
  1172. are matched against file names, and values."
  1173. :group 'org-remember
  1174. :group 'org-refile
  1175. :type '(choice
  1176. (const :tag "Reverse always" t)
  1177. (const :tag "Reverse never" nil)
  1178. (repeat :tag "By file name regexp"
  1179. (cons regexp boolean))))
  1180. (defcustom org-refile-targets nil
  1181. "Targets for refiling entries with \\[org-refile].
  1182. This is list of cons cells. Each cell contains:
  1183. - a specification of the files to be considered, either a list of files,
  1184. or a symbol whose function or variable value will be used to retrieve
  1185. a file name or a list of file names. If you use `org-agenda-files' for
  1186. that, all agenda files will be scanned for targets. Nil means, consider
  1187. headings in the current buffer.
  1188. - A specification of how to select find candidate refile targets. This
  1189. may be any of
  1190. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1191. This tag has to be present in all target headlines, inheritance will
  1192. not be considered.
  1193. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1194. todo keyword.
  1195. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1196. headlines that are refiling targets.
  1197. - a cons cell (:level . N). Any headline of level N is considered a target.
  1198. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1199. When this variable is nil, all top-level headlines in the current buffer
  1200. are used, equivalent to the value `((nil . (:level . 1))'."
  1201. :group 'org-refile
  1202. :type '(repeat
  1203. (cons
  1204. (choice :value org-agenda-files
  1205. (const :tag "All agenda files" org-agenda-files)
  1206. (const :tag "Current buffer" nil)
  1207. (function) (variable) (file))
  1208. (choice :tag "Identify target headline by"
  1209. (cons :tag "Specific tag" (const :value :tag) (string))
  1210. (cons :tag "TODO keyword" (const :value :todo) (string))
  1211. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1212. (cons :tag "Level number" (const :value :level) (integer))
  1213. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1214. (defcustom org-refile-use-outline-path nil
  1215. "Non-nil means, provide refile targets as paths.
  1216. So a level 3 headline will be available as level1/level2/level3.
  1217. When the value is `file', also include the file name (without directory)
  1218. into the path. When `full-file-path', include the full file path."
  1219. :group 'org-refile
  1220. :type '(choice
  1221. (const :tag "Not" nil)
  1222. (const :tag "Yes" t)
  1223. (const :tag "Start with file name" file)
  1224. (const :tag "Start with full file path" full-file-path)))
  1225. (defcustom org-outline-path-complete-in-steps t
  1226. "Non-nil means, complete the outline path in hierarchical steps.
  1227. When Org-mode uses the refile interface to select an outline path
  1228. \(see variable `org-refile-use-outline-path'), the completion of
  1229. the path can be done is a single go, or if can be done in steps down
  1230. the headline hierarchy. Going in steps is probably the best if you
  1231. do not use a special completion package like `ido' or `icicles'.
  1232. However, when using these packages, going in one step can be very
  1233. fast, while still showing the whole path to the entry."
  1234. :group 'org-refile
  1235. :type 'boolean)
  1236. (defgroup org-todo nil
  1237. "Options concerning TODO items in Org-mode."
  1238. :tag "Org TODO"
  1239. :group 'org)
  1240. (defgroup org-progress nil
  1241. "Options concerning Progress logging in Org-mode."
  1242. :tag "Org Progress"
  1243. :group 'org-time)
  1244. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1245. "List of TODO entry keyword sequences and their interpretation.
  1246. \\<org-mode-map>This is a list of sequences.
  1247. Each sequence starts with a symbol, either `sequence' or `type',
  1248. indicating if the keywords should be interpreted as a sequence of
  1249. action steps, or as different types of TODO items. The first
  1250. keywords are states requiring action - these states will select a headline
  1251. for inclusion into the global TODO list Org-mode produces. If one of
  1252. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1253. signify that no further action is necessary. If \"|\" is not found,
  1254. the last keyword is treated as the only DONE state of the sequence.
  1255. The command \\[org-todo] cycles an entry through these states, and one
  1256. additional state where no keyword is present. For details about this
  1257. cycling, see the manual.
  1258. TODO keywords and interpretation can also be set on a per-file basis with
  1259. the special #+SEQ_TODO and #+TYP_TODO lines.
  1260. Each keyword can optionally specify a character for fast state selection
  1261. \(in combination with the variable `org-use-fast-todo-selection')
  1262. and specifiers for state change logging, using the same syntax
  1263. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1264. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1265. indicates to record a time stamp each time this state is selected.
  1266. Each keyword may also specify if a timestamp or a note should be
  1267. recorded when entering or leaving the state, by adding additional
  1268. characters in the parenthesis after the keyword. This looks like this:
  1269. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1270. record only the time of the state change. With X and Y being either
  1271. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1272. Y when leaving the state if and only if the *target* state does not
  1273. define X. You may omit any of the fast-selection key or X or /Y,
  1274. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1275. For backward compatibility, this variable may also be just a list
  1276. of keywords - in this case the interpretation (sequence or type) will be
  1277. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1278. :group 'org-todo
  1279. :group 'org-keywords
  1280. :type '(choice
  1281. (repeat :tag "Old syntax, just keywords"
  1282. (string :tag "Keyword"))
  1283. (repeat :tag "New syntax"
  1284. (cons
  1285. (choice
  1286. :tag "Interpretation"
  1287. (const :tag "Sequence (cycling hits every state)" sequence)
  1288. (const :tag "Type (cycling directly to DONE)" type))
  1289. (repeat
  1290. (string :tag "Keyword"))))))
  1291. (defvar org-todo-keywords-1 nil
  1292. "All TODO and DONE keywords active in a buffer.")
  1293. (make-variable-buffer-local 'org-todo-keywords-1)
  1294. (defvar org-todo-keywords-for-agenda nil)
  1295. (defvar org-done-keywords-for-agenda nil)
  1296. (defvar org-todo-keyword-alist-for-agenda nil)
  1297. (defvar org-tag-alist-for-agenda nil)
  1298. (defvar org-agenda-contributing-files nil)
  1299. (defvar org-not-done-keywords nil)
  1300. (make-variable-buffer-local 'org-not-done-keywords)
  1301. (defvar org-done-keywords nil)
  1302. (make-variable-buffer-local 'org-done-keywords)
  1303. (defvar org-todo-heads nil)
  1304. (make-variable-buffer-local 'org-todo-heads)
  1305. (defvar org-todo-sets nil)
  1306. (make-variable-buffer-local 'org-todo-sets)
  1307. (defvar org-todo-log-states nil)
  1308. (make-variable-buffer-local 'org-todo-log-states)
  1309. (defvar org-todo-kwd-alist nil)
  1310. (make-variable-buffer-local 'org-todo-kwd-alist)
  1311. (defvar org-todo-key-alist nil)
  1312. (make-variable-buffer-local 'org-todo-key-alist)
  1313. (defvar org-todo-key-trigger nil)
  1314. (make-variable-buffer-local 'org-todo-key-trigger)
  1315. (defcustom org-todo-interpretation 'sequence
  1316. "Controls how TODO keywords are interpreted.
  1317. This variable is in principle obsolete and is only used for
  1318. backward compatibility, if the interpretation of todo keywords is
  1319. not given already in `org-todo-keywords'. See that variable for
  1320. more information."
  1321. :group 'org-todo
  1322. :group 'org-keywords
  1323. :type '(choice (const sequence)
  1324. (const type)))
  1325. (defcustom org-use-fast-todo-selection 'prefix
  1326. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1327. This variable describes if and under what circumstances the cycling
  1328. mechanism for TODO keywords will be replaced by a single-key, direct
  1329. selection scheme.
  1330. When nil, fast selection is never used.
  1331. When the symbol `prefix', it will be used when `org-todo' is called with
  1332. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1333. in an agenda buffer.
  1334. When t, fast selection is used by default. In this case, the prefix
  1335. argument forces cycling instead.
  1336. In all cases, the special interface is only used if access keys have actually
  1337. been assigned by the user, i.e. if keywords in the configuration are followed
  1338. by a letter in parenthesis, like TODO(t)."
  1339. :group 'org-todo
  1340. :type '(choice
  1341. (const :tag "Never" nil)
  1342. (const :tag "By default" t)
  1343. (const :tag "Only with C-u C-c C-t" prefix)))
  1344. (defcustom org-provide-todo-statistics t
  1345. "Non-nil means, update todo statistics after insert and toggle.
  1346. When this is set, todo statistics is updated in the parent of the current
  1347. entry each time a todo state is changed."
  1348. :group 'org-todo
  1349. :type 'boolean)
  1350. (defcustom org-after-todo-state-change-hook nil
  1351. "Hook which is run after the state of a TODO item was changed.
  1352. The new state (a string with a TODO keyword, or nil) is available in the
  1353. Lisp variable `state'."
  1354. :group 'org-todo
  1355. :type 'hook)
  1356. (defcustom org-todo-state-tags-triggers nil
  1357. "Tag changes that should be triggered by TODO state changes.
  1358. This is a list. Each entry is
  1359. (state-change (tag . flag) .......)
  1360. State-change can be a string with a state, and empty string to indicate the
  1361. state that has no TODO keyword, or it can be one of the symbols `todo'
  1362. or `done', meaning any not-done or done state, respectively."
  1363. :group 'org-todo
  1364. :group 'org-tags
  1365. :type '(repeat
  1366. (cons (choice :tag "When changing to"
  1367. (const :tag "Not-done state" todo)
  1368. (const :tag "Done state" done)
  1369. (string :tag "State"))
  1370. (repeat
  1371. (cons :tag "Tag action"
  1372. (string :tag "Tag")
  1373. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1374. (defcustom org-log-done nil
  1375. "Information to record when a task moves to the DONE state.
  1376. Possible values are:
  1377. nil Don't add anything, just change the keyword
  1378. time Add a time stamp to the task
  1379. note Prompt a closing note and add it with template `org-log-note-headings'
  1380. This option can also be set with on a per-file-basis with
  1381. #+STARTUP: nologdone
  1382. #+STARTUP: logdone
  1383. #+STARTUP: lognotedone
  1384. You can have local logging settings for a subtree by setting the LOGGING
  1385. property to one or more of these keywords."
  1386. :group 'org-todo
  1387. :group 'org-progress
  1388. :type '(choice
  1389. (const :tag "No logging" nil)
  1390. (const :tag "Record CLOSED timestamp" time)
  1391. (const :tag "Record CLOSED timestamp with closing note." note)))
  1392. ;; Normalize old uses of org-log-done.
  1393. (cond
  1394. ((eq org-log-done t) (setq org-log-done 'time))
  1395. ((and (listp org-log-done) (memq 'done org-log-done))
  1396. (setq org-log-done 'note)))
  1397. (defcustom org-log-note-clock-out nil
  1398. "Non-nil means, record a note when clocking out of an item.
  1399. This can also be configured on a per-file basis by adding one of
  1400. the following lines anywhere in the buffer:
  1401. #+STARTUP: lognoteclock-out
  1402. #+STARTUP: nolognoteclock-out"
  1403. :group 'org-todo
  1404. :group 'org-progress
  1405. :type 'boolean)
  1406. (defcustom org-log-done-with-time t
  1407. "Non-nil means, the CLOSED time stamp will contain date and time.
  1408. When nil, only the date will be recorded."
  1409. :group 'org-progress
  1410. :type 'boolean)
  1411. (defcustom org-log-note-headings
  1412. '((done . "CLOSING NOTE %t")
  1413. (state . "State %-12s %t")
  1414. (note . "Note taken on %t")
  1415. (clock-out . ""))
  1416. "Headings for notes added to entries.
  1417. The value is an alist, with the car being a symbol indicating the note
  1418. context, and the cdr is the heading to be used. The heading may also be the
  1419. empty string.
  1420. %t in the heading will be replaced by a time stamp.
  1421. %s will be replaced by the new TODO state, in double quotes.
  1422. %u will be replaced by the user name.
  1423. %U will be replaced by the full user name."
  1424. :group 'org-todo
  1425. :group 'org-progress
  1426. :type '(list :greedy t
  1427. (cons (const :tag "Heading when closing an item" done) string)
  1428. (cons (const :tag
  1429. "Heading when changing todo state (todo sequence only)"
  1430. state) string)
  1431. (cons (const :tag "Heading when just taking a note" note) string)
  1432. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1433. (unless (assq 'note org-log-note-headings)
  1434. (push '(note . "%t") org-log-note-headings))
  1435. (defcustom org-log-state-notes-insert-after-drawers nil
  1436. "Non-nil means, insert state change notes after any drawers in entry.
  1437. Only the drawers that *immediately* follow the headline and the
  1438. deadline/scheduled line are skipped.
  1439. When nil, insert notes right after the heading and perhaps the line
  1440. with deadline/scheduling if present."
  1441. :group 'org-todo
  1442. :group 'org-progress
  1443. :type 'boolean)
  1444. (defcustom org-log-states-order-reversed t
  1445. "Non-nil means, the latest state change note will be directly after heading.
  1446. When nil, the notes will be orderer according to time."
  1447. :group 'org-todo
  1448. :group 'org-progress
  1449. :type 'boolean)
  1450. (defcustom org-log-repeat 'time
  1451. "Non-nil means, record moving through the DONE state when triggering repeat.
  1452. An auto-repeating tasks is immediately switched back to TODO when marked
  1453. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1454. the TODO keyword definition, or recording a closing note by setting
  1455. `org-log-done', there will be no record of the task moving through DONE.
  1456. This variable forces taking a note anyway. Possible values are:
  1457. nil Don't force a record
  1458. time Record a time stamp
  1459. note Record a note
  1460. This option can also be set with on a per-file-basis with
  1461. #+STARTUP: logrepeat
  1462. #+STARTUP: lognoterepeat
  1463. #+STARTUP: nologrepeat
  1464. You can have local logging settings for a subtree by setting the LOGGING
  1465. property to one or more of these keywords."
  1466. :group 'org-todo
  1467. :group 'org-progress
  1468. :type '(choice
  1469. (const :tag "Don't force a record" nil)
  1470. (const :tag "Force recording the DONE state" time)
  1471. (const :tag "Force recording a note with the DONE state" note)))
  1472. (defgroup org-priorities nil
  1473. "Priorities in Org-mode."
  1474. :tag "Org Priorities"
  1475. :group 'org-todo)
  1476. (defcustom org-highest-priority ?A
  1477. "The highest priority of TODO items. A character like ?A, ?B etc.
  1478. Must have a smaller ASCII number than `org-lowest-priority'."
  1479. :group 'org-priorities
  1480. :type 'character)
  1481. (defcustom org-lowest-priority ?C
  1482. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1483. Must have a larger ASCII number than `org-highest-priority'."
  1484. :group 'org-priorities
  1485. :type 'character)
  1486. (defcustom org-default-priority ?B
  1487. "The default priority of TODO items.
  1488. This is the priority an item get if no explicit priority is given."
  1489. :group 'org-priorities
  1490. :type 'character)
  1491. (defcustom org-priority-start-cycle-with-default t
  1492. "Non-nil means, start with default priority when starting to cycle.
  1493. When this is nil, the first step in the cycle will be (depending on the
  1494. command used) one higher or lower that the default priority."
  1495. :group 'org-priorities
  1496. :type 'boolean)
  1497. (defgroup org-time nil
  1498. "Options concerning time stamps and deadlines in Org-mode."
  1499. :tag "Org Time"
  1500. :group 'org)
  1501. (defcustom org-insert-labeled-timestamps-at-point nil
  1502. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1503. When nil, these labeled time stamps are forces into the second line of an
  1504. entry, just after the headline. When scheduling from the global TODO list,
  1505. the time stamp will always be forced into the second line."
  1506. :group 'org-time
  1507. :type 'boolean)
  1508. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1509. "Formats for `format-time-string' which are used for time stamps.
  1510. It is not recommended to change this constant.")
  1511. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1512. "Number of minutes to round time stamps to.
  1513. These are two values, the first applies when first creating a time stamp.
  1514. The second applies when changing it with the commands `S-up' and `S-down'.
  1515. When changing the time stamp, this means that it will change in steps
  1516. of N minutes, as given by the second value.
  1517. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1518. numbers should be factors of 60, so for example 5, 10, 15.
  1519. When this is larger than 1, you can still force an exact time-stamp by using
  1520. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1521. and by using a prefix arg to `S-up/down' to specify the exact number
  1522. of minutes to shift."
  1523. :group 'org-time
  1524. :get '(lambda (var) ; Make sure all entries have 5 elements
  1525. (if (integerp (default-value var))
  1526. (list (default-value var) 5)
  1527. (default-value var)))
  1528. :type '(list
  1529. (integer :tag "when inserting times")
  1530. (integer :tag "when modifying times")))
  1531. ;; Normalize old customizations of this variable.
  1532. (when (integerp org-time-stamp-rounding-minutes)
  1533. (setq org-time-stamp-rounding-minutes
  1534. (list org-time-stamp-rounding-minutes
  1535. org-time-stamp-rounding-minutes)))
  1536. (defcustom org-display-custom-times nil
  1537. "Non-nil means, overlay custom formats over all time stamps.
  1538. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1539. To turn this on on a per-file basis, insert anywhere in the file:
  1540. #+STARTUP: customtime"
  1541. :group 'org-time
  1542. :set 'set-default
  1543. :type 'sexp)
  1544. (make-variable-buffer-local 'org-display-custom-times)
  1545. (defcustom org-time-stamp-custom-formats
  1546. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1547. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1548. These are overlayed over the default ISO format if the variable
  1549. `org-display-custom-times' is set. Time like %H:%M should be at the
  1550. end of the second format."
  1551. :group 'org-time
  1552. :type 'sexp)
  1553. (defun org-time-stamp-format (&optional long inactive)
  1554. "Get the right format for a time string."
  1555. (let ((f (if long (cdr org-time-stamp-formats)
  1556. (car org-time-stamp-formats))))
  1557. (if inactive
  1558. (concat "[" (substring f 1 -1) "]")
  1559. f)))
  1560. (defcustom org-time-clocksum-format "%d:%02d"
  1561. "The format string used when creating CLOCKSUM lines, or when
  1562. org-mode generates a time duration."
  1563. :group 'org-time
  1564. :type 'string)
  1565. (defcustom org-deadline-warning-days 14
  1566. "No. of days before expiration during which a deadline becomes active.
  1567. This variable governs the display in sparse trees and in the agenda.
  1568. When 0 or negative, it means use this number (the absolute value of it)
  1569. even if a deadline has a different individual lead time specified."
  1570. :group 'org-time
  1571. :group 'org-agenda-daily/weekly
  1572. :type 'number)
  1573. (defcustom org-read-date-prefer-future t
  1574. "Non-nil means, assume future for incomplete date input from user.
  1575. This affects the following situations:
  1576. 1. The user gives a day, but no month.
  1577. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1578. read this as the third of *next* month. However, if you enter \"17\",
  1579. it will be considered as *this* month.
  1580. 2. The user gives a month but not a year.
  1581. For example, if it is april and you enter \"feb 2\", this will be read
  1582. as feb 2, *next* year. \"May 5\", however, will be this year.
  1583. Currently this does not work for ISO week specifications.
  1584. When this option is nil, the current month and year will always be used
  1585. as defaults."
  1586. :group 'org-time
  1587. :type 'boolean)
  1588. (defcustom org-read-date-display-live t
  1589. "Non-nil means, display current interpretation of date prompt live.
  1590. This display will be in an overlay, in the minibuffer."
  1591. :group 'org-time
  1592. :type 'boolean)
  1593. (defcustom org-read-date-popup-calendar t
  1594. "Non-nil means, pop up a calendar when prompting for a date.
  1595. In the calendar, the date can be selected with mouse-1. However, the
  1596. minibuffer will also be active, and you can simply enter the date as well.
  1597. When nil, only the minibuffer will be available."
  1598. :group 'org-time
  1599. :type 'boolean)
  1600. (if (fboundp 'defvaralias)
  1601. (defvaralias 'org-popup-calendar-for-date-prompt
  1602. 'org-read-date-popup-calendar))
  1603. (defcustom org-extend-today-until 0
  1604. "The hour when your day really ends. Must be an integer.
  1605. This has influence for the following applications:
  1606. - When switching the agenda to \"today\". It it is still earlier than
  1607. the time given here, the day recognized as TODAY is actually yesterday.
  1608. - When a date is read from the user and it is still before the time given
  1609. here, the current date and time will be assumed to be yesterday, 23:59.
  1610. Also, timestamps inserted in remember templates follow this rule.
  1611. IMPORTANT: This is a feature whose implementation is and likely will
  1612. remain incomplete. Really, it is only here because past midnight seems to
  1613. be the favorite working time of John Wiegley :-)"
  1614. :group 'org-time
  1615. :type 'number)
  1616. (defcustom org-edit-timestamp-down-means-later nil
  1617. "Non-nil means, S-down will increase the time in a time stamp.
  1618. When nil, S-up will increase."
  1619. :group 'org-time
  1620. :type 'boolean)
  1621. (defcustom org-calendar-follow-timestamp-change t
  1622. "Non-nil means, make the calendar window follow timestamp changes.
  1623. When a timestamp is modified and the calendar window is visible, it will be
  1624. moved to the new date."
  1625. :group 'org-time
  1626. :type 'boolean)
  1627. (defgroup org-tags nil
  1628. "Options concerning tags in Org-mode."
  1629. :tag "Org Tags"
  1630. :group 'org)
  1631. (defcustom org-tag-alist nil
  1632. "List of tags allowed in Org-mode files.
  1633. When this list is nil, Org-mode will base TAG input on what is already in the
  1634. buffer.
  1635. The value of this variable is an alist, the car of each entry must be a
  1636. keyword as a string, the cdr may be a character that is used to select
  1637. that tag through the fast-tag-selection interface.
  1638. See the manual for details."
  1639. :group 'org-tags
  1640. :type '(repeat
  1641. (choice
  1642. (cons (string :tag "Tag name")
  1643. (character :tag "Access char"))
  1644. (const :tag "Start radio group" (:startgroup))
  1645. (const :tag "End radio group" (:endgroup)))))
  1646. (defvar org-file-tags nil
  1647. "List of tags that can be inherited by all entries in the file.
  1648. The tags will be inherited if the variable `org-use-tag-inheritance'
  1649. says they should be.
  1650. This variable is populated from #+TAG lines.")
  1651. (defcustom org-use-fast-tag-selection 'auto
  1652. "Non-nil means, use fast tag selection scheme.
  1653. This is a special interface to select and deselect tags with single keys.
  1654. When nil, fast selection is never used.
  1655. When the symbol `auto', fast selection is used if and only if selection
  1656. characters for tags have been configured, either through the variable
  1657. `org-tag-alist' or through a #+TAGS line in the buffer.
  1658. When t, fast selection is always used and selection keys are assigned
  1659. automatically if necessary."
  1660. :group 'org-tags
  1661. :type '(choice
  1662. (const :tag "Always" t)
  1663. (const :tag "Never" nil)
  1664. (const :tag "When selection characters are configured" 'auto)))
  1665. (defcustom org-fast-tag-selection-single-key nil
  1666. "Non-nil means, fast tag selection exits after first change.
  1667. When nil, you have to press RET to exit it.
  1668. During fast tag selection, you can toggle this flag with `C-c'.
  1669. This variable can also have the value `expert'. In this case, the window
  1670. displaying the tags menu is not even shown, until you press C-c again."
  1671. :group 'org-tags
  1672. :type '(choice
  1673. (const :tag "No" nil)
  1674. (const :tag "Yes" t)
  1675. (const :tag "Expert" expert)))
  1676. (defvar org-fast-tag-selection-include-todo nil
  1677. "Non-nil means, fast tags selection interface will also offer TODO states.
  1678. This is an undocumented feature, you should not rely on it.")
  1679. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1680. "The column to which tags should be indented in a headline.
  1681. If this number is positive, it specifies the column. If it is negative,
  1682. it means that the tags should be flushright to that column. For example,
  1683. -80 works well for a normal 80 character screen."
  1684. :group 'org-tags
  1685. :type 'integer)
  1686. (defcustom org-auto-align-tags t
  1687. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1688. These operations change the length of a headline and therefore shift
  1689. the tags around. With this options turned on, after each such operation
  1690. the tags are again aligned to `org-tags-column'."
  1691. :group 'org-tags
  1692. :type 'boolean)
  1693. (defcustom org-use-tag-inheritance t
  1694. "Non-nil means, tags in levels apply also for sublevels.
  1695. When nil, only the tags directly given in a specific line apply there.
  1696. This may also be a list of tags that should be inherited, or a regexp that
  1697. matches tags that should be inherited. Additional control is possible
  1698. with the variable `org-tags-exclude-from-inheritance' which gives an
  1699. explicit list of tags to be excluded from inheritance., even if the value of
  1700. `org-use-tag-inheritance' would select it for inheritance.
  1701. If this option is t, a match early-on in a tree can lead to a large
  1702. number of matches in the subtree when constructing the agenda or creating
  1703. a sparse tree. If you only want to see the first match in a tree during
  1704. a search, check out the variable `org-tags-match-list-sublevels'."
  1705. :group 'org-tags
  1706. :type '(choice
  1707. (const :tag "Not" nil)
  1708. (const :tag "Always" t)
  1709. (repeat :tag "Specific tags" (string :tag "Tag"))
  1710. (regexp :tag "Tags matched by regexp")))
  1711. (defcustom org-tags-exclude-from-inheritance nil
  1712. "List of tags that should never be inherited.
  1713. This is a way to exclude a few tags from inheritance. For way to do
  1714. the opposite, to actively allow inheritance for selected tags,
  1715. see the variable `org-use-tag-inheritance'."
  1716. :group 'org-tags
  1717. :type '(repeat (string :tag "Tag")))
  1718. (defun org-tag-inherit-p (tag)
  1719. "Check if TAG is one that should be inherited."
  1720. (cond
  1721. ((member tag org-tags-exclude-from-inheritance) nil)
  1722. ((eq org-use-tag-inheritance t) t)
  1723. ((not org-use-tag-inheritance) nil)
  1724. ((stringp org-use-tag-inheritance)
  1725. (string-match org-use-tag-inheritance tag))
  1726. ((listp org-use-tag-inheritance)
  1727. (member tag org-use-tag-inheritance))
  1728. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1729. (defcustom org-tags-match-list-sublevels t
  1730. "Non-nil means list also sublevels of headlines matching tag search.
  1731. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1732. the sublevels of a headline matching a tag search often also match
  1733. the same search. Listing all of them can create very long lists.
  1734. Setting this variable to nil causes subtrees of a match to be skipped.
  1735. This option is off by default, because inheritance in on. If you turn
  1736. inheritance off, you very likely want to turn this option on.
  1737. As a special case, if the tag search is restricted to TODO items, the
  1738. value of this variable is ignored and sublevels are always checked, to
  1739. make sure all corresponding TODO items find their way into the list.
  1740. This variable is semi-obsolete and probably should always be true. It
  1741. is better to limit inheritance to certain tags using the variables
  1742. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1743. :group 'org-tags
  1744. :type 'boolean)
  1745. (defvar org-tags-history nil
  1746. "History of minibuffer reads for tags.")
  1747. (defvar org-last-tags-completion-table nil
  1748. "The last used completion table for tags.")
  1749. (defvar org-after-tags-change-hook nil
  1750. "Hook that is run after the tags in a line have changed.")
  1751. (defgroup org-properties nil
  1752. "Options concerning properties in Org-mode."
  1753. :tag "Org Properties"
  1754. :group 'org)
  1755. (defcustom org-property-format "%-10s %s"
  1756. "How property key/value pairs should be formatted by `indent-line'.
  1757. When `indent-line' hits a property definition, it will format the line
  1758. according to this format, mainly to make sure that the values are
  1759. lined-up with respect to each other."
  1760. :group 'org-properties
  1761. :type 'string)
  1762. (defcustom org-use-property-inheritance nil
  1763. "Non-nil means, properties apply also for sublevels.
  1764. This setting is chiefly used during property searches. Turning it on can
  1765. cause significant overhead when doing a search, which is why it is not
  1766. on by default.
  1767. When nil, only the properties directly given in the current entry count.
  1768. When t, every property is inherited. The value may also be a list of
  1769. properties that should have inheritance, or a regular expression matching
  1770. properties that should be inherited.
  1771. However, note that some special properties use inheritance under special
  1772. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1773. and the properties ending in \"_ALL\" when they are used as descriptor
  1774. for valid values of a property.
  1775. Note for programmers:
  1776. When querying an entry with `org-entry-get', you can control if inheritance
  1777. should be used. By default, `org-entry-get' looks only at the local
  1778. properties. You can request inheritance by setting the inherit argument
  1779. to t (to force inheritance) or to `selective' (to respect the setting
  1780. in this variable)."
  1781. :group 'org-properties
  1782. :type '(choice
  1783. (const :tag "Not" nil)
  1784. (const :tag "Always" t)
  1785. (repeat :tag "Specific properties" (string :tag "Property"))
  1786. (regexp :tag "Properties matched by regexp")))
  1787. (defun org-property-inherit-p (property)
  1788. "Check if PROPERTY is one that should be inherited."
  1789. (cond
  1790. ((eq org-use-property-inheritance t) t)
  1791. ((not org-use-property-inheritance) nil)
  1792. ((stringp org-use-property-inheritance)
  1793. (string-match org-use-property-inheritance property))
  1794. ((listp org-use-property-inheritance)
  1795. (member property org-use-property-inheritance))
  1796. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1797. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1798. "The default column format, if no other format has been defined.
  1799. This variable can be set on the per-file basis by inserting a line
  1800. #+COLUMNS: %25ITEM ....."
  1801. :group 'org-properties
  1802. :type 'string)
  1803. (defcustom org-columns-ellipses ".."
  1804. "The ellipses to be used when a field in column view is truncated.
  1805. When this is the empty string, as many characters as possible are shown,
  1806. but then there will be no visual indication that the field has been truncated.
  1807. When this is a string of length N, the last N characters of a truncated
  1808. field are replaced by this string. If the column is narrower than the
  1809. ellipses string, only part of the ellipses string will be shown."
  1810. :group 'org-properties
  1811. :type 'string)
  1812. (defcustom org-columns-modify-value-for-display-function nil
  1813. "Function that modifies values for display in column view.
  1814. For example, it can be used to cut out a certain part from a time stamp.
  1815. The function must take 2 arguments:
  1816. column-title The title of the column (*not* the property name)
  1817. value The value that should be modified.
  1818. The function should return the value that should be displayed,
  1819. or nil if the normal value should be used."
  1820. :group 'org-properties
  1821. :type 'function)
  1822. (defcustom org-effort-property "Effort"
  1823. "The property that is being used to keep track of effort estimates.
  1824. Effort estimates given in this property need to have the format H:MM."
  1825. :group 'org-properties
  1826. :group 'org-progress
  1827. :type '(string :tag "Property"))
  1828. (defconst org-global-properties-fixed
  1829. '(("VISIBILITY_ALL" . "folded children content all"))
  1830. "List of property/value pairs that can be inherited by any entry.
  1831. These are fixed values, for the preset properties.")
  1832. (defcustom org-global-properties nil
  1833. "List of property/value pairs that can be inherited by any entry.
  1834. You can set buffer-local values for the same purpose in the variable
  1835. `org-file-properties' this by adding lines like
  1836. #+PROPERTY: NAME VALUE"
  1837. :group 'org-properties
  1838. :type '(repeat
  1839. (cons (string :tag "Property")
  1840. (string :tag "Value"))))
  1841. (defvar org-file-properties nil
  1842. "List of property/value pairs that can be inherited by any entry.
  1843. Valid for the current buffer.
  1844. This variable is populated from #+PROPERTY lines.")
  1845. (make-variable-buffer-local 'org-file-properties)
  1846. (defgroup org-agenda nil
  1847. "Options concerning agenda views in Org-mode."
  1848. :tag "Org Agenda"
  1849. :group 'org)
  1850. (defvar org-category nil
  1851. "Variable used by org files to set a category for agenda display.
  1852. Such files should use a file variable to set it, for example
  1853. # -*- mode: org; org-category: \"ELisp\"
  1854. or contain a special line
  1855. #+CATEGORY: ELisp
  1856. If the file does not specify a category, then file's base name
  1857. is used instead.")
  1858. (make-variable-buffer-local 'org-category)
  1859. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1860. (defcustom org-agenda-files nil
  1861. "The files to be used for agenda display.
  1862. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1863. \\[org-remove-file]. You can also use customize to edit the list.
  1864. If an entry is a directory, all files in that directory that are matched by
  1865. `org-agenda-file-regexp' will be part of the file list.
  1866. If the value of the variable is not a list but a single file name, then
  1867. the list of agenda files is actually stored and maintained in that file, one
  1868. agenda file per line."
  1869. :group 'org-agenda
  1870. :type '(choice
  1871. (repeat :tag "List of files and directories" file)
  1872. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1873. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1874. "Regular expression to match files for `org-agenda-files'.
  1875. If any element in the list in that variable contains a directory instead
  1876. of a normal file, all files in that directory that are matched by this
  1877. regular expression will be included."
  1878. :group 'org-agenda
  1879. :type 'regexp)
  1880. (defcustom org-agenda-text-search-extra-files nil
  1881. "List of extra files to be searched by text search commands.
  1882. These files will be search in addition to the agenda files by the
  1883. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1884. Note that these files will only be searched for text search commands,
  1885. not for the other agenda views like todo lists, tag searches or the weekly
  1886. agenda. This variable is intended to list notes and possibly archive files
  1887. that should also be searched by these two commands.
  1888. In fact, if the first element in the list is the symbol `agenda-archives',
  1889. than all archive files of all agenda files will be added to the search
  1890. scope."
  1891. :group 'org-agenda
  1892. :type '(set :greedy t
  1893. (const :tag "Agenda Archives" agenda-archives)
  1894. (repeat :inline t (file))))
  1895. (if (fboundp 'defvaralias)
  1896. (defvaralias 'org-agenda-multi-occur-extra-files
  1897. 'org-agenda-text-search-extra-files))
  1898. (defcustom org-agenda-skip-unavailable-files nil
  1899. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1900. A nil value means to remove them, after a query, from the list."
  1901. :group 'org-agenda
  1902. :type 'boolean)
  1903. (defcustom org-calendar-to-agenda-key [?c]
  1904. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1905. The command `org-calendar-goto-agenda' will be bound to this key. The
  1906. default is the character `c' because then `c' can be used to switch back and
  1907. forth between agenda and calendar."
  1908. :group 'org-agenda
  1909. :type 'sexp)
  1910. (defcustom org-calendar-agenda-action-key [?k]
  1911. "The key to be installed in `calendar-mode-map' for agenda-action.
  1912. The command `org-agenda-action' will be bound to this key. The
  1913. default is the character `k' because we use the same key in the agenda."
  1914. :group 'org-agenda
  1915. :type 'sexp)
  1916. (eval-after-load "calendar"
  1917. '(progn
  1918. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1919. 'org-calendar-goto-agenda)
  1920. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1921. 'org-agenda-action)))
  1922. (defgroup org-latex nil
  1923. "Options for embedding LaTeX code into Org-mode."
  1924. :tag "Org LaTeX"
  1925. :group 'org)
  1926. (defcustom org-format-latex-options
  1927. '(:foreground default :background default :scale 1.0
  1928. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1929. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1930. "Options for creating images from LaTeX fragments.
  1931. This is a property list with the following properties:
  1932. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1933. `default' means use the foreground of the default face.
  1934. :background the background color, or \"Transparent\".
  1935. `default' means use the background of the default face.
  1936. :scale a scaling factor for the size of the images.
  1937. :html-foreground, :html-background, :html-scale
  1938. the same numbers for HTML export.
  1939. :matchers a list indicating which matchers should be used to
  1940. find LaTeX fragments. Valid members of this list are:
  1941. \"begin\" find environments
  1942. \"$\" find math expressions surrounded by $...$
  1943. \"$$\" find math expressions surrounded by $$....$$
  1944. \"\\(\" find math expressions surrounded by \\(...\\)
  1945. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1946. :group 'org-latex
  1947. :type 'plist)
  1948. (defcustom org-format-latex-header "\\documentclass{article}
  1949. \\usepackage{fullpage} % do not remove
  1950. \\usepackage{amssymb}
  1951. \\usepackage[usenames]{color}
  1952. \\usepackage{amsmath}
  1953. \\usepackage{latexsym}
  1954. \\usepackage[mathscr]{eucal}
  1955. \\pagestyle{empty} % do not remove"
  1956. "The document header used for processing LaTeX fragments."
  1957. :group 'org-latex
  1958. :type 'string)
  1959. (defgroup org-font-lock nil
  1960. "Font-lock settings for highlighting in Org-mode."
  1961. :tag "Org Font Lock"
  1962. :group 'org)
  1963. (defcustom org-level-color-stars-only nil
  1964. "Non-nil means fontify only the stars in each headline.
  1965. When nil, the entire headline is fontified.
  1966. Changing it requires restart of `font-lock-mode' to become effective
  1967. also in regions already fontified."
  1968. :group 'org-font-lock
  1969. :type 'boolean)
  1970. (defcustom org-hide-leading-stars nil
  1971. "Non-nil means, hide the first N-1 stars in a headline.
  1972. This works by using the face `org-hide' for these stars. This
  1973. face is white for a light background, and black for a dark
  1974. background. You may have to customize the face `org-hide' to
  1975. make this work.
  1976. Changing it requires restart of `font-lock-mode' to become effective
  1977. also in regions already fontified.
  1978. You may also set this on a per-file basis by adding one of the following
  1979. lines to the buffer:
  1980. #+STARTUP: hidestars
  1981. #+STARTUP: showstars"
  1982. :group 'org-font-lock
  1983. :type 'boolean)
  1984. (defcustom org-fontify-done-headline nil
  1985. "Non-nil means, change the face of a headline if it is marked DONE.
  1986. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1987. When this is non-nil, the headline after the keyword is set to the
  1988. `org-headline-done' as an additional indication."
  1989. :group 'org-font-lock
  1990. :type 'boolean)
  1991. (defcustom org-fontify-emphasized-text t
  1992. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1993. Changing this variable requires a restart of Emacs to take effect."
  1994. :group 'org-font-lock
  1995. :type 'boolean)
  1996. (defcustom org-highlight-latex-fragments-and-specials nil
  1997. "Non-nil means, fontify what is treated specially by the exporters."
  1998. :group 'org-font-lock
  1999. :type 'boolean)
  2000. (defcustom org-hide-emphasis-markers nil
  2001. "Non-nil mean font-lock should hide the emphasis marker characters."
  2002. :group 'org-font-lock
  2003. :type 'boolean)
  2004. (defvar org-emph-re nil
  2005. "Regular expression for matching emphasis.")
  2006. (defvar org-verbatim-re nil
  2007. "Regular expression for matching verbatim text.")
  2008. (defvar org-emphasis-regexp-components) ; defined just below
  2009. (defvar org-emphasis-alist) ; defined just below
  2010. (defun org-set-emph-re (var val)
  2011. "Set variable and compute the emphasis regular expression."
  2012. (set var val)
  2013. (when (and (boundp 'org-emphasis-alist)
  2014. (boundp 'org-emphasis-regexp-components)
  2015. org-emphasis-alist org-emphasis-regexp-components)
  2016. (let* ((e org-emphasis-regexp-components)
  2017. (pre (car e))
  2018. (post (nth 1 e))
  2019. (border (nth 2 e))
  2020. (body (nth 3 e))
  2021. (nl (nth 4 e))
  2022. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  2023. (body1 (concat body "*?"))
  2024. (markers (mapconcat 'car org-emphasis-alist ""))
  2025. (vmarkers (mapconcat
  2026. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2027. org-emphasis-alist "")))
  2028. ;; make sure special characters appear at the right position in the class
  2029. (if (string-match "\\^" markers)
  2030. (setq markers (concat (replace-match "" t t markers) "^")))
  2031. (if (string-match "-" markers)
  2032. (setq markers (concat (replace-match "" t t markers) "-")))
  2033. (if (string-match "\\^" vmarkers)
  2034. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2035. (if (string-match "-" vmarkers)
  2036. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2037. (if (> nl 0)
  2038. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2039. (int-to-string nl) "\\}")))
  2040. ;; Make the regexp
  2041. (setq org-emph-re
  2042. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  2043. "\\("
  2044. "\\([" markers "]\\)"
  2045. "\\("
  2046. "[^" border "]\\|"
  2047. "[^" border (if (and nil stacked) markers) "]"
  2048. body1
  2049. "[^" border (if (and nil stacked) markers) "]"
  2050. "\\)"
  2051. "\\3\\)"
  2052. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  2053. (setq org-verbatim-re
  2054. (concat "\\([" pre "]\\|^\\)"
  2055. "\\("
  2056. "\\([" vmarkers "]\\)"
  2057. "\\("
  2058. "[^" border "]\\|"
  2059. "[^" border "]"
  2060. body1
  2061. "[^" border "]"
  2062. "\\)"
  2063. "\\3\\)"
  2064. "\\([" post "]\\|$\\)")))))
  2065. (defcustom org-emphasis-regexp-components
  2066. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2067. "Components used to build the regular expression for emphasis.
  2068. This is a list with 6 entries. Terminology: In an emphasis string
  2069. like \" *strong word* \", we call the initial space PREMATCH, the final
  2070. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2071. and \"trong wor\" is the body. The different components in this variable
  2072. specify what is allowed/forbidden in each part:
  2073. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2074. post Chars allowed as postmatch. End of line will be allowed too.
  2075. border The chars *forbidden* as border characters.
  2076. body-regexp A regexp like \".\" to match a body character. Don't use
  2077. non-shy groups here, and don't allow newline here.
  2078. newline The maximum number of newlines allowed in an emphasis exp.
  2079. Use customize to modify this, or restart Emacs after changing it."
  2080. :group 'org-font-lock
  2081. :set 'org-set-emph-re
  2082. :type '(list
  2083. (sexp :tag "Allowed chars in pre ")
  2084. (sexp :tag "Allowed chars in post ")
  2085. (sexp :tag "Forbidden chars in border ")
  2086. (sexp :tag "Regexp for body ")
  2087. (integer :tag "number of newlines allowed")
  2088. (option (boolean :tag "Please ignore this button"))))
  2089. (defcustom org-emphasis-alist
  2090. `(("*" bold "<b>" "</b>")
  2091. ("/" italic "<i>" "</i>")
  2092. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2093. ("=" org-code "<code>" "</code>" verbatim)
  2094. ("~" org-verbatim "<code>" "</code>" verbatim)
  2095. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2096. "<del>" "</del>")
  2097. )
  2098. "Special syntax for emphasized text.
  2099. Text starting and ending with a special character will be emphasized, for
  2100. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2101. characters, the face to be used by font-lock for highlighting in Org-mode
  2102. Emacs buffers, and the HTML tags to be used for this.
  2103. Use customize to modify this, or restart Emacs after changing it."
  2104. :group 'org-font-lock
  2105. :set 'org-set-emph-re
  2106. :type '(repeat
  2107. (list
  2108. (string :tag "Marker character")
  2109. (choice
  2110. (face :tag "Font-lock-face")
  2111. (plist :tag "Face property list"))
  2112. (string :tag "HTML start tag")
  2113. (string :tag "HTML end tag")
  2114. (option (const verbatim)))))
  2115. ;;; Miscellaneous options
  2116. (defgroup org-completion nil
  2117. "Completion in Org-mode."
  2118. :tag "Org Completion"
  2119. :group 'org)
  2120. (defcustom org-completion-use-ido nil
  2121. "Non-nil means, use ido completion wherever possible.
  2122. Note that `ido-mode' must be active for this variable to be relevant.
  2123. If you decide to turn this variable on, you might well want to turn off
  2124. `org-outline-path-complete-in-steps'."
  2125. :group 'org-completion
  2126. :type 'boolean)
  2127. (defcustom org-completion-fallback-command 'hippie-expand
  2128. "The expansion command called by \\[org-complete] in normal context.
  2129. Normal means, no org-mode-specific context."
  2130. :group 'org-completion
  2131. :type 'function)
  2132. ;;; Functions and variables from ther packages
  2133. ;; Declared here to avoid compiler warnings
  2134. ;; XEmacs only
  2135. (defvar outline-mode-menu-heading)
  2136. (defvar outline-mode-menu-show)
  2137. (defvar outline-mode-menu-hide)
  2138. (defvar zmacs-regions) ; XEmacs regions
  2139. ;; Emacs only
  2140. (defvar mark-active)
  2141. ;; Various packages
  2142. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2143. (declare-function calendar-forward-day "cal-move" (arg))
  2144. (declare-function calendar-goto-date "cal-move" (date))
  2145. (declare-function calendar-goto-today "cal-move" ())
  2146. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2147. (defvar calc-embedded-close-formula)
  2148. (defvar calc-embedded-open-formula)
  2149. (declare-function cdlatex-tab "ext:cdlatex" ())
  2150. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2151. (defvar font-lock-unfontify-region-function)
  2152. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2153. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2154. (defvar iswitchb-temp-buflist)
  2155. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2156. (declare-function org-agenda-skip "org-agenda" ())
  2157. (declare-function org-format-agenda-item "org-agenda"
  2158. (extra txt &optional category tags dotime noprefix remove-re))
  2159. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2160. (declare-function org-agenda-change-all-lines "org-agenda"
  2161. (newhead hdmarker &optional fixface just-this))
  2162. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2163. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2164. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2165. (beg end))
  2166. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2167. (declare-function parse-time-string "parse-time" (string))
  2168. (declare-function remember "remember" (&optional initial))
  2169. (declare-function remember-buffer-desc "remember" ())
  2170. (declare-function remember-finalize "remember" ())
  2171. (defvar remember-save-after-remembering)
  2172. (defvar remember-data-file)
  2173. (defvar remember-register)
  2174. (defvar remember-buffer)
  2175. (defvar remember-handler-functions)
  2176. (defvar remember-annotation-functions)
  2177. (defvar texmathp-why)
  2178. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2179. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2180. (defvar w3m-current-url)
  2181. (defvar w3m-current-title)
  2182. (defvar org-latex-regexps)
  2183. ;;; Autoload and prepare some org modules
  2184. ;; Some table stuff that needs to be defined here, because it is used
  2185. ;; by the functions setting up org-mode or checking for table context.
  2186. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2187. "Detects an org-type or table-type table.")
  2188. (defconst org-table-line-regexp "^[ \t]*|"
  2189. "Detects an org-type table line.")
  2190. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2191. "Detects an org-type table line.")
  2192. (defconst org-table-hline-regexp "^[ \t]*|-"
  2193. "Detects an org-type table hline.")
  2194. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2195. "Detects a table-type table hline.")
  2196. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2197. "Searching from within a table (any type) this finds the first line
  2198. outside the table.")
  2199. ;; Autoload the functions in org-table.el that are needed by functions here.
  2200. (eval-and-compile
  2201. (org-autoload "org-table"
  2202. '(org-table-align org-table-begin org-table-blank-field
  2203. org-table-convert org-table-convert-region org-table-copy-down
  2204. org-table-copy-region org-table-create
  2205. org-table-create-or-convert-from-region
  2206. org-table-create-with-table.el org-table-current-dline
  2207. org-table-cut-region org-table-delete-column org-table-edit-field
  2208. org-table-edit-formulas org-table-end org-table-eval-formula
  2209. org-table-export org-table-field-info
  2210. org-table-get-stored-formulas org-table-goto-column
  2211. org-table-hline-and-move org-table-import org-table-insert-column
  2212. org-table-insert-hline org-table-insert-row org-table-iterate
  2213. org-table-justify-field-maybe org-table-kill-row
  2214. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2215. org-table-move-column org-table-move-column-left
  2216. org-table-move-column-right org-table-move-row
  2217. org-table-move-row-down org-table-move-row-up
  2218. org-table-next-field org-table-next-row org-table-paste-rectangle
  2219. org-table-previous-field org-table-recalculate
  2220. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2221. org-table-toggle-coordinate-overlays
  2222. org-table-toggle-formula-debugger org-table-wrap-region
  2223. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2224. (defun org-at-table-p (&optional table-type)
  2225. "Return t if the cursor is inside an org-type table.
  2226. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2227. (if org-enable-table-editor
  2228. (save-excursion
  2229. (beginning-of-line 1)
  2230. (looking-at (if table-type org-table-any-line-regexp
  2231. org-table-line-regexp)))
  2232. nil))
  2233. (defsubst org-table-p () (org-at-table-p))
  2234. (defun org-at-table.el-p ()
  2235. "Return t if and only if we are at a table.el table."
  2236. (and (org-at-table-p 'any)
  2237. (save-excursion
  2238. (goto-char (org-table-begin 'any))
  2239. (looking-at org-table1-hline-regexp))))
  2240. (defun org-table-recognize-table.el ()
  2241. "If there is a table.el table nearby, recognize it and move into it."
  2242. (if org-table-tab-recognizes-table.el
  2243. (if (org-at-table.el-p)
  2244. (progn
  2245. (beginning-of-line 1)
  2246. (if (looking-at org-table-dataline-regexp)
  2247. nil
  2248. (if (looking-at org-table1-hline-regexp)
  2249. (progn
  2250. (beginning-of-line 2)
  2251. (if (looking-at org-table-any-border-regexp)
  2252. (beginning-of-line -1)))))
  2253. (if (re-search-forward "|" (org-table-end t) t)
  2254. (progn
  2255. (require 'table)
  2256. (if (table--at-cell-p (point))
  2257. t
  2258. (message "recognizing table.el table...")
  2259. (table-recognize-table)
  2260. (message "recognizing table.el table...done")))
  2261. (error "This should not happen..."))
  2262. t)
  2263. nil)
  2264. nil))
  2265. (defun org-at-table-hline-p ()
  2266. "Return t if the cursor is inside a hline in a table."
  2267. (if org-enable-table-editor
  2268. (save-excursion
  2269. (beginning-of-line 1)
  2270. (looking-at org-table-hline-regexp))
  2271. nil))
  2272. (defvar org-table-clean-did-remove-column nil)
  2273. (defun org-table-map-tables (function)
  2274. "Apply FUNCTION to the start of all tables in the buffer."
  2275. (save-excursion
  2276. (save-restriction
  2277. (widen)
  2278. (goto-char (point-min))
  2279. (while (re-search-forward org-table-any-line-regexp nil t)
  2280. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2281. (beginning-of-line 1)
  2282. (if (looking-at org-table-line-regexp)
  2283. (save-excursion (funcall function)))
  2284. (re-search-forward org-table-any-border-regexp nil 1))))
  2285. (message "Mapping tables: done"))
  2286. ;; Declare and autoload functions from org-exp.el
  2287. (declare-function org-default-export-plist "org-exp")
  2288. (declare-function org-infile-export-plist "org-exp")
  2289. (declare-function org-get-current-options "org-exp")
  2290. (eval-and-compile
  2291. (org-autoload "org-exp"
  2292. '(org-export org-export-as-ascii org-export-visible
  2293. org-insert-export-options-template org-export-as-html-and-open
  2294. org-export-as-html-batch org-export-as-html-to-buffer
  2295. org-replace-region-by-html org-export-region-as-html
  2296. org-export-as-html org-export-icalendar-this-file
  2297. org-export-icalendar-all-agenda-files
  2298. org-table-clean-before-export
  2299. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2300. ;; Declare and autoload functions from org-agenda.el
  2301. (eval-and-compile
  2302. (org-autoload "org-agenda"
  2303. '(org-agenda org-agenda-list org-search-view
  2304. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2305. org-diary org-agenda-to-appt)))
  2306. ;; Autoload org-remember
  2307. (eval-and-compile
  2308. (org-autoload "org-remember"
  2309. '(org-remember-insinuate org-remember-annotation
  2310. org-remember-apply-template org-remember org-remember-handler)))
  2311. ;; Autoload org-clock.el
  2312. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2313. (beg end))
  2314. (declare-function org-update-mode-line "org-clock" ())
  2315. (defvar org-clock-start-time)
  2316. (defvar org-clock-marker (make-marker)
  2317. "Marker recording the last clock-in.")
  2318. (eval-and-compile
  2319. (org-autoload
  2320. "org-clock"
  2321. '(org-clock-in org-clock-out org-clock-cancel
  2322. org-clock-goto org-clock-sum org-clock-display
  2323. org-remove-clock-overlays org-clock-report
  2324. org-clocktable-shift org-dblock-write:clocktable
  2325. org-get-clocktable)))
  2326. (defun org-clock-update-time-maybe ()
  2327. "If this is a CLOCK line, update it and return t.
  2328. Otherwise, return nil."
  2329. (interactive)
  2330. (save-excursion
  2331. (beginning-of-line 1)
  2332. (skip-chars-forward " \t")
  2333. (when (looking-at org-clock-string)
  2334. (let ((re (concat "[ \t]*" org-clock-string
  2335. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2336. "\\([ \t]*=>.*\\)?\\)?"))
  2337. ts te h m s neg)
  2338. (cond
  2339. ((not (looking-at re))
  2340. nil)
  2341. ((not (match-end 2))
  2342. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2343. (> org-clock-marker (point))
  2344. (<= org-clock-marker (point-at-eol)))
  2345. ;; The clock is running here
  2346. (setq org-clock-start-time
  2347. (apply 'encode-time
  2348. (org-parse-time-string (match-string 1))))
  2349. (org-update-mode-line)))
  2350. (t
  2351. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2352. (end-of-line 1)
  2353. (setq ts (match-string 1)
  2354. te (match-string 3))
  2355. (setq s (- (time-to-seconds
  2356. (apply 'encode-time (org-parse-time-string te)))
  2357. (time-to-seconds
  2358. (apply 'encode-time (org-parse-time-string ts))))
  2359. neg (< s 0)
  2360. s (abs s)
  2361. h (floor (/ s 3600))
  2362. s (- s (* 3600 h))
  2363. m (floor (/ s 60))
  2364. s (- s (* 60 s)))
  2365. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2366. t))))))
  2367. (defun org-check-running-clock ()
  2368. "Check if the current buffer contains the running clock.
  2369. If yes, offer to stop it and to save the buffer with the changes."
  2370. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2371. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2372. (buffer-name))))
  2373. (org-clock-out)
  2374. (when (y-or-n-p "Save changed buffer?")
  2375. (save-buffer))))
  2376. (defun org-clocktable-try-shift (dir n)
  2377. "Check if this line starts a clock table, if yes, shift the time block."
  2378. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2379. (org-clocktable-shift dir n)))
  2380. ;; Autoload org-timer.el
  2381. ;(declare-function org-timer "org-timer")
  2382. (eval-and-compile
  2383. (org-autoload
  2384. "org-timer"
  2385. '(org-timer-start org-timer org-timer-item
  2386. org-timer-change-times-in-region)))
  2387. ;; Autoload archiving code
  2388. ;; The stuff that is needed for cycling and tags has to be defined here.
  2389. (defgroup org-archive nil
  2390. "Options concerning archiving in Org-mode."
  2391. :tag "Org Archive"
  2392. :group 'org-structure)
  2393. (defcustom org-archive-location "%s_archive::"
  2394. "The location where subtrees should be archived.
  2395. The value of this variable is a string, consisting of two parts,
  2396. separated by a double-colon. The first part is a filename and
  2397. the second part is a headline.
  2398. When the filename is omitted, archiving happens in the same file.
  2399. %s in the filename will be replaced by the current file
  2400. name (without the directory part). Archiving to a different file
  2401. is useful to keep archived entries from contributing to the
  2402. Org-mode Agenda.
  2403. The archived entries will be filed as subtrees of the specified
  2404. headline. When the headline is omitted, the subtrees are simply
  2405. filed away at the end of the file, as top-level entries.
  2406. Here are a few examples:
  2407. \"%s_archive::\"
  2408. If the current file is Projects.org, archive in file
  2409. Projects.org_archive, as top-level trees. This is the default.
  2410. \"::* Archived Tasks\"
  2411. Archive in the current file, under the top-level headline
  2412. \"* Archived Tasks\".
  2413. \"~/org/archive.org::\"
  2414. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2415. \"basement::** Finished Tasks\"
  2416. Archive in file ./basement (relative path), as level 3 trees
  2417. below the level 2 heading \"** Finished Tasks\".
  2418. You may set this option on a per-file basis by adding to the buffer a
  2419. line like
  2420. #+ARCHIVE: basement::** Finished Tasks
  2421. You may also define it locally for a subtree by setting an ARCHIVE property
  2422. in the entry. If such a property is found in an entry, or anywhere up
  2423. the hierarchy, it will be used."
  2424. :group 'org-archive
  2425. :type 'string)
  2426. (defcustom org-archive-tag "ARCHIVE"
  2427. "The tag that marks a subtree as archived.
  2428. An archived subtree does not open during visibility cycling, and does
  2429. not contribute to the agenda listings.
  2430. After changing this, font-lock must be restarted in the relevant buffers to
  2431. get the proper fontification."
  2432. :group 'org-archive
  2433. :group 'org-keywords
  2434. :type 'string)
  2435. (defcustom org-agenda-skip-archived-trees t
  2436. "Non-nil means, the agenda will skip any items located in archived trees.
  2437. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2438. variable is no longer recommended, you should leave it at the value t.
  2439. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2440. :group 'org-archive
  2441. :group 'org-agenda-skip
  2442. :type 'boolean)
  2443. (defcustom org-cycle-open-archived-trees nil
  2444. "Non-nil means, `org-cycle' will open archived trees.
  2445. An archived tree is a tree marked with the tag ARCHIVE.
  2446. When nil, archived trees will stay folded. You can still open them with
  2447. normal outline commands like `show-all', but not with the cycling commands."
  2448. :group 'org-archive
  2449. :group 'org-cycle
  2450. :type 'boolean)
  2451. (defcustom org-sparse-tree-open-archived-trees nil
  2452. "Non-nil means sparse tree construction shows matches in archived trees.
  2453. When nil, matches in these trees are highlighted, but the trees are kept in
  2454. collapsed state."
  2455. :group 'org-archive
  2456. :group 'org-sparse-trees
  2457. :type 'boolean)
  2458. (defun org-cycle-hide-archived-subtrees (state)
  2459. "Re-hide all archived subtrees after a visibility state change."
  2460. (when (and (not org-cycle-open-archived-trees)
  2461. (not (memq state '(overview folded))))
  2462. (save-excursion
  2463. (let* ((globalp (memq state '(contents all)))
  2464. (beg (if globalp (point-min) (point)))
  2465. (end (if globalp (point-max) (org-end-of-subtree t))))
  2466. (org-hide-archived-subtrees beg end)
  2467. (goto-char beg)
  2468. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2469. (message "%s" (substitute-command-keys
  2470. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2471. (defun org-force-cycle-archived ()
  2472. "Cycle subtree even if it is archived."
  2473. (interactive)
  2474. (setq this-command 'org-cycle)
  2475. (let ((org-cycle-open-archived-trees t))
  2476. (call-interactively 'org-cycle)))
  2477. (defun org-hide-archived-subtrees (beg end)
  2478. "Re-hide all archived subtrees after a visibility state change."
  2479. (save-excursion
  2480. (let* ((re (concat ":" org-archive-tag ":")))
  2481. (goto-char beg)
  2482. (while (re-search-forward re end t)
  2483. (and (org-on-heading-p) (hide-subtree))
  2484. (org-end-of-subtree t)))))
  2485. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2486. (eval-and-compile
  2487. (org-autoload "org-archive"
  2488. '(org-add-archive-files org-archive-subtree
  2489. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2490. ;; Autoload Column View Code
  2491. (declare-function org-columns-number-to-string "org-colview")
  2492. (declare-function org-columns-get-format-and-top-level "org-colview")
  2493. (declare-function org-columns-compute "org-colview")
  2494. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2495. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2496. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2497. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2498. ;; Autoload ID code
  2499. (declare-function org-id-store-link "org-id")
  2500. (org-autoload "org-id"
  2501. '(org-id-get-create org-id-new org-id-copy org-id-get
  2502. org-id-get-with-outline-path-completion
  2503. org-id-get-with-outline-drilling
  2504. org-id-goto org-id-find org-id-store-link))
  2505. ;;; Variables for pre-computed regular expressions, all buffer local
  2506. (defvar org-drawer-regexp nil
  2507. "Matches first line of a hidden block.")
  2508. (make-variable-buffer-local 'org-drawer-regexp)
  2509. (defvar org-todo-regexp nil
  2510. "Matches any of the TODO state keywords.")
  2511. (make-variable-buffer-local 'org-todo-regexp)
  2512. (defvar org-not-done-regexp nil
  2513. "Matches any of the TODO state keywords except the last one.")
  2514. (make-variable-buffer-local 'org-not-done-regexp)
  2515. (defvar org-todo-line-regexp nil
  2516. "Matches a headline and puts TODO state into group 2 if present.")
  2517. (make-variable-buffer-local 'org-todo-line-regexp)
  2518. (defvar org-complex-heading-regexp nil
  2519. "Matches a headline and puts everything into groups:
  2520. group 1: the stars
  2521. group 2: The todo keyword, maybe
  2522. group 3: Priority cookie
  2523. group 4: True headline
  2524. group 5: Tags")
  2525. (make-variable-buffer-local 'org-complex-heading-regexp)
  2526. (defvar org-todo-line-tags-regexp nil
  2527. "Matches a headline and puts TODO state into group 2 if present.
  2528. Also put tags into group 4 if tags are present.")
  2529. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2530. (defvar org-nl-done-regexp nil
  2531. "Matches newline followed by a headline with the DONE keyword.")
  2532. (make-variable-buffer-local 'org-nl-done-regexp)
  2533. (defvar org-looking-at-done-regexp nil
  2534. "Matches the DONE keyword a point.")
  2535. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2536. (defvar org-ds-keyword-length 12
  2537. "Maximum length of the Deadline and SCHEDULED keywords.")
  2538. (make-variable-buffer-local 'org-ds-keyword-length)
  2539. (defvar org-deadline-regexp nil
  2540. "Matches the DEADLINE keyword.")
  2541. (make-variable-buffer-local 'org-deadline-regexp)
  2542. (defvar org-deadline-time-regexp nil
  2543. "Matches the DEADLINE keyword together with a time stamp.")
  2544. (make-variable-buffer-local 'org-deadline-time-regexp)
  2545. (defvar org-deadline-line-regexp nil
  2546. "Matches the DEADLINE keyword and the rest of the line.")
  2547. (make-variable-buffer-local 'org-deadline-line-regexp)
  2548. (defvar org-scheduled-regexp nil
  2549. "Matches the SCHEDULED keyword.")
  2550. (make-variable-buffer-local 'org-scheduled-regexp)
  2551. (defvar org-scheduled-time-regexp nil
  2552. "Matches the SCHEDULED keyword together with a time stamp.")
  2553. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2554. (defvar org-closed-time-regexp nil
  2555. "Matches the CLOSED keyword together with a time stamp.")
  2556. (make-variable-buffer-local 'org-closed-time-regexp)
  2557. (defvar org-keyword-time-regexp nil
  2558. "Matches any of the 4 keywords, together with the time stamp.")
  2559. (make-variable-buffer-local 'org-keyword-time-regexp)
  2560. (defvar org-keyword-time-not-clock-regexp nil
  2561. "Matches any of the 3 keywords, together with the time stamp.")
  2562. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2563. (defvar org-maybe-keyword-time-regexp nil
  2564. "Matches a timestamp, possibly preceeded by a keyword.")
  2565. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2566. (defvar org-planning-or-clock-line-re nil
  2567. "Matches a line with planning or clock info.")
  2568. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2569. (defconst org-plain-time-of-day-regexp
  2570. (concat
  2571. "\\(\\<[012]?[0-9]"
  2572. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2573. "\\(--?"
  2574. "\\(\\<[012]?[0-9]"
  2575. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2576. "\\)?")
  2577. "Regular expression to match a plain time or time range.
  2578. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2579. groups carry important information:
  2580. 0 the full match
  2581. 1 the first time, range or not
  2582. 8 the second time, if it is a range.")
  2583. (defconst org-plain-time-extension-regexp
  2584. (concat
  2585. "\\(\\<[012]?[0-9]"
  2586. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2587. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2588. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2589. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2590. groups carry important information:
  2591. 0 the full match
  2592. 7 hours of duration
  2593. 9 minutes of duration")
  2594. (defconst org-stamp-time-of-day-regexp
  2595. (concat
  2596. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2597. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2598. "\\(--?"
  2599. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2600. "Regular expression to match a timestamp time or time range.
  2601. After a match, the following groups carry important information:
  2602. 0 the full match
  2603. 1 date plus weekday, for backreferencing to make sure both times on same day
  2604. 2 the first time, range or not
  2605. 4 the second time, if it is a range.")
  2606. (defconst org-startup-options
  2607. '(("fold" org-startup-folded t)
  2608. ("overview" org-startup-folded t)
  2609. ("nofold" org-startup-folded nil)
  2610. ("showall" org-startup-folded nil)
  2611. ("content" org-startup-folded content)
  2612. ("hidestars" org-hide-leading-stars t)
  2613. ("showstars" org-hide-leading-stars nil)
  2614. ("odd" org-odd-levels-only t)
  2615. ("oddeven" org-odd-levels-only nil)
  2616. ("align" org-startup-align-all-tables t)
  2617. ("noalign" org-startup-align-all-tables nil)
  2618. ("customtime" org-display-custom-times t)
  2619. ("logdone" org-log-done time)
  2620. ("lognotedone" org-log-done note)
  2621. ("nologdone" org-log-done nil)
  2622. ("lognoteclock-out" org-log-note-clock-out t)
  2623. ("nolognoteclock-out" org-log-note-clock-out nil)
  2624. ("logrepeat" org-log-repeat state)
  2625. ("lognoterepeat" org-log-repeat note)
  2626. ("nologrepeat" org-log-repeat nil)
  2627. ("fninline" org-footnote-define-inline t)
  2628. ("nofninline" org-footnote-define-inline nil)
  2629. ("fnauto" org-footnote-auto-label t)
  2630. ("fnprompt" org-footnote-auto-label nil)
  2631. ("fnconfirm" org-footnote-auto-label confirm)
  2632. ("fnplain" org-footnote-auto-label plain)
  2633. ("constcgs" constants-unit-system cgs)
  2634. ("constSI" constants-unit-system SI))
  2635. "Variable associated with STARTUP options for org-mode.
  2636. Each element is a list of three items: The startup options as written
  2637. in the #+STARTUP line, the corresponding variable, and the value to
  2638. set this variable to if the option is found. An optional forth element PUSH
  2639. means to push this value onto the list in the variable.")
  2640. (defun org-set-regexps-and-options ()
  2641. "Precompute regular expressions for current buffer."
  2642. (when (org-mode-p)
  2643. (org-set-local 'org-todo-kwd-alist nil)
  2644. (org-set-local 'org-todo-key-alist nil)
  2645. (org-set-local 'org-todo-key-trigger nil)
  2646. (org-set-local 'org-todo-keywords-1 nil)
  2647. (org-set-local 'org-done-keywords nil)
  2648. (org-set-local 'org-todo-heads nil)
  2649. (org-set-local 'org-todo-sets nil)
  2650. (org-set-local 'org-todo-log-states nil)
  2651. (org-set-local 'org-file-properties nil)
  2652. (org-set-local 'org-file-tags nil)
  2653. (let ((re (org-make-options-regexp
  2654. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2655. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2656. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2657. (splitre "[ \t]+")
  2658. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2659. tail sep kws1 prio props ftags drawers
  2660. ext-setup-or-nil setup-contents (start 0))
  2661. (save-excursion
  2662. (save-restriction
  2663. (widen)
  2664. (goto-char (point-min))
  2665. (while (or (and ext-setup-or-nil
  2666. (string-match re ext-setup-or-nil start)
  2667. (setq start (match-end 0)))
  2668. (and (setq ext-setup-or-nil nil start 0)
  2669. (re-search-forward re nil t)))
  2670. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2671. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2672. (cond
  2673. ((equal key "CATEGORY")
  2674. (if (string-match "[ \t]+$" value)
  2675. (setq value (replace-match "" t t value)))
  2676. (setq cat value))
  2677. ((member key '("SEQ_TODO" "TODO"))
  2678. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2679. ((equal key "TYP_TODO")
  2680. (push (cons 'type (org-split-string value splitre)) kwds))
  2681. ((equal key "TAGS")
  2682. (setq tags (append tags (org-split-string value splitre))))
  2683. ((equal key "COLUMNS")
  2684. (org-set-local 'org-columns-default-format value))
  2685. ((equal key "LINK")
  2686. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2687. (push (cons (match-string 1 value)
  2688. (org-trim (match-string 2 value)))
  2689. links)))
  2690. ((equal key "PRIORITIES")
  2691. (setq prio (org-split-string value " +")))
  2692. ((equal key "PROPERTY")
  2693. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2694. (push (cons (match-string 1 value) (match-string 2 value))
  2695. props)))
  2696. ((equal key "FILETAGS")
  2697. (when (string-match "\\S-" value)
  2698. (setq ftags
  2699. (append
  2700. ftags
  2701. (apply 'append
  2702. (mapcar (lambda (x) (org-split-string x ":"))
  2703. (org-split-string value)))))))
  2704. ((equal key "DRAWERS")
  2705. (setq drawers (org-split-string value splitre)))
  2706. ((equal key "CONSTANTS")
  2707. (setq const (append const (org-split-string value splitre))))
  2708. ((equal key "STARTUP")
  2709. (let ((opts (org-split-string value splitre))
  2710. l var val)
  2711. (while (setq l (pop opts))
  2712. (when (setq l (assoc l org-startup-options))
  2713. (setq var (nth 1 l) val (nth 2 l))
  2714. (if (not (nth 3 l))
  2715. (set (make-local-variable var) val)
  2716. (if (not (listp (symbol-value var)))
  2717. (set (make-local-variable var) nil))
  2718. (set (make-local-variable var) (symbol-value var))
  2719. (add-to-list var val))))))
  2720. ((equal key "ARCHIVE")
  2721. (string-match " *$" value)
  2722. (setq arch (replace-match "" t t value))
  2723. (remove-text-properties 0 (length arch)
  2724. '(face t fontified t) arch))
  2725. ((equal key "SETUPFILE")
  2726. (setq setup-contents (org-file-contents
  2727. (expand-file-name
  2728. (org-remove-double-quotes value))
  2729. 'noerror))
  2730. (if (not ext-setup-or-nil)
  2731. (setq ext-setup-or-nil setup-contents start 0)
  2732. (setq ext-setup-or-nil
  2733. (concat (substring ext-setup-or-nil 0 start)
  2734. "\n" setup-contents "\n"
  2735. (substring ext-setup-or-nil start)))))
  2736. ))))
  2737. (when cat
  2738. (org-set-local 'org-category (intern cat))
  2739. (push (cons "CATEGORY" cat) props))
  2740. (when prio
  2741. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2742. (setq prio (mapcar 'string-to-char prio))
  2743. (org-set-local 'org-highest-priority (nth 0 prio))
  2744. (org-set-local 'org-lowest-priority (nth 1 prio))
  2745. (org-set-local 'org-default-priority (nth 2 prio)))
  2746. (and props (org-set-local 'org-file-properties (nreverse props)))
  2747. (and ftags (org-set-local 'org-file-tags ftags))
  2748. (and drawers (org-set-local 'org-drawers drawers))
  2749. (and arch (org-set-local 'org-archive-location arch))
  2750. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2751. ;; Process the TODO keywords
  2752. (unless kwds
  2753. ;; Use the global values as if they had been given locally.
  2754. (setq kwds (default-value 'org-todo-keywords))
  2755. (if (stringp (car kwds))
  2756. (setq kwds (list (cons org-todo-interpretation
  2757. (default-value 'org-todo-keywords)))))
  2758. (setq kwds (reverse kwds)))
  2759. (setq kwds (nreverse kwds))
  2760. (let (inter kws kw)
  2761. (while (setq kws (pop kwds))
  2762. (setq inter (pop kws) sep (member "|" kws)
  2763. kws0 (delete "|" (copy-sequence kws))
  2764. kwsa nil
  2765. kws1 (mapcar
  2766. (lambda (x)
  2767. ;; 1 2
  2768. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2769. (progn
  2770. (setq kw (match-string 1 x)
  2771. key (and (match-end 2) (match-string 2 x))
  2772. log (org-extract-log-state-settings x))
  2773. (push (cons kw (and key (string-to-char key))) kwsa)
  2774. (and log (push log org-todo-log-states))
  2775. kw)
  2776. (error "Invalid TODO keyword %s" x)))
  2777. kws0)
  2778. kwsa (if kwsa (append '((:startgroup))
  2779. (nreverse kwsa)
  2780. '((:endgroup))))
  2781. hw (car kws1)
  2782. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2783. tail (list inter hw (car dws) (org-last dws)))
  2784. (add-to-list 'org-todo-heads hw 'append)
  2785. (push kws1 org-todo-sets)
  2786. (setq org-done-keywords (append org-done-keywords dws nil))
  2787. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2788. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2789. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2790. (setq org-todo-sets (nreverse org-todo-sets)
  2791. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2792. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2793. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2794. ;; Process the constants
  2795. (when const
  2796. (let (e cst)
  2797. (while (setq e (pop const))
  2798. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2799. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2800. (setq org-table-formula-constants-local cst)))
  2801. ;; Process the tags.
  2802. (when tags
  2803. (let (e tgs)
  2804. (while (setq e (pop tags))
  2805. (cond
  2806. ((equal e "{") (push '(:startgroup) tgs))
  2807. ((equal e "}") (push '(:endgroup) tgs))
  2808. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2809. (push (cons (match-string 1 e)
  2810. (string-to-char (match-string 2 e)))
  2811. tgs))
  2812. (t (push (list e) tgs))))
  2813. (org-set-local 'org-tag-alist nil)
  2814. (while (setq e (pop tgs))
  2815. (or (and (stringp (car e))
  2816. (assoc (car e) org-tag-alist))
  2817. (push e org-tag-alist)))))
  2818. ;; Compute the regular expressions and other local variables
  2819. (if (not org-done-keywords)
  2820. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2821. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2822. (length org-scheduled-string)
  2823. (length org-clock-string)
  2824. (length org-closed-string)))
  2825. org-drawer-regexp
  2826. (concat "^[ \t]*:\\("
  2827. (mapconcat 'regexp-quote org-drawers "\\|")
  2828. "\\):[ \t]*$")
  2829. org-not-done-keywords
  2830. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2831. org-todo-regexp
  2832. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2833. "\\|") "\\)\\>")
  2834. org-not-done-regexp
  2835. (concat "\\<\\("
  2836. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2837. "\\)\\>")
  2838. org-todo-line-regexp
  2839. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2840. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2841. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2842. org-complex-heading-regexp
  2843. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2844. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2845. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2846. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2847. org-nl-done-regexp
  2848. (concat "\n\\*+[ \t]+"
  2849. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2850. "\\)" "\\>")
  2851. org-todo-line-tags-regexp
  2852. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2853. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2854. (org-re
  2855. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2856. org-looking-at-done-regexp
  2857. (concat "^" "\\(?:"
  2858. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2859. "\\>")
  2860. org-deadline-regexp (concat "\\<" org-deadline-string)
  2861. org-deadline-time-regexp
  2862. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2863. org-deadline-line-regexp
  2864. (concat "\\<\\(" org-deadline-string "\\).*")
  2865. org-scheduled-regexp
  2866. (concat "\\<" org-scheduled-string)
  2867. org-scheduled-time-regexp
  2868. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2869. org-closed-time-regexp
  2870. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2871. org-keyword-time-regexp
  2872. (concat "\\<\\(" org-scheduled-string
  2873. "\\|" org-deadline-string
  2874. "\\|" org-closed-string
  2875. "\\|" org-clock-string "\\)"
  2876. " *[[<]\\([^]>]+\\)[]>]")
  2877. org-keyword-time-not-clock-regexp
  2878. (concat "\\<\\(" org-scheduled-string
  2879. "\\|" org-deadline-string
  2880. "\\|" org-closed-string
  2881. "\\)"
  2882. " *[[<]\\([^]>]+\\)[]>]")
  2883. org-maybe-keyword-time-regexp
  2884. (concat "\\(\\<\\(" org-scheduled-string
  2885. "\\|" org-deadline-string
  2886. "\\|" org-closed-string
  2887. "\\|" org-clock-string "\\)\\)?"
  2888. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2889. org-planning-or-clock-line-re
  2890. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2891. "\\|" org-deadline-string
  2892. "\\|" org-closed-string "\\|" org-clock-string
  2893. "\\)\\>\\)")
  2894. )
  2895. (org-compute-latex-and-specials-regexp)
  2896. (org-set-font-lock-defaults))))
  2897. (defun org-file-contents (file &optional noerror)
  2898. "Return the contents of FILE, as a string."
  2899. (if (or (not file)
  2900. (not (file-readable-p file)))
  2901. (if noerror
  2902. (progn
  2903. (message "Cannot read file %s" file)
  2904. (ding) (sit-for 2)
  2905. "")
  2906. (error "Cannot read file %s" file))
  2907. (with-temp-buffer
  2908. (insert-file-contents file)
  2909. (buffer-string))))
  2910. (defun org-extract-log-state-settings (x)
  2911. "Extract the log state setting from a TODO keyword string.
  2912. This will extract info from a string like \"WAIT(w@/!)\"."
  2913. (let (kw key log1 log2)
  2914. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2915. (setq kw (match-string 1 x)
  2916. key (and (match-end 2) (match-string 2 x))
  2917. log1 (and (match-end 3) (match-string 3 x))
  2918. log2 (and (match-end 4) (match-string 4 x)))
  2919. (and (or log1 log2)
  2920. (list kw
  2921. (and log1 (if (equal log1 "!") 'time 'note))
  2922. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2923. (defun org-remove-keyword-keys (list)
  2924. "Remove a pair of parenthesis at the end of each string in LIST."
  2925. (mapcar (lambda (x)
  2926. (if (string-match "(.*)$" x)
  2927. (substring x 0 (match-beginning 0))
  2928. x))
  2929. list))
  2930. ;; FIXME: this could be done much better, using second characters etc.
  2931. (defun org-assign-fast-keys (alist)
  2932. "Assign fast keys to a keyword-key alist.
  2933. Respect keys that are already there."
  2934. (let (new e k c c1 c2 (char ?a))
  2935. (while (setq e (pop alist))
  2936. (cond
  2937. ((equal e '(:startgroup)) (push e new))
  2938. ((equal e '(:endgroup)) (push e new))
  2939. (t
  2940. (setq k (car e) c2 nil)
  2941. (if (cdr e)
  2942. (setq c (cdr e))
  2943. ;; automatically assign a character.
  2944. (setq c1 (string-to-char
  2945. (downcase (substring
  2946. k (if (= (string-to-char k) ?@) 1 0)))))
  2947. (if (or (rassoc c1 new) (rassoc c1 alist))
  2948. (while (or (rassoc char new) (rassoc char alist))
  2949. (setq char (1+ char)))
  2950. (setq c2 c1))
  2951. (setq c (or c2 char)))
  2952. (push (cons k c) new))))
  2953. (nreverse new)))
  2954. ;;; Some variables used in various places
  2955. (defvar org-window-configuration nil
  2956. "Used in various places to store a window configuration.")
  2957. (defvar org-finish-function nil
  2958. "Function to be called when `C-c C-c' is used.
  2959. This is for getting out of special buffers like remember.")
  2960. ;; FIXME: Occasionally check by commenting these, to make sure
  2961. ;; no other functions uses these, forgetting to let-bind them.
  2962. (defvar entry)
  2963. (defvar state)
  2964. (defvar last-state)
  2965. (defvar date)
  2966. (defvar description)
  2967. ;; Defined somewhere in this file, but used before definition.
  2968. (defvar org-html-entities)
  2969. (defvar org-struct-menu)
  2970. (defvar org-org-menu)
  2971. (defvar org-tbl-menu)
  2972. (defvar org-agenda-keymap)
  2973. ;;;; Define the Org-mode
  2974. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2975. (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."))
  2976. ;; We use a before-change function to check if a table might need
  2977. ;; an update.
  2978. (defvar org-table-may-need-update t
  2979. "Indicates that a table might need an update.
  2980. This variable is set by `org-before-change-function'.
  2981. `org-table-align' sets it back to nil.")
  2982. (defun org-before-change-function (beg end)
  2983. "Every change indicates that a table might need an update."
  2984. (setq org-table-may-need-update t))
  2985. (defvar org-mode-map)
  2986. (defvar org-mode-hook nil
  2987. "Mode hook for Org-mode, run after the mode was turned on.")
  2988. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2989. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2990. (defvar org-table-buffer-is-an nil)
  2991. (defconst org-outline-regexp "\\*+ ")
  2992. ;;;###autoload
  2993. (define-derived-mode org-mode outline-mode "Org"
  2994. "Outline-based notes management and organizer, alias
  2995. \"Carsten's outline-mode for keeping track of everything.\"
  2996. Org-mode develops organizational tasks around a NOTES file which
  2997. contains information about projects as plain text. Org-mode is
  2998. implemented on top of outline-mode, which is ideal to keep the content
  2999. of large files well structured. It supports ToDo items, deadlines and
  3000. time stamps, which magically appear in the diary listing of the Emacs
  3001. calendar. Tables are easily created with a built-in table editor.
  3002. Plain text URL-like links connect to websites, emails (VM), Usenet
  3003. messages (Gnus), BBDB entries, and any files related to the project.
  3004. For printing and sharing of notes, an Org-mode file (or a part of it)
  3005. can be exported as a structured ASCII or HTML file.
  3006. The following commands are available:
  3007. \\{org-mode-map}"
  3008. ;; Get rid of Outline menus, they are not needed
  3009. ;; Need to do this here because define-derived-mode sets up
  3010. ;; the keymap so late. Still, it is a waste to call this each time
  3011. ;; we switch another buffer into org-mode.
  3012. (if (featurep 'xemacs)
  3013. (when (boundp 'outline-mode-menu-heading)
  3014. ;; Assume this is Greg's port, it used easymenu
  3015. (easy-menu-remove outline-mode-menu-heading)
  3016. (easy-menu-remove outline-mode-menu-show)
  3017. (easy-menu-remove outline-mode-menu-hide))
  3018. (define-key org-mode-map [menu-bar headings] 'undefined)
  3019. (define-key org-mode-map [menu-bar hide] 'undefined)
  3020. (define-key org-mode-map [menu-bar show] 'undefined))
  3021. (org-load-modules-maybe)
  3022. (easy-menu-add org-org-menu)
  3023. (easy-menu-add org-tbl-menu)
  3024. (org-install-agenda-files-menu)
  3025. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3026. (org-add-to-invisibility-spec '(org-cwidth))
  3027. (when (featurep 'xemacs)
  3028. (org-set-local 'line-move-ignore-invisible t))
  3029. (org-set-local 'outline-regexp org-outline-regexp)
  3030. (org-set-local 'outline-level 'org-outline-level)
  3031. (when (and org-ellipsis
  3032. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3033. (fboundp 'make-glyph-code))
  3034. (unless org-display-table
  3035. (setq org-display-table (make-display-table)))
  3036. (set-display-table-slot
  3037. org-display-table 4
  3038. (vconcat (mapcar
  3039. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3040. org-ellipsis)))
  3041. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3042. (setq buffer-display-table org-display-table))
  3043. (org-set-regexps-and-options)
  3044. (when (and org-tag-faces (not org-tags-special-faces-re))
  3045. ;; tag faces set outside customize.... force initialization.
  3046. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3047. ;; Calc embedded
  3048. (org-set-local 'calc-embedded-open-mode "# ")
  3049. (modify-syntax-entry ?# "<")
  3050. (modify-syntax-entry ?@ "w")
  3051. (if org-startup-truncated (setq truncate-lines t))
  3052. (org-set-local 'font-lock-unfontify-region-function
  3053. 'org-unfontify-region)
  3054. ;; Activate before-change-function
  3055. (org-set-local 'org-table-may-need-update t)
  3056. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3057. 'local)
  3058. ;; Check for running clock before killing a buffer
  3059. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3060. ;; Paragraphs and auto-filling
  3061. (org-set-autofill-regexps)
  3062. (setq indent-line-function 'org-indent-line-function)
  3063. (org-update-radio-target-regexp)
  3064. ;; Comment characters
  3065. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3066. (org-set-local 'comment-padding " ")
  3067. ;; Align options lines
  3068. (org-set-local
  3069. 'align-mode-rules-list
  3070. '((org-in-buffer-settings
  3071. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3072. (modes . '(org-mode)))))
  3073. ;; Imenu
  3074. (org-set-local 'imenu-create-index-function
  3075. 'org-imenu-get-tree)
  3076. ;; Make isearch reveal context
  3077. (if (or (featurep 'xemacs)
  3078. (not (boundp 'outline-isearch-open-invisible-function)))
  3079. ;; Emacs 21 and XEmacs make use of the hook
  3080. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3081. ;; Emacs 22 deals with this through a special variable
  3082. (org-set-local 'outline-isearch-open-invisible-function
  3083. (lambda (&rest ignore) (org-show-context 'isearch))))
  3084. ;; If empty file that did not turn on org-mode automatically, make it to.
  3085. (if (and org-insert-mode-line-in-empty-file
  3086. (interactive-p)
  3087. (= (point-min) (point-max)))
  3088. (insert "# -*- mode: org -*-\n\n"))
  3089. (unless org-inhibit-startup
  3090. (when org-startup-align-all-tables
  3091. (let ((bmp (buffer-modified-p)))
  3092. (org-table-map-tables 'org-table-align)
  3093. (set-buffer-modified-p bmp)))
  3094. (org-set-startup-visibility)))
  3095. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3096. (defun org-current-time ()
  3097. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3098. (if (> (car org-time-stamp-rounding-minutes) 1)
  3099. (let ((r (car org-time-stamp-rounding-minutes))
  3100. (time (decode-time)))
  3101. (apply 'encode-time
  3102. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3103. (nthcdr 2 time))))
  3104. (current-time)))
  3105. ;;;; Font-Lock stuff, including the activators
  3106. (defvar org-mouse-map (make-sparse-keymap))
  3107. (org-defkey org-mouse-map
  3108. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3109. (org-defkey org-mouse-map
  3110. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3111. (when org-mouse-1-follows-link
  3112. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3113. (when org-tab-follows-link
  3114. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3115. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3116. (when org-return-follows-link
  3117. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3118. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3119. (require 'font-lock)
  3120. (defconst org-non-link-chars "]\t\n\r<>")
  3121. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3122. "shell" "elisp"))
  3123. (defvar org-link-types-re nil
  3124. "Matches a link that has a url-like prefix like \"http:\"")
  3125. (defvar org-link-re-with-space nil
  3126. "Matches a link with spaces, optional angular brackets around it.")
  3127. (defvar org-link-re-with-space2 nil
  3128. "Matches a link with spaces, optional angular brackets around it.")
  3129. (defvar org-link-re-with-space3 nil
  3130. "Matches a link with spaces, only for internal part in bracket links.")
  3131. (defvar org-angle-link-re nil
  3132. "Matches link with angular brackets, spaces are allowed.")
  3133. (defvar org-plain-link-re nil
  3134. "Matches plain link, without spaces.")
  3135. (defvar org-bracket-link-regexp nil
  3136. "Matches a link in double brackets.")
  3137. (defvar org-bracket-link-analytic-regexp nil
  3138. "Regular expression used to analyze links.
  3139. Here is what the match groups contain after a match:
  3140. 1: http:
  3141. 2: http
  3142. 3: path
  3143. 4: [desc]
  3144. 5: desc")
  3145. (defvar org-bracket-link-analytic-regexp++ nil
  3146. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3147. (defvar org-any-link-re nil
  3148. "Regular expression matching any link.")
  3149. (defun org-make-link-regexps ()
  3150. "Update the link regular expressions.
  3151. This should be called after the variable `org-link-types' has changed."
  3152. (setq org-link-types-re
  3153. (concat
  3154. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3155. org-link-re-with-space
  3156. (concat
  3157. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3158. "\\([^" org-non-link-chars " ]"
  3159. "[^" org-non-link-chars "]*"
  3160. "[^" org-non-link-chars " ]\\)>?")
  3161. org-link-re-with-space2
  3162. (concat
  3163. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3164. "\\([^" org-non-link-chars " ]"
  3165. "[^\t\n\r]*"
  3166. "[^" org-non-link-chars " ]\\)>?")
  3167. org-link-re-with-space3
  3168. (concat
  3169. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3170. "\\([^" org-non-link-chars " ]"
  3171. "[^\t\n\r]*\\)")
  3172. org-angle-link-re
  3173. (concat
  3174. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3175. "\\([^" org-non-link-chars " ]"
  3176. "[^" org-non-link-chars "]*"
  3177. "\\)>")
  3178. org-plain-link-re
  3179. (concat
  3180. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3181. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3182. org-bracket-link-regexp
  3183. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3184. org-bracket-link-analytic-regexp
  3185. (concat
  3186. "\\[\\["
  3187. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3188. "\\([^]]+\\)"
  3189. "\\]"
  3190. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3191. "\\]")
  3192. org-bracket-link-analytic-regexp++
  3193. (concat
  3194. "\\[\\["
  3195. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3196. "\\([^]]+\\)"
  3197. "\\]"
  3198. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3199. "\\]")
  3200. org-any-link-re
  3201. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3202. org-angle-link-re "\\)\\|\\("
  3203. org-plain-link-re "\\)")))
  3204. (org-make-link-regexps)
  3205. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3206. "Regular expression for fast time stamp matching.")
  3207. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3208. "Regular expression for fast time stamp matching.")
  3209. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3210. "Regular expression matching time strings for analysis.
  3211. This one does not require the space after the date, so it can be used
  3212. on a string that terminates immediately after the date.")
  3213. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3214. "Regular expression matching time strings for analysis.")
  3215. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3216. "Regular expression matching time stamps, with groups.")
  3217. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3218. "Regular expression matching time stamps (also [..]), with groups.")
  3219. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3220. "Regular expression matching a time stamp range.")
  3221. (defconst org-tr-regexp-both
  3222. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3223. "Regular expression matching a time stamp range.")
  3224. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3225. org-ts-regexp "\\)?")
  3226. "Regular expression matching a time stamp or time stamp range.")
  3227. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3228. org-ts-regexp-both "\\)?")
  3229. "Regular expression matching a time stamp or time stamp range.
  3230. The time stamps may be either active or inactive.")
  3231. (defvar org-emph-face nil)
  3232. (defun org-do-emphasis-faces (limit)
  3233. "Run through the buffer and add overlays to links."
  3234. (let (rtn)
  3235. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3236. (if (not (= (char-after (match-beginning 3))
  3237. (char-after (match-beginning 4))))
  3238. (progn
  3239. (setq rtn t)
  3240. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3241. 'face
  3242. (nth 1 (assoc (match-string 3)
  3243. org-emphasis-alist)))
  3244. (add-text-properties (match-beginning 2) (match-end 2)
  3245. '(font-lock-multiline t))
  3246. (when org-hide-emphasis-markers
  3247. (add-text-properties (match-end 4) (match-beginning 5)
  3248. '(invisible org-link))
  3249. (add-text-properties (match-beginning 3) (match-end 3)
  3250. '(invisible org-link)))))
  3251. (backward-char 1))
  3252. rtn))
  3253. (defun org-emphasize (&optional char)
  3254. "Insert or change an emphasis, i.e. a font like bold or italic.
  3255. If there is an active region, change that region to a new emphasis.
  3256. If there is no region, just insert the marker characters and position
  3257. the cursor between them.
  3258. CHAR should be either the marker character, or the first character of the
  3259. HTML tag associated with that emphasis. If CHAR is a space, the means
  3260. to remove the emphasis of the selected region.
  3261. If char is not given (for example in an interactive call) it
  3262. will be prompted for."
  3263. (interactive)
  3264. (let ((eal org-emphasis-alist) e det
  3265. (erc org-emphasis-regexp-components)
  3266. (prompt "")
  3267. (string "") beg end move tag c s)
  3268. (if (org-region-active-p)
  3269. (setq beg (region-beginning) end (region-end)
  3270. string (buffer-substring beg end))
  3271. (setq move t))
  3272. (while (setq e (pop eal))
  3273. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3274. c (aref tag 0))
  3275. (push (cons c (string-to-char (car e))) det)
  3276. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3277. (substring tag 1)))))
  3278. (setq det (nreverse det))
  3279. (unless char
  3280. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3281. (setq char (read-char-exclusive)))
  3282. (setq char (or (cdr (assoc char det)) char))
  3283. (if (equal char ?\ )
  3284. (setq s "" move nil)
  3285. (unless (assoc (char-to-string char) org-emphasis-alist)
  3286. (error "No such emphasis marker: \"%c\"" char))
  3287. (setq s (char-to-string char)))
  3288. (while (and (> (length string) 1)
  3289. (equal (substring string 0 1) (substring string -1))
  3290. (assoc (substring string 0 1) org-emphasis-alist))
  3291. (setq string (substring string 1 -1)))
  3292. (setq string (concat s string s))
  3293. (if beg (delete-region beg end))
  3294. (unless (or (bolp)
  3295. (string-match (concat "[" (nth 0 erc) "\n]")
  3296. (char-to-string (char-before (point)))))
  3297. (insert " "))
  3298. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3299. (char-to-string (char-after (point))))
  3300. (insert " ") (backward-char 1))
  3301. (insert string)
  3302. (and move (backward-char 1))))
  3303. (defconst org-nonsticky-props
  3304. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3305. (defun org-activate-plain-links (limit)
  3306. "Run through the buffer and add overlays to links."
  3307. (catch 'exit
  3308. (let (f)
  3309. (while (re-search-forward org-plain-link-re limit t)
  3310. (setq f (get-text-property (match-beginning 0) 'face))
  3311. (if (or (eq f 'org-tag)
  3312. (and (listp f) (memq 'org-tag f)))
  3313. nil
  3314. (add-text-properties (match-beginning 0) (match-end 0)
  3315. (list 'mouse-face 'highlight
  3316. 'rear-nonsticky org-nonsticky-props
  3317. 'keymap org-mouse-map
  3318. ))
  3319. (throw 'exit t))))))
  3320. (defun org-activate-code (limit)
  3321. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3322. (progn
  3323. (remove-text-properties (match-beginning 0) (match-end 0)
  3324. '(display t invisible t intangible t))
  3325. t)))
  3326. (defun org-activate-angle-links (limit)
  3327. "Run through the buffer and add overlays to links."
  3328. (if (re-search-forward org-angle-link-re limit t)
  3329. (progn
  3330. (add-text-properties (match-beginning 0) (match-end 0)
  3331. (list 'mouse-face 'highlight
  3332. 'rear-nonsticky org-nonsticky-props
  3333. 'keymap org-mouse-map
  3334. ))
  3335. t)))
  3336. (defun org-activate-footnote-links (limit)
  3337. "Run through the buffer and add overlays to links."
  3338. (if (re-search-forward "\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)"
  3339. limit t)
  3340. (progn
  3341. (add-text-properties (match-beginning 0) (match-end 0)
  3342. (list 'mouse-face 'highlight
  3343. 'rear-nonsticky org-nonsticky-props
  3344. 'keymap org-mouse-map
  3345. 'help-echo
  3346. (if (= (point-at-bol) (match-beginning 0))
  3347. "Footnote definition"
  3348. "Footnote reference")
  3349. ))
  3350. t)))
  3351. (defun org-activate-bracket-links (limit)
  3352. "Run through the buffer and add overlays to bracketed links."
  3353. (if (re-search-forward org-bracket-link-regexp limit t)
  3354. (let* ((help (concat "LINK: "
  3355. (org-match-string-no-properties 1)))
  3356. ;; FIXME: above we should remove the escapes.
  3357. ;; but that requires another match, protecting match data,
  3358. ;; a lot of overhead for font-lock.
  3359. (ip (org-maybe-intangible
  3360. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3361. 'keymap org-mouse-map 'mouse-face 'highlight
  3362. 'font-lock-multiline t 'help-echo help)))
  3363. (vp (list 'rear-nonsticky org-nonsticky-props
  3364. 'keymap org-mouse-map 'mouse-face 'highlight
  3365. ' font-lock-multiline t 'help-echo help)))
  3366. ;; We need to remove the invisible property here. Table narrowing
  3367. ;; may have made some of this invisible.
  3368. (remove-text-properties (match-beginning 0) (match-end 0)
  3369. '(invisible nil))
  3370. (if (match-end 3)
  3371. (progn
  3372. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3373. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3374. (add-text-properties (match-end 3) (match-end 0) ip))
  3375. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3376. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3377. (add-text-properties (match-end 1) (match-end 0) ip))
  3378. t)))
  3379. (defun org-activate-dates (limit)
  3380. "Run through the buffer and add overlays to dates."
  3381. (if (re-search-forward org-tsr-regexp-both limit t)
  3382. (progn
  3383. (add-text-properties (match-beginning 0) (match-end 0)
  3384. (list 'mouse-face 'highlight
  3385. 'rear-nonsticky org-nonsticky-props
  3386. 'keymap org-mouse-map))
  3387. (when org-display-custom-times
  3388. (if (match-end 3)
  3389. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3390. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3391. t)))
  3392. (defvar org-target-link-regexp nil
  3393. "Regular expression matching radio targets in plain text.")
  3394. (make-variable-buffer-local 'org-target-link-regexp)
  3395. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3396. "Regular expression matching a link target.")
  3397. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3398. "Regular expression matching a radio target.")
  3399. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3400. "Regular expression matching any target.")
  3401. (defun org-activate-target-links (limit)
  3402. "Run through the buffer and add overlays to target matches."
  3403. (when org-target-link-regexp
  3404. (let ((case-fold-search t))
  3405. (if (re-search-forward org-target-link-regexp limit t)
  3406. (progn
  3407. (add-text-properties (match-beginning 0) (match-end 0)
  3408. (list 'mouse-face 'highlight
  3409. 'rear-nonsticky org-nonsticky-props
  3410. 'keymap org-mouse-map
  3411. 'help-echo "Radio target link"
  3412. 'org-linked-text t))
  3413. t)))))
  3414. (defun org-update-radio-target-regexp ()
  3415. "Find all radio targets in this file and update the regular expression."
  3416. (interactive)
  3417. (when (memq 'radio org-activate-links)
  3418. (setq org-target-link-regexp
  3419. (org-make-target-link-regexp (org-all-targets 'radio)))
  3420. (org-restart-font-lock)))
  3421. (defun org-hide-wide-columns (limit)
  3422. (let (s e)
  3423. (setq s (text-property-any (point) (or limit (point-max))
  3424. 'org-cwidth t))
  3425. (when s
  3426. (setq e (next-single-property-change s 'org-cwidth))
  3427. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3428. (goto-char e)
  3429. t)))
  3430. (defvar org-latex-and-specials-regexp nil
  3431. "Regular expression for highlighting export special stuff.")
  3432. (defvar org-match-substring-regexp)
  3433. (defvar org-match-substring-with-braces-regexp)
  3434. (defvar org-export-html-special-string-regexps)
  3435. (defun org-compute-latex-and-specials-regexp ()
  3436. "Compute regular expression for stuff treated specially by exporters."
  3437. (if (not org-highlight-latex-fragments-and-specials)
  3438. (org-set-local 'org-latex-and-specials-regexp nil)
  3439. (require 'org-exp)
  3440. (let*
  3441. ((matchers (plist-get org-format-latex-options :matchers))
  3442. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3443. org-latex-regexps)))
  3444. (options (org-combine-plists (org-default-export-plist)
  3445. (org-infile-export-plist)))
  3446. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3447. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3448. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3449. (org-export-html-expand (plist-get options :expand-quoted-html))
  3450. (org-export-with-special-strings (plist-get options :special-strings))
  3451. (re-sub
  3452. (cond
  3453. ((equal org-export-with-sub-superscripts '{})
  3454. (list org-match-substring-with-braces-regexp))
  3455. (org-export-with-sub-superscripts
  3456. (list org-match-substring-regexp))
  3457. (t nil)))
  3458. (re-latex
  3459. (if org-export-with-LaTeX-fragments
  3460. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3461. (re-macros
  3462. (if org-export-with-TeX-macros
  3463. (list (concat "\\\\"
  3464. (regexp-opt
  3465. (append (mapcar 'car org-html-entities)
  3466. (if (boundp 'org-latex-entities)
  3467. org-latex-entities nil))
  3468. 'words))) ; FIXME
  3469. ))
  3470. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3471. (re-special (if org-export-with-special-strings
  3472. (mapcar (lambda (x) (car x))
  3473. org-export-html-special-string-regexps)))
  3474. (re-rest
  3475. (delq nil
  3476. (list
  3477. (if org-export-html-expand "@<[^>\n]+>")
  3478. ))))
  3479. (org-set-local
  3480. 'org-latex-and-specials-regexp
  3481. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3482. re-rest) "\\|")))))
  3483. (defun org-do-latex-and-special-faces (limit)
  3484. "Run through the buffer and add overlays to links."
  3485. (when org-latex-and-specials-regexp
  3486. (let (rtn d)
  3487. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3488. limit t))
  3489. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3490. 'face))
  3491. '(org-code org-verbatim underline)))
  3492. (progn
  3493. (setq rtn t
  3494. d (cond ((member (char-after (1+ (match-beginning 0)))
  3495. '(?_ ?^)) 1)
  3496. (t 0)))
  3497. (font-lock-prepend-text-property
  3498. (+ d (match-beginning 0)) (match-end 0)
  3499. 'face 'org-latex-and-export-specials)
  3500. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3501. '(font-lock-multiline t)))))
  3502. rtn)))
  3503. (defun org-restart-font-lock ()
  3504. "Restart font-lock-mode, to force refontification."
  3505. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3506. (font-lock-mode -1)
  3507. (font-lock-mode 1)))
  3508. (defun org-all-targets (&optional radio)
  3509. "Return a list of all targets in this file.
  3510. With optional argument RADIO, only find radio targets."
  3511. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3512. rtn)
  3513. (save-excursion
  3514. (goto-char (point-min))
  3515. (while (re-search-forward re nil t)
  3516. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3517. rtn)))
  3518. (defun org-make-target-link-regexp (targets)
  3519. "Make regular expression matching all strings in TARGETS.
  3520. The regular expression finds the targets also if there is a line break
  3521. between words."
  3522. (and targets
  3523. (concat
  3524. "\\<\\("
  3525. (mapconcat
  3526. (lambda (x)
  3527. (while (string-match " +" x)
  3528. (setq x (replace-match "\\s-+" t t x)))
  3529. x)
  3530. targets
  3531. "\\|")
  3532. "\\)\\>")))
  3533. (defun org-activate-tags (limit)
  3534. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3535. (progn
  3536. (add-text-properties (match-beginning 1) (match-end 1)
  3537. (list 'mouse-face 'highlight
  3538. 'rear-nonsticky org-nonsticky-props
  3539. 'keymap org-mouse-map))
  3540. t)))
  3541. (defun org-outline-level ()
  3542. (save-excursion
  3543. (looking-at outline-regexp)
  3544. (if (match-beginning 1)
  3545. (+ (org-get-string-indentation (match-string 1)) 1000)
  3546. (1- (- (match-end 0) (match-beginning 0))))))
  3547. (defvar org-font-lock-keywords nil)
  3548. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3549. "Regular expression matching a property line.")
  3550. (defvar org-font-lock-hook nil
  3551. "Functions to be called for special font lock stuff.")
  3552. (defun org-font-lock-hook (limit)
  3553. (run-hook-with-args 'org-font-lock-hook limit))
  3554. (defun org-set-font-lock-defaults ()
  3555. (let* ((em org-fontify-emphasized-text)
  3556. (lk org-activate-links)
  3557. (org-font-lock-extra-keywords
  3558. (list
  3559. ;; Call the hook
  3560. '(org-font-lock-hook)
  3561. ;; Headlines
  3562. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3563. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3564. ;; Table lines
  3565. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3566. (1 'org-table t))
  3567. ;; Table internals
  3568. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3569. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3570. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3571. ;; Drawers
  3572. (list org-drawer-regexp '(0 'org-special-keyword t))
  3573. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3574. ;; Properties
  3575. (list org-property-re
  3576. '(1 'org-special-keyword t)
  3577. '(3 'org-property-value t))
  3578. (if org-format-transports-properties-p
  3579. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3580. ;; Links
  3581. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3582. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3583. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3584. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3585. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3586. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3587. (if (memq 'footnote lk) '(org-activate-footnote-links
  3588. (0 'org-footnote t)))
  3589. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3590. '(org-hide-wide-columns (0 nil append))
  3591. ;; TODO lines
  3592. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3593. '(1 (org-get-todo-face 1) t))
  3594. ;; DONE
  3595. (if org-fontify-done-headline
  3596. (list (concat "^[*]+ +\\<\\("
  3597. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3598. "\\)\\(.*\\)")
  3599. '(2 'org-headline-done t))
  3600. nil)
  3601. ;; Priorities
  3602. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3603. ;; Tags
  3604. '(org-font-lock-add-tag-faces)
  3605. ;; Special keywords
  3606. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3607. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3608. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3609. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3610. ;; Emphasis
  3611. (if em
  3612. (if (featurep 'xemacs)
  3613. '(org-do-emphasis-faces (0 nil append))
  3614. '(org-do-emphasis-faces)))
  3615. ;; Checkboxes
  3616. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3617. 2 'bold prepend)
  3618. (if org-provide-checkbox-statistics
  3619. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3620. (0 (org-get-checkbox-statistics-face) t)))
  3621. ;; Description list items
  3622. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3623. 2 'bold prepend)
  3624. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3625. '(1 'org-archived prepend))
  3626. ;; Specials
  3627. '(org-do-latex-and-special-faces)
  3628. ;; Code
  3629. '(org-activate-code (1 'org-code t))
  3630. ;; COMMENT
  3631. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3632. "\\|" org-quote-string "\\)\\>")
  3633. '(1 'org-special-keyword t))
  3634. '("^#.*" (0 'font-lock-comment-face t))
  3635. )))
  3636. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3637. ;; Now set the full font-lock-keywords
  3638. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3639. (org-set-local 'font-lock-defaults
  3640. '(org-font-lock-keywords t nil nil backward-paragraph))
  3641. (kill-local-variable 'font-lock-keywords) nil))
  3642. (defvar org-m nil)
  3643. (defvar org-l nil)
  3644. (defvar org-f nil)
  3645. (defun org-get-level-face (n)
  3646. "Get the right face for match N in font-lock matching of headlines."
  3647. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3648. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3649. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3650. (cond
  3651. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3652. ((eq n 2) org-f)
  3653. (t (if org-level-color-stars-only nil org-f))))
  3654. (defun org-get-todo-face (kwd)
  3655. "Get the right face for a TODO keyword KWD.
  3656. If KWD is a number, get the corresponding match group."
  3657. (if (numberp kwd) (setq kwd (match-string kwd)))
  3658. (or (cdr (assoc kwd org-todo-keyword-faces))
  3659. (and (member kwd org-done-keywords) 'org-done)
  3660. 'org-todo))
  3661. (defun org-font-lock-add-tag-faces (limit)
  3662. "Add the special tag faces."
  3663. (when (and org-tag-faces org-tags-special-faces-re)
  3664. (while (re-search-forward org-tags-special-faces-re limit t)
  3665. (add-text-properties (match-beginning 1) (match-end 1)
  3666. (list 'face (org-get-tag-face 1)
  3667. 'font-lock-fontified t))
  3668. (backward-char 1))))
  3669. (defun org-get-tag-face (kwd)
  3670. "Get the right face for a TODO keyword KWD.
  3671. If KWD is a number, get the corresponding match group."
  3672. (if (numberp kwd) (setq kwd (match-string kwd)))
  3673. (or (cdr (assoc kwd org-tag-faces))
  3674. 'org-tag))
  3675. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3676. "Remove fontification and activation overlays from links."
  3677. (font-lock-default-unfontify-region beg end)
  3678. (let* ((buffer-undo-list t)
  3679. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3680. (inhibit-modification-hooks t)
  3681. deactivate-mark buffer-file-name buffer-file-truename)
  3682. (remove-text-properties beg end
  3683. '(mouse-face t keymap t org-linked-text t
  3684. invisible t intangible t))))
  3685. ;;;; Visibility cycling, including org-goto and indirect buffer
  3686. ;;; Cycling
  3687. (defvar org-cycle-global-status nil)
  3688. (make-variable-buffer-local 'org-cycle-global-status)
  3689. (defvar org-cycle-subtree-status nil)
  3690. (make-variable-buffer-local 'org-cycle-subtree-status)
  3691. ;;;###autoload
  3692. (defun org-cycle (&optional arg)
  3693. "Visibility cycling for Org-mode.
  3694. - When this function is called with a prefix argument, rotate the entire
  3695. buffer through 3 states (global cycling)
  3696. 1. OVERVIEW: Show only top-level headlines.
  3697. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3698. 3. SHOW ALL: Show everything.
  3699. When called with two C-u C-u prefixes, switch to the startup visibility,
  3700. determined by the variable `org-startup-folded', and by any VISIBILITY
  3701. properties in the buffer.
  3702. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3703. including drawers.
  3704. - When point is at the beginning of a headline, rotate the subtree started
  3705. by this line through 3 different states (local cycling)
  3706. 1. FOLDED: Only the main headline is shown.
  3707. 2. CHILDREN: The main headline and the direct children are shown.
  3708. From this state, you can move to one of the children
  3709. and zoom in further.
  3710. 3. SUBTREE: Show the entire subtree, including body text.
  3711. - When there is a numeric prefix, go up to a heading with level ARG, do
  3712. a `show-subtree' and return to the previous cursor position. If ARG
  3713. is negative, go up that many levels.
  3714. - When point is not at the beginning of a headline, execute the global
  3715. binding for TAB, which is re-indenting the line. See the option
  3716. `org-cycle-emulate-tab' for details.
  3717. - Special case: if point is at the beginning of the buffer and there is
  3718. no headline in line 1, this function will act as if called with prefix arg.
  3719. But only if also the variable `org-cycle-global-at-bob' is t."
  3720. (interactive "P")
  3721. (org-load-modules-maybe)
  3722. (let* ((outline-regexp
  3723. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3724. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3725. outline-regexp))
  3726. (bob-special (and org-cycle-global-at-bob (bobp)
  3727. (not (looking-at outline-regexp))))
  3728. (org-cycle-hook
  3729. (if bob-special
  3730. (delq 'org-optimize-window-after-visibility-change
  3731. (copy-sequence org-cycle-hook))
  3732. org-cycle-hook))
  3733. (pos (point)))
  3734. (if (or bob-special (equal arg '(4)))
  3735. ;; special case: use global cycling
  3736. (setq arg t))
  3737. (cond
  3738. ((equal arg '(16))
  3739. (org-set-startup-visibility)
  3740. (message "Startup visibility, plus VISIBILITY properties"))
  3741. ((equal arg '(64))
  3742. (show-all)
  3743. (message "Entire buffer visible, including drawers"))
  3744. ((org-at-table-p 'any)
  3745. ;; Enter the table or move to the next field in the table
  3746. (or (org-table-recognize-table.el)
  3747. (progn
  3748. (if arg (org-table-edit-field t)
  3749. (org-table-justify-field-maybe)
  3750. (call-interactively 'org-table-next-field)))))
  3751. ((eq arg t) ;; Global cycling
  3752. (cond
  3753. ((and (eq last-command this-command)
  3754. (eq org-cycle-global-status 'overview))
  3755. ;; We just created the overview - now do table of contents
  3756. ;; This can be slow in very large buffers, so indicate action
  3757. (message "CONTENTS...")
  3758. (org-content)
  3759. (message "CONTENTS...done")
  3760. (setq org-cycle-global-status 'contents)
  3761. (run-hook-with-args 'org-cycle-hook 'contents))
  3762. ((and (eq last-command this-command)
  3763. (eq org-cycle-global-status 'contents))
  3764. ;; We just showed the table of contents - now show everything
  3765. (show-all)
  3766. (message "SHOW ALL")
  3767. (setq org-cycle-global-status 'all)
  3768. (run-hook-with-args 'org-cycle-hook 'all))
  3769. (t
  3770. ;; Default action: go to overview
  3771. (org-overview)
  3772. (message "OVERVIEW")
  3773. (setq org-cycle-global-status 'overview)
  3774. (run-hook-with-args 'org-cycle-hook 'overview))))
  3775. ((and org-drawers org-drawer-regexp
  3776. (save-excursion
  3777. (beginning-of-line 1)
  3778. (looking-at org-drawer-regexp)))
  3779. ;; Toggle block visibility
  3780. (org-flag-drawer
  3781. (not (get-char-property (match-end 0) 'invisible))))
  3782. ((integerp arg)
  3783. ;; Show-subtree, ARG levels up from here.
  3784. (save-excursion
  3785. (org-back-to-heading)
  3786. (outline-up-heading (if (< arg 0) (- arg)
  3787. (- (funcall outline-level) arg)))
  3788. (org-show-subtree)))
  3789. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3790. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3791. ;; At a heading: rotate between three different views
  3792. (org-back-to-heading)
  3793. (let ((goal-column 0) eoh eol eos)
  3794. ;; First, some boundaries
  3795. (save-excursion
  3796. (org-back-to-heading)
  3797. (save-excursion
  3798. (beginning-of-line 2)
  3799. (while (and (not (eobp)) ;; this is like `next-line'
  3800. (get-char-property (1- (point)) 'invisible))
  3801. (beginning-of-line 2)) (setq eol (point)))
  3802. (outline-end-of-heading) (setq eoh (point))
  3803. (org-end-of-subtree t)
  3804. (unless (eobp)
  3805. (skip-chars-forward " \t\n")
  3806. (beginning-of-line 1) ; in case this is an item
  3807. )
  3808. (setq eos (1- (point))))
  3809. ;; Find out what to do next and set `this-command'
  3810. (cond
  3811. ((= eos eoh)
  3812. ;; Nothing is hidden behind this heading
  3813. (message "EMPTY ENTRY")
  3814. (setq org-cycle-subtree-status nil)
  3815. (save-excursion
  3816. (goto-char eos)
  3817. (outline-next-heading)
  3818. (if (org-invisible-p) (org-flag-heading nil))))
  3819. ((or (>= eol eos)
  3820. (not (string-match "\\S-" (buffer-substring eol eos))))
  3821. ;; Entire subtree is hidden in one line: open it
  3822. (org-show-entry)
  3823. (show-children)
  3824. (message "CHILDREN")
  3825. (save-excursion
  3826. (goto-char eos)
  3827. (outline-next-heading)
  3828. (if (org-invisible-p) (org-flag-heading nil)))
  3829. (setq org-cycle-subtree-status 'children)
  3830. (run-hook-with-args 'org-cycle-hook 'children))
  3831. ((and (eq last-command this-command)
  3832. (eq org-cycle-subtree-status 'children))
  3833. ;; We just showed the children, now show everything.
  3834. (org-show-subtree)
  3835. (message "SUBTREE")
  3836. (setq org-cycle-subtree-status 'subtree)
  3837. (run-hook-with-args 'org-cycle-hook 'subtree))
  3838. (t
  3839. ;; Default action: hide the subtree.
  3840. (hide-subtree)
  3841. (message "FOLDED")
  3842. (setq org-cycle-subtree-status 'folded)
  3843. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3844. ;; TAB emulation and template completion
  3845. (buffer-read-only (org-back-to-heading))
  3846. ((org-try-structure-completion))
  3847. ((org-try-cdlatex-tab))
  3848. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3849. (or (not (bolp))
  3850. (not (looking-at outline-regexp))))
  3851. (call-interactively (global-key-binding "\t")))
  3852. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3853. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3854. (or (and (eq org-cycle-emulate-tab 'white)
  3855. (= (match-end 0) (point-at-eol)))
  3856. (and (eq org-cycle-emulate-tab 'whitestart)
  3857. (>= (match-end 0) pos))))
  3858. t
  3859. (eq org-cycle-emulate-tab t))
  3860. (call-interactively (global-key-binding "\t")))
  3861. (t (save-excursion
  3862. (org-back-to-heading)
  3863. (org-cycle))))))
  3864. ;;;###autoload
  3865. (defun org-global-cycle (&optional arg)
  3866. "Cycle the global visibility. For details see `org-cycle'.
  3867. With C-u prefix arg, switch to startup visibility.
  3868. With a numeric prefix, show all headlines up to that level."
  3869. (interactive "P")
  3870. (let ((org-cycle-include-plain-lists
  3871. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3872. (cond
  3873. ((integerp arg)
  3874. (show-all)
  3875. (hide-sublevels arg)
  3876. (setq org-cycle-global-status 'contents))
  3877. ((equal arg '(4))
  3878. (org-set-startup-visibility)
  3879. (message "Startup visibility, plus VISIBILITY properties."))
  3880. (t
  3881. (org-cycle '(4))))))
  3882. (defun org-set-startup-visibility ()
  3883. "Set the visibility required by startup options and properties."
  3884. (cond
  3885. ((eq org-startup-folded t)
  3886. (org-cycle '(4)))
  3887. ((eq org-startup-folded 'content)
  3888. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3889. (org-cycle '(4)) (org-cycle '(4)))))
  3890. (org-set-visibility-according-to-property 'no-cleanup)
  3891. (org-cycle-hide-archived-subtrees 'all)
  3892. (org-cycle-hide-drawers 'all)
  3893. (org-cycle-show-empty-lines 'all))
  3894. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3895. "Switch subtree visibilities according to :VISIBILITY: property."
  3896. (interactive)
  3897. (let (state)
  3898. (save-excursion
  3899. (goto-char (point-min))
  3900. (while (re-search-forward
  3901. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3902. nil t)
  3903. (setq state (match-string 1))
  3904. (save-excursion
  3905. (org-back-to-heading t)
  3906. (hide-subtree)
  3907. (org-reveal)
  3908. (cond
  3909. ((equal state '("fold" "folded"))
  3910. (hide-subtree))
  3911. ((equal state "children")
  3912. (org-show-hidden-entry)
  3913. (show-children))
  3914. ((equal state "content")
  3915. (save-excursion
  3916. (save-restriction
  3917. (org-narrow-to-subtree)
  3918. (org-content))))
  3919. ((member state '("all" "showall"))
  3920. (show-subtree)))))
  3921. (unless no-cleanup
  3922. (org-cycle-hide-archived-subtrees 'all)
  3923. (org-cycle-hide-drawers 'all)
  3924. (org-cycle-show-empty-lines 'all)))))
  3925. (defun org-overview ()
  3926. "Switch to overview mode, showing only top-level headlines.
  3927. Really, this shows all headlines with level equal or greater than the level
  3928. of the first headline in the buffer. This is important, because if the
  3929. first headline is not level one, then (hide-sublevels 1) gives confusing
  3930. results."
  3931. (interactive)
  3932. (let ((level (save-excursion
  3933. (goto-char (point-min))
  3934. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3935. (progn
  3936. (goto-char (match-beginning 0))
  3937. (funcall outline-level))))))
  3938. (and level (hide-sublevels level))))
  3939. (defun org-content (&optional arg)
  3940. "Show all headlines in the buffer, like a table of contents.
  3941. With numerical argument N, show content up to level N."
  3942. (interactive "P")
  3943. (save-excursion
  3944. ;; Visit all headings and show their offspring
  3945. (and (integerp arg) (org-overview))
  3946. (goto-char (point-max))
  3947. (catch 'exit
  3948. (while (and (progn (condition-case nil
  3949. (outline-previous-visible-heading 1)
  3950. (error (goto-char (point-min))))
  3951. t)
  3952. (looking-at outline-regexp))
  3953. (if (integerp arg)
  3954. (show-children (1- arg))
  3955. (show-branches))
  3956. (if (bobp) (throw 'exit nil))))))
  3957. (defun org-optimize-window-after-visibility-change (state)
  3958. "Adjust the window after a change in outline visibility.
  3959. This function is the default value of the hook `org-cycle-hook'."
  3960. (when (get-buffer-window (current-buffer))
  3961. (cond
  3962. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3963. ; ((eq state 'overview) (org-beginning-of-line))
  3964. ((eq state 'content) nil)
  3965. ((eq state 'all) nil)
  3966. ((eq state 'folded) nil)
  3967. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3968. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3969. (defun org-compact-display-after-subtree-move ()
  3970. (let (beg end)
  3971. (save-excursion
  3972. (if (org-up-heading-safe)
  3973. (progn
  3974. (hide-subtree)
  3975. (show-entry)
  3976. (show-children)
  3977. (org-cycle-show-empty-lines 'children)
  3978. (org-cycle-hide-drawers 'children))
  3979. (org-overview)))))
  3980. (defun org-cycle-show-empty-lines (state)
  3981. "Show empty lines above all visible headlines.
  3982. The region to be covered depends on STATE when called through
  3983. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3984. entire buffer covered. Note that an empty line is only shown if there
  3985. are at least `org-cycle-separator-lines' empty lines before the headline."
  3986. (when (> org-cycle-separator-lines 0)
  3987. (save-excursion
  3988. (let* ((n org-cycle-separator-lines)
  3989. (re (cond
  3990. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3991. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3992. (t (let ((ns (number-to-string (- n 2))))
  3993. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3994. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3995. beg end)
  3996. (cond
  3997. ((memq state '(overview contents t))
  3998. (setq beg (point-min) end (point-max)))
  3999. ((memq state '(children folded))
  4000. (setq beg (point) end (progn (org-end-of-subtree t t)
  4001. (beginning-of-line 2)
  4002. (point)))))
  4003. (when beg
  4004. (goto-char beg)
  4005. (while (re-search-forward re end t)
  4006. (if (not (get-char-property (match-end 1) 'invisible))
  4007. (outline-flag-region
  4008. (match-beginning 1) (match-end 1) nil)))))))
  4009. ;; Never hide empty lines at the end of the file.
  4010. (save-excursion
  4011. (goto-char (point-max))
  4012. (outline-previous-heading)
  4013. (outline-end-of-heading)
  4014. (if (and (looking-at "[ \t\n]+")
  4015. (= (match-end 0) (point-max)))
  4016. (outline-flag-region (point) (match-end 0) nil))))
  4017. (defun org-show-empty-lines-in-parent ()
  4018. "Move to the parent and re-show empty lines before visible headlines."
  4019. (save-excursion
  4020. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4021. (org-cycle-show-empty-lines context))))
  4022. (defun org-cycle-hide-drawers (state)
  4023. "Re-hide all drawers after a visibility state change."
  4024. (when (and (org-mode-p)
  4025. (not (memq state '(overview folded))))
  4026. (save-excursion
  4027. (let* ((globalp (memq state '(contents all)))
  4028. (beg (if globalp (point-min) (point)))
  4029. (end (if globalp (point-max) (org-end-of-subtree t))))
  4030. (goto-char beg)
  4031. (while (re-search-forward org-drawer-regexp end t)
  4032. (org-flag-drawer t))))))
  4033. (defun org-flag-drawer (flag)
  4034. (save-excursion
  4035. (beginning-of-line 1)
  4036. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4037. (let ((b (match-end 0))
  4038. (outline-regexp org-outline-regexp))
  4039. (if (re-search-forward
  4040. "^[ \t]*:END:"
  4041. (save-excursion (outline-next-heading) (point)) t)
  4042. (outline-flag-region b (point-at-eol) flag)
  4043. (error ":END: line missing"))))))
  4044. (defun org-subtree-end-visible-p ()
  4045. "Is the end of the current subtree visible?"
  4046. (pos-visible-in-window-p
  4047. (save-excursion (org-end-of-subtree t) (point))))
  4048. (defun org-first-headline-recenter (&optional N)
  4049. "Move cursor to the first headline and recenter the headline.
  4050. Optional argument N means, put the headline into the Nth line of the window."
  4051. (goto-char (point-min))
  4052. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4053. (beginning-of-line)
  4054. (recenter (prefix-numeric-value N))))
  4055. ;;; Org-goto
  4056. (defvar org-goto-window-configuration nil)
  4057. (defvar org-goto-marker nil)
  4058. (defvar org-goto-map
  4059. (let ((map (make-sparse-keymap)))
  4060. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4061. (while (setq cmd (pop cmds))
  4062. (substitute-key-definition cmd cmd map global-map)))
  4063. (suppress-keymap map)
  4064. (org-defkey map "\C-m" 'org-goto-ret)
  4065. (org-defkey map [(return)] 'org-goto-ret)
  4066. (org-defkey map [(left)] 'org-goto-left)
  4067. (org-defkey map [(right)] 'org-goto-right)
  4068. (org-defkey map [(control ?g)] 'org-goto-quit)
  4069. (org-defkey map "\C-i" 'org-cycle)
  4070. (org-defkey map [(tab)] 'org-cycle)
  4071. (org-defkey map [(down)] 'outline-next-visible-heading)
  4072. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4073. (if org-goto-auto-isearch
  4074. (if (fboundp 'define-key-after)
  4075. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4076. nil)
  4077. (org-defkey map "q" 'org-goto-quit)
  4078. (org-defkey map "n" 'outline-next-visible-heading)
  4079. (org-defkey map "p" 'outline-previous-visible-heading)
  4080. (org-defkey map "f" 'outline-forward-same-level)
  4081. (org-defkey map "b" 'outline-backward-same-level)
  4082. (org-defkey map "u" 'outline-up-heading))
  4083. (org-defkey map "/" 'org-occur)
  4084. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4085. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4086. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4087. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4088. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4089. map))
  4090. (defconst org-goto-help
  4091. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4092. RET=jump to location [Q]uit and return to previous location
  4093. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4094. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4095. ;; FIXME: Docstring doe not mention both interfaces
  4096. (defun org-goto (&optional alternative-interface)
  4097. "Look up a different location in the current file, keeping current visibility.
  4098. When you want look-up or go to a different location in a document, the
  4099. fastest way is often to fold the entire buffer and then dive into the tree.
  4100. This method has the disadvantage, that the previous location will be folded,
  4101. which may not be what you want.
  4102. This command works around this by showing a copy of the current buffer
  4103. in an indirect buffer, in overview mode. You can dive into the tree in
  4104. that copy, use org-occur and incremental search to find a location.
  4105. When pressing RET or `Q', the command returns to the original buffer in
  4106. which the visibility is still unchanged. After RET is will also jump to
  4107. the location selected in the indirect buffer and expose the
  4108. the headline hierarchy above."
  4109. (interactive "P")
  4110. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4111. (org-refile-use-outline-path t)
  4112. (interface
  4113. (if (not alternative-interface)
  4114. org-goto-interface
  4115. (if (eq org-goto-interface 'outline)
  4116. 'outline-path-completion
  4117. 'outline)))
  4118. (org-goto-start-pos (point))
  4119. (selected-point
  4120. (if (eq interface 'outline)
  4121. (car (org-get-location (current-buffer) org-goto-help))
  4122. (nth 3 (org-refile-get-location "Goto: ")))))
  4123. (if selected-point
  4124. (progn
  4125. (org-mark-ring-push org-goto-start-pos)
  4126. (goto-char selected-point)
  4127. (if (or (org-invisible-p) (org-invisible-p2))
  4128. (org-show-context 'org-goto)))
  4129. (message "Quit"))))
  4130. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4131. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4132. (defvar org-goto-local-auto-isearch-map) ; defined below
  4133. (defun org-get-location (buf help)
  4134. "Let the user select a location in the Org-mode buffer BUF.
  4135. This function uses a recursive edit. It returns the selected position
  4136. or nil."
  4137. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4138. (isearch-hide-immediately nil)
  4139. (isearch-search-fun-function
  4140. (lambda () 'org-goto-local-search-headings))
  4141. (org-goto-selected-point org-goto-exit-command))
  4142. (save-excursion
  4143. (save-window-excursion
  4144. (delete-other-windows)
  4145. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4146. (switch-to-buffer
  4147. (condition-case nil
  4148. (make-indirect-buffer (current-buffer) "*org-goto*")
  4149. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4150. (with-output-to-temp-buffer "*Help*"
  4151. (princ help))
  4152. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4153. (setq buffer-read-only nil)
  4154. (let ((org-startup-truncated t)
  4155. (org-startup-folded nil)
  4156. (org-startup-align-all-tables nil))
  4157. (org-mode)
  4158. (org-overview))
  4159. (setq buffer-read-only t)
  4160. (if (and (boundp 'org-goto-start-pos)
  4161. (integer-or-marker-p org-goto-start-pos))
  4162. (let ((org-show-hierarchy-above t)
  4163. (org-show-siblings t)
  4164. (org-show-following-heading t))
  4165. (goto-char org-goto-start-pos)
  4166. (and (org-invisible-p) (org-show-context)))
  4167. (goto-char (point-min)))
  4168. (org-beginning-of-line)
  4169. (message "Select location and press RET")
  4170. (use-local-map org-goto-map)
  4171. (recursive-edit)
  4172. ))
  4173. (kill-buffer "*org-goto*")
  4174. (cons org-goto-selected-point org-goto-exit-command)))
  4175. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4176. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4177. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4178. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4179. (defun org-goto-local-search-headings (string bound noerror)
  4180. "Search and make sure that any matches are in headlines."
  4181. (catch 'return
  4182. (while (if isearch-forward
  4183. (search-forward string bound noerror)
  4184. (search-backward string bound noerror))
  4185. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4186. (and (member :headline context)
  4187. (not (member :tags context))))
  4188. (throw 'return (point))))))
  4189. (defun org-goto-local-auto-isearch ()
  4190. "Start isearch."
  4191. (interactive)
  4192. (goto-char (point-min))
  4193. (let ((keys (this-command-keys)))
  4194. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4195. (isearch-mode t)
  4196. (isearch-process-search-char (string-to-char keys)))))
  4197. (defun org-goto-ret (&optional arg)
  4198. "Finish `org-goto' by going to the new location."
  4199. (interactive "P")
  4200. (setq org-goto-selected-point (point)
  4201. org-goto-exit-command 'return)
  4202. (throw 'exit nil))
  4203. (defun org-goto-left ()
  4204. "Finish `org-goto' by going to the new location."
  4205. (interactive)
  4206. (if (org-on-heading-p)
  4207. (progn
  4208. (beginning-of-line 1)
  4209. (setq org-goto-selected-point (point)
  4210. org-goto-exit-command 'left)
  4211. (throw 'exit nil))
  4212. (error "Not on a heading")))
  4213. (defun org-goto-right ()
  4214. "Finish `org-goto' by going to the new location."
  4215. (interactive)
  4216. (if (org-on-heading-p)
  4217. (progn
  4218. (setq org-goto-selected-point (point)
  4219. org-goto-exit-command 'right)
  4220. (throw 'exit nil))
  4221. (error "Not on a heading")))
  4222. (defun org-goto-quit ()
  4223. "Finish `org-goto' without cursor motion."
  4224. (interactive)
  4225. (setq org-goto-selected-point nil)
  4226. (setq org-goto-exit-command 'quit)
  4227. (throw 'exit nil))
  4228. ;;; Indirect buffer display of subtrees
  4229. (defvar org-indirect-dedicated-frame nil
  4230. "This is the frame being used for indirect tree display.")
  4231. (defvar org-last-indirect-buffer nil)
  4232. (defun org-tree-to-indirect-buffer (&optional arg)
  4233. "Create indirect buffer and narrow it to current subtree.
  4234. With numerical prefix ARG, go up to this level and then take that tree.
  4235. If ARG is negative, go up that many levels.
  4236. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4237. indirect buffer previously made with this command, to avoid proliferation of
  4238. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4239. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4240. is kept so that you can work with several indirect buffers at the same time.
  4241. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4242. requests that a new frame be made for the new buffer, so that the dedicated
  4243. frame is not changed."
  4244. (interactive "P")
  4245. (let ((cbuf (current-buffer))
  4246. (cwin (selected-window))
  4247. (pos (point))
  4248. beg end level heading ibuf)
  4249. (save-excursion
  4250. (org-back-to-heading t)
  4251. (when (numberp arg)
  4252. (setq level (org-outline-level))
  4253. (if (< arg 0) (setq arg (+ level arg)))
  4254. (while (> (setq level (org-outline-level)) arg)
  4255. (outline-up-heading 1 t)))
  4256. (setq beg (point)
  4257. heading (org-get-heading))
  4258. (org-end-of-subtree t) (setq end (point)))
  4259. (if (and (buffer-live-p org-last-indirect-buffer)
  4260. (not (eq org-indirect-buffer-display 'new-frame))
  4261. (not arg))
  4262. (kill-buffer org-last-indirect-buffer))
  4263. (setq ibuf (org-get-indirect-buffer cbuf)
  4264. org-last-indirect-buffer ibuf)
  4265. (cond
  4266. ((or (eq org-indirect-buffer-display 'new-frame)
  4267. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4268. (select-frame (make-frame))
  4269. (delete-other-windows)
  4270. (switch-to-buffer ibuf)
  4271. (org-set-frame-title heading))
  4272. ((eq org-indirect-buffer-display 'dedicated-frame)
  4273. (raise-frame
  4274. (select-frame (or (and org-indirect-dedicated-frame
  4275. (frame-live-p org-indirect-dedicated-frame)
  4276. org-indirect-dedicated-frame)
  4277. (setq org-indirect-dedicated-frame (make-frame)))))
  4278. (delete-other-windows)
  4279. (switch-to-buffer ibuf)
  4280. (org-set-frame-title (concat "Indirect: " heading)))
  4281. ((eq org-indirect-buffer-display 'current-window)
  4282. (switch-to-buffer ibuf))
  4283. ((eq org-indirect-buffer-display 'other-window)
  4284. (pop-to-buffer ibuf))
  4285. (t (error "Invalid value.")))
  4286. (if (featurep 'xemacs)
  4287. (save-excursion (org-mode) (turn-on-font-lock)))
  4288. (narrow-to-region beg end)
  4289. (show-all)
  4290. (goto-char pos)
  4291. (and (window-live-p cwin) (select-window cwin))))
  4292. (defun org-get-indirect-buffer (&optional buffer)
  4293. (setq buffer (or buffer (current-buffer)))
  4294. (let ((n 1) (base (buffer-name buffer)) bname)
  4295. (while (buffer-live-p
  4296. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4297. (setq n (1+ n)))
  4298. (condition-case nil
  4299. (make-indirect-buffer buffer bname 'clone)
  4300. (error (make-indirect-buffer buffer bname)))))
  4301. (defun org-set-frame-title (title)
  4302. "Set the title of the current frame to the string TITLE."
  4303. ;; FIXME: how to name a single frame in XEmacs???
  4304. (unless (featurep 'xemacs)
  4305. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4306. ;;;; Structure editing
  4307. ;;; Inserting headlines
  4308. (defun org-insert-heading (&optional force-heading)
  4309. "Insert a new heading or item with same depth at point.
  4310. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4311. If point is at the beginning of a headline, insert a sibling before the
  4312. current headline. If point is not at the beginning, do not split the line,
  4313. but create the new headline after the current line."
  4314. (interactive "P")
  4315. (if (= (buffer-size) 0)
  4316. (insert "\n* ")
  4317. (when (or force-heading (not (org-insert-item)))
  4318. (let* ((head (save-excursion
  4319. (condition-case nil
  4320. (progn
  4321. (org-back-to-heading)
  4322. (match-string 0))
  4323. (error "*"))))
  4324. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4325. pos hide-previous previous-pos)
  4326. (cond
  4327. ((and (org-on-heading-p) (bolp)
  4328. (or (bobp)
  4329. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4330. ;; insert before the current line
  4331. (open-line (if blank 2 1)))
  4332. ((and (bolp)
  4333. (or (bobp)
  4334. (save-excursion
  4335. (backward-char 1) (not (org-invisible-p)))))
  4336. ;; insert right here
  4337. nil)
  4338. (t
  4339. ;; somewhere in the line
  4340. (save-excursion
  4341. (setq previous-pos (point-at-bol))
  4342. (end-of-line)
  4343. (setq hide-previous (org-invisible-p)))
  4344. (and org-insert-heading-respect-content (org-show-subtree))
  4345. (let ((split
  4346. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4347. (save-excursion
  4348. (let ((p (point)))
  4349. (goto-char (point-at-bol))
  4350. (and (looking-at org-complex-heading-regexp)
  4351. (> p (match-beginning 4)))))))
  4352. tags pos)
  4353. (cond
  4354. (org-insert-heading-respect-content
  4355. (org-end-of-subtree nil t)
  4356. (or (bolp) (newline))
  4357. (open-line 1))
  4358. ((org-on-heading-p)
  4359. (when hide-previous
  4360. (show-children)
  4361. (org-show-entry))
  4362. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4363. (setq tags (and (match-end 2) (match-string 2)))
  4364. (and (match-end 1)
  4365. (delete-region (match-beginning 1) (match-end 1)))
  4366. (setq pos (point-at-bol))
  4367. (or split (end-of-line 1))
  4368. (delete-horizontal-space)
  4369. (newline (if blank 2 1))
  4370. (when tags
  4371. (save-excursion
  4372. (goto-char pos)
  4373. (end-of-line 1)
  4374. (insert " " tags)
  4375. (org-set-tags nil 'align))))
  4376. (t
  4377. (or split (end-of-line 1))
  4378. (newline (if blank 2 1)))))))
  4379. (insert head) (just-one-space)
  4380. (setq pos (point))
  4381. (end-of-line 1)
  4382. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4383. (when (and org-insert-heading-respect-content hide-previous)
  4384. (save-excursion
  4385. (goto-char previous-pos)
  4386. (hide-subtree)))
  4387. (run-hooks 'org-insert-heading-hook)))))
  4388. (defun org-get-heading (&optional no-tags)
  4389. "Return the heading of the current entry, without the stars."
  4390. (save-excursion
  4391. (org-back-to-heading t)
  4392. (if (looking-at
  4393. (if no-tags
  4394. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4395. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4396. (match-string 1) "")))
  4397. (defun org-insert-heading-after-current ()
  4398. "Insert a new heading with same level as current, after current subtree."
  4399. (interactive)
  4400. (org-back-to-heading)
  4401. (org-insert-heading)
  4402. (org-move-subtree-down)
  4403. (end-of-line 1))
  4404. (defun org-insert-heading-respect-content ()
  4405. (interactive)
  4406. (let ((org-insert-heading-respect-content t))
  4407. (org-insert-heading t)))
  4408. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4409. (interactive "P")
  4410. (let ((org-insert-heading-respect-content t))
  4411. (org-insert-todo-heading force-state t)))
  4412. (defun org-insert-todo-heading (arg &optional force-heading)
  4413. "Insert a new heading with the same level and TODO state as current heading.
  4414. If the heading has no TODO state, or if the state is DONE, use the first
  4415. state (TODO by default). Also with prefix arg, force first state."
  4416. (interactive "P")
  4417. (when (or force-heading (not (org-insert-item 'checkbox)))
  4418. (org-insert-heading force-heading)
  4419. (save-excursion
  4420. (org-back-to-heading)
  4421. (outline-previous-heading)
  4422. (looking-at org-todo-line-regexp))
  4423. (if (or arg
  4424. (not (match-beginning 2))
  4425. (member (match-string 2) org-done-keywords))
  4426. (insert (car org-todo-keywords-1) " ")
  4427. (insert (match-string 2) " "))
  4428. (when org-provide-todo-statistics
  4429. (org-update-parent-todo-statistics))))
  4430. (defun org-insert-subheading (arg)
  4431. "Insert a new subheading and demote it.
  4432. Works for outline headings and for plain lists alike."
  4433. (interactive "P")
  4434. (org-insert-heading arg)
  4435. (cond
  4436. ((org-on-heading-p) (org-do-demote))
  4437. ((org-at-item-p) (org-indent-item 1))))
  4438. (defun org-insert-todo-subheading (arg)
  4439. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4440. Works for outline headings and for plain lists alike."
  4441. (interactive "P")
  4442. (org-insert-todo-heading arg)
  4443. (cond
  4444. ((org-on-heading-p) (org-do-demote))
  4445. ((org-at-item-p) (org-indent-item 1))))
  4446. ;;; Promotion and Demotion
  4447. (defun org-promote-subtree ()
  4448. "Promote the entire subtree.
  4449. See also `org-promote'."
  4450. (interactive)
  4451. (save-excursion
  4452. (org-map-tree 'org-promote))
  4453. (org-fix-position-after-promote))
  4454. (defun org-demote-subtree ()
  4455. "Demote the entire subtree. See `org-demote'.
  4456. See also `org-promote'."
  4457. (interactive)
  4458. (save-excursion
  4459. (org-map-tree 'org-demote))
  4460. (org-fix-position-after-promote))
  4461. (defun org-do-promote ()
  4462. "Promote the current heading higher up the tree.
  4463. If the region is active in `transient-mark-mode', promote all headings
  4464. in the region."
  4465. (interactive)
  4466. (save-excursion
  4467. (if (org-region-active-p)
  4468. (org-map-region 'org-promote (region-beginning) (region-end))
  4469. (org-promote)))
  4470. (org-fix-position-after-promote))
  4471. (defun org-do-demote ()
  4472. "Demote the current heading lower down the tree.
  4473. If the region is active in `transient-mark-mode', demote all headings
  4474. in the region."
  4475. (interactive)
  4476. (save-excursion
  4477. (if (org-region-active-p)
  4478. (org-map-region 'org-demote (region-beginning) (region-end))
  4479. (org-demote)))
  4480. (org-fix-position-after-promote))
  4481. (defun org-fix-position-after-promote ()
  4482. "Make sure that after pro/demotion cursor position is right."
  4483. (let ((pos (point)))
  4484. (when (save-excursion
  4485. (beginning-of-line 1)
  4486. (looking-at org-todo-line-regexp)
  4487. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4488. (cond ((eobp) (insert " "))
  4489. ((eolp) (insert " "))
  4490. ((equal (char-after) ?\ ) (forward-char 1))))))
  4491. (defun org-reduced-level (l)
  4492. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4493. (defun org-get-valid-level (level &optional change)
  4494. "Rectify a level change under the influence of `org-odd-levels-only'
  4495. LEVEL is a current level, CHANGE is by how much the level should be
  4496. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4497. even level numbers will become the next higher odd number."
  4498. (if org-odd-levels-only
  4499. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4500. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4501. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4502. (max 1 (+ level change))))
  4503. (if (boundp 'define-obsolete-function-alias)
  4504. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4505. (define-obsolete-function-alias 'org-get-legal-level
  4506. 'org-get-valid-level)
  4507. (define-obsolete-function-alias 'org-get-legal-level
  4508. 'org-get-valid-level "23.1")))
  4509. (defun org-promote ()
  4510. "Promote the current heading higher up the tree.
  4511. If the region is active in `transient-mark-mode', promote all headings
  4512. in the region."
  4513. (org-back-to-heading t)
  4514. (let* ((level (save-match-data (funcall outline-level)))
  4515. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4516. (diff (abs (- level (length up-head) -1))))
  4517. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4518. (replace-match up-head nil t)
  4519. ;; Fixup tag positioning
  4520. (and org-auto-align-tags (org-set-tags nil t))
  4521. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4522. (defun org-demote ()
  4523. "Demote the current heading lower down the tree.
  4524. If the region is active in `transient-mark-mode', demote all headings
  4525. in the region."
  4526. (org-back-to-heading t)
  4527. (let* ((level (save-match-data (funcall outline-level)))
  4528. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4529. (diff (abs (- level (length down-head) -1))))
  4530. (replace-match down-head nil t)
  4531. ;; Fixup tag positioning
  4532. (and org-auto-align-tags (org-set-tags nil t))
  4533. (if org-adapt-indentation (org-fixup-indentation diff))))
  4534. (defun org-map-tree (fun)
  4535. "Call FUN for every heading underneath the current one."
  4536. (org-back-to-heading)
  4537. (let ((level (funcall outline-level)))
  4538. (save-excursion
  4539. (funcall fun)
  4540. (while (and (progn
  4541. (outline-next-heading)
  4542. (> (funcall outline-level) level))
  4543. (not (eobp)))
  4544. (funcall fun)))))
  4545. (defun org-map-region (fun beg end)
  4546. "Call FUN for every heading between BEG and END."
  4547. (let ((org-ignore-region t))
  4548. (save-excursion
  4549. (setq end (copy-marker end))
  4550. (goto-char beg)
  4551. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4552. (< (point) end))
  4553. (funcall fun))
  4554. (while (and (progn
  4555. (outline-next-heading)
  4556. (< (point) end))
  4557. (not (eobp)))
  4558. (funcall fun)))))
  4559. (defun org-fixup-indentation (diff)
  4560. "Change the indentation in the current entry by DIFF
  4561. However, if any line in the current entry has no indentation, or if it
  4562. would end up with no indentation after the change, nothing at all is done."
  4563. (save-excursion
  4564. (let ((end (save-excursion (outline-next-heading)
  4565. (point-marker)))
  4566. (prohibit (if (> diff 0)
  4567. "^\\S-"
  4568. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4569. col)
  4570. (unless (save-excursion (end-of-line 1)
  4571. (re-search-forward prohibit end t))
  4572. (while (and (< (point) end)
  4573. (re-search-forward "^[ \t]+" end t))
  4574. (goto-char (match-end 0))
  4575. (setq col (current-column))
  4576. (if (< diff 0) (replace-match ""))
  4577. (org-indent-to-column (+ diff col))))
  4578. (move-marker end nil))))
  4579. (defun org-convert-to-odd-levels ()
  4580. "Convert an org-mode file with all levels allowed to one with odd levels.
  4581. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4582. level 5 etc."
  4583. (interactive)
  4584. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  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 (- (length (match-string 0)) 2))
  4590. (while (>= (setq n (1- n)) 0)
  4591. (org-demote))
  4592. (end-of-line 1))))))
  4593. (defun org-convert-to-oddeven-levels ()
  4594. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4595. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4596. section with an even level, conversion would destroy the structure of the file. An error
  4597. is signaled in this case."
  4598. (interactive)
  4599. (goto-char (point-min))
  4600. ;; First check if there are no even levels
  4601. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4602. (org-show-context t)
  4603. (error "Not all levels are odd in this file. Conversion not possible."))
  4604. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4605. (let ((org-odd-levels-only nil) n)
  4606. (save-excursion
  4607. (goto-char (point-min))
  4608. (while (re-search-forward "^\\*\\*+ " nil t)
  4609. (setq n (/ (1- (length (match-string 0))) 2))
  4610. (while (>= (setq n (1- n)) 0)
  4611. (org-promote))
  4612. (end-of-line 1))))))
  4613. (defun org-tr-level (n)
  4614. "Make N odd if required."
  4615. (if org-odd-levels-only (1+ (/ n 2)) n))
  4616. ;;; Vertical tree motion, cutting and pasting of subtrees
  4617. (defun org-move-subtree-up (&optional arg)
  4618. "Move the current subtree up past ARG headlines of the same level."
  4619. (interactive "p")
  4620. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4621. (defun org-move-subtree-down (&optional arg)
  4622. "Move the current subtree down past ARG headlines of the same level."
  4623. (interactive "p")
  4624. (setq arg (prefix-numeric-value arg))
  4625. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4626. 'outline-get-last-sibling))
  4627. (ins-point (make-marker))
  4628. (cnt (abs arg))
  4629. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4630. ;; Select the tree
  4631. (org-back-to-heading)
  4632. (setq beg0 (point))
  4633. (save-excursion
  4634. (setq ne-beg (org-back-over-empty-lines))
  4635. (setq beg (point)))
  4636. (save-match-data
  4637. (save-excursion (outline-end-of-heading)
  4638. (setq folded (org-invisible-p)))
  4639. (outline-end-of-subtree))
  4640. (outline-next-heading)
  4641. (setq ne-end (org-back-over-empty-lines))
  4642. (setq end (point))
  4643. (goto-char beg0)
  4644. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4645. ;; include less whitespace
  4646. (save-excursion
  4647. (goto-char beg)
  4648. (forward-line (- ne-beg ne-end))
  4649. (setq beg (point))))
  4650. ;; Find insertion point, with error handling
  4651. (while (> cnt 0)
  4652. (or (and (funcall movfunc) (looking-at outline-regexp))
  4653. (progn (goto-char beg0)
  4654. (error "Cannot move past superior level or buffer limit")))
  4655. (setq cnt (1- cnt)))
  4656. (if (> arg 0)
  4657. ;; Moving forward - still need to move over subtree
  4658. (progn (org-end-of-subtree t t)
  4659. (save-excursion
  4660. (org-back-over-empty-lines)
  4661. (or (bolp) (newline)))))
  4662. (setq ne-ins (org-back-over-empty-lines))
  4663. (move-marker ins-point (point))
  4664. (setq txt (buffer-substring beg end))
  4665. (org-save-markers-in-region beg end)
  4666. (delete-region beg end)
  4667. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4668. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4669. (let ((bbb (point)))
  4670. (insert-before-markers txt)
  4671. (org-reinstall-markers-in-region bbb)
  4672. (move-marker ins-point bbb))
  4673. (or (bolp) (insert "\n"))
  4674. (setq ins-end (point))
  4675. (goto-char ins-point)
  4676. (org-skip-whitespace)
  4677. (when (and (< arg 0)
  4678. (org-first-sibling-p)
  4679. (> ne-ins ne-beg))
  4680. ;; Move whitespace back to beginning
  4681. (save-excursion
  4682. (goto-char ins-end)
  4683. (let ((kill-whole-line t))
  4684. (kill-line (- ne-ins ne-beg)) (point)))
  4685. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4686. (move-marker ins-point nil)
  4687. (org-compact-display-after-subtree-move)
  4688. (org-show-empty-lines-in-parent)
  4689. (unless folded
  4690. (org-show-entry)
  4691. (show-children)
  4692. (org-cycle-hide-drawers 'children))))
  4693. (defvar org-subtree-clip ""
  4694. "Clipboard for cut and paste of subtrees.
  4695. This is actually only a copy of the kill, because we use the normal kill
  4696. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4697. (defvar org-subtree-clip-folded nil
  4698. "Was the last copied subtree folded?
  4699. This is used to fold the tree back after pasting.")
  4700. (defun org-cut-subtree (&optional n)
  4701. "Cut the current subtree into the clipboard.
  4702. With prefix arg N, cut this many sequential subtrees.
  4703. This is a short-hand for marking the subtree and then cutting it."
  4704. (interactive "p")
  4705. (org-copy-subtree n 'cut))
  4706. (defun org-copy-subtree (&optional n cut force-store-markers)
  4707. "Cut the current subtree into the clipboard.
  4708. With prefix arg N, cut this many sequential subtrees.
  4709. This is a short-hand for marking the subtree and then copying it.
  4710. If CUT is non-nil, actually cut the subtree.
  4711. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4712. of some markers in the region, even if CUT is non-nil. This is
  4713. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4714. (interactive "p")
  4715. (let (beg end folded (beg0 (point)))
  4716. (if (interactive-p)
  4717. (org-back-to-heading nil) ; take what looks like a subtree
  4718. (org-back-to-heading t)) ; take what is really there
  4719. (org-back-over-empty-lines)
  4720. (setq beg (point))
  4721. (skip-chars-forward " \t\r\n")
  4722. (save-match-data
  4723. (save-excursion (outline-end-of-heading)
  4724. (setq folded (org-invisible-p)))
  4725. (condition-case nil
  4726. (outline-forward-same-level (1- n))
  4727. (error nil))
  4728. (org-end-of-subtree t t))
  4729. (org-back-over-empty-lines)
  4730. (setq end (point))
  4731. (goto-char beg0)
  4732. (when (> end beg)
  4733. (setq org-subtree-clip-folded folded)
  4734. (when (or cut force-store-markers)
  4735. (org-save-markers-in-region beg end))
  4736. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4737. (setq org-subtree-clip (current-kill 0))
  4738. (message "%s: Subtree(s) with %d characters"
  4739. (if cut "Cut" "Copied")
  4740. (length org-subtree-clip)))))
  4741. (defun org-paste-subtree (&optional level tree for-yank)
  4742. "Paste the clipboard as a subtree, with modification of headline level.
  4743. The entire subtree is promoted or demoted in order to match a new headline
  4744. level.
  4745. If the cursor is at the beginning of a headline, the same level as
  4746. that headline is used to paste the tree
  4747. If not, the new level is derived from the *visible* headings
  4748. before and after the insertion point, and taken to be the inferior headline
  4749. level of the two. So if the previous visible heading is level 3 and the
  4750. next is level 4 (or vice versa), level 4 will be used for insertion.
  4751. This makes sure that the subtree remains an independent subtree and does
  4752. not swallow low level entries.
  4753. You can also force a different level, either by using a numeric prefix
  4754. argument, or by inserting the heading marker by hand. For example, if the
  4755. cursor is after \"*****\", then the tree will be shifted to level 5.
  4756. If optional TREE is given, use this text instead of the kill ring.
  4757. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4758. move back over whitespace before inserting, and move point to the end of
  4759. the inserted text when done."
  4760. (interactive "P")
  4761. (unless (org-kill-is-subtree-p tree)
  4762. (error "%s"
  4763. (substitute-command-keys
  4764. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4765. (let* ((visp (not (org-invisible-p)))
  4766. (txt (or tree (and kill-ring (current-kill 0))))
  4767. (^re (concat "^\\(" outline-regexp "\\)"))
  4768. (re (concat "\\(" outline-regexp "\\)"))
  4769. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4770. (old-level (if (string-match ^re txt)
  4771. (- (match-end 0) (match-beginning 0) 1)
  4772. -1))
  4773. (force-level (cond (level (prefix-numeric-value level))
  4774. ((and (looking-at "[ \t]*$")
  4775. (string-match
  4776. ^re_ (buffer-substring
  4777. (point-at-bol) (point))))
  4778. (- (match-end 1) (match-beginning 1)))
  4779. ((and (bolp)
  4780. (looking-at org-outline-regexp))
  4781. (- (match-end 0) (point) 1))
  4782. (t nil)))
  4783. (previous-level (save-excursion
  4784. (condition-case nil
  4785. (progn
  4786. (outline-previous-visible-heading 1)
  4787. (if (looking-at re)
  4788. (- (match-end 0) (match-beginning 0) 1)
  4789. 1))
  4790. (error 1))))
  4791. (next-level (save-excursion
  4792. (condition-case nil
  4793. (progn
  4794. (or (looking-at outline-regexp)
  4795. (outline-next-visible-heading 1))
  4796. (if (looking-at re)
  4797. (- (match-end 0) (match-beginning 0) 1)
  4798. 1))
  4799. (error 1))))
  4800. (new-level (or force-level (max previous-level next-level)))
  4801. (shift (if (or (= old-level -1)
  4802. (= new-level -1)
  4803. (= old-level new-level))
  4804. 0
  4805. (- new-level old-level)))
  4806. (delta (if (> shift 0) -1 1))
  4807. (func (if (> shift 0) 'org-demote 'org-promote))
  4808. (org-odd-levels-only nil)
  4809. beg end newend)
  4810. ;; Remove the forced level indicator
  4811. (if force-level
  4812. (delete-region (point-at-bol) (point)))
  4813. ;; Paste
  4814. (beginning-of-line 1)
  4815. (unless for-yank (org-back-over-empty-lines))
  4816. (setq beg (point))
  4817. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  4818. (insert-before-markers txt)
  4819. (unless (string-match "\n\\'" txt) (insert "\n"))
  4820. (setq newend (point))
  4821. (org-reinstall-markers-in-region beg)
  4822. (setq end (point))
  4823. (goto-char beg)
  4824. (skip-chars-forward " \t\n\r")
  4825. (setq beg (point))
  4826. (if (and (org-invisible-p) visp)
  4827. (save-excursion (outline-show-heading)))
  4828. ;; Shift if necessary
  4829. (unless (= shift 0)
  4830. (save-restriction
  4831. (narrow-to-region beg end)
  4832. (while (not (= shift 0))
  4833. (org-map-region func (point-min) (point-max))
  4834. (setq shift (+ delta shift)))
  4835. (goto-char (point-min))
  4836. (setq newend (point-max))))
  4837. (when (or (interactive-p) for-yank)
  4838. (message "Clipboard pasted as level %d subtree" new-level))
  4839. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4840. kill-ring
  4841. (eq org-subtree-clip (current-kill 0))
  4842. org-subtree-clip-folded)
  4843. ;; The tree was folded before it was killed/copied
  4844. (hide-subtree))
  4845. (and for-yank (goto-char newend))))
  4846. (defun org-kill-is-subtree-p (&optional txt)
  4847. "Check if the current kill is an outline subtree, or a set of trees.
  4848. Returns nil if kill does not start with a headline, or if the first
  4849. headline level is not the largest headline level in the tree.
  4850. So this will actually accept several entries of equal levels as well,
  4851. which is OK for `org-paste-subtree'.
  4852. If optional TXT is given, check this string instead of the current kill."
  4853. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4854. (start-level (and kill
  4855. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4856. org-outline-regexp "\\)")
  4857. kill)
  4858. (- (match-end 2) (match-beginning 2) 1)))
  4859. (re (concat "^" org-outline-regexp))
  4860. (start (1+ (or (match-beginning 2) -1))))
  4861. (if (not start-level)
  4862. (progn
  4863. nil) ;; does not even start with a heading
  4864. (catch 'exit
  4865. (while (setq start (string-match re kill (1+ start)))
  4866. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4867. (throw 'exit nil)))
  4868. t))))
  4869. (defvar org-markers-to-move nil
  4870. "Markers that should be moved with a cut-and-paste operation.
  4871. Those markers are stored together with their positions relative to
  4872. the start of the region.")
  4873. (defun org-save-markers-in-region (beg end)
  4874. "Check markers in region.
  4875. If these markers are between BEG and END, record their position relative
  4876. to BEG, so that after moving the block of text, we can put the markers back
  4877. into place.
  4878. This function gets called just before an entry or tree gets cut from the
  4879. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4880. called immediately, to move the markers with the entries."
  4881. (setq org-markers-to-move nil)
  4882. (when (featurep 'org-clock)
  4883. (org-clock-save-markers-for-cut-and-paste beg end))
  4884. (when (featurep 'org-agenda)
  4885. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4886. (defun org-check-and-save-marker (marker beg end)
  4887. "Check if MARKER is between BEG and END.
  4888. If yes, remember the marker and the distance to BEG."
  4889. (when (and (marker-buffer marker)
  4890. (equal (marker-buffer marker) (current-buffer)))
  4891. (if (and (>= marker beg) (< marker end))
  4892. (push (cons marker (- marker beg)) org-markers-to-move))))
  4893. (defun org-reinstall-markers-in-region (beg)
  4894. "Move all remembered markers to their position relative to BEG."
  4895. (mapc (lambda (x)
  4896. (move-marker (car x) (+ beg (cdr x))))
  4897. org-markers-to-move)
  4898. (setq org-markers-to-move nil))
  4899. (defun org-narrow-to-subtree ()
  4900. "Narrow buffer to the current subtree."
  4901. (interactive)
  4902. (save-excursion
  4903. (save-match-data
  4904. (narrow-to-region
  4905. (progn (org-back-to-heading) (point))
  4906. (progn (org-end-of-subtree t) (point))))))
  4907. ;;; Outline Sorting
  4908. (defun org-sort (with-case)
  4909. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4910. Optional argument WITH-CASE means sort case-sensitively."
  4911. (interactive "P")
  4912. (if (org-at-table-p)
  4913. (org-call-with-arg 'org-table-sort-lines with-case)
  4914. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4915. (defun org-sort-remove-invisible (s)
  4916. (remove-text-properties 0 (length s) org-rm-props s)
  4917. (while (string-match org-bracket-link-regexp s)
  4918. (setq s (replace-match (if (match-end 2)
  4919. (match-string 3 s)
  4920. (match-string 1 s)) t t s)))
  4921. s)
  4922. (defvar org-priority-regexp) ; defined later in the file
  4923. (defun org-sort-entries-or-items
  4924. (&optional with-case sorting-type getkey-func compare-func property)
  4925. "Sort entries on a certain level of an outline tree.
  4926. If there is an active region, the entries in the region are sorted.
  4927. Else, if the cursor is before the first entry, sort the top-level items.
  4928. Else, the children of the entry at point are sorted.
  4929. Sorting can be alphabetically, numerically, and by date/time as given by
  4930. the first time stamp in the entry. The command prompts for the sorting
  4931. type unless it has been given to the function through the SORTING-TYPE
  4932. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4933. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4934. called with point at the beginning of the record. It must return either
  4935. a string or a number that should serve as the sorting key for that record.
  4936. Comparing entries ignores case by default. However, with an optional argument
  4937. WITH-CASE, the sorting considers case as well."
  4938. (interactive "P")
  4939. (let ((case-func (if with-case 'identity 'downcase))
  4940. start beg end stars re re2
  4941. txt what tmp plain-list-p)
  4942. ;; Find beginning and end of region to sort
  4943. (cond
  4944. ((org-region-active-p)
  4945. ;; we will sort the region
  4946. (setq end (region-end)
  4947. what "region")
  4948. (goto-char (region-beginning))
  4949. (if (not (org-on-heading-p)) (outline-next-heading))
  4950. (setq start (point)))
  4951. ((org-at-item-p)
  4952. ;; we will sort this plain list
  4953. (org-beginning-of-item-list) (setq start (point))
  4954. (org-end-of-item-list) (setq end (point))
  4955. (goto-char start)
  4956. (setq plain-list-p t
  4957. what "plain list"))
  4958. ((or (org-on-heading-p)
  4959. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4960. ;; we will sort the children of the current headline
  4961. (org-back-to-heading)
  4962. (setq start (point)
  4963. end (progn (org-end-of-subtree t t)
  4964. (org-back-over-empty-lines)
  4965. (point))
  4966. what "children")
  4967. (goto-char start)
  4968. (show-subtree)
  4969. (outline-next-heading))
  4970. (t
  4971. ;; we will sort the top-level entries in this file
  4972. (goto-char (point-min))
  4973. (or (org-on-heading-p) (outline-next-heading))
  4974. (setq start (point) end (point-max) what "top-level")
  4975. (goto-char start)
  4976. (show-all)))
  4977. (setq beg (point))
  4978. (if (>= beg end) (error "Nothing to sort"))
  4979. (unless plain-list-p
  4980. (looking-at "\\(\\*+\\)")
  4981. (setq stars (match-string 1)
  4982. re (concat "^" (regexp-quote stars) " +")
  4983. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4984. txt (buffer-substring beg end))
  4985. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4986. (if (and (not (equal stars "*")) (string-match re2 txt))
  4987. (error "Region to sort contains a level above the first entry")))
  4988. (unless sorting-type
  4989. (message
  4990. (if plain-list-p
  4991. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4992. "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:")
  4993. what)
  4994. (setq sorting-type (read-char-exclusive))
  4995. (and (= (downcase sorting-type) ?f)
  4996. (setq getkey-func
  4997. (org-ido-completing-read "Sort using function: "
  4998. obarray 'fboundp t nil nil))
  4999. (setq getkey-func (intern getkey-func)))
  5000. (and (= (downcase sorting-type) ?r)
  5001. (setq property
  5002. (org-ido-completing-read "Property: "
  5003. (mapcar 'list (org-buffer-property-keys t))
  5004. nil t))))
  5005. (message "Sorting entries...")
  5006. (save-restriction
  5007. (narrow-to-region start end)
  5008. (let ((dcst (downcase sorting-type))
  5009. (now (current-time)))
  5010. (sort-subr
  5011. (/= dcst sorting-type)
  5012. ;; This function moves to the beginning character of the "record" to
  5013. ;; be sorted.
  5014. (if plain-list-p
  5015. (lambda nil
  5016. (if (org-at-item-p) t (goto-char (point-max))))
  5017. (lambda nil
  5018. (if (re-search-forward re nil t)
  5019. (goto-char (match-beginning 0))
  5020. (goto-char (point-max)))))
  5021. ;; This function moves to the last character of the "record" being
  5022. ;; sorted.
  5023. (if plain-list-p
  5024. 'org-end-of-item
  5025. (lambda nil
  5026. (save-match-data
  5027. (condition-case nil
  5028. (outline-forward-same-level 1)
  5029. (error
  5030. (goto-char (point-max)))))))
  5031. ;; This function returns the value that gets sorted against.
  5032. (if plain-list-p
  5033. (lambda nil
  5034. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5035. (cond
  5036. ((= dcst ?n)
  5037. (string-to-number (buffer-substring (match-end 0)
  5038. (point-at-eol))))
  5039. ((= dcst ?a)
  5040. (buffer-substring (match-end 0) (point-at-eol)))
  5041. ((= dcst ?t)
  5042. (if (re-search-forward org-ts-regexp
  5043. (point-at-eol) t)
  5044. (org-time-string-to-time (match-string 0))
  5045. now))
  5046. ((= dcst ?f)
  5047. (if getkey-func
  5048. (progn
  5049. (setq tmp (funcall getkey-func))
  5050. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5051. tmp)
  5052. (error "Invalid key function `%s'" getkey-func)))
  5053. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5054. (lambda nil
  5055. (cond
  5056. ((= dcst ?n)
  5057. (if (looking-at org-complex-heading-regexp)
  5058. (string-to-number (match-string 4))
  5059. nil))
  5060. ((= dcst ?a)
  5061. (if (looking-at org-complex-heading-regexp)
  5062. (funcall case-func (match-string 4))
  5063. nil))
  5064. ((= dcst ?t)
  5065. (if (re-search-forward org-ts-regexp
  5066. (save-excursion
  5067. (forward-line 2)
  5068. (point)) t)
  5069. (org-time-string-to-time (match-string 0))
  5070. now))
  5071. ((= dcst ?p)
  5072. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5073. (string-to-char (match-string 2))
  5074. org-default-priority))
  5075. ((= dcst ?r)
  5076. (or (org-entry-get nil property) ""))
  5077. ((= dcst ?o)
  5078. (if (looking-at org-complex-heading-regexp)
  5079. (- 9999 (length (member (match-string 2)
  5080. org-todo-keywords-1)))))
  5081. ((= dcst ?f)
  5082. (if getkey-func
  5083. (progn
  5084. (setq tmp (funcall getkey-func))
  5085. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5086. tmp)
  5087. (error "Invalid key function `%s'" getkey-func)))
  5088. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5089. nil
  5090. (cond
  5091. ((= dcst ?a) 'string<)
  5092. ((= dcst ?t) 'time-less-p)
  5093. ((= dcst ?f) compare-func)
  5094. (t nil)))))
  5095. (message "Sorting entries...done")))
  5096. (defun org-do-sort (table what &optional with-case sorting-type)
  5097. "Sort TABLE of WHAT according to SORTING-TYPE.
  5098. The user will be prompted for the SORTING-TYPE if the call to this
  5099. function does not specify it. WHAT is only for the prompt, to indicate
  5100. what is being sorted. The sorting key will be extracted from
  5101. the car of the elements of the table.
  5102. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5103. (unless sorting-type
  5104. (message
  5105. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5106. what)
  5107. (setq sorting-type (read-char-exclusive)))
  5108. (let ((dcst (downcase sorting-type))
  5109. extractfun comparefun)
  5110. ;; Define the appropriate functions
  5111. (cond
  5112. ((= dcst ?n)
  5113. (setq extractfun 'string-to-number
  5114. comparefun (if (= dcst sorting-type) '< '>)))
  5115. ((= dcst ?a)
  5116. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5117. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5118. comparefun (if (= dcst sorting-type)
  5119. 'string<
  5120. (lambda (a b) (and (not (string< a b))
  5121. (not (string= a b)))))))
  5122. ((= dcst ?t)
  5123. (setq extractfun
  5124. (lambda (x)
  5125. (if (string-match org-ts-regexp x)
  5126. (time-to-seconds
  5127. (org-time-string-to-time (match-string 0 x)))
  5128. 0))
  5129. comparefun (if (= dcst sorting-type) '< '>)))
  5130. (t (error "Invalid sorting type `%c'" sorting-type)))
  5131. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5132. table)
  5133. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5134. ;;; Editing source examples
  5135. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5136. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5137. (defvar org-edit-src-force-single-line nil)
  5138. (defvar org-edit-src-from-org-mode nil)
  5139. (defvar org-edit-src-picture nil)
  5140. (define-minor-mode org-exit-edit-mode
  5141. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5142. (defun org-edit-src-code ()
  5143. "Edit the source code example at point.
  5144. An indirect buffer is created, and that buffer is then narrowed to the
  5145. example at point and switched to the correct language mode. When done,
  5146. exit by killing the buffer with \\[org-edit-src-exit]."
  5147. (interactive)
  5148. (let ((line (org-current-line))
  5149. (case-fold-search t)
  5150. (msg (substitute-command-keys
  5151. "Edit, then exit with C-c ' (C-c and single quote)"))
  5152. (info (org-edit-src-find-region-and-lang))
  5153. (org-mode-p (eq major-mode 'org-mode))
  5154. beg end lang lang-f single)
  5155. (if (not info)
  5156. nil
  5157. (setq beg (nth 0 info)
  5158. end (nth 1 info)
  5159. lang (nth 2 info)
  5160. single (nth 3 info)
  5161. lang-f (intern (concat lang "-mode")))
  5162. (unless (functionp lang-f)
  5163. (error "No such language mode: %s" lang-f))
  5164. (goto-line line)
  5165. (if (get-buffer "*Org Edit Src Example*")
  5166. (kill-buffer "*Org Edit Src Example*"))
  5167. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5168. "*Org Edit Src Example*"))
  5169. (narrow-to-region beg end)
  5170. (remove-text-properties beg end '(display nil invisible nil
  5171. intangible nil))
  5172. (let ((org-inhibit-startup t))
  5173. (funcall lang-f))
  5174. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5175. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5176. (when org-mode-p
  5177. (goto-char (point-min))
  5178. (while (re-search-forward "^," nil t)
  5179. (replace-match "")))
  5180. (goto-line line)
  5181. (org-exit-edit-mode)
  5182. (org-set-local 'header-line-format msg)
  5183. (message "%s" msg)
  5184. t)))
  5185. (defun org-edit-fixed-width-region ()
  5186. "Edit the fixed-width ascii drawing at point.
  5187. This must be a region where each line starts with a colon followed by
  5188. a space character.
  5189. An indirect buffer is created, and that buffer is then narrowed to the
  5190. example at point and switched to artist-mode. When done,
  5191. exit by killing the buffer with \\[org-edit-src-exit]."
  5192. (interactive)
  5193. (let ((line (org-current-line))
  5194. (case-fold-search t)
  5195. (msg (substitute-command-keys
  5196. "Edit, then exit with C-c ' (C-c and single quote)"))
  5197. (org-mode-p (eq major-mode 'org-mode))
  5198. beg end lang lang-f)
  5199. (beginning-of-line 1)
  5200. (if (looking-at "[ \t]*[^:\n \t]")
  5201. nil
  5202. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5203. (setq beg (point) end beg)
  5204. (save-excursion
  5205. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5206. (setq beg (point-at-bol 2))
  5207. (setq beg (point))))
  5208. (save-excursion
  5209. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5210. (setq end (1- (match-beginning 0)))
  5211. (setq end (point))))
  5212. (goto-line line))
  5213. (if (get-buffer "*Org Edit Picture*")
  5214. (kill-buffer "*Org Edit Picture*"))
  5215. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5216. "*Org Edit Picture*"))
  5217. (narrow-to-region beg end)
  5218. (remove-text-properties beg end '(display nil invisible nil
  5219. intangible nil))
  5220. (when (fboundp 'font-lock-unfontify-region)
  5221. (font-lock-unfontify-region (point-min) (point-max)))
  5222. (cond
  5223. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5224. (fundamental-mode)
  5225. (artist-mode 1))
  5226. (t (funcall org-edit-fixed-width-region-mode)))
  5227. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5228. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5229. (set (make-local-variable 'org-edit-src-picture) t)
  5230. (goto-char (point-min))
  5231. (while (re-search-forward "^[ \t]*: ?" nil t)
  5232. (replace-match ""))
  5233. (goto-line line)
  5234. (org-exit-edit-mode)
  5235. (org-set-local 'header-line-format msg)
  5236. (message "%s" msg)
  5237. t)))
  5238. (defun org-edit-src-find-region-and-lang ()
  5239. "Find the region and language for a local edit.
  5240. Return a list with beginning and end of the region, a string representing
  5241. the language, a switch telling of the content should be in a single line."
  5242. (let ((re-list
  5243. (append
  5244. org-edit-src-region-extra
  5245. '(
  5246. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5247. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5248. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5249. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5250. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5251. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5252. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5253. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5254. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5255. ("^#\\+html:" "\n" "html" single-line)
  5256. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5257. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5258. ("^#\\+latex:" "\n" "latex" single-line)
  5259. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5260. ("^#\\+ascii:" "\n" "ascii" single-line)
  5261. )))
  5262. (pos (point))
  5263. re re1 re2 single beg end lang)
  5264. (catch 'exit
  5265. (while (setq entry (pop re-list))
  5266. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5267. single (nth 3 entry))
  5268. (save-excursion
  5269. (if (or (looking-at re1)
  5270. (re-search-backward re1 nil t))
  5271. (progn
  5272. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5273. (if (and (re-search-forward re2 nil t)
  5274. (>= (match-end 0) pos))
  5275. (throw 'exit (list beg (match-beginning 0) lang single))))
  5276. (if (or (looking-at re2)
  5277. (re-search-forward re2 nil t))
  5278. (progn
  5279. (setq end (match-beginning 0))
  5280. (if (and (re-search-backward re1 nil t)
  5281. (<= (match-beginning 0) pos))
  5282. (throw 'exit
  5283. (list (match-end 0) end
  5284. (org-edit-src-get-lang lang) single)))))))))))
  5285. (defun org-edit-src-get-lang (lang)
  5286. "Extract the src language."
  5287. (let ((m (match-string 0)))
  5288. (cond
  5289. ((stringp lang) lang)
  5290. ((integerp lang) (match-string lang))
  5291. ((and (eq lang 'lang)
  5292. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5293. (match-string 1 m))
  5294. ((and (eq lang 'style)
  5295. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5296. (match-string 1 m))
  5297. (t "fundamental"))))
  5298. (defun org-edit-src-exit ()
  5299. "Exit special edit and protect problematic lines."
  5300. (interactive)
  5301. (unless (buffer-base-buffer (current-buffer))
  5302. (error "This is not an indirect buffer, something is wrong..."))
  5303. (unless (> (point-min) 1)
  5304. (error "This buffer is not narrowed, something is wrong..."))
  5305. (goto-char (point-min))
  5306. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5307. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5308. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5309. (goto-char (point-min))
  5310. (while (re-search-forward "\n" nil t)
  5311. (replace-match " "))
  5312. (goto-char (point-min))
  5313. (if (looking-at "\\s-*") (replace-match " "))
  5314. (if (re-search-forward "\\s-+\\'" nil t)
  5315. (replace-match "")))
  5316. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5317. (goto-char (point-min))
  5318. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5319. (replace-match ",\\1"))
  5320. (when font-lock-mode
  5321. (font-lock-unfontify-region (point-min) (point-max)))
  5322. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5323. (when (org-bound-and-true-p org-edit-src-picture)
  5324. (untabify (point-min) (point-max))
  5325. (goto-char (point-min))
  5326. (while (re-search-forward "^" nil t)
  5327. (replace-match ": "))
  5328. (when font-lock-mode
  5329. (font-lock-unfontify-region (point-min) (point-max)))
  5330. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5331. (kill-buffer (current-buffer))
  5332. (and (org-mode-p) (org-restart-font-lock)))
  5333. ;;; The orgstruct minor mode
  5334. ;; Define a minor mode which can be used in other modes in order to
  5335. ;; integrate the org-mode structure editing commands.
  5336. ;; This is really a hack, because the org-mode structure commands use
  5337. ;; keys which normally belong to the major mode. Here is how it
  5338. ;; works: The minor mode defines all the keys necessary to operate the
  5339. ;; structure commands, but wraps the commands into a function which
  5340. ;; tests if the cursor is currently at a headline or a plain list
  5341. ;; item. If that is the case, the structure command is used,
  5342. ;; temporarily setting many Org-mode variables like regular
  5343. ;; expressions for filling etc. However, when any of those keys is
  5344. ;; used at a different location, function uses `key-binding' to look
  5345. ;; up if the key has an associated command in another currently active
  5346. ;; keymap (minor modes, major mode, global), and executes that
  5347. ;; command. There might be problems if any of the keys is otherwise
  5348. ;; used as a prefix key.
  5349. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5350. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5351. ;; addresses this by checking explicitly for both bindings.
  5352. (defvar orgstruct-mode-map (make-sparse-keymap)
  5353. "Keymap for the minor `orgstruct-mode'.")
  5354. (defvar org-local-vars nil
  5355. "List of local variables, for use by `orgstruct-mode'")
  5356. ;;;###autoload
  5357. (define-minor-mode orgstruct-mode
  5358. "Toggle the minor more `orgstruct-mode'.
  5359. This mode is for using Org-mode structure commands in other modes.
  5360. The following key behave as if Org-mode was active, if the cursor
  5361. is on a headline, or on a plain list item (both in the definition
  5362. of Org-mode).
  5363. M-up Move entry/item up
  5364. M-down Move entry/item down
  5365. M-left Promote
  5366. M-right Demote
  5367. M-S-up Move entry/item up
  5368. M-S-down Move entry/item down
  5369. M-S-left Promote subtree
  5370. M-S-right Demote subtree
  5371. M-q Fill paragraph and items like in Org-mode
  5372. C-c ^ Sort entries
  5373. C-c - Cycle list bullet
  5374. TAB Cycle item visibility
  5375. M-RET Insert new heading/item
  5376. S-M-RET Insert new TODO heading / Checkbox item
  5377. C-c C-c Set tags / toggle checkbox"
  5378. nil " OrgStruct" nil
  5379. (org-load-modules-maybe)
  5380. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5381. ;;;###autoload
  5382. (defun turn-on-orgstruct ()
  5383. "Unconditionally turn on `orgstruct-mode'."
  5384. (orgstruct-mode 1))
  5385. ;;;###autoload
  5386. (defun turn-on-orgstruct++ ()
  5387. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5388. In addition to setting orgstruct-mode, this also exports all indentation and
  5389. autofilling variables from org-mode into the buffer. Note that turning
  5390. off orgstruct-mode will *not* remove these additional settings."
  5391. (orgstruct-mode 1)
  5392. (let (var val)
  5393. (mapc
  5394. (lambda (x)
  5395. (when (string-match
  5396. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5397. (symbol-name (car x)))
  5398. (setq var (car x) val (nth 1 x))
  5399. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5400. org-local-vars)))
  5401. (defun orgstruct-error ()
  5402. "Error when there is no default binding for a structure key."
  5403. (interactive)
  5404. (error "This key has no function outside structure elements"))
  5405. (defun orgstruct-setup ()
  5406. "Setup orgstruct keymaps."
  5407. (let ((nfunc 0)
  5408. (bindings
  5409. (list
  5410. '([(meta up)] org-metaup)
  5411. '([(meta down)] org-metadown)
  5412. '([(meta left)] org-metaleft)
  5413. '([(meta right)] org-metaright)
  5414. '([(meta shift up)] org-shiftmetaup)
  5415. '([(meta shift down)] org-shiftmetadown)
  5416. '([(meta shift left)] org-shiftmetaleft)
  5417. '([(meta shift right)] org-shiftmetaright)
  5418. '([(shift up)] org-shiftup)
  5419. '([(shift down)] org-shiftdown)
  5420. '([(shift left)] org-shiftleft)
  5421. '([(shift right)] org-shiftright)
  5422. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5423. '("\M-q" fill-paragraph)
  5424. '("\C-c^" org-sort)
  5425. '("\C-c-" org-cycle-list-bullet)))
  5426. elt key fun cmd)
  5427. (while (setq elt (pop bindings))
  5428. (setq nfunc (1+ nfunc))
  5429. (setq key (org-key (car elt))
  5430. fun (nth 1 elt)
  5431. cmd (orgstruct-make-binding fun nfunc key))
  5432. (org-defkey orgstruct-mode-map key cmd))
  5433. ;; Special treatment needed for TAB and RET
  5434. (org-defkey orgstruct-mode-map [(tab)]
  5435. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5436. (org-defkey orgstruct-mode-map "\C-i"
  5437. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5438. (org-defkey orgstruct-mode-map "\M-\C-m"
  5439. (orgstruct-make-binding 'org-insert-heading 105
  5440. "\M-\C-m" [(meta return)]))
  5441. (org-defkey orgstruct-mode-map [(meta return)]
  5442. (orgstruct-make-binding 'org-insert-heading 106
  5443. [(meta return)] "\M-\C-m"))
  5444. (org-defkey orgstruct-mode-map [(shift meta return)]
  5445. (orgstruct-make-binding 'org-insert-todo-heading 107
  5446. [(meta return)] "\M-\C-m"))
  5447. (unless org-local-vars
  5448. (setq org-local-vars (org-get-local-variables)))
  5449. t))
  5450. (defun orgstruct-make-binding (fun n &rest keys)
  5451. "Create a function for binding in the structure minor mode.
  5452. FUN is the command to call inside a table. N is used to create a unique
  5453. command name. KEYS are keys that should be checked in for a command
  5454. to execute outside of tables."
  5455. (eval
  5456. (list 'defun
  5457. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5458. '(arg)
  5459. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5460. "Outside of structure, run the binding of `"
  5461. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5462. "'.")
  5463. '(interactive "p")
  5464. (list 'if
  5465. '(org-context-p 'headline 'item)
  5466. (list 'org-run-like-in-org-mode (list 'quote fun))
  5467. (list 'let '(orgstruct-mode)
  5468. (list 'call-interactively
  5469. (append '(or)
  5470. (mapcar (lambda (k)
  5471. (list 'key-binding k))
  5472. keys)
  5473. '('orgstruct-error))))))))
  5474. (defun org-context-p (&rest contexts)
  5475. "Check if local context is any of CONTEXTS.
  5476. Possible values in the list of contexts are `table', `headline', and `item'."
  5477. (let ((pos (point)))
  5478. (goto-char (point-at-bol))
  5479. (prog1 (or (and (memq 'table contexts)
  5480. (looking-at "[ \t]*|"))
  5481. (and (memq 'headline contexts)
  5482. ;;????????? (looking-at "\\*+"))
  5483. (looking-at outline-regexp))
  5484. (and (memq 'item contexts)
  5485. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5486. (goto-char pos))))
  5487. (defun org-get-local-variables ()
  5488. "Return a list of all local variables in an org-mode buffer."
  5489. (let (varlist)
  5490. (with-current-buffer (get-buffer-create "*Org tmp*")
  5491. (erase-buffer)
  5492. (org-mode)
  5493. (setq varlist (buffer-local-variables)))
  5494. (kill-buffer "*Org tmp*")
  5495. (delq nil
  5496. (mapcar
  5497. (lambda (x)
  5498. (setq x
  5499. (if (symbolp x)
  5500. (list x)
  5501. (list (car x) (list 'quote (cdr x)))))
  5502. (if (string-match
  5503. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5504. (symbol-name (car x)))
  5505. x nil))
  5506. varlist))))
  5507. ;;;###autoload
  5508. (defun org-run-like-in-org-mode (cmd)
  5509. (org-load-modules-maybe)
  5510. (unless org-local-vars
  5511. (setq org-local-vars (org-get-local-variables)))
  5512. (eval (list 'let org-local-vars
  5513. (list 'call-interactively (list 'quote cmd)))))
  5514. ;;;; Archiving
  5515. (defun org-get-category (&optional pos)
  5516. "Get the category applying to position POS."
  5517. (get-text-property (or pos (point)) 'org-category))
  5518. (defun org-refresh-category-properties ()
  5519. "Refresh category text properties in the buffer."
  5520. (let ((def-cat (cond
  5521. ((null org-category)
  5522. (if buffer-file-name
  5523. (file-name-sans-extension
  5524. (file-name-nondirectory buffer-file-name))
  5525. "???"))
  5526. ((symbolp org-category) (symbol-name org-category))
  5527. (t org-category)))
  5528. beg end cat pos optionp)
  5529. (org-unmodified
  5530. (save-excursion
  5531. (save-restriction
  5532. (widen)
  5533. (goto-char (point-min))
  5534. (put-text-property (point) (point-max) 'org-category def-cat)
  5535. (while (re-search-forward
  5536. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5537. (setq pos (match-end 0)
  5538. optionp (equal (char-after (match-beginning 0)) ?#)
  5539. cat (org-trim (match-string 2)))
  5540. (if optionp
  5541. (setq beg (point-at-bol) end (point-max))
  5542. (org-back-to-heading t)
  5543. (setq beg (point) end (org-end-of-subtree t t)))
  5544. (put-text-property beg end 'org-category cat)
  5545. (goto-char pos)))))))
  5546. ;;;; Link Stuff
  5547. ;;; Link abbreviations
  5548. (defun org-link-expand-abbrev (link)
  5549. "Apply replacements as defined in `org-link-abbrev-alist."
  5550. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5551. (let* ((key (match-string 1 link))
  5552. (as (or (assoc key org-link-abbrev-alist-local)
  5553. (assoc key org-link-abbrev-alist)))
  5554. (tag (and (match-end 2) (match-string 3 link)))
  5555. rpl)
  5556. (if (not as)
  5557. link
  5558. (setq rpl (cdr as))
  5559. (cond
  5560. ((symbolp rpl) (funcall rpl tag))
  5561. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5562. ((string-match "%h" rpl)
  5563. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5564. (t (concat rpl tag)))))
  5565. link))
  5566. ;;; Storing and inserting links
  5567. (defvar org-insert-link-history nil
  5568. "Minibuffer history for links inserted with `org-insert-link'.")
  5569. (defvar org-stored-links nil
  5570. "Contains the links stored with `org-store-link'.")
  5571. (defvar org-store-link-plist nil
  5572. "Plist with info about the most recently link created with `org-store-link'.")
  5573. (defvar org-link-protocols nil
  5574. "Link protocols added to Org-mode using `org-add-link-type'.")
  5575. (defvar org-store-link-functions nil
  5576. "List of functions that are called to create and store a link.
  5577. Each function will be called in turn until one returns a non-nil
  5578. value. Each function should check if it is responsible for creating
  5579. this link (for example by looking at the major mode).
  5580. If not, it must exit and return nil.
  5581. If yes, it should return a non-nil value after a calling
  5582. `org-store-link-props' with a list of properties and values.
  5583. Special properties are:
  5584. :type The link prefix. like \"http\". This must be given.
  5585. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5586. This is obligatory as well.
  5587. :description Optional default description for the second pair
  5588. of brackets in an Org-mode link. The user can still change
  5589. this when inserting this link into an Org-mode buffer.
  5590. In addition to these, any additional properties can be specified
  5591. and then used in remember templates.")
  5592. (defun org-add-link-type (type &optional follow export)
  5593. "Add TYPE to the list of `org-link-types'.
  5594. Re-compute all regular expressions depending on `org-link-types'
  5595. FOLLOW and EXPORT are two functions.
  5596. FOLLOW should take the link path as the single argument and do whatever
  5597. is necessary to follow the link, for example find a file or display
  5598. a mail message.
  5599. EXPORT should format the link path for export to one of the export formats.
  5600. It should be a function accepting three arguments:
  5601. path the path of the link, the text after the prefix (like \"http:\")
  5602. desc the description of the link, if any, nil if there was no description
  5603. format the export format, a symbol like `html' or `latex'.
  5604. The function may use the FORMAT information to return different values
  5605. depending on the format. The return value will be put literally into
  5606. the exported file.
  5607. Org-mode has a built-in default for exporting links. If you are happy with
  5608. this default, there is no need to define an export function for the link
  5609. type. For a simple example of an export function, see `org-bbdb.el'."
  5610. (add-to-list 'org-link-types type t)
  5611. (org-make-link-regexps)
  5612. (if (assoc type org-link-protocols)
  5613. (setcdr (assoc type org-link-protocols) (list follow export))
  5614. (push (list type follow export) org-link-protocols)))
  5615. ;;;###autoload
  5616. (defun org-store-link (arg)
  5617. "\\<org-mode-map>Store an org-link to the current location.
  5618. This link is added to `org-stored-links' and can later be inserted
  5619. into an org-buffer with \\[org-insert-link].
  5620. For some link types, a prefix arg is interpreted:
  5621. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5622. For file links, arg negates `org-context-in-file-links'."
  5623. (interactive "P")
  5624. (org-load-modules-maybe)
  5625. (setq org-store-link-plist nil) ; reset
  5626. (let (link cpltxt desc description search txt)
  5627. (cond
  5628. ((run-hook-with-args-until-success 'org-store-link-functions)
  5629. (setq link (plist-get org-store-link-plist :link)
  5630. desc (or (plist-get org-store-link-plist :description) link)))
  5631. ((equal (buffer-name) "*Org Edit Src Example*")
  5632. (let (label gc)
  5633. (while (or (not label)
  5634. (save-excursion
  5635. (save-restriction
  5636. (widen)
  5637. (goto-char (point-min))
  5638. (re-search-forward (concat "((" label "))") nil t))))
  5639. (when label (message "Label exists already") (sit-for 2))
  5640. (setq label (read-string "Code line label: " label)))
  5641. (end-of-line 1)
  5642. (setq link (format "((%s))" label))
  5643. (setq gc (- 79 (length link)))
  5644. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5645. (insert link)
  5646. (setq desc nil)))
  5647. ((eq major-mode 'calendar-mode)
  5648. (let ((cd (calendar-cursor-to-date)))
  5649. (setq link
  5650. (format-time-string
  5651. (car org-time-stamp-formats)
  5652. (apply 'encode-time
  5653. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5654. nil nil nil))))
  5655. (org-store-link-props :type "calendar" :date cd)))
  5656. ((eq major-mode 'w3-mode)
  5657. (setq cpltxt (url-view-url t)
  5658. link (org-make-link cpltxt))
  5659. (org-store-link-props :type "w3" :url (url-view-url t)))
  5660. ((eq major-mode 'w3m-mode)
  5661. (setq cpltxt (or w3m-current-title w3m-current-url)
  5662. link (org-make-link w3m-current-url))
  5663. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5664. ((setq search (run-hook-with-args-until-success
  5665. 'org-create-file-search-functions))
  5666. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5667. "::" search))
  5668. (setq cpltxt (or description link)))
  5669. ((eq major-mode 'image-mode)
  5670. (setq cpltxt (concat "file:"
  5671. (abbreviate-file-name buffer-file-name))
  5672. link (org-make-link cpltxt))
  5673. (org-store-link-props :type "image" :file buffer-file-name))
  5674. ((eq major-mode 'dired-mode)
  5675. ;; link to the file in the current line
  5676. (setq cpltxt (concat "file:"
  5677. (abbreviate-file-name
  5678. (expand-file-name
  5679. (dired-get-filename nil t))))
  5680. link (org-make-link cpltxt)))
  5681. ((and buffer-file-name (org-mode-p))
  5682. (cond
  5683. ((org-in-regexp "<<\\(.*?\\)>>")
  5684. (setq cpltxt
  5685. (concat "file:"
  5686. (abbreviate-file-name buffer-file-name)
  5687. "::" (match-string 1))
  5688. link (org-make-link cpltxt)))
  5689. ((and (featurep 'org-id)
  5690. (or (eq org-link-to-org-use-id t)
  5691. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5692. (interactive-p))
  5693. (and org-link-to-org-use-id
  5694. (condition-case nil
  5695. (org-entry-get nil "ID")
  5696. (error nil)))))
  5697. ;; We can make a link using the ID.
  5698. (setq link (condition-case nil
  5699. (prog1 (org-id-store-link)
  5700. (setq desc (plist-get org-store-link-plist
  5701. :description)))
  5702. (error
  5703. ;; probably before first headline, link to file only
  5704. (concat "file:"
  5705. (abbreviate-file-name buffer-file-name))))))
  5706. (t
  5707. ;; Just link to current headline
  5708. (setq cpltxt (concat "file:"
  5709. (abbreviate-file-name buffer-file-name)))
  5710. ;; Add a context search string
  5711. (when (org-xor org-context-in-file-links arg)
  5712. (setq txt (cond
  5713. ((org-on-heading-p) nil)
  5714. ((org-region-active-p)
  5715. (buffer-substring (region-beginning) (region-end)))
  5716. (t nil)))
  5717. (when (or (null txt) (string-match "\\S-" txt))
  5718. (setq cpltxt
  5719. (concat cpltxt "::"
  5720. (condition-case nil
  5721. (org-make-org-heading-search-string txt)
  5722. (error "")))
  5723. desc "NONE")))
  5724. (if (string-match "::\\'" cpltxt)
  5725. (setq cpltxt (substring cpltxt 0 -2)))
  5726. (setq link (org-make-link cpltxt)))))
  5727. ((buffer-file-name (buffer-base-buffer))
  5728. ;; Just link to this file here.
  5729. (setq cpltxt (concat "file:"
  5730. (abbreviate-file-name
  5731. (buffer-file-name (buffer-base-buffer)))))
  5732. ;; Add a context string
  5733. (when (org-xor org-context-in-file-links arg)
  5734. (setq txt (if (org-region-active-p)
  5735. (buffer-substring (region-beginning) (region-end))
  5736. (buffer-substring (point-at-bol) (point-at-eol))))
  5737. ;; Only use search option if there is some text.
  5738. (when (string-match "\\S-" txt)
  5739. (setq cpltxt
  5740. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5741. desc "NONE")))
  5742. (setq link (org-make-link cpltxt)))
  5743. ((interactive-p)
  5744. (error "Cannot link to a buffer which is not visiting a file"))
  5745. (t (setq link nil)))
  5746. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5747. (setq link (or link cpltxt)
  5748. desc (or desc cpltxt))
  5749. (if (equal desc "NONE") (setq desc nil))
  5750. (if (and (interactive-p) link)
  5751. (progn
  5752. (setq org-stored-links
  5753. (cons (list link desc) org-stored-links))
  5754. (message "Stored: %s" (or desc link)))
  5755. (and link (org-make-link-string link desc)))))
  5756. (defun org-store-link-props (&rest plist)
  5757. "Store link properties, extract names and addresses."
  5758. (let (x adr)
  5759. (when (setq x (plist-get plist :from))
  5760. (setq adr (mail-extract-address-components x))
  5761. (setq plist (plist-put plist :fromname (car adr)))
  5762. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5763. (when (setq x (plist-get plist :to))
  5764. (setq adr (mail-extract-address-components x))
  5765. (setq plist (plist-put plist :toname (car adr)))
  5766. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5767. (let ((from (plist-get plist :from))
  5768. (to (plist-get plist :to)))
  5769. (when (and from to org-from-is-user-regexp)
  5770. (setq plist
  5771. (plist-put plist :fromto
  5772. (if (string-match org-from-is-user-regexp from)
  5773. (concat "to %t")
  5774. (concat "from %f"))))))
  5775. (setq org-store-link-plist plist))
  5776. (defun org-add-link-props (&rest plist)
  5777. "Add these properties to the link property list."
  5778. (let (key value)
  5779. (while plist
  5780. (setq key (pop plist) value (pop plist))
  5781. (setq org-store-link-plist
  5782. (plist-put org-store-link-plist key value)))))
  5783. (defun org-email-link-description (&optional fmt)
  5784. "Return the description part of an email link.
  5785. This takes information from `org-store-link-plist' and formats it
  5786. according to FMT (default from `org-email-link-description-format')."
  5787. (setq fmt (or fmt org-email-link-description-format))
  5788. (let* ((p org-store-link-plist)
  5789. (to (plist-get p :toaddress))
  5790. (from (plist-get p :fromaddress))
  5791. (table
  5792. (list
  5793. (cons "%c" (plist-get p :fromto))
  5794. (cons "%F" (plist-get p :from))
  5795. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5796. (cons "%T" (plist-get p :to))
  5797. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5798. (cons "%s" (plist-get p :subject))
  5799. (cons "%m" (plist-get p :message-id)))))
  5800. (when (string-match "%c" fmt)
  5801. ;; Check if the user wrote this message
  5802. (if (and org-from-is-user-regexp from to
  5803. (save-match-data (string-match org-from-is-user-regexp from)))
  5804. (setq fmt (replace-match "to %t" t t fmt))
  5805. (setq fmt (replace-match "from %f" t t fmt))))
  5806. (org-replace-escapes fmt table)))
  5807. (defun org-make-org-heading-search-string (&optional string heading)
  5808. "Make search string for STRING or current headline."
  5809. (interactive)
  5810. (let ((s (or string (org-get-heading))))
  5811. (unless (and string (not heading))
  5812. ;; We are using a headline, clean up garbage in there.
  5813. (if (string-match org-todo-regexp s)
  5814. (setq s (replace-match "" t t s)))
  5815. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5816. (setq s (replace-match "" t t s)))
  5817. (setq s (org-trim s))
  5818. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5819. org-comment-string "\\)") s)
  5820. (setq s (replace-match "" t t s)))
  5821. (while (string-match org-ts-regexp s)
  5822. (setq s (replace-match "" t t s))))
  5823. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5824. (setq s (replace-match " " t t s)))
  5825. (or string (setq s (concat "*" s))) ; Add * for headlines
  5826. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5827. (defun org-make-link (&rest strings)
  5828. "Concatenate STRINGS."
  5829. (apply 'concat strings))
  5830. (defun org-make-link-string (link &optional description)
  5831. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5832. (unless (string-match "\\S-" link)
  5833. (error "Empty link"))
  5834. (when (stringp description)
  5835. ;; Remove brackets from the description, they are fatal.
  5836. (while (string-match "\\[" description)
  5837. (setq description (replace-match "{" t t description)))
  5838. (while (string-match "\\]" description)
  5839. (setq description (replace-match "}" t t description))))
  5840. (when (equal (org-link-escape link) description)
  5841. ;; No description needed, it is identical
  5842. (setq description nil))
  5843. (when (and (not description)
  5844. (not (equal link (org-link-escape link))))
  5845. (setq description (org-extract-attributes link)))
  5846. (concat "[[" (org-link-escape link) "]"
  5847. (if description (concat "[" description "]") "")
  5848. "]"))
  5849. (defconst org-link-escape-chars
  5850. '((?\ . "%20")
  5851. (?\[ . "%5B")
  5852. (?\] . "%5D")
  5853. (?\340 . "%E0") ; `a
  5854. (?\342 . "%E2") ; ^a
  5855. (?\347 . "%E7") ; ,c
  5856. (?\350 . "%E8") ; `e
  5857. (?\351 . "%E9") ; 'e
  5858. (?\352 . "%EA") ; ^e
  5859. (?\356 . "%EE") ; ^i
  5860. (?\364 . "%F4") ; ^o
  5861. (?\371 . "%F9") ; `u
  5862. (?\373 . "%FB") ; ^u
  5863. (?\; . "%3B")
  5864. (?? . "%3F")
  5865. (?= . "%3D")
  5866. (?+ . "%2B")
  5867. )
  5868. "Association list of escapes for some characters problematic in links.
  5869. This is the list that is used for internal purposes.")
  5870. (defconst org-link-escape-chars-browser
  5871. '((?\ . "%20")) ; 32 for the SPC char
  5872. "Association list of escapes for some characters problematic in links.
  5873. This is the list that is used before handing over to the browser.")
  5874. (defun org-link-escape (text &optional table)
  5875. "Escape characters in TEXT that are problematic for links."
  5876. (setq table (or table org-link-escape-chars))
  5877. (when text
  5878. (let ((re (mapconcat (lambda (x) (regexp-quote
  5879. (char-to-string (car x))))
  5880. table "\\|")))
  5881. (while (string-match re text)
  5882. (setq text
  5883. (replace-match
  5884. (cdr (assoc (string-to-char (match-string 0 text))
  5885. table))
  5886. t t text)))
  5887. text)))
  5888. (defun org-link-unescape (text &optional table)
  5889. "Reverse the action of `org-link-escape'."
  5890. (setq table (or table org-link-escape-chars))
  5891. (when text
  5892. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5893. table "\\|")))
  5894. (while (string-match re text)
  5895. (setq text
  5896. (replace-match
  5897. (char-to-string (car (rassoc (match-string 0 text) table)))
  5898. t t text)))
  5899. text)))
  5900. (defun org-xor (a b)
  5901. "Exclusive or."
  5902. (if a (not b) b))
  5903. (defun org-get-header (header)
  5904. "Find a header field in the current buffer."
  5905. (save-excursion
  5906. (goto-char (point-min))
  5907. (let ((case-fold-search t) s)
  5908. (cond
  5909. ((eq header 'from)
  5910. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5911. (setq s (match-string 1)))
  5912. (while (string-match "\"" s)
  5913. (setq s (replace-match "" t t s)))
  5914. (if (string-match "[<(].*" s)
  5915. (setq s (replace-match "" t t s))))
  5916. ((eq header 'message-id)
  5917. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5918. (setq s (match-string 1))))
  5919. ((eq header 'subject)
  5920. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5921. (setq s (match-string 1)))))
  5922. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5923. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5924. s)))
  5925. (defun org-fixup-message-id-for-http (s)
  5926. "Replace special characters in a message id, so it can be used in an http query."
  5927. (while (string-match "<" s)
  5928. (setq s (replace-match "%3C" t t s)))
  5929. (while (string-match ">" s)
  5930. (setq s (replace-match "%3E" t t s)))
  5931. (while (string-match "@" s)
  5932. (setq s (replace-match "%40" t t s)))
  5933. s)
  5934. ;;;###autoload
  5935. (defun org-insert-link-global ()
  5936. "Insert a link like Org-mode does.
  5937. This command can be called in any mode to insert a link in Org-mode syntax."
  5938. (interactive)
  5939. (org-load-modules-maybe)
  5940. (org-run-like-in-org-mode 'org-insert-link))
  5941. (defun org-insert-link (&optional complete-file link-location)
  5942. "Insert a link. At the prompt, enter the link.
  5943. Completion can be used to insert any of the link protocol prefixes like
  5944. http or ftp in use.
  5945. The history can be used to select a link previously stored with
  5946. `org-store-link'. When the empty string is entered (i.e. if you just
  5947. press RET at the prompt), the link defaults to the most recently
  5948. stored link. As SPC triggers completion in the minibuffer, you need to
  5949. use M-SPC or C-q SPC to force the insertion of a space character.
  5950. You will also be prompted for a description, and if one is given, it will
  5951. be displayed in the buffer instead of the link.
  5952. If there is already a link at point, this command will allow you to edit link
  5953. and description parts.
  5954. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5955. be selected using completion. The path to the file will be relative to the
  5956. current directory if the file is in the current directory or a subdirectory.
  5957. Otherwise, the link will be the absolute path as completed in the minibuffer
  5958. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  5959. option `org-link-file-path-type'.
  5960. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5961. the current directory or below.
  5962. With three \\[universal-argument] prefixes, negate the meaning of
  5963. `org-keep-stored-link-after-insertion'.
  5964. If `org-make-link-description-function' is non-nil, this function will be
  5965. called with the link target, and the result will be the default
  5966. link description.
  5967. If the LINK-LOCATION parameter is non-nil, this value will be
  5968. used as the link location instead of reading one interactively."
  5969. (interactive "P")
  5970. (let* ((wcf (current-window-configuration))
  5971. (region (if (org-region-active-p)
  5972. (buffer-substring (region-beginning) (region-end))))
  5973. (remove (and region (list (region-beginning) (region-end))))
  5974. (desc region)
  5975. tmphist ; byte-compile incorrectly complains about this
  5976. (link link-location)
  5977. entry file)
  5978. (cond
  5979. (link-location) ; specified by arg, just use it.
  5980. ((org-in-regexp org-bracket-link-regexp 1)
  5981. ;; We do have a link at point, and we are going to edit it.
  5982. (setq remove (list (match-beginning 0) (match-end 0)))
  5983. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5984. (setq link (read-string "Link: "
  5985. (org-link-unescape
  5986. (org-match-string-no-properties 1)))))
  5987. ((or (org-in-regexp org-angle-link-re)
  5988. (org-in-regexp org-plain-link-re))
  5989. ;; Convert to bracket link
  5990. (setq remove (list (match-beginning 0) (match-end 0))
  5991. link (read-string "Link: "
  5992. (org-remove-angle-brackets (match-string 0)))))
  5993. ((member complete-file '((4) (16)))
  5994. ;; Completing read for file names.
  5995. (setq file (read-file-name "File: "))
  5996. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5997. (pwd1 (file-name-as-directory (abbreviate-file-name
  5998. (expand-file-name ".")))))
  5999. (cond
  6000. ((equal complete-file '(16))
  6001. (setq link (org-make-link
  6002. "file:"
  6003. (abbreviate-file-name (expand-file-name file)))))
  6004. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6005. (setq link (org-make-link "file:" (match-string 1 file))))
  6006. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6007. (expand-file-name file))
  6008. (setq link (org-make-link
  6009. "file:" (match-string 1 (expand-file-name file)))))
  6010. (t (setq link (org-make-link "file:" file))))))
  6011. (t
  6012. ;; Read link, with completion for stored links.
  6013. (with-output-to-temp-buffer "*Org Links*"
  6014. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6015. (when org-stored-links
  6016. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6017. (princ (mapconcat
  6018. (lambda (x)
  6019. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6020. (reverse org-stored-links) "\n"))))
  6021. (let ((cw (selected-window)))
  6022. (select-window (get-buffer-window "*Org Links*"))
  6023. (setq truncate-lines t)
  6024. (org-fit-window-to-buffer)
  6025. (select-window cw))
  6026. ;; Fake a link history, containing the stored links.
  6027. (setq tmphist (append (mapcar 'car org-stored-links)
  6028. org-insert-link-history))
  6029. (unwind-protect
  6030. (setq link
  6031. (let ((org-completion-use-ido nil))
  6032. (org-completing-read
  6033. "Link: "
  6034. (append
  6035. (mapcar (lambda (x) (list (concat (car x) ":")))
  6036. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6037. (mapcar (lambda (x) (list (concat x ":")))
  6038. org-link-types))
  6039. nil nil nil
  6040. 'tmphist
  6041. (or (car (car org-stored-links))))))
  6042. (set-window-configuration wcf)
  6043. (kill-buffer "*Org Links*"))
  6044. (setq entry (assoc link org-stored-links))
  6045. (or entry (push link org-insert-link-history))
  6046. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6047. (not org-keep-stored-link-after-insertion))
  6048. (setq org-stored-links (delq (assoc link org-stored-links)
  6049. org-stored-links)))
  6050. (setq desc (or desc (nth 1 entry)))))
  6051. (if (string-match org-plain-link-re link)
  6052. ;; URL-like link, normalize the use of angular brackets.
  6053. (setq link (org-make-link (org-remove-angle-brackets link))))
  6054. ;; Check if we are linking to the current file with a search option
  6055. ;; If yes, simplify the link by using only the search option.
  6056. (when (and buffer-file-name
  6057. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6058. (let* ((path (match-string 1 link))
  6059. (case-fold-search nil)
  6060. (search (match-string 2 link)))
  6061. (save-match-data
  6062. (if (equal (file-truename buffer-file-name) (file-truename path))
  6063. ;; We are linking to this same file, with a search option
  6064. (setq link search)))))
  6065. ;; Check if we can/should use a relative path. If yes, simplify the link
  6066. (when (string-match "^file:\\(.*\\)" link)
  6067. (let* ((path (match-string 1 link))
  6068. (origpath path)
  6069. (case-fold-search nil))
  6070. (cond
  6071. ((or (eq org-link-file-path-type 'absolute)
  6072. (equal complete-file '(16)))
  6073. (setq path (abbreviate-file-name (expand-file-name path))))
  6074. ((eq org-link-file-path-type 'noabbrev)
  6075. (setq path (expand-file-name path)))
  6076. ((eq org-link-file-path-type 'relative)
  6077. (setq path (file-relative-name path)))
  6078. (t
  6079. (save-match-data
  6080. (if (string-match (concat "^" (regexp-quote
  6081. (file-name-as-directory
  6082. (expand-file-name "."))))
  6083. (expand-file-name path))
  6084. ;; We are linking a file with relative path name.
  6085. (setq path (substring (expand-file-name path)
  6086. (match-end 0)))
  6087. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6088. (setq link (concat "file:" path))
  6089. (if (equal desc origpath)
  6090. (setq desc path))))
  6091. (if org-make-link-description-function
  6092. (setq desc (funcall org-make-link-description-function link desc)))
  6093. (setq desc (read-string "Description: " desc))
  6094. (unless (string-match "\\S-" desc) (setq desc nil))
  6095. (if remove (apply 'delete-region remove))
  6096. (insert (org-make-link-string link desc))))
  6097. (defun org-completing-read (&rest args)
  6098. "Completing-read with SPACE being a normal character."
  6099. (let ((minibuffer-local-completion-map
  6100. (copy-keymap minibuffer-local-completion-map)))
  6101. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6102. (apply 'org-ido-completing-read args)))
  6103. (defun org-ido-completing-read (&rest args)
  6104. "Completing-read using `ido-mode' speedups if available"
  6105. (if (and org-completion-use-ido
  6106. (fboundp 'ido-completing-read)
  6107. (boundp 'ido-mode) ido-mode
  6108. (listp (second args)))
  6109. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6110. (apply 'completing-read args)))
  6111. (defun org-extract-attributes (s)
  6112. "Extract the attributes cookie from a string and set as text property."
  6113. (let (a attr (start 0) key value)
  6114. (save-match-data
  6115. (when (string-match "{{\\([^}]+\\)}}$" s)
  6116. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6117. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6118. (setq key (match-string 1 a) value (match-string 2 a)
  6119. start (match-end 0)
  6120. attr (plist-put attr (intern key) value))))
  6121. (org-add-props s nil 'org-attr attr))
  6122. s))
  6123. (defun org-attributes-to-string (plist)
  6124. "Format a property list into an HTML attribute list."
  6125. (let ((s "") key value)
  6126. (while plist
  6127. (setq key (pop plist) value (pop plist))
  6128. (and value
  6129. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6130. s))
  6131. ;;; Opening/following a link
  6132. (defvar org-link-search-failed nil)
  6133. (defun org-next-link ()
  6134. "Move forward to the next link.
  6135. If the link is in hidden text, expose it."
  6136. (interactive)
  6137. (when (and org-link-search-failed (eq this-command last-command))
  6138. (goto-char (point-min))
  6139. (message "Link search wrapped back to beginning of buffer"))
  6140. (setq org-link-search-failed nil)
  6141. (let* ((pos (point))
  6142. (ct (org-context))
  6143. (a (assoc :link ct)))
  6144. (if a (goto-char (nth 2 a)))
  6145. (if (re-search-forward org-any-link-re nil t)
  6146. (progn
  6147. (goto-char (match-beginning 0))
  6148. (if (org-invisible-p) (org-show-context)))
  6149. (goto-char pos)
  6150. (setq org-link-search-failed t)
  6151. (error "No further link found"))))
  6152. (defun org-previous-link ()
  6153. "Move backward to the previous link.
  6154. If the link is in hidden text, expose it."
  6155. (interactive)
  6156. (when (and org-link-search-failed (eq this-command last-command))
  6157. (goto-char (point-max))
  6158. (message "Link search wrapped back to end of buffer"))
  6159. (setq org-link-search-failed nil)
  6160. (let* ((pos (point))
  6161. (ct (org-context))
  6162. (a (assoc :link ct)))
  6163. (if a (goto-char (nth 1 a)))
  6164. (if (re-search-backward org-any-link-re nil t)
  6165. (progn
  6166. (goto-char (match-beginning 0))
  6167. (if (org-invisible-p) (org-show-context)))
  6168. (goto-char pos)
  6169. (setq org-link-search-failed t)
  6170. (error "No further link found"))))
  6171. (defun org-translate-link (s)
  6172. "Translate a link string if a translation function has been defined."
  6173. (if (and org-link-translation-function
  6174. (fboundp org-link-translation-function)
  6175. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6176. (progn
  6177. (setq s (funcall org-link-translation-function
  6178. (match-string 1) (match-string 2)))
  6179. (concat (car s) ":" (cdr s)))
  6180. s))
  6181. (defun org-translate-link-from-planner (type path)
  6182. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6183. This is still an experimental function, your mileage may vary."
  6184. (cond
  6185. ((member type '("http" "https" "news" "ftp"))
  6186. ;; standard Internet links are the same.
  6187. nil)
  6188. ((and (equal type "irc") (string-match "^//" path))
  6189. ;; Planner has two / at the beginning of an irc link, we have 1.
  6190. ;; We should have zero, actually....
  6191. (setq path (substring path 1)))
  6192. ((and (equal type "lisp") (string-match "^/" path))
  6193. ;; Planner has a slash, we do not.
  6194. (setq type "elisp" path (substring path 1)))
  6195. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6196. ;; A typical message link. Planner has the id after the fina slash,
  6197. ;; we separate it with a hash mark
  6198. (setq path (concat (match-string 1 path) "#"
  6199. (org-remove-angle-brackets (match-string 2 path)))))
  6200. )
  6201. (cons type path))
  6202. (defun org-find-file-at-mouse (ev)
  6203. "Open file link or URL at mouse."
  6204. (interactive "e")
  6205. (mouse-set-point ev)
  6206. (org-open-at-point 'in-emacs))
  6207. (defun org-open-at-mouse (ev)
  6208. "Open file link or URL at mouse."
  6209. (interactive "e")
  6210. (mouse-set-point ev)
  6211. (if (eq major-mode 'org-agenda-mode)
  6212. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6213. (org-open-at-point))
  6214. (defvar org-window-config-before-follow-link nil
  6215. "The window configuration before following a link.
  6216. This is saved in case the need arises to restore it.")
  6217. (defvar org-open-link-marker (make-marker)
  6218. "Marker pointing to the location where `org-open-at-point; was called.")
  6219. ;;;###autoload
  6220. (defun org-open-at-point-global ()
  6221. "Follow a link like Org-mode does.
  6222. This command can be called in any mode to follow a link that has
  6223. Org-mode syntax."
  6224. (interactive)
  6225. (org-run-like-in-org-mode 'org-open-at-point))
  6226. ;;;###autoload
  6227. (defun org-open-link-from-string (s &optional arg)
  6228. "Open a link in the string S, as if it was in Org-mode."
  6229. (interactive "sLink: \nP")
  6230. (with-temp-buffer
  6231. (let ((org-inhibit-startup t))
  6232. (org-mode)
  6233. (insert s)
  6234. (goto-char (point-min))
  6235. (org-open-at-point arg))))
  6236. (defun org-open-at-point (&optional in-emacs)
  6237. "Open link at or after point.
  6238. If there is no link at point, this function will search forward up to
  6239. the end of the current subtree.
  6240. Normally, files will be opened by an appropriate application. If the
  6241. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6242. With a double prefix argument, try to open outside of Emacs, in the
  6243. application the system uses for this file type."
  6244. (interactive "P")
  6245. (org-load-modules-maybe)
  6246. (move-marker org-open-link-marker (point))
  6247. (setq org-window-config-before-follow-link (current-window-configuration))
  6248. (org-remove-occur-highlights nil nil t)
  6249. (cond
  6250. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6251. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6252. (org-footnote-action))
  6253. (t
  6254. (let (type path link line search (pos (point)))
  6255. (catch 'match
  6256. (save-excursion
  6257. (skip-chars-forward "^]\n\r")
  6258. (when (org-in-regexp org-bracket-link-regexp)
  6259. (setq link (org-extract-attributes
  6260. (org-link-unescape (org-match-string-no-properties 1))))
  6261. (while (string-match " *\n *" link)
  6262. (setq link (replace-match " " t t link)))
  6263. (setq link (org-link-expand-abbrev link))
  6264. (cond
  6265. ((or (file-name-absolute-p link)
  6266. (string-match "^\\.\\.?/" link))
  6267. (setq type "file" path link))
  6268. ((string-match org-link-re-with-space3 link)
  6269. (setq type (match-string 1 link) path (match-string 2 link)))
  6270. (t (setq type "thisfile" path link)))
  6271. (throw 'match t)))
  6272. (when (get-text-property (point) 'org-linked-text)
  6273. (setq type "thisfile"
  6274. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6275. (1+ (point)) (point))
  6276. path (buffer-substring
  6277. (previous-single-property-change pos 'org-linked-text)
  6278. (next-single-property-change pos 'org-linked-text)))
  6279. (throw 'match t))
  6280. (save-excursion
  6281. (when (or (org-in-regexp org-angle-link-re)
  6282. (org-in-regexp org-plain-link-re))
  6283. (setq type (match-string 1) path (match-string 2))
  6284. (throw 'match t)))
  6285. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6286. (setq type "tree-match"
  6287. path (match-string 1))
  6288. (throw 'match t))
  6289. (save-excursion
  6290. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6291. (setq type "tags"
  6292. path (match-string 1))
  6293. (while (string-match ":" path)
  6294. (setq path (replace-match "+" t t path)))
  6295. (throw 'match t))))
  6296. (unless path
  6297. (error "No link found"))
  6298. ;; Remove any trailing spaces in path
  6299. (if (string-match " +\\'" path)
  6300. (setq path (replace-match "" t t path)))
  6301. (if (and org-link-translation-function
  6302. (fboundp org-link-translation-function))
  6303. ;; Check if we need to translate the link
  6304. (let ((tmp (funcall org-link-translation-function type path)))
  6305. (setq type (car tmp) path (cdr tmp))))
  6306. (cond
  6307. ((assoc type org-link-protocols)
  6308. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6309. ((equal type "mailto")
  6310. (let ((cmd (car org-link-mailto-program))
  6311. (args (cdr org-link-mailto-program)) args1
  6312. (address path) (subject "") a)
  6313. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6314. (setq address (match-string 1 path)
  6315. subject (org-link-escape (match-string 2 path))))
  6316. (while args
  6317. (cond
  6318. ((not (stringp (car args))) (push (pop args) args1))
  6319. (t (setq a (pop args))
  6320. (if (string-match "%a" a)
  6321. (setq a (replace-match address t t a)))
  6322. (if (string-match "%s" a)
  6323. (setq a (replace-match subject t t a)))
  6324. (push a args1))))
  6325. (apply cmd (nreverse args1))))
  6326. ((member type '("http" "https" "ftp" "news"))
  6327. (browse-url (concat type ":" (org-link-escape
  6328. path org-link-escape-chars-browser))))
  6329. ((member type '("message"))
  6330. (browse-url (concat type ":" path)))
  6331. ((string= type "tags")
  6332. (org-tags-view in-emacs path))
  6333. ((string= type "thisfile")
  6334. (if in-emacs
  6335. (switch-to-buffer-other-window
  6336. (org-get-buffer-for-internal-link (current-buffer)))
  6337. (org-mark-ring-push))
  6338. (let ((cmd `(org-link-search
  6339. ,path
  6340. ,(cond ((equal in-emacs '(4)) 'occur)
  6341. ((equal in-emacs '(16)) 'org-occur)
  6342. (t nil))
  6343. ,pos)))
  6344. (condition-case nil (eval cmd)
  6345. (error (progn (widen) (eval cmd))))))
  6346. ((string= type "tree-match")
  6347. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6348. ((string= type "file")
  6349. (if (string-match "::\\([0-9]+\\)\\'" path)
  6350. (setq line (string-to-number (match-string 1 path))
  6351. path (substring path 0 (match-beginning 0)))
  6352. (if (string-match "::\\(.+\\)\\'" path)
  6353. (setq search (match-string 1 path)
  6354. path (substring path 0 (match-beginning 0)))))
  6355. (if (string-match "[*?{]" (file-name-nondirectory path))
  6356. (dired path)
  6357. (org-open-file path in-emacs line search)))
  6358. ((string= type "news")
  6359. (require 'org-gnus)
  6360. (org-gnus-follow-link path))
  6361. ((string= type "shell")
  6362. (let ((cmd path))
  6363. (if (or (not org-confirm-shell-link-function)
  6364. (funcall org-confirm-shell-link-function
  6365. (format "Execute \"%s\" in shell? "
  6366. (org-add-props cmd nil
  6367. 'face 'org-warning))))
  6368. (progn
  6369. (message "Executing %s" cmd)
  6370. (shell-command cmd))
  6371. (error "Abort"))))
  6372. ((string= type "elisp")
  6373. (let ((cmd path))
  6374. (if (or (not org-confirm-elisp-link-function)
  6375. (funcall org-confirm-elisp-link-function
  6376. (format "Execute \"%s\" as elisp? "
  6377. (org-add-props cmd nil
  6378. 'face 'org-warning))))
  6379. (message "%s => %s" cmd
  6380. (if (equal (string-to-char cmd) ?\()
  6381. (eval (read cmd))
  6382. (call-interactively (read cmd))))
  6383. (error "Abort"))))
  6384. (t
  6385. (browse-url-at-point))))))
  6386. (move-marker org-open-link-marker nil)
  6387. (run-hook-with-args 'org-follow-link-hook))
  6388. ;;;; Time estimates
  6389. (defun org-get-effort (&optional pom)
  6390. "Get the effort estimate for the current entry."
  6391. (org-entry-get pom org-effort-property))
  6392. ;;; File search
  6393. (defvar org-create-file-search-functions nil
  6394. "List of functions to construct the right search string for a file link.
  6395. These functions are called in turn with point at the location to
  6396. which the link should point.
  6397. A function in the hook should first test if it would like to
  6398. handle this file type, for example by checking the major-mode or
  6399. the file extension. If it decides not to handle this file, it
  6400. should just return nil to give other functions a chance. If it
  6401. does handle the file, it must return the search string to be used
  6402. when following the link. The search string will be part of the
  6403. file link, given after a double colon, and `org-open-at-point'
  6404. will automatically search for it. If special measures must be
  6405. taken to make the search successful, another function should be
  6406. added to the companion hook `org-execute-file-search-functions',
  6407. which see.
  6408. A function in this hook may also use `setq' to set the variable
  6409. `description' to provide a suggestion for the descriptive text to
  6410. be used for this link when it gets inserted into an Org-mode
  6411. buffer with \\[org-insert-link].")
  6412. (defvar org-execute-file-search-functions nil
  6413. "List of functions to execute a file search triggered by a link.
  6414. Functions added to this hook must accept a single argument, the
  6415. search string that was part of the file link, the part after the
  6416. double colon. The function must first check if it would like to
  6417. handle this search, for example by checking the major-mode or the
  6418. file extension. If it decides not to handle this search, it
  6419. should just return nil to give other functions a chance. If it
  6420. does handle the search, it must return a non-nil value to keep
  6421. other functions from trying.
  6422. Each function can access the current prefix argument through the
  6423. variable `current-prefix-argument'. Note that a single prefix is
  6424. used to force opening a link in Emacs, so it may be good to only
  6425. use a numeric or double prefix to guide the search function.
  6426. In case this is needed, a function in this hook can also restore
  6427. the window configuration before `org-open-at-point' was called using:
  6428. (set-window-configuration org-window-config-before-follow-link)")
  6429. (defun org-link-search (s &optional type avoid-pos)
  6430. "Search for a link search option.
  6431. If S is surrounded by forward slashes, it is interpreted as a
  6432. regular expression. In org-mode files, this will create an `org-occur'
  6433. sparse tree. In ordinary files, `occur' will be used to list matches.
  6434. If the current buffer is in `dired-mode', grep will be used to search
  6435. in all files. If AVOID-POS is given, ignore matches near that position."
  6436. (let ((case-fold-search t)
  6437. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6438. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6439. (append '(("") (" ") ("\t") ("\n"))
  6440. org-emphasis-alist)
  6441. "\\|") "\\)"))
  6442. (pos (point))
  6443. (pre nil) (post nil)
  6444. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6445. (cond
  6446. ;; First check if there are any special
  6447. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6448. ;; Now try the builtin stuff
  6449. ((save-excursion
  6450. (goto-char (point-min))
  6451. (and
  6452. (re-search-forward
  6453. (concat "<<" (regexp-quote s0) ">>") nil t)
  6454. (setq type 'dedicated
  6455. pos (match-beginning 0))))
  6456. ;; There is an exact target for this
  6457. (goto-char pos))
  6458. ((and (string-match "^((.*))$" s0)
  6459. (save-excursion
  6460. (goto-char (point-min))
  6461. (and
  6462. (re-search-forward (concat "[^[]" (regexp-quote s0)) nil t)
  6463. (setq type 'dedicated
  6464. pos (1+ (match-beginning 0))))))
  6465. ;; There is a coderef target for this
  6466. (goto-char pos))
  6467. ((string-match "^/\\(.*\\)/$" s)
  6468. ;; A regular expression
  6469. (cond
  6470. ((org-mode-p)
  6471. (org-occur (match-string 1 s)))
  6472. ;;((eq major-mode 'dired-mode)
  6473. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6474. (t (org-do-occur (match-string 1 s)))))
  6475. (t
  6476. ;; A normal search strings
  6477. (when (equal (string-to-char s) ?*)
  6478. ;; Anchor on headlines, post may include tags.
  6479. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6480. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6481. s (substring s 1)))
  6482. (remove-text-properties
  6483. 0 (length s)
  6484. '(face nil mouse-face nil keymap nil fontified nil) s)
  6485. ;; Make a series of regular expressions to find a match
  6486. (setq words (org-split-string s "[ \n\r\t]+")
  6487. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6488. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6489. "\\)" markers)
  6490. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6491. re2a (concat "[ \t\r\n]" re2a_)
  6492. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6493. re4 (concat "[^a-zA-Z_]" re4_)
  6494. re1 (concat pre re2 post)
  6495. re3 (concat pre (if pre re4_ re4) post)
  6496. re5 (concat pre ".*" re4)
  6497. re2 (concat pre re2)
  6498. re2a (concat pre (if pre re2a_ re2a))
  6499. re4 (concat pre (if pre re4_ re4))
  6500. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6501. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6502. re5 "\\)"
  6503. ))
  6504. (cond
  6505. ((eq type 'org-occur) (org-occur reall))
  6506. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6507. (t (goto-char (point-min))
  6508. (setq type 'fuzzy)
  6509. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6510. (org-search-not-self 1 re1 nil t)
  6511. (org-search-not-self 1 re2 nil t)
  6512. (org-search-not-self 1 re2a nil t)
  6513. (org-search-not-self 1 re3 nil t)
  6514. (org-search-not-self 1 re4 nil t)
  6515. (org-search-not-self 1 re5 nil t)
  6516. )
  6517. (goto-char (match-beginning 1))
  6518. (goto-char pos)
  6519. (error "No match")))))
  6520. (t
  6521. ;; Normal string-search
  6522. (goto-char (point-min))
  6523. (if (search-forward s nil t)
  6524. (goto-char (match-beginning 0))
  6525. (error "No match"))))
  6526. (and (org-mode-p) (org-show-context 'link-search))
  6527. type))
  6528. (defun org-search-not-self (group &rest args)
  6529. "Execute `re-search-forward', but only accept matches that do not
  6530. enclose the position of `org-open-link-marker'."
  6531. (let ((m org-open-link-marker))
  6532. (catch 'exit
  6533. (while (apply 're-search-forward args)
  6534. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6535. (goto-char (match-end group))
  6536. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6537. (> (match-beginning 0) (marker-position m))
  6538. (< (match-end 0) (marker-position m)))
  6539. (save-match-data
  6540. (or (not (org-in-regexp
  6541. org-bracket-link-analytic-regexp 1))
  6542. (not (match-end 4)) ; no description
  6543. (and (<= (match-beginning 4) (point))
  6544. (>= (match-end 4) (point))))))
  6545. (throw 'exit (point))))))))
  6546. (defun org-get-buffer-for-internal-link (buffer)
  6547. "Return a buffer to be used for displaying the link target of internal links."
  6548. (cond
  6549. ((not org-display-internal-link-with-indirect-buffer)
  6550. buffer)
  6551. ((string-match "(Clone)$" (buffer-name buffer))
  6552. (message "Buffer is already a clone, not making another one")
  6553. ;; we also do not modify visibility in this case
  6554. buffer)
  6555. (t ; make a new indirect buffer for displaying the link
  6556. (let* ((bn (buffer-name buffer))
  6557. (ibn (concat bn "(Clone)"))
  6558. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6559. (with-current-buffer ib (org-overview))
  6560. ib))))
  6561. (defun org-do-occur (regexp &optional cleanup)
  6562. "Call the Emacs command `occur'.
  6563. If CLEANUP is non-nil, remove the printout of the regular expression
  6564. in the *Occur* buffer. This is useful if the regex is long and not useful
  6565. to read."
  6566. (occur regexp)
  6567. (when cleanup
  6568. (let ((cwin (selected-window)) win beg end)
  6569. (when (setq win (get-buffer-window "*Occur*"))
  6570. (select-window win))
  6571. (goto-char (point-min))
  6572. (when (re-search-forward "match[a-z]+" nil t)
  6573. (setq beg (match-end 0))
  6574. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6575. (setq end (1- (match-beginning 0)))))
  6576. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6577. (goto-char (point-min))
  6578. (select-window cwin))))
  6579. ;;; The mark ring for links jumps
  6580. (defvar org-mark-ring nil
  6581. "Mark ring for positions before jumps in Org-mode.")
  6582. (defvar org-mark-ring-last-goto nil
  6583. "Last position in the mark ring used to go back.")
  6584. ;; Fill and close the ring
  6585. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6586. (loop for i from 1 to org-mark-ring-length do
  6587. (push (make-marker) org-mark-ring))
  6588. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6589. org-mark-ring)
  6590. (defun org-mark-ring-push (&optional pos buffer)
  6591. "Put the current position or POS into the mark ring and rotate it."
  6592. (interactive)
  6593. (setq pos (or pos (point)))
  6594. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6595. (move-marker (car org-mark-ring)
  6596. (or pos (point))
  6597. (or buffer (current-buffer)))
  6598. (message "%s"
  6599. (substitute-command-keys
  6600. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6601. (defun org-mark-ring-goto (&optional n)
  6602. "Jump to the previous position in the mark ring.
  6603. With prefix arg N, jump back that many stored positions. When
  6604. called several times in succession, walk through the entire ring.
  6605. Org-mode commands jumping to a different position in the current file,
  6606. or to another Org-mode file, automatically push the old position
  6607. onto the ring."
  6608. (interactive "p")
  6609. (let (p m)
  6610. (if (eq last-command this-command)
  6611. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6612. (setq p org-mark-ring))
  6613. (setq org-mark-ring-last-goto p)
  6614. (setq m (car p))
  6615. (switch-to-buffer (marker-buffer m))
  6616. (goto-char m)
  6617. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6618. (defun org-remove-angle-brackets (s)
  6619. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6620. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6621. s)
  6622. (defun org-add-angle-brackets (s)
  6623. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6624. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6625. s)
  6626. (defun org-remove-double-quotes (s)
  6627. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6628. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6629. s)
  6630. ;;; Following specific links
  6631. (defun org-follow-timestamp-link ()
  6632. (cond
  6633. ((org-at-date-range-p t)
  6634. (let ((org-agenda-start-on-weekday)
  6635. (t1 (match-string 1))
  6636. (t2 (match-string 2)))
  6637. (setq t1 (time-to-days (org-time-string-to-time t1))
  6638. t2 (time-to-days (org-time-string-to-time t2)))
  6639. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6640. ((org-at-timestamp-p t)
  6641. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6642. (substring (match-string 1) 0 10)))
  6643. 1))
  6644. (t (error "This should not happen"))))
  6645. ;;; Following file links
  6646. (defvar org-wait nil)
  6647. (defun org-open-file (path &optional in-emacs line search)
  6648. "Open the file at PATH.
  6649. First, this expands any special file name abbreviations. Then the
  6650. configuration variable `org-file-apps' is checked if it contains an
  6651. entry for this file type, and if yes, the corresponding command is launched.
  6652. If no application is found, Emacs simply visits the file.
  6653. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6654. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6655. and o use an external application to visit the file.
  6656. Optional LINE specifies a line to go to, optional SEARCH a string to
  6657. search for. If LINE or SEARCH is given, the file will always be
  6658. opened in Emacs.
  6659. If the file does not exist, an error is thrown."
  6660. (setq in-emacs (or in-emacs line search))
  6661. (let* ((file (if (equal path "")
  6662. buffer-file-name
  6663. (substitute-in-file-name (expand-file-name path))))
  6664. (apps (append org-file-apps (org-default-apps)))
  6665. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6666. (dirp (if remp nil (file-directory-p file)))
  6667. (file (if (and dirp org-open-directory-means-index-dot-org)
  6668. (concat (file-name-as-directory file) "index.org")
  6669. file))
  6670. (a-m-a-p (assq 'auto-mode apps))
  6671. (dfile (downcase file))
  6672. (old-buffer (current-buffer))
  6673. (old-pos (point))
  6674. (old-mode major-mode)
  6675. ext cmd)
  6676. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6677. (setq ext (match-string 1 dfile))
  6678. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6679. (setq ext (match-string 1 dfile))))
  6680. (cond
  6681. ((equal in-emacs '(16))
  6682. (setq cmd (cdr (assoc 'system apps))))
  6683. (in-emacs (setq cmd 'emacs))
  6684. (t
  6685. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6686. (and dirp (cdr (assoc 'directory apps)))
  6687. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6688. 'string-match)
  6689. (cdr (assoc ext apps))
  6690. (cdr (assoc t apps))))))
  6691. (when (eq cmd 'system)
  6692. (setq cmd (cdr (assoc 'system apps))))
  6693. (when (eq cmd 'default)
  6694. (setq cmd (cdr (assoc t apps))))
  6695. (when (eq cmd 'mailcap)
  6696. (require 'mailcap)
  6697. (mailcap-parse-mailcaps)
  6698. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6699. (command (mailcap-mime-info mime-type)))
  6700. (if (stringp command)
  6701. (setq cmd command)
  6702. (setq cmd 'emacs))))
  6703. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6704. (not (file-exists-p file))
  6705. (not org-open-non-existing-files))
  6706. (error "No such file: %s" file))
  6707. (cond
  6708. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6709. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6710. (while (string-match "['\"]%s['\"]" cmd)
  6711. (setq cmd (replace-match "%s" t t cmd)))
  6712. (while (string-match "%s" cmd)
  6713. (setq cmd (replace-match
  6714. (save-match-data
  6715. (shell-quote-argument
  6716. (convert-standard-filename file)))
  6717. t t cmd)))
  6718. (save-window-excursion
  6719. (start-process-shell-command cmd nil cmd)
  6720. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6721. ))
  6722. ((or (stringp cmd)
  6723. (eq cmd 'emacs))
  6724. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6725. (widen)
  6726. (if line (goto-line line)
  6727. (if search (org-link-search search))))
  6728. ((consp cmd)
  6729. (let ((file (convert-standard-filename file)))
  6730. (eval cmd)))
  6731. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6732. (and (org-mode-p) (eq old-mode 'org-mode)
  6733. (or (not (equal old-buffer (current-buffer)))
  6734. (not (equal old-pos (point))))
  6735. (org-mark-ring-push old-pos old-buffer))))
  6736. (defun org-default-apps ()
  6737. "Return the default applications for this operating system."
  6738. (cond
  6739. ((eq system-type 'darwin)
  6740. org-file-apps-defaults-macosx)
  6741. ((eq system-type 'windows-nt)
  6742. org-file-apps-defaults-windowsnt)
  6743. (t org-file-apps-defaults-gnu)))
  6744. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6745. "Convert extensions to regular expressions in the cars of LIST.
  6746. Also, weed out any non-string entries, because the return value is used
  6747. only for regexp matching.
  6748. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6749. point to the symbol `emacs', indicating that the file should
  6750. be opened in Emacs."
  6751. (append
  6752. (delq nil
  6753. (mapcar (lambda (x)
  6754. (if (not (stringp (car x)))
  6755. nil
  6756. (if (string-match "\\W" (car x))
  6757. x
  6758. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6759. list))
  6760. (if add-auto-mode
  6761. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6762. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6763. (defun org-file-remote-p (file)
  6764. "Test whether FILE specifies a location on a remote system.
  6765. Return non-nil if the location is indeed remote.
  6766. For example, the filename \"/user@host:/foo\" specifies a location
  6767. on the system \"/user@host:\"."
  6768. (cond ((fboundp 'file-remote-p)
  6769. (file-remote-p file))
  6770. ((fboundp 'tramp-handle-file-remote-p)
  6771. (tramp-handle-file-remote-p file))
  6772. ((and (boundp 'ange-ftp-name-format)
  6773. (string-match (car ange-ftp-name-format) file))
  6774. t)
  6775. (t nil)))
  6776. ;;;; Refiling
  6777. (defun org-get-org-file ()
  6778. "Read a filename, with default directory `org-directory'."
  6779. (let ((default (or org-default-notes-file remember-data-file)))
  6780. (read-file-name (format "File name [%s]: " default)
  6781. (file-name-as-directory org-directory)
  6782. default)))
  6783. (defun org-notes-order-reversed-p ()
  6784. "Check if the current file should receive notes in reversed order."
  6785. (cond
  6786. ((not org-reverse-note-order) nil)
  6787. ((eq t org-reverse-note-order) t)
  6788. ((not (listp org-reverse-note-order)) nil)
  6789. (t (catch 'exit
  6790. (let ((all org-reverse-note-order)
  6791. entry)
  6792. (while (setq entry (pop all))
  6793. (if (string-match (car entry) buffer-file-name)
  6794. (throw 'exit (cdr entry))))
  6795. nil)))))
  6796. (defvar org-refile-target-table nil
  6797. "The list of refile targets, created by `org-refile'.")
  6798. (defvar org-agenda-new-buffers nil
  6799. "Buffers created to visit agenda files.")
  6800. (defun org-get-refile-targets (&optional default-buffer)
  6801. "Produce a table with refile targets."
  6802. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6803. targets txt re files f desc descre fast-path-p level)
  6804. (message "Getting targets...")
  6805. (with-current-buffer (or default-buffer (current-buffer))
  6806. (while (setq entry (pop entries))
  6807. (setq files (car entry) desc (cdr entry))
  6808. (setq fast-path-p nil)
  6809. (cond
  6810. ((null files) (setq files (list (current-buffer))))
  6811. ((eq files 'org-agenda-files)
  6812. (setq files (org-agenda-files 'unrestricted)))
  6813. ((and (symbolp files) (fboundp files))
  6814. (setq files (funcall files)))
  6815. ((and (symbolp files) (boundp files))
  6816. (setq files (symbol-value files))))
  6817. (if (stringp files) (setq files (list files)))
  6818. (cond
  6819. ((eq (car desc) :tag)
  6820. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6821. ((eq (car desc) :todo)
  6822. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6823. ((eq (car desc) :regexp)
  6824. (setq descre (cdr desc)))
  6825. ((eq (car desc) :level)
  6826. (setq descre (concat "^\\*\\{" (number-to-string
  6827. (if org-odd-levels-only
  6828. (1- (* 2 (cdr desc)))
  6829. (cdr desc)))
  6830. "\\}[ \t]")))
  6831. ((eq (car desc) :maxlevel)
  6832. (setq fast-path-p t)
  6833. (setq descre (concat "^\\*\\{1," (number-to-string
  6834. (if org-odd-levels-only
  6835. (1- (* 2 (cdr desc)))
  6836. (cdr desc)))
  6837. "\\}[ \t]")))
  6838. (t (error "Bad refiling target description %s" desc)))
  6839. (while (setq f (pop files))
  6840. (save-excursion
  6841. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6842. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6843. (setq f (expand-file-name f))
  6844. (save-excursion
  6845. (save-restriction
  6846. (widen)
  6847. (goto-char (point-min))
  6848. (while (re-search-forward descre nil t)
  6849. (goto-char (point-at-bol))
  6850. (when (looking-at org-complex-heading-regexp)
  6851. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  6852. txt (org-link-display-format (match-string 4))
  6853. re (concat "^" (regexp-quote
  6854. (buffer-substring (match-beginning 1)
  6855. (match-end 4)))))
  6856. (if (match-end 5) (setq re (concat re "[ \t]+"
  6857. (regexp-quote
  6858. (match-string 5)))))
  6859. (setq re (concat re "[ \t]*$"))
  6860. (when org-refile-use-outline-path
  6861. (setq txt (mapconcat 'org-protect-slash
  6862. (append
  6863. (if (eq org-refile-use-outline-path 'file)
  6864. (list (file-name-nondirectory
  6865. (buffer-file-name (buffer-base-buffer))))
  6866. (if (eq org-refile-use-outline-path 'full-file-path)
  6867. (list (buffer-file-name (buffer-base-buffer)))))
  6868. (org-get-outline-path fast-path-p level txt)
  6869. (list txt))
  6870. "/")))
  6871. (push (list txt f re (point)) targets))
  6872. (goto-char (point-at-eol))))))))
  6873. (message "Getting targets...done")
  6874. (nreverse targets))))
  6875. (defun org-protect-slash (s)
  6876. (while (string-match "/" s)
  6877. (setq s (replace-match "\\" t t s)))
  6878. s)
  6879. (defvar org-olpa (make-vector 20 nil))
  6880. (defun org-get-outline-path (&optional fastp level heading)
  6881. "Return the outline path to the current entry, as a list."
  6882. (if fastp
  6883. (progn
  6884. (if (> level 19)
  6885. (error "Outline path failure, more than 19 levels."))
  6886. (loop for i from level upto 19 do
  6887. (aset org-olpa i nil))
  6888. (prog1
  6889. (delq nil (append org-olpa nil))
  6890. (aset org-olpa level heading)))
  6891. (let (rtn)
  6892. (save-excursion
  6893. (while (org-up-heading-safe)
  6894. (when (looking-at org-complex-heading-regexp)
  6895. (push (org-match-string-no-properties 4) rtn)))
  6896. rtn))))
  6897. (defvar org-refile-history nil
  6898. "History for refiling operations.")
  6899. (defun org-refile (&optional goto default-buffer)
  6900. "Move the entry at point to another heading.
  6901. The list of target headings is compiled using the information in
  6902. `org-refile-targets', which see. This list is created before each use
  6903. and will therefore always be up-to-date.
  6904. At the target location, the entry is filed as a subitem of the target heading.
  6905. Depending on `org-reverse-note-order', the new subitem will either be the
  6906. first or the last subitem.
  6907. If there is an active region, all entries in that region will be moved.
  6908. However, the region must fulfil the requirement that the first heading
  6909. is the first one sets the top-level of the moved text - at most siblings
  6910. below it are allowed.
  6911. With prefix arg GOTO, the command will only visit the target location,
  6912. not actually move anything.
  6913. With a double prefix `C-u C-u', go to the location where the last refiling
  6914. operation has put the subtree."
  6915. (interactive "P")
  6916. (let* ((cbuf (current-buffer))
  6917. (regionp (org-region-active-p))
  6918. (region-start (and regionp (region-beginning)))
  6919. (region-end (and regionp (region-end)))
  6920. (region-length (and regionp (- region-end region-start)))
  6921. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6922. pos it nbuf file re level reversed)
  6923. (when regionp (goto-char region-start)
  6924. (unless (org-kill-is-subtree-p
  6925. (buffer-substring region-start region-end))
  6926. (error "The region is not a (sequence of) subtree(s)")))
  6927. (if (equal goto '(16))
  6928. (org-refile-goto-last-stored)
  6929. (when (setq it (org-refile-get-location
  6930. (if goto "Goto: " "Refile to: ") default-buffer))
  6931. (setq file (nth 1 it)
  6932. re (nth 2 it)
  6933. pos (nth 3 it))
  6934. (if (and (equal (buffer-file-name) file)
  6935. (if regionp
  6936. (and (>= pos region-start)
  6937. (<= pos region-end))
  6938. (and (>= pos (point))
  6939. (< pos (save-excursion
  6940. (org-end-of-subtree t t))))))
  6941. (error "Cannot refile to position inside the tree or region"))
  6942. (setq nbuf (or (find-buffer-visiting file)
  6943. (find-file-noselect file)))
  6944. (if goto
  6945. (progn
  6946. (switch-to-buffer nbuf)
  6947. (goto-char pos)
  6948. (org-show-context 'org-goto))
  6949. (if regionp
  6950. (progn
  6951. (kill-new (buffer-substring region-start region-end))
  6952. (org-save-markers-in-region region-start region-end))
  6953. (org-copy-subtree 1 nil t))
  6954. (save-excursion
  6955. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6956. (find-file-noselect file))))
  6957. (setq reversed (org-notes-order-reversed-p))
  6958. (save-excursion
  6959. (save-restriction
  6960. (widen)
  6961. (goto-char pos)
  6962. (looking-at outline-regexp)
  6963. (setq level (org-get-valid-level (funcall outline-level) 1))
  6964. (goto-char
  6965. (if reversed
  6966. (or (outline-next-heading) (point-max))
  6967. (or (save-excursion (outline-get-next-sibling))
  6968. (org-end-of-subtree t t)
  6969. (point-max))))
  6970. (if (not (bolp)) (newline))
  6971. (bookmark-set "org-refile-last-stored")
  6972. (org-paste-subtree level))))
  6973. (if regionp
  6974. (delete-region (point) (+ (point) region-length))
  6975. (org-cut-subtree))
  6976. (setq org-markers-to-move nil)
  6977. (message "Refiled to \"%s\"" (car it)))))))
  6978. (defun org-refile-goto-last-stored ()
  6979. "Go to the location where the last refile was stored."
  6980. (interactive)
  6981. (bookmark-jump "org-refile-last-stored")
  6982. (message "This is the location of the last refile"))
  6983. (defun org-refile-get-location (&optional prompt default-buffer)
  6984. "Prompt the user for a refile location, using PROMPT."
  6985. (let ((org-refile-targets org-refile-targets)
  6986. (org-refile-use-outline-path org-refile-use-outline-path))
  6987. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6988. (unless org-refile-target-table
  6989. (error "No refile targets"))
  6990. (let* ((cbuf (current-buffer))
  6991. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  6992. (cfunc (if (and org-refile-use-outline-path
  6993. org-outline-path-complete-in-steps)
  6994. 'org-olpath-completing-read
  6995. 'org-ido-completing-read))
  6996. (extra (if org-refile-use-outline-path "/" ""))
  6997. (filename (and cfn (expand-file-name cfn)))
  6998. (tbl (mapcar
  6999. (lambda (x)
  7000. (if (not (equal filename (nth 1 x)))
  7001. (cons (concat (car x) extra " ("
  7002. (file-name-nondirectory (nth 1 x)) ")")
  7003. (cdr x))
  7004. (cons (concat (car x) extra) (cdr x))))
  7005. org-refile-target-table))
  7006. (completion-ignore-case t))
  7007. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7008. tbl)))
  7009. (defun org-olpath-completing-read (prompt collection &rest args)
  7010. "Read an outline path like a file name."
  7011. (let ((thetable collection))
  7012. (apply
  7013. 'org-ido-completing-read prompt
  7014. (lambda (string predicate &optional flag)
  7015. (let (rtn r s f (l (length string)))
  7016. (cond
  7017. ((eq flag nil)
  7018. ;; try completion
  7019. (try-completion string thetable))
  7020. ((eq flag t)
  7021. ;; all-completions
  7022. (setq rtn (all-completions string thetable predicate))
  7023. (mapcar
  7024. (lambda (x)
  7025. (setq r (substring x l))
  7026. (if (string-match " ([^)]*)$" x)
  7027. (setq f (match-string 0 x))
  7028. (setq f ""))
  7029. (if (string-match "/" r)
  7030. (concat string (substring r 0 (match-end 0)) f)
  7031. x))
  7032. rtn))
  7033. ((eq flag 'lambda)
  7034. ;; exact match?
  7035. (assoc string thetable)))
  7036. ))
  7037. args)))
  7038. ;;;; Dynamic blocks
  7039. (defun org-find-dblock (name)
  7040. "Find the first dynamic block with name NAME in the buffer.
  7041. If not found, stay at current position and return nil."
  7042. (let (pos)
  7043. (save-excursion
  7044. (goto-char (point-min))
  7045. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7046. nil t)
  7047. (match-beginning 0))))
  7048. (if pos (goto-char pos))
  7049. pos))
  7050. (defconst org-dblock-start-re
  7051. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7052. "Matches the startline of a dynamic block, with parameters.")
  7053. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7054. "Matches the end of a dynamic block.")
  7055. (defun org-create-dblock (plist)
  7056. "Create a dynamic block section, with parameters taken from PLIST.
  7057. PLIST must contain a :name entry which is used as name of the block."
  7058. (unless (bolp) (newline))
  7059. (let ((name (plist-get plist :name)))
  7060. (insert "#+BEGIN: " name)
  7061. (while plist
  7062. (if (eq (car plist) :name)
  7063. (setq plist (cddr plist))
  7064. (insert " " (prin1-to-string (pop plist)))))
  7065. (insert "\n\n#+END:\n")
  7066. (beginning-of-line -2)))
  7067. (defun org-prepare-dblock ()
  7068. "Prepare dynamic block for refresh.
  7069. This empties the block, puts the cursor at the insert position and returns
  7070. the property list including an extra property :name with the block name."
  7071. (unless (looking-at org-dblock-start-re)
  7072. (error "Not at a dynamic block"))
  7073. (let* ((begdel (1+ (match-end 0)))
  7074. (name (org-no-properties (match-string 1)))
  7075. (params (append (list :name name)
  7076. (read (concat "(" (match-string 3) ")")))))
  7077. (unless (re-search-forward org-dblock-end-re nil t)
  7078. (error "Dynamic block not terminated"))
  7079. (setq params
  7080. (append params
  7081. (list :content (buffer-substring
  7082. begdel (match-beginning 0)))))
  7083. (delete-region begdel (match-beginning 0))
  7084. (goto-char begdel)
  7085. (open-line 1)
  7086. params))
  7087. (defun org-map-dblocks (&optional command)
  7088. "Apply COMMAND to all dynamic blocks in the current buffer.
  7089. If COMMAND is not given, use `org-update-dblock'."
  7090. (let ((cmd (or command 'org-update-dblock))
  7091. pos)
  7092. (save-excursion
  7093. (goto-char (point-min))
  7094. (while (re-search-forward org-dblock-start-re nil t)
  7095. (goto-char (setq pos (match-beginning 0)))
  7096. (condition-case nil
  7097. (funcall cmd)
  7098. (error (message "Error during update of dynamic block")))
  7099. (goto-char pos)
  7100. (unless (re-search-forward org-dblock-end-re nil t)
  7101. (error "Dynamic block not terminated"))))))
  7102. (defun org-dblock-update (&optional arg)
  7103. "User command for updating dynamic blocks.
  7104. Update the dynamic block at point. With prefix ARG, update all dynamic
  7105. blocks in the buffer."
  7106. (interactive "P")
  7107. (if arg
  7108. (org-update-all-dblocks)
  7109. (or (looking-at org-dblock-start-re)
  7110. (org-beginning-of-dblock))
  7111. (org-update-dblock)))
  7112. (defun org-update-dblock ()
  7113. "Update the dynamic block at point
  7114. This means to empty the block, parse for parameters and then call
  7115. the correct writing function."
  7116. (save-window-excursion
  7117. (let* ((pos (point))
  7118. (line (org-current-line))
  7119. (params (org-prepare-dblock))
  7120. (name (plist-get params :name))
  7121. (cmd (intern (concat "org-dblock-write:" name))))
  7122. (message "Updating dynamic block `%s' at line %d..." name line)
  7123. (funcall cmd params)
  7124. (message "Updating dynamic block `%s' at line %d...done" name line)
  7125. (goto-char pos))))
  7126. (defun org-beginning-of-dblock ()
  7127. "Find the beginning of the dynamic block at point.
  7128. Error if there is no such block at point."
  7129. (let ((pos (point))
  7130. beg)
  7131. (end-of-line 1)
  7132. (if (and (re-search-backward org-dblock-start-re nil t)
  7133. (setq beg (match-beginning 0))
  7134. (re-search-forward org-dblock-end-re nil t)
  7135. (> (match-end 0) pos))
  7136. (goto-char beg)
  7137. (goto-char pos)
  7138. (error "Not in a dynamic block"))))
  7139. (defun org-update-all-dblocks ()
  7140. "Update all dynamic blocks in the buffer.
  7141. This function can be used in a hook."
  7142. (when (org-mode-p)
  7143. (org-map-dblocks 'org-update-dblock)))
  7144. ;;;; Completion
  7145. (defconst org-additional-option-like-keywords
  7146. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7147. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7148. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7149. "BEGIN_QUOTE" "END_QUOTE"
  7150. "BEGIN_VERSE" "END_VERSE"
  7151. "BEGIN_SRC" "END_SRC"
  7152. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7153. (defcustom org-structure-template-alist
  7154. '(
  7155. ("s" "#+begin_src ?\n\n#+end_src"
  7156. "<src lang=\"?\">\n\n</src>")
  7157. ("e" "#+begin_example\n?\n#+end_example"
  7158. "<example>\n?\n</example>")
  7159. ("q" "#+begin_quote\n?\n#+end_quote"
  7160. "<quote>\n?\n</quote>")
  7161. ("v" "#+begin_verse\n?\n#+end_verse"
  7162. "<verse>\n?\n/verse>")
  7163. ("l" "#+begin_latex\n?\n#+end_latex"
  7164. "<literal style=\"latex\">\n?\n</literal>")
  7165. ("L" "#+latex: "
  7166. "<literal style=\"latex\">?</literal>")
  7167. ("h" "#+begin_html\n?\n#+end_html"
  7168. "<literal style=\"html\">\n?\n</literal>")
  7169. ("H" "#+html: "
  7170. "<literal style=\"html\">?</literal>")
  7171. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7172. ("A" "#+ascii: ")
  7173. ("i" "#+include %file ?"
  7174. "<include file=%file markup=\"?\">")
  7175. )
  7176. "Structure completion elements.
  7177. This is a list of abbreviation keys and values. The value gets inserted
  7178. it you type @samp{.} followed by the key and then the completion key,
  7179. usually `M-TAB'. %file will be replaced by a file name after prompting
  7180. for the file using completion.
  7181. There are two templates for each key, the first uses the original Org syntax,
  7182. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7183. the default when the /org-mtags.el/ module has been loaded. See also the
  7184. variable `org-mtags-prefer-muse-templates'.
  7185. This is an experimental feature, it is undecided if it is going to stay in."
  7186. :group 'org-completion
  7187. :type '(repeat
  7188. (string :tag "Key")
  7189. (string :tag "Template")
  7190. (string :tag "Muse Template")))
  7191. (defun org-try-structure-completion ()
  7192. "Try to complete a structure template before point.
  7193. This looks for strings like \"<e\" on an otherwise empty line and
  7194. expands them."
  7195. (let ((l (buffer-substring (point-at-bol) (point)))
  7196. a)
  7197. (when (and (looking-at "[ \t]*$")
  7198. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7199. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7200. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7201. (match-beginning 1)) a)
  7202. t)))
  7203. (defun org-complete-expand-structure-template (start cell)
  7204. "Expand a structure template."
  7205. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7206. (rpl (nth (if musep 2 1) cell)))
  7207. (delete-region start (point))
  7208. (when (string-match "\\`#\\+" rpl)
  7209. (cond
  7210. ((bolp))
  7211. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7212. (delete-region (point-at-bol) (point)))
  7213. (t (newline))))
  7214. (setq start (point))
  7215. (if (string-match "%file" rpl)
  7216. (setq rpl (replace-match
  7217. (concat
  7218. "\""
  7219. (save-match-data
  7220. (abbreviate-file-name (read-file-name "Include file: ")))
  7221. "\"")
  7222. t t rpl)))
  7223. (insert rpl)
  7224. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7225. (defun org-complete (&optional arg)
  7226. "Perform completion on word at point.
  7227. At the beginning of a headline, this completes TODO keywords as given in
  7228. `org-todo-keywords'.
  7229. If the current word is preceded by a backslash, completes the TeX symbols
  7230. that are supported for HTML support.
  7231. If the current word is preceded by \"#+\", completes special words for
  7232. setting file options.
  7233. In the line after \"#+STARTUP:, complete valid keywords.\"
  7234. At all other locations, this simply calls the value of
  7235. `org-completion-fallback-command'."
  7236. (interactive "P")
  7237. (org-without-partial-completion
  7238. (catch 'exit
  7239. (let* ((a nil)
  7240. (end (point))
  7241. (beg1 (save-excursion
  7242. (skip-chars-backward (org-re "[:alnum:]_@"))
  7243. (point)))
  7244. (beg (save-excursion
  7245. (skip-chars-backward "a-zA-Z0-9_:$")
  7246. (point)))
  7247. (confirm (lambda (x) (stringp (car x))))
  7248. (searchhead (equal (char-before beg) ?*))
  7249. (struct
  7250. (when (and (member (char-before beg1) '(?. ?<))
  7251. (setq a (assoc (buffer-substring beg1 (point))
  7252. org-structure-template-alist)))
  7253. (org-complete-expand-structure-template (1- beg1) a)
  7254. (throw 'exit t)))
  7255. (tag (and (equal (char-before beg1) ?:)
  7256. (equal (char-after (point-at-bol)) ?*)))
  7257. (prop (and (equal (char-before beg1) ?:)
  7258. (not (equal (char-after (point-at-bol)) ?*))))
  7259. (texp (equal (char-before beg) ?\\))
  7260. (link (equal (char-before beg) ?\[))
  7261. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7262. beg)
  7263. "#+"))
  7264. (startup (string-match "^#\\+STARTUP:.*"
  7265. (buffer-substring (point-at-bol) (point))))
  7266. (completion-ignore-case opt)
  7267. (type nil)
  7268. (tbl nil)
  7269. (table (cond
  7270. (opt
  7271. (setq type :opt)
  7272. (require 'org-exp)
  7273. (append
  7274. (mapcar
  7275. (lambda (x)
  7276. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7277. (cons (match-string 2 x) (match-string 1 x)))
  7278. (org-split-string (org-get-current-options) "\n"))
  7279. (mapcar 'list org-additional-option-like-keywords)))
  7280. (startup
  7281. (setq type :startup)
  7282. org-startup-options)
  7283. (link (append org-link-abbrev-alist-local
  7284. org-link-abbrev-alist))
  7285. (texp
  7286. (setq type :tex)
  7287. org-html-entities)
  7288. ((string-match "\\`\\*+[ \t]+\\'"
  7289. (buffer-substring (point-at-bol) beg))
  7290. (setq type :todo)
  7291. (mapcar 'list org-todo-keywords-1))
  7292. (searchhead
  7293. (setq type :searchhead)
  7294. (save-excursion
  7295. (goto-char (point-min))
  7296. (while (re-search-forward org-todo-line-regexp nil t)
  7297. (push (list
  7298. (org-make-org-heading-search-string
  7299. (match-string 3) t))
  7300. tbl)))
  7301. tbl)
  7302. (tag (setq type :tag beg beg1)
  7303. (or org-tag-alist (org-get-buffer-tags)))
  7304. (prop (setq type :prop beg beg1)
  7305. (mapcar 'list (org-buffer-property-keys nil t t)))
  7306. (t (progn
  7307. (call-interactively org-completion-fallback-command)
  7308. (throw 'exit nil)))))
  7309. (pattern (buffer-substring-no-properties beg end))
  7310. (completion (try-completion pattern table confirm)))
  7311. (cond ((eq completion t)
  7312. (if (not (assoc (upcase pattern) table))
  7313. (message "Already complete")
  7314. (if (and (equal type :opt)
  7315. (not (member (car (assoc (upcase pattern) table))
  7316. org-additional-option-like-keywords)))
  7317. (insert (substring (cdr (assoc (upcase pattern) table))
  7318. (length pattern)))
  7319. (if (memq type '(:tag :prop)) (insert ":")))))
  7320. ((null completion)
  7321. (message "Can't find completion for \"%s\"" pattern)
  7322. (ding))
  7323. ((not (string= pattern completion))
  7324. (delete-region beg end)
  7325. (if (string-match " +$" completion)
  7326. (setq completion (replace-match "" t t completion)))
  7327. (insert completion)
  7328. (if (get-buffer-window "*Completions*")
  7329. (delete-window (get-buffer-window "*Completions*")))
  7330. (if (assoc completion table)
  7331. (if (eq type :todo) (insert " ")
  7332. (if (memq type '(:tag :prop)) (insert ":"))))
  7333. (if (and (equal type :opt) (assoc completion table))
  7334. (message "%s" (substitute-command-keys
  7335. "Press \\[org-complete] again to insert example settings"))))
  7336. (t
  7337. (message "Making completion list...")
  7338. (let ((list (sort (all-completions pattern table confirm)
  7339. 'string<)))
  7340. (with-output-to-temp-buffer "*Completions*"
  7341. (condition-case nil
  7342. ;; Protection needed for XEmacs and emacs 21
  7343. (display-completion-list list pattern)
  7344. (error (display-completion-list list)))))
  7345. (message "Making completion list...%s" "done")))))))
  7346. ;;;; TODO, DEADLINE, Comments
  7347. (defun org-toggle-comment ()
  7348. "Change the COMMENT state of an entry."
  7349. (interactive)
  7350. (save-excursion
  7351. (org-back-to-heading)
  7352. (let (case-fold-search)
  7353. (if (looking-at (concat outline-regexp
  7354. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7355. (replace-match "" t t nil 1)
  7356. (if (looking-at outline-regexp)
  7357. (progn
  7358. (goto-char (match-end 0))
  7359. (insert org-comment-string " ")))))))
  7360. (defvar org-last-todo-state-is-todo nil
  7361. "This is non-nil when the last TODO state change led to a TODO state.
  7362. If the last change removed the TODO tag or switched to DONE, then
  7363. this is nil.")
  7364. (defvar org-setting-tags nil) ; dynamically skipped
  7365. (defun org-parse-local-options (string var)
  7366. "Parse STRING for startup setting relevant for variable VAR."
  7367. (let ((rtn (symbol-value var))
  7368. e opts)
  7369. (save-match-data
  7370. (if (or (not string) (not (string-match "\\S-" string)))
  7371. rtn
  7372. (setq opts (delq nil (mapcar (lambda (x)
  7373. (setq e (assoc x org-startup-options))
  7374. (if (eq (nth 1 e) var) e nil))
  7375. (org-split-string string "[ \t]+"))))
  7376. (if (not opts)
  7377. rtn
  7378. (setq rtn nil)
  7379. (while (setq e (pop opts))
  7380. (if (not (nth 3 e))
  7381. (setq rtn (nth 2 e))
  7382. (if (not (listp rtn)) (setq rtn nil))
  7383. (push (nth 2 e) rtn)))
  7384. rtn)))))
  7385. (defvar org-blocker-hook nil
  7386. "Hook for functions that are allowed to block a state change.
  7387. Each function gets as its single argument a property list, see
  7388. `org-trigger-hook' for more information about this list.
  7389. If any of the functions in this hook returns nil, the state change
  7390. is blocked.")
  7391. (defvar org-trigger-hook nil
  7392. "Hook for functions that are triggered by a state change.
  7393. Each function gets as its single argument a property list with at least
  7394. the following elements:
  7395. (:type type-of-change :position pos-at-entry-start
  7396. :from old-state :to new-state)
  7397. Depending on the type, more properties may be present.
  7398. This mechanism is currently implemented for:
  7399. TODO state changes
  7400. ------------------
  7401. :type todo-state-change
  7402. :from previous state (keyword as a string), or nil
  7403. :to new state (keyword as a string), or nil")
  7404. (defvar org-agenda-headline-snapshot-before-repeat)
  7405. (defun org-todo (&optional arg)
  7406. "Change the TODO state of an item.
  7407. The state of an item is given by a keyword at the start of the heading,
  7408. like
  7409. *** TODO Write paper
  7410. *** DONE Call mom
  7411. The different keywords are specified in the variable `org-todo-keywords'.
  7412. By default the available states are \"TODO\" and \"DONE\".
  7413. So for this example: when the item starts with TODO, it is changed to DONE.
  7414. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7415. DONE are present, add TODO at the beginning of the heading.
  7416. With C-u prefix arg, use completion to determine the new state.
  7417. With numeric prefix arg, switch to that state.
  7418. For calling through lisp, arg is also interpreted in the following way:
  7419. 'none -> empty state
  7420. \"\"(empty string) -> switch to empty state
  7421. 'done -> switch to DONE
  7422. 'nextset -> switch to the next set of keywords
  7423. 'previousset -> switch to the previous set of keywords
  7424. \"WAITING\" -> switch to the specified keyword, but only if it
  7425. really is a member of `org-todo-keywords'."
  7426. (interactive "P")
  7427. (save-excursion
  7428. (catch 'exit
  7429. (org-back-to-heading)
  7430. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7431. (or (looking-at (concat " +" org-todo-regexp " *"))
  7432. (looking-at " *"))
  7433. (let* ((match-data (match-data))
  7434. (startpos (point-at-bol))
  7435. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7436. (org-log-done org-log-done)
  7437. (org-log-repeat org-log-repeat)
  7438. (org-todo-log-states org-todo-log-states)
  7439. (this (match-string 1))
  7440. (hl-pos (match-beginning 0))
  7441. (head (org-get-todo-sequence-head this))
  7442. (ass (assoc head org-todo-kwd-alist))
  7443. (interpret (nth 1 ass))
  7444. (done-word (nth 3 ass))
  7445. (final-done-word (nth 4 ass))
  7446. (last-state (or this ""))
  7447. (completion-ignore-case t)
  7448. (member (member this org-todo-keywords-1))
  7449. (tail (cdr member))
  7450. (state (cond
  7451. ((and org-todo-key-trigger
  7452. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7453. (and (not arg) org-use-fast-todo-selection
  7454. (not (eq org-use-fast-todo-selection 'prefix)))))
  7455. ;; Use fast selection
  7456. (org-fast-todo-selection))
  7457. ((and (equal arg '(4))
  7458. (or (not org-use-fast-todo-selection)
  7459. (not org-todo-key-trigger)))
  7460. ;; Read a state with completion
  7461. (org-ido-completing-read "State: " (mapcar (lambda(x) (list x))
  7462. org-todo-keywords-1)
  7463. nil t))
  7464. ((eq arg 'right)
  7465. (if this
  7466. (if tail (car tail) nil)
  7467. (car org-todo-keywords-1)))
  7468. ((eq arg 'left)
  7469. (if (equal member org-todo-keywords-1)
  7470. nil
  7471. (if this
  7472. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7473. org-todo-keywords-1)
  7474. (org-last org-todo-keywords-1))))
  7475. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7476. (setq arg nil))) ; hack to fall back to cycling
  7477. (arg
  7478. ;; user or caller requests a specific state
  7479. (cond
  7480. ((equal arg "") nil)
  7481. ((eq arg 'none) nil)
  7482. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7483. ((eq arg 'nextset)
  7484. (or (car (cdr (member head org-todo-heads)))
  7485. (car org-todo-heads)))
  7486. ((eq arg 'previousset)
  7487. (let ((org-todo-heads (reverse org-todo-heads)))
  7488. (or (car (cdr (member head org-todo-heads)))
  7489. (car org-todo-heads))))
  7490. ((car (member arg org-todo-keywords-1)))
  7491. ((nth (1- (prefix-numeric-value arg))
  7492. org-todo-keywords-1))))
  7493. ((null member) (or head (car org-todo-keywords-1)))
  7494. ((equal this final-done-word) nil) ;; -> make empty
  7495. ((null tail) nil) ;; -> first entry
  7496. ((eq interpret 'sequence)
  7497. (car tail))
  7498. ((memq interpret '(type priority))
  7499. (if (eq this-command last-command)
  7500. (car tail)
  7501. (if (> (length tail) 0)
  7502. (or done-word (car org-done-keywords))
  7503. nil)))
  7504. (t nil)))
  7505. (next (if state (concat " " state " ") " "))
  7506. (change-plist (list :type 'todo-state-change :from this :to state
  7507. :position startpos))
  7508. dolog now-done-p)
  7509. (when org-blocker-hook
  7510. (unless (save-excursion
  7511. (save-match-data
  7512. (run-hook-with-args-until-failure
  7513. 'org-blocker-hook change-plist)))
  7514. (if (interactive-p)
  7515. (error "TODO state change from %s to %s blocked" this state)
  7516. ;; fail silently
  7517. (message "TODO state change from %s to %s blocked" this state)
  7518. (throw 'exit nil))))
  7519. (store-match-data match-data)
  7520. (replace-match next t t)
  7521. (unless (pos-visible-in-window-p hl-pos)
  7522. (message "TODO state changed to %s" (org-trim next)))
  7523. (unless head
  7524. (setq head (org-get-todo-sequence-head state)
  7525. ass (assoc head org-todo-kwd-alist)
  7526. interpret (nth 1 ass)
  7527. done-word (nth 3 ass)
  7528. final-done-word (nth 4 ass)))
  7529. (when (memq arg '(nextset previousset))
  7530. (message "Keyword-Set %d/%d: %s"
  7531. (- (length org-todo-sets) -1
  7532. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7533. (length org-todo-sets)
  7534. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7535. (setq org-last-todo-state-is-todo
  7536. (not (member state org-done-keywords)))
  7537. (setq now-done-p (and (member state org-done-keywords)
  7538. (not (member this org-done-keywords))))
  7539. (and logging (org-local-logging logging))
  7540. (when (and (or org-todo-log-states org-log-done)
  7541. (not (memq arg '(nextset previousset))))
  7542. ;; we need to look at recording a time and note
  7543. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7544. (nth 2 (assoc this org-todo-log-states))))
  7545. (when (and state
  7546. (member state org-not-done-keywords)
  7547. (not (member this org-not-done-keywords)))
  7548. ;; This is now a todo state and was not one before
  7549. ;; If there was a CLOSED time stamp, get rid of it.
  7550. (org-add-planning-info nil nil 'closed))
  7551. (when (and now-done-p org-log-done)
  7552. ;; It is now done, and it was not done before
  7553. (org-add-planning-info 'closed (org-current-time))
  7554. (if (and (not dolog) (eq 'note org-log-done))
  7555. (org-add-log-setup 'done state 'findpos 'note)))
  7556. (when (and state dolog)
  7557. ;; This is a non-nil state, and we need to log it
  7558. (org-add-log-setup 'state state 'findpos dolog)))
  7559. ;; Fixup tag positioning
  7560. (org-todo-trigger-tag-changes state)
  7561. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7562. (when org-provide-todo-statistics
  7563. (org-update-parent-todo-statistics))
  7564. (run-hooks 'org-after-todo-state-change-hook)
  7565. (if (and arg (not (member state org-done-keywords)))
  7566. (setq head (org-get-todo-sequence-head state)))
  7567. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7568. ;; Do we need to trigger a repeat?
  7569. (when now-done-p
  7570. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7571. ;; This is for the agenda, take a snapshot of the headline.
  7572. (save-match-data
  7573. (setq org-agenda-headline-snapshot-before-repeat
  7574. (org-get-heading))))
  7575. (org-auto-repeat-maybe state))
  7576. ;; Fixup cursor location if close to the keyword
  7577. (if (and (outline-on-heading-p)
  7578. (not (bolp))
  7579. (save-excursion (beginning-of-line 1)
  7580. (looking-at org-todo-line-regexp))
  7581. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7582. (progn
  7583. (goto-char (or (match-end 2) (match-end 1)))
  7584. (just-one-space)))
  7585. (when org-trigger-hook
  7586. (save-excursion
  7587. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7588. (defun org-update-parent-todo-statistics ()
  7589. "Update any statistics cookie in the parent of the current headline."
  7590. (interactive)
  7591. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7592. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7593. (catch 'exit
  7594. (save-excursion
  7595. (setq level (org-up-heading-safe))
  7596. (unless (and level
  7597. (re-search-forward box-re (point-at-eol) t))
  7598. (throw 'exit nil))
  7599. (setq is-percent (match-end 2))
  7600. (save-match-data
  7601. (unless (outline-next-heading) (throw 'exit nil))
  7602. (while (looking-at org-todo-line-regexp)
  7603. (setq kwd (match-string 2))
  7604. (and kwd (setq cnt-all (1+ cnt-all)))
  7605. (and (member kwd org-done-keywords)
  7606. (setq cnt-done (1+ cnt-done)))
  7607. (condition-case nil
  7608. (org-forward-same-level 1)
  7609. (error (end-of-line 1)))))
  7610. (replace-match
  7611. (if is-percent
  7612. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7613. (format "[%d/%d]" cnt-done cnt-all)))
  7614. (run-hook-with-args 'org-after-todo-statistics-hook
  7615. cnt-done (- cnt-all cnt-done))))))
  7616. (defvar org-after-todo-statistics-hook nil
  7617. "Hook that is called after a TODO statistics cookie has been updated.
  7618. Each function is called with two arguments: the number of not-done entries
  7619. and the number of done entries.
  7620. For example, the following function, when added to this hook, will switch
  7621. an entry to DONE when all children are done, and back to TODO when new
  7622. entries are set to a TODO status. Note that this hook is only called
  7623. when there is a statistics cookie in the headline!
  7624. (defun org-summary-todo (n-done n-not-done)
  7625. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7626. (let (org-log-done org-log-states) ; turn off logging
  7627. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7628. ")
  7629. (defun org-todo-trigger-tag-changes (state)
  7630. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7631. (let ((l org-todo-state-tags-triggers)
  7632. changes)
  7633. (when (or (not state) (equal state ""))
  7634. (setq changes (append changes (cdr (assoc "" l)))))
  7635. (when (and (stringp state) (> (length state) 0))
  7636. (setq changes (append changes (cdr (assoc state l)))))
  7637. (when (member state org-not-done-keywords)
  7638. (setq changes (append changes (cdr (assoc 'todo l)))))
  7639. (when (member state org-done-keywords)
  7640. (setq changes (append changes (cdr (assoc 'done l)))))
  7641. (dolist (c changes)
  7642. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7643. (defun org-local-logging (value)
  7644. "Get logging settings from a property VALUE."
  7645. (let* (words w a)
  7646. ;; directly set the variables, they are already local.
  7647. (setq org-log-done nil
  7648. org-log-repeat nil
  7649. org-todo-log-states nil)
  7650. (setq words (org-split-string value))
  7651. (while (setq w (pop words))
  7652. (cond
  7653. ((setq a (assoc w org-startup-options))
  7654. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7655. (set (nth 1 a) (nth 2 a))))
  7656. ((setq a (org-extract-log-state-settings w))
  7657. (and (member (car a) org-todo-keywords-1)
  7658. (push a org-todo-log-states)))))))
  7659. (defun org-get-todo-sequence-head (kwd)
  7660. "Return the head of the TODO sequence to which KWD belongs.
  7661. If KWD is not set, check if there is a text property remembering the
  7662. right sequence."
  7663. (let (p)
  7664. (cond
  7665. ((not kwd)
  7666. (or (get-text-property (point-at-bol) 'org-todo-head)
  7667. (progn
  7668. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7669. nil (point-at-eol)))
  7670. (get-text-property p 'org-todo-head))))
  7671. ((not (member kwd org-todo-keywords-1))
  7672. (car org-todo-keywords-1))
  7673. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7674. (defun org-fast-todo-selection ()
  7675. "Fast TODO keyword selection with single keys.
  7676. Returns the new TODO keyword, or nil if no state change should occur."
  7677. (let* ((fulltable org-todo-key-alist)
  7678. (done-keywords org-done-keywords) ;; needed for the faces.
  7679. (maxlen (apply 'max (mapcar
  7680. (lambda (x)
  7681. (if (stringp (car x)) (string-width (car x)) 0))
  7682. fulltable)))
  7683. (expert nil)
  7684. (fwidth (+ maxlen 3 1 3))
  7685. (ncol (/ (- (window-width) 4) fwidth))
  7686. tg cnt e c tbl
  7687. groups ingroup)
  7688. (save-window-excursion
  7689. (if expert
  7690. (set-buffer (get-buffer-create " *Org todo*"))
  7691. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7692. (erase-buffer)
  7693. (org-set-local 'org-done-keywords done-keywords)
  7694. (setq tbl fulltable cnt 0)
  7695. (while (setq e (pop tbl))
  7696. (cond
  7697. ((equal e '(:startgroup))
  7698. (push '() groups) (setq ingroup t)
  7699. (when (not (= cnt 0))
  7700. (setq cnt 0)
  7701. (insert "\n"))
  7702. (insert "{ "))
  7703. ((equal e '(:endgroup))
  7704. (setq ingroup nil cnt 0)
  7705. (insert "}\n"))
  7706. (t
  7707. (setq tg (car e) c (cdr e))
  7708. (if ingroup (push tg (car groups)))
  7709. (setq tg (org-add-props tg nil 'face
  7710. (org-get-todo-face tg)))
  7711. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7712. (insert "[" c "] " tg (make-string
  7713. (- fwidth 4 (length tg)) ?\ ))
  7714. (when (= (setq cnt (1+ cnt)) ncol)
  7715. (insert "\n")
  7716. (if ingroup (insert " "))
  7717. (setq cnt 0)))))
  7718. (insert "\n")
  7719. (goto-char (point-min))
  7720. (if (not expert) (org-fit-window-to-buffer))
  7721. (message "[a-z..]:Set [SPC]:clear")
  7722. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7723. (cond
  7724. ((or (= c ?\C-g)
  7725. (and (= c ?q) (not (rassoc c fulltable))))
  7726. (setq quit-flag t))
  7727. ((= c ?\ ) nil)
  7728. ((setq e (rassoc c fulltable) tg (car e))
  7729. tg)
  7730. (t (setq quit-flag t))))))
  7731. (defun org-entry-is-todo-p ()
  7732. (member (org-get-todo-state) org-not-done-keywords))
  7733. (defun org-entry-is-done-p ()
  7734. (member (org-get-todo-state) org-done-keywords))
  7735. (defun org-get-todo-state ()
  7736. (save-excursion
  7737. (org-back-to-heading t)
  7738. (and (looking-at org-todo-line-regexp)
  7739. (match-end 2)
  7740. (match-string 2))))
  7741. (defun org-at-date-range-p (&optional inactive-ok)
  7742. "Is the cursor inside a date range?"
  7743. (interactive)
  7744. (save-excursion
  7745. (catch 'exit
  7746. (let ((pos (point)))
  7747. (skip-chars-backward "^[<\r\n")
  7748. (skip-chars-backward "<[")
  7749. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7750. (>= (match-end 0) pos)
  7751. (throw 'exit t))
  7752. (skip-chars-backward "^<[\r\n")
  7753. (skip-chars-backward "<[")
  7754. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7755. (>= (match-end 0) pos)
  7756. (throw 'exit t)))
  7757. nil)))
  7758. (defun org-get-repeat ()
  7759. "Check if there is a deadline/schedule with repeater in this entry."
  7760. (save-match-data
  7761. (save-excursion
  7762. (org-back-to-heading t)
  7763. (if (re-search-forward
  7764. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7765. (match-string 1)))))
  7766. (defvar org-last-changed-timestamp)
  7767. (defvar org-last-inserted-timestamp)
  7768. (defvar org-log-post-message)
  7769. (defvar org-log-note-purpose)
  7770. (defvar org-log-note-how)
  7771. (defvar org-log-note-extra)
  7772. (defun org-auto-repeat-maybe (done-word)
  7773. "Check if the current headline contains a repeated deadline/schedule.
  7774. If yes, set TODO state back to what it was and change the base date
  7775. of repeating deadline/scheduled time stamps to new date.
  7776. This function is run automatically after each state change to a DONE state."
  7777. ;; last-state is dynamically scoped into this function
  7778. (let* ((repeat (org-get-repeat))
  7779. (aa (assoc last-state org-todo-kwd-alist))
  7780. (interpret (nth 1 aa))
  7781. (head (nth 2 aa))
  7782. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7783. (msg "Entry repeats: ")
  7784. (org-log-done nil)
  7785. (org-todo-log-states nil)
  7786. (nshiftmax 10) (nshift 0)
  7787. re type n what ts mb0 time)
  7788. (when repeat
  7789. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7790. (org-todo (if (eq interpret 'type) last-state head))
  7791. (when org-log-repeat
  7792. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7793. (memq 'org-add-log-note post-command-hook))
  7794. ;; OK, we are already setup for some record
  7795. (if (eq org-log-repeat 'note)
  7796. ;; make sure we take a note, not only a time stamp
  7797. (setq org-log-note-how 'note))
  7798. ;; Set up for taking a record
  7799. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7800. 'findpos org-log-repeat)))
  7801. (org-back-to-heading t)
  7802. (org-add-planning-info nil nil 'closed)
  7803. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7804. org-deadline-time-regexp "\\)\\|\\("
  7805. org-ts-regexp "\\)"))
  7806. (while (re-search-forward
  7807. re (save-excursion (outline-next-heading) (point)) t)
  7808. (setq type (if (match-end 1) org-scheduled-string
  7809. (if (match-end 3) org-deadline-string "Plain:"))
  7810. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7811. mb0 (match-beginning 0))
  7812. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7813. (setq n (string-to-number (match-string 2 ts))
  7814. what (match-string 3 ts))
  7815. (if (equal what "w") (setq n (* n 7) what "d"))
  7816. ;; Preparation, see if we need to modify the start date for the change
  7817. (when (match-end 1)
  7818. (setq time (save-match-data (org-time-string-to-time ts)))
  7819. (cond
  7820. ((equal (match-string 1 ts) ".")
  7821. ;; Shift starting date to today
  7822. (org-timestamp-change
  7823. (- (time-to-days (current-time)) (time-to-days time))
  7824. 'day))
  7825. ((equal (match-string 1 ts) "+")
  7826. (while (or (= nshift 0)
  7827. (<= (time-to-days time) (time-to-days (current-time))))
  7828. (when (= (incf nshift) nshiftmax)
  7829. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7830. (error "Abort")))
  7831. (org-timestamp-change n (cdr (assoc what whata)))
  7832. (org-at-timestamp-p t)
  7833. (setq ts (match-string 1))
  7834. (setq time (save-match-data (org-time-string-to-time ts))))
  7835. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7836. ;; rematch, so that we have everything in place for the real shift
  7837. (org-at-timestamp-p t)
  7838. (setq ts (match-string 1))
  7839. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7840. (org-timestamp-change n (cdr (assoc what whata)))
  7841. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7842. (setq org-log-post-message msg)
  7843. (message "%s" msg))))
  7844. (defun org-show-todo-tree (arg)
  7845. "Make a compact tree which shows all headlines marked with TODO.
  7846. The tree will show the lines where the regexp matches, and all higher
  7847. headlines above the match.
  7848. With a \\[universal-argument] prefix, also show the DONE entries.
  7849. With a numeric prefix N, construct a sparse tree for the Nth element
  7850. of `org-todo-keywords-1'."
  7851. (interactive "P")
  7852. (let ((case-fold-search nil)
  7853. (kwd-re
  7854. (cond ((null arg) org-not-done-regexp)
  7855. ((equal arg '(4))
  7856. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  7857. (mapcar 'list org-todo-keywords-1))))
  7858. (concat "\\("
  7859. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7860. "\\)\\>")))
  7861. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7862. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7863. org-todo-keywords-1)))
  7864. (t (error "Invalid prefix argument: %s" arg)))))
  7865. (message "%d TODO entries found"
  7866. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7867. (defun org-deadline (&optional remove time)
  7868. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7869. With argument REMOVE, remove any deadline from the item.
  7870. When TIME is set, it should be an internal time specification, and the
  7871. scheduling will use the corresponding date."
  7872. (interactive "P")
  7873. (if remove
  7874. (progn
  7875. (org-remove-timestamp-with-keyword org-deadline-string)
  7876. (message "Item no longer has a deadline."))
  7877. (if (org-get-repeat)
  7878. (error "Cannot change deadline on task with repeater, please do that by hand")
  7879. (org-add-planning-info 'deadline time 'closed)
  7880. (message "Deadline on %s" org-last-inserted-timestamp))))
  7881. (defun org-schedule (&optional remove time)
  7882. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7883. With argument REMOVE, remove any scheduling date from the item.
  7884. When TIME is set, it should be an internal time specification, and the
  7885. scheduling will use the corresponding date."
  7886. (interactive "P")
  7887. (if remove
  7888. (progn
  7889. (org-remove-timestamp-with-keyword org-scheduled-string)
  7890. (message "Item is no longer scheduled."))
  7891. (if (org-get-repeat)
  7892. (error "Cannot reschedule task with repeater, please do that by hand")
  7893. (org-add-planning-info 'scheduled time 'closed)
  7894. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7895. (defun org-remove-timestamp-with-keyword (keyword)
  7896. "Remove all time stamps with KEYWORD in the current entry."
  7897. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7898. beg)
  7899. (save-excursion
  7900. (org-back-to-heading t)
  7901. (setq beg (point))
  7902. (org-end-of-subtree t t)
  7903. (while (re-search-backward re beg t)
  7904. (replace-match "")
  7905. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7906. (equal (char-before) ?\ ))
  7907. (backward-delete-char 1)
  7908. (if (string-match "^[ \t]*$" (buffer-substring
  7909. (point-at-bol) (point-at-eol)))
  7910. (delete-region (point-at-bol)
  7911. (min (point-max) (1+ (point-at-eol))))))))))
  7912. (defun org-add-planning-info (what &optional time &rest remove)
  7913. "Insert new timestamp with keyword in the line directly after the headline.
  7914. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7915. If non is given, the user is prompted for a date.
  7916. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7917. be removed."
  7918. (interactive)
  7919. (let (org-time-was-given org-end-time-was-given ts
  7920. end default-time default-input)
  7921. (when (and (not time) (memq what '(scheduled deadline)))
  7922. ;; Try to get a default date/time from existing timestamp
  7923. (save-excursion
  7924. (org-back-to-heading t)
  7925. (setq end (save-excursion (outline-next-heading) (point)))
  7926. (when (re-search-forward (if (eq what 'scheduled)
  7927. org-scheduled-time-regexp
  7928. org-deadline-time-regexp)
  7929. end t)
  7930. (setq ts (match-string 1)
  7931. default-time
  7932. (apply 'encode-time (org-parse-time-string ts))
  7933. default-input (and ts (org-get-compact-tod ts))))))
  7934. (when what
  7935. ;; If necessary, get the time from the user
  7936. (setq time (or time (org-read-date nil 'to-time nil nil
  7937. default-time default-input))))
  7938. (when (and org-insert-labeled-timestamps-at-point
  7939. (member what '(scheduled deadline)))
  7940. (insert
  7941. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7942. (org-insert-time-stamp time org-time-was-given
  7943. nil nil nil (list org-end-time-was-given))
  7944. (setq what nil))
  7945. (save-excursion
  7946. (save-restriction
  7947. (let (col list elt ts buffer-invisibility-spec)
  7948. (org-back-to-heading t)
  7949. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7950. (goto-char (match-end 1))
  7951. (setq col (current-column))
  7952. (goto-char (match-end 0))
  7953. (if (eobp) (insert "\n") (forward-char 1))
  7954. (if (and (not (looking-at outline-regexp))
  7955. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7956. "[^\r\n]*"))
  7957. (not (equal (match-string 1) org-clock-string)))
  7958. (narrow-to-region (match-beginning 0) (match-end 0))
  7959. (insert-before-markers "\n")
  7960. (backward-char 1)
  7961. (narrow-to-region (point) (point))
  7962. (and org-adapt-indentation (org-indent-to-column col)))
  7963. ;; Check if we have to remove something.
  7964. (setq list (cons what remove))
  7965. (while list
  7966. (setq elt (pop list))
  7967. (goto-char (point-min))
  7968. (when (or (and (eq elt 'scheduled)
  7969. (re-search-forward org-scheduled-time-regexp nil t))
  7970. (and (eq elt 'deadline)
  7971. (re-search-forward org-deadline-time-regexp nil t))
  7972. (and (eq elt 'closed)
  7973. (re-search-forward org-closed-time-regexp nil t)))
  7974. (replace-match "")
  7975. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7976. (if (looking-at " +") (replace-match ""))))
  7977. (goto-char (point-max))
  7978. (when what
  7979. (insert
  7980. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7981. (cond ((eq what 'scheduled) org-scheduled-string)
  7982. ((eq what 'deadline) org-deadline-string)
  7983. ((eq what 'closed) org-closed-string))
  7984. " ")
  7985. (setq ts (org-insert-time-stamp
  7986. time
  7987. (or org-time-was-given
  7988. (and (eq what 'closed) org-log-done-with-time))
  7989. (eq what 'closed)
  7990. nil nil (list org-end-time-was-given)))
  7991. (end-of-line 1))
  7992. (goto-char (point-min))
  7993. (widen)
  7994. (if (and (looking-at "[ \t]+\n")
  7995. (equal (char-before) ?\n))
  7996. (delete-region (1- (point)) (point-at-eol)))
  7997. ts)))))
  7998. (defvar org-log-note-marker (make-marker))
  7999. (defvar org-log-note-purpose nil)
  8000. (defvar org-log-note-state nil)
  8001. (defvar org-log-note-how nil)
  8002. (defvar org-log-note-extra nil)
  8003. (defvar org-log-note-window-configuration nil)
  8004. (defvar org-log-note-return-to (make-marker))
  8005. (defvar org-log-post-message nil
  8006. "Message to be displayed after a log note has been stored.
  8007. The auto-repeater uses this.")
  8008. (defun org-add-note ()
  8009. "Add a note to the current entry.
  8010. This is done in the same way as adding a state change note."
  8011. (interactive)
  8012. (org-add-log-setup 'note nil 'findpos nil))
  8013. (defvar org-property-end-re)
  8014. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  8015. "Set up the post command hook to take a note.
  8016. If this is about to TODO state change, the new state is expected in STATE.
  8017. When FINDPOS is non-nil, find the correct position for the note in
  8018. the current entry. If not, assume that it can be inserted at point.
  8019. HOW is an indicator what kind of note should be created.
  8020. EXTRA is additional text that will be inserted into the notes buffer."
  8021. (save-restriction
  8022. (save-excursion
  8023. (when findpos
  8024. (org-back-to-heading t)
  8025. (narrow-to-region (point) (save-excursion
  8026. (outline-next-heading) (point)))
  8027. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8028. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8029. "[^\r\n]*\\)?"))
  8030. (goto-char (match-end 0))
  8031. (when (and org-log-state-notes-insert-after-drawers
  8032. (save-excursion
  8033. (forward-line) (looking-at org-drawer-regexp)))
  8034. (progn (forward-line)
  8035. (while (looking-at org-drawer-regexp)
  8036. (goto-char (match-end 0))
  8037. (re-search-forward org-property-end-re (point-max) t)
  8038. (forward-line))
  8039. (forward-line -1)))
  8040. (unless org-log-states-order-reversed
  8041. (and (= (char-after) ?\n) (forward-char 1))
  8042. (org-skip-over-state-notes)
  8043. (skip-chars-backward " \t\n\r")))
  8044. (move-marker org-log-note-marker (point))
  8045. (setq org-log-note-purpose purpose
  8046. org-log-note-state state
  8047. org-log-note-how how
  8048. org-log-note-extra extra)
  8049. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  8050. (defun org-skip-over-state-notes ()
  8051. "Skip past the list of State notes in an entry."
  8052. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8053. (while (looking-at "[ \t]*- State")
  8054. (condition-case nil
  8055. (org-next-item)
  8056. (error (org-end-of-item)))))
  8057. (defun org-add-log-note (&optional purpose)
  8058. "Pop up a window for taking a note, and add this note later at point."
  8059. (remove-hook 'post-command-hook 'org-add-log-note)
  8060. (setq org-log-note-window-configuration (current-window-configuration))
  8061. (delete-other-windows)
  8062. (move-marker org-log-note-return-to (point))
  8063. (switch-to-buffer (marker-buffer org-log-note-marker))
  8064. (goto-char org-log-note-marker)
  8065. (org-switch-to-buffer-other-window "*Org Note*")
  8066. (erase-buffer)
  8067. (if (memq org-log-note-how '(time state))
  8068. (let (current-prefix-arg) (org-store-log-note))
  8069. (let ((org-inhibit-startup t)) (org-mode))
  8070. (insert (format "# Insert note for %s.
  8071. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8072. (cond
  8073. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8074. ((eq org-log-note-purpose 'done) "closed todo item")
  8075. ((eq org-log-note-purpose 'state)
  8076. (format "state change to \"%s\"" org-log-note-state))
  8077. ((eq org-log-note-purpose 'note)
  8078. "this entry")
  8079. (t (error "This should not happen")))))
  8080. (if org-log-note-extra (insert org-log-note-extra))
  8081. (org-set-local 'org-finish-function 'org-store-log-note)))
  8082. (defvar org-note-abort nil) ; dynamically scoped
  8083. (defun org-store-log-note ()
  8084. "Finish taking a log note, and insert it to where it belongs."
  8085. (let ((txt (buffer-string))
  8086. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8087. lines ind)
  8088. (kill-buffer (current-buffer))
  8089. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8090. (setq txt (replace-match "" t t txt)))
  8091. (if (string-match "\\s-+\\'" txt)
  8092. (setq txt (replace-match "" t t txt)))
  8093. (setq lines (org-split-string txt "\n"))
  8094. (when (and note (string-match "\\S-" note))
  8095. (setq note
  8096. (org-replace-escapes
  8097. note
  8098. (list (cons "%u" (user-login-name))
  8099. (cons "%U" user-full-name)
  8100. (cons "%t" (format-time-string
  8101. (org-time-stamp-format 'long 'inactive)
  8102. (current-time)))
  8103. (cons "%s" (if org-log-note-state
  8104. (concat "\"" org-log-note-state "\"")
  8105. "")))))
  8106. (if lines (setq note (concat note " \\\\")))
  8107. (push note lines))
  8108. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8109. (when lines
  8110. (save-excursion
  8111. (set-buffer (marker-buffer org-log-note-marker))
  8112. (save-excursion
  8113. (goto-char org-log-note-marker)
  8114. (move-marker org-log-note-marker nil)
  8115. (end-of-line 1)
  8116. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8117. (indent-relative nil)
  8118. (insert "- " (pop lines))
  8119. (org-indent-line-function)
  8120. (beginning-of-line 1)
  8121. (looking-at "[ \t]*")
  8122. (setq ind (concat (match-string 0) " "))
  8123. (end-of-line 1)
  8124. (while lines (insert "\n" ind (pop lines)))))))
  8125. (set-window-configuration org-log-note-window-configuration)
  8126. (with-current-buffer (marker-buffer org-log-note-return-to)
  8127. (goto-char org-log-note-return-to))
  8128. (move-marker org-log-note-return-to nil)
  8129. (and org-log-post-message (message "%s" org-log-post-message)))
  8130. (defun org-sparse-tree (&optional arg)
  8131. "Create a sparse tree, prompt for the details.
  8132. This command can create sparse trees. You first need to select the type
  8133. of match used to create the tree:
  8134. t Show entries with a specific TODO keyword.
  8135. T Show entries selected by a tags match.
  8136. p Enter a property name and its value (both with completion on existing
  8137. names/values) and show entries with that property.
  8138. r Show entries matching a regular expression
  8139. d Show deadlines due within `org-deadline-warning-days'."
  8140. (interactive "P")
  8141. (let (ans kwd value)
  8142. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8143. (setq ans (read-char-exclusive))
  8144. (cond
  8145. ((equal ans ?d)
  8146. (call-interactively 'org-check-deadlines))
  8147. ((equal ans ?b)
  8148. (call-interactively 'org-check-before-date))
  8149. ((equal ans ?t)
  8150. (org-show-todo-tree '(4)))
  8151. ((equal ans ?T)
  8152. (call-interactively 'org-tags-sparse-tree))
  8153. ((member ans '(?p ?P))
  8154. (setq kwd (org-ido-completing-read "Property: "
  8155. (mapcar 'list (org-buffer-property-keys))))
  8156. (setq value (org-ido-completing-read "Value: "
  8157. (mapcar 'list (org-property-values kwd))))
  8158. (unless (string-match "\\`{.*}\\'" value)
  8159. (setq value (concat "\"" value "\"")))
  8160. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8161. ((member ans '(?r ?R ?/))
  8162. (call-interactively 'org-occur))
  8163. (t (error "No such sparse tree command \"%c\"" ans)))))
  8164. (defvar org-occur-highlights nil
  8165. "List of overlays used for occur matches.")
  8166. (make-variable-buffer-local 'org-occur-highlights)
  8167. (defvar org-occur-parameters nil
  8168. "Parameters of the active org-occur calls.
  8169. This is a list, each call to org-occur pushes as cons cell,
  8170. containing the regular expression and the callback, onto the list.
  8171. The list can contain several entries if `org-occur' has been called
  8172. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8173. will only contain one set of parameters. When the highlights are
  8174. removed (for example with `C-c C-c', or with the next edit (depending
  8175. on `org-remove-highlights-with-change'), this variable is emptied
  8176. as well.")
  8177. (make-variable-buffer-local 'org-occur-parameters)
  8178. (defun org-occur (regexp &optional keep-previous callback)
  8179. "Make a compact tree which shows all matches of REGEXP.
  8180. The tree will show the lines where the regexp matches, and all higher
  8181. headlines above the match. It will also show the heading after the match,
  8182. to make sure editing the matching entry is easy.
  8183. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8184. call to `org-occur' will be kept, to allow stacking of calls to this
  8185. command.
  8186. If CALLBACK is non-nil, it is a function which is called to confirm
  8187. that the match should indeed be shown."
  8188. (interactive "sRegexp: \nP")
  8189. (unless keep-previous
  8190. (org-remove-occur-highlights nil nil t))
  8191. (push (cons regexp callback) org-occur-parameters)
  8192. (let ((cnt 0))
  8193. (save-excursion
  8194. (goto-char (point-min))
  8195. (if (or (not keep-previous) ; do not want to keep
  8196. (not org-occur-highlights)) ; no previous matches
  8197. ;; hide everything
  8198. (org-overview))
  8199. (while (re-search-forward regexp nil t)
  8200. (when (or (not callback)
  8201. (save-match-data (funcall callback)))
  8202. (setq cnt (1+ cnt))
  8203. (when org-highlight-sparse-tree-matches
  8204. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8205. (org-show-context 'occur-tree))))
  8206. (when org-remove-highlights-with-change
  8207. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8208. nil 'local))
  8209. (unless org-sparse-tree-open-archived-trees
  8210. (org-hide-archived-subtrees (point-min) (point-max)))
  8211. (run-hooks 'org-occur-hook)
  8212. (if (interactive-p)
  8213. (message "%d match(es) for regexp %s" cnt regexp))
  8214. cnt))
  8215. (defun org-show-context (&optional key)
  8216. "Make sure point and context and visible.
  8217. How much context is shown depends upon the variables
  8218. `org-show-hierarchy-above', `org-show-following-heading'. and
  8219. `org-show-siblings'."
  8220. (let ((heading-p (org-on-heading-p t))
  8221. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8222. (following-p (org-get-alist-option org-show-following-heading key))
  8223. (entry-p (org-get-alist-option org-show-entry-below key))
  8224. (siblings-p (org-get-alist-option org-show-siblings key)))
  8225. (catch 'exit
  8226. ;; Show heading or entry text
  8227. (if (and heading-p (not entry-p))
  8228. (org-flag-heading nil) ; only show the heading
  8229. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8230. (org-show-hidden-entry))) ; show entire entry
  8231. (when following-p
  8232. ;; Show next sibling, or heading below text
  8233. (save-excursion
  8234. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8235. (org-flag-heading nil))))
  8236. (when siblings-p (org-show-siblings))
  8237. (when hierarchy-p
  8238. ;; show all higher headings, possibly with siblings
  8239. (save-excursion
  8240. (while (and (condition-case nil
  8241. (progn (org-up-heading-all 1) t)
  8242. (error nil))
  8243. (not (bobp)))
  8244. (org-flag-heading nil)
  8245. (when siblings-p (org-show-siblings))))))))
  8246. (defun org-reveal (&optional siblings)
  8247. "Show current entry, hierarchy above it, and the following headline.
  8248. This can be used to show a consistent set of context around locations
  8249. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8250. not t for the search context.
  8251. With optional argument SIBLINGS, on each level of the hierarchy all
  8252. siblings are shown. This repairs the tree structure to what it would
  8253. look like when opened with hierarchical calls to `org-cycle'."
  8254. (interactive "P")
  8255. (let ((org-show-hierarchy-above t)
  8256. (org-show-following-heading t)
  8257. (org-show-siblings (if siblings t org-show-siblings)))
  8258. (org-show-context nil)))
  8259. (defun org-highlight-new-match (beg end)
  8260. "Highlight from BEG to END and mark the highlight is an occur headline."
  8261. (let ((ov (org-make-overlay beg end)))
  8262. (org-overlay-put ov 'face 'secondary-selection)
  8263. (push ov org-occur-highlights)))
  8264. (defun org-remove-occur-highlights (&optional beg end noremove)
  8265. "Remove the occur highlights from the buffer.
  8266. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8267. from the `before-change-functions' in the current buffer."
  8268. (interactive)
  8269. (unless org-inhibit-highlight-removal
  8270. (mapc 'org-delete-overlay org-occur-highlights)
  8271. (setq org-occur-highlights nil)
  8272. (setq org-occur-parameters nil)
  8273. (unless noremove
  8274. (remove-hook 'before-change-functions
  8275. 'org-remove-occur-highlights 'local))))
  8276. ;;;; Priorities
  8277. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8278. "Regular expression matching the priority indicator.")
  8279. (defvar org-remove-priority-next-time nil)
  8280. (defun org-priority-up ()
  8281. "Increase the priority of the current item."
  8282. (interactive)
  8283. (org-priority 'up))
  8284. (defun org-priority-down ()
  8285. "Decrease the priority of the current item."
  8286. (interactive)
  8287. (org-priority 'down))
  8288. (defun org-priority (&optional action)
  8289. "Change the priority of an item by ARG.
  8290. ACTION can be `set', `up', `down', or a character."
  8291. (interactive)
  8292. (setq action (or action 'set))
  8293. (let (current new news have remove)
  8294. (save-excursion
  8295. (org-back-to-heading)
  8296. (if (looking-at org-priority-regexp)
  8297. (setq current (string-to-char (match-string 2))
  8298. have t)
  8299. (setq current org-default-priority))
  8300. (cond
  8301. ((or (eq action 'set)
  8302. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8303. (if (not (eq action 'set))
  8304. (setq new action)
  8305. (message "Priority %c-%c, SPC to remove: "
  8306. org-highest-priority org-lowest-priority)
  8307. (setq new (read-char-exclusive)))
  8308. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8309. (= (upcase org-lowest-priority) org-lowest-priority))
  8310. (setq new (upcase new)))
  8311. (cond ((equal new ?\ ) (setq remove t))
  8312. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8313. (error "Priority must be between `%c' and `%c'"
  8314. org-highest-priority org-lowest-priority))))
  8315. ((eq action 'up)
  8316. (if (and (not have) (eq last-command this-command))
  8317. (setq new org-lowest-priority)
  8318. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8319. org-default-priority (1- current)))))
  8320. ((eq action 'down)
  8321. (if (and (not have) (eq last-command this-command))
  8322. (setq new org-highest-priority)
  8323. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8324. org-default-priority (1+ current)))))
  8325. (t (error "Invalid action")))
  8326. (if (or (< (upcase new) org-highest-priority)
  8327. (> (upcase new) org-lowest-priority))
  8328. (setq remove t))
  8329. (setq news (format "%c" new))
  8330. (if have
  8331. (if remove
  8332. (replace-match "" t t nil 1)
  8333. (replace-match news t t nil 2))
  8334. (if remove
  8335. (error "No priority cookie found in line")
  8336. (looking-at org-todo-line-regexp)
  8337. (if (match-end 2)
  8338. (progn
  8339. (goto-char (match-end 2))
  8340. (insert " [#" news "]"))
  8341. (goto-char (match-beginning 3))
  8342. (insert "[#" news "] ")))))
  8343. (org-preserve-lc (org-set-tags nil 'align))
  8344. (if remove
  8345. (message "Priority removed")
  8346. (message "Priority of current item set to %s" news))))
  8347. (defun org-get-priority (s)
  8348. "Find priority cookie and return priority."
  8349. (save-match-data
  8350. (if (not (string-match org-priority-regexp s))
  8351. (* 1000 (- org-lowest-priority org-default-priority))
  8352. (* 1000 (- org-lowest-priority
  8353. (string-to-char (match-string 2 s)))))))
  8354. ;;;; Tags
  8355. (defvar org-agenda-archives-mode)
  8356. (defun org-scan-tags (action matcher &optional todo-only)
  8357. "Scan headline tags with inheritance and produce output ACTION.
  8358. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8359. or `agenda' to produce an entry list for an agenda view. It can also be
  8360. a Lisp form or a function that should be called at each matched headline, in
  8361. this case the return value is a list of all return values from these calls.
  8362. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8363. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8364. only lines with a TODO keyword are included in the output."
  8365. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8366. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8367. (org-re
  8368. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8369. (props (list 'face 'default
  8370. 'done-face 'org-done
  8371. 'undone-face 'default
  8372. 'mouse-face 'highlight
  8373. 'org-not-done-regexp org-not-done-regexp
  8374. 'org-todo-regexp org-todo-regexp
  8375. 'keymap org-agenda-keymap
  8376. 'help-echo
  8377. (format "mouse-2 or RET jump to org file %s"
  8378. (abbreviate-file-name
  8379. (or (buffer-file-name (buffer-base-buffer))
  8380. (buffer-name (buffer-base-buffer)))))))
  8381. (case-fold-search nil)
  8382. lspos tags tags-list
  8383. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8384. (llast 0) rtn rtn1 level category i txt
  8385. todo marker entry priority)
  8386. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8387. (setq action (list 'lambda nil action)))
  8388. (save-excursion
  8389. (goto-char (point-min))
  8390. (when (eq action 'sparse-tree)
  8391. (org-overview)
  8392. (org-remove-occur-highlights))
  8393. (while (re-search-forward re nil t)
  8394. (catch :skip
  8395. (setq todo (if (match-end 1) (match-string 2))
  8396. tags (if (match-end 4) (match-string 4)))
  8397. (goto-char (setq lspos (1+ (match-beginning 0))))
  8398. (setq level (org-reduced-level (funcall outline-level))
  8399. category (org-get-category))
  8400. (setq i llast llast level)
  8401. ;; remove tag lists from same and sublevels
  8402. (while (>= i level)
  8403. (when (setq entry (assoc i tags-alist))
  8404. (setq tags-alist (delete entry tags-alist)))
  8405. (setq i (1- i)))
  8406. ;; add the next tags
  8407. (when tags
  8408. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8409. tags-alist
  8410. (cons (cons level tags) tags-alist)))
  8411. ;; compile tags for current headline
  8412. (setq tags-list
  8413. (if org-use-tag-inheritance
  8414. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8415. tags))
  8416. (when org-use-tag-inheritance
  8417. (setcdr (car tags-alist)
  8418. (mapcar (lambda (x)
  8419. (setq x (copy-sequence x))
  8420. (org-add-prop-inherited x))
  8421. (cdar tags-alist))))
  8422. (when (and tags org-use-tag-inheritance
  8423. (not (eq t org-use-tag-inheritance)))
  8424. ;; selective inheritance, remove uninherited ones
  8425. (setcdr (car tags-alist)
  8426. (org-remove-uniherited-tags (cdar tags-alist))))
  8427. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8428. (let ((case-fold-search t)) (eval matcher))
  8429. (or
  8430. (not (member org-archive-tag tags-list))
  8431. ;; we have an archive tag, should we use this anyway?
  8432. (or (not org-agenda-skip-archived-trees)
  8433. (and (eq action 'agenda) org-agenda-archives-mode))))
  8434. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8435. ;; select this headline
  8436. (cond
  8437. ((eq action 'sparse-tree)
  8438. (and org-highlight-sparse-tree-matches
  8439. (org-get-heading) (match-end 0)
  8440. (org-highlight-new-match
  8441. (match-beginning 0) (match-beginning 1)))
  8442. (org-show-context 'tags-tree))
  8443. ((eq action 'agenda)
  8444. (setq txt (org-format-agenda-item
  8445. ""
  8446. (concat
  8447. (if org-tags-match-list-sublevels
  8448. (make-string (1- level) ?.) "")
  8449. (org-get-heading))
  8450. category (org-get-tags-at))
  8451. priority (org-get-priority txt))
  8452. (goto-char lspos)
  8453. (setq marker (org-agenda-new-marker))
  8454. (org-add-props txt props
  8455. 'org-marker marker 'org-hd-marker marker 'org-category category
  8456. 'priority priority 'type "tagsmatch")
  8457. (push txt rtn))
  8458. ((functionp action)
  8459. (save-excursion
  8460. (setq rtn1 (funcall action))
  8461. (push rtn1 rtn))
  8462. (goto-char (point-at-eol)))
  8463. (t (error "Invalid action")))
  8464. ;; if we are to skip sublevels, jump to end of subtree
  8465. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8466. (when (and (eq action 'sparse-tree)
  8467. (not org-sparse-tree-open-archived-trees))
  8468. (org-hide-archived-subtrees (point-min) (point-max)))
  8469. (nreverse rtn)))
  8470. (defun org-remove-uniherited-tags (tags)
  8471. "Remove all tags that are not inherited from the list TAGS."
  8472. (cond
  8473. ((eq org-use-tag-inheritance t)
  8474. (if org-tags-exclude-from-inheritance
  8475. (org-delete-all org-tags-exclude-from-inheritance tags)
  8476. tags))
  8477. ((not org-use-tag-inheritance) nil)
  8478. ((stringp org-use-tag-inheritance)
  8479. (delq nil (mapcar
  8480. (lambda (x)
  8481. (if (and (string-match org-use-tag-inheritance x)
  8482. (not (member x org-tags-exclude-from-inheritance)))
  8483. x nil))
  8484. tags)))
  8485. ((listp org-use-tag-inheritance)
  8486. (delq nil (mapcar
  8487. (lambda (x)
  8488. (if (member x org-use-tag-inheritance) x nil))
  8489. tags)))))
  8490. (defvar todo-only) ;; dynamically scoped
  8491. (defun org-tags-sparse-tree (&optional todo-only match)
  8492. "Create a sparse tree according to tags string MATCH.
  8493. MATCH can contain positive and negative selection of tags, like
  8494. \"+WORK+URGENT-WITHBOSS\".
  8495. If optional argument TODO-ONLY is non-nil, only select lines that are
  8496. also TODO lines."
  8497. (interactive "P")
  8498. (org-prepare-agenda-buffers (list (current-buffer)))
  8499. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8500. (defvar org-cached-props nil)
  8501. (defun org-cached-entry-get (pom property)
  8502. (if (or (eq t org-use-property-inheritance)
  8503. (and (stringp org-use-property-inheritance)
  8504. (string-match org-use-property-inheritance property))
  8505. (and (listp org-use-property-inheritance)
  8506. (member property org-use-property-inheritance)))
  8507. ;; Caching is not possible, check it directly
  8508. (org-entry-get pom property 'inherit)
  8509. ;; Get all properties, so that we can do complicated checks easily
  8510. (cdr (assoc property (or org-cached-props
  8511. (setq org-cached-props
  8512. (org-entry-properties pom)))))))
  8513. (defun org-global-tags-completion-table (&optional files)
  8514. "Return the list of all tags in all agenda buffer/files."
  8515. (save-excursion
  8516. (org-uniquify
  8517. (delq nil
  8518. (apply 'append
  8519. (mapcar
  8520. (lambda (file)
  8521. (set-buffer (find-file-noselect file))
  8522. (append (org-get-buffer-tags)
  8523. (mapcar (lambda (x) (if (stringp (car-safe x))
  8524. (list (car-safe x)) nil))
  8525. org-tag-alist)))
  8526. (if (and files (car files))
  8527. files
  8528. (org-agenda-files))))))))
  8529. (defun org-make-tags-matcher (match)
  8530. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8531. ;; todo-only is scoped dynamically into this function, and the function
  8532. ;; may change it if the matcher asks for it.
  8533. (unless match
  8534. ;; Get a new match request, with completion
  8535. (let ((org-last-tags-completion-table
  8536. (org-global-tags-completion-table)))
  8537. (setq match (org-ido-completing-read
  8538. "Match: " 'org-tags-completion-function nil nil nil
  8539. 'org-tags-history))))
  8540. ;; Parse the string and create a lisp form
  8541. (let ((match0 match)
  8542. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8543. minus tag mm
  8544. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8545. orterms term orlist re-p str-p level-p level-op time-p
  8546. prop-p pn pv po cat-p gv rest)
  8547. (if (string-match "/+" match)
  8548. ;; match contains also a todo-matching request
  8549. (progn
  8550. (setq tagsmatch (substring match 0 (match-beginning 0))
  8551. todomatch (substring match (match-end 0)))
  8552. (if (string-match "^!" todomatch)
  8553. (setq todo-only t todomatch (substring todomatch 1)))
  8554. (if (string-match "^\\s-*$" todomatch)
  8555. (setq todomatch nil)))
  8556. ;; only matching tags
  8557. (setq tagsmatch match todomatch nil))
  8558. ;; Make the tags matcher
  8559. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8560. (setq tagsmatcher t)
  8561. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8562. (while (setq term (pop orterms))
  8563. (while (and (equal (substring term -1) "\\") orterms)
  8564. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8565. (while (string-match re term)
  8566. (setq rest (substring term (match-end 0))
  8567. minus (and (match-end 1)
  8568. (equal (match-string 1 term) "-"))
  8569. tag (match-string 2 term)
  8570. re-p (equal (string-to-char tag) ?{)
  8571. level-p (match-end 4)
  8572. prop-p (match-end 5)
  8573. mm (cond
  8574. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8575. (level-p
  8576. (setq level-op (org-op-to-function (match-string 3 term)))
  8577. `(,level-op level ,(string-to-number
  8578. (match-string 4 term))))
  8579. (prop-p
  8580. (setq pn (match-string 5 term)
  8581. po (match-string 6 term)
  8582. pv (match-string 7 term)
  8583. cat-p (equal pn "CATEGORY")
  8584. re-p (equal (string-to-char pv) ?{)
  8585. str-p (equal (string-to-char pv) ?\")
  8586. time-p (save-match-data
  8587. (string-match "^\"[[<].*[]>]\"$" pv))
  8588. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8589. (if time-p (setq pv (org-matcher-time pv)))
  8590. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8591. (cond
  8592. ((equal pn "CATEGORY")
  8593. (setq gv '(get-text-property (point) 'org-category)))
  8594. ((equal pn "TODO")
  8595. (setq gv 'todo))
  8596. (t
  8597. (setq gv `(org-cached-entry-get nil ,pn))))
  8598. (if re-p
  8599. (if (eq po 'org<>)
  8600. `(not (string-match ,pv (or ,gv "")))
  8601. `(string-match ,pv (or ,gv "")))
  8602. (if str-p
  8603. `(,po (or ,gv "") ,pv)
  8604. `(,po (string-to-number (or ,gv ""))
  8605. ,(string-to-number pv) ))))
  8606. (t `(member ,(downcase tag) tags-list)))
  8607. mm (if minus (list 'not mm) mm)
  8608. term rest)
  8609. (push mm tagsmatcher))
  8610. (push (if (> (length tagsmatcher) 1)
  8611. (cons 'and tagsmatcher)
  8612. (car tagsmatcher))
  8613. orlist)
  8614. (setq tagsmatcher nil))
  8615. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8616. (setq tagsmatcher
  8617. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8618. ;; Make the todo matcher
  8619. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8620. (setq todomatcher t)
  8621. (setq orterms (org-split-string todomatch "|") orlist nil)
  8622. (while (setq term (pop orterms))
  8623. (while (string-match re term)
  8624. (setq minus (and (match-end 1)
  8625. (equal (match-string 1 term) "-"))
  8626. kwd (match-string 2 term)
  8627. re-p (equal (string-to-char kwd) ?{)
  8628. term (substring term (match-end 0))
  8629. mm (if re-p
  8630. `(string-match ,(substring kwd 1 -1) todo)
  8631. (list 'equal 'todo kwd))
  8632. mm (if minus (list 'not mm) mm))
  8633. (push mm todomatcher))
  8634. (push (if (> (length todomatcher) 1)
  8635. (cons 'and todomatcher)
  8636. (car todomatcher))
  8637. orlist)
  8638. (setq todomatcher nil))
  8639. (setq todomatcher (if (> (length orlist) 1)
  8640. (cons 'or orlist) (car orlist))))
  8641. ;; Return the string and lisp forms of the matcher
  8642. (setq matcher (if todomatcher
  8643. (list 'and tagsmatcher todomatcher)
  8644. tagsmatcher))
  8645. (cons match0 matcher)))
  8646. (defun org-op-to-function (op &optional stringp)
  8647. "Turn an operator into the appropriate function."
  8648. (setq op
  8649. (cond
  8650. ((equal op "<" ) '(< string< org-time<))
  8651. ((equal op ">" ) '(> org-string> org-time>))
  8652. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8653. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8654. ((member op '("=" "==")) '(= string= org-time=))
  8655. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8656. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8657. (defun org<> (a b) (not (= a b)))
  8658. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8659. (defun org-string>= (a b) (not (string< a b)))
  8660. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8661. (defun org-string<> (a b) (not (string= a b)))
  8662. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8663. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8664. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8665. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8666. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8667. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8668. (defun org-2ft (s)
  8669. "Convert S to a floating point time.
  8670. If S is already a number, just return it. If it is a string, parse
  8671. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8672. (cond
  8673. ((numberp s) s)
  8674. ((stringp s)
  8675. (condition-case nil
  8676. (float-time (apply 'encode-time (org-parse-time-string s)))
  8677. (error 0.)))
  8678. (t 0.)))
  8679. (defun org-time-today ()
  8680. "Time in seconds today at 0:00.
  8681. Returns the float number of seconds since the beginning of the
  8682. epoch to the beginning of today (00:00)."
  8683. (float-time (apply 'encode-time
  8684. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  8685. (defun org-matcher-time (s)
  8686. "Interpret a time comparison value."
  8687. (save-match-data
  8688. (cond
  8689. ((string= s "<now>") (float-time))
  8690. ((string= s "<today>") (org-time-today))
  8691. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  8692. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  8693. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  8694. (+ (org-time-today)
  8695. (* (string-to-number (match-string 1 s))
  8696. (cdr (assoc (match-string 2 s)
  8697. '(("d" . 86400.0) ("w" . 604800.0)
  8698. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  8699. (t (org-2ft s)))))
  8700. (defun org-match-any-p (re list)
  8701. "Does re match any element of list?"
  8702. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8703. (delq nil list))
  8704. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  8705. (defvar org-tags-overlay (org-make-overlay 1 1))
  8706. (org-detach-overlay org-tags-overlay)
  8707. (defun org-get-local-tags-at (&optional pos)
  8708. "Get a list of tags defined in the current headline."
  8709. (org-get-tags-at pos 'local))
  8710. (defun org-get-local-tags ()
  8711. "Get a list of tags defined in the current headline."
  8712. (org-get-tags-at nil 'local))
  8713. (defun org-get-tags-at (&optional pos local)
  8714. "Get a list of all headline tags applicable at POS.
  8715. POS defaults to point. If tags are inherited, the list contains
  8716. the targets in the same sequence as the headlines appear, i.e.
  8717. the tags of the current headline come last.
  8718. When LOCAL is non-nil, only return tags from the current headline,
  8719. ignore inherited ones."
  8720. (interactive)
  8721. (let (tags ltags lastpos parent)
  8722. (save-excursion
  8723. (save-restriction
  8724. (widen)
  8725. (goto-char (or pos (point)))
  8726. (save-match-data
  8727. (catch 'done
  8728. (condition-case nil
  8729. (progn
  8730. (org-back-to-heading t)
  8731. (while (not (equal lastpos (point)))
  8732. (setq lastpos (point))
  8733. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8734. (setq ltags (org-split-string
  8735. (org-match-string-no-properties 1) ":"))
  8736. (when parent
  8737. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  8738. (setq tags (append
  8739. (if parent
  8740. (org-remove-uniherited-tags ltags)
  8741. ltags)
  8742. tags)))
  8743. (or org-use-tag-inheritance (throw 'done t))
  8744. (if local (throw 'done t))
  8745. (org-up-heading-all 1)
  8746. (setq parent t)))
  8747. (error nil)))))
  8748. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8749. (defun org-add-prop-inherited (s)
  8750. (add-text-properties 0 (length s) '(inherited t) s)
  8751. s)
  8752. (defun org-toggle-tag (tag &optional onoff)
  8753. "Toggle the tag TAG for the current line.
  8754. If ONOFF is `on' or `off', don't toggle but set to this state."
  8755. (let (res current)
  8756. (save-excursion
  8757. (org-back-to-heading t)
  8758. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8759. (point-at-eol) t)
  8760. (progn
  8761. (setq current (match-string 1))
  8762. (replace-match ""))
  8763. (setq current ""))
  8764. (setq current (nreverse (org-split-string current ":")))
  8765. (cond
  8766. ((eq onoff 'on)
  8767. (setq res t)
  8768. (or (member tag current) (push tag current)))
  8769. ((eq onoff 'off)
  8770. (or (not (member tag current)) (setq current (delete tag current))))
  8771. (t (if (member tag current)
  8772. (setq current (delete tag current))
  8773. (setq res t)
  8774. (push tag current))))
  8775. (end-of-line 1)
  8776. (if current
  8777. (progn
  8778. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8779. (org-set-tags nil t))
  8780. (delete-horizontal-space))
  8781. (run-hooks 'org-after-tags-change-hook))
  8782. res))
  8783. (defun org-align-tags-here (to-col)
  8784. ;; Assumes that this is a headline
  8785. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8786. (beginning-of-line 1)
  8787. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8788. (< pos (match-beginning 2)))
  8789. (progn
  8790. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8791. (goto-char (match-beginning 1))
  8792. (insert " ")
  8793. (delete-region (point) (1+ (match-beginning 2)))
  8794. (setq ncol (max (1+ (current-column))
  8795. (1+ col)
  8796. (if (> to-col 0)
  8797. to-col
  8798. (- (abs to-col) tags-l))))
  8799. (setq p (point))
  8800. (insert (make-string (- ncol (current-column)) ?\ ))
  8801. (setq ncol (current-column))
  8802. (when indent-tabs-mode (tabify p (point-at-eol)))
  8803. (org-move-to-column (min ncol col) t))
  8804. (goto-char pos))))
  8805. (defun org-set-tags-command (&optional arg just-align)
  8806. "Call the set-tags command for the current entry."
  8807. (interactive "P")
  8808. (if (org-on-heading-p)
  8809. (org-set-tags arg just-align)
  8810. (save-excursion
  8811. (org-back-to-heading t)
  8812. (org-set-tags arg just-align))))
  8813. (defun org-set-tags (&optional arg just-align)
  8814. "Set the tags for the current headline.
  8815. With prefix ARG, realign all tags in headings in the current buffer."
  8816. (interactive "P")
  8817. (let* ((re (concat "^" outline-regexp))
  8818. (current (org-get-tags-string))
  8819. (col (current-column))
  8820. (org-setting-tags t)
  8821. table current-tags inherited-tags ; computed below when needed
  8822. tags p0 c0 c1 rpl)
  8823. (if arg
  8824. (save-excursion
  8825. (goto-char (point-min))
  8826. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8827. (while (re-search-forward re nil t)
  8828. (org-set-tags nil t)
  8829. (end-of-line 1)))
  8830. (message "All tags realigned to column %d" org-tags-column))
  8831. (if just-align
  8832. (setq tags current)
  8833. ;; Get a new set of tags from the user
  8834. (save-excursion
  8835. (setq table (or org-tag-alist (org-get-buffer-tags))
  8836. org-last-tags-completion-table table
  8837. current-tags (org-split-string current ":")
  8838. inherited-tags (nreverse
  8839. (nthcdr (length current-tags)
  8840. (nreverse (org-get-tags-at))))
  8841. tags
  8842. (if (or (eq t org-use-fast-tag-selection)
  8843. (and org-use-fast-tag-selection
  8844. (delq nil (mapcar 'cdr table))))
  8845. (org-fast-tag-selection
  8846. current-tags inherited-tags table
  8847. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8848. (let ((org-add-colon-after-tag-completion t))
  8849. (org-trim
  8850. (org-without-partial-completion
  8851. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  8852. nil nil current 'org-tags-history)))))))
  8853. (while (string-match "[-+&]+" tags)
  8854. ;; No boolean logic, just a list
  8855. (setq tags (replace-match ":" t t tags))))
  8856. (if (string-match "\\`[\t ]*\\'" tags)
  8857. (setq tags "")
  8858. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8859. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8860. ;; Insert new tags at the correct column
  8861. (beginning-of-line 1)
  8862. (cond
  8863. ((and (equal current "") (equal tags "")))
  8864. ((re-search-forward
  8865. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8866. (point-at-eol) t)
  8867. (if (equal tags "")
  8868. (setq rpl "")
  8869. (goto-char (match-beginning 0))
  8870. (setq c0 (current-column) p0 (point)
  8871. c1 (max (1+ c0) (if (> org-tags-column 0)
  8872. org-tags-column
  8873. (- (- org-tags-column) (length tags))))
  8874. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8875. (replace-match rpl t t)
  8876. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8877. tags)
  8878. (t (error "Tags alignment failed")))
  8879. (org-move-to-column col)
  8880. (unless just-align
  8881. (run-hooks 'org-after-tags-change-hook)))))
  8882. (defun org-change-tag-in-region (beg end tag off)
  8883. "Add or remove TAG for each entry in the region.
  8884. This works in the agenda, and also in an org-mode buffer."
  8885. (interactive
  8886. (list (region-beginning) (region-end)
  8887. (let ((org-last-tags-completion-table
  8888. (if (org-mode-p)
  8889. (org-get-buffer-tags)
  8890. (org-global-tags-completion-table))))
  8891. (org-ido-completing-read
  8892. "Tag: " 'org-tags-completion-function nil nil nil
  8893. 'org-tags-history))
  8894. (progn
  8895. (message "[s]et or [r]emove? ")
  8896. (equal (read-char-exclusive) ?r))))
  8897. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8898. (let ((agendap (equal major-mode 'org-agenda-mode))
  8899. l1 l2 m buf pos newhead (cnt 0))
  8900. (goto-char end)
  8901. (setq l2 (1- (org-current-line)))
  8902. (goto-char beg)
  8903. (setq l1 (org-current-line))
  8904. (loop for l from l1 to l2 do
  8905. (goto-line l)
  8906. (setq m (get-text-property (point) 'org-hd-marker))
  8907. (when (or (and (org-mode-p) (org-on-heading-p))
  8908. (and agendap m))
  8909. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8910. pos (if agendap m (point)))
  8911. (with-current-buffer buf
  8912. (save-excursion
  8913. (save-restriction
  8914. (goto-char pos)
  8915. (setq cnt (1+ cnt))
  8916. (org-toggle-tag tag (if off 'off 'on))
  8917. (setq newhead (org-get-heading)))))
  8918. (and agendap (org-agenda-change-all-lines newhead m))))
  8919. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8920. (defun org-tags-completion-function (string predicate &optional flag)
  8921. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8922. (confirm (lambda (x) (stringp (car x)))))
  8923. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8924. (setq s1 (match-string 1 string)
  8925. s2 (match-string 2 string))
  8926. (setq s1 "" s2 string))
  8927. (cond
  8928. ((eq flag nil)
  8929. ;; try completion
  8930. (setq rtn (try-completion s2 ctable confirm))
  8931. (if (stringp rtn)
  8932. (setq rtn
  8933. (concat s1 s2 (substring rtn (length s2))
  8934. (if (and org-add-colon-after-tag-completion
  8935. (assoc rtn ctable))
  8936. ":" ""))))
  8937. rtn)
  8938. ((eq flag t)
  8939. ;; all-completions
  8940. (all-completions s2 ctable confirm)
  8941. )
  8942. ((eq flag 'lambda)
  8943. ;; exact match?
  8944. (assoc s2 ctable)))
  8945. ))
  8946. (defun org-fast-tag-insert (kwd tags face &optional end)
  8947. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  8948. (insert (format "%-12s" (concat kwd ":"))
  8949. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8950. (or end "")))
  8951. (defun org-fast-tag-show-exit (flag)
  8952. (save-excursion
  8953. (goto-line 3)
  8954. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8955. (replace-match ""))
  8956. (when flag
  8957. (end-of-line 1)
  8958. (org-move-to-column (- (window-width) 19) t)
  8959. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8960. (defun org-set-current-tags-overlay (current prefix)
  8961. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8962. (if (featurep 'xemacs)
  8963. (org-overlay-display org-tags-overlay (concat prefix s)
  8964. 'secondary-selection)
  8965. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8966. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8967. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8968. "Fast tag selection with single keys.
  8969. CURRENT is the current list of tags in the headline, INHERITED is the
  8970. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8971. possibly with grouping information. TODO-TABLE is a similar table with
  8972. TODO keywords, should these have keys assigned to them.
  8973. If the keys are nil, a-z are automatically assigned.
  8974. Returns the new tags string, or nil to not change the current settings."
  8975. (let* ((fulltable (append table todo-table))
  8976. (maxlen (apply 'max (mapcar
  8977. (lambda (x)
  8978. (if (stringp (car x)) (string-width (car x)) 0))
  8979. fulltable)))
  8980. (buf (current-buffer))
  8981. (expert (eq org-fast-tag-selection-single-key 'expert))
  8982. (buffer-tags nil)
  8983. (fwidth (+ maxlen 3 1 3))
  8984. (ncol (/ (- (window-width) 4) fwidth))
  8985. (i-face 'org-done)
  8986. (c-face 'org-todo)
  8987. tg cnt e c char c1 c2 ntable tbl rtn
  8988. ov-start ov-end ov-prefix
  8989. (exit-after-next org-fast-tag-selection-single-key)
  8990. (done-keywords org-done-keywords)
  8991. groups ingroup)
  8992. (save-excursion
  8993. (beginning-of-line 1)
  8994. (if (looking-at
  8995. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8996. (setq ov-start (match-beginning 1)
  8997. ov-end (match-end 1)
  8998. ov-prefix "")
  8999. (setq ov-start (1- (point-at-eol))
  9000. ov-end (1+ ov-start))
  9001. (skip-chars-forward "^\n\r")
  9002. (setq ov-prefix
  9003. (concat
  9004. (buffer-substring (1- (point)) (point))
  9005. (if (> (current-column) org-tags-column)
  9006. " "
  9007. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9008. (org-move-overlay org-tags-overlay ov-start ov-end)
  9009. (save-window-excursion
  9010. (if expert
  9011. (set-buffer (get-buffer-create " *Org tags*"))
  9012. (delete-other-windows)
  9013. (split-window-vertically)
  9014. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9015. (erase-buffer)
  9016. (org-set-local 'org-done-keywords done-keywords)
  9017. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9018. (org-fast-tag-insert "Current" current c-face "\n\n")
  9019. (org-fast-tag-show-exit exit-after-next)
  9020. (org-set-current-tags-overlay current ov-prefix)
  9021. (setq tbl fulltable char ?a cnt 0)
  9022. (while (setq e (pop tbl))
  9023. (cond
  9024. ((equal e '(:startgroup))
  9025. (push '() groups) (setq ingroup t)
  9026. (when (not (= cnt 0))
  9027. (setq cnt 0)
  9028. (insert "\n"))
  9029. (insert "{ "))
  9030. ((equal e '(:endgroup))
  9031. (setq ingroup nil cnt 0)
  9032. (insert "}\n"))
  9033. (t
  9034. (setq tg (car e) c2 nil)
  9035. (if (cdr e)
  9036. (setq c (cdr e))
  9037. ;; automatically assign a character.
  9038. (setq c1 (string-to-char
  9039. (downcase (substring
  9040. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9041. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9042. (while (or (rassoc char ntable) (rassoc char table))
  9043. (setq char (1+ char)))
  9044. (setq c2 c1))
  9045. (setq c (or c2 char)))
  9046. (if ingroup (push tg (car groups)))
  9047. (setq tg (org-add-props tg nil 'face
  9048. (cond
  9049. ((not (assoc tg table))
  9050. (org-get-todo-face tg))
  9051. ((member tg current) c-face)
  9052. ((member tg inherited) i-face)
  9053. (t nil))))
  9054. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9055. (insert "[" c "] " tg (make-string
  9056. (- fwidth 4 (length tg)) ?\ ))
  9057. (push (cons tg c) ntable)
  9058. (when (= (setq cnt (1+ cnt)) ncol)
  9059. (insert "\n")
  9060. (if ingroup (insert " "))
  9061. (setq cnt 0)))))
  9062. (setq ntable (nreverse ntable))
  9063. (insert "\n")
  9064. (goto-char (point-min))
  9065. (if (not expert) (org-fit-window-to-buffer))
  9066. (setq rtn
  9067. (catch 'exit
  9068. (while t
  9069. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9070. (if groups " [!] no groups" " [!]groups")
  9071. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9072. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9073. (cond
  9074. ((= c ?\r) (throw 'exit t))
  9075. ((= c ?!)
  9076. (setq groups (not groups))
  9077. (goto-char (point-min))
  9078. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9079. ((= c ?\C-c)
  9080. (if (not expert)
  9081. (org-fast-tag-show-exit
  9082. (setq exit-after-next (not exit-after-next)))
  9083. (setq expert nil)
  9084. (delete-other-windows)
  9085. (split-window-vertically)
  9086. (org-switch-to-buffer-other-window " *Org tags*")
  9087. (org-fit-window-to-buffer)))
  9088. ((or (= c ?\C-g)
  9089. (and (= c ?q) (not (rassoc c ntable))))
  9090. (org-detach-overlay org-tags-overlay)
  9091. (setq quit-flag t))
  9092. ((= c ?\ )
  9093. (setq current nil)
  9094. (if exit-after-next (setq exit-after-next 'now)))
  9095. ((= c ?\t)
  9096. (condition-case nil
  9097. (setq tg (org-ido-completing-read
  9098. "Tag: "
  9099. (or buffer-tags
  9100. (with-current-buffer buf
  9101. (org-get-buffer-tags)))))
  9102. (quit (setq tg "")))
  9103. (when (string-match "\\S-" tg)
  9104. (add-to-list 'buffer-tags (list tg))
  9105. (if (member tg current)
  9106. (setq current (delete tg current))
  9107. (push tg current)))
  9108. (if exit-after-next (setq exit-after-next 'now)))
  9109. ((setq e (rassoc c todo-table) tg (car e))
  9110. (with-current-buffer buf
  9111. (save-excursion (org-todo tg)))
  9112. (if exit-after-next (setq exit-after-next 'now)))
  9113. ((setq e (rassoc c ntable) tg (car e))
  9114. (if (member tg current)
  9115. (setq current (delete tg current))
  9116. (loop for g in groups do
  9117. (if (member tg g)
  9118. (mapc (lambda (x)
  9119. (setq current (delete x current)))
  9120. g)))
  9121. (push tg current))
  9122. (if exit-after-next (setq exit-after-next 'now))))
  9123. ;; Create a sorted list
  9124. (setq current
  9125. (sort current
  9126. (lambda (a b)
  9127. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9128. (if (eq exit-after-next 'now) (throw 'exit t))
  9129. (goto-char (point-min))
  9130. (beginning-of-line 2)
  9131. (delete-region (point) (point-at-eol))
  9132. (org-fast-tag-insert "Current" current c-face)
  9133. (org-set-current-tags-overlay current ov-prefix)
  9134. (while (re-search-forward
  9135. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9136. (setq tg (match-string 1))
  9137. (add-text-properties
  9138. (match-beginning 1) (match-end 1)
  9139. (list 'face
  9140. (cond
  9141. ((member tg current) c-face)
  9142. ((member tg inherited) i-face)
  9143. (t (get-text-property (match-beginning 1) 'face))))))
  9144. (goto-char (point-min)))))
  9145. (org-detach-overlay org-tags-overlay)
  9146. (if rtn
  9147. (mapconcat 'identity current ":")
  9148. nil))))
  9149. (defun org-get-tags-string ()
  9150. "Get the TAGS string in the current headline."
  9151. (unless (org-on-heading-p t)
  9152. (error "Not on a heading"))
  9153. (save-excursion
  9154. (beginning-of-line 1)
  9155. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9156. (org-match-string-no-properties 1)
  9157. "")))
  9158. (defun org-get-tags ()
  9159. "Get the list of tags specified in the current headline."
  9160. (org-split-string (org-get-tags-string) ":"))
  9161. (defun org-get-buffer-tags ()
  9162. "Get a table of all tags used in the buffer, for completion."
  9163. (let (tags)
  9164. (save-excursion
  9165. (goto-char (point-min))
  9166. (while (re-search-forward
  9167. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9168. (when (equal (char-after (point-at-bol 0)) ?*)
  9169. (mapc (lambda (x) (add-to-list 'tags x))
  9170. (org-split-string (org-match-string-no-properties 1) ":")))))
  9171. (mapcar 'list tags)))
  9172. ;;;; The mapping API
  9173. ;;;###autoload
  9174. (defun org-map-entries (func &optional match scope &rest skip)
  9175. "Call FUNC at each headline selected by MATCH in SCOPE.
  9176. FUNC is a function or a lisp form. The function will be called without
  9177. arguments, with the cursor positioned at the beginning of the headline.
  9178. The return values of all calls to the function will be collected and
  9179. returned as a list.
  9180. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9181. Only headlines that are matched by this query will be considered during
  9182. the iteration. When MATCH is nil or t, all headlines will be
  9183. visited by the iteration.
  9184. SCOPE determines the scope of this command. It can be any of:
  9185. nil The current buffer, respecting the restriction if any
  9186. tree The subtree started with the entry at point
  9187. file The current buffer, without restriction
  9188. file-with-archives
  9189. The current buffer, and any archives associated with it
  9190. agenda All agenda files
  9191. agenda-with-archives
  9192. All agenda files with any archive files associated with them
  9193. \(file1 file2 ...)
  9194. If this is a list, all files in the list will be scanned
  9195. The remaining args are treated as settings for the skipping facilities of
  9196. the scanner. The following items can be given here:
  9197. archive skip trees with the archive tag.
  9198. comment skip trees with the COMMENT keyword
  9199. function or Emacs Lisp form:
  9200. will be used as value for `org-agenda-skip-function', so whenever
  9201. the the function returns t, FUNC will not be called for that
  9202. entry and search will continue from the point where the
  9203. function leaves it."
  9204. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9205. (org-agenda-skip-archived-trees (memq 'archive skip))
  9206. (org-agenda-skip-comment-trees (memq 'comment skip))
  9207. (org-agenda-skip-function
  9208. (car (org-delete-all '(comment archive) skip)))
  9209. (org-tags-match-list-sublevels t)
  9210. matcher pos file res
  9211. org-todo-keywords-for-agenda
  9212. org-done-keywords-for-agenda
  9213. org-todo-keyword-alist-for-agenda
  9214. org-tag-alist-for-agenda)
  9215. (cond
  9216. ((eq match t) (setq matcher t))
  9217. ((eq match nil) (setq matcher t))
  9218. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9219. (when (eq scope 'tree)
  9220. (org-back-to-heading t)
  9221. (org-narrow-to-subtree)
  9222. (setq scope nil))
  9223. (if (not scope)
  9224. (progn
  9225. (org-prepare-agenda-buffers
  9226. (list (buffer-file-name (current-buffer))))
  9227. (org-scan-tags func matcher))
  9228. ;; Get the right scope
  9229. (setq pos (point))
  9230. (cond
  9231. ((and scope (listp scope) (symbolp (car scope)))
  9232. (setq scope (eval scope)))
  9233. ((eq scope 'agenda)
  9234. (setq scope (org-agenda-files t)))
  9235. ((eq scope 'agenda-with-archives)
  9236. (setq scope (org-agenda-files t))
  9237. (setq scope (org-add-archive-files scope)))
  9238. ((eq scope 'file)
  9239. (setq scope (list (buffer-file-name))))
  9240. ((eq scope 'file-with-archives)
  9241. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9242. (org-prepare-agenda-buffers scope)
  9243. (while (setq file (pop scope))
  9244. (with-current-buffer (org-find-base-buffer-visiting file)
  9245. (save-excursion
  9246. (save-restriction
  9247. (widen)
  9248. (goto-char (point-min))
  9249. (setq res (append res (org-scan-tags func matcher)))))))
  9250. res)))
  9251. ;;;; Properties
  9252. ;;; Setting and retrieving properties
  9253. (defconst org-special-properties
  9254. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9255. "TIMESTAMP" "TIMESTAMP_IA")
  9256. "The special properties valid in Org-mode.
  9257. These are properties that are not defined in the property drawer,
  9258. but in some other way.")
  9259. (defconst org-default-properties
  9260. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9261. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9262. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9263. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9264. "Some properties that are used by Org-mode for various purposes.
  9265. Being in this list makes sure that they are offered for completion.")
  9266. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9267. "Regular expression matching the first line of a property drawer.")
  9268. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9269. "Regular expression matching the first line of a property drawer.")
  9270. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9271. "Regular expression matching the first line of a property drawer.")
  9272. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9273. "Regular expression matching the first line of a property drawer.")
  9274. (defconst org-property-drawer-re
  9275. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9276. org-property-end-re "\\)\n?")
  9277. "Matches an entire property drawer.")
  9278. (defconst org-clock-drawer-re
  9279. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9280. org-property-end-re "\\)\n?")
  9281. "Matches an entire clock drawer.")
  9282. (defun org-property-action ()
  9283. "Do an action on properties."
  9284. (interactive)
  9285. (let (c)
  9286. (org-at-property-p)
  9287. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9288. (setq c (read-char-exclusive))
  9289. (cond
  9290. ((equal c ?s)
  9291. (call-interactively 'org-set-property))
  9292. ((equal c ?d)
  9293. (call-interactively 'org-delete-property))
  9294. ((equal c ?D)
  9295. (call-interactively 'org-delete-property-globally))
  9296. ((equal c ?c)
  9297. (call-interactively 'org-compute-property-at-point))
  9298. (t (error "No such property action %c" c)))))
  9299. (defun org-at-property-p ()
  9300. "Is the cursor in a property line?"
  9301. ;; FIXME: Does not check if we are actually in the drawer.
  9302. ;; FIXME: also returns true on any drawers.....
  9303. ;; This is used by C-c C-c for property action.
  9304. (save-excursion
  9305. (beginning-of-line 1)
  9306. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9307. (defun org-get-property-block (&optional beg end force)
  9308. "Return the (beg . end) range of the body of the property drawer.
  9309. BEG and END can be beginning and end of subtree, if not given
  9310. they will be found.
  9311. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9312. (catch 'exit
  9313. (save-excursion
  9314. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9315. (end (or end (progn (outline-next-heading) (point)))))
  9316. (goto-char beg)
  9317. (if (re-search-forward org-property-start-re end t)
  9318. (setq beg (1+ (match-end 0)))
  9319. (if force
  9320. (save-excursion
  9321. (org-insert-property-drawer)
  9322. (setq end (progn (outline-next-heading) (point))))
  9323. (throw 'exit nil))
  9324. (goto-char beg)
  9325. (if (re-search-forward org-property-start-re end t)
  9326. (setq beg (1+ (match-end 0)))))
  9327. (if (re-search-forward org-property-end-re end t)
  9328. (setq end (match-beginning 0))
  9329. (or force (throw 'exit nil))
  9330. (goto-char beg)
  9331. (setq end beg)
  9332. (org-indent-line-function)
  9333. (insert ":END:\n"))
  9334. (cons beg end)))))
  9335. (defun org-entry-properties (&optional pom which)
  9336. "Get all properties of the entry at point-or-marker POM.
  9337. This includes the TODO keyword, the tags, time strings for deadline,
  9338. scheduled, and clocking, and any additional properties defined in the
  9339. entry. The return value is an alist, keys may occur multiple times
  9340. if the property key was used several times.
  9341. POM may also be nil, in which case the current entry is used.
  9342. If WHICH is nil or `all', get all properties. If WHICH is
  9343. `special' or `standard', only get that subclass."
  9344. (setq which (or which 'all))
  9345. (org-with-point-at pom
  9346. (let ((clockstr (substring org-clock-string 0 -1))
  9347. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9348. beg end range props sum-props key value string clocksum)
  9349. (save-excursion
  9350. (when (condition-case nil (org-back-to-heading t) (error nil))
  9351. (setq beg (point))
  9352. (setq sum-props (get-text-property (point) 'org-summaries))
  9353. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9354. (outline-next-heading)
  9355. (setq end (point))
  9356. (when (memq which '(all special))
  9357. ;; Get the special properties, like TODO and tags
  9358. (goto-char beg)
  9359. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9360. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9361. (when (looking-at org-priority-regexp)
  9362. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9363. (when (and (setq value (org-get-tags-string))
  9364. (string-match "\\S-" value))
  9365. (push (cons "TAGS" value) props))
  9366. (when (setq value (org-get-tags-at))
  9367. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9368. props))
  9369. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9370. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9371. string (if (equal key clockstr)
  9372. (org-no-properties
  9373. (org-trim
  9374. (buffer-substring
  9375. (match-beginning 3) (goto-char (point-at-eol)))))
  9376. (substring (org-match-string-no-properties 3) 1 -1)))
  9377. (unless key
  9378. (if (= (char-after (match-beginning 3)) ?\[)
  9379. (setq key "TIMESTAMP_IA")
  9380. (setq key "TIMESTAMP")))
  9381. (when (or (equal key clockstr) (not (assoc key props)))
  9382. (push (cons key string) props)))
  9383. )
  9384. (when (memq which '(all standard))
  9385. ;; Get the standard properties, like :PORP: ...
  9386. (setq range (org-get-property-block beg end))
  9387. (when range
  9388. (goto-char (car range))
  9389. (while (re-search-forward
  9390. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9391. (cdr range) t)
  9392. (setq key (org-match-string-no-properties 1)
  9393. value (org-trim (or (org-match-string-no-properties 2) "")))
  9394. (unless (member key excluded)
  9395. (push (cons key (or value "")) props)))))
  9396. (if clocksum
  9397. (push (cons "CLOCKSUM"
  9398. (org-columns-number-to-string (/ (float clocksum) 60.)
  9399. 'add_times))
  9400. props))
  9401. (unless (assoc "CATEGORY" props)
  9402. (setq value (or (org-get-category)
  9403. (progn (org-refresh-category-properties)
  9404. (org-get-category))))
  9405. (push (cons "CATEGORY" value) props))
  9406. (append sum-props (nreverse props)))))))
  9407. (defun org-entry-get (pom property &optional inherit)
  9408. "Get value of PROPERTY for entry at point-or-marker POM.
  9409. If INHERIT is non-nil and the entry does not have the property,
  9410. then also check higher levels of the hierarchy.
  9411. If INHERIT is the symbol `selective', use inheritance only if the setting
  9412. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9413. If the property is present but empty, the return value is the empty string.
  9414. If the property is not present at all, nil is returned."
  9415. (org-with-point-at pom
  9416. (if (and inherit (if (eq inherit 'selective)
  9417. (org-property-inherit-p property)
  9418. t))
  9419. (org-entry-get-with-inheritance property)
  9420. (if (member property org-special-properties)
  9421. ;; We need a special property. Use brute force, get all properties.
  9422. (cdr (assoc property (org-entry-properties nil 'special)))
  9423. (let ((range (org-get-property-block)))
  9424. (if (and range
  9425. (goto-char (car range))
  9426. (re-search-forward
  9427. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9428. (cdr range) t))
  9429. ;; Found the property, return it.
  9430. (if (match-end 1)
  9431. (org-match-string-no-properties 1)
  9432. "")))))))
  9433. (defun org-property-or-variable-value (var &optional inherit)
  9434. "Check if there is a property fixing the value of VAR.
  9435. If yes, return this value. If not, return the current value of the variable."
  9436. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9437. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9438. (read prop)
  9439. (symbol-value var))))
  9440. (defun org-entry-delete (pom property)
  9441. "Delete the property PROPERTY from entry at point-or-marker POM."
  9442. (org-with-point-at pom
  9443. (if (member property org-special-properties)
  9444. nil ; cannot delete these properties.
  9445. (let ((range (org-get-property-block)))
  9446. (if (and range
  9447. (goto-char (car range))
  9448. (re-search-forward
  9449. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9450. (cdr range) t))
  9451. (progn
  9452. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9453. t)
  9454. nil)))))
  9455. ;; Multi-values properties are properties that contain multiple values
  9456. ;; These values are assumed to be single words, separated by whitespace.
  9457. (defun org-entry-add-to-multivalued-property (pom property value)
  9458. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9459. (let* ((old (org-entry-get pom property))
  9460. (values (and old (org-split-string old "[ \t]"))))
  9461. (setq value (org-entry-protect-space value))
  9462. (unless (member value values)
  9463. (setq values (cons value values))
  9464. (org-entry-put pom property
  9465. (mapconcat 'identity values " ")))))
  9466. (defun org-entry-remove-from-multivalued-property (pom property value)
  9467. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9468. (let* ((old (org-entry-get pom property))
  9469. (values (and old (org-split-string old "[ \t]"))))
  9470. (setq value (org-entry-protect-space value))
  9471. (when (member value values)
  9472. (setq values (delete value values))
  9473. (org-entry-put pom property
  9474. (mapconcat 'identity values " ")))))
  9475. (defun org-entry-member-in-multivalued-property (pom property value)
  9476. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9477. (let* ((old (org-entry-get pom property))
  9478. (values (and old (org-split-string old "[ \t]"))))
  9479. (setq value (org-entry-protect-space value))
  9480. (member value values)))
  9481. (defun org-entry-get-multivalued-property (pom property)
  9482. "Return a list of values in a multivalued property."
  9483. (let* ((value (org-entry-get pom property))
  9484. (values (and value (org-split-string value "[ \t]"))))
  9485. (mapcar 'org-entry-restore-space values)))
  9486. (defun org-entry-put-multivalued-property (pom property &rest values)
  9487. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9488. VALUES should be a list of strings. Spaces will be protected."
  9489. (org-entry-put pom property
  9490. (mapconcat 'org-entry-protect-space values " "))
  9491. (let* ((value (org-entry-get pom property))
  9492. (values (and value (org-split-string value "[ \t]"))))
  9493. (mapcar 'org-entry-restore-space values)))
  9494. (defun org-entry-protect-space (s)
  9495. "Protect spaces and newline in string S."
  9496. (while (string-match " " s)
  9497. (setq s (replace-match "%20" t t s)))
  9498. (while (string-match "\n" s)
  9499. (setq s (replace-match "%0A" t t s)))
  9500. s)
  9501. (defun org-entry-restore-space (s)
  9502. "Restore spaces and newline in string S."
  9503. (while (string-match "%20" s)
  9504. (setq s (replace-match " " t t s)))
  9505. (while (string-match "%0A" s)
  9506. (setq s (replace-match "\n" t t s)))
  9507. s)
  9508. (defvar org-entry-property-inherited-from (make-marker)
  9509. "Marker pointing to the entry from where a property was inherited.
  9510. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9511. location of the entry where the inheritance search matched. If there was
  9512. no match, the marker will point nowhere.
  9513. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9514. is set.")
  9515. (defun org-entry-get-with-inheritance (property)
  9516. "Get entry property, and search higher levels if not present."
  9517. (move-marker org-entry-property-inherited-from nil)
  9518. (let (tmp)
  9519. (save-excursion
  9520. (save-restriction
  9521. (widen)
  9522. (catch 'ex
  9523. (while t
  9524. (when (setq tmp (org-entry-get nil property))
  9525. (org-back-to-heading t)
  9526. (move-marker org-entry-property-inherited-from (point))
  9527. (throw 'ex tmp))
  9528. (or (org-up-heading-safe) (throw 'ex nil)))))
  9529. (or tmp
  9530. (cdr (assoc property org-file-properties))
  9531. (cdr (assoc property org-global-properties))
  9532. (cdr (assoc property org-global-properties-fixed))))))
  9533. (defun org-entry-put (pom property value)
  9534. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9535. (org-with-point-at pom
  9536. (org-back-to-heading t)
  9537. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9538. range)
  9539. (cond
  9540. ((equal property "TODO")
  9541. (when (and (stringp value) (string-match "\\S-" value)
  9542. (not (member value org-todo-keywords-1)))
  9543. (error "\"%s\" is not a valid TODO state" value))
  9544. (if (or (not value)
  9545. (not (string-match "\\S-" value)))
  9546. (setq value 'none))
  9547. (org-todo value)
  9548. (org-set-tags nil 'align))
  9549. ((equal property "PRIORITY")
  9550. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9551. (string-to-char value) ?\ ))
  9552. (org-set-tags nil 'align))
  9553. ((equal property "SCHEDULED")
  9554. (if (re-search-forward org-scheduled-time-regexp end t)
  9555. (cond
  9556. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9557. ((eq value 'later) (org-timestamp-change 1 'day))
  9558. (t (call-interactively 'org-schedule)))
  9559. (call-interactively 'org-schedule)))
  9560. ((equal property "DEADLINE")
  9561. (if (re-search-forward org-deadline-time-regexp end t)
  9562. (cond
  9563. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9564. ((eq value 'later) (org-timestamp-change 1 'day))
  9565. (t (call-interactively 'org-deadline)))
  9566. (call-interactively 'org-deadline)))
  9567. ((member property org-special-properties)
  9568. (error "The %s property can not yet be set with `org-entry-put'"
  9569. property))
  9570. (t ; a non-special property
  9571. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9572. (setq range (org-get-property-block beg end 'force))
  9573. (goto-char (car range))
  9574. (if (re-search-forward
  9575. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9576. (progn
  9577. (delete-region (match-beginning 1) (match-end 1))
  9578. (goto-char (match-beginning 1)))
  9579. (goto-char (cdr range))
  9580. (insert "\n")
  9581. (backward-char 1)
  9582. (org-indent-line-function)
  9583. (insert ":" property ":"))
  9584. (and value (insert " " value))
  9585. (org-indent-line-function)))))))
  9586. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9587. "Get all property keys in the current buffer.
  9588. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9589. like tags and TODO state.
  9590. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9591. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9592. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9593. formats in the current buffer."
  9594. (let (rtn range cfmt cols s p)
  9595. (save-excursion
  9596. (save-restriction
  9597. (widen)
  9598. (goto-char (point-min))
  9599. (while (re-search-forward org-property-start-re nil t)
  9600. (setq range (org-get-property-block))
  9601. (goto-char (car range))
  9602. (while (re-search-forward
  9603. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9604. (cdr range) t)
  9605. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9606. (outline-next-heading))))
  9607. (when include-specials
  9608. (setq rtn (append org-special-properties rtn)))
  9609. (when include-defaults
  9610. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9611. (when include-columns
  9612. (save-excursion
  9613. (save-restriction
  9614. (widen)
  9615. (goto-char (point-min))
  9616. (while (re-search-forward
  9617. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9618. nil t)
  9619. (setq cfmt (match-string 2) s 0)
  9620. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9621. cfmt s)
  9622. (setq s (match-end 0)
  9623. p (match-string 1 cfmt))
  9624. (unless (or (equal p "ITEM")
  9625. (member p org-special-properties))
  9626. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9627. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9628. (defun org-property-values (key)
  9629. "Return a list of all values of property KEY."
  9630. (save-excursion
  9631. (save-restriction
  9632. (widen)
  9633. (goto-char (point-min))
  9634. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9635. values)
  9636. (while (re-search-forward re nil t)
  9637. (add-to-list 'values (org-trim (match-string 1))))
  9638. (delete "" values)))))
  9639. (defun org-insert-property-drawer ()
  9640. "Insert a property drawer into the current entry."
  9641. (interactive)
  9642. (org-back-to-heading t)
  9643. (looking-at outline-regexp)
  9644. (let ((indent (- (match-end 0)(match-beginning 0)))
  9645. (beg (point))
  9646. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9647. end hiddenp)
  9648. (outline-next-heading)
  9649. (setq end (point))
  9650. (goto-char beg)
  9651. (while (re-search-forward re end t))
  9652. (setq hiddenp (org-invisible-p))
  9653. (end-of-line 1)
  9654. (and (equal (char-after) ?\n) (forward-char 1))
  9655. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9656. (beginning-of-line 2))
  9657. (org-skip-over-state-notes)
  9658. (skip-chars-backward " \t\n\r")
  9659. (if (eq (char-before) ?*) (forward-char 1))
  9660. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9661. (beginning-of-line 0)
  9662. (org-indent-to-column indent)
  9663. (beginning-of-line 2)
  9664. (org-indent-to-column indent)
  9665. (beginning-of-line 0)
  9666. (if hiddenp
  9667. (save-excursion
  9668. (org-back-to-heading t)
  9669. (hide-entry))
  9670. (org-flag-drawer t))))
  9671. (defun org-set-property (property value)
  9672. "In the current entry, set PROPERTY to VALUE.
  9673. When called interactively, this will prompt for a property name, offering
  9674. completion on existing and default properties. And then it will prompt
  9675. for a value, offering completion either on allowed values (via an inherited
  9676. xxx_ALL property) or on existing values in other instances of this property
  9677. in the current file."
  9678. (interactive
  9679. (let* ((completion-ignore-case t)
  9680. (keys (org-buffer-property-keys nil t t))
  9681. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  9682. (prop (if (member prop0 keys)
  9683. prop0
  9684. (or (cdr (assoc (downcase prop0)
  9685. (mapcar (lambda (x) (cons (downcase x) x))
  9686. keys)))
  9687. prop0)))
  9688. (cur (org-entry-get nil prop))
  9689. (allowed (org-property-get-allowed-values nil prop 'table))
  9690. (existing (mapcar 'list (org-property-values prop)))
  9691. (val (if allowed
  9692. (org-completing-read "Value: " allowed nil 'req-match)
  9693. (org-completing-read
  9694. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9695. (concat "[" cur "]") "")
  9696. ": ")
  9697. existing nil nil "" nil cur))))
  9698. (list prop (if (equal val "") cur val))))
  9699. (unless (equal (org-entry-get nil property) value)
  9700. (org-entry-put nil property value)))
  9701. (defun org-delete-property (property)
  9702. "In the current entry, delete PROPERTY."
  9703. (interactive
  9704. (let* ((completion-ignore-case t)
  9705. (prop (org-ido-completing-read
  9706. "Property: " (org-entry-properties nil 'standard))))
  9707. (list prop)))
  9708. (message "Property %s %s" property
  9709. (if (org-entry-delete nil property)
  9710. "deleted"
  9711. "was not present in the entry")))
  9712. (defun org-delete-property-globally (property)
  9713. "Remove PROPERTY globally, from all entries."
  9714. (interactive
  9715. (let* ((completion-ignore-case t)
  9716. (prop (org-ido-completing-read
  9717. "Globally remove property: "
  9718. (mapcar 'list (org-buffer-property-keys)))))
  9719. (list prop)))
  9720. (save-excursion
  9721. (save-restriction
  9722. (widen)
  9723. (goto-char (point-min))
  9724. (let ((cnt 0))
  9725. (while (re-search-forward
  9726. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9727. nil t)
  9728. (setq cnt (1+ cnt))
  9729. (replace-match ""))
  9730. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9731. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9732. (defun org-compute-property-at-point ()
  9733. "Compute the property at point.
  9734. This looks for an enclosing column format, extracts the operator and
  9735. then applies it to the property in the column format's scope."
  9736. (interactive)
  9737. (unless (org-at-property-p)
  9738. (error "Not at a property"))
  9739. (let ((prop (org-match-string-no-properties 2)))
  9740. (org-columns-get-format-and-top-level)
  9741. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9742. (error "No operator defined for property %s" prop))
  9743. (org-columns-compute prop)))
  9744. (defun org-property-get-allowed-values (pom property &optional table)
  9745. "Get allowed values for the property PROPERTY.
  9746. When TABLE is non-nil, return an alist that can directly be used for
  9747. completion."
  9748. (let (vals)
  9749. (cond
  9750. ((equal property "TODO")
  9751. (setq vals (org-with-point-at pom
  9752. (append org-todo-keywords-1 '("")))))
  9753. ((equal property "PRIORITY")
  9754. (let ((n org-lowest-priority))
  9755. (while (>= n org-highest-priority)
  9756. (push (char-to-string n) vals)
  9757. (setq n (1- n)))))
  9758. ((member property org-special-properties))
  9759. (t
  9760. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9761. (when (and vals (string-match "\\S-" vals))
  9762. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9763. (setq vals (mapcar (lambda (x)
  9764. (cond ((stringp x) x)
  9765. ((numberp x) (number-to-string x))
  9766. ((symbolp x) (symbol-name x))
  9767. (t "???")))
  9768. vals)))))
  9769. (if table (mapcar 'list vals) vals)))
  9770. (defun org-property-previous-allowed-value (&optional previous)
  9771. "Switch to the next allowed value for this property."
  9772. (interactive)
  9773. (org-property-next-allowed-value t))
  9774. (defun org-property-next-allowed-value (&optional previous)
  9775. "Switch to the next allowed value for this property."
  9776. (interactive)
  9777. (unless (org-at-property-p)
  9778. (error "Not at a property"))
  9779. (let* ((key (match-string 2))
  9780. (value (match-string 3))
  9781. (allowed (or (org-property-get-allowed-values (point) key)
  9782. (and (member value '("[ ]" "[-]" "[X]"))
  9783. '("[ ]" "[X]"))))
  9784. nval)
  9785. (unless allowed
  9786. (error "Allowed values for this property have not been defined"))
  9787. (if previous (setq allowed (reverse allowed)))
  9788. (if (member value allowed)
  9789. (setq nval (car (cdr (member value allowed)))))
  9790. (setq nval (or nval (car allowed)))
  9791. (if (equal nval value)
  9792. (error "Only one allowed value for this property"))
  9793. (org-at-property-p)
  9794. (replace-match (concat " :" key ": " nval) t t)
  9795. (org-indent-line-function)
  9796. (beginning-of-line 1)
  9797. (skip-chars-forward " \t")))
  9798. (defun org-find-entry-with-id (ident)
  9799. "Locate the entry that contains the ID property with exact value IDENT.
  9800. IDENT can be a string, a symbol or a number, this function will search for
  9801. the string representation of it.
  9802. Return the position where this entry starts, or nil if there is no such entry."
  9803. (interactive "sID: ")
  9804. (let ((id (cond
  9805. ((stringp ident) ident)
  9806. ((symbol-name ident) (symbol-name ident))
  9807. ((numberp ident) (number-to-string ident))
  9808. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9809. (case-fold-search nil))
  9810. (save-excursion
  9811. (save-restriction
  9812. (widen)
  9813. (goto-char (point-min))
  9814. (when (re-search-forward
  9815. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9816. nil t)
  9817. (org-back-to-heading)
  9818. (point))))))
  9819. ;;;; Timestamps
  9820. (defvar org-last-changed-timestamp nil)
  9821. (defvar org-last-inserted-timestamp nil
  9822. "The last time stamp inserted with `org-insert-time-stamp'.")
  9823. (defvar org-time-was-given) ; dynamically scoped parameter
  9824. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9825. (defvar org-ts-what) ; dynamically scoped parameter
  9826. (defun org-time-stamp (arg &optional inactive)
  9827. "Prompt for a date/time and insert a time stamp.
  9828. If the user specifies a time like HH:MM, or if this command is called
  9829. with a prefix argument, the time stamp will contain date and time.
  9830. Otherwise, only the date will be included. All parts of a date not
  9831. specified by the user will be filled in from the current date/time.
  9832. So if you press just return without typing anything, the time stamp
  9833. will represent the current date/time. If there is already a timestamp
  9834. at the cursor, it will be modified."
  9835. (interactive "P")
  9836. (let* ((ts nil)
  9837. (default-time
  9838. ;; Default time is either today, or, when entering a range,
  9839. ;; the range start.
  9840. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9841. (save-excursion
  9842. (re-search-backward
  9843. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9844. (- (point) 20) t)))
  9845. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9846. (current-time)))
  9847. (default-input (and ts (org-get-compact-tod ts)))
  9848. org-time-was-given org-end-time-was-given time)
  9849. (cond
  9850. ((and (org-at-timestamp-p t)
  9851. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9852. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9853. (insert "--")
  9854. (setq time (let ((this-command this-command))
  9855. (org-read-date arg 'totime nil nil
  9856. default-time default-input)))
  9857. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9858. ((org-at-timestamp-p t)
  9859. (setq time (let ((this-command this-command))
  9860. (org-read-date arg 'totime nil nil default-time default-input)))
  9861. (when (org-at-timestamp-p t) ; just to get the match data
  9862. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9863. (replace-match "")
  9864. (setq org-last-changed-timestamp
  9865. (org-insert-time-stamp
  9866. time (or org-time-was-given arg)
  9867. inactive nil nil (list org-end-time-was-given))))
  9868. (message "Timestamp updated"))
  9869. (t
  9870. (setq time (let ((this-command this-command))
  9871. (org-read-date arg 'totime nil nil default-time default-input)))
  9872. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9873. nil nil (list org-end-time-was-given))))))
  9874. ;; FIXME: can we use this for something else, like computing time differences?
  9875. (defun org-get-compact-tod (s)
  9876. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9877. (let* ((t1 (match-string 1 s))
  9878. (h1 (string-to-number (match-string 2 s)))
  9879. (m1 (string-to-number (match-string 3 s)))
  9880. (t2 (and (match-end 4) (match-string 5 s)))
  9881. (h2 (and t2 (string-to-number (match-string 6 s))))
  9882. (m2 (and t2 (string-to-number (match-string 7 s))))
  9883. dh dm)
  9884. (if (not t2)
  9885. t1
  9886. (setq dh (- h2 h1) dm (- m2 m1))
  9887. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9888. (concat t1 "+" (number-to-string dh)
  9889. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9890. (defun org-time-stamp-inactive (&optional arg)
  9891. "Insert an inactive time stamp.
  9892. An inactive time stamp is enclosed in square brackets instead of angle
  9893. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9894. does not link to the calendar and cannot be changed with the S-cursor keys.
  9895. So these are more for recording a certain time/date."
  9896. (interactive "P")
  9897. (org-time-stamp arg 'inactive))
  9898. (defvar org-date-ovl (org-make-overlay 1 1))
  9899. (org-overlay-put org-date-ovl 'face 'org-warning)
  9900. (org-detach-overlay org-date-ovl)
  9901. (defvar org-ans1) ; dynamically scoped parameter
  9902. (defvar org-ans2) ; dynamically scoped parameter
  9903. (defvar org-plain-time-of-day-regexp) ; defined below
  9904. (defvar org-overriding-default-time nil) ; dynamically scoped
  9905. (defvar org-read-date-overlay nil)
  9906. (defvar org-dcst nil) ; dynamically scoped
  9907. (defun org-read-date (&optional with-time to-time from-string prompt
  9908. default-time default-input)
  9909. "Read a date, possibly a time, and make things smooth for the user.
  9910. The prompt will suggest to enter an ISO date, but you can also enter anything
  9911. which will at least partially be understood by `parse-time-string'.
  9912. Unrecognized parts of the date will default to the current day, month, year,
  9913. hour and minute. If this command is called to replace a timestamp at point,
  9914. of to enter the second timestamp of a range, the default time is taken from the
  9915. existing stamp. For example,
  9916. 3-2-5 --> 2003-02-05
  9917. feb 15 --> currentyear-02-15
  9918. sep 12 9 --> 2009-09-12
  9919. 12:45 --> today 12:45
  9920. 22 sept 0:34 --> currentyear-09-22 0:34
  9921. 12 --> currentyear-currentmonth-12
  9922. Fri --> nearest Friday (today or later)
  9923. etc.
  9924. Furthermore you can specify a relative date by giving, as the *first* thing
  9925. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9926. change in days weeks, months, years.
  9927. With a single plus or minus, the date is relative to today. With a double
  9928. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9929. +4d --> four days from today
  9930. +4 --> same as above
  9931. +2w --> two weeks from today
  9932. ++5 --> five days from default date
  9933. The function understands only English month and weekday abbreviations,
  9934. but this can be configured with the variables `parse-time-months' and
  9935. `parse-time-weekdays'.
  9936. While prompting, a calendar is popped up - you can also select the
  9937. date with the mouse (button 1). The calendar shows a period of three
  9938. months. To scroll it to other months, use the keys `>' and `<'.
  9939. If you don't like the calendar, turn it off with
  9940. \(setq org-read-date-popup-calendar nil)
  9941. With optional argument TO-TIME, the date will immediately be converted
  9942. to an internal time.
  9943. With an optional argument WITH-TIME, the prompt will suggest to also
  9944. insert a time. Note that when WITH-TIME is not set, you can still
  9945. enter a time, and this function will inform the calling routine about
  9946. this change. The calling routine may then choose to change the format
  9947. used to insert the time stamp into the buffer to include the time.
  9948. With optional argument FROM-STRING, read from this string instead from
  9949. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9950. the time/date that is used for everything that is not specified by the
  9951. user."
  9952. (require 'parse-time)
  9953. (let* ((org-time-stamp-rounding-minutes
  9954. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9955. (org-dcst org-display-custom-times)
  9956. (ct (org-current-time))
  9957. (def (or org-overriding-default-time default-time ct))
  9958. (defdecode (decode-time def))
  9959. (dummy (progn
  9960. (when (< (nth 2 defdecode) org-extend-today-until)
  9961. (setcar (nthcdr 2 defdecode) -1)
  9962. (setcar (nthcdr 1 defdecode) 59)
  9963. (setq def (apply 'encode-time defdecode)
  9964. defdecode (decode-time def)))))
  9965. (calendar-move-hook nil)
  9966. (calendar-view-diary-initially-flag nil)
  9967. (view-diary-entries-initially nil)
  9968. (calendar-view-holidays-initially-flag nil)
  9969. (view-calendar-holidays-initially nil)
  9970. (timestr (format-time-string
  9971. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9972. (prompt (concat (if prompt (concat prompt " ") "")
  9973. (format "Date+time [%s]: " timestr)))
  9974. ans (org-ans0 "") org-ans1 org-ans2 final)
  9975. (cond
  9976. (from-string (setq ans from-string))
  9977. (org-read-date-popup-calendar
  9978. (save-excursion
  9979. (save-window-excursion
  9980. (calendar)
  9981. (calendar-forward-day (- (time-to-days def)
  9982. (calendar-absolute-from-gregorian
  9983. (calendar-current-date))))
  9984. (org-eval-in-calendar nil t)
  9985. (let* ((old-map (current-local-map))
  9986. (map (copy-keymap calendar-mode-map))
  9987. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9988. (org-defkey map (kbd "RET") 'org-calendar-select)
  9989. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9990. 'org-calendar-select-mouse)
  9991. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9992. 'org-calendar-select-mouse)
  9993. (org-defkey minibuffer-local-map [(meta shift left)]
  9994. (lambda () (interactive)
  9995. (org-eval-in-calendar '(calendar-backward-month 1))))
  9996. (org-defkey minibuffer-local-map [(meta shift right)]
  9997. (lambda () (interactive)
  9998. (org-eval-in-calendar '(calendar-forward-month 1))))
  9999. (org-defkey minibuffer-local-map [(meta shift up)]
  10000. (lambda () (interactive)
  10001. (org-eval-in-calendar '(calendar-backward-year 1))))
  10002. (org-defkey minibuffer-local-map [(meta shift down)]
  10003. (lambda () (interactive)
  10004. (org-eval-in-calendar '(calendar-forward-year 1))))
  10005. (org-defkey minibuffer-local-map [(shift up)]
  10006. (lambda () (interactive)
  10007. (org-eval-in-calendar '(calendar-backward-week 1))))
  10008. (org-defkey minibuffer-local-map [(shift down)]
  10009. (lambda () (interactive)
  10010. (org-eval-in-calendar '(calendar-forward-week 1))))
  10011. (org-defkey minibuffer-local-map [(shift left)]
  10012. (lambda () (interactive)
  10013. (org-eval-in-calendar '(calendar-backward-day 1))))
  10014. (org-defkey minibuffer-local-map [(shift right)]
  10015. (lambda () (interactive)
  10016. (org-eval-in-calendar '(calendar-forward-day 1))))
  10017. (org-defkey minibuffer-local-map ">"
  10018. (lambda () (interactive)
  10019. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10020. (org-defkey minibuffer-local-map "<"
  10021. (lambda () (interactive)
  10022. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10023. (unwind-protect
  10024. (progn
  10025. (use-local-map map)
  10026. (add-hook 'post-command-hook 'org-read-date-display)
  10027. (setq org-ans0 (read-string prompt default-input nil nil))
  10028. ;; org-ans0: from prompt
  10029. ;; org-ans1: from mouse click
  10030. ;; org-ans2: from calendar motion
  10031. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10032. (remove-hook 'post-command-hook 'org-read-date-display)
  10033. (use-local-map old-map)
  10034. (when org-read-date-overlay
  10035. (org-delete-overlay org-read-date-overlay)
  10036. (setq org-read-date-overlay nil)))))))
  10037. (t ; Naked prompt only
  10038. (unwind-protect
  10039. (setq ans (read-string prompt default-input nil timestr))
  10040. (when org-read-date-overlay
  10041. (org-delete-overlay org-read-date-overlay)
  10042. (setq org-read-date-overlay nil)))))
  10043. (setq final (org-read-date-analyze ans def defdecode))
  10044. (if to-time
  10045. (apply 'encode-time final)
  10046. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10047. (format "%04d-%02d-%02d %02d:%02d"
  10048. (nth 5 final) (nth 4 final) (nth 3 final)
  10049. (nth 2 final) (nth 1 final))
  10050. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10051. (defvar def)
  10052. (defvar defdecode)
  10053. (defvar with-time)
  10054. (defun org-read-date-display ()
  10055. "Display the current date prompt interpretation in the minibuffer."
  10056. (when org-read-date-display-live
  10057. (when org-read-date-overlay
  10058. (org-delete-overlay org-read-date-overlay))
  10059. (let ((p (point)))
  10060. (end-of-line 1)
  10061. (while (not (equal (buffer-substring
  10062. (max (point-min) (- (point) 4)) (point))
  10063. " "))
  10064. (insert " "))
  10065. (goto-char p))
  10066. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10067. " " (or org-ans1 org-ans2)))
  10068. (org-end-time-was-given nil)
  10069. (f (org-read-date-analyze ans def defdecode))
  10070. (fmts (if org-dcst
  10071. org-time-stamp-custom-formats
  10072. org-time-stamp-formats))
  10073. (fmt (if (or with-time
  10074. (and (boundp 'org-time-was-given) org-time-was-given))
  10075. (cdr fmts)
  10076. (car fmts)))
  10077. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10078. (when (and org-end-time-was-given
  10079. (string-match org-plain-time-of-day-regexp txt))
  10080. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10081. org-end-time-was-given
  10082. (substring txt (match-end 0)))))
  10083. (setq org-read-date-overlay
  10084. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10085. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10086. (defun org-read-date-analyze (ans def defdecode)
  10087. "Analyse the combined answer of the date prompt."
  10088. ;; FIXME: cleanup and comment
  10089. (let (delta deltan deltaw deltadef year month day
  10090. hour minute second wday pm h2 m2 tl wday1
  10091. iso-year iso-weekday iso-week iso-year iso-date)
  10092. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10093. (setq ans "+0"))
  10094. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10095. (setq ans (replace-match "" t t ans)
  10096. deltan (car delta)
  10097. deltaw (nth 1 delta)
  10098. deltadef (nth 2 delta)))
  10099. ;; Check if there is an iso week date in there
  10100. ;; If yes, sore the info and postpone interpreting it until the rest
  10101. ;; of the parsing is done
  10102. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10103. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10104. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10105. iso-week (string-to-number (match-string 2 ans)))
  10106. (setq ans (replace-match "" t t ans)))
  10107. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10108. (when (string-match
  10109. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10110. (setq year (if (match-end 2)
  10111. (string-to-number (match-string 2 ans))
  10112. (string-to-number (format-time-string "%Y")))
  10113. month (string-to-number (match-string 3 ans))
  10114. day (string-to-number (match-string 4 ans)))
  10115. (if (< year 100) (setq year (+ 2000 year)))
  10116. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10117. t nil ans)))
  10118. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10119. ;; If there is a time with am/pm, and *no* time without it, we convert
  10120. ;; so that matching will be successful.
  10121. (loop for i from 1 to 2 do ; twice, for end time as well
  10122. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10123. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10124. (setq hour (string-to-number (match-string 1 ans))
  10125. minute (if (match-end 3)
  10126. (string-to-number (match-string 3 ans))
  10127. 0)
  10128. pm (equal ?p
  10129. (string-to-char (downcase (match-string 4 ans)))))
  10130. (if (and (= hour 12) (not pm))
  10131. (setq hour 0)
  10132. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10133. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10134. t t ans))))
  10135. ;; Check if a time range is given as a duration
  10136. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10137. (setq hour (string-to-number (match-string 1 ans))
  10138. h2 (+ hour (string-to-number (match-string 3 ans)))
  10139. minute (string-to-number (match-string 2 ans))
  10140. m2 (+ minute (if (match-end 5) (string-to-number
  10141. (match-string 5 ans))0)))
  10142. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10143. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10144. t t ans)))
  10145. ;; Check if there is a time range
  10146. (when (boundp 'org-end-time-was-given)
  10147. (setq org-time-was-given nil)
  10148. (when (and (string-match org-plain-time-of-day-regexp ans)
  10149. (match-end 8))
  10150. (setq org-end-time-was-given (match-string 8 ans))
  10151. (setq ans (concat (substring ans 0 (match-beginning 7))
  10152. (substring ans (match-end 7))))))
  10153. (setq tl (parse-time-string ans)
  10154. day (or (nth 3 tl) (nth 3 defdecode))
  10155. month (or (nth 4 tl)
  10156. (if (and org-read-date-prefer-future
  10157. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10158. (1+ (nth 4 defdecode))
  10159. (nth 4 defdecode)))
  10160. year (or (nth 5 tl)
  10161. (if (and org-read-date-prefer-future
  10162. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10163. (1+ (nth 5 defdecode))
  10164. (nth 5 defdecode)))
  10165. hour (or (nth 2 tl) (nth 2 defdecode))
  10166. minute (or (nth 1 tl) (nth 1 defdecode))
  10167. second (or (nth 0 tl) 0)
  10168. wday (nth 6 tl))
  10169. ;; Special date definitions below
  10170. (cond
  10171. (iso-week
  10172. ;; There was an iso week
  10173. (setq year (or iso-year year)
  10174. day (or iso-weekday wday 1)
  10175. wday nil ; to make sure that the trigger below does not match
  10176. iso-date (calendar-gregorian-from-absolute
  10177. (calendar-absolute-from-iso
  10178. (list iso-week day year))))
  10179. ; FIXME: Should we also push ISO weeks into the future?
  10180. ; (when (and org-read-date-prefer-future
  10181. ; (not iso-year)
  10182. ; (< (calendar-absolute-from-gregorian iso-date)
  10183. ; (time-to-days (current-time))))
  10184. ; (setq year (1+ year)
  10185. ; iso-date (calendar-gregorian-from-absolute
  10186. ; (calendar-absolute-from-iso
  10187. ; (list iso-week day year)))))
  10188. (setq month (car iso-date)
  10189. year (nth 2 iso-date)
  10190. day (nth 1 iso-date)))
  10191. (deltan
  10192. (unless deltadef
  10193. (let ((now (decode-time (current-time))))
  10194. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10195. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10196. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10197. ((equal deltaw "m") (setq month (+ month deltan)))
  10198. ((equal deltaw "y") (setq year (+ year deltan)))))
  10199. ((and wday (not (nth 3 tl)))
  10200. ;; Weekday was given, but no day, so pick that day in the week
  10201. ;; on or after the derived date.
  10202. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10203. (unless (equal wday wday1)
  10204. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10205. (if (and (boundp 'org-time-was-given)
  10206. (nth 2 tl))
  10207. (setq org-time-was-given t))
  10208. (if (< year 100) (setq year (+ 2000 year)))
  10209. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10210. (list second minute hour day month year)))
  10211. (defvar parse-time-weekdays)
  10212. (defun org-read-date-get-relative (s today default)
  10213. "Check string S for special relative date string.
  10214. TODAY and DEFAULT are internal times, for today and for a default.
  10215. Return shift list (N what def-flag)
  10216. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10217. N is the number of WHATs to shift.
  10218. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10219. the DEFAULT date rather than TODAY."
  10220. (when (and
  10221. (string-match
  10222. (concat
  10223. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10224. "\\([0-9]+\\)?"
  10225. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10226. "\\([ \t]\\|$\\)") s)
  10227. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10228. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10229. (string-to-char (substring (match-string 1 s) -1))
  10230. ?+))
  10231. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10232. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10233. (what (if (match-end 3) (match-string 3 s) "d"))
  10234. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10235. (date (if rel default today))
  10236. (wday (nth 6 (decode-time date)))
  10237. delta)
  10238. (if wday1
  10239. (progn
  10240. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10241. (if (= dir ?-) (setq delta (- delta 7)))
  10242. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10243. (list delta "d" rel))
  10244. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10245. (defun org-eval-in-calendar (form &optional keepdate)
  10246. "Eval FORM in the calendar window and return to current window.
  10247. Also, store the cursor date in variable org-ans2."
  10248. (let ((sw (selected-window)))
  10249. (select-window (get-buffer-window "*Calendar*"))
  10250. (eval form)
  10251. (when (and (not keepdate) (calendar-cursor-to-date))
  10252. (let* ((date (calendar-cursor-to-date))
  10253. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10254. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10255. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10256. (select-window sw)))
  10257. (defun org-calendar-select ()
  10258. "Return to `org-read-date' with the date currently selected.
  10259. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10260. (interactive)
  10261. (when (calendar-cursor-to-date)
  10262. (let* ((date (calendar-cursor-to-date))
  10263. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10264. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10265. (if (active-minibuffer-window) (exit-minibuffer))))
  10266. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10267. "Insert a date stamp for the date given by the internal TIME.
  10268. WITH-HM means, use the stamp format that includes the time of the day.
  10269. INACTIVE means use square brackets instead of angular ones, so that the
  10270. stamp will not contribute to the agenda.
  10271. PRE and POST are optional strings to be inserted before and after the
  10272. stamp.
  10273. The command returns the inserted time stamp."
  10274. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10275. stamp)
  10276. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10277. (insert-before-markers (or pre ""))
  10278. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10279. (when (listp extra)
  10280. (setq extra (car extra))
  10281. (if (and (stringp extra)
  10282. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10283. (setq extra (format "-%02d:%02d"
  10284. (string-to-number (match-string 1 extra))
  10285. (string-to-number (match-string 2 extra))))
  10286. (setq extra nil)))
  10287. (when extra
  10288. (backward-char 1)
  10289. (insert-before-markers extra)
  10290. (forward-char 1))
  10291. (insert-before-markers (or post ""))
  10292. (setq org-last-inserted-timestamp stamp)))
  10293. (defun org-toggle-time-stamp-overlays ()
  10294. "Toggle the use of custom time stamp formats."
  10295. (interactive)
  10296. (setq org-display-custom-times (not org-display-custom-times))
  10297. (unless org-display-custom-times
  10298. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10299. (while (setq p (next-single-property-change p 'display))
  10300. (if (and (get-text-property p 'display)
  10301. (eq (get-text-property p 'face) 'org-date))
  10302. (remove-text-properties
  10303. p (setq p (next-single-property-change p 'display))
  10304. '(display t))))
  10305. (set-buffer-modified-p bmp)))
  10306. (if (featurep 'xemacs)
  10307. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10308. (org-restart-font-lock)
  10309. (setq org-table-may-need-update t)
  10310. (if org-display-custom-times
  10311. (message "Time stamps are overlayed with custom format")
  10312. (message "Time stamp overlays removed")))
  10313. (defun org-display-custom-time (beg end)
  10314. "Overlay modified time stamp format over timestamp between BEG and END."
  10315. (let* ((ts (buffer-substring beg end))
  10316. t1 w1 with-hm tf time str w2 (off 0))
  10317. (save-match-data
  10318. (setq t1 (org-parse-time-string ts t))
  10319. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10320. (setq off (- (match-end 0) (match-beginning 0)))))
  10321. (setq end (- end off))
  10322. (setq w1 (- end beg)
  10323. with-hm (and (nth 1 t1) (nth 2 t1))
  10324. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10325. time (org-fix-decoded-time t1)
  10326. str (org-add-props
  10327. (format-time-string
  10328. (substring tf 1 -1) (apply 'encode-time time))
  10329. nil 'mouse-face 'highlight)
  10330. w2 (length str))
  10331. (if (not (= w2 w1))
  10332. (add-text-properties (1+ beg) (+ 2 beg)
  10333. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10334. (if (featurep 'xemacs)
  10335. (progn
  10336. (put-text-property beg end 'invisible t)
  10337. (put-text-property beg end 'end-glyph (make-glyph str)))
  10338. (put-text-property beg end 'display str))))
  10339. (defun org-translate-time (string)
  10340. "Translate all timestamps in STRING to custom format.
  10341. But do this only if the variable `org-display-custom-times' is set."
  10342. (when org-display-custom-times
  10343. (save-match-data
  10344. (let* ((start 0)
  10345. (re org-ts-regexp-both)
  10346. t1 with-hm inactive tf time str beg end)
  10347. (while (setq start (string-match re string start))
  10348. (setq beg (match-beginning 0)
  10349. end (match-end 0)
  10350. t1 (save-match-data
  10351. (org-parse-time-string (substring string beg end) t))
  10352. with-hm (and (nth 1 t1) (nth 2 t1))
  10353. inactive (equal (substring string beg (1+ beg)) "[")
  10354. tf (funcall (if with-hm 'cdr 'car)
  10355. org-time-stamp-custom-formats)
  10356. time (org-fix-decoded-time t1)
  10357. str (format-time-string
  10358. (concat
  10359. (if inactive "[" "<") (substring tf 1 -1)
  10360. (if inactive "]" ">"))
  10361. (apply 'encode-time time))
  10362. string (replace-match str t t string)
  10363. start (+ start (length str)))))))
  10364. string)
  10365. (defun org-fix-decoded-time (time)
  10366. "Set 0 instead of nil for the first 6 elements of time.
  10367. Don't touch the rest."
  10368. (let ((n 0))
  10369. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10370. (defun org-days-to-time (timestamp-string)
  10371. "Difference between TIMESTAMP-STRING and now in days."
  10372. (- (time-to-days (org-time-string-to-time timestamp-string))
  10373. (time-to-days (current-time))))
  10374. (defun org-deadline-close (timestamp-string &optional ndays)
  10375. "Is the time in TIMESTAMP-STRING close to the current date?"
  10376. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10377. (and (< (org-days-to-time timestamp-string) ndays)
  10378. (not (org-entry-is-done-p))))
  10379. (defun org-get-wdays (ts)
  10380. "Get the deadline lead time appropriate for timestring TS."
  10381. (cond
  10382. ((<= org-deadline-warning-days 0)
  10383. ;; 0 or negative, enforce this value no matter what
  10384. (- org-deadline-warning-days))
  10385. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10386. ;; lead time is specified.
  10387. (floor (* (string-to-number (match-string 1 ts))
  10388. (cdr (assoc (match-string 2 ts)
  10389. '(("d" . 1) ("w" . 7)
  10390. ("m" . 30.4) ("y" . 365.25)))))))
  10391. ;; go for the default.
  10392. (t org-deadline-warning-days)))
  10393. (defun org-calendar-select-mouse (ev)
  10394. "Return to `org-read-date' with the date currently selected.
  10395. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10396. (interactive "e")
  10397. (mouse-set-point ev)
  10398. (when (calendar-cursor-to-date)
  10399. (let* ((date (calendar-cursor-to-date))
  10400. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10401. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10402. (if (active-minibuffer-window) (exit-minibuffer))))
  10403. (defun org-check-deadlines (ndays)
  10404. "Check if there are any deadlines due or past due.
  10405. A deadline is considered due if it happens within `org-deadline-warning-days'
  10406. days from today's date. If the deadline appears in an entry marked DONE,
  10407. it is not shown. The prefix arg NDAYS can be used to test that many
  10408. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10409. (interactive "P")
  10410. (let* ((org-warn-days
  10411. (cond
  10412. ((equal ndays '(4)) 100000)
  10413. (ndays (prefix-numeric-value ndays))
  10414. (t (abs org-deadline-warning-days))))
  10415. (case-fold-search nil)
  10416. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10417. (callback
  10418. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10419. (message "%d deadlines past-due or due within %d days"
  10420. (org-occur regexp nil callback)
  10421. org-warn-days)))
  10422. (defun org-check-before-date (date)
  10423. "Check if there are deadlines or scheduled entries before DATE."
  10424. (interactive (list (org-read-date)))
  10425. (let ((case-fold-search nil)
  10426. (regexp (concat "\\<\\(" org-deadline-string
  10427. "\\|" org-scheduled-string
  10428. "\\) *<\\([^>]+\\)>"))
  10429. (callback
  10430. (lambda () (time-less-p
  10431. (org-time-string-to-time (match-string 2))
  10432. (org-time-string-to-time date)))))
  10433. (message "%d entries before %s"
  10434. (org-occur regexp nil callback) date)))
  10435. (defun org-evaluate-time-range (&optional to-buffer)
  10436. "Evaluate a time range by computing the difference between start and end.
  10437. Normally the result is just printed in the echo area, but with prefix arg
  10438. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10439. If the time range is actually in a table, the result is inserted into the
  10440. next column.
  10441. For time difference computation, a year is assumed to be exactly 365
  10442. days in order to avoid rounding problems."
  10443. (interactive "P")
  10444. (or
  10445. (org-clock-update-time-maybe)
  10446. (save-excursion
  10447. (unless (org-at-date-range-p t)
  10448. (goto-char (point-at-bol))
  10449. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10450. (if (not (org-at-date-range-p t))
  10451. (error "Not at a time-stamp range, and none found in current line")))
  10452. (let* ((ts1 (match-string 1))
  10453. (ts2 (match-string 2))
  10454. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10455. (match-end (match-end 0))
  10456. (time1 (org-time-string-to-time ts1))
  10457. (time2 (org-time-string-to-time ts2))
  10458. (t1 (time-to-seconds time1))
  10459. (t2 (time-to-seconds time2))
  10460. (diff (abs (- t2 t1)))
  10461. (negative (< (- t2 t1) 0))
  10462. ;; (ys (floor (* 365 24 60 60)))
  10463. (ds (* 24 60 60))
  10464. (hs (* 60 60))
  10465. (fy "%dy %dd %02d:%02d")
  10466. (fy1 "%dy %dd")
  10467. (fd "%dd %02d:%02d")
  10468. (fd1 "%dd")
  10469. (fh "%02d:%02d")
  10470. y d h m align)
  10471. (if havetime
  10472. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10473. y 0
  10474. d (floor (/ diff ds)) diff (mod diff ds)
  10475. h (floor (/ diff hs)) diff (mod diff hs)
  10476. m (floor (/ diff 60)))
  10477. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10478. y 0
  10479. d (floor (+ (/ diff ds) 0.5))
  10480. h 0 m 0))
  10481. (if (not to-buffer)
  10482. (message "%s" (org-make-tdiff-string y d h m))
  10483. (if (org-at-table-p)
  10484. (progn
  10485. (goto-char match-end)
  10486. (setq align t)
  10487. (and (looking-at " *|") (goto-char (match-end 0))))
  10488. (goto-char match-end))
  10489. (if (looking-at
  10490. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10491. (replace-match ""))
  10492. (if negative (insert " -"))
  10493. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10494. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10495. (insert " " (format fh h m))))
  10496. (if align (org-table-align))
  10497. (message "Time difference inserted")))))
  10498. (defun org-make-tdiff-string (y d h m)
  10499. (let ((fmt "")
  10500. (l nil))
  10501. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10502. l (push y l)))
  10503. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10504. l (push d l)))
  10505. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10506. l (push h l)))
  10507. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10508. l (push m l)))
  10509. (apply 'format fmt (nreverse l))))
  10510. (defun org-time-string-to-time (s)
  10511. (apply 'encode-time (org-parse-time-string s)))
  10512. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10513. "Convert a time stamp to an absolute day number.
  10514. If there is a specifyer for a cyclic time stamp, get the closest date to
  10515. DAYNR.
  10516. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10517. (cond
  10518. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10519. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10520. daynr
  10521. (+ daynr 1000)))
  10522. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10523. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10524. (time-to-days (current-time))) (match-string 0 s)
  10525. prefer show-all))
  10526. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10527. (defun org-days-to-iso-week (days)
  10528. "Return the iso week number."
  10529. (require 'cal-iso)
  10530. (car (calendar-iso-from-absolute days)))
  10531. (defun org-small-year-to-year (year)
  10532. "Convert 2-digit years into 4-digit years.
  10533. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10534. The year 2000 cannot be abbreviated. Any year larger than 99
  10535. is returned unchanged."
  10536. (if (< year 38)
  10537. (setq year (+ 2000 year))
  10538. (if (< year 100)
  10539. (setq year (+ 1900 year))))
  10540. year)
  10541. (defun org-time-from-absolute (d)
  10542. "Return the time corresponding to date D.
  10543. D may be an absolute day number, or a calendar-type list (month day year)."
  10544. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10545. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10546. (defun org-calendar-holiday ()
  10547. "List of holidays, for Diary display in Org-mode."
  10548. (require 'holidays)
  10549. (let ((hl (funcall
  10550. (if (fboundp 'calendar-check-holidays)
  10551. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10552. (if hl (mapconcat 'identity hl "; "))))
  10553. (defun org-diary-sexp-entry (sexp entry date)
  10554. "Process a SEXP diary ENTRY for DATE."
  10555. (require 'diary-lib)
  10556. (let ((result (if calendar-debug-sexp
  10557. (let ((stack-trace-on-error t))
  10558. (eval (car (read-from-string sexp))))
  10559. (condition-case nil
  10560. (eval (car (read-from-string sexp)))
  10561. (error
  10562. (beep)
  10563. (message "Bad sexp at line %d in %s: %s"
  10564. (org-current-line)
  10565. (buffer-file-name) sexp)
  10566. (sleep-for 2))))))
  10567. (cond ((stringp result) result)
  10568. ((and (consp result)
  10569. (stringp (cdr result))) (cdr result))
  10570. (result entry)
  10571. (t nil))))
  10572. (defun org-diary-to-ical-string (frombuf)
  10573. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10574. This uses the icalendar.el library."
  10575. (let* ((tmpdir (if (featurep 'xemacs)
  10576. (temp-directory)
  10577. temporary-file-directory))
  10578. (tmpfile (make-temp-name
  10579. (expand-file-name "orgics" tmpdir)))
  10580. buf rtn b e)
  10581. (save-excursion
  10582. (set-buffer frombuf)
  10583. (icalendar-export-region (point-min) (point-max) tmpfile)
  10584. (setq buf (find-buffer-visiting tmpfile))
  10585. (set-buffer buf)
  10586. (goto-char (point-min))
  10587. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10588. (setq b (match-beginning 0)))
  10589. (goto-char (point-max))
  10590. (if (re-search-backward "^END:VEVENT" nil t)
  10591. (setq e (match-end 0)))
  10592. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10593. (kill-buffer buf)
  10594. (delete-file tmpfile)
  10595. rtn))
  10596. (defun org-closest-date (start current change prefer show-all)
  10597. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10598. When PREFER is `past' return a date that is either CURRENT or past.
  10599. When PREFER is `future', return a date that is either CURRENT or future.
  10600. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10601. ;; Make the proper lists from the dates
  10602. (catch 'exit
  10603. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10604. dn dw sday cday n1 n2
  10605. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10606. (setq start (org-date-to-gregorian start)
  10607. current (org-date-to-gregorian
  10608. (if show-all
  10609. current
  10610. (time-to-days (current-time))))
  10611. sday (calendar-absolute-from-gregorian start)
  10612. cday (calendar-absolute-from-gregorian current))
  10613. (if (<= cday sday) (throw 'exit sday))
  10614. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10615. (setq dn (string-to-number (match-string 1 change))
  10616. dw (cdr (assoc (match-string 2 change) a1)))
  10617. (error "Invalid change specifyer: %s" change))
  10618. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10619. (cond
  10620. ((eq dw 'day)
  10621. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10622. n2 (+ n1 dn)))
  10623. ((eq dw 'year)
  10624. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10625. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10626. (setq date1 (list m d y1)
  10627. n1 (calendar-absolute-from-gregorian date1)
  10628. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10629. n2 (calendar-absolute-from-gregorian date2)))
  10630. ((eq dw 'month)
  10631. ;; approx number of month between the two dates
  10632. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10633. ;; How often does dn fit in there?
  10634. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10635. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10636. m (+ m nm)
  10637. ny (floor (/ m 12))
  10638. y (+ y ny)
  10639. m (- m (* ny 12)))
  10640. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10641. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10642. (setq m2 (+ m dn) y2 y)
  10643. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10644. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10645. (while (<= n2 cday)
  10646. (setq n1 n2 m m2 y y2)
  10647. (setq m2 (+ m dn) y2 y)
  10648. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10649. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10650. (if show-all
  10651. (cond
  10652. ((eq prefer 'past) n1)
  10653. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10654. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10655. (cond
  10656. ((eq prefer 'past) n1)
  10657. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10658. (t (if (= cday n1) n1 n2)))))))
  10659. (defun org-date-to-gregorian (date)
  10660. "Turn any specification of DATE into a gregorian date for the calendar."
  10661. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10662. ((and (listp date) (= (length date) 3)) date)
  10663. ((stringp date)
  10664. (setq date (org-parse-time-string date))
  10665. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10666. ((listp date)
  10667. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10668. (defun org-parse-time-string (s &optional nodefault)
  10669. "Parse the standard Org-mode time string.
  10670. This should be a lot faster than the normal `parse-time-string'.
  10671. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10672. hour and minute fields will be nil if not given."
  10673. (if (string-match org-ts-regexp0 s)
  10674. (list 0
  10675. (if (or (match-beginning 8) (not nodefault))
  10676. (string-to-number (or (match-string 8 s) "0")))
  10677. (if (or (match-beginning 7) (not nodefault))
  10678. (string-to-number (or (match-string 7 s) "0")))
  10679. (string-to-number (match-string 4 s))
  10680. (string-to-number (match-string 3 s))
  10681. (string-to-number (match-string 2 s))
  10682. nil nil nil)
  10683. (make-list 9 0)))
  10684. (defun org-timestamp-up (&optional arg)
  10685. "Increase the date item at the cursor by one.
  10686. If the cursor is on the year, change the year. If it is on the month or
  10687. the day, change that.
  10688. With prefix ARG, change by that many units."
  10689. (interactive "p")
  10690. (org-timestamp-change (prefix-numeric-value arg)))
  10691. (defun org-timestamp-down (&optional arg)
  10692. "Decrease the date item at the cursor by one.
  10693. If the cursor is on the year, change the year. If it is on the month or
  10694. the day, change that.
  10695. With prefix ARG, change by that many units."
  10696. (interactive "p")
  10697. (org-timestamp-change (- (prefix-numeric-value arg))))
  10698. (defun org-timestamp-up-day (&optional arg)
  10699. "Increase the date in the time stamp by one day.
  10700. With prefix ARG, change that many days."
  10701. (interactive "p")
  10702. (if (and (not (org-at-timestamp-p t))
  10703. (org-on-heading-p))
  10704. (org-todo 'up)
  10705. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10706. (defun org-timestamp-down-day (&optional arg)
  10707. "Decrease the date in the time stamp by one day.
  10708. With prefix ARG, change that many days."
  10709. (interactive "p")
  10710. (if (and (not (org-at-timestamp-p t))
  10711. (org-on-heading-p))
  10712. (org-todo 'down)
  10713. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10714. (defun org-at-timestamp-p (&optional inactive-ok)
  10715. "Determine if the cursor is in or at a timestamp."
  10716. (interactive)
  10717. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10718. (pos (point))
  10719. (ans (or (looking-at tsr)
  10720. (save-excursion
  10721. (skip-chars-backward "^[<\n\r\t")
  10722. (if (> (point) (point-min)) (backward-char 1))
  10723. (and (looking-at tsr)
  10724. (> (- (match-end 0) pos) -1))))))
  10725. (and ans
  10726. (boundp 'org-ts-what)
  10727. (setq org-ts-what
  10728. (cond
  10729. ((= pos (match-beginning 0)) 'bracket)
  10730. ((= pos (1- (match-end 0))) 'bracket)
  10731. ((org-pos-in-match-range pos 2) 'year)
  10732. ((org-pos-in-match-range pos 3) 'month)
  10733. ((org-pos-in-match-range pos 7) 'hour)
  10734. ((org-pos-in-match-range pos 8) 'minute)
  10735. ((or (org-pos-in-match-range pos 4)
  10736. (org-pos-in-match-range pos 5)) 'day)
  10737. ((and (> pos (or (match-end 8) (match-end 5)))
  10738. (< pos (match-end 0)))
  10739. (- pos (or (match-end 8) (match-end 5))))
  10740. (t 'day))))
  10741. ans))
  10742. (defun org-toggle-timestamp-type ()
  10743. "Toggle the type (<active> or [inactive]) of a time stamp."
  10744. (interactive)
  10745. (when (org-at-timestamp-p t)
  10746. (let ((beg (match-beginning 0)) (end (match-end 0))
  10747. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10748. (save-excursion
  10749. (goto-char beg)
  10750. (while (re-search-forward "[][<>]" end t)
  10751. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10752. t t)))
  10753. (message "Timestamp is now %sactive"
  10754. (if (equal (char-after beg) ?<) "" "in")))))
  10755. (defun org-timestamp-change (n &optional what)
  10756. "Change the date in the time stamp at point.
  10757. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10758. `year', `minute', `second'. If WHAT is not given, the cursor position
  10759. in the timestamp determines what will be changed."
  10760. (let ((pos (point))
  10761. with-hm inactive
  10762. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10763. org-ts-what
  10764. extra rem
  10765. ts time time0)
  10766. (if (not (org-at-timestamp-p t))
  10767. (error "Not at a timestamp"))
  10768. (if (and (not what) (eq org-ts-what 'bracket))
  10769. (org-toggle-timestamp-type)
  10770. (if (and (not what) (not (eq org-ts-what 'day))
  10771. org-display-custom-times
  10772. (get-text-property (point) 'display)
  10773. (not (get-text-property (1- (point)) 'display)))
  10774. (setq org-ts-what 'day))
  10775. (setq org-ts-what (or what org-ts-what)
  10776. inactive (= (char-after (match-beginning 0)) ?\[)
  10777. ts (match-string 0))
  10778. (replace-match "")
  10779. (if (string-match
  10780. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10781. ts)
  10782. (setq extra (match-string 1 ts)))
  10783. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10784. (setq with-hm t))
  10785. (setq time0 (org-parse-time-string ts))
  10786. (when (and (eq org-ts-what 'minute)
  10787. (eq current-prefix-arg nil))
  10788. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10789. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10790. (setcar (cdr time0) (+ (nth 1 time0)
  10791. (if (> n 0) (- rem) (- dm rem))))))
  10792. (setq time
  10793. (encode-time (or (car time0) 0)
  10794. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10795. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10796. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10797. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10798. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10799. (nthcdr 6 time0)))
  10800. (when (integerp org-ts-what)
  10801. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10802. (if (eq what 'calendar)
  10803. (let ((cal-date (org-get-date-from-calendar)))
  10804. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10805. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10806. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10807. (setcar time0 (or (car time0) 0))
  10808. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10809. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10810. (setq time (apply 'encode-time time0))))
  10811. (setq org-last-changed-timestamp
  10812. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10813. (org-clock-update-time-maybe)
  10814. (goto-char pos)
  10815. ;; Try to recenter the calendar window, if any
  10816. (if (and org-calendar-follow-timestamp-change
  10817. (get-buffer-window "*Calendar*" t)
  10818. (memq org-ts-what '(day month year)))
  10819. (org-recenter-calendar (time-to-days time))))))
  10820. (defun org-modify-ts-extra (s pos n dm)
  10821. "Change the different parts of the lead-time and repeat fields in timestamp."
  10822. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10823. ng h m new rem)
  10824. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10825. (cond
  10826. ((or (org-pos-in-match-range pos 2)
  10827. (org-pos-in-match-range pos 3))
  10828. (setq m (string-to-number (match-string 3 s))
  10829. h (string-to-number (match-string 2 s)))
  10830. (if (org-pos-in-match-range pos 2)
  10831. (setq h (+ h n))
  10832. (setq n (* dm (org-no-warnings (signum n))))
  10833. (when (not (= 0 (setq rem (% m dm))))
  10834. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10835. (setq m (+ m n)))
  10836. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10837. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10838. (setq h (min 24 (max 0 h)))
  10839. (setq ng 1 new (format "-%02d:%02d" h m)))
  10840. ((org-pos-in-match-range pos 6)
  10841. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10842. ((org-pos-in-match-range pos 5)
  10843. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10844. ((org-pos-in-match-range pos 9)
  10845. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10846. ((org-pos-in-match-range pos 8)
  10847. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10848. (when ng
  10849. (setq s (concat
  10850. (substring s 0 (match-beginning ng))
  10851. new
  10852. (substring s (match-end ng))))))
  10853. s))
  10854. (defun org-recenter-calendar (date)
  10855. "If the calendar is visible, recenter it to DATE."
  10856. (let* ((win (selected-window))
  10857. (cwin (get-buffer-window "*Calendar*" t))
  10858. (calendar-move-hook nil))
  10859. (when cwin
  10860. (select-window cwin)
  10861. (calendar-goto-date (if (listp date) date
  10862. (calendar-gregorian-from-absolute date)))
  10863. (select-window win))))
  10864. (defun org-goto-calendar (&optional arg)
  10865. "Go to the Emacs calendar at the current date.
  10866. If there is a time stamp in the current line, go to that date.
  10867. A prefix ARG can be used to force the current date."
  10868. (interactive "P")
  10869. (let ((tsr org-ts-regexp) diff
  10870. (calendar-move-hook nil)
  10871. (calendar-view-holidays-initially-flag nil)
  10872. (view-calendar-holidays-initially nil)
  10873. (calendar-view-diary-initially-flag nil)
  10874. (view-diary-entries-initially nil))
  10875. (if (or (org-at-timestamp-p)
  10876. (save-excursion
  10877. (beginning-of-line 1)
  10878. (looking-at (concat ".*" tsr))))
  10879. (let ((d1 (time-to-days (current-time)))
  10880. (d2 (time-to-days
  10881. (org-time-string-to-time (match-string 1)))))
  10882. (setq diff (- d2 d1))))
  10883. (calendar)
  10884. (calendar-goto-today)
  10885. (if (and diff (not arg)) (calendar-forward-day diff))))
  10886. (defun org-get-date-from-calendar ()
  10887. "Return a list (month day year) of date at point in calendar."
  10888. (with-current-buffer "*Calendar*"
  10889. (save-match-data
  10890. (calendar-cursor-to-date))))
  10891. (defun org-date-from-calendar ()
  10892. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10893. If there is already a time stamp at the cursor position, update it."
  10894. (interactive)
  10895. (if (org-at-timestamp-p t)
  10896. (org-timestamp-change 0 'calendar)
  10897. (let ((cal-date (org-get-date-from-calendar)))
  10898. (org-insert-time-stamp
  10899. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10900. (defun org-minutes-to-hh:mm-string (m)
  10901. "Compute H:MM from a number of minutes."
  10902. (let ((h (/ m 60)))
  10903. (setq m (- m (* 60 h)))
  10904. (format org-time-clocksum-format h m)))
  10905. (defun org-hh:mm-string-to-minutes (s)
  10906. "Convert a string H:MM to a number of minutes."
  10907. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10908. (+ (* (string-to-number (match-string 1 s)) 60)
  10909. (string-to-number (match-string 2 s)))
  10910. 0))
  10911. ;;;; Agenda files
  10912. ;;;###autoload
  10913. (defun org-iswitchb (&optional arg)
  10914. "Use `iswitchb-read-buffer' 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. Due to some yet unresolved reason, the global function
  10918. `iswitchb-mode' needs to be active for this function to work."
  10919. (interactive "P")
  10920. (require 'iswitchb)
  10921. (let ((enabled iswitchb-mode) blist)
  10922. (or enabled (iswitchb-mode 1))
  10923. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10924. ((equal arg '(16)) (org-buffer-list 'agenda))
  10925. (t (org-buffer-list))))
  10926. (unwind-protect
  10927. (let ((iswitchb-make-buflist-hook
  10928. (lambda ()
  10929. (setq iswitchb-temp-buflist
  10930. (mapcar 'buffer-name blist)))))
  10931. (switch-to-buffer
  10932. (iswitchb-read-buffer
  10933. "Switch-to: " nil t))
  10934. (or enabled (iswitchb-mode -1))))))
  10935. ;;;###autoload
  10936. (defun org-ido-switchb (&optional arg)
  10937. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  10938. With a prefix argument, restrict available to files.
  10939. With two prefix arguments, restrict available buffers to agenda files."
  10940. (interactive "P")
  10941. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10942. ((equal arg '(16)) (org-buffer-list 'agenda))
  10943. (t (org-buffer-list)))))
  10944. (switch-to-buffer
  10945. (org-ido-completing-read "Org buffer: "
  10946. (mapcar 'buffer-name blist)
  10947. nil t))))
  10948. (defun org-buffer-list (&optional predicate exclude-tmp)
  10949. "Return a list of Org buffers.
  10950. PREDICATE can be `export', `files' or `agenda'.
  10951. export restrict the list to Export buffers.
  10952. files restrict the list to buffers visiting Org files.
  10953. agenda restrict the list to buffers visiting agenda files.
  10954. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10955. (let* ((bfn nil)
  10956. (agenda-files (and (eq predicate 'agenda)
  10957. (mapcar 'file-truename (org-agenda-files t))))
  10958. (filter
  10959. (cond
  10960. ((eq predicate 'files)
  10961. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10962. ((eq predicate 'export)
  10963. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  10964. ((eq predicate 'agenda)
  10965. (lambda (b)
  10966. (with-current-buffer b
  10967. (and (eq major-mode 'org-mode)
  10968. (setq bfn (buffer-file-name b))
  10969. (member (file-truename bfn) agenda-files)))))
  10970. (t (lambda (b) (with-current-buffer b
  10971. (or (eq major-mode 'org-mode)
  10972. (string-match "\*Org .*Export"
  10973. (buffer-name b)))))))))
  10974. (delq nil
  10975. (mapcar
  10976. (lambda(b)
  10977. (if (and (funcall filter b)
  10978. (or (not exclude-tmp)
  10979. (not (string-match "tmp" (buffer-name b)))))
  10980. b
  10981. nil))
  10982. (buffer-list)))))
  10983. (defun org-agenda-files (&optional unrestricted archives)
  10984. "Get the list of agenda files.
  10985. Optional UNRESTRICTED means return the full list even if a restriction
  10986. is currently in place.
  10987. When ARCHIVES is t, include all archive files hat are really being
  10988. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10989. `org-agenda-archives-mode' is t."
  10990. (let ((files
  10991. (cond
  10992. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10993. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10994. ((listp org-agenda-files) org-agenda-files)
  10995. (t (error "Invalid value of `org-agenda-files'")))))
  10996. (setq files (apply 'append
  10997. (mapcar (lambda (f)
  10998. (if (file-directory-p f)
  10999. (directory-files
  11000. f t org-agenda-file-regexp)
  11001. (list f)))
  11002. files)))
  11003. (when org-agenda-skip-unavailable-files
  11004. (setq files (delq nil
  11005. (mapcar (function
  11006. (lambda (file)
  11007. (and (file-readable-p file) file)))
  11008. files))))
  11009. (when (or (eq archives t)
  11010. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11011. (setq files (org-add-archive-files files)))
  11012. files))
  11013. (defun org-edit-agenda-file-list ()
  11014. "Edit the list of agenda files.
  11015. Depending on setup, this either uses customize to edit the variable
  11016. `org-agenda-files', or it visits the file that is holding the list. In the
  11017. latter case, the buffer is set up in a way that saving it automatically kills
  11018. the buffer and restores the previous window configuration."
  11019. (interactive)
  11020. (if (stringp org-agenda-files)
  11021. (let ((cw (current-window-configuration)))
  11022. (find-file org-agenda-files)
  11023. (org-set-local 'org-window-configuration cw)
  11024. (org-add-hook 'after-save-hook
  11025. (lambda ()
  11026. (set-window-configuration
  11027. (prog1 org-window-configuration
  11028. (kill-buffer (current-buffer))))
  11029. (org-install-agenda-files-menu)
  11030. (message "New agenda file list installed"))
  11031. nil 'local)
  11032. (message "%s" (substitute-command-keys
  11033. "Edit list and finish with \\[save-buffer]")))
  11034. (customize-variable 'org-agenda-files)))
  11035. (defun org-store-new-agenda-file-list (list)
  11036. "Set new value for the agenda file list and save it correctly."
  11037. (if (stringp org-agenda-files)
  11038. (let ((f org-agenda-files) b)
  11039. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11040. (with-temp-file f
  11041. (insert (mapconcat 'identity list "\n") "\n")))
  11042. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11043. (setq org-agenda-files list)
  11044. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11045. (defun org-read-agenda-file-list ()
  11046. "Read the list of agenda files from a file."
  11047. (when (file-directory-p org-agenda-files)
  11048. (error "`org-agenda-files' cannot be a single directory"))
  11049. (when (stringp org-agenda-files)
  11050. (with-temp-buffer
  11051. (insert-file-contents org-agenda-files)
  11052. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11053. ;;;###autoload
  11054. (defun org-cycle-agenda-files ()
  11055. "Cycle through the files in `org-agenda-files'.
  11056. If the current buffer visits an agenda file, find the next one in the list.
  11057. If the current buffer does not, find the first agenda file."
  11058. (interactive)
  11059. (let* ((fs (org-agenda-files t))
  11060. (files (append fs (list (car fs))))
  11061. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11062. file)
  11063. (unless files (error "No agenda files"))
  11064. (catch 'exit
  11065. (while (setq file (pop files))
  11066. (if (equal (file-truename file) tcf)
  11067. (when (car files)
  11068. (find-file (car files))
  11069. (throw 'exit t))))
  11070. (find-file (car fs)))
  11071. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11072. (defun org-agenda-file-to-front (&optional to-end)
  11073. "Move/add the current file to the top of the agenda file list.
  11074. If the file is not present in the list, it is added to the front. If it is
  11075. present, it is moved there. With optional argument TO-END, add/move to the
  11076. end of the list."
  11077. (interactive "P")
  11078. (let ((org-agenda-skip-unavailable-files nil)
  11079. (file-alist (mapcar (lambda (x)
  11080. (cons (file-truename x) x))
  11081. (org-agenda-files t)))
  11082. (ctf (file-truename buffer-file-name))
  11083. x had)
  11084. (setq x (assoc ctf file-alist) had x)
  11085. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11086. (if to-end
  11087. (setq file-alist (append (delq x file-alist) (list x)))
  11088. (setq file-alist (cons x (delq x file-alist))))
  11089. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11090. (org-install-agenda-files-menu)
  11091. (message "File %s to %s of agenda file list"
  11092. (if had "moved" "added") (if to-end "end" "front"))))
  11093. (defun org-remove-file (&optional file)
  11094. "Remove current file from the list of files in variable `org-agenda-files'.
  11095. These are the files which are being checked for agenda entries.
  11096. Optional argument FILE means, use this file instead of the current."
  11097. (interactive)
  11098. (let* ((org-agenda-skip-unavailable-files nil)
  11099. (file (or file buffer-file-name))
  11100. (true-file (file-truename file))
  11101. (afile (abbreviate-file-name file))
  11102. (files (delq nil (mapcar
  11103. (lambda (x)
  11104. (if (equal true-file
  11105. (file-truename x))
  11106. nil x))
  11107. (org-agenda-files t)))))
  11108. (if (not (= (length files) (length (org-agenda-files t))))
  11109. (progn
  11110. (org-store-new-agenda-file-list files)
  11111. (org-install-agenda-files-menu)
  11112. (message "Removed file: %s" afile))
  11113. (message "File was not in list: %s (not removed)" afile))))
  11114. (defun org-file-menu-entry (file)
  11115. (vector file (list 'find-file file) t))
  11116. (defun org-check-agenda-file (file)
  11117. "Make sure FILE exists. If not, ask user what to do."
  11118. (when (not (file-exists-p file))
  11119. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11120. (abbreviate-file-name file))
  11121. (let ((r (downcase (read-char-exclusive))))
  11122. (cond
  11123. ((equal r ?r)
  11124. (org-remove-file file)
  11125. (throw 'nextfile t))
  11126. (t (error "Abort"))))))
  11127. (defun org-get-agenda-file-buffer (file)
  11128. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11129. it to the list of buffers which might be released later."
  11130. (let ((buf (org-find-base-buffer-visiting file)))
  11131. (if buf
  11132. buf ; just return it
  11133. ;; Make a new buffer and remember it
  11134. (setq buf (find-file-noselect file))
  11135. (if buf (push buf org-agenda-new-buffers))
  11136. buf)))
  11137. (defun org-release-buffers (blist)
  11138. "Release all buffers in list, asking the user for confirmation when needed.
  11139. When a buffer is unmodified, it is just killed. When modified, it is saved
  11140. \(if the user agrees) and then killed."
  11141. (let (buf file)
  11142. (while (setq buf (pop blist))
  11143. (setq file (buffer-file-name buf))
  11144. (when (and (buffer-modified-p buf)
  11145. file
  11146. (y-or-n-p (format "Save file %s? " file)))
  11147. (with-current-buffer buf (save-buffer)))
  11148. (kill-buffer buf))))
  11149. (defun org-prepare-agenda-buffers (files)
  11150. "Create buffers for all agenda files, protect archived trees and comments."
  11151. (interactive)
  11152. (let ((pa '(:org-archived t))
  11153. (pc '(:org-comment t))
  11154. (pall '(:org-archived t :org-comment t))
  11155. (inhibit-read-only t)
  11156. (rea (concat ":" org-archive-tag ":"))
  11157. bmp file re)
  11158. (save-excursion
  11159. (save-restriction
  11160. (while (setq file (pop files))
  11161. (if (bufferp file)
  11162. (set-buffer file)
  11163. (org-check-agenda-file file)
  11164. (set-buffer (org-get-agenda-file-buffer file)))
  11165. (widen)
  11166. (setq bmp (buffer-modified-p))
  11167. (org-refresh-category-properties)
  11168. (setq org-todo-keywords-for-agenda
  11169. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11170. (setq org-done-keywords-for-agenda
  11171. (append org-done-keywords-for-agenda org-done-keywords))
  11172. (setq org-todo-keyword-alist-for-agenda
  11173. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11174. (setq org-tag-alist-for-agenda
  11175. (append org-tag-alist-for-agenda org-tag-alist))
  11176. (save-excursion
  11177. (remove-text-properties (point-min) (point-max) pall)
  11178. (when org-agenda-skip-archived-trees
  11179. (goto-char (point-min))
  11180. (while (re-search-forward rea nil t)
  11181. (if (org-on-heading-p t)
  11182. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11183. (goto-char (point-min))
  11184. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11185. (while (re-search-forward re nil t)
  11186. (add-text-properties
  11187. (match-beginning 0) (org-end-of-subtree t) pc)))
  11188. (set-buffer-modified-p bmp))))
  11189. (setq org-todo-keyword-alist-for-agenda
  11190. (org-uniquify org-todo-keyword-alist-for-agenda)
  11191. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11192. ;;;; Embedded LaTeX
  11193. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11194. "Keymap for the minor `org-cdlatex-mode'.")
  11195. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11196. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11197. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11198. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11199. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11200. (defvar org-cdlatex-texmathp-advice-is-done nil
  11201. "Flag remembering if we have applied the advice to texmathp already.")
  11202. (define-minor-mode org-cdlatex-mode
  11203. "Toggle the minor `org-cdlatex-mode'.
  11204. This mode supports entering LaTeX environment and math in LaTeX fragments
  11205. in Org-mode.
  11206. \\{org-cdlatex-mode-map}"
  11207. nil " OCDL" nil
  11208. (when org-cdlatex-mode (require 'cdlatex))
  11209. (unless org-cdlatex-texmathp-advice-is-done
  11210. (setq org-cdlatex-texmathp-advice-is-done t)
  11211. (defadvice texmathp (around org-math-always-on activate)
  11212. "Always return t in org-mode buffers.
  11213. This is because we want to insert math symbols without dollars even outside
  11214. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11215. an embedded LaTeX fragment, let texmathp do its job.
  11216. \\[org-cdlatex-mode-map]"
  11217. (interactive)
  11218. (let (p)
  11219. (cond
  11220. ((not (org-mode-p)) ad-do-it)
  11221. ((eq this-command 'cdlatex-math-symbol)
  11222. (setq ad-return-value t
  11223. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11224. (t
  11225. (let ((p (org-inside-LaTeX-fragment-p)))
  11226. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11227. (setq ad-return-value t
  11228. texmathp-why '("Org-mode embedded math" . 0))
  11229. (if p ad-do-it)))))))))
  11230. (defun turn-on-org-cdlatex ()
  11231. "Unconditionally turn on `org-cdlatex-mode'."
  11232. (org-cdlatex-mode 1))
  11233. (defun org-inside-LaTeX-fragment-p ()
  11234. "Test if point is inside a LaTeX fragment.
  11235. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11236. sequence appearing also before point.
  11237. Even though the matchers for math are configurable, this function assumes
  11238. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11239. delimiters are skipped when they have been removed by customization.
  11240. The return value is nil, or a cons cell with the delimiter and
  11241. and the position of this delimiter.
  11242. This function does a reasonably good job, but can locally be fooled by
  11243. for example currency specifications. For example it will assume being in
  11244. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11245. fragments that are properly closed, but during editing, we have to live
  11246. with the uncertainty caused by missing closing delimiters. This function
  11247. looks only before point, not after."
  11248. (catch 'exit
  11249. (let ((pos (point))
  11250. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11251. (lim (progn
  11252. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11253. (point)))
  11254. dd-on str (start 0) m re)
  11255. (goto-char pos)
  11256. (when dodollar
  11257. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11258. re (nth 1 (assoc "$" org-latex-regexps)))
  11259. (while (string-match re str start)
  11260. (cond
  11261. ((= (match-end 0) (length str))
  11262. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11263. ((= (match-end 0) (- (length str) 5))
  11264. (throw 'exit nil))
  11265. (t (setq start (match-end 0))))))
  11266. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11267. (goto-char pos)
  11268. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11269. (and (match-beginning 2) (throw 'exit nil))
  11270. ;; count $$
  11271. (while (re-search-backward "\\$\\$" lim t)
  11272. (setq dd-on (not dd-on)))
  11273. (goto-char pos)
  11274. (if dd-on (cons "$$" m))))))
  11275. (defun org-try-cdlatex-tab ()
  11276. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11277. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11278. - inside a LaTeX fragment, or
  11279. - after the first word in a line, where an abbreviation expansion could
  11280. insert a LaTeX environment."
  11281. (when org-cdlatex-mode
  11282. (cond
  11283. ((save-excursion
  11284. (skip-chars-backward "a-zA-Z0-9*")
  11285. (skip-chars-backward " \t")
  11286. (bolp))
  11287. (cdlatex-tab) t)
  11288. ((org-inside-LaTeX-fragment-p)
  11289. (cdlatex-tab) t)
  11290. (t nil))))
  11291. (defun org-cdlatex-underscore-caret (&optional arg)
  11292. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11293. Revert to the normal definition outside of these fragments."
  11294. (interactive "P")
  11295. (if (org-inside-LaTeX-fragment-p)
  11296. (call-interactively 'cdlatex-sub-superscript)
  11297. (let (org-cdlatex-mode)
  11298. (call-interactively (key-binding (vector last-input-event))))))
  11299. (defun org-cdlatex-math-modify (&optional arg)
  11300. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11301. Revert to the normal definition outside of these fragments."
  11302. (interactive "P")
  11303. (if (org-inside-LaTeX-fragment-p)
  11304. (call-interactively 'cdlatex-math-modify)
  11305. (let (org-cdlatex-mode)
  11306. (call-interactively (key-binding (vector last-input-event))))))
  11307. (defvar org-latex-fragment-image-overlays nil
  11308. "List of overlays carrying the images of latex fragments.")
  11309. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11310. (defun org-remove-latex-fragment-image-overlays ()
  11311. "Remove all overlays with LaTeX fragment images in current buffer."
  11312. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11313. (setq org-latex-fragment-image-overlays nil))
  11314. (defun org-preview-latex-fragment (&optional subtree)
  11315. "Preview the LaTeX fragment at point, or all locally or globally.
  11316. If the cursor is in a LaTeX fragment, create the image and overlay
  11317. it over the source code. If there is no fragment at point, display
  11318. all fragments in the current text, from one headline to the next. With
  11319. prefix SUBTREE, display all fragments in the current subtree. With a
  11320. double prefix `C-u C-u', or when the cursor is before the first headline,
  11321. display all fragments in the buffer.
  11322. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11323. (interactive "P")
  11324. (org-remove-latex-fragment-image-overlays)
  11325. (save-excursion
  11326. (save-restriction
  11327. (let (beg end at msg)
  11328. (cond
  11329. ((or (equal subtree '(16))
  11330. (not (save-excursion
  11331. (re-search-backward (concat "^" outline-regexp) nil t))))
  11332. (setq beg (point-min) end (point-max)
  11333. msg "Creating images for buffer...%s"))
  11334. ((equal subtree '(4))
  11335. (org-back-to-heading)
  11336. (setq beg (point) end (org-end-of-subtree t)
  11337. msg "Creating images for subtree...%s"))
  11338. (t
  11339. (if (setq at (org-inside-LaTeX-fragment-p))
  11340. (goto-char (max (point-min) (- (cdr at) 2)))
  11341. (org-back-to-heading))
  11342. (setq beg (point) end (progn (outline-next-heading) (point))
  11343. msg (if at "Creating image...%s"
  11344. "Creating images for entry...%s"))))
  11345. (message msg "")
  11346. (narrow-to-region beg end)
  11347. (goto-char beg)
  11348. (org-format-latex
  11349. (concat "ltxpng/" (file-name-sans-extension
  11350. (file-name-nondirectory
  11351. buffer-file-name)))
  11352. default-directory 'overlays msg at 'forbuffer)
  11353. (message msg "done. Use `C-c C-c' to remove images.")))))
  11354. (defvar org-latex-regexps
  11355. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11356. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11357. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11358. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11359. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11360. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11361. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11362. "Regular expressions for matching embedded LaTeX.")
  11363. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11364. "Replace LaTeX fragments with links to an image, and produce images."
  11365. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11366. (let* ((prefixnodir (file-name-nondirectory prefix))
  11367. (absprefix (expand-file-name prefix dir))
  11368. (todir (file-name-directory absprefix))
  11369. (opt org-format-latex-options)
  11370. (matchers (plist-get opt :matchers))
  11371. (re-list org-latex-regexps)
  11372. (cnt 0) txt link beg end re e checkdir
  11373. m n block linkfile movefile ov)
  11374. ;; Check if there are old images files with this prefix, and remove them
  11375. (when (file-directory-p todir)
  11376. (mapc 'delete-file
  11377. (directory-files
  11378. todir 'full
  11379. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11380. ;; Check the different regular expressions
  11381. (while (setq e (pop re-list))
  11382. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11383. block (if (nth 3 e) "\n\n" ""))
  11384. (when (member m matchers)
  11385. (goto-char (point-min))
  11386. (while (re-search-forward re nil t)
  11387. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11388. (setq txt (match-string n)
  11389. beg (match-beginning n) end (match-end n)
  11390. cnt (1+ cnt)
  11391. linkfile (format "%s_%04d.png" prefix cnt)
  11392. movefile (format "%s_%04d.png" absprefix cnt)
  11393. link (concat block "[[file:" linkfile "]]" block))
  11394. (if msg (message msg cnt))
  11395. (goto-char beg)
  11396. (unless checkdir ; make sure the directory exists
  11397. (setq checkdir t)
  11398. (or (file-directory-p todir) (make-directory todir)))
  11399. (org-create-formula-image
  11400. txt movefile opt forbuffer)
  11401. (if overlays
  11402. (progn
  11403. (setq ov (org-make-overlay beg end))
  11404. (if (featurep 'xemacs)
  11405. (progn
  11406. (org-overlay-put ov 'invisible t)
  11407. (org-overlay-put
  11408. ov 'end-glyph
  11409. (make-glyph (vector 'png :file movefile))))
  11410. (org-overlay-put
  11411. ov 'display
  11412. (list 'image :type 'png :file movefile :ascent 'center)))
  11413. (push ov org-latex-fragment-image-overlays)
  11414. (goto-char end))
  11415. (delete-region beg end)
  11416. (insert link))))))))
  11417. ;; This function borrows from Ganesh Swami's latex2png.el
  11418. (defun org-create-formula-image (string tofile options buffer)
  11419. (let* ((tmpdir (if (featurep 'xemacs)
  11420. (temp-directory)
  11421. temporary-file-directory))
  11422. (texfilebase (make-temp-name
  11423. (expand-file-name "orgtex" tmpdir)))
  11424. (texfile (concat texfilebase ".tex"))
  11425. (dvifile (concat texfilebase ".dvi"))
  11426. (pngfile (concat texfilebase ".png"))
  11427. (fnh (if (featurep 'xemacs)
  11428. (font-height (get-face-font 'default))
  11429. (face-attribute 'default :height nil)))
  11430. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11431. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11432. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11433. "Black"))
  11434. (bg (or (plist-get options (if buffer :background :html-background))
  11435. "Transparent")))
  11436. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11437. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11438. (with-temp-file texfile
  11439. (insert org-format-latex-header
  11440. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11441. (let ((dir default-directory))
  11442. (condition-case nil
  11443. (progn
  11444. (cd tmpdir)
  11445. (call-process "latex" nil nil nil texfile))
  11446. (error nil))
  11447. (cd dir))
  11448. (if (not (file-exists-p dvifile))
  11449. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11450. (condition-case nil
  11451. (call-process "dvipng" nil nil nil
  11452. "-E" "-fg" fg "-bg" bg
  11453. "-D" dpi
  11454. ;;"-x" scale "-y" scale
  11455. "-T" "tight"
  11456. "-o" pngfile
  11457. dvifile)
  11458. (error nil))
  11459. (if (not (file-exists-p pngfile))
  11460. (progn (message "Failed to create png file from %s" texfile) nil)
  11461. ;; Use the requested file name and clean up
  11462. (copy-file pngfile tofile 'replace)
  11463. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11464. (delete-file (concat texfilebase e)))
  11465. pngfile))))
  11466. (defun org-dvipng-color (attr)
  11467. "Return an rgb color specification for dvipng."
  11468. (apply 'format "rgb %s %s %s"
  11469. (mapcar 'org-normalize-color
  11470. (color-values (face-attribute 'default attr nil)))))
  11471. (defun org-normalize-color (value)
  11472. "Return string to be used as color value for an RGB component."
  11473. (format "%g" (/ value 65535.0)))
  11474. ;;;; Key bindings
  11475. ;; Make `C-c C-x' a prefix key
  11476. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11477. ;; TAB key with modifiers
  11478. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11479. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11480. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11481. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11482. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11483. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11484. ;; The following line is necessary under Suse GNU/Linux
  11485. (unless (featurep 'xemacs)
  11486. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11487. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11488. (define-key org-mode-map [backtab] 'org-shifttab)
  11489. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11490. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11491. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11492. ;; Cursor keys with modifiers
  11493. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11494. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11495. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11496. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11497. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11498. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11499. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11500. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11501. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11502. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11503. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11504. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11505. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11506. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11507. ;;; Extra keys for tty access.
  11508. ;; We only set them when really needed because otherwise the
  11509. ;; menus don't show the simple keys
  11510. (when (or org-use-extra-keys
  11511. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11512. (not window-system))
  11513. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11514. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11515. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11516. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11517. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11518. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11519. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11520. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11521. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11522. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11523. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11524. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11525. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11526. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11527. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11528. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11529. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11530. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11531. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11532. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11533. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11534. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11535. ;; All the other keys
  11536. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11537. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11538. (if (boundp 'narrow-map)
  11539. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11540. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11541. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11542. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11543. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11544. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11545. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11546. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11547. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11548. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11549. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11550. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11551. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11552. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11553. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11554. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11555. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11556. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11557. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11558. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11559. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11560. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11561. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11562. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11563. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11564. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11565. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11566. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11567. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11568. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11569. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11570. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11571. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11572. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11573. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11574. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11575. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11576. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11577. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11578. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11579. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11580. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11581. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11582. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11583. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11584. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11585. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11586. (org-defkey org-mode-map "\C-m" 'org-return)
  11587. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11588. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11589. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11590. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11591. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11592. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11593. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11594. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11595. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11596. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11597. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11598. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11599. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11600. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11601. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11602. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11603. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  11604. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11605. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11606. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11607. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11608. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11609. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11610. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11611. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11612. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11613. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11614. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11615. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11616. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11617. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11618. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11619. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11620. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  11621. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  11622. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  11623. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11624. (when (featurep 'xemacs)
  11625. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11626. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11627. (defun org-self-insert-command (N)
  11628. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11629. If the cursor is in a table looking at whitespace, the whitespace is
  11630. overwritten, and the table is not marked as requiring realignment."
  11631. (interactive "p")
  11632. (if (and (org-table-p)
  11633. (progn
  11634. ;; check if we blank the field, and if that triggers align
  11635. (and (featurep 'org-table) org-table-auto-blank-field
  11636. (member last-command
  11637. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11638. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11639. ;; got extra space, this field does not determine column width
  11640. (let (org-table-may-need-update) (org-table-blank-field))
  11641. ;; no extra space, this field may determine column width
  11642. (org-table-blank-field)))
  11643. t)
  11644. (eq N 1)
  11645. (looking-at "[^|\n]* |"))
  11646. (let (org-table-may-need-update)
  11647. (goto-char (1- (match-end 0)))
  11648. (delete-backward-char 1)
  11649. (goto-char (match-beginning 0))
  11650. (self-insert-command N))
  11651. (setq org-table-may-need-update t)
  11652. (self-insert-command N)
  11653. (org-fix-tags-on-the-fly)))
  11654. (defun org-fix-tags-on-the-fly ()
  11655. (when (and (equal (char-after (point-at-bol)) ?*)
  11656. (org-on-heading-p))
  11657. (org-align-tags-here org-tags-column)))
  11658. (defun org-delete-backward-char (N)
  11659. "Like `delete-backward-char', insert whitespace at field end in tables.
  11660. When deleting backwards, 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. (eq N 1)
  11667. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11668. (looking-at ".*?|"))
  11669. (let ((pos (point))
  11670. (noalign (looking-at "[^|\n\r]* |"))
  11671. (c org-table-may-need-update))
  11672. (backward-delete-char N)
  11673. (skip-chars-forward "^|")
  11674. (insert " ")
  11675. (goto-char (1- pos))
  11676. ;; noalign: if there were two spaces at the end, this field
  11677. ;; does not determine the width of the column.
  11678. (if noalign (setq org-table-may-need-update c)))
  11679. (backward-delete-char N)
  11680. (org-fix-tags-on-the-fly)))
  11681. (defun org-delete-char (N)
  11682. "Like `delete-char', but insert whitespace at field end in tables.
  11683. When deleting characters, in tables this function will insert whitespace in
  11684. front of the next \"|\" separator, to keep the table aligned. The table will
  11685. still be marked for re-alignment if the field did fill the entire column,
  11686. because, in this case the deletion might narrow the column."
  11687. (interactive "p")
  11688. (if (and (org-table-p)
  11689. (not (bolp))
  11690. (not (= (char-after) ?|))
  11691. (eq N 1))
  11692. (if (looking-at ".*?|")
  11693. (let ((pos (point))
  11694. (noalign (looking-at "[^|\n\r]* |"))
  11695. (c org-table-may-need-update))
  11696. (replace-match (concat
  11697. (substring (match-string 0) 1 -1)
  11698. " |"))
  11699. (goto-char pos)
  11700. ;; noalign: if there were two spaces at the end, this field
  11701. ;; does not determine the width of the column.
  11702. (if noalign (setq org-table-may-need-update c)))
  11703. (delete-char N))
  11704. (delete-char N)
  11705. (org-fix-tags-on-the-fly)))
  11706. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11707. (put 'org-self-insert-command 'delete-selection t)
  11708. (put 'orgtbl-self-insert-command 'delete-selection t)
  11709. (put 'org-delete-char 'delete-selection 'supersede)
  11710. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11711. ;; Make `flyspell-mode' delay after some commands
  11712. (put 'org-self-insert-command 'flyspell-delayed t)
  11713. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11714. (put 'org-delete-char 'flyspell-delayed t)
  11715. (put 'org-delete-backward-char 'flyspell-delayed t)
  11716. ;; Make pabbrev-mode expand after org-mode commands
  11717. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11718. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  11719. ;; How to do this: Measure non-white length of current string
  11720. ;; If equal to column width, we should realign.
  11721. (defun org-remap (map &rest commands)
  11722. "In MAP, remap the functions given in COMMANDS.
  11723. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11724. (let (new old)
  11725. (while commands
  11726. (setq old (pop commands) new (pop commands))
  11727. (if (fboundp 'command-remapping)
  11728. (org-defkey map (vector 'remap old) new)
  11729. (substitute-key-definition old new map global-map)))))
  11730. (when (eq org-enable-table-editor 'optimized)
  11731. ;; If the user wants maximum table support, we need to hijack
  11732. ;; some standard editing functions
  11733. (org-remap org-mode-map
  11734. 'self-insert-command 'org-self-insert-command
  11735. 'delete-char 'org-delete-char
  11736. 'delete-backward-char 'org-delete-backward-char)
  11737. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11738. (defun org-shiftcursor-error ()
  11739. "Throw an error because Shift-Cursor command was applied in wrong context."
  11740. (error "This command is active in special context like tables, headlines or timestamps"))
  11741. (defun org-shifttab (&optional arg)
  11742. "Global visibility cycling or move to previous table field.
  11743. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11744. on context.
  11745. See the individual commands for more information."
  11746. (interactive "P")
  11747. (cond
  11748. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11749. ((integerp arg)
  11750. (message "Content view to level: %d" arg)
  11751. (org-content (prefix-numeric-value arg))
  11752. (setq org-cycle-global-status 'overview))
  11753. (t (call-interactively 'org-global-cycle))))
  11754. (defun org-shiftmetaleft ()
  11755. "Promote subtree or delete table column.
  11756. Calls `org-promote-subtree', `org-outdent-item',
  11757. or `org-table-delete-column', depending on context.
  11758. See the individual commands for more information."
  11759. (interactive)
  11760. (cond
  11761. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11762. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11763. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11764. (t (org-shiftcursor-error))))
  11765. (defun org-shiftmetaright ()
  11766. "Demote subtree or insert table column.
  11767. Calls `org-demote-subtree', `org-indent-item',
  11768. or `org-table-insert-column', depending on context.
  11769. See the individual commands for more information."
  11770. (interactive)
  11771. (cond
  11772. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11773. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11774. ((org-at-item-p) (call-interactively 'org-indent-item))
  11775. (t (org-shiftcursor-error))))
  11776. (defun org-shiftmetaup (&optional arg)
  11777. "Move subtree up or kill table row.
  11778. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11779. `org-move-item-up' depending on context. See the individual commands
  11780. for more information."
  11781. (interactive "P")
  11782. (cond
  11783. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11784. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11785. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11786. (t (org-shiftcursor-error))))
  11787. (defun org-shiftmetadown (&optional arg)
  11788. "Move subtree down or insert table row.
  11789. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11790. `org-move-item-down', depending on context. See the individual
  11791. commands for more information."
  11792. (interactive "P")
  11793. (cond
  11794. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11795. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11796. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11797. (t (org-shiftcursor-error))))
  11798. (defun org-metaleft (&optional arg)
  11799. "Promote heading or move table column to left.
  11800. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11801. With no specific context, calls the Emacs default `backward-word'.
  11802. See the individual commands for more information."
  11803. (interactive "P")
  11804. (cond
  11805. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11806. ((or (org-on-heading-p) (org-region-active-p))
  11807. (call-interactively 'org-do-promote))
  11808. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11809. (t (call-interactively 'backward-word))))
  11810. (defun org-metaright (&optional arg)
  11811. "Demote subtree or move table column to right.
  11812. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11813. With no specific context, calls the Emacs default `forward-word'.
  11814. See the individual commands for more information."
  11815. (interactive "P")
  11816. (cond
  11817. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11818. ((or (org-on-heading-p) (org-region-active-p))
  11819. (call-interactively 'org-do-demote))
  11820. ((org-at-item-p) (call-interactively 'org-indent-item))
  11821. (t (call-interactively 'forward-word))))
  11822. (defun org-metaup (&optional arg)
  11823. "Move subtree up or move table row up.
  11824. Calls `org-move-subtree-up' or `org-table-move-row' or
  11825. `org-move-item-up', depending on context. See the individual commands
  11826. for more information."
  11827. (interactive "P")
  11828. (cond
  11829. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11830. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11831. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11832. (t (transpose-lines 1) (beginning-of-line -1))))
  11833. (defun org-metadown (&optional arg)
  11834. "Move subtree down or move table row down.
  11835. Calls `org-move-subtree-down' or `org-table-move-row' or
  11836. `org-move-item-down', depending on context. See the individual
  11837. commands for more information."
  11838. (interactive "P")
  11839. (cond
  11840. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11841. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11842. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11843. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11844. (defun org-shiftup (&optional arg)
  11845. "Increase item in timestamp or increase priority of current headline.
  11846. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11847. depending on context. See the individual commands for more information."
  11848. (interactive "P")
  11849. (cond
  11850. ((org-at-timestamp-p t)
  11851. (call-interactively (if org-edit-timestamp-down-means-later
  11852. 'org-timestamp-down 'org-timestamp-up)))
  11853. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11854. ((org-at-item-p) (call-interactively 'org-previous-item))
  11855. ((org-clocktable-try-shift 'up arg))
  11856. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11857. (defun org-shiftdown (&optional arg)
  11858. "Decrease item in timestamp or decrease priority of current headline.
  11859. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11860. depending on context. See the individual commands for more information."
  11861. (interactive "P")
  11862. (cond
  11863. ((org-at-timestamp-p t)
  11864. (call-interactively (if org-edit-timestamp-down-means-later
  11865. 'org-timestamp-up 'org-timestamp-down)))
  11866. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11867. ((org-clocktable-try-shift 'down arg))
  11868. (t (call-interactively 'org-next-item))))
  11869. (defun org-shiftright (&optional arg)
  11870. "Cycle the thing at point or in the current line, depending on context.
  11871. Depending on context, this does one of the following:
  11872. - switch a timestamp at point one day into the future
  11873. - on a headline, switch to the next TODO keyword.
  11874. - on an item, switch entire list to the next bullet type
  11875. - on a property line, switch to the next allowed value
  11876. - on a clocktable definition line, move time block into the future"
  11877. (interactive "P")
  11878. (cond
  11879. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11880. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11881. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11882. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11883. ((org-clocktable-try-shift 'right arg))
  11884. (t (org-shiftcursor-error))))
  11885. (defun org-shiftleft (&optional arg)
  11886. "Cycle the thing at point or in the current line, depending on context.
  11887. Depending on context, this does one of the following:
  11888. - switch a timestamp at point one day into the past
  11889. - on a headline, switch to the previous TODO keyword.
  11890. - on an item, switch entire list to the previous bullet type
  11891. - on a property line, switch to the previous allowed value
  11892. - on a clocktable definition line, move time block into the past"
  11893. (interactive "P")
  11894. (cond
  11895. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11896. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11897. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11898. ((org-at-property-p)
  11899. (call-interactively 'org-property-previous-allowed-value))
  11900. ((org-clocktable-try-shift 'left arg))
  11901. (t (org-shiftcursor-error))))
  11902. (defun org-shiftcontrolright ()
  11903. "Switch to next TODO set."
  11904. (interactive)
  11905. (cond
  11906. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11907. (t (org-shiftcursor-error))))
  11908. (defun org-shiftcontrolleft ()
  11909. "Switch to previous TODO set."
  11910. (interactive)
  11911. (cond
  11912. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11913. (t (org-shiftcursor-error))))
  11914. (defun org-ctrl-c-ret ()
  11915. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11916. (interactive)
  11917. (cond
  11918. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11919. (t (call-interactively 'org-insert-heading))))
  11920. (defun org-copy-special ()
  11921. "Copy region in table or copy current subtree.
  11922. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11923. See the individual commands for more information."
  11924. (interactive)
  11925. (call-interactively
  11926. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11927. (defun org-cut-special ()
  11928. "Cut region in table or cut current subtree.
  11929. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11930. See the individual commands for more information."
  11931. (interactive)
  11932. (call-interactively
  11933. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11934. (defun org-paste-special (arg)
  11935. "Paste rectangular region into table, or past subtree relative to level.
  11936. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11937. See the individual commands for more information."
  11938. (interactive "P")
  11939. (if (org-at-table-p)
  11940. (org-table-paste-rectangle)
  11941. (org-paste-subtree arg)))
  11942. (defun org-edit-special ()
  11943. "Call a special editor for the stuff at point.
  11944. When at a table, call the formula editor with `org-table-edit-formulas'.
  11945. When at the first line of an src example, call `org-edit-src-code'.
  11946. When in an #+include line, visit the include file. Otherwise call
  11947. `ffap' to visit the file at point."
  11948. (interactive)
  11949. (cond
  11950. ((org-at-table-p)
  11951. (call-interactively 'org-table-edit-formulas))
  11952. ((save-excursion
  11953. (beginning-of-line 1)
  11954. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11955. (find-file (org-trim (match-string 1))))
  11956. ((org-edit-src-code))
  11957. ((org-edit-fixed-width-region))
  11958. (t (call-interactively 'ffap))))
  11959. (defun org-ctrl-c-ctrl-c (&optional arg)
  11960. "Set tags in headline, or update according to changed information at point.
  11961. This command does many different things, depending on context:
  11962. - If the cursor is in a headline, prompt for tags and insert them
  11963. into the current line, aligned to `org-tags-column'. When called
  11964. with prefix arg, realign all tags in the current buffer.
  11965. - If the cursor is in one of the special #+KEYWORD lines, this
  11966. triggers scanning the buffer for these lines and updating the
  11967. information.
  11968. - If the cursor is inside a table, realign the table. This command
  11969. works even if the automatic table editor has been turned off.
  11970. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11971. the entire table.
  11972. - If the cursor is at a footnote reference or definition, jump to
  11973. the corresponding definition or references, respectively.
  11974. - If the cursor is a the beginning of a dynamic block, update it.
  11975. - If the cursor is inside a table created by the table.el package,
  11976. activate that table.
  11977. - If the current buffer is a remember buffer, close note and file
  11978. it. A prefix argument of 1 files to the default location
  11979. without further interaction. A prefix argument of 2 files to
  11980. the currently clocking task.
  11981. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11982. links in this buffer.
  11983. - If the cursor is on a numbered item in a plain list, renumber the
  11984. ordered list.
  11985. - If the cursor is on a checkbox, toggle it."
  11986. (interactive "P")
  11987. (let ((org-enable-table-editor t))
  11988. (cond
  11989. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11990. org-occur-highlights
  11991. org-latex-fragment-image-overlays)
  11992. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11993. (org-remove-occur-highlights)
  11994. (org-remove-latex-fragment-image-overlays)
  11995. (message "Temporary highlights/overlays removed from current buffer"))
  11996. ((and (local-variable-p 'org-finish-function (current-buffer))
  11997. (fboundp org-finish-function))
  11998. (funcall org-finish-function))
  11999. ((org-at-property-p)
  12000. (call-interactively 'org-property-action))
  12001. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12002. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12003. ((org-at-table.el-p)
  12004. (require 'table)
  12005. (beginning-of-line 1)
  12006. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12007. (call-interactively 'table-recognize-table))
  12008. ((org-at-table-p)
  12009. (org-table-maybe-eval-formula)
  12010. (if arg
  12011. (call-interactively 'org-table-recalculate)
  12012. (org-table-maybe-recalculate-line))
  12013. (call-interactively 'org-table-align))
  12014. ((or (org-footnote-at-reference-p)
  12015. (org-footnote-at-definition-p))
  12016. (call-interactively 'org-footnote-action))
  12017. ((org-at-item-checkbox-p)
  12018. (call-interactively 'org-toggle-checkbox))
  12019. ((org-at-item-p)
  12020. (call-interactively 'org-maybe-renumber-ordered-list))
  12021. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12022. ;; Dynamic block
  12023. (beginning-of-line 1)
  12024. (save-excursion (org-update-dblock)))
  12025. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12026. (cond
  12027. ((equal (match-string 1) "TBLFM")
  12028. ;; Recalculate the table before this line
  12029. (save-excursion
  12030. (beginning-of-line 1)
  12031. (skip-chars-backward " \r\n\t")
  12032. (if (org-at-table-p)
  12033. (org-call-with-arg 'org-table-recalculate t))))
  12034. (t
  12035. ; (org-set-regexps-and-options)
  12036. ; (org-restart-font-lock)
  12037. (let ((org-inhibit-startup t)) (org-mode-restart))
  12038. (message "Local setup has been refreshed"))))
  12039. (t (error "C-c C-c can do nothing useful at this location.")))))
  12040. (defun org-mode-restart ()
  12041. "Restart Org-mode, to scan again for special lines.
  12042. Also updates the keyword regular expressions."
  12043. (interactive)
  12044. (org-mode)
  12045. (message "Org-mode restarted"))
  12046. (defun org-kill-note-or-show-branches ()
  12047. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12048. (interactive)
  12049. (if (not org-finish-function)
  12050. (call-interactively 'show-branches)
  12051. (let ((org-note-abort t))
  12052. (funcall org-finish-function))))
  12053. (defun org-return (&optional indent)
  12054. "Goto next table row or insert a newline.
  12055. Calls `org-table-next-row' or `newline', depending on context.
  12056. See the individual commands for more information."
  12057. (interactive)
  12058. (cond
  12059. ((bobp) (if indent (newline-and-indent) (newline)))
  12060. ((and (org-at-heading-p)
  12061. (looking-at
  12062. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12063. (org-show-entry)
  12064. (end-of-line 1)
  12065. (newline))
  12066. ((org-at-table-p)
  12067. (org-table-justify-field-maybe)
  12068. (call-interactively 'org-table-next-row))
  12069. (t (if indent (newline-and-indent) (newline)))))
  12070. (defun org-return-indent ()
  12071. "Goto next table row or insert a newline and indent.
  12072. Calls `org-table-next-row' or `newline-and-indent', depending on
  12073. context. See the individual commands for more information."
  12074. (interactive)
  12075. (org-return t))
  12076. (defun org-ctrl-c-star ()
  12077. "Compute table, or change heading status of lines.
  12078. Calls `org-table-recalculate' or `org-toggle-region-headings',
  12079. depending on context. This will also turn a plain list item or a normal
  12080. line into a subheading."
  12081. (interactive)
  12082. (cond
  12083. ((org-at-table-p)
  12084. (call-interactively 'org-table-recalculate))
  12085. ((org-region-active-p)
  12086. ;; Convert all lines in region to list items
  12087. (call-interactively 'org-toggle-region-headings))
  12088. ((org-on-heading-p)
  12089. (org-toggle-region-headings (point-at-bol)
  12090. (min (1+ (point-at-eol)) (point-max))))
  12091. ((org-at-item-p)
  12092. ;; Convert to heading
  12093. (let ((level (save-match-data
  12094. (save-excursion
  12095. (condition-case nil
  12096. (progn
  12097. (org-back-to-heading t)
  12098. (funcall outline-level))
  12099. (error 0))))))
  12100. (replace-match
  12101. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  12102. (t (org-toggle-region-headings (point-at-bol)
  12103. (min (1+ (point-at-eol)) (point-max))))))
  12104. (defun org-ctrl-c-minus ()
  12105. "Insert separator line in table or modify bullet status of line.
  12106. Also turns a plain line or a region of lines into list items.
  12107. Calls `org-table-insert-hline', `org-toggle-region-items', or
  12108. `org-cycle-list-bullet', depending on context."
  12109. (interactive)
  12110. (cond
  12111. ((org-at-table-p)
  12112. (call-interactively 'org-table-insert-hline))
  12113. ((org-on-heading-p)
  12114. ;; Convert to item
  12115. (save-excursion
  12116. (beginning-of-line 1)
  12117. (if (looking-at "\\*+ ")
  12118. (replace-match
  12119. (concat (make-string
  12120. (- (match-end 0) (point) (if org-odd-levels-only 2 1)) ?\ )
  12121. "- ")))))
  12122. ((org-region-active-p)
  12123. ;; Convert all lines in region to list items
  12124. (call-interactively 'org-toggle-region-items))
  12125. ((org-in-item-p)
  12126. (call-interactively 'org-cycle-list-bullet))
  12127. (t (org-toggle-region-items (point-at-bol)
  12128. (min (1+ (point-at-eol)) (point-max))))))
  12129. (defun org-toggle-region-items (beg end)
  12130. "Convert all lines in region to list items.
  12131. If the first line is already an item, convert all list items in the region
  12132. to normal lines."
  12133. (interactive "r")
  12134. (let (l2 l)
  12135. (save-excursion
  12136. (goto-char end)
  12137. (setq l2 (org-current-line))
  12138. (goto-char beg)
  12139. (beginning-of-line 1)
  12140. (setq l (1- (org-current-line)))
  12141. (if (org-at-item-p)
  12142. ;; We already have items, de-itemize
  12143. (while (< (setq l (1+ l)) l2)
  12144. (when (org-at-item-p)
  12145. (goto-char (match-beginning 2))
  12146. (delete-region (match-beginning 2) (match-end 2))
  12147. (and (looking-at "[ \t]+") (replace-match "")))
  12148. (beginning-of-line 2))
  12149. (while (< (setq l (1+ l)) l2)
  12150. (unless (org-at-item-p)
  12151. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12152. (replace-match "\\1- \\2")))
  12153. (beginning-of-line 2))))))
  12154. (defun org-toggle-region-headings (beg end)
  12155. "Convert all lines in region to list items.
  12156. If the first line is already an item, convert all list items in the region
  12157. to normal lines."
  12158. (interactive "r")
  12159. (let (l2 l)
  12160. (save-excursion
  12161. (goto-char end)
  12162. (setq l2 (org-current-line))
  12163. (goto-char beg)
  12164. (beginning-of-line 1)
  12165. (setq l (1- (org-current-line)))
  12166. (if (org-on-heading-p)
  12167. ;; We already have headlines, de-star them
  12168. (while (< (setq l (1+ l)) l2)
  12169. (when (org-on-heading-p t)
  12170. (and (looking-at outline-regexp) (replace-match "")))
  12171. (beginning-of-line 2))
  12172. (let* ((stars (save-excursion
  12173. (re-search-backward org-complex-heading-regexp nil t)
  12174. (or (match-string 1) "*")))
  12175. (add-stars (if org-odd-levels-only "**" "*"))
  12176. (rpl (concat stars add-stars " \\2")))
  12177. (while (< (setq l (1+ l)) l2)
  12178. (unless (org-on-heading-p)
  12179. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12180. (replace-match rpl)))
  12181. (beginning-of-line 2)))))))
  12182. (defun org-meta-return (&optional arg)
  12183. "Insert a new heading or wrap a region in a table.
  12184. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12185. See the individual commands for more information."
  12186. (interactive "P")
  12187. (cond
  12188. ((org-at-table-p)
  12189. (call-interactively 'org-table-wrap-region))
  12190. (t (call-interactively 'org-insert-heading))))
  12191. ;;; Menu entries
  12192. ;; Define the Org-mode menus
  12193. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12194. '("Tbl"
  12195. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12196. ["Next Field" org-cycle (org-at-table-p)]
  12197. ["Previous Field" org-shifttab (org-at-table-p)]
  12198. ["Next Row" org-return (org-at-table-p)]
  12199. "--"
  12200. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12201. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12202. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12203. "--"
  12204. ("Column"
  12205. ["Move Column Left" org-metaleft (org-at-table-p)]
  12206. ["Move Column Right" org-metaright (org-at-table-p)]
  12207. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12208. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12209. ("Row"
  12210. ["Move Row Up" org-metaup (org-at-table-p)]
  12211. ["Move Row Down" org-metadown (org-at-table-p)]
  12212. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12213. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12214. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12215. "--"
  12216. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12217. ("Rectangle"
  12218. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12219. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12220. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12221. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12222. "--"
  12223. ("Calculate"
  12224. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12225. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12226. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12227. "--"
  12228. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12229. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12230. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12231. "--"
  12232. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12233. "--"
  12234. ["Sum Column/Rectangle" org-table-sum
  12235. (or (org-at-table-p) (org-region-active-p))]
  12236. ["Which Column?" org-table-current-column (org-at-table-p)])
  12237. ["Debug Formulas"
  12238. org-table-toggle-formula-debugger
  12239. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12240. ["Show Col/Row Numbers"
  12241. org-table-toggle-coordinate-overlays
  12242. :style toggle
  12243. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12244. "--"
  12245. ["Create" org-table-create (and (not (org-at-table-p))
  12246. org-enable-table-editor)]
  12247. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12248. ["Import from File" org-table-import (not (org-at-table-p))]
  12249. ["Export to File" org-table-export (org-at-table-p)]
  12250. "--"
  12251. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12252. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12253. '("Org"
  12254. ("Show/Hide"
  12255. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12256. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12257. ["Sparse Tree..." org-sparse-tree t]
  12258. ["Reveal Context" org-reveal t]
  12259. ["Show All" show-all t]
  12260. "--"
  12261. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12262. "--"
  12263. ["New Heading" org-insert-heading t]
  12264. ("Navigate Headings"
  12265. ["Up" outline-up-heading t]
  12266. ["Next" outline-next-visible-heading t]
  12267. ["Previous" outline-previous-visible-heading t]
  12268. ["Next Same Level" outline-forward-same-level t]
  12269. ["Previous Same Level" outline-backward-same-level t]
  12270. "--"
  12271. ["Jump" org-goto t])
  12272. ("Edit Structure"
  12273. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12274. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12275. "--"
  12276. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12277. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12278. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12279. "--"
  12280. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12281. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12282. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12283. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12284. "--"
  12285. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12286. "--"
  12287. ["Convert to odd levels" org-convert-to-odd-levels t]
  12288. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12289. ("Editing"
  12290. ["Emphasis..." org-emphasize t]
  12291. ["Edit Source Example" org-edit-special t]
  12292. "--"
  12293. ["Footnote new/jump" org-footnote-action t]
  12294. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12295. ("Archive"
  12296. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12297. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12298. ; :active t :keys "C-u C-c C-x C-a"]
  12299. ["Sparse trees open ARCHIVE trees"
  12300. (setq org-sparse-tree-open-archived-trees
  12301. (not org-sparse-tree-open-archived-trees))
  12302. :style toggle :selected org-sparse-tree-open-archived-trees]
  12303. ["Cycling opens ARCHIVE trees"
  12304. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12305. :style toggle :selected org-cycle-open-archived-trees]
  12306. "--"
  12307. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12308. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12309. ; ["Check and Move Children" (org-archive-subtree '(4))
  12310. ; :active t :keys "C-u C-c C-x C-s"]
  12311. )
  12312. "--"
  12313. ("TODO Lists"
  12314. ["TODO/DONE/-" org-todo t]
  12315. ("Select keyword"
  12316. ["Next keyword" org-shiftright (org-on-heading-p)]
  12317. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12318. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12319. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12320. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12321. ["Show TODO Tree" org-show-todo-tree t]
  12322. ["Global TODO list" org-todo-list t]
  12323. "--"
  12324. ["Set Priority" org-priority t]
  12325. ["Priority Up" org-shiftup t]
  12326. ["Priority Down" org-shiftdown t])
  12327. ("TAGS and Properties"
  12328. ["Set Tags" org-set-tags-command t]
  12329. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12330. "--"
  12331. ["Set property" org-set-property t]
  12332. ["Column view of properties" org-columns t]
  12333. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12334. ("Dates and Scheduling"
  12335. ["Timestamp" org-time-stamp t]
  12336. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12337. ("Change Date"
  12338. ["1 Day Later" org-shiftright t]
  12339. ["1 Day Earlier" org-shiftleft t]
  12340. ["1 ... Later" org-shiftup t]
  12341. ["1 ... Earlier" org-shiftdown t])
  12342. ["Compute Time Range" org-evaluate-time-range t]
  12343. ["Schedule Item" org-schedule t]
  12344. ["Deadline" org-deadline t]
  12345. "--"
  12346. ["Custom time format" org-toggle-time-stamp-overlays
  12347. :style radio :selected org-display-custom-times]
  12348. "--"
  12349. ["Goto Calendar" org-goto-calendar t]
  12350. ["Date from Calendar" org-date-from-calendar t]
  12351. "--"
  12352. ["Start/restart timer" org-timer-start t]
  12353. ["Insert timer string" org-timer t]
  12354. ["Insert timer item" org-timer-item t])
  12355. ("Logging work"
  12356. ["Clock in" org-clock-in t]
  12357. ["Clock out" org-clock-out t]
  12358. ["Clock cancel" org-clock-cancel t]
  12359. ["Goto running clock" org-clock-goto t]
  12360. ["Display times" org-clock-display t]
  12361. ["Create clock table" org-clock-report t]
  12362. "--"
  12363. ["Record DONE time"
  12364. (progn (setq org-log-done (not org-log-done))
  12365. (message "Switching to %s will %s record a timestamp"
  12366. (car org-done-keywords)
  12367. (if org-log-done "automatically" "not")))
  12368. :style toggle :selected org-log-done])
  12369. "--"
  12370. ["Agenda Command..." org-agenda t]
  12371. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12372. ("File List for Agenda")
  12373. ("Special views current file"
  12374. ["TODO Tree" org-show-todo-tree t]
  12375. ["Check Deadlines" org-check-deadlines t]
  12376. ["Timeline" org-timeline t]
  12377. ["Tags Tree" org-tags-sparse-tree t])
  12378. "--"
  12379. ("Hyperlinks"
  12380. ["Store Link (Global)" org-store-link t]
  12381. ["Insert Link" org-insert-link t]
  12382. ["Follow Link" org-open-at-point t]
  12383. "--"
  12384. ["Next link" org-next-link t]
  12385. ["Previous link" org-previous-link t]
  12386. "--"
  12387. ["Descriptive Links"
  12388. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12389. :style radio
  12390. :selected (member '(org-link) buffer-invisibility-spec)]
  12391. ["Literal Links"
  12392. (progn
  12393. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12394. :style radio
  12395. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12396. "--"
  12397. ["Export/Publish..." org-export t]
  12398. ("LaTeX"
  12399. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12400. :selected org-cdlatex-mode]
  12401. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12402. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12403. ["Modify math symbol" org-cdlatex-math-modify
  12404. (org-inside-LaTeX-fragment-p)]
  12405. ["Export LaTeX fragments as images"
  12406. (if (featurep 'org-exp)
  12407. (setq org-export-with-LaTeX-fragments
  12408. (not org-export-with-LaTeX-fragments))
  12409. (require 'org-exp))
  12410. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12411. org-export-with-LaTeX-fragments)])
  12412. "--"
  12413. ("Documentation"
  12414. ["Show Version" org-version t]
  12415. ["Info Documentation" org-info t])
  12416. ("Customize"
  12417. ["Browse Org Group" org-customize t]
  12418. "--"
  12419. ["Expand This Menu" org-create-customize-menu
  12420. (fboundp 'customize-menu-create)])
  12421. "--"
  12422. ["Refresh setup" org-mode-restart t]
  12423. ))
  12424. (defun org-info (&optional node)
  12425. "Read documentation for Org-mode in the info system.
  12426. With optional NODE, go directly to that node."
  12427. (interactive)
  12428. (info (format "(org)%s" (or node ""))))
  12429. (defun org-install-agenda-files-menu ()
  12430. (let ((bl (buffer-list)))
  12431. (save-excursion
  12432. (while bl
  12433. (set-buffer (pop bl))
  12434. (if (org-mode-p) (setq bl nil)))
  12435. (when (org-mode-p)
  12436. (easy-menu-change
  12437. '("Org") "File List for Agenda"
  12438. (append
  12439. (list
  12440. ["Edit File List" (org-edit-agenda-file-list) t]
  12441. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12442. ["Remove Current File from List" org-remove-file t]
  12443. ["Cycle through agenda files" org-cycle-agenda-files t]
  12444. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12445. "--")
  12446. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12447. ;;;; Documentation
  12448. ;;;###autoload
  12449. (defun org-require-autoloaded-modules ()
  12450. (interactive)
  12451. (mapc 'require
  12452. '(org-agenda org-archive org-clock org-colview
  12453. org-exp org-id org-export-latex org-publish
  12454. org-remember org-table)))
  12455. ;;;###autoload
  12456. (defun org-customize ()
  12457. "Call the customize function with org as argument."
  12458. (interactive)
  12459. (org-load-modules-maybe)
  12460. (org-require-autoloaded-modules)
  12461. (customize-browse 'org))
  12462. (defun org-create-customize-menu ()
  12463. "Create a full customization menu for Org-mode, insert it into the menu."
  12464. (interactive)
  12465. (org-load-modules-maybe)
  12466. (org-require-autoloaded-modules)
  12467. (if (fboundp 'customize-menu-create)
  12468. (progn
  12469. (easy-menu-change
  12470. '("Org") "Customize"
  12471. `(["Browse Org group" org-customize t]
  12472. "--"
  12473. ,(customize-menu-create 'org)
  12474. ["Set" Custom-set t]
  12475. ["Save" Custom-save t]
  12476. ["Reset to Current" Custom-reset-current t]
  12477. ["Reset to Saved" Custom-reset-saved t]
  12478. ["Reset to Standard Settings" Custom-reset-standard t]))
  12479. (message "\"Org\"-menu now contains full customization menu"))
  12480. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12481. ;;;; Miscellaneous stuff
  12482. ;;; Generally useful functions
  12483. (defun org-find-text-property-in-string (prop s)
  12484. "Return the first non-nil value of property PROP in string S."
  12485. (or (get-text-property 0 prop s)
  12486. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12487. prop s)))
  12488. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12489. "Display the given MESSAGE as a warning."
  12490. (if (fboundp 'display-warning)
  12491. (display-warning 'org message
  12492. (if (featurep 'xemacs)
  12493. 'warning
  12494. :warning))
  12495. (let ((buf (get-buffer-create "*Org warnings*")))
  12496. (with-current-buffer buf
  12497. (goto-char (point-max))
  12498. (insert "Warning (Org): " message)
  12499. (unless (bolp)
  12500. (newline)))
  12501. (display-buffer buf)
  12502. (sit-for 0))))
  12503. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12504. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12505. (if (and marker (marker-buffer marker)
  12506. (buffer-live-p (marker-buffer marker)))
  12507. (progn
  12508. (switch-to-buffer (marker-buffer marker))
  12509. (if (or (> marker (point-max)) (< marker (point-min)))
  12510. (widen))
  12511. (goto-char marker))
  12512. (if bookmark
  12513. (bookmark-jump bookmark)
  12514. (error "Cannot find location"))))
  12515. (defun org-quote-csv-field (s)
  12516. "Quote field for inclusion in CSV material."
  12517. (if (string-match "[\",]" s)
  12518. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12519. s))
  12520. (defun org-plist-delete (plist property)
  12521. "Delete PROPERTY from PLIST.
  12522. This is in contrast to merely setting it to 0."
  12523. (let (p)
  12524. (while plist
  12525. (if (not (eq property (car plist)))
  12526. (setq p (plist-put p (car plist) (nth 1 plist))))
  12527. (setq plist (cddr plist)))
  12528. p))
  12529. (defun org-force-self-insert (N)
  12530. "Needed to enforce self-insert under remapping."
  12531. (interactive "p")
  12532. (self-insert-command N))
  12533. (defun org-string-width (s)
  12534. "Compute width of string, ignoring invisible characters.
  12535. This ignores character with invisibility property `org-link', and also
  12536. characters with property `org-cwidth', because these will become invisible
  12537. upon the next fontification round."
  12538. (let (b l)
  12539. (when (or (eq t buffer-invisibility-spec)
  12540. (assq 'org-link buffer-invisibility-spec))
  12541. (while (setq b (text-property-any 0 (length s)
  12542. 'invisible 'org-link s))
  12543. (setq s (concat (substring s 0 b)
  12544. (substring s (or (next-single-property-change
  12545. b 'invisible s) (length s)))))))
  12546. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12547. (setq s (concat (substring s 0 b)
  12548. (substring s (or (next-single-property-change
  12549. b 'org-cwidth s) (length s))))))
  12550. (setq l (string-width s) b -1)
  12551. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12552. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12553. l))
  12554. (defun org-get-indentation (&optional line)
  12555. "Get the indentation of the current line, interpreting tabs.
  12556. When LINE is given, assume it represents a line and compute its indentation."
  12557. (if line
  12558. (if (string-match "^ *" (org-remove-tabs line))
  12559. (match-end 0))
  12560. (save-excursion
  12561. (beginning-of-line 1)
  12562. (skip-chars-forward " \t")
  12563. (current-column))))
  12564. (defun org-remove-tabs (s &optional width)
  12565. "Replace tabulators in S with spaces.
  12566. Assumes that s is a single line, starting in column 0."
  12567. (setq width (or width tab-width))
  12568. (while (string-match "\t" s)
  12569. (setq s (replace-match
  12570. (make-string
  12571. (- (* width (/ (+ (match-beginning 0) width) width))
  12572. (match-beginning 0)) ?\ )
  12573. t t s)))
  12574. s)
  12575. (defun org-fix-indentation (line ind)
  12576. "Fix indentation in LINE.
  12577. IND is a cons cell with target and minimum indentation.
  12578. If the current indentation in LINE is smaller than the minimum,
  12579. leave it alone. If it is larger than ind, set it to the target."
  12580. (let* ((l (org-remove-tabs line))
  12581. (i (org-get-indentation l))
  12582. (i1 (car ind)) (i2 (cdr ind)))
  12583. (if (>= i i2) (setq l (substring line i2)))
  12584. (if (> i1 0)
  12585. (concat (make-string i1 ?\ ) l)
  12586. l)))
  12587. (defun org-base-buffer (buffer)
  12588. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12589. (if (not buffer)
  12590. buffer
  12591. (or (buffer-base-buffer buffer)
  12592. buffer)))
  12593. (defun org-trim (s)
  12594. "Remove whitespace at beginning and end of string."
  12595. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12596. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12597. s)
  12598. (defun org-wrap (string &optional width lines)
  12599. "Wrap string to either a number of lines, or a width in characters.
  12600. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12601. that costs. If there is a word longer than WIDTH, the text is actually
  12602. wrapped to the length of that word.
  12603. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12604. many lines, whatever width that takes.
  12605. The return value is a list of lines, without newlines at the end."
  12606. (let* ((words (org-split-string string "[ \t\n]+"))
  12607. (maxword (apply 'max (mapcar 'org-string-width words)))
  12608. w ll)
  12609. (cond (width
  12610. (org-do-wrap words (max maxword width)))
  12611. (lines
  12612. (setq w maxword)
  12613. (setq ll (org-do-wrap words maxword))
  12614. (if (<= (length ll) lines)
  12615. ll
  12616. (setq ll words)
  12617. (while (> (length ll) lines)
  12618. (setq w (1+ w))
  12619. (setq ll (org-do-wrap words w)))
  12620. ll))
  12621. (t (error "Cannot wrap this")))))
  12622. (defun org-do-wrap (words width)
  12623. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12624. (let (lines line)
  12625. (while words
  12626. (setq line (pop words))
  12627. (while (and words (< (+ (length line) (length (car words))) width))
  12628. (setq line (concat line " " (pop words))))
  12629. (setq lines (push line lines)))
  12630. (nreverse lines)))
  12631. (defun org-split-string (string &optional separators)
  12632. "Splits STRING into substrings at SEPARATORS.
  12633. No empty strings are returned if there are matches at the beginning
  12634. and end of string."
  12635. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12636. (start 0)
  12637. notfirst
  12638. (list nil))
  12639. (while (and (string-match rexp string
  12640. (if (and notfirst
  12641. (= start (match-beginning 0))
  12642. (< start (length string)))
  12643. (1+ start) start))
  12644. (< (match-beginning 0) (length string)))
  12645. (setq notfirst t)
  12646. (or (eq (match-beginning 0) 0)
  12647. (and (eq (match-beginning 0) (match-end 0))
  12648. (eq (match-beginning 0) start))
  12649. (setq list
  12650. (cons (substring string start (match-beginning 0))
  12651. list)))
  12652. (setq start (match-end 0)))
  12653. (or (eq start (length string))
  12654. (setq list
  12655. (cons (substring string start)
  12656. list)))
  12657. (nreverse list)))
  12658. (defun org-context ()
  12659. "Return a list of contexts of the current cursor position.
  12660. If several contexts apply, all are returned.
  12661. Each context entry is a list with a symbol naming the context, and
  12662. two positions indicating start and end of the context. Possible
  12663. contexts are:
  12664. :headline anywhere in a headline
  12665. :headline-stars on the leading stars in a headline
  12666. :todo-keyword on a TODO keyword (including DONE) in a headline
  12667. :tags on the TAGS in a headline
  12668. :priority on the priority cookie in a headline
  12669. :item on the first line of a plain list item
  12670. :item-bullet on the bullet/number of a plain list item
  12671. :checkbox on the checkbox in a plain list item
  12672. :table in an org-mode table
  12673. :table-special on a special filed in a table
  12674. :table-table in a table.el table
  12675. :link on a hyperlink
  12676. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12677. :target on a <<target>>
  12678. :radio-target on a <<<radio-target>>>
  12679. :latex-fragment on a LaTeX fragment
  12680. :latex-preview on a LaTeX fragment with overlayed preview image
  12681. This function expects the position to be visible because it uses font-lock
  12682. faces as a help to recognize the following contexts: :table-special, :link,
  12683. and :keyword."
  12684. (let* ((f (get-text-property (point) 'face))
  12685. (faces (if (listp f) f (list f)))
  12686. (p (point)) clist o)
  12687. ;; First the large context
  12688. (cond
  12689. ((org-on-heading-p t)
  12690. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12691. (when (progn
  12692. (beginning-of-line 1)
  12693. (looking-at org-todo-line-tags-regexp))
  12694. (push (org-point-in-group p 1 :headline-stars) clist)
  12695. (push (org-point-in-group p 2 :todo-keyword) clist)
  12696. (push (org-point-in-group p 4 :tags) clist))
  12697. (goto-char p)
  12698. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12699. (if (looking-at "\\[#[A-Z0-9]\\]")
  12700. (push (org-point-in-group p 0 :priority) clist)))
  12701. ((org-at-item-p)
  12702. (push (org-point-in-group p 2 :item-bullet) clist)
  12703. (push (list :item (point-at-bol)
  12704. (save-excursion (org-end-of-item) (point)))
  12705. clist)
  12706. (and (org-at-item-checkbox-p)
  12707. (push (org-point-in-group p 0 :checkbox) clist)))
  12708. ((org-at-table-p)
  12709. (push (list :table (org-table-begin) (org-table-end)) clist)
  12710. (if (memq 'org-formula faces)
  12711. (push (list :table-special
  12712. (previous-single-property-change p 'face)
  12713. (next-single-property-change p 'face)) clist)))
  12714. ((org-at-table-p 'any)
  12715. (push (list :table-table) clist)))
  12716. (goto-char p)
  12717. ;; Now the small context
  12718. (cond
  12719. ((org-at-timestamp-p)
  12720. (push (org-point-in-group p 0 :timestamp) clist))
  12721. ((memq 'org-link faces)
  12722. (push (list :link
  12723. (previous-single-property-change p 'face)
  12724. (next-single-property-change p 'face)) clist))
  12725. ((memq 'org-special-keyword faces)
  12726. (push (list :keyword
  12727. (previous-single-property-change p 'face)
  12728. (next-single-property-change p 'face)) clist))
  12729. ((org-on-target-p)
  12730. (push (org-point-in-group p 0 :target) clist)
  12731. (goto-char (1- (match-beginning 0)))
  12732. (if (looking-at org-radio-target-regexp)
  12733. (push (org-point-in-group p 0 :radio-target) clist))
  12734. (goto-char p))
  12735. ((setq o (car (delq nil
  12736. (mapcar
  12737. (lambda (x)
  12738. (if (memq x org-latex-fragment-image-overlays) x))
  12739. (org-overlays-at (point))))))
  12740. (push (list :latex-fragment
  12741. (org-overlay-start o) (org-overlay-end o)) clist)
  12742. (push (list :latex-preview
  12743. (org-overlay-start o) (org-overlay-end o)) clist))
  12744. ((org-inside-LaTeX-fragment-p)
  12745. ;; FIXME: positions wrong.
  12746. (push (list :latex-fragment (point) (point)) clist)))
  12747. (setq clist (nreverse (delq nil clist)))
  12748. clist))
  12749. ;; FIXME: Compare with at-regexp-p Do we need both?
  12750. (defun org-in-regexp (re &optional nlines visually)
  12751. "Check if point is inside a match of regexp.
  12752. Normally only the current line is checked, but you can include NLINES extra
  12753. lines both before and after point into the search.
  12754. If VISUALLY is set, require that the cursor is not after the match but
  12755. really on, so that the block visually is on the match."
  12756. (catch 'exit
  12757. (let ((pos (point))
  12758. (eol (point-at-eol (+ 1 (or nlines 0))))
  12759. (inc (if visually 1 0)))
  12760. (save-excursion
  12761. (beginning-of-line (- 1 (or nlines 0)))
  12762. (while (re-search-forward re eol t)
  12763. (if (and (<= (match-beginning 0) pos)
  12764. (>= (+ inc (match-end 0)) pos))
  12765. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12766. (defun org-at-regexp-p (regexp)
  12767. "Is point inside a match of REGEXP in the current line?"
  12768. (catch 'exit
  12769. (save-excursion
  12770. (let ((pos (point)) (end (point-at-eol)))
  12771. (beginning-of-line 1)
  12772. (while (re-search-forward regexp end t)
  12773. (if (and (<= (match-beginning 0) pos)
  12774. (>= (match-end 0) pos))
  12775. (throw 'exit t)))
  12776. nil))))
  12777. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12778. "Call `multi-occur' with buffers for all agenda files."
  12779. (interactive "sOrg-files matching: \np")
  12780. (let* ((files (org-agenda-files))
  12781. (tnames (mapcar 'file-truename files))
  12782. (extra org-agenda-text-search-extra-files)
  12783. f)
  12784. (when (eq (car extra) 'agenda-archives)
  12785. (setq extra (cdr extra))
  12786. (setq files (org-add-archive-files files)))
  12787. (while (setq f (pop extra))
  12788. (unless (member (file-truename f) tnames)
  12789. (add-to-list 'files f 'append)
  12790. (add-to-list 'tnames (file-truename f) 'append)))
  12791. (multi-occur
  12792. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12793. regexp)))
  12794. (if (boundp 'occur-mode-find-occurrence-hook)
  12795. ;; Emacs 23
  12796. (add-hook 'occur-mode-find-occurrence-hook
  12797. (lambda ()
  12798. (when (org-mode-p)
  12799. (org-reveal))))
  12800. ;; Emacs 22
  12801. (defadvice occur-mode-goto-occurrence
  12802. (after org-occur-reveal activate)
  12803. (and (org-mode-p) (org-reveal)))
  12804. (defadvice occur-mode-goto-occurrence-other-window
  12805. (after org-occur-reveal activate)
  12806. (and (org-mode-p) (org-reveal)))
  12807. (defadvice occur-mode-display-occurrence
  12808. (after org-occur-reveal activate)
  12809. (when (org-mode-p)
  12810. (let ((pos (occur-mode-find-occurrence)))
  12811. (with-current-buffer (marker-buffer pos)
  12812. (save-excursion
  12813. (goto-char pos)
  12814. (org-reveal)))))))
  12815. (defun org-uniquify (list)
  12816. "Remove duplicate elements from LIST."
  12817. (let (res)
  12818. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12819. res))
  12820. (defun org-delete-all (elts list)
  12821. "Remove all elements in ELTS from LIST."
  12822. (while elts
  12823. (setq list (delete (pop elts) list)))
  12824. list)
  12825. (defun org-back-over-empty-lines ()
  12826. "Move backwards over whitespace, to the beginning of the first empty line.
  12827. Returns the number of empty lines passed."
  12828. (let ((pos (point)))
  12829. (skip-chars-backward " \t\n\r")
  12830. (beginning-of-line 2)
  12831. (goto-char (min (point) pos))
  12832. (count-lines (point) pos)))
  12833. (defun org-skip-whitespace ()
  12834. (skip-chars-forward " \t\n\r"))
  12835. (defun org-point-in-group (point group &optional context)
  12836. "Check if POINT is in match-group GROUP.
  12837. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12838. match. If the match group does ot exist or point is not inside it,
  12839. return nil."
  12840. (and (match-beginning group)
  12841. (>= point (match-beginning group))
  12842. (<= point (match-end group))
  12843. (if context
  12844. (list context (match-beginning group) (match-end group))
  12845. t)))
  12846. (defun org-switch-to-buffer-other-window (&rest args)
  12847. "Switch to buffer in a second window on the current frame.
  12848. In particular, do not allow pop-up frames."
  12849. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12850. special-display-function)
  12851. (apply 'switch-to-buffer-other-window args)))
  12852. (defun org-combine-plists (&rest plists)
  12853. "Create a single property list from all plists in PLISTS.
  12854. The process starts by copying the first list, and then setting properties
  12855. from the other lists. Settings in the last list are the most significant
  12856. ones and overrule settings in the other lists."
  12857. (let ((rtn (copy-sequence (pop plists)))
  12858. p v ls)
  12859. (while plists
  12860. (setq ls (pop plists))
  12861. (while ls
  12862. (setq p (pop ls) v (pop ls))
  12863. (setq rtn (plist-put rtn p v))))
  12864. rtn))
  12865. (defun org-move-line-down (arg)
  12866. "Move the current line down. With prefix argument, move it past ARG lines."
  12867. (interactive "p")
  12868. (let ((col (current-column))
  12869. beg end pos)
  12870. (beginning-of-line 1) (setq beg (point))
  12871. (beginning-of-line 2) (setq end (point))
  12872. (beginning-of-line (+ 1 arg))
  12873. (setq pos (move-marker (make-marker) (point)))
  12874. (insert (delete-and-extract-region beg end))
  12875. (goto-char pos)
  12876. (org-move-to-column col)))
  12877. (defun org-move-line-up (arg)
  12878. "Move the current line up. With prefix argument, move it past ARG lines."
  12879. (interactive "p")
  12880. (let ((col (current-column))
  12881. beg end pos)
  12882. (beginning-of-line 1) (setq beg (point))
  12883. (beginning-of-line 2) (setq end (point))
  12884. (beginning-of-line (- arg))
  12885. (setq pos (move-marker (make-marker) (point)))
  12886. (insert (delete-and-extract-region beg end))
  12887. (goto-char pos)
  12888. (org-move-to-column col)))
  12889. (defun org-replace-escapes (string table)
  12890. "Replace %-escapes in STRING with values in TABLE.
  12891. TABLE is an association list with keys like \"%a\" and string values.
  12892. The sequences in STRING may contain normal field width and padding information,
  12893. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12894. so values can contain further %-escapes if they are define later in TABLE."
  12895. (let ((case-fold-search nil)
  12896. e re rpl)
  12897. (while (setq e (pop table))
  12898. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12899. (while (string-match re string)
  12900. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12901. (cdr e)))
  12902. (setq string (replace-match rpl t t string))))
  12903. string))
  12904. (defun org-sublist (list start end)
  12905. "Return a section of LIST, from START to END.
  12906. Counting starts at 1."
  12907. (let (rtn (c start))
  12908. (setq list (nthcdr (1- start) list))
  12909. (while (and list (<= c end))
  12910. (push (pop list) rtn)
  12911. (setq c (1+ c)))
  12912. (nreverse rtn)))
  12913. (defun org-find-base-buffer-visiting (file)
  12914. "Like `find-buffer-visiting' but alway return the base buffer and
  12915. not an indirect buffer."
  12916. (let ((buf (find-buffer-visiting file)))
  12917. (if buf
  12918. (or (buffer-base-buffer buf) buf)
  12919. nil)))
  12920. (defun org-image-file-name-regexp ()
  12921. "Return regexp matching the file names of images."
  12922. (if (fboundp 'image-file-name-regexp)
  12923. (image-file-name-regexp)
  12924. (let ((image-file-name-extensions
  12925. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12926. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12927. (concat "\\."
  12928. (regexp-opt (nconc (mapcar 'upcase
  12929. image-file-name-extensions)
  12930. image-file-name-extensions)
  12931. t)
  12932. "\\'"))))
  12933. (defun org-file-image-p (file)
  12934. "Return non-nil if FILE is an image."
  12935. (save-match-data
  12936. (string-match (org-image-file-name-regexp) file)))
  12937. (defun org-get-cursor-date ()
  12938. "Return the date at cursor in as a time.
  12939. This works in the calendar and in the agenda, anywhere else it just
  12940. returns the current time."
  12941. (let (date day defd)
  12942. (cond
  12943. ((eq major-mode 'calendar-mode)
  12944. (setq date (calendar-cursor-to-date)
  12945. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12946. ((eq major-mode 'org-agenda-mode)
  12947. (setq day (get-text-property (point) 'day))
  12948. (if day
  12949. (setq date (calendar-gregorian-from-absolute day)
  12950. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12951. (nth 2 date))))))
  12952. (or defd (current-time))))
  12953. (defvar org-agenda-action-marker (make-marker)
  12954. "Marker pointing to the entry for the next agenda action.")
  12955. (defun org-mark-entry-for-agenda-action ()
  12956. "Mark the current entry as target of an agenda action.
  12957. Agenda actions are actions executed from the agenda with the key `k',
  12958. which make use of the date at the cursor."
  12959. (interactive)
  12960. (move-marker org-agenda-action-marker
  12961. (save-excursion (org-back-to-heading t) (point))
  12962. (current-buffer))
  12963. (message
  12964. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12965. ;;; Paragraph filling stuff.
  12966. ;; We want this to be just right, so use the full arsenal.
  12967. (defun org-indent-line-function ()
  12968. "Indent line like previous, but further if previous was headline or item."
  12969. (interactive)
  12970. (let* ((pos (point))
  12971. (itemp (org-at-item-p))
  12972. column bpos bcol tpos tcol bullet btype bullet-type)
  12973. ;; Find the previous relevant line
  12974. (beginning-of-line 1)
  12975. (cond
  12976. ((looking-at "#") (setq column 0))
  12977. ((looking-at "\\*+ ") (setq column 0))
  12978. (t
  12979. (beginning-of-line 0)
  12980. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12981. (beginning-of-line 0))
  12982. (cond
  12983. ((looking-at "\\*+[ \t]+")
  12984. (if (not org-adapt-indentation)
  12985. (setq column 0)
  12986. (goto-char (match-end 0))
  12987. (setq column (current-column))))
  12988. ((org-in-item-p)
  12989. (org-beginning-of-item)
  12990. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12991. (setq bpos (match-beginning 1) tpos (match-end 0)
  12992. bcol (progn (goto-char bpos) (current-column))
  12993. tcol (progn (goto-char tpos) (current-column))
  12994. bullet (match-string 1)
  12995. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12996. (if (> tcol (+ bcol org-description-max-indent))
  12997. (setq tcol (+ bcol 5)))
  12998. (if (not itemp)
  12999. (setq column tcol)
  13000. (goto-char pos)
  13001. (beginning-of-line 1)
  13002. (if (looking-at "\\S-")
  13003. (progn
  13004. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13005. (setq bullet (match-string 1)
  13006. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13007. (setq column (if (equal btype bullet-type) bcol tcol)))
  13008. (setq column (org-get-indentation)))))
  13009. (t (setq column (org-get-indentation))))))
  13010. (goto-char pos)
  13011. (if (<= (current-column) (current-indentation))
  13012. (org-indent-line-to column)
  13013. (save-excursion (org-indent-line-to column)))
  13014. (setq column (current-column))
  13015. (beginning-of-line 1)
  13016. (if (looking-at
  13017. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13018. (replace-match (concat "\\1" (format org-property-format
  13019. (match-string 2) (match-string 3)))
  13020. t nil))
  13021. (org-move-to-column column)))
  13022. (defun org-set-autofill-regexps ()
  13023. (interactive)
  13024. ;; In the paragraph separator we include headlines, because filling
  13025. ;; text in a line directly attached to a headline would otherwise
  13026. ;; fill the headline as well.
  13027. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13028. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13029. ;; The paragraph starter includes hand-formatted lists.
  13030. (org-set-local 'paragraph-start
  13031. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13032. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13033. ;; But only if the user has not turned off tables or fixed-width regions
  13034. (org-set-local
  13035. 'auto-fill-inhibit-regexp
  13036. (concat "\\*+ \\|#\\+"
  13037. "\\|[ \t]*" org-keyword-time-regexp
  13038. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13039. (concat
  13040. "\\|[ \t]*["
  13041. (if org-enable-table-editor "|" "")
  13042. (if org-enable-fixed-width-editor ":" "")
  13043. "]"))))
  13044. ;; We use our own fill-paragraph function, to make sure that tables
  13045. ;; and fixed-width regions are not wrapped. That function will pass
  13046. ;; through to `fill-paragraph' when appropriate.
  13047. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13048. ; Adaptive filling: To get full control, first make sure that
  13049. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13050. (org-set-local 'adaptive-fill-regexp "\000")
  13051. (org-set-local 'adaptive-fill-function
  13052. 'org-adaptive-fill-function)
  13053. (org-set-local
  13054. 'align-mode-rules-list
  13055. '((org-in-buffer-settings
  13056. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13057. (modes . '(org-mode))))))
  13058. (defun org-fill-paragraph (&optional justify)
  13059. "Re-align a table, pass through to fill-paragraph if no table."
  13060. (let ((table-p (org-at-table-p))
  13061. (table.el-p (org-at-table.el-p)))
  13062. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13063. (save-excursion (goto-char (point-at-bol))
  13064. (looking-at outline-regexp)))
  13065. t) ; skip headlines
  13066. (table.el-p t) ; skip table.el tables
  13067. (table-p (org-table-align) t) ; align org-mode tables
  13068. (t nil)))) ; call paragraph-fill
  13069. ;; For reference, this is the default value of adaptive-fill-regexp
  13070. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13071. (defun org-adaptive-fill-function ()
  13072. "Return a fill prefix for org-mode files.
  13073. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13074. work correctly."
  13075. (cond ((looking-at "#[ \t]+")
  13076. (match-string 0))
  13077. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13078. (save-excursion
  13079. (if (> (match-end 1) (+ (match-beginning 1)
  13080. org-description-max-indent))
  13081. (goto-char (+ (match-beginning 1) 5))
  13082. (goto-char (match-end 0)))
  13083. (make-string (current-column) ?\ )))
  13084. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13085. (save-excursion
  13086. (goto-char (match-end 0))
  13087. (make-string (current-column) ?\ )))
  13088. (t nil)))
  13089. ;;; Other stuff.
  13090. (defun org-toggle-fixed-width-section (arg)
  13091. "Toggle the fixed-width export.
  13092. If there is no active region, the QUOTE keyword at the current headline is
  13093. inserted or removed. When present, it causes the text between this headline
  13094. and the next to be exported as fixed-width text, and unmodified.
  13095. If there is an active region, this command adds or removes a colon as the
  13096. first character of this line. If the first character of a line is a colon,
  13097. this line is also exported in fixed-width font."
  13098. (interactive "P")
  13099. (let* ((cc 0)
  13100. (regionp (org-region-active-p))
  13101. (beg (if regionp (region-beginning) (point)))
  13102. (end (if regionp (region-end)))
  13103. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13104. (case-fold-search nil)
  13105. (re "[ \t]*\\(:\\)")
  13106. off)
  13107. (if regionp
  13108. (save-excursion
  13109. (goto-char beg)
  13110. (setq cc (current-column))
  13111. (beginning-of-line 1)
  13112. (setq off (looking-at re))
  13113. (while (> nlines 0)
  13114. (setq nlines (1- nlines))
  13115. (beginning-of-line 1)
  13116. (cond
  13117. (arg
  13118. (org-move-to-column cc t)
  13119. (insert ":\n")
  13120. (forward-line -1))
  13121. ((and off (looking-at re))
  13122. (replace-match "" t t nil 1))
  13123. ((not off) (org-move-to-column cc t) (insert ":")))
  13124. (forward-line 1)))
  13125. (save-excursion
  13126. (org-back-to-heading)
  13127. (if (looking-at (concat outline-regexp
  13128. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13129. (replace-match "" t t nil 1)
  13130. (if (looking-at outline-regexp)
  13131. (progn
  13132. (goto-char (match-end 0))
  13133. (insert org-quote-string " "))))))))
  13134. ;;;; Functions extending outline functionality
  13135. (defun org-beginning-of-line (&optional arg)
  13136. "Go to the beginning of the current line. If that is invisible, continue
  13137. to a visible line beginning. This makes the function of C-a more intuitive.
  13138. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13139. first attempt, and only move to after the tags when the cursor is already
  13140. beyond the end of the headline."
  13141. (interactive "P")
  13142. (let ((pos (point)) refpos)
  13143. (beginning-of-line 1)
  13144. (if (bobp)
  13145. nil
  13146. (backward-char 1)
  13147. (if (org-invisible-p)
  13148. (while (and (not (bobp)) (org-invisible-p))
  13149. (backward-char 1)
  13150. (beginning-of-line 1))
  13151. (forward-char 1)))
  13152. (when org-special-ctrl-a/e
  13153. (cond
  13154. ((and (looking-at org-complex-heading-regexp)
  13155. (= (char-after (match-end 1)) ?\ ))
  13156. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13157. (point-at-eol)))
  13158. (goto-char
  13159. (if (eq org-special-ctrl-a/e t)
  13160. (cond ((> pos refpos) refpos)
  13161. ((= pos (point)) refpos)
  13162. (t (point)))
  13163. (cond ((> pos (point)) (point))
  13164. ((not (eq last-command this-command)) (point))
  13165. (t refpos)))))
  13166. ((org-at-item-p)
  13167. (goto-char
  13168. (if (eq org-special-ctrl-a/e t)
  13169. (cond ((> pos (match-end 4)) (match-end 4))
  13170. ((= pos (point)) (match-end 4))
  13171. (t (point)))
  13172. (cond ((> pos (point)) (point))
  13173. ((not (eq last-command this-command)) (point))
  13174. (t (match-end 4))))))))
  13175. (org-no-warnings
  13176. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13177. (defun org-end-of-line (&optional arg)
  13178. "Go to the end of the line.
  13179. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13180. first attempt, and only move to after the tags when the cursor is already
  13181. beyond the end of the headline."
  13182. (interactive "P")
  13183. (if (or (not org-special-ctrl-a/e)
  13184. (not (org-on-heading-p)))
  13185. (end-of-line arg)
  13186. (let ((pos (point)))
  13187. (beginning-of-line 1)
  13188. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13189. (if (eq org-special-ctrl-a/e t)
  13190. (if (or (< pos (match-beginning 1))
  13191. (= pos (match-end 0)))
  13192. (goto-char (match-beginning 1))
  13193. (goto-char (match-end 0)))
  13194. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13195. (goto-char (match-end 0))
  13196. (goto-char (match-beginning 1))))
  13197. (end-of-line arg))))
  13198. (org-no-warnings
  13199. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13200. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13201. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13202. (defun org-kill-line (&optional arg)
  13203. "Kill line, to tags or end of line."
  13204. (interactive "P")
  13205. (cond
  13206. ((or (not org-special-ctrl-k)
  13207. (bolp)
  13208. (not (org-on-heading-p)))
  13209. (call-interactively 'kill-line))
  13210. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13211. (kill-region (point) (match-beginning 1))
  13212. (org-set-tags nil t))
  13213. (t (kill-region (point) (point-at-eol)))))
  13214. (define-key org-mode-map "\C-k" 'org-kill-line)
  13215. (defun org-yank (&optional arg)
  13216. "Yank. If the kill is a subtree, treat it specially.
  13217. This command will look at the current kill and check if is a single
  13218. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13219. cursor is at the beginning of a line or after the stars of a currently
  13220. empty headline, then the yank is handled specially. How exactly depends
  13221. on the value of the following variables, both set by default.
  13222. org-yank-folded-subtrees
  13223. When set, the subtree(s) will be folded after insertion, but only
  13224. if doing so would now swallow text after the yanked text.
  13225. org-yank-adjusted-subtrees
  13226. When set, the subtree will be promoted or demoted in order to
  13227. fit into the local outline tree structure, which means that the level
  13228. will be adjusted so that it becomes the smaller one of the two
  13229. *visible* surrounding headings.
  13230. Any prefix to this command will cause `yank' to be called directly with
  13231. no special treatment. In particular, a simple `C-u' prefix will just
  13232. plainly yank the text as it is.
  13233. \[1] Basically, the test checks if the first non-white line is a heading
  13234. and if there are no other headings with fewer stars."
  13235. (interactive "P")
  13236. (setq this-command 'yank)
  13237. (if arg
  13238. (call-interactively 'yank)
  13239. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13240. (and (org-kill-is-subtree-p)
  13241. (or (bolp)
  13242. (and (looking-at "[ \t]*$")
  13243. (string-match
  13244. "\\`\\*+\\'"
  13245. (buffer-substring (point-at-bol) (point)))))))
  13246. swallowp)
  13247. (cond
  13248. ((and subtreep org-yank-folded-subtrees)
  13249. (let ((beg (point))
  13250. end)
  13251. (if (and subtreep org-yank-adjusted-subtrees)
  13252. (org-paste-subtree nil nil 'for-yank)
  13253. (call-interactively 'yank))
  13254. (setq end (point))
  13255. (goto-char beg)
  13256. (when (and (bolp) subtreep
  13257. (not (setq swallowp
  13258. (org-yank-folding-would-swallow-text beg end))))
  13259. (or (looking-at outline-regexp)
  13260. (re-search-forward (concat "^" outline-regexp) end t))
  13261. (while (and (< (point) end) (looking-at outline-regexp))
  13262. (hide-subtree)
  13263. (org-cycle-show-empty-lines 'folded)
  13264. (condition-case nil
  13265. (outline-forward-same-level 1)
  13266. (error (goto-char end)))))
  13267. (when swallowp
  13268. (message
  13269. "Yanked text not folded because that would swallow text"))
  13270. (goto-char end)
  13271. (skip-chars-forward " \t\n\r")
  13272. (beginning-of-line 1)
  13273. (push-mark beg 'nomsg)))
  13274. ((and subtreep org-yank-adjusted-subtrees)
  13275. (let ((beg (point-at-bol)))
  13276. (org-paste-subtree nil nil 'for-yank)
  13277. (push-mark beg 'nomsg)))
  13278. (t
  13279. (call-interactively 'yank))))))
  13280. (defun org-yank-folding-would-swallow-text (beg end)
  13281. "Would hide-subtree at BEG swallow any text after END?"
  13282. (let (level)
  13283. (save-excursion
  13284. (goto-char beg)
  13285. (when (or (looking-at outline-regexp)
  13286. (re-search-forward (concat "^" outline-regexp) end t))
  13287. (setq level (org-outline-level)))
  13288. (goto-char end)
  13289. (skip-chars-forward " \t\r\n\v\f")
  13290. (if (or (eobp)
  13291. (and (bolp) (looking-at org-outline-regexp)
  13292. (<= (org-outline-level) level)))
  13293. nil ; Nothing would be swallowed
  13294. t)))) ; something would swallow
  13295. (define-key org-mode-map "\C-y" 'org-yank)
  13296. (defun org-invisible-p ()
  13297. "Check if point is at a character currently not visible."
  13298. ;; Early versions of noutline don't have `outline-invisible-p'.
  13299. (if (fboundp 'outline-invisible-p)
  13300. (outline-invisible-p)
  13301. (get-char-property (point) 'invisible)))
  13302. (defun org-invisible-p2 ()
  13303. "Check if point is at a character currently not visible."
  13304. (save-excursion
  13305. (if (and (eolp) (not (bobp))) (backward-char 1))
  13306. ;; Early versions of noutline don't have `outline-invisible-p'.
  13307. (if (fboundp 'outline-invisible-p)
  13308. (outline-invisible-p)
  13309. (get-char-property (point) 'invisible))))
  13310. (defun org-back-to-heading (&optional invisible-ok)
  13311. "Call `outline-back-to-heading', but provide a better error message."
  13312. (condition-case nil
  13313. (outline-back-to-heading invisible-ok)
  13314. (error (error "Before first headline at position %d in buffer %s"
  13315. (point) (current-buffer)))))
  13316. (defun org-before-first-heading-p ()
  13317. "Before first heading?"
  13318. (save-excursion
  13319. (null (re-search-backward "^\\*+ " nil t))))
  13320. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13321. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13322. (defun org-at-heading-or-item-p ()
  13323. (or (org-on-heading-p) (org-at-item-p)))
  13324. (defun org-on-target-p ()
  13325. (or (org-in-regexp org-radio-target-regexp)
  13326. (org-in-regexp org-target-regexp)))
  13327. (defun org-up-heading-all (arg)
  13328. "Move to the heading line of which the present line is a subheading.
  13329. This function considers both visible and invisible heading lines.
  13330. With argument, move up ARG levels."
  13331. (if (fboundp 'outline-up-heading-all)
  13332. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13333. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13334. (defun org-up-heading-safe ()
  13335. "Move to the heading line of which the present line is a subheading.
  13336. This version will not throw an error. It will return the level of the
  13337. headline found, or nil if no higher level is found."
  13338. (let (start-level re)
  13339. (org-back-to-heading t)
  13340. (setq start-level (funcall outline-level))
  13341. (if (equal start-level 1)
  13342. nil
  13343. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13344. (if (re-search-backward re nil t)
  13345. (funcall outline-level)))))
  13346. (defun org-first-sibling-p ()
  13347. "Is this heading the first child of its parents?"
  13348. (interactive)
  13349. (let ((re (concat "^" outline-regexp))
  13350. level l)
  13351. (unless (org-at-heading-p t)
  13352. (error "Not at a heading"))
  13353. (setq level (funcall outline-level))
  13354. (save-excursion
  13355. (if (not (re-search-backward re nil t))
  13356. t
  13357. (setq l (funcall outline-level))
  13358. (< l level)))))
  13359. (defun org-goto-sibling (&optional previous)
  13360. "Goto the next sibling, even if it is invisible.
  13361. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13362. when a sibling was found. When none is found, return nil and don't
  13363. move point."
  13364. (let ((fun (if previous 're-search-backward 're-search-forward))
  13365. (pos (point))
  13366. (re (concat "^" outline-regexp))
  13367. level l)
  13368. (when (condition-case nil (org-back-to-heading t) (error nil))
  13369. (setq level (funcall outline-level))
  13370. (catch 'exit
  13371. (or previous (forward-char 1))
  13372. (while (funcall fun re nil t)
  13373. (setq l (funcall outline-level))
  13374. (when (< l level) (goto-char pos) (throw 'exit nil))
  13375. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13376. (goto-char pos)
  13377. nil))))
  13378. (defun org-show-siblings ()
  13379. "Show all siblings of the current headline."
  13380. (save-excursion
  13381. (while (org-goto-sibling) (org-flag-heading nil)))
  13382. (save-excursion
  13383. (while (org-goto-sibling 'previous)
  13384. (org-flag-heading nil))))
  13385. (defun org-show-hidden-entry ()
  13386. "Show an entry where even the heading is hidden."
  13387. (save-excursion
  13388. (org-show-entry)))
  13389. (defun org-flag-heading (flag &optional entry)
  13390. "Flag the current heading. FLAG non-nil means make invisible.
  13391. When ENTRY is non-nil, show the entire entry."
  13392. (save-excursion
  13393. (org-back-to-heading t)
  13394. ;; Check if we should show the entire entry
  13395. (if entry
  13396. (progn
  13397. (org-show-entry)
  13398. (save-excursion
  13399. (and (outline-next-heading)
  13400. (org-flag-heading nil))))
  13401. (outline-flag-region (max (point-min) (1- (point)))
  13402. (save-excursion (outline-end-of-heading) (point))
  13403. flag))))
  13404. (defun org-forward-same-level (arg)
  13405. "Move forward to the ARG'th subheading at same level as this one.
  13406. Stop at the first and last subheadings of a superior heading.
  13407. This is like outline-forward-same-level, but invisible headings are ok."
  13408. (interactive "p")
  13409. (org-back-to-heading t)
  13410. (while (> arg 0)
  13411. (let ((point-to-move-to (save-excursion
  13412. (org-get-next-sibling))))
  13413. (if point-to-move-to
  13414. (progn
  13415. (goto-char point-to-move-to)
  13416. (setq arg (1- arg)))
  13417. (progn
  13418. (setq arg 0)
  13419. (error "No following same-level heading"))))))
  13420. (defun org-get-next-sibling ()
  13421. "Move to next heading of the same level, and return point.
  13422. If there is no such heading, return nil.
  13423. This is like outline-next-sibling, but invisible headings are ok."
  13424. (let ((level (funcall outline-level)))
  13425. (outline-next-heading)
  13426. (while (and (not (eobp)) (> (funcall outline-level) level))
  13427. (outline-next-heading))
  13428. (if (or (eobp) (< (funcall outline-level) level))
  13429. nil
  13430. (point))))
  13431. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13432. ;; This is an exact copy of the original function, but it uses
  13433. ;; `org-back-to-heading', to make it work also in invisible
  13434. ;; trees. And is uses an invisible-OK argument.
  13435. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13436. (org-back-to-heading invisible-OK)
  13437. (let ((first t)
  13438. (level (funcall outline-level)))
  13439. (while (and (not (eobp))
  13440. (or first (> (funcall outline-level) level)))
  13441. (setq first nil)
  13442. (outline-next-heading))
  13443. (unless to-heading
  13444. (if (memq (preceding-char) '(?\n ?\^M))
  13445. (progn
  13446. ;; Go to end of line before heading
  13447. (forward-char -1)
  13448. (if (memq (preceding-char) '(?\n ?\^M))
  13449. ;; leave blank line before heading
  13450. (forward-char -1))))))
  13451. (point))
  13452. (defun org-show-subtree ()
  13453. "Show everything after this heading at deeper levels."
  13454. (outline-flag-region
  13455. (point)
  13456. (save-excursion
  13457. (outline-end-of-subtree) (outline-next-heading) (point))
  13458. nil))
  13459. (defun org-show-entry ()
  13460. "Show the body directly following this heading.
  13461. Show the heading too, if it is currently invisible."
  13462. (interactive)
  13463. (save-excursion
  13464. (condition-case nil
  13465. (progn
  13466. (org-back-to-heading t)
  13467. (outline-flag-region
  13468. (max (point-min) (1- (point)))
  13469. (save-excursion
  13470. (re-search-forward
  13471. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13472. (or (match-beginning 1) (point-max)))
  13473. nil))
  13474. (error nil))))
  13475. (defun org-make-options-regexp (kwds)
  13476. "Make a regular expression for keyword lines."
  13477. (concat
  13478. "^"
  13479. "#?[ \t]*\\+\\("
  13480. (mapconcat 'regexp-quote kwds "\\|")
  13481. "\\):[ \t]*"
  13482. "\\(.+\\)"))
  13483. ;; Make isearch reveal the necessary context
  13484. (defun org-isearch-end ()
  13485. "Reveal context after isearch exits."
  13486. (when isearch-success ; only if search was successful
  13487. (if (featurep 'xemacs)
  13488. ;; Under XEmacs, the hook is run in the correct place,
  13489. ;; we directly show the context.
  13490. (org-show-context 'isearch)
  13491. ;; In Emacs the hook runs *before* restoring the overlays.
  13492. ;; So we have to use a one-time post-command-hook to do this.
  13493. ;; (Emacs 22 has a special variable, see function `org-mode')
  13494. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13495. isearch-mode-end-hook-quit)
  13496. ;; Only when the isearch was not quitted.
  13497. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13498. 'append 'local)))))
  13499. (defun org-isearch-post-command ()
  13500. "Remove self from hook, and show context."
  13501. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13502. (org-show-context 'isearch))
  13503. ;;;; Integration with and fixes for other packages
  13504. ;;; Imenu support
  13505. (defvar org-imenu-markers nil
  13506. "All markers currently used by Imenu.")
  13507. (make-variable-buffer-local 'org-imenu-markers)
  13508. (defun org-imenu-new-marker (&optional pos)
  13509. "Return a new marker for use by Imenu, and remember the marker."
  13510. (let ((m (make-marker)))
  13511. (move-marker m (or pos (point)))
  13512. (push m org-imenu-markers)
  13513. m))
  13514. (defun org-imenu-get-tree ()
  13515. "Produce the index for Imenu."
  13516. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13517. (setq org-imenu-markers nil)
  13518. (let* ((n org-imenu-depth)
  13519. (re (concat "^" outline-regexp))
  13520. (subs (make-vector (1+ n) nil))
  13521. (last-level 0)
  13522. m tree level head)
  13523. (save-excursion
  13524. (save-restriction
  13525. (widen)
  13526. (goto-char (point-max))
  13527. (while (re-search-backward re nil t)
  13528. (setq level (org-reduced-level (funcall outline-level)))
  13529. (when (<= level n)
  13530. (looking-at org-complex-heading-regexp)
  13531. (setq head (org-link-display-format
  13532. (org-match-string-no-properties 4))
  13533. m (org-imenu-new-marker))
  13534. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13535. (if (>= level last-level)
  13536. (push (cons head m) (aref subs level))
  13537. (push (cons head (aref subs (1+ level))) (aref subs level))
  13538. (loop for i from (1+ level) to n do (aset subs i nil)))
  13539. (setq last-level level)))))
  13540. (aref subs 1)))
  13541. (eval-after-load "imenu"
  13542. '(progn
  13543. (add-hook 'imenu-after-jump-hook
  13544. (lambda ()
  13545. (if (eq major-mode 'org-mode)
  13546. (org-show-context 'org-goto))))))
  13547. (defun org-link-display-format (link)
  13548. "Replace a link with either the description, or the link target
  13549. if no description is present"
  13550. (save-match-data
  13551. (if (string-match org-bracket-link-analytic-regexp link)
  13552. (replace-match (or (match-string 5 link)
  13553. (concat (match-string 1 link)
  13554. (match-string 3 link)))
  13555. nil nil link)
  13556. link)))
  13557. ;; Speedbar support
  13558. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13559. "Overlay marking the agenda restriction line in speedbar.")
  13560. (org-overlay-put org-speedbar-restriction-lock-overlay
  13561. 'face 'org-agenda-restriction-lock)
  13562. (org-overlay-put org-speedbar-restriction-lock-overlay
  13563. 'help-echo "Agendas are currently limited to this item.")
  13564. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13565. (defun org-speedbar-set-agenda-restriction ()
  13566. "Restrict future agenda commands to the location at point in speedbar.
  13567. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13568. (interactive)
  13569. (require 'org-agenda)
  13570. (let (p m tp np dir txt w)
  13571. (cond
  13572. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13573. 'org-imenu t))
  13574. (setq m (get-text-property p 'org-imenu-marker))
  13575. (save-excursion
  13576. (save-restriction
  13577. (set-buffer (marker-buffer m))
  13578. (goto-char m)
  13579. (org-agenda-set-restriction-lock 'subtree))))
  13580. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13581. 'speedbar-function 'speedbar-find-file))
  13582. (setq tp (previous-single-property-change
  13583. (1+ p) 'speedbar-function)
  13584. np (next-single-property-change
  13585. tp 'speedbar-function)
  13586. dir (speedbar-line-directory)
  13587. txt (buffer-substring-no-properties (or tp (point-min))
  13588. (or np (point-max))))
  13589. (save-excursion
  13590. (save-restriction
  13591. (set-buffer (find-file-noselect
  13592. (let ((default-directory dir))
  13593. (expand-file-name txt))))
  13594. (unless (org-mode-p)
  13595. (error "Cannot restrict to non-Org-mode file"))
  13596. (org-agenda-set-restriction-lock 'file))))
  13597. (t (error "Don't know how to restrict Org-mode's agenda")))
  13598. (org-move-overlay org-speedbar-restriction-lock-overlay
  13599. (point-at-bol) (point-at-eol))
  13600. (setq current-prefix-arg nil)
  13601. (org-agenda-maybe-redo)))
  13602. (eval-after-load "speedbar"
  13603. '(progn
  13604. (speedbar-add-supported-extension ".org")
  13605. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13606. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13607. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13608. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13609. (add-hook 'speedbar-visiting-tag-hook
  13610. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  13611. ;;; Fixes and Hacks for problems with other packages
  13612. ;; Make flyspell not check words in links, to not mess up our keymap
  13613. (defun org-mode-flyspell-verify ()
  13614. "Don't let flyspell put overlays at active buttons."
  13615. (not (get-text-property (point) 'keymap)))
  13616. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13617. (eval-after-load "bookmark"
  13618. '(if (boundp 'bookmark-after-jump-hook)
  13619. ;; We can use the hook
  13620. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13621. ;; Hook not available, use advice
  13622. (defadvice bookmark-jump (after org-make-visible activate)
  13623. "Make the position visible."
  13624. (org-bookmark-jump-unhide))))
  13625. ;; Make sure saveplace show the location if it was hidden
  13626. (eval-after-load "saveplace"
  13627. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13628. "Make the position visible."
  13629. (org-bookmark-jump-unhide)))
  13630. (defun org-bookmark-jump-unhide ()
  13631. "Unhide the current position, to show the bookmark location."
  13632. (and (org-mode-p)
  13633. (or (org-invisible-p)
  13634. (save-excursion (goto-char (max (point-min) (1- (point))))
  13635. (org-invisible-p)))
  13636. (org-show-context 'bookmark-jump)))
  13637. ;; Make session.el ignore our circular variable
  13638. (eval-after-load "session"
  13639. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13640. ;;;; Experimental code
  13641. (defun org-closed-in-range ()
  13642. "Sparse tree of items closed in a certain time range.
  13643. Still experimental, may disappear in the future."
  13644. (interactive)
  13645. ;; Get the time interval from the user.
  13646. (let* ((time1 (time-to-seconds
  13647. (org-read-date nil 'to-time nil "Starting date: ")))
  13648. (time2 (time-to-seconds
  13649. (org-read-date nil 'to-time nil "End date:")))
  13650. ;; callback function
  13651. (callback (lambda ()
  13652. (let ((time
  13653. (time-to-seconds
  13654. (apply 'encode-time
  13655. (org-parse-time-string
  13656. (match-string 1))))))
  13657. ;; check if time in interval
  13658. (and (>= time time1) (<= time time2))))))
  13659. ;; make tree, check each match with the callback
  13660. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13661. ;;;; Finish up
  13662. (provide 'org)
  13663. (run-hooks 'org-load-hook)
  13664. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13665. ;;; org.el ends here