org.el 518 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130
  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.03
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. ;;;; Customization variables
  84. ;;; Version
  85. (defconst org-version "6.03"
  86. "The version number of the file org.el.")
  87. (defun org-version (&optional here)
  88. "Show the org-mode version in the echo area.
  89. With prefix arg HERE, insert it at point."
  90. (interactive "P")
  91. (let ((version (format "Org-mode version %s" org-version)))
  92. (message version)
  93. (if here
  94. (insert version))))
  95. ;;; Compatibility constants
  96. ;;; The custom variables
  97. (defgroup org nil
  98. "Outline-based notes management and organizer."
  99. :tag "Org"
  100. :group 'outlines
  101. :group 'hypermedia
  102. :group 'calendar)
  103. (defcustom org-load-hook nil
  104. "Hook that is run after org.el has been loaded."
  105. :group 'org
  106. :type 'hook)
  107. (defvar org-modules) ; defined below
  108. (defvar org-modules-loaded nil
  109. "Have the modules been loaded already?")
  110. (defun org-load-modules-maybe (&optional force)
  111. "Load all extensions listed in `org-default-extensions'."
  112. (when (or force (not org-modules-loaded))
  113. (mapc (lambda (ext)
  114. (condition-case nil (require ext)
  115. (error (message "Problems while trying to load feature `%s'" ext))))
  116. org-modules)
  117. (setq org-modules-loaded t)))
  118. (defun org-set-modules (var value)
  119. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  120. (set var value)
  121. (when (featurep 'org)
  122. (org-load-modules-maybe 'force)))
  123. (when (org-bound-and-true-p org-modules)
  124. (let ((a (member 'org-infojs org-modules)))
  125. (and a (setcar a 'org-jsinfo))))
  126. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-wl)
  127. "Modules that should always be loaded together with org.el.
  128. If a description starts with <C>, the file is not part of Emacs
  129. and loading it will require that you have downloaded and properly installed
  130. the org-mode distribution.
  131. You can also use this system to load external packages (i.e. neither Org
  132. core modules, not modules from the CONTRIB directory). Just add symbols
  133. to the end of the list. If the package is called org-xyz.el, then you need
  134. to add the symbol `xyz', and the package must have a call to
  135. (provide 'org-xyz)"
  136. :group 'org
  137. :set 'org-set-modules
  138. :type
  139. '(set :greedy t
  140. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  141. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  142. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  143. (const :tag " id: Global id's for identifying entries" org-id)
  144. (const :tag " info: Links to Info nodes" org-info)
  145. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  146. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  147. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  148. (const :tag " mew Links to Mew folders/messages" org-mew)
  149. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  150. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  151. (const :tag " vm: Links to VM folders/messages" org-vm)
  152. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  153. (const :tag " mouse: Additional mouse support" org-mouse)
  154. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  155. (const :tag "C annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
  156. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  157. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  158. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  159. (const :tag "C eval: Include command output as text" org-eval)
  160. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  161. (const :tag "C id: Global id's for identifying entries" org-id)
  162. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  163. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  164. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  165. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  166. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  167. (const :tag "C registry: A registry for Org links" org-registry)
  168. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  169. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  170. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  171. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  172. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  173. (defgroup org-startup nil
  174. "Options concerning startup of Org-mode."
  175. :tag "Org Startup"
  176. :group 'org)
  177. (defcustom org-startup-folded t
  178. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  179. This can also be configured on a per-file basis by adding one of
  180. the following lines anywhere in the buffer:
  181. #+STARTUP: fold
  182. #+STARTUP: nofold
  183. #+STARTUP: content"
  184. :group 'org-startup
  185. :type '(choice
  186. (const :tag "nofold: show all" nil)
  187. (const :tag "fold: overview" t)
  188. (const :tag "content: all headlines" content)))
  189. (defcustom org-startup-truncated t
  190. "Non-nil means, entering Org-mode will set `truncate-lines'.
  191. This is useful since some lines containing links can be very long and
  192. uninteresting. Also tables look terrible when wrapped."
  193. :group 'org-startup
  194. :type 'boolean)
  195. (defcustom org-startup-align-all-tables nil
  196. "Non-nil means, align all tables when visiting a file.
  197. This is useful when the column width in tables is forced with <N> cookies
  198. in table fields. Such tables will look correct only after the first re-align.
  199. This can also be configured on a per-file basis by adding one of
  200. the following lines anywhere in the buffer:
  201. #+STARTUP: align
  202. #+STARTUP: noalign"
  203. :group 'org-startup
  204. :type 'boolean)
  205. (defcustom org-insert-mode-line-in-empty-file nil
  206. "Non-nil means insert the first line setting Org-mode in empty files.
  207. When the function `org-mode' is called interactively in an empty file, this
  208. normally means that the file name does not automatically trigger Org-mode.
  209. To ensure that the file will always be in Org-mode in the future, a
  210. line enforcing Org-mode will be inserted into the buffer, if this option
  211. has been set."
  212. :group 'org-startup
  213. :type 'boolean)
  214. (defcustom org-replace-disputed-keys nil
  215. "Non-nil means use alternative key bindings for some keys.
  216. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  217. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  218. If you want to use Org-mode together with one of these other modes,
  219. or more generally if you would like to move some Org-mode commands to
  220. other keys, set this variable and configure the keys with the variable
  221. `org-disputed-keys'.
  222. This option is only relevant at load-time of Org-mode, and must be set
  223. *before* org.el is loaded. Changing it requires a restart of Emacs to
  224. become effective."
  225. :group 'org-startup
  226. :type 'boolean)
  227. (if (fboundp 'defvaralias)
  228. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  229. (defcustom org-disputed-keys
  230. '(([(shift up)] . [(meta p)])
  231. ([(shift down)] . [(meta n)])
  232. ([(shift left)] . [(meta -)])
  233. ([(shift right)] . [(meta +)])
  234. ([(control shift right)] . [(meta shift +)])
  235. ([(control shift left)] . [(meta shift -)]))
  236. "Keys for which Org-mode and other modes compete.
  237. This is an alist, cars are the default keys, second element specifies
  238. the alternative to use when `org-replace-disputed-keys' is t.
  239. Keys can be specified in any syntax supported by `define-key'.
  240. The value of this option takes effect only at Org-mode's startup,
  241. therefore you'll have to restart Emacs to apply it after changing."
  242. :group 'org-startup
  243. :type 'alist)
  244. (defun org-key (key)
  245. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  246. Or return the original if not disputed."
  247. (if org-replace-disputed-keys
  248. (let* ((nkey (key-description key))
  249. (x (org-find-if (lambda (x)
  250. (equal (key-description (car x)) nkey))
  251. org-disputed-keys)))
  252. (if x (cdr x) key))
  253. key))
  254. (defun org-find-if (predicate seq)
  255. (catch 'exit
  256. (while seq
  257. (if (funcall predicate (car seq))
  258. (throw 'exit (car seq))
  259. (pop seq)))))
  260. (defun org-defkey (keymap key def)
  261. "Define a key, possibly translated, as returned by `org-key'."
  262. (define-key keymap (org-key key) def))
  263. (defcustom org-ellipsis nil
  264. "The ellipsis to use in the Org-mode outline.
  265. When nil, just use the standard three dots. When a string, use that instead,
  266. When a face, use the standart 3 dots, but with the specified face.
  267. The change affects only Org-mode (which will then use its own display table).
  268. Changing this requires executing `M-x org-mode' in a buffer to become
  269. effective."
  270. :group 'org-startup
  271. :type '(choice (const :tag "Default" nil)
  272. (face :tag "Face" :value org-warning)
  273. (string :tag "String" :value "...#")))
  274. (defvar org-display-table nil
  275. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  276. (defgroup org-keywords nil
  277. "Keywords in Org-mode."
  278. :tag "Org Keywords"
  279. :group 'org)
  280. (defcustom org-deadline-string "DEADLINE:"
  281. "String to mark deadline entries.
  282. A deadline is this string, followed by a time stamp. Should be a word,
  283. terminated by a colon. You can insert a schedule keyword and
  284. a timestamp with \\[org-deadline].
  285. Changes become only effective after restarting Emacs."
  286. :group 'org-keywords
  287. :type 'string)
  288. (defcustom org-scheduled-string "SCHEDULED:"
  289. "String to mark scheduled TODO entries.
  290. A schedule is this string, followed by a time stamp. Should be a word,
  291. terminated by a colon. You can insert a schedule keyword and
  292. a timestamp with \\[org-schedule].
  293. Changes become only effective after restarting Emacs."
  294. :group 'org-keywords
  295. :type 'string)
  296. (defcustom org-closed-string "CLOSED:"
  297. "String used as the prefix for timestamps logging closing a TODO entry."
  298. :group 'org-keywords
  299. :type 'string)
  300. (defcustom org-clock-string "CLOCK:"
  301. "String used as prefix for timestamps clocking work hours on an item."
  302. :group 'org-keywords
  303. :type 'string)
  304. (defcustom org-comment-string "COMMENT"
  305. "Entries starting with this keyword will never be exported.
  306. An entry can be toggled between COMMENT and normal with
  307. \\[org-toggle-comment].
  308. Changes become only effective after restarting Emacs."
  309. :group 'org-keywords
  310. :type 'string)
  311. (defcustom org-quote-string "QUOTE"
  312. "Entries starting with this keyword will be exported in fixed-width font.
  313. Quoting applies only to the text in the entry following the headline, and does
  314. not extend beyond the next headline, even if that is lower level.
  315. An entry can be toggled between QUOTE and normal with
  316. \\[org-toggle-fixed-width-section]."
  317. :group 'org-keywords
  318. :type 'string)
  319. (defconst org-repeat-re
  320. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  321. "Regular expression for specifying repeated events.
  322. After a match, group 1 contains the repeat expression.")
  323. (defgroup org-structure nil
  324. "Options concerning the general structure of Org-mode files."
  325. :tag "Org Structure"
  326. :group 'org)
  327. (defgroup org-reveal-location nil
  328. "Options about how to make context of a location visible."
  329. :tag "Org Reveal Location"
  330. :group 'org-structure)
  331. (defconst org-context-choice
  332. '(choice
  333. (const :tag "Always" t)
  334. (const :tag "Never" nil)
  335. (repeat :greedy t :tag "Individual contexts"
  336. (cons
  337. (choice :tag "Context"
  338. (const agenda)
  339. (const org-goto)
  340. (const occur-tree)
  341. (const tags-tree)
  342. (const link-search)
  343. (const mark-goto)
  344. (const bookmark-jump)
  345. (const isearch)
  346. (const default))
  347. (boolean))))
  348. "Contexts for the reveal options.")
  349. (defcustom org-show-hierarchy-above '((default . t))
  350. "Non-nil means, show full hierarchy when revealing a location.
  351. Org-mode often shows locations in an org-mode file which might have
  352. been invisible before. When this is set, the hierarchy of headings
  353. above the exposed location is shown.
  354. Turning this off for example for sparse trees makes them very compact.
  355. Instead of t, this can also be an alist specifying this option for different
  356. contexts. Valid contexts are
  357. agenda when exposing an entry from the agenda
  358. org-goto when using the command `org-goto' on key C-c C-j
  359. occur-tree when using the command `org-occur' on key C-c /
  360. tags-tree when constructing a sparse tree based on tags matches
  361. link-search when exposing search matches associated with a link
  362. mark-goto when exposing the jump goal of a mark
  363. bookmark-jump when exposing a bookmark location
  364. isearch when exiting from an incremental search
  365. default default for all contexts not set explicitly"
  366. :group 'org-reveal-location
  367. :type org-context-choice)
  368. (defcustom org-show-following-heading '((default . nil))
  369. "Non-nil means, show following heading when revealing a location.
  370. Org-mode often shows locations in an org-mode file which might have
  371. been invisible before. When this is set, the heading following the
  372. match is shown.
  373. Turning this off for example for sparse trees makes them very compact,
  374. but makes it harder to edit the location of the match. In such a case,
  375. use the command \\[org-reveal] to show more context.
  376. Instead of t, this can also be an alist specifying this option for different
  377. contexts. See `org-show-hierarchy-above' for valid contexts."
  378. :group 'org-reveal-location
  379. :type org-context-choice)
  380. (defcustom org-show-siblings '((default . nil) (isearch t))
  381. "Non-nil means, show all sibling heading when revealing a location.
  382. Org-mode often shows locations in an org-mode file which might have
  383. been invisible before. When this is set, the sibling of the current entry
  384. heading are all made visible. If `org-show-hierarchy-above' is t,
  385. the same happens on each level of the hierarchy above the current entry.
  386. By default this is on for the isearch context, off for all other contexts.
  387. Turning this off for example for sparse trees makes them very compact,
  388. but makes it harder to edit the location of the match. In such a case,
  389. use the command \\[org-reveal] to show more context.
  390. Instead of t, this can also be an alist specifying this option for different
  391. contexts. See `org-show-hierarchy-above' for valid contexts."
  392. :group 'org-reveal-location
  393. :type org-context-choice)
  394. (defcustom org-show-entry-below '((default . nil))
  395. "Non-nil means, show the entry below a headline when revealing a location.
  396. Org-mode often shows locations in an org-mode file which might have
  397. been invisible before. When this is set, the text below the headline that is
  398. exposed is also shown.
  399. By default this is off for all contexts.
  400. Instead of t, this can also be an alist specifying this option for different
  401. contexts. See `org-show-hierarchy-above' for valid contexts."
  402. :group 'org-reveal-location
  403. :type org-context-choice)
  404. (defcustom org-indirect-buffer-display 'other-window
  405. "How should indirect tree buffers be displayed?
  406. This applies to indirect buffers created with the commands
  407. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  408. Valid values are:
  409. current-window Display in the current window
  410. other-window Just display in another window.
  411. dedicated-frame Create one new frame, and re-use it each time.
  412. new-frame Make a new frame each time. Note that in this case
  413. previously-made indirect buffers are kept, and you need to
  414. kill these buffers yourself."
  415. :group 'org-structure
  416. :group 'org-agenda-windows
  417. :type '(choice
  418. (const :tag "In current window" current-window)
  419. (const :tag "In current frame, other window" other-window)
  420. (const :tag "Each time a new frame" new-frame)
  421. (const :tag "One dedicated frame" dedicated-frame)))
  422. (defgroup org-cycle nil
  423. "Options concerning visibility cycling in Org-mode."
  424. :tag "Org Cycle"
  425. :group 'org-structure)
  426. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  427. "Names of drawers. Drawers are not opened by cycling on the headline above.
  428. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  429. this:
  430. :DRAWERNAME:
  431. .....
  432. :END:
  433. The drawer \"PROPERTIES\" is special for capturing properties through
  434. the property API.
  435. Drawers can be defined on the per-file basis with a line like:
  436. #+DRAWERS: HIDDEN STATE PROPERTIES"
  437. :group 'org-structure
  438. :type '(repeat (string :tag "Drawer Name")))
  439. (defcustom org-cycle-global-at-bob nil
  440. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  441. This makes it possible to do global cycling without having to use S-TAB or
  442. C-u TAB. For this special case to work, the first line of the buffer
  443. must not be a headline - it may be empty ot some other text. When used in
  444. this way, `org-cycle-hook' is disables temporarily, to make sure the
  445. cursor stays at the beginning of the buffer.
  446. When this option is nil, don't do anything special at the beginning
  447. of the buffer."
  448. :group 'org-cycle
  449. :type 'boolean)
  450. (defcustom org-cycle-emulate-tab t
  451. "Where should `org-cycle' emulate TAB.
  452. nil Never
  453. white Only in completely white lines
  454. whitestart Only at the beginning of lines, before the first non-white char
  455. t Everywhere except in headlines
  456. exc-hl-bol Everywhere except at the start of a headline
  457. If TAB is used in a place where it does not emulate TAB, the current subtree
  458. visibility is cycled."
  459. :group 'org-cycle
  460. :type '(choice (const :tag "Never" nil)
  461. (const :tag "Only in completely white lines" white)
  462. (const :tag "Before first char in a line" whitestart)
  463. (const :tag "Everywhere except in headlines" t)
  464. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  465. ))
  466. (defcustom org-cycle-separator-lines 2
  467. "Number of empty lines needed to keep an empty line between collapsed trees.
  468. If you leave an empty line between the end of a subtree and the following
  469. headline, this empty line is hidden when the subtree is folded.
  470. Org-mode will leave (exactly) one empty line visible if the number of
  471. empty lines is equal or larger to the number given in this variable.
  472. So the default 2 means, at least 2 empty lines after the end of a subtree
  473. are needed to produce free space between a collapsed subtree and the
  474. following headline.
  475. Special case: when 0, never leave empty lines in collapsed view."
  476. :group 'org-cycle
  477. :type 'integer)
  478. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  479. org-cycle-hide-drawers
  480. org-cycle-show-empty-lines
  481. org-optimize-window-after-visibility-change)
  482. "Hook that is run after `org-cycle' has changed the buffer visibility.
  483. The function(s) in this hook must accept a single argument which indicates
  484. the new state that was set by the most recent `org-cycle' command. The
  485. argument is a symbol. After a global state change, it can have the values
  486. `overview', `content', or `all'. After a local state change, it can have
  487. the values `folded', `children', or `subtree'."
  488. :group 'org-cycle
  489. :type 'hook)
  490. (defgroup org-edit-structure nil
  491. "Options concerning structure editing in Org-mode."
  492. :tag "Org Edit Structure"
  493. :group 'org-structure)
  494. (defcustom org-odd-levels-only nil
  495. "Non-nil means, skip even levels and only use odd levels for the outline.
  496. This has the effect that two stars are being added/taken away in
  497. promotion/demotion commands. It also influences how levels are
  498. handled by the exporters.
  499. Changing it requires restart of `font-lock-mode' to become effective
  500. for fontification also in regions already fontified.
  501. You may also set this on a per-file basis by adding one of the following
  502. lines to the buffer:
  503. #+STARTUP: odd
  504. #+STARTUP: oddeven"
  505. :group 'org-edit-structure
  506. :group 'org-font-lock
  507. :type 'boolean)
  508. (defcustom org-adapt-indentation t
  509. "Non-nil means, adapt indentation when promoting and demoting.
  510. When this is set and the *entire* text in an entry is indented, the
  511. indentation is increased by one space in a demotion command, and
  512. decreased by one in a promotion command. If any line in the entry
  513. body starts at column 0, indentation is not changed at all."
  514. :group 'org-edit-structure
  515. :type 'boolean)
  516. (defcustom org-special-ctrl-a/e nil
  517. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  518. When t, `C-a' will bring back the cursor to the beginning of the
  519. headline text, i.e. after the stars and after a possible TODO keyword.
  520. In an item, this will be the position after the bullet.
  521. When the cursor is already at that position, another `C-a' will bring
  522. it to the beginning of the line.
  523. `C-e' will jump to the end of the headline, ignoring the presence of tags
  524. in the headline. A second `C-e' will then jump to the true end of the
  525. line, after any tags.
  526. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  527. and only a directly following, identical keypress will bring the cursor
  528. to the special positions."
  529. :group 'org-edit-structure
  530. :type '(choice
  531. (const :tag "off" nil)
  532. (const :tag "after bullet first" t)
  533. (const :tag "border first" reversed)))
  534. (if (fboundp 'defvaralias)
  535. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  536. (defcustom org-special-ctrl-k nil
  537. "Non-nil means `C-k' will behave specially in headlines.
  538. When nil, `C-k' will call the default `kill-line' command.
  539. When t, the following will happen while the cursor is in the headline:
  540. - When the cursor is at the beginning of a headline, kill the entire
  541. line and possible the folded subtree below the line.
  542. - When in the middle of the headline text, kill the headline up to the tags.
  543. - When after the headline text, kill the tags."
  544. :group 'org-edit-structure
  545. :type 'boolean)
  546. (defcustom org-M-RET-may-split-line '((default . t))
  547. "Non-nil means, M-RET will split the line at the cursor position.
  548. When nil, it will go to the end of the line before making a
  549. new line.
  550. You may also set this option in a different way for different
  551. contexts. Valid contexts are:
  552. headline when creating a new headline
  553. item when creating a new item
  554. table in a table field
  555. default the value to be used for all contexts not explicitly
  556. customized"
  557. :group 'org-structure
  558. :group 'org-table
  559. :type '(choice
  560. (const :tag "Always" t)
  561. (const :tag "Never" nil)
  562. (repeat :greedy t :tag "Individual contexts"
  563. (cons
  564. (choice :tag "Context"
  565. (const headline)
  566. (const item)
  567. (const table)
  568. (const default))
  569. (boolean)))))
  570. (defcustom org-blank-before-new-entry '((heading . nil)
  571. (plain-list-item . nil))
  572. "Should `org-insert-heading' leave a blank line before new heading/item?
  573. The value is an alist, with `heading' and `plain-list-item' as car,
  574. and a boolean flag as cdr."
  575. :group 'org-edit-structure
  576. :type '(list
  577. (cons (const heading) (boolean))
  578. (cons (const plain-list-item) (boolean))))
  579. (defcustom org-insert-heading-hook nil
  580. "Hook being run after inserting a new heading."
  581. :group 'org-edit-structure
  582. :type 'hook)
  583. (defcustom org-enable-fixed-width-editor t
  584. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  585. This currently only means, they are never auto-wrapped.
  586. When nil, such lines will be treated like ordinary lines.
  587. See also the QUOTE keyword."
  588. :group 'org-edit-structure
  589. :type 'boolean)
  590. (defcustom org-goto-auto-isearch t
  591. "Non-nil means, typing characters in org-goto starts incremental search."
  592. :group 'org-edit-structure
  593. :type 'boolean)
  594. (defgroup org-sparse-trees nil
  595. "Options concerning sparse trees in Org-mode."
  596. :tag "Org Sparse Trees"
  597. :group 'org-structure)
  598. (defcustom org-highlight-sparse-tree-matches t
  599. "Non-nil means, highlight all matches that define a sparse tree.
  600. The highlights will automatically disappear the next time the buffer is
  601. changed by an edit command."
  602. :group 'org-sparse-trees
  603. :type 'boolean)
  604. (defcustom org-remove-highlights-with-change t
  605. "Non-nil means, any change to the buffer will remove temporary highlights.
  606. Such highlights are created by `org-occur' and `org-clock-display'.
  607. When nil, `C-c C-c needs to be used to get rid of the highlights.
  608. The highlights created by `org-preview-latex-fragment' always need
  609. `C-c C-c' to be removed."
  610. :group 'org-sparse-trees
  611. :group 'org-time
  612. :type 'boolean)
  613. (defcustom org-occur-hook '(org-first-headline-recenter)
  614. "Hook that is run after `org-occur' has constructed a sparse tree.
  615. This can be used to recenter the window to show as much of the structure
  616. as possible."
  617. :group 'org-sparse-trees
  618. :type 'hook)
  619. (defgroup org-plain-lists nil
  620. "Options concerning plain lists in Org-mode."
  621. :tag "Org Plain lists"
  622. :group 'org-structure)
  623. (defcustom org-cycle-include-plain-lists nil
  624. "Non-nil means, include plain lists into visibility cycling.
  625. This means that during cycling, plain list items will *temporarily* be
  626. interpreted as outline headlines with a level given by 1000+i where i is the
  627. indentation of the bullet. In all other operations, plain list items are
  628. not seen as headlines. For example, you cannot assign a TODO keyword to
  629. such an item."
  630. :group 'org-plain-lists
  631. :type 'boolean)
  632. (defcustom org-plain-list-ordered-item-terminator t
  633. "The character that makes a line with leading number an ordered list item.
  634. Valid values are ?. and ?\). To get both terminators, use t. While
  635. ?. may look nicer, it creates the danger that a line with leading
  636. number may be incorrectly interpreted as an item. ?\) therefore is
  637. the safe choice."
  638. :group 'org-plain-lists
  639. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  640. (const :tag "paren like in \"2)\"" ?\))
  641. (const :tab "both" t)))
  642. (defcustom org-empty-line-terminates-plain-lists nil
  643. "Non-nil means, an empty line ends all plain list levels.
  644. When nil, empty lines are part of the preceeding item."
  645. :group 'org-plain-lists
  646. :type 'boolean)
  647. (defcustom org-auto-renumber-ordered-lists t
  648. "Non-nil means, automatically renumber ordered plain lists.
  649. Renumbering happens when the sequence have been changed with
  650. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  651. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  652. :group 'org-plain-lists
  653. :type 'boolean)
  654. (defcustom org-provide-checkbox-statistics t
  655. "Non-nil means, update checkbox statistics after insert and toggle.
  656. When this is set, checkbox statistics is updated each time you either insert
  657. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  658. with \\[org-ctrl-c-ctrl-c\\]."
  659. :group 'org-plain-lists
  660. :type 'boolean)
  661. (defcustom org-description-max-indent 20
  662. "Maximum indentation for the second line of a description list.
  663. When the indentation would be larger than this, it will become
  664. 5 characters instead."
  665. :group 'org-plain-lists
  666. :type 'integer)
  667. (defgroup org-imenu-and-speedbar nil
  668. "Options concerning imenu and speedbar in Org-mode."
  669. :tag "Org Imenu and Speedbar"
  670. :group 'org-structure)
  671. (defcustom org-imenu-depth 2
  672. "The maximum level for Imenu access to Org-mode headlines.
  673. This also applied for speedbar access."
  674. :group 'org-imenu-and-speedbar
  675. :type 'number)
  676. (defgroup org-table nil
  677. "Options concerning tables in Org-mode."
  678. :tag "Org Table"
  679. :group 'org)
  680. (defcustom org-enable-table-editor 'optimized
  681. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  682. When nil, such lines will be treated like ordinary lines.
  683. When equal to the symbol `optimized', the table editor will be optimized to
  684. do the following:
  685. - Automatic overwrite mode in front of whitespace in table fields.
  686. This makes the structure of the table stay in tact as long as the edited
  687. field does not exceed the column width.
  688. - Minimize the number of realigns. Normally, the table is aligned each time
  689. TAB or RET are pressed to move to another field. With optimization this
  690. happens only if changes to a field might have changed the column width.
  691. Optimization requires replacing the functions `self-insert-command',
  692. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  693. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  694. very good at guessing when a re-align will be necessary, but you can always
  695. force one with \\[org-ctrl-c-ctrl-c].
  696. If you would like to use the optimized version in Org-mode, but the
  697. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  698. This variable can be used to turn on and off the table editor during a session,
  699. but in order to toggle optimization, a restart is required.
  700. See also the variable `org-table-auto-blank-field'."
  701. :group 'org-table
  702. :type '(choice
  703. (const :tag "off" nil)
  704. (const :tag "on" t)
  705. (const :tag "on, optimized" optimized)))
  706. (defcustom org-table-tab-recognizes-table.el t
  707. "Non-nil means, TAB will automatically notice a table.el table.
  708. When it sees such a table, it moves point into it and - if necessary -
  709. calls `table-recognize-table'."
  710. :group 'org-table-editing
  711. :type 'boolean)
  712. (defgroup org-link nil
  713. "Options concerning links in Org-mode."
  714. :tag "Org Link"
  715. :group 'org)
  716. (defvar org-link-abbrev-alist-local nil
  717. "Buffer-local version of `org-link-abbrev-alist', which see.
  718. The value of this is taken from the #+LINK lines.")
  719. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  720. (defcustom org-link-abbrev-alist nil
  721. "Alist of link abbreviations.
  722. The car of each element is a string, to be replaced at the start of a link.
  723. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  724. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  725. [[linkkey:tag][description]]
  726. If REPLACE is a string, the tag will simply be appended to create the link.
  727. If the string contains \"%s\", the tag will be inserted there.
  728. REPLACE may also be a function that will be called with the tag as the
  729. only argument to create the link, which should be returned as a string.
  730. See the manual for examples."
  731. :group 'org-link
  732. :type 'alist)
  733. (defcustom org-descriptive-links t
  734. "Non-nil means, hide link part and only show description of bracket links.
  735. Bracket links are like [[link][descritpion]]. This variable sets the initial
  736. state in new org-mode buffers. The setting can then be toggled on a
  737. per-buffer basis from the Org->Hyperlinks menu."
  738. :group 'org-link
  739. :type 'boolean)
  740. (defcustom org-link-file-path-type 'adaptive
  741. "How the path name in file links should be stored.
  742. Valid values are:
  743. relative Relative to the current directory, i.e. the directory of the file
  744. into which the link is being inserted.
  745. absolute Absolute path, if possible with ~ for home directory.
  746. noabbrev Absolute path, no abbreviation of home directory.
  747. adaptive Use relative path for files in the current directory and sub-
  748. directories of it. For other files, use an absolute path."
  749. :group 'org-link
  750. :type '(choice
  751. (const relative)
  752. (const absolute)
  753. (const noabbrev)
  754. (const adaptive)))
  755. (defcustom org-activate-links '(bracket angle plain radio tag date)
  756. "Types of links that should be activated in Org-mode files.
  757. This is a list of symbols, each leading to the activation of a certain link
  758. type. In principle, it does not hurt to turn on most link types - there may
  759. be a small gain when turning off unused link types. The types are:
  760. bracket The recommended [[link][description]] or [[link]] links with hiding.
  761. angular Links in angular brackes that may contain whitespace like
  762. <bbdb:Carsten Dominik>.
  763. plain Plain links in normal text, no whitespace, like http://google.com.
  764. radio Text that is matched by a radio target, see manual for details.
  765. tag Tag settings in a headline (link to tag search).
  766. date Time stamps (link to calendar).
  767. Changing this variable requires a restart of Emacs to become effective."
  768. :group 'org-link
  769. :type '(set (const :tag "Double bracket links (new style)" bracket)
  770. (const :tag "Angular bracket links (old style)" angular)
  771. (const :tag "Plain text links" plain)
  772. (const :tag "Radio target matches" radio)
  773. (const :tag "Tags" tag)
  774. (const :tag "Timestamps" date)))
  775. (defcustom org-make-link-description-function nil
  776. "Function to use to generate link descriptions from links. If
  777. nil the link location will be used. This function must take two
  778. parameters; the first is the link and the second the description
  779. org-insert-link has generated, and should return the description
  780. to use."
  781. :group 'org-link
  782. :type 'function)
  783. (defgroup org-link-store nil
  784. "Options concerning storing links in Org-mode."
  785. :tag "Org Store Link"
  786. :group 'org-link)
  787. (defcustom org-email-link-description-format "Email %c: %.30s"
  788. "Format of the description part of a link to an email or usenet message.
  789. The following %-excapes will be replaced by corresponding information:
  790. %F full \"From\" field
  791. %f name, taken from \"From\" field, address if no name
  792. %T full \"To\" field
  793. %t first name in \"To\" field, address if no name
  794. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  795. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  796. %s subject
  797. %m message-id.
  798. You may use normal field width specification between the % and the letter.
  799. This is for example useful to limit the length of the subject.
  800. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  801. :group 'org-link-store
  802. :type 'string)
  803. (defcustom org-from-is-user-regexp
  804. (let (r1 r2)
  805. (when (and user-mail-address (not (string= user-mail-address "")))
  806. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  807. (when (and user-full-name (not (string= user-full-name "")))
  808. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  809. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  810. "Regexp mached against the \"From:\" header of an email or usenet message.
  811. It should match if the message is from the user him/herself."
  812. :group 'org-link-store
  813. :type 'regexp)
  814. (defcustom org-context-in-file-links t
  815. "Non-nil means, file links from `org-store-link' contain context.
  816. A search string will be added to the file name with :: as separator and
  817. used to find the context when the link is activated by the command
  818. `org-open-at-point'.
  819. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  820. negates this setting for the duration of the command."
  821. :group 'org-link-store
  822. :type 'boolean)
  823. (defcustom org-keep-stored-link-after-insertion nil
  824. "Non-nil means, keep link in list for entire session.
  825. The command `org-store-link' adds a link pointing to the current
  826. location to an internal list. These links accumulate during a session.
  827. The command `org-insert-link' can be used to insert links into any
  828. Org-mode file (offering completion for all stored links). When this
  829. option is nil, every link which has been inserted once using \\[org-insert-link]
  830. will be removed from the list, to make completing the unused links
  831. more efficient."
  832. :group 'org-link-store
  833. :type 'boolean)
  834. (defgroup org-link-follow nil
  835. "Options concerning following links in Org-mode."
  836. :tag "Org Follow Link"
  837. :group 'org-link)
  838. (defcustom org-follow-link-hook nil
  839. "Hook that is run after a link has been followed."
  840. :group 'org-link-follow
  841. :type 'hook)
  842. (defcustom org-tab-follows-link nil
  843. "Non-nil means, on links TAB will follow the link.
  844. Needs to be set before org.el is loaded."
  845. :group 'org-link-follow
  846. :type 'boolean)
  847. (defcustom org-return-follows-link nil
  848. "Non-nil means, on links RET will follow the link.
  849. Needs to be set before org.el is loaded."
  850. :group 'org-link-follow
  851. :type 'boolean)
  852. (defcustom org-mouse-1-follows-link
  853. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  854. "Non-nil means, mouse-1 on a link will follow the link.
  855. A longer mouse click will still set point. Does not work on XEmacs.
  856. Needs to be set before org.el is loaded."
  857. :group 'org-link-follow
  858. :type 'boolean)
  859. (defcustom org-mark-ring-length 4
  860. "Number of different positions to be recorded in the ring
  861. Changing this requires a restart of Emacs to work correctly."
  862. :group 'org-link-follow
  863. :type 'interger)
  864. (defcustom org-link-frame-setup
  865. '((vm . vm-visit-folder-other-frame)
  866. (gnus . gnus-other-frame)
  867. (file . find-file-other-window))
  868. "Setup the frame configuration for following links.
  869. When following a link with Emacs, it may often be useful to display
  870. this link in another window or frame. This variable can be used to
  871. set this up for the different types of links.
  872. For VM, use any of
  873. `vm-visit-folder'
  874. `vm-visit-folder-other-frame'
  875. For Gnus, use any of
  876. `gnus'
  877. `gnus-other-frame'
  878. For FILE, use any of
  879. `find-file'
  880. `find-file-other-window'
  881. `find-file-other-frame'
  882. For the calendar, use the variable `calendar-setup'.
  883. For BBDB, it is currently only possible to display the matches in
  884. another window."
  885. :group 'org-link-follow
  886. :type '(list
  887. (cons (const vm)
  888. (choice
  889. (const vm-visit-folder)
  890. (const vm-visit-folder-other-window)
  891. (const vm-visit-folder-other-frame)))
  892. (cons (const gnus)
  893. (choice
  894. (const gnus)
  895. (const gnus-other-frame)))
  896. (cons (const file)
  897. (choice
  898. (const find-file)
  899. (const find-file-other-window)
  900. (const find-file-other-frame)))))
  901. (defcustom org-display-internal-link-with-indirect-buffer nil
  902. "Non-nil means, use indirect buffer to display infile links.
  903. Activating internal links (from one location in a file to another location
  904. in the same file) normally just jumps to the location. When the link is
  905. activated with a C-u prefix (or with mouse-3), the link is displayed in
  906. another window. When this option is set, the other window actually displays
  907. an indirect buffer clone of the current buffer, to avoid any visibility
  908. changes to the current buffer."
  909. :group 'org-link-follow
  910. :type 'boolean)
  911. (defcustom org-open-non-existing-files nil
  912. "Non-nil means, `org-open-file' will open non-existing files.
  913. When nil, an error will be generated."
  914. :group 'org-link-follow
  915. :type 'boolean)
  916. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  917. "Function and arguments to call for following mailto links.
  918. This is a list with the first element being a lisp function, and the
  919. remaining elements being arguments to the function. In string arguments,
  920. %a will be replaced by the address, and %s will be replaced by the subject
  921. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  922. :group 'org-link-follow
  923. :type '(choice
  924. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  925. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  926. (const :tag "message-mail" (message-mail "%a" "%s"))
  927. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  928. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  929. "Non-nil means, ask for confirmation before executing shell links.
  930. Shell links can be dangerous: just think about a link
  931. [[shell:rm -rf ~/*][Google Search]]
  932. This link would show up in your Org-mode document as \"Google Search\",
  933. but really it would remove your entire home directory.
  934. Therefore we advise against setting this variable to nil.
  935. Just change it to `y-or-n-p' of you want to confirm with a
  936. single keystroke rather than having to type \"yes\"."
  937. :group 'org-link-follow
  938. :type '(choice
  939. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  940. (const :tag "with y-or-n (faster)" y-or-n-p)
  941. (const :tag "no confirmation (dangerous)" nil)))
  942. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  943. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  944. Elisp links can be dangerous: just think about a link
  945. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  946. This link would show up in your Org-mode document as \"Google Search\",
  947. but really it would remove your entire home directory.
  948. Therefore we advise against setting this variable to nil.
  949. Just change it to `y-or-n-p' of you want to confirm with a
  950. single keystroke rather than having to type \"yes\"."
  951. :group 'org-link-follow
  952. :type '(choice
  953. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  954. (const :tag "with y-or-n (faster)" y-or-n-p)
  955. (const :tag "no confirmation (dangerous)" nil)))
  956. (defconst org-file-apps-defaults-gnu
  957. '((remote . emacs)
  958. (t . mailcap))
  959. "Default file applications on a UNIX or GNU/Linux system.
  960. See `org-file-apps'.")
  961. (defconst org-file-apps-defaults-macosx
  962. '((remote . emacs)
  963. (t . "open %s")
  964. ("ps" . "gv %s")
  965. ("ps.gz" . "gv %s")
  966. ("eps" . "gv %s")
  967. ("eps.gz" . "gv %s")
  968. ("dvi" . "xdvi %s")
  969. ("fig" . "xfig %s"))
  970. "Default file applications on a MacOS X system.
  971. The system \"open\" is known as a default, but we use X11 applications
  972. for some files for which the OS does not have a good default.
  973. See `org-file-apps'.")
  974. (defconst org-file-apps-defaults-windowsnt
  975. (list
  976. '(remote . emacs)
  977. (cons t
  978. (list (if (featurep 'xemacs)
  979. 'mswindows-shell-execute
  980. 'w32-shell-execute)
  981. "open" 'file)))
  982. "Default file applications on a Windows NT system.
  983. The system \"open\" is used for most files.
  984. See `org-file-apps'.")
  985. (defcustom org-file-apps
  986. '(
  987. ("txt" . emacs)
  988. ("tex" . emacs)
  989. ("ltx" . emacs)
  990. ("org" . emacs)
  991. ("el" . emacs)
  992. ("bib" . emacs)
  993. )
  994. "External applications for opening `file:path' items in a document.
  995. Org-mode uses system defaults for different file types, but
  996. you can use this variable to set the application for a given file
  997. extension. The entries in this list are cons cells where the car identifies
  998. files and the cdr the corresponding command. Possible values for the
  999. file identifier are
  1000. \"ext\" A string identifying an extension
  1001. `directory' Matches a directory
  1002. `remote' Matches a remote file, accessible through tramp or efs.
  1003. Remote files most likely should be visited through Emacs
  1004. because external applications cannot handle such paths.
  1005. t Default for all remaining files
  1006. Possible values for the command are:
  1007. `emacs' The file will be visited by the current Emacs process.
  1008. `default' Use the default application for this file type.
  1009. string A command to be executed by a shell; %s will be replaced
  1010. by the path to the file.
  1011. sexp A Lisp form which will be evaluated. The file path will
  1012. be available in the Lisp variable `file'.
  1013. For more examples, see the system specific constants
  1014. `org-file-apps-defaults-macosx'
  1015. `org-file-apps-defaults-windowsnt'
  1016. `org-file-apps-defaults-gnu'."
  1017. :group 'org-link-follow
  1018. :type '(repeat
  1019. (cons (choice :value ""
  1020. (string :tag "Extension")
  1021. (const :tag "Default for unrecognized files" t)
  1022. (const :tag "Remote file" remote)
  1023. (const :tag "Links to a directory" directory))
  1024. (choice :value ""
  1025. (const :tag "Visit with Emacs" emacs)
  1026. (const :tag "Use system default" default)
  1027. (string :tag "Command")
  1028. (sexp :tag "Lisp form")))))
  1029. (defgroup org-refile nil
  1030. "Options concerning refiling entries in Org-mode."
  1031. :tag "Org Remember"
  1032. :group 'org)
  1033. (defcustom org-directory "~/org"
  1034. "Directory with org files.
  1035. This directory will be used as default to prompt for org files.
  1036. Used by the hooks for remember.el."
  1037. :group 'org-refile
  1038. :group 'org-remember
  1039. :type 'directory)
  1040. (defcustom org-default-notes-file "~/.notes"
  1041. "Default target for storing notes.
  1042. Used by the hooks for remember.el. This can be a string, or nil to mean
  1043. the value of `remember-data-file'.
  1044. You can set this on a per-template basis with the variable
  1045. `org-remember-templates'."
  1046. :group 'org-refile
  1047. :group 'org-remember
  1048. :type '(choice
  1049. (const :tag "Default from remember-data-file" nil)
  1050. file))
  1051. (defcustom org-goto-interface 'outline
  1052. "The default interface to be used for `org-goto'.
  1053. Allowed vaues are:
  1054. outline The interface shows an outline of the relevant file
  1055. and the correct heading is found by moving through
  1056. the outline or by searching with incremental search.
  1057. outline-path-completion Headlines in the current buffer are offered via
  1058. completion."
  1059. :group 'org-refile
  1060. :type '(choice
  1061. (const :tag "Outline" outline)
  1062. (const :tag "Outline-path-completion" outline-path-completion)))
  1063. (defcustom org-reverse-note-order nil
  1064. "Non-nil means, store new notes at the beginning of a file or entry.
  1065. When nil, new notes will be filed to the end of a file or entry.
  1066. This can also be a list with cons cells of regular expressions that
  1067. are matched against file names, and values."
  1068. :group 'org-remember
  1069. :type '(choice
  1070. (const :tag "Reverse always" t)
  1071. (const :tag "Reverse never" nil)
  1072. (repeat :tag "By file name regexp"
  1073. (cons regexp boolean))))
  1074. (defcustom org-refile-targets nil
  1075. "Targets for refiling entries with \\[org-refile].
  1076. This is list of cons cells. Each cell contains:
  1077. - a specification of the files to be considered, either a list of files,
  1078. or a symbol whose function or variable value will be used to retrieve
  1079. a file name or a list of file names. Nil means, refile to a different
  1080. heading in the current buffer.
  1081. - A specification of how to find candidate refile targets. This may be
  1082. any of
  1083. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1084. This tag has to be present in all target headlines, inheritance will
  1085. not be considered.
  1086. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1087. todo keyword.
  1088. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1089. headlines that are refiling targets.
  1090. - a cons cell (:level . N). Any headline of level N is considered a target.
  1091. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1092. :group 'org-remember
  1093. :type '(repeat
  1094. (cons
  1095. (choice :value org-agenda-files
  1096. (const :tag "All agenda files" org-agenda-files)
  1097. (const :tag "Current buffer" nil)
  1098. (function) (variable) (file))
  1099. (choice :tag "Identify target headline by"
  1100. (cons :tag "Specific tag" (const :tag) (string))
  1101. (cons :tag "TODO keyword" (const :todo) (string))
  1102. (cons :tag "Regular expression" (const :regexp) (regexp))
  1103. (cons :tag "Level number" (const :level) (integer))
  1104. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1105. (defcustom org-refile-use-outline-path nil
  1106. "Non-nil means, provide refile targets as paths.
  1107. So a level 3 headline will be available as level1/level2/level3.
  1108. When the value is `file', also include the file name (without directory)
  1109. into the path. When `full-file-path', include the full file path."
  1110. :group 'org-remember
  1111. :type '(choice
  1112. (const :tag "Not" nil)
  1113. (const :tag "Yes" t)
  1114. (const :tag "Start with file name" file)
  1115. (const :tag "Start with full file path" full-file-path)))
  1116. (defgroup org-todo nil
  1117. "Options concerning TODO items in Org-mode."
  1118. :tag "Org TODO"
  1119. :group 'org)
  1120. (defgroup org-progress nil
  1121. "Options concerning Progress logging in Org-mode."
  1122. :tag "Org Progress"
  1123. :group 'org-time)
  1124. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1125. "List of TODO entry keyword sequences and their interpretation.
  1126. \\<org-mode-map>This is a list of sequences.
  1127. Each sequence starts with a symbol, either `sequence' or `type',
  1128. indicating if the keywords should be interpreted as a sequence of
  1129. action steps, or as different types of TODO items. The first
  1130. keywords are states requiring action - these states will select a headline
  1131. for inclusion into the global TODO list Org-mode produces. If one of
  1132. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1133. signify that no further action is necessary. If \"|\" is not found,
  1134. the last keyword is treated as the only DONE state of the sequence.
  1135. The command \\[org-todo] cycles an entry through these states, and one
  1136. additional state where no keyword is present. For details about this
  1137. cycling, see the manual.
  1138. TODO keywords and interpretation can also be set on a per-file basis with
  1139. the special #+SEQ_TODO and #+TYP_TODO lines.
  1140. Each keyword can optionally specify a character for fast state selection
  1141. \(in combination with the variable `org-use-fast-todo-selection')
  1142. and specifiers for state change logging, using the same syntax
  1143. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1144. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1145. indicates to record a time stamp each time this state is selected.
  1146. Each keyword may also specify if a timestamp or a note should be
  1147. recorded when entering or leaving the state, by adding additional
  1148. characters in the parenthesis after the keyword. This looks like this:
  1149. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1150. record only the time of the state change. With X and Y being either
  1151. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1152. Y when leaving the state if and only if the *target* state does not
  1153. define X. You may omit any of the fast-selection key or X or /Y,
  1154. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1155. For backward compatibility, this variable may also be just a list
  1156. of keywords - in this case the interptetation (sequence or type) will be
  1157. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1158. :group 'org-todo
  1159. :group 'org-keywords
  1160. :type '(choice
  1161. (repeat :tag "Old syntax, just keywords"
  1162. (string :tag "Keyword"))
  1163. (repeat :tag "New syntax"
  1164. (cons
  1165. (choice
  1166. :tag "Interpretation"
  1167. (const :tag "Sequence (cycling hits every state)" sequence)
  1168. (const :tag "Type (cycling directly to DONE)" type))
  1169. (repeat
  1170. (string :tag "Keyword"))))))
  1171. (defvar org-todo-keywords-1 nil
  1172. "All TODO and DONE keywords active in a buffer.")
  1173. (make-variable-buffer-local 'org-todo-keywords-1)
  1174. (defvar org-todo-keywords-for-agenda nil)
  1175. (defvar org-done-keywords-for-agenda nil)
  1176. (defvar org-agenda-contributing-files nil)
  1177. (defvar org-not-done-keywords nil)
  1178. (make-variable-buffer-local 'org-not-done-keywords)
  1179. (defvar org-done-keywords nil)
  1180. (make-variable-buffer-local 'org-done-keywords)
  1181. (defvar org-todo-heads nil)
  1182. (make-variable-buffer-local 'org-todo-heads)
  1183. (defvar org-todo-sets nil)
  1184. (make-variable-buffer-local 'org-todo-sets)
  1185. (defvar org-todo-log-states nil)
  1186. (make-variable-buffer-local 'org-todo-log-states)
  1187. (defvar org-todo-kwd-alist nil)
  1188. (make-variable-buffer-local 'org-todo-kwd-alist)
  1189. (defvar org-todo-key-alist nil)
  1190. (make-variable-buffer-local 'org-todo-key-alist)
  1191. (defvar org-todo-key-trigger nil)
  1192. (make-variable-buffer-local 'org-todo-key-trigger)
  1193. (defcustom org-todo-interpretation 'sequence
  1194. "Controls how TODO keywords are interpreted.
  1195. This variable is in principle obsolete and is only used for
  1196. backward compatibility, if the interpretation of todo keywords is
  1197. not given already in `org-todo-keywords'. See that variable for
  1198. more information."
  1199. :group 'org-todo
  1200. :group 'org-keywords
  1201. :type '(choice (const sequence)
  1202. (const type)))
  1203. (defcustom org-use-fast-todo-selection 'prefix
  1204. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1205. This variable describes if and under what circumstances the cycling
  1206. mechanism for TODO keywords will be replaced by a single-key, direct
  1207. selection scheme.
  1208. When nil, fast selection is never used.
  1209. When the symbol `prefix', it will be used when `org-todo' is called with
  1210. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1211. in an agenda buffer.
  1212. When t, fast selection is used by default. In this case, the prefix
  1213. argument forces cycling instead.
  1214. In all cases, the special interface is only used if access keys have actually
  1215. been assigned by the user, i.e. if keywords in the configuration are followed
  1216. by a letter in parenthesis, like TODO(t)."
  1217. :group 'org-todo
  1218. :type '(choice
  1219. (const :tag "Never" nil)
  1220. (const :tag "By default" t)
  1221. (const :tag "Only with C-u C-c C-t" prefix)))
  1222. (defcustom org-provide-todo-statistics t
  1223. "Non-nil means, update todo statistics after insert and toggle.
  1224. When this is set, todo statistics is updated in the parent of the current
  1225. entry each time a todo state is changed."
  1226. :group 'org-todo
  1227. :type 'boolean)
  1228. (defcustom org-after-todo-state-change-hook nil
  1229. "Hook which is run after the state of a TODO item was changed.
  1230. The new state (a string with a TODO keyword, or nil) is available in the
  1231. Lisp variable `state'."
  1232. :group 'org-todo
  1233. :type 'hook)
  1234. (defcustom org-log-done nil
  1235. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1236. When equal to the list (done), also prompt for a closing note.
  1237. This can also be configured on a per-file basis by adding one of
  1238. the following lines anywhere in the buffer:
  1239. #+STARTUP: logdone
  1240. #+STARTUP: lognotedone
  1241. #+STARTUP: nologdone"
  1242. :group 'org-todo
  1243. :group 'org-progress
  1244. :type '(choice
  1245. (const :tag "No logging" nil)
  1246. (const :tag "Record CLOSED timestamp" time)
  1247. (const :tag "Record CLOSED timestamp with closing note." note)))
  1248. ;; Normalize old uses of org-log-done.
  1249. (cond
  1250. ((eq org-log-done t) (setq org-log-done 'time))
  1251. ((and (listp org-log-done) (memq 'done org-log-done))
  1252. (setq org-log-done 'note)))
  1253. (defcustom org-log-note-clock-out nil
  1254. "Non-nil means, recored a note when clocking out of an item.
  1255. This can also be configured on a per-file basis by adding one of
  1256. the following lines anywhere in the buffer:
  1257. #+STARTUP: lognoteclock-out
  1258. #+STARTUP: nolognoteclock-out"
  1259. :group 'org-todo
  1260. :group 'org-progress
  1261. :type 'boolean)
  1262. (defcustom org-log-done-with-time t
  1263. "Non-nil means, the CLOSED time stamp will contain date and time.
  1264. When nil, only the date will be recorded."
  1265. :group 'org-progress
  1266. :type 'boolean)
  1267. (defcustom org-log-note-headings
  1268. '((done . "CLOSING NOTE %t")
  1269. (state . "State %-12s %t")
  1270. (note . "Note taken on %t")
  1271. (clock-out . ""))
  1272. "Headings for notes added to entries.
  1273. The value is an alist, with the car being a symbol indicating the note
  1274. context, and the cdr is the heading to be used. The heading may also be the
  1275. empty string.
  1276. %t in the heading will be replaced by a time stamp.
  1277. %s will be replaced by the new TODO state, in double quotes.
  1278. %u will be replaced by the user name.
  1279. %U will be replaced by the full user name."
  1280. :group 'org-todo
  1281. :group 'org-progress
  1282. :type '(list :greedy t
  1283. (cons (const :tag "Heading when closing an item" done) string)
  1284. (cons (const :tag
  1285. "Heading when changing todo state (todo sequence only)"
  1286. state) string)
  1287. (cons (const :tag "Heading when just taking a note" note) string)
  1288. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1289. (unless (assq 'note org-log-note-headings)
  1290. (push '(note . "%t") org-log-note-headings))
  1291. (defcustom org-log-states-order-reversed t
  1292. "Non-nil means, the latest state change note will be directly after heading.
  1293. When nil, the notes will be orderer according to time."
  1294. :group 'org-todo
  1295. :group 'org-progress
  1296. :type 'boolean)
  1297. (defcustom org-log-repeat 'time
  1298. "Non-nil means, record moving through the DONE state when triggering repeat.
  1299. An auto-repeating tasks is immediately switched back to TODO when marked
  1300. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1301. the TODO keyword definition, or recording a closing note by setting
  1302. `org-log-done', there will be no record of the task moving through DONE.
  1303. This variable forces taking a note anyway. Possible values are:
  1304. nil Don't force a record
  1305. time Record a time stamp
  1306. note Record a note
  1307. This option can also be set with on a per-file-basis with
  1308. #+STARTUP: logrepeat
  1309. #+STARTUP: lognoterepeat
  1310. #+STARTUP: nologrepeat
  1311. You can have local logging settings for a subtree by setting the LOGGING
  1312. property to one or more of these keywords."
  1313. :group 'org-todo
  1314. :group 'org-progress
  1315. :type '(choice
  1316. (const :tag "Don't force a record" nil)
  1317. (const :tag "Force recording the DONE state" time)
  1318. (const :tag "Force recording a note with the DONE state" note)))
  1319. (defgroup org-priorities nil
  1320. "Priorities in Org-mode."
  1321. :tag "Org Priorities"
  1322. :group 'org-todo)
  1323. (defcustom org-highest-priority ?A
  1324. "The highest priority of TODO items. A character like ?A, ?B etc.
  1325. Must have a smaller ASCII number than `org-lowest-priority'."
  1326. :group 'org-priorities
  1327. :type 'character)
  1328. (defcustom org-lowest-priority ?C
  1329. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1330. Must have a larger ASCII number than `org-highest-priority'."
  1331. :group 'org-priorities
  1332. :type 'character)
  1333. (defcustom org-default-priority ?B
  1334. "The default priority of TODO items.
  1335. This is the priority an item get if no explicit priority is given."
  1336. :group 'org-priorities
  1337. :type 'character)
  1338. (defcustom org-priority-start-cycle-with-default t
  1339. "Non-nil means, start with default priority when starting to cycle.
  1340. When this is nil, the first step in the cycle will be (depending on the
  1341. command used) one higher or lower that the default priority."
  1342. :group 'org-priorities
  1343. :type 'boolean)
  1344. (defgroup org-time nil
  1345. "Options concerning time stamps and deadlines in Org-mode."
  1346. :tag "Org Time"
  1347. :group 'org)
  1348. (defcustom org-insert-labeled-timestamps-at-point nil
  1349. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1350. When nil, these labeled time stamps are forces into the second line of an
  1351. entry, just after the headline. When scheduling from the global TODO list,
  1352. the time stamp will always be forced into the second line."
  1353. :group 'org-time
  1354. :type 'boolean)
  1355. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1356. "Formats for `format-time-string' which are used for time stamps.
  1357. It is not recommended to change this constant.")
  1358. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1359. "Number of minutes to round time stamps to.
  1360. These are two values, the first applies when first creating a time stamp.
  1361. The second applies when changing it with the commands `S-up' and `S-down'.
  1362. When changing the time stamp, this means that it will change in steps
  1363. of N minutes, as given by the second value.
  1364. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1365. numbers should be factors of 60, so for example 5, 10, 15.
  1366. When this is larger than 1, you can still force an exact time-stamp by using
  1367. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1368. and by using a prefix arg to `S-up/down' to specify the exact number
  1369. of minutes to shift."
  1370. :group 'org-time
  1371. :get '(lambda (var) ; Make sure all entries have 5 elements
  1372. (if (integerp (default-value var))
  1373. (list (default-value var) 5)
  1374. (default-value var)))
  1375. :type '(list
  1376. (integer :tag "when inserting times")
  1377. (integer :tag "when modifying times")))
  1378. ;; Normalize old customizations of this variable.
  1379. (when (integerp org-time-stamp-rounding-minutes)
  1380. (setq org-time-stamp-rounding-minutes
  1381. (list org-time-stamp-rounding-minutes
  1382. org-time-stamp-rounding-minutes)))
  1383. (defcustom org-display-custom-times nil
  1384. "Non-nil means, overlay custom formats over all time stamps.
  1385. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1386. To turn this on on a per-file basis, insert anywhere in the file:
  1387. #+STARTUP: customtime"
  1388. :group 'org-time
  1389. :set 'set-default
  1390. :type 'sexp)
  1391. (make-variable-buffer-local 'org-display-custom-times)
  1392. (defcustom org-time-stamp-custom-formats
  1393. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1394. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1395. These are overlayed over the default ISO format if the variable
  1396. `org-display-custom-times' is set. Time like %H:%M should be at the
  1397. end of the second format."
  1398. :group 'org-time
  1399. :type 'sexp)
  1400. (defun org-time-stamp-format (&optional long inactive)
  1401. "Get the right format for a time string."
  1402. (let ((f (if long (cdr org-time-stamp-formats)
  1403. (car org-time-stamp-formats))))
  1404. (if inactive
  1405. (concat "[" (substring f 1 -1) "]")
  1406. f)))
  1407. (defcustom org-time-clocksum-format "%d:%02d"
  1408. "The format string used when creating CLOCKSUM lines, or when
  1409. org-mode generates a time duration."
  1410. :group 'org-time
  1411. :type 'string)
  1412. (defcustom org-deadline-warning-days 14
  1413. "No. of days before expiration during which a deadline becomes active.
  1414. This variable governs the display in sparse trees and in the agenda.
  1415. When 0 or negative, it means use this number (the absolute value of it)
  1416. even if a deadline has a different individual lead time specified."
  1417. :group 'org-time
  1418. :group 'org-agenda-daily/weekly
  1419. :type 'number)
  1420. (defcustom org-read-date-prefer-future t
  1421. "Non-nil means, assume future for incomplete date input from user.
  1422. This affects the following situations:
  1423. 1. The user gives a day, but no month.
  1424. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1425. read this as the third of *next* month. However, if you enter \"17\",
  1426. it will be considered as *this* month.
  1427. 2. The user gives a month but not a year.
  1428. For example, if it is april and you enter \"feb 2\", this will be read
  1429. as feb 2, *next* year. \"May 5\", however, will be this year.
  1430. Currently this does not work for ISO week specifications.
  1431. When this option is nil, the current month and year will always be used
  1432. as defaults."
  1433. :group 'org-time
  1434. :type 'boolean)
  1435. (defcustom org-read-date-display-live t
  1436. "Non-nil means, display current interpretation of date prompt live.
  1437. This display will be in an overlay, in the minibuffer."
  1438. :group 'org-time
  1439. :type 'boolean)
  1440. (defcustom org-read-date-popup-calendar t
  1441. "Non-nil means, pop up a calendar when prompting for a date.
  1442. In the calendar, the date can be selected with mouse-1. However, the
  1443. minibuffer will also be active, and you can simply enter the date as well.
  1444. When nil, only the minibuffer will be available."
  1445. :group 'org-time
  1446. :type 'boolean)
  1447. (if (fboundp 'defvaralias)
  1448. (defvaralias 'org-popup-calendar-for-date-prompt
  1449. 'org-read-date-popup-calendar))
  1450. (defcustom org-extend-today-until 0
  1451. "The hour when your day really ends.
  1452. This has influence for the following applications:
  1453. - When switching the agenda to \"today\". It it is still earlier than
  1454. the time given here, the day recognized as TODAY is actually yesterday.
  1455. - When a date is read from the user and it is still before the time given
  1456. here, the current date and time will be assumed to be yesterday, 23:59.
  1457. FIXME:
  1458. IMPORTANT: This is still a very experimental feature, it may disappear
  1459. again or it may be extended to mean more things."
  1460. :group 'org-time
  1461. :type 'number)
  1462. (defcustom org-edit-timestamp-down-means-later nil
  1463. "Non-nil means, S-down will increase the time in a time stamp.
  1464. When nil, S-up will increase."
  1465. :group 'org-time
  1466. :type 'boolean)
  1467. (defcustom org-calendar-follow-timestamp-change t
  1468. "Non-nil means, make the calendar window follow timestamp changes.
  1469. When a timestamp is modified and the calendar window is visible, it will be
  1470. moved to the new date."
  1471. :group 'org-time
  1472. :type 'boolean)
  1473. (defgroup org-tags nil
  1474. "Options concerning tags in Org-mode."
  1475. :tag "Org Tags"
  1476. :group 'org)
  1477. (defcustom org-tag-alist nil
  1478. "List of tags allowed in Org-mode files.
  1479. When this list is nil, Org-mode will base TAG input on what is already in the
  1480. buffer.
  1481. The value of this variable is an alist, the car of each entry must be a
  1482. keyword as a string, the cdr may be a character that is used to select
  1483. that tag through the fast-tag-selection interface.
  1484. See the manual for details."
  1485. :group 'org-tags
  1486. :type '(repeat
  1487. (choice
  1488. (cons (string :tag "Tag name")
  1489. (character :tag "Access char"))
  1490. (const :tag "Start radio group" (:startgroup))
  1491. (const :tag "End radio group" (:endgroup)))))
  1492. (defcustom org-use-fast-tag-selection 'auto
  1493. "Non-nil means, use fast tag selection scheme.
  1494. This is a special interface to select and deselect tags with single keys.
  1495. When nil, fast selection is never used.
  1496. When the symbol `auto', fast selection is used if and only if selection
  1497. characters for tags have been configured, either through the variable
  1498. `org-tag-alist' or through a #+TAGS line in the buffer.
  1499. When t, fast selection is always used and selection keys are assigned
  1500. automatically if necessary."
  1501. :group 'org-tags
  1502. :type '(choice
  1503. (const :tag "Always" t)
  1504. (const :tag "Never" nil)
  1505. (const :tag "When selection characters are configured" 'auto)))
  1506. (defcustom org-fast-tag-selection-single-key nil
  1507. "Non-nil means, fast tag selection exits after first change.
  1508. When nil, you have to press RET to exit it.
  1509. During fast tag selection, you can toggle this flag with `C-c'.
  1510. This variable can also have the value `expert'. In this case, the window
  1511. displaying the tags menu is not even shown, until you press C-c again."
  1512. :group 'org-tags
  1513. :type '(choice
  1514. (const :tag "No" nil)
  1515. (const :tag "Yes" t)
  1516. (const :tag "Expert" expert)))
  1517. (defvar org-fast-tag-selection-include-todo nil
  1518. "Non-nil means, fast tags selection interface will also offer TODO states.
  1519. This is an undocumented feature, you should not rely on it.")
  1520. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1521. "The column to which tags should be indented in a headline.
  1522. If this number is positive, it specifies the column. If it is negative,
  1523. it means that the tags should be flushright to that column. For example,
  1524. -80 works well for a normal 80 character screen."
  1525. :group 'org-tags
  1526. :type 'integer)
  1527. (defcustom org-auto-align-tags t
  1528. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1529. These operations change the length of a headline and therefore shift
  1530. the tags around. With this options turned on, after each such operation
  1531. the tags are again aligned to `org-tags-column'."
  1532. :group 'org-tags
  1533. :type 'boolean)
  1534. (defcustom org-use-tag-inheritance t
  1535. "Non-nil means, tags in levels apply also for sublevels.
  1536. When nil, only the tags directly given in a specific line apply there.
  1537. If you turn off this option, you very likely want to turn on the
  1538. companion option `org-tags-match-list-sublevels'.
  1539. This may also be a list of tags that should be inherited, or a regexp that
  1540. matches tags that should be inherited."
  1541. :group 'org-tags
  1542. :type '(choice
  1543. (const :tag "Not" nil)
  1544. (const :tag "Always" t)
  1545. (repeat :tag "Specific tags" (string :tag "Tag"))
  1546. (regexp :tag "Tags matched by regexp")))
  1547. (defun org-tag-inherit-p (tag)
  1548. "Check if TAG is one that should be inherited."
  1549. (cond
  1550. ((eq org-use-tag-inheritance t) t)
  1551. ((not org-use-tag-inheritance) nil)
  1552. ((stringp org-use-tag-inheritance)
  1553. (string-match org-use-tag-inheritance tag))
  1554. ((listp org-use-tag-inheritance)
  1555. (member tag org-use-tag-inheritance))
  1556. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1557. (defcustom org-tags-match-list-sublevels nil
  1558. "Non-nil means list also sublevels of headlines matching tag search.
  1559. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1560. the sublevels of a headline matching a tag search often also match
  1561. the same search. Listing all of them can create very long lists.
  1562. Setting this variable to nil causes subtrees of a match to be skipped.
  1563. This option is off by default, because inheritance in on. If you turn
  1564. inheritance off, you very likely want to turn this option on.
  1565. As a special case, if the tag search is restricted to TODO items, the
  1566. value of this variable is ignored and sublevels are always checked, to
  1567. make sure all corresponding TODO items find their way into the list."
  1568. :group 'org-tags
  1569. :type 'boolean)
  1570. (defvar org-tags-history nil
  1571. "History of minibuffer reads for tags.")
  1572. (defvar org-last-tags-completion-table nil
  1573. "The last used completion table for tags.")
  1574. (defvar org-after-tags-change-hook nil
  1575. "Hook that is run after the tags in a line have changed.")
  1576. (defgroup org-properties nil
  1577. "Options concerning properties in Org-mode."
  1578. :tag "Org Properties"
  1579. :group 'org)
  1580. (defcustom org-property-format "%-10s %s"
  1581. "How property key/value pairs should be formatted by `indent-line'.
  1582. When `indent-line' hits a property definition, it will format the line
  1583. according to this format, mainly to make sure that the values are
  1584. lined-up with respect to each other."
  1585. :group 'org-properties
  1586. :type 'string)
  1587. (defcustom org-use-property-inheritance nil
  1588. "Non-nil means, properties apply also for sublevels.
  1589. This setting is chiefly used during property searches. Turning it on can
  1590. cause significant overhead when doing a search, which is why it is not
  1591. on by default.
  1592. When nil, only the properties directly given in the current entry count.
  1593. When t, every property is inherited. The value may also be a list of
  1594. properties that should have inheritance, or a regular expression matching
  1595. properties that should be inherited.
  1596. However, note that some special properties use inheritance under special
  1597. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1598. and the properties ending in \"_ALL\" when they are used as descriptor
  1599. for valid values of a property.
  1600. Note for programmers:
  1601. When querying an entry with `org-entry-get', you can control if inheritance
  1602. should be used. By default, `org-entry-get' looks only at the local
  1603. properties. You can request inheritance by setting the inherit argument
  1604. to t (to force inheritance) or to `selective' (to respect the setting
  1605. in this variable)."
  1606. :group 'org-properties
  1607. :type '(choice
  1608. (const :tag "Not" nil)
  1609. (const :tag "Always" t)
  1610. (repeat :tag "Specific properties" (string :tag "Property"))
  1611. (regexp :tag "Properties matched by regexp")))
  1612. (defun org-property-inherit-p (property)
  1613. "Check if PROPERTY is one that should be inherited."
  1614. (cond
  1615. ((eq org-use-property-inheritance t) t)
  1616. ((not org-use-property-inheritance) nil)
  1617. ((stringp org-use-property-inheritance)
  1618. (string-match org-use-property-inheritance property))
  1619. ((listp org-use-property-inheritance)
  1620. (member property org-use-property-inheritance))
  1621. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1622. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1623. "The default column format, if no other format has been defined.
  1624. This variable can be set on the per-file basis by inserting a line
  1625. #+COLUMNS: %25ITEM ....."
  1626. :group 'org-properties
  1627. :type 'string)
  1628. (defcustom org-effort-property "Effort"
  1629. "The property that is being used to keep track of effort estimates.
  1630. Effort estimates given in this property need to have the format H:MM."
  1631. :group 'org-properties
  1632. :group 'org-progress
  1633. :type '(string :tag "Property"))
  1634. (defconst org-global-properties-fixed
  1635. '(("VISIBILITY_ALL" . "folded children content all"))
  1636. "List of property/value pairs that can be inherited by any entry.
  1637. These are fixed values, for the preset properties.")
  1638. (defcustom org-global-properties nil
  1639. "List of property/value pairs that can be inherited by any entry.
  1640. You can set buffer-local values for this by adding lines like
  1641. #+PROPERTY: NAME VALUE"
  1642. :group 'org-properties
  1643. :type '(repeat
  1644. (cons (string :tag "Property")
  1645. (string :tag "Value"))))
  1646. (defvar org-local-properties nil
  1647. "List of property/value pairs that can be inherited by any entry.
  1648. Valid for the current buffer.
  1649. This variable is populated from #+PROPERTY lines.")
  1650. (defgroup org-agenda nil
  1651. "Options concerning agenda views in Org-mode."
  1652. :tag "Org Agenda"
  1653. :group 'org)
  1654. (defvar org-category nil
  1655. "Variable used by org files to set a category for agenda display.
  1656. Such files should use a file variable to set it, for example
  1657. # -*- mode: org; org-category: \"ELisp\"
  1658. or contain a special line
  1659. #+CATEGORY: ELisp
  1660. If the file does not specify a category, then file's base name
  1661. is used instead.")
  1662. (make-variable-buffer-local 'org-category)
  1663. (defcustom org-agenda-files nil
  1664. "The files to be used for agenda display.
  1665. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1666. \\[org-remove-file]. You can also use customize to edit the list.
  1667. If an entry is a directory, all files in that directory that are matched by
  1668. `org-agenda-file-regexp' will be part of the file list.
  1669. If the value of the variable is not a list but a single file name, then
  1670. the list of agenda files is actually stored and maintained in that file, one
  1671. agenda file per line."
  1672. :group 'org-agenda
  1673. :type '(choice
  1674. (repeat :tag "List of files and directories" file)
  1675. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1676. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1677. "Regular expression to match files for `org-agenda-files'.
  1678. If any element in the list in that variable contains a directory instead
  1679. of a normal file, all files in that directory that are matched by this
  1680. regular expression will be included."
  1681. :group 'org-agenda
  1682. :type 'regexp)
  1683. (defcustom org-agenda-text-search-extra-files nil
  1684. "List of extra files to be searched by text search commands.
  1685. These files will be search in addition to the agenda files by the
  1686. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1687. Note that these files will only be searched for text search commands,
  1688. not for the other agenda views like todo lists, tag searches or the weekly
  1689. agenda. This variable is intended to list notes and possibly archive files
  1690. that should also be searched by these two commands.
  1691. In fact, if the first element in the list is the symbol `agenda-archives',
  1692. than all archive files of all agenda files will be added to the search
  1693. scope."
  1694. :group 'org-agenda
  1695. :type '(set :greedy t
  1696. (const :tag "Agenda Archives" agenda-archives)
  1697. (repeat :inline t (file))))
  1698. (if (fboundp 'defvaralias)
  1699. (defvaralias 'org-agenda-multi-occur-extra-files
  1700. 'org-agenda-text-search-extra-files))
  1701. (defcustom org-agenda-skip-unavailable-files nil
  1702. "t means to just skip non-reachable files in `org-agenda-files'.
  1703. Nil means to remove them, after a query, from the list."
  1704. :group 'org-agenda
  1705. :type 'boolean)
  1706. (defcustom org-calendar-to-agenda-key [?c]
  1707. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1708. The command `org-calendar-goto-agenda' will be bound to this key. The
  1709. default is the character `c' because then `c' can be used to switch back and
  1710. forth between agenda and calendar."
  1711. :group 'org-agenda
  1712. :type 'sexp)
  1713. (eval-after-load "calendar"
  1714. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1715. 'org-calendar-goto-agenda))
  1716. (defgroup org-latex nil
  1717. "Options for embedding LaTeX code into Org-mode."
  1718. :tag "Org LaTeX"
  1719. :group 'org)
  1720. (defcustom org-format-latex-options
  1721. '(:foreground default :background default :scale 1.0
  1722. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1723. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1724. "Options for creating images from LaTeX fragments.
  1725. This is a property list with the following properties:
  1726. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1727. `default' means use the foreground of the default face.
  1728. :background the background color, or \"Transparent\".
  1729. `default' means use the background of the default face.
  1730. :scale a scaling factor for the size of the images.
  1731. :html-foreground, :html-background, :html-scale
  1732. the same numbers for HTML export.
  1733. :matchers a list indicating which matchers should be used to
  1734. find LaTeX fragments. Valid members of this list are:
  1735. \"begin\" find environments
  1736. \"$\" find math expressions surrounded by $...$
  1737. \"$$\" find math expressions surrounded by $$....$$
  1738. \"\\(\" find math expressions surrounded by \\(...\\)
  1739. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1740. :group 'org-latex
  1741. :type 'plist)
  1742. (defcustom org-format-latex-header "\\documentclass{article}
  1743. \\usepackage{fullpage} % do not remove
  1744. \\usepackage{amssymb}
  1745. \\usepackage[usenames]{color}
  1746. \\usepackage{amsmath}
  1747. \\usepackage{latexsym}
  1748. \\usepackage[mathscr]{eucal}
  1749. \\pagestyle{empty} % do not remove"
  1750. "The document header used for processing LaTeX fragments."
  1751. :group 'org-latex
  1752. :type 'string)
  1753. (defgroup org-font-lock nil
  1754. "Font-lock settings for highlighting in Org-mode."
  1755. :tag "Org Font Lock"
  1756. :group 'org)
  1757. (defcustom org-level-color-stars-only nil
  1758. "Non-nil means fontify only the stars in each headline.
  1759. When nil, the entire headline is fontified.
  1760. Changing it requires restart of `font-lock-mode' to become effective
  1761. also in regions already fontified."
  1762. :group 'org-font-lock
  1763. :type 'boolean)
  1764. (defcustom org-hide-leading-stars nil
  1765. "Non-nil means, hide the first N-1 stars in a headline.
  1766. This works by using the face `org-hide' for these stars. This
  1767. face is white for a light background, and black for a dark
  1768. background. You may have to customize the face `org-hide' to
  1769. make this work.
  1770. Changing it requires restart of `font-lock-mode' to become effective
  1771. also in regions already fontified.
  1772. You may also set this on a per-file basis by adding one of the following
  1773. lines to the buffer:
  1774. #+STARTUP: hidestars
  1775. #+STARTUP: showstars"
  1776. :group 'org-font-lock
  1777. :type 'boolean)
  1778. (defcustom org-fontify-done-headline nil
  1779. "Non-nil means, change the face of a headline if it is marked DONE.
  1780. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1781. When this is non-nil, the headline after the keyword is set to the
  1782. `org-headline-done' as an additional indication."
  1783. :group 'org-font-lock
  1784. :type 'boolean)
  1785. (defcustom org-fontify-emphasized-text t
  1786. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1787. Changing this variable requires a restart of Emacs to take effect."
  1788. :group 'org-font-lock
  1789. :type 'boolean)
  1790. (defcustom org-highlight-latex-fragments-and-specials nil
  1791. "Non-nil means, fontify what is treated specially by the exporters."
  1792. :group 'org-font-lock
  1793. :type 'boolean)
  1794. (defcustom org-hide-emphasis-markers nil
  1795. "Non-nil mean font-lock should hide the emphasis marker characters."
  1796. :group 'org-font-lock
  1797. :type 'boolean)
  1798. (defvar org-emph-re nil
  1799. "Regular expression for matching emphasis.")
  1800. (defvar org-verbatim-re nil
  1801. "Regular expression for matching verbatim text.")
  1802. (defvar org-emphasis-regexp-components) ; defined just below
  1803. (defvar org-emphasis-alist) ; defined just below
  1804. (defun org-set-emph-re (var val)
  1805. "Set variable and compute the emphasis regular expression."
  1806. (set var val)
  1807. (when (and (boundp 'org-emphasis-alist)
  1808. (boundp 'org-emphasis-regexp-components)
  1809. org-emphasis-alist org-emphasis-regexp-components)
  1810. (let* ((e org-emphasis-regexp-components)
  1811. (pre (car e))
  1812. (post (nth 1 e))
  1813. (border (nth 2 e))
  1814. (body (nth 3 e))
  1815. (nl (nth 4 e))
  1816. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1817. (body1 (concat body "*?"))
  1818. (markers (mapconcat 'car org-emphasis-alist ""))
  1819. (vmarkers (mapconcat
  1820. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1821. org-emphasis-alist "")))
  1822. ;; make sure special characters appear at the right position in the class
  1823. (if (string-match "\\^" markers)
  1824. (setq markers (concat (replace-match "" t t markers) "^")))
  1825. (if (string-match "-" markers)
  1826. (setq markers (concat (replace-match "" t t markers) "-")))
  1827. (if (string-match "\\^" vmarkers)
  1828. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1829. (if (string-match "-" vmarkers)
  1830. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1831. (if (> nl 0)
  1832. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1833. (int-to-string nl) "\\}")))
  1834. ;; Make the regexp
  1835. (setq org-emph-re
  1836. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1837. "\\("
  1838. "\\([" markers "]\\)"
  1839. "\\("
  1840. "[^" border "]\\|"
  1841. "[^" border (if (and nil stacked) markers) "]"
  1842. body1
  1843. "[^" border (if (and nil stacked) markers) "]"
  1844. "\\)"
  1845. "\\3\\)"
  1846. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1847. (setq org-verbatim-re
  1848. (concat "\\([" pre "]\\|^\\)"
  1849. "\\("
  1850. "\\([" vmarkers "]\\)"
  1851. "\\("
  1852. "[^" border "]\\|"
  1853. "[^" border "]"
  1854. body1
  1855. "[^" border "]"
  1856. "\\)"
  1857. "\\3\\)"
  1858. "\\([" post "]\\|$\\)")))))
  1859. (defcustom org-emphasis-regexp-components
  1860. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1861. "Components used to build the regular expression for emphasis.
  1862. This is a list with 6 entries. Terminology: In an emphasis string
  1863. like \" *strong word* \", we call the initial space PREMATCH, the final
  1864. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1865. and \"trong wor\" is the body. The different components in this variable
  1866. specify what is allowed/forbidden in each part:
  1867. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1868. post Chars allowed as postmatch. End of line will be allowed too.
  1869. border The chars *forbidden* as border characters.
  1870. body-regexp A regexp like \".\" to match a body character. Don't use
  1871. non-shy groups here, and don't allow newline here.
  1872. newline The maximum number of newlines allowed in an emphasis exp.
  1873. Use customize to modify this, or restart Emacs after changing it."
  1874. :group 'org-font-lock
  1875. :set 'org-set-emph-re
  1876. :type '(list
  1877. (sexp :tag "Allowed chars in pre ")
  1878. (sexp :tag "Allowed chars in post ")
  1879. (sexp :tag "Forbidden chars in border ")
  1880. (sexp :tag "Regexp for body ")
  1881. (integer :tag "number of newlines allowed")
  1882. (option (boolean :tag "Stacking (DISABLED) "))))
  1883. (defcustom org-emphasis-alist
  1884. `(("*" bold "<b>" "</b>")
  1885. ("/" italic "<i>" "</i>")
  1886. ("_" underline "<u>" "</u>")
  1887. ("=" org-code "<code>" "</code>" verbatim)
  1888. ("~" org-verbatim "" "" verbatim)
  1889. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1890. "<del>" "</del>")
  1891. )
  1892. "Special syntax for emphasized text.
  1893. Text starting and ending with a special character will be emphasized, for
  1894. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1895. characters, the face to be used by font-lock for highlighting in Org-mode
  1896. Emacs buffers, and the HTML tags to be used for this.
  1897. Use customize to modify this, or restart Emacs after changing it."
  1898. :group 'org-font-lock
  1899. :set 'org-set-emph-re
  1900. :type '(repeat
  1901. (list
  1902. (string :tag "Marker character")
  1903. (choice
  1904. (face :tag "Font-lock-face")
  1905. (plist :tag "Face property list"))
  1906. (string :tag "HTML start tag")
  1907. (string :tag "HTML end tag")
  1908. (option (const verbatim)))))
  1909. ;;; Miscellaneous options
  1910. (defgroup org-completion nil
  1911. "Completion in Org-mode."
  1912. :tag "Org Completion"
  1913. :group 'org)
  1914. (defcustom org-completion-fallback-command 'hippie-expand
  1915. "The expansion command called by \\[org-complete] in normal context.
  1916. Normal means, no org-mode-specific context."
  1917. :group 'org-completion
  1918. :type 'function)
  1919. ;;; Functions and variables from ther packages
  1920. ;; Declared here to avoid compiler warnings
  1921. ;; XEmacs only
  1922. (defvar outline-mode-menu-heading)
  1923. (defvar outline-mode-menu-show)
  1924. (defvar outline-mode-menu-hide)
  1925. (defvar zmacs-regions) ; XEmacs regions
  1926. ;; Emacs only
  1927. (defvar mark-active)
  1928. ;; Various packages
  1929. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  1930. (declare-function calendar-forward-day "cal-move" (arg))
  1931. (declare-function calendar-goto-date "cal-move" (date))
  1932. (declare-function calendar-goto-today "cal-move" ())
  1933. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  1934. (defvar calc-embedded-close-formula)
  1935. (defvar calc-embedded-open-formula)
  1936. (declare-function cdlatex-tab "ext:cdlatex" ())
  1937. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1938. (defvar font-lock-unfontify-region-function)
  1939. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1940. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  1941. (defvar iswitchb-temp-buflist)
  1942. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  1943. (declare-function org-agenda-skip "org-agenda" ())
  1944. (declare-function org-format-agenda-item "org-agenda"
  1945. (extra txt &optional category tags dotime noprefix remove-re))
  1946. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  1947. (declare-function org-agenda-change-all-lines "org-agenda"
  1948. (newhead hdmarker &optional fixface))
  1949. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  1950. (declare-function org-agenda-maybe-redo "org-agenda" ())
  1951. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  1952. (beg end))
  1953. (declare-function parse-time-string "parse-time" (string))
  1954. (declare-function remember "remember" (&optional initial))
  1955. (declare-function remember-buffer-desc "remember" ())
  1956. (declare-function remember-finalize "remember" ())
  1957. (defvar remember-save-after-remembering)
  1958. (defvar remember-data-file)
  1959. (defvar remember-register)
  1960. (defvar remember-buffer)
  1961. (defvar remember-handler-functions)
  1962. (defvar remember-annotation-functions)
  1963. (defvar texmathp-why)
  1964. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  1965. (declare-function table--at-cell-p "table" (position &optional object at-column))
  1966. (defvar w3m-current-url)
  1967. (defvar w3m-current-title)
  1968. (defvar org-latex-regexps)
  1969. ;;; Autoload and prepare some org modules
  1970. ;; Some table stuff that needs to be defined here, because it is used
  1971. ;; by the functions setting up org-mode or checking for table context.
  1972. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  1973. "Detects an org-type or table-type table.")
  1974. (defconst org-table-line-regexp "^[ \t]*|"
  1975. "Detects an org-type table line.")
  1976. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  1977. "Detects an org-type table line.")
  1978. (defconst org-table-hline-regexp "^[ \t]*|-"
  1979. "Detects an org-type table hline.")
  1980. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  1981. "Detects a table-type table hline.")
  1982. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  1983. "Searching from within a table (any type) this finds the first line
  1984. outside the table.")
  1985. ;; Autoload the functions in org-table.el that are needed by functions here.
  1986. (eval-and-compile
  1987. (org-autoload "org-table"
  1988. '(org-table-align org-table-begin org-table-blank-field
  1989. org-table-convert org-table-convert-region org-table-copy-down
  1990. org-table-copy-region org-table-create
  1991. org-table-create-or-convert-from-region
  1992. org-table-create-with-table.el org-table-current-dline
  1993. org-table-cut-region org-table-delete-column org-table-edit-field
  1994. org-table-edit-formulas org-table-end org-table-eval-formula
  1995. org-table-export org-table-field-info
  1996. org-table-get-stored-formulas org-table-goto-column
  1997. org-table-hline-and-move org-table-import org-table-insert-column
  1998. org-table-insert-hline org-table-insert-row org-table-iterate
  1999. org-table-justify-field-maybe org-table-kill-row
  2000. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2001. org-table-move-column org-table-move-column-left
  2002. org-table-move-column-right org-table-move-row
  2003. org-table-move-row-down org-table-move-row-up
  2004. org-table-next-field org-table-next-row org-table-paste-rectangle
  2005. org-table-previous-field org-table-recalculate
  2006. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2007. org-table-toggle-coordinate-overlays
  2008. org-table-toggle-formula-debugger org-table-wrap-region
  2009. orgtbl-mode turn-on-orgtbl)))
  2010. (defun org-at-table-p (&optional table-type)
  2011. "Return t if the cursor is inside an org-type table.
  2012. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2013. (if org-enable-table-editor
  2014. (save-excursion
  2015. (beginning-of-line 1)
  2016. (looking-at (if table-type org-table-any-line-regexp
  2017. org-table-line-regexp)))
  2018. nil))
  2019. (defsubst org-table-p () (org-at-table-p))
  2020. (defun org-at-table.el-p ()
  2021. "Return t if and only if we are at a table.el table."
  2022. (and (org-at-table-p 'any)
  2023. (save-excursion
  2024. (goto-char (org-table-begin 'any))
  2025. (looking-at org-table1-hline-regexp))))
  2026. (defun org-table-recognize-table.el ()
  2027. "If there is a table.el table nearby, recognize it and move into it."
  2028. (if org-table-tab-recognizes-table.el
  2029. (if (org-at-table.el-p)
  2030. (progn
  2031. (beginning-of-line 1)
  2032. (if (looking-at org-table-dataline-regexp)
  2033. nil
  2034. (if (looking-at org-table1-hline-regexp)
  2035. (progn
  2036. (beginning-of-line 2)
  2037. (if (looking-at org-table-any-border-regexp)
  2038. (beginning-of-line -1)))))
  2039. (if (re-search-forward "|" (org-table-end t) t)
  2040. (progn
  2041. (require 'table)
  2042. (if (table--at-cell-p (point))
  2043. t
  2044. (message "recognizing table.el table...")
  2045. (table-recognize-table)
  2046. (message "recognizing table.el table...done")))
  2047. (error "This should not happen..."))
  2048. t)
  2049. nil)
  2050. nil))
  2051. (defun org-at-table-hline-p ()
  2052. "Return t if the cursor is inside a hline in a table."
  2053. (if org-enable-table-editor
  2054. (save-excursion
  2055. (beginning-of-line 1)
  2056. (looking-at org-table-hline-regexp))
  2057. nil))
  2058. (defvar org-table-clean-did-remove-column nil)
  2059. (defun org-table-map-tables (function)
  2060. "Apply FUNCTION to the start of all tables in the buffer."
  2061. (save-excursion
  2062. (save-restriction
  2063. (widen)
  2064. (goto-char (point-min))
  2065. (while (re-search-forward org-table-any-line-regexp nil t)
  2066. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2067. (beginning-of-line 1)
  2068. (if (looking-at org-table-line-regexp)
  2069. (save-excursion (funcall function)))
  2070. (re-search-forward org-table-any-border-regexp nil 1))))
  2071. (message "Mapping tables: done"))
  2072. ;; Declare and autoload functions from org-exp.el
  2073. (declare-function org-default-export-plist "org-exp")
  2074. (declare-function org-infile-export-plist "org-exp")
  2075. (declare-function org-get-current-options "org-exp")
  2076. (eval-and-compile
  2077. (org-autoload "org-exp"
  2078. '(org-export org-export-as-ascii org-export-visible
  2079. org-insert-export-options-template org-export-as-html-and-open
  2080. org-export-as-html-batch org-export-as-html-to-buffer
  2081. org-replace-region-by-html org-export-region-as-html
  2082. org-export-as-html org-export-icalendar-this-file
  2083. org-export-icalendar-all-agenda-files
  2084. org-table-clean-before-export
  2085. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2086. ;; Declare and autoload functions from org-exp.el
  2087. (eval-and-compile
  2088. (org-autoload "org-exp"
  2089. '(org-agenda org-agenda-list org-search-view
  2090. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2091. org-diary org-agenda-to-appt)))
  2092. ;; Autoload org-remember
  2093. (eval-and-compile
  2094. (org-autoload "org-remember"
  2095. '(org-remember-insinuate org-remember-annotation
  2096. org-remember-apply-template org-remember org-remember-handler)))
  2097. ;; Autoload org-clock.el
  2098. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2099. (beg end))
  2100. (declare-function org-update-mode-line "org-clock" ())
  2101. (defvar org-clock-start-time)
  2102. (defvar org-clock-marker (make-marker)
  2103. "Marker recording the last clock-in.")
  2104. (eval-and-compile
  2105. (org-autoload
  2106. "org-clock"
  2107. '(org-clock-in org-clock-out org-clock-cancel
  2108. org-clock-goto org-clock-sum org-clock-display
  2109. org-remove-clock-overlays org-clock-report
  2110. org-clocktable-shift org-dblock-write:clocktable
  2111. org-get-clocktable)))
  2112. (defun org-clock-update-time-maybe ()
  2113. "If this is a CLOCK line, update it and return t.
  2114. Otherwise, return nil."
  2115. (interactive)
  2116. (save-excursion
  2117. (beginning-of-line 1)
  2118. (skip-chars-forward " \t")
  2119. (when (looking-at org-clock-string)
  2120. (let ((re (concat "[ \t]*" org-clock-string
  2121. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2122. "\\([ \t]*=>.*\\)?\\)?"))
  2123. ts te h m s)
  2124. (cond
  2125. ((not (looking-at re))
  2126. nil)
  2127. ((not (match-end 2))
  2128. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2129. (> org-clock-marker (point))
  2130. (<= org-clock-marker (point-at-eol)))
  2131. ;; The clock is running here
  2132. (setq org-clock-start-time
  2133. (apply 'encode-time
  2134. (org-parse-time-string (match-string 1))))
  2135. (org-update-mode-line)))
  2136. (t
  2137. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2138. (end-of-line 1)
  2139. (setq ts (match-string 1)
  2140. te (match-string 3))
  2141. (setq s (- (time-to-seconds
  2142. (apply 'encode-time (org-parse-time-string te)))
  2143. (time-to-seconds
  2144. (apply 'encode-time (org-parse-time-string ts))))
  2145. h (floor (/ s 3600))
  2146. s (- s (* 3600 h))
  2147. m (floor (/ s 60))
  2148. s (- s (* 60 s)))
  2149. (insert " => " (format "%2d:%02d" h m))
  2150. t))))))
  2151. (defun org-check-running-clock ()
  2152. "Check if the current buffer contains the running clock.
  2153. If yes, offer to stop it and to save the buffer with the changes."
  2154. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2155. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2156. (buffer-name))))
  2157. (org-clock-out)
  2158. (when (y-or-n-p "Save changed buffer?")
  2159. (save-buffer))))
  2160. (defun org-clocktable-try-shift (dir n)
  2161. "Check if this line starts a clock table, if yes, shift the time block."
  2162. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2163. (org-clocktable-shift dir n)))
  2164. ;; Autoload archiving code
  2165. ;; The stuff that is needed for cycling and tags has to be defined here.
  2166. (defgroup org-archive nil
  2167. "Options concerning archiving in Org-mode."
  2168. :tag "Org Archive"
  2169. :group 'org-structure)
  2170. (defcustom org-archive-location "%s_archive::"
  2171. "The location where subtrees should be archived.
  2172. Otherwise, the value of this variable is a string, consisting of two
  2173. parts, separated by a double-colon.
  2174. The first part is a file name - when omitted, archiving happens in the same
  2175. file. %s will be replaced by the current file name (without directory part).
  2176. Archiving to a different file is useful to keep archived entries from
  2177. contributing to the Org-mode Agenda.
  2178. The part after the double colon is a headline. The archived entries will be
  2179. filed under that headline. When omitted, the subtrees are simply filed away
  2180. at the end of the file, as top-level entries.
  2181. Here are a few examples:
  2182. \"%s_archive::\"
  2183. If the current file is Projects.org, archive in file
  2184. Projects.org_archive, as top-level trees. This is the default.
  2185. \"::* Archived Tasks\"
  2186. Archive in the current file, under the top-level headline
  2187. \"* Archived Tasks\".
  2188. \"~/org/archive.org::\"
  2189. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2190. \"basement::** Finished Tasks\"
  2191. Archive in file ./basement (relative path), as level 3 trees
  2192. below the level 2 heading \"** Finished Tasks\".
  2193. You may set this option on a per-file basis by adding to the buffer a
  2194. line like
  2195. #+ARCHIVE: basement::** Finished Tasks
  2196. You may also define it locally for a subtree by setting an ARCHIVE property
  2197. in the entry. If such a property is found in an entry, or anywhere up
  2198. the hierarchy, it will be used."
  2199. :group 'org-archive
  2200. :type 'string)
  2201. (defcustom org-archive-tag "ARCHIVE"
  2202. "The tag that marks a subtree as archived.
  2203. An archived subtree does not open during visibility cycling, and does
  2204. not contribute to the agenda listings.
  2205. After changing this, font-lock must be restarted in the relevant buffers to
  2206. get the proper fontification."
  2207. :group 'org-archive
  2208. :group 'org-keywords
  2209. :type 'string)
  2210. (defcustom org-agenda-skip-archived-trees t
  2211. "Non-nil means, the agenda will skip any items located in archived trees.
  2212. An archived tree is a tree marked with the tag ARCHIVE."
  2213. :group 'org-archive
  2214. :group 'org-agenda-skip
  2215. :type 'boolean)
  2216. (defcustom org-cycle-open-archived-trees nil
  2217. "Non-nil means, `org-cycle' will open archived trees.
  2218. An archived tree is a tree marked with the tag ARCHIVE.
  2219. When nil, archived trees will stay folded. You can still open them with
  2220. normal outline commands like `show-all', but not with the cycling commands."
  2221. :group 'org-archive
  2222. :group 'org-cycle
  2223. :type 'boolean)
  2224. (defcustom org-sparse-tree-open-archived-trees nil
  2225. "Non-nil means sparse tree construction shows matches in archived trees.
  2226. When nil, matches in these trees are highlighted, but the trees are kept in
  2227. collapsed state."
  2228. :group 'org-archive
  2229. :group 'org-sparse-trees
  2230. :type 'boolean)
  2231. (defun org-cycle-hide-archived-subtrees (state)
  2232. "Re-hide all archived subtrees after a visibility state change."
  2233. (when (and (not org-cycle-open-archived-trees)
  2234. (not (memq state '(overview folded))))
  2235. (save-excursion
  2236. (let* ((globalp (memq state '(contents all)))
  2237. (beg (if globalp (point-min) (point)))
  2238. (end (if globalp (point-max) (org-end-of-subtree t))))
  2239. (org-hide-archived-subtrees beg end)
  2240. (goto-char beg)
  2241. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2242. (message "%s" (substitute-command-keys
  2243. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2244. (defun org-force-cycle-archived ()
  2245. "Cycle subtree even if it is archived."
  2246. (interactive)
  2247. (setq this-command 'org-cycle)
  2248. (let ((org-cycle-open-archived-trees t))
  2249. (call-interactively 'org-cycle)))
  2250. (defun org-hide-archived-subtrees (beg end)
  2251. "Re-hide all archived subtrees after a visibility state change."
  2252. (save-excursion
  2253. (let* ((re (concat ":" org-archive-tag ":")))
  2254. (goto-char beg)
  2255. (while (re-search-forward re end t)
  2256. (and (org-on-heading-p) (hide-subtree))
  2257. (org-end-of-subtree t)))))
  2258. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2259. (eval-and-compile
  2260. (org-autoload "org-archive"
  2261. '(org-add-archive-files org-archive-subtree
  2262. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2263. ;; Autoload Column View Code
  2264. (declare-function org-columns-number-to-string "org-colview")
  2265. (declare-function org-columns-get-format-and-top-level "org-colview")
  2266. (declare-function org-columns-compute "org-colview")
  2267. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2268. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2269. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2270. org-columns org-insert-columns-dblock))
  2271. ;; Autoload ID code
  2272. (org-autoload "org-id"
  2273. '(org-id-get-create org-id-copy org-id-get
  2274. org-id-get-with-outline-path-completion
  2275. org-id-get-with-outline-drilling
  2276. org-id-goto org-id-find))
  2277. ;;; Variables for pre-computed regular expressions, all buffer local
  2278. (defvar org-drawer-regexp nil
  2279. "Matches first line of a hidden block.")
  2280. (make-variable-buffer-local 'org-drawer-regexp)
  2281. (defvar org-todo-regexp nil
  2282. "Matches any of the TODO state keywords.")
  2283. (make-variable-buffer-local 'org-todo-regexp)
  2284. (defvar org-not-done-regexp nil
  2285. "Matches any of the TODO state keywords except the last one.")
  2286. (make-variable-buffer-local 'org-not-done-regexp)
  2287. (defvar org-todo-line-regexp nil
  2288. "Matches a headline and puts TODO state into group 2 if present.")
  2289. (make-variable-buffer-local 'org-todo-line-regexp)
  2290. (defvar org-complex-heading-regexp nil
  2291. "Matches a headline and puts everything into groups:
  2292. group 1: the stars
  2293. group 2: The todo keyword, maybe
  2294. group 3: Priority cookie
  2295. group 4: True headline
  2296. group 5: Tags")
  2297. (make-variable-buffer-local 'org-complex-heading-regexp)
  2298. (defvar org-todo-line-tags-regexp nil
  2299. "Matches a headline and puts TODO state into group 2 if present.
  2300. Also put tags into group 4 if tags are present.")
  2301. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2302. (defvar org-nl-done-regexp nil
  2303. "Matches newline followed by a headline with the DONE keyword.")
  2304. (make-variable-buffer-local 'org-nl-done-regexp)
  2305. (defvar org-looking-at-done-regexp nil
  2306. "Matches the DONE keyword a point.")
  2307. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2308. (defvar org-ds-keyword-length 12
  2309. "Maximum length of the Deadline and SCHEDULED keywords.")
  2310. (make-variable-buffer-local 'org-ds-keyword-length)
  2311. (defvar org-deadline-regexp nil
  2312. "Matches the DEADLINE keyword.")
  2313. (make-variable-buffer-local 'org-deadline-regexp)
  2314. (defvar org-deadline-time-regexp nil
  2315. "Matches the DEADLINE keyword together with a time stamp.")
  2316. (make-variable-buffer-local 'org-deadline-time-regexp)
  2317. (defvar org-deadline-line-regexp nil
  2318. "Matches the DEADLINE keyword and the rest of the line.")
  2319. (make-variable-buffer-local 'org-deadline-line-regexp)
  2320. (defvar org-scheduled-regexp nil
  2321. "Matches the SCHEDULED keyword.")
  2322. (make-variable-buffer-local 'org-scheduled-regexp)
  2323. (defvar org-scheduled-time-regexp nil
  2324. "Matches the SCHEDULED keyword together with a time stamp.")
  2325. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2326. (defvar org-closed-time-regexp nil
  2327. "Matches the CLOSED keyword together with a time stamp.")
  2328. (make-variable-buffer-local 'org-closed-time-regexp)
  2329. (defvar org-keyword-time-regexp nil
  2330. "Matches any of the 4 keywords, together with the time stamp.")
  2331. (make-variable-buffer-local 'org-keyword-time-regexp)
  2332. (defvar org-keyword-time-not-clock-regexp nil
  2333. "Matches any of the 3 keywords, together with the time stamp.")
  2334. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2335. (defvar org-maybe-keyword-time-regexp nil
  2336. "Matches a timestamp, possibly preceeded by a keyword.")
  2337. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2338. (defvar org-planning-or-clock-line-re nil
  2339. "Matches a line with planning or clock info.")
  2340. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2341. (defconst org-plain-time-of-day-regexp
  2342. (concat
  2343. "\\(\\<[012]?[0-9]"
  2344. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2345. "\\(--?"
  2346. "\\(\\<[012]?[0-9]"
  2347. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2348. "\\)?")
  2349. "Regular expression to match a plain time or time range.
  2350. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2351. groups carry important information:
  2352. 0 the full match
  2353. 1 the first time, range or not
  2354. 8 the second time, if it is a range.")
  2355. (defconst org-plain-time-extension-regexp
  2356. (concat
  2357. "\\(\\<[012]?[0-9]"
  2358. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2359. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2360. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2361. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2362. groups carry important information:
  2363. 0 the full match
  2364. 7 hours of duration
  2365. 9 minutes of duration")
  2366. (defconst org-stamp-time-of-day-regexp
  2367. (concat
  2368. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2369. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2370. "\\(--?"
  2371. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2372. "Regular expression to match a timestamp time or time range.
  2373. After a match, the following groups carry important information:
  2374. 0 the full match
  2375. 1 date plus weekday, for backreferencing to make sure both times on same day
  2376. 2 the first time, range or not
  2377. 4 the second time, if it is a range.")
  2378. (defconst org-startup-options
  2379. '(("fold" org-startup-folded t)
  2380. ("overview" org-startup-folded t)
  2381. ("nofold" org-startup-folded nil)
  2382. ("showall" org-startup-folded nil)
  2383. ("content" org-startup-folded content)
  2384. ("hidestars" org-hide-leading-stars t)
  2385. ("showstars" org-hide-leading-stars nil)
  2386. ("odd" org-odd-levels-only t)
  2387. ("oddeven" org-odd-levels-only nil)
  2388. ("align" org-startup-align-all-tables t)
  2389. ("noalign" org-startup-align-all-tables nil)
  2390. ("customtime" org-display-custom-times t)
  2391. ("logdone" org-log-done time)
  2392. ("lognotedone" org-log-done note)
  2393. ("nologdone" org-log-done nil)
  2394. ("lognoteclock-out" org-log-note-clock-out t)
  2395. ("nolognoteclock-out" org-log-note-clock-out nil)
  2396. ("logrepeat" org-log-repeat state)
  2397. ("lognoterepeat" org-log-repeat note)
  2398. ("nologrepeat" org-log-repeat nil)
  2399. ("constcgs" constants-unit-system cgs)
  2400. ("constSI" constants-unit-system SI))
  2401. "Variable associated with STARTUP options for org-mode.
  2402. Each element is a list of three items: The startup options as written
  2403. in the #+STARTUP line, the corresponding variable, and the value to
  2404. set this variable to if the option is found. An optional forth element PUSH
  2405. means to push this value onto the list in the variable.")
  2406. (defun org-set-regexps-and-options ()
  2407. "Precompute regular expressions for current buffer."
  2408. (when (org-mode-p)
  2409. (org-set-local 'org-todo-kwd-alist nil)
  2410. (org-set-local 'org-todo-key-alist nil)
  2411. (org-set-local 'org-todo-key-trigger nil)
  2412. (org-set-local 'org-todo-keywords-1 nil)
  2413. (org-set-local 'org-done-keywords nil)
  2414. (org-set-local 'org-todo-heads nil)
  2415. (org-set-local 'org-todo-sets nil)
  2416. (org-set-local 'org-todo-log-states nil)
  2417. (let ((re (org-make-options-regexp
  2418. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2419. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2420. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2421. (splitre "[ \t]+")
  2422. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2423. tail sep kws1 prio props drawers
  2424. ext-setup-or-nil setup-contents (start 0))
  2425. (save-excursion
  2426. (save-restriction
  2427. (widen)
  2428. (goto-char (point-min))
  2429. (while (or (and ext-setup-or-nil
  2430. (string-match re ext-setup-or-nil start)
  2431. (setq start (match-end 0)))
  2432. (and (setq ext-setup-or-nil nil start 0)
  2433. (re-search-forward re nil t)))
  2434. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2435. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2436. (cond
  2437. ((equal key "CATEGORY")
  2438. (if (string-match "[ \t]+$" value)
  2439. (setq value (replace-match "" t t value)))
  2440. (setq cat value))
  2441. ((member key '("SEQ_TODO" "TODO"))
  2442. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2443. ((equal key "TYP_TODO")
  2444. (push (cons 'type (org-split-string value splitre)) kwds))
  2445. ((equal key "TAGS")
  2446. (setq tags (append tags (org-split-string value splitre))))
  2447. ((equal key "COLUMNS")
  2448. (org-set-local 'org-columns-default-format value))
  2449. ((equal key "LINK")
  2450. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2451. (push (cons (match-string 1 value)
  2452. (org-trim (match-string 2 value)))
  2453. links)))
  2454. ((equal key "PRIORITIES")
  2455. (setq prio (org-split-string value " +")))
  2456. ((equal key "PROPERTY")
  2457. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2458. (push (cons (match-string 1 value) (match-string 2 value))
  2459. props)))
  2460. ((equal key "DRAWERS")
  2461. (setq drawers (org-split-string value splitre)))
  2462. ((equal key "CONSTANTS")
  2463. (setq const (append const (org-split-string value splitre))))
  2464. ((equal key "STARTUP")
  2465. (let ((opts (org-split-string value splitre))
  2466. l var val)
  2467. (while (setq l (pop opts))
  2468. (when (setq l (assoc l org-startup-options))
  2469. (setq var (nth 1 l) val (nth 2 l))
  2470. (if (not (nth 3 l))
  2471. (set (make-local-variable var) val)
  2472. (if (not (listp (symbol-value var)))
  2473. (set (make-local-variable var) nil))
  2474. (set (make-local-variable var) (symbol-value var))
  2475. (add-to-list var val))))))
  2476. ((equal key "ARCHIVE")
  2477. (string-match " *$" value)
  2478. (setq arch (replace-match "" t t value))
  2479. (remove-text-properties 0 (length arch)
  2480. '(face t fontified t) arch))
  2481. ((equal key "SETUPFILE")
  2482. (setq setup-contents (org-file-contents
  2483. (expand-file-name
  2484. (org-remove-double-quotes value))
  2485. 'noerror))
  2486. (if (not ext-setup-or-nil)
  2487. (setq ext-setup-or-nil setup-contents start 0)
  2488. (setq ext-setup-or-nil
  2489. (concat (substring ext-setup-or-nil 0 start)
  2490. "\n" setup-contents "\n"
  2491. (substring ext-setup-or-nil start)))))
  2492. ))))
  2493. (when cat
  2494. (org-set-local 'org-category (intern cat))
  2495. (push (cons "CATEGORY" cat) props))
  2496. (when prio
  2497. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2498. (setq prio (mapcar 'string-to-char prio))
  2499. (org-set-local 'org-highest-priority (nth 0 prio))
  2500. (org-set-local 'org-lowest-priority (nth 1 prio))
  2501. (org-set-local 'org-default-priority (nth 2 prio)))
  2502. (and props (org-set-local 'org-local-properties (nreverse props)))
  2503. (and drawers (org-set-local 'org-drawers drawers))
  2504. (and arch (org-set-local 'org-archive-location arch))
  2505. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2506. ;; Process the TODO keywords
  2507. (unless kwds
  2508. ;; Use the global values as if they had been given locally.
  2509. (setq kwds (default-value 'org-todo-keywords))
  2510. (if (stringp (car kwds))
  2511. (setq kwds (list (cons org-todo-interpretation
  2512. (default-value 'org-todo-keywords)))))
  2513. (setq kwds (reverse kwds)))
  2514. (setq kwds (nreverse kwds))
  2515. (let (inter kws kw)
  2516. (while (setq kws (pop kwds))
  2517. (setq inter (pop kws) sep (member "|" kws)
  2518. kws0 (delete "|" (copy-sequence kws))
  2519. kwsa nil
  2520. kws1 (mapcar
  2521. (lambda (x)
  2522. ;; 1 2
  2523. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2524. (progn
  2525. (setq kw (match-string 1 x)
  2526. key (and (match-end 2) (match-string 2 x))
  2527. log (org-extract-log-state-settings x))
  2528. (push (cons kw (and key (string-to-char key))) kwsa)
  2529. (and log (push log org-todo-log-states))
  2530. kw)
  2531. (error "Invalid TODO keyword %s" x)))
  2532. kws0)
  2533. kwsa (if kwsa (append '((:startgroup))
  2534. (nreverse kwsa)
  2535. '((:endgroup))))
  2536. hw (car kws1)
  2537. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2538. tail (list inter hw (car dws) (org-last dws)))
  2539. (add-to-list 'org-todo-heads hw 'append)
  2540. (push kws1 org-todo-sets)
  2541. (setq org-done-keywords (append org-done-keywords dws nil))
  2542. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2543. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2544. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2545. (setq org-todo-sets (nreverse org-todo-sets)
  2546. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2547. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2548. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2549. ;; Process the constants
  2550. (when const
  2551. (let (e cst)
  2552. (while (setq e (pop const))
  2553. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2554. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2555. (setq org-table-formula-constants-local cst)))
  2556. ;; Process the tags.
  2557. (when tags
  2558. (let (e tgs)
  2559. (while (setq e (pop tags))
  2560. (cond
  2561. ((equal e "{") (push '(:startgroup) tgs))
  2562. ((equal e "}") (push '(:endgroup) tgs))
  2563. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2564. (push (cons (match-string 1 e)
  2565. (string-to-char (match-string 2 e)))
  2566. tgs))
  2567. (t (push (list e) tgs))))
  2568. (org-set-local 'org-tag-alist nil)
  2569. (while (setq e (pop tgs))
  2570. (or (and (stringp (car e))
  2571. (assoc (car e) org-tag-alist))
  2572. (push e org-tag-alist))))))
  2573. ;; Compute the regular expressions and other local variables
  2574. (if (not org-done-keywords)
  2575. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2576. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2577. (length org-scheduled-string)
  2578. (length org-clock-string)
  2579. (length org-closed-string)))
  2580. org-drawer-regexp
  2581. (concat "^[ \t]*:\\("
  2582. (mapconcat 'regexp-quote org-drawers "\\|")
  2583. "\\):[ \t]*$")
  2584. org-not-done-keywords
  2585. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2586. org-todo-regexp
  2587. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2588. "\\|") "\\)\\>")
  2589. org-not-done-regexp
  2590. (concat "\\<\\("
  2591. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2592. "\\)\\>")
  2593. org-todo-line-regexp
  2594. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2595. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2596. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2597. org-complex-heading-regexp
  2598. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2599. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2600. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2601. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2602. org-nl-done-regexp
  2603. (concat "\n\\*+[ \t]+"
  2604. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2605. "\\)" "\\>")
  2606. org-todo-line-tags-regexp
  2607. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2608. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2609. (org-re
  2610. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2611. org-looking-at-done-regexp
  2612. (concat "^" "\\(?:"
  2613. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2614. "\\>")
  2615. org-deadline-regexp (concat "\\<" org-deadline-string)
  2616. org-deadline-time-regexp
  2617. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2618. org-deadline-line-regexp
  2619. (concat "\\<\\(" org-deadline-string "\\).*")
  2620. org-scheduled-regexp
  2621. (concat "\\<" org-scheduled-string)
  2622. org-scheduled-time-regexp
  2623. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2624. org-closed-time-regexp
  2625. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2626. org-keyword-time-regexp
  2627. (concat "\\<\\(" org-scheduled-string
  2628. "\\|" org-deadline-string
  2629. "\\|" org-closed-string
  2630. "\\|" org-clock-string "\\)"
  2631. " *[[<]\\([^]>]+\\)[]>]")
  2632. org-keyword-time-not-clock-regexp
  2633. (concat "\\<\\(" org-scheduled-string
  2634. "\\|" org-deadline-string
  2635. "\\|" org-closed-string
  2636. "\\)"
  2637. " *[[<]\\([^]>]+\\)[]>]")
  2638. org-maybe-keyword-time-regexp
  2639. (concat "\\(\\<\\(" org-scheduled-string
  2640. "\\|" org-deadline-string
  2641. "\\|" org-closed-string
  2642. "\\|" org-clock-string "\\)\\)?"
  2643. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2644. org-planning-or-clock-line-re
  2645. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2646. "\\|" org-deadline-string
  2647. "\\|" org-closed-string "\\|" org-clock-string
  2648. "\\)\\>\\)")
  2649. )
  2650. (org-compute-latex-and-specials-regexp)
  2651. (org-set-font-lock-defaults)))
  2652. (defun org-file-contents (file &optional noerror)
  2653. "Return the contents of FILE, as a string."
  2654. (if (or (not file)
  2655. (not (file-readable-p file)))
  2656. (if noerror
  2657. (progn
  2658. (message "Cannot read file %s" file)
  2659. (ding) (sit-for 2)
  2660. "")
  2661. (error "Cannot read file %s" file))
  2662. (with-temp-buffer
  2663. (insert-file-contents file)
  2664. (buffer-string))))
  2665. (defun org-extract-log-state-settings (x)
  2666. "Extract the log state setting from a TODO keyword string.
  2667. This will extract info from a string like \"WAIT(w@/!)\"."
  2668. (let (kw key log1 log2)
  2669. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2670. (setq kw (match-string 1 x)
  2671. key (and (match-end 2) (match-string 2 x))
  2672. log1 (and (match-end 3) (match-string 3 x))
  2673. log2 (and (match-end 4) (match-string 4 x)))
  2674. (and (or log1 log2)
  2675. (list kw
  2676. (and log1 (if (equal log1 "!") 'time 'note))
  2677. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2678. (defun org-remove-keyword-keys (list)
  2679. "Remove a pair of parenthesis at the end of each string in LIST."
  2680. (mapcar (lambda (x)
  2681. (if (string-match "(.*)$" x)
  2682. (substring x 0 (match-beginning 0))
  2683. x))
  2684. list))
  2685. ;; FIXME: this could be done much better, using second characters etc.
  2686. (defun org-assign-fast-keys (alist)
  2687. "Assign fast keys to a keyword-key alist.
  2688. Respect keys that are already there."
  2689. (let (new e k c c1 c2 (char ?a))
  2690. (while (setq e (pop alist))
  2691. (cond
  2692. ((equal e '(:startgroup)) (push e new))
  2693. ((equal e '(:endgroup)) (push e new))
  2694. (t
  2695. (setq k (car e) c2 nil)
  2696. (if (cdr e)
  2697. (setq c (cdr e))
  2698. ;; automatically assign a character.
  2699. (setq c1 (string-to-char
  2700. (downcase (substring
  2701. k (if (= (string-to-char k) ?@) 1 0)))))
  2702. (if (or (rassoc c1 new) (rassoc c1 alist))
  2703. (while (or (rassoc char new) (rassoc char alist))
  2704. (setq char (1+ char)))
  2705. (setq c2 c1))
  2706. (setq c (or c2 char)))
  2707. (push (cons k c) new))))
  2708. (nreverse new)))
  2709. ;;; Some variables used in various places
  2710. (defvar org-window-configuration nil
  2711. "Used in various places to store a window configuration.")
  2712. (defvar org-finish-function nil
  2713. "Function to be called when `C-c C-c' is used.
  2714. This is for getting out of special buffers like remember.")
  2715. ;; FIXME: Occasionally check by commenting these, to make sure
  2716. ;; no other functions uses these, forgetting to let-bind them.
  2717. (defvar entry)
  2718. (defvar state)
  2719. (defvar last-state)
  2720. (defvar date)
  2721. (defvar description)
  2722. ;; Defined somewhere in this file, but used before definition.
  2723. (defvar org-html-entities)
  2724. (defvar org-struct-menu)
  2725. (defvar org-org-menu)
  2726. (defvar org-tbl-menu)
  2727. (defvar org-agenda-keymap)
  2728. ;;;; Define the Org-mode
  2729. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2730. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or ugrade to newer allout, for example by switching to Emacs 22."))
  2731. ;; We use a before-change function to check if a table might need
  2732. ;; an update.
  2733. (defvar org-table-may-need-update t
  2734. "Indicates that a table might need an update.
  2735. This variable is set by `org-before-change-function'.
  2736. `org-table-align' sets it back to nil.")
  2737. (defun org-before-change-function (beg end)
  2738. "Every change indicates that a table might need an update."
  2739. (setq org-table-may-need-update t))
  2740. (defvar org-mode-map)
  2741. (defvar org-mode-hook nil)
  2742. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2743. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2744. (defvar org-table-buffer-is-an nil)
  2745. (defconst org-outline-regexp "\\*+ ")
  2746. ;;;###autoload
  2747. (define-derived-mode org-mode outline-mode "Org"
  2748. "Outline-based notes management and organizer, alias
  2749. \"Carsten's outline-mode for keeping track of everything.\"
  2750. Org-mode develops organizational tasks around a NOTES file which
  2751. contains information about projects as plain text. Org-mode is
  2752. implemented on top of outline-mode, which is ideal to keep the content
  2753. of large files well structured. It supports ToDo items, deadlines and
  2754. time stamps, which magically appear in the diary listing of the Emacs
  2755. calendar. Tables are easily created with a built-in table editor.
  2756. Plain text URL-like links connect to websites, emails (VM), Usenet
  2757. messages (Gnus), BBDB entries, and any files related to the project.
  2758. For printing and sharing of notes, an Org-mode file (or a part of it)
  2759. can be exported as a structured ASCII or HTML file.
  2760. The following commands are available:
  2761. \\{org-mode-map}"
  2762. ;; Get rid of Outline menus, they are not needed
  2763. ;; Need to do this here because define-derived-mode sets up
  2764. ;; the keymap so late. Still, it is a waste to call this each time
  2765. ;; we switch another buffer into org-mode.
  2766. (if (featurep 'xemacs)
  2767. (when (boundp 'outline-mode-menu-heading)
  2768. ;; Assume this is Greg's port, it used easymenu
  2769. (easy-menu-remove outline-mode-menu-heading)
  2770. (easy-menu-remove outline-mode-menu-show)
  2771. (easy-menu-remove outline-mode-menu-hide))
  2772. (define-key org-mode-map [menu-bar headings] 'undefined)
  2773. (define-key org-mode-map [menu-bar hide] 'undefined)
  2774. (define-key org-mode-map [menu-bar show] 'undefined))
  2775. (org-load-modules-maybe)
  2776. (easy-menu-add org-org-menu)
  2777. (easy-menu-add org-tbl-menu)
  2778. (org-install-agenda-files-menu)
  2779. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2780. (org-add-to-invisibility-spec '(org-cwidth))
  2781. (when (featurep 'xemacs)
  2782. (org-set-local 'line-move-ignore-invisible t))
  2783. (org-set-local 'outline-regexp org-outline-regexp)
  2784. (org-set-local 'outline-level 'org-outline-level)
  2785. (when (and org-ellipsis
  2786. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2787. (fboundp 'make-glyph-code))
  2788. (unless org-display-table
  2789. (setq org-display-table (make-display-table)))
  2790. (set-display-table-slot
  2791. org-display-table 4
  2792. (vconcat (mapcar
  2793. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2794. org-ellipsis)))
  2795. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2796. (setq buffer-display-table org-display-table))
  2797. (org-set-regexps-and-options)
  2798. ;; Calc embedded
  2799. (org-set-local 'calc-embedded-open-mode "# ")
  2800. (modify-syntax-entry ?# "<")
  2801. (modify-syntax-entry ?@ "w")
  2802. (if org-startup-truncated (setq truncate-lines t))
  2803. (org-set-local 'font-lock-unfontify-region-function
  2804. 'org-unfontify-region)
  2805. ;; Activate before-change-function
  2806. (org-set-local 'org-table-may-need-update t)
  2807. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2808. 'local)
  2809. ;; Check for running clock before killing a buffer
  2810. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2811. ;; Paragraphs and auto-filling
  2812. (org-set-autofill-regexps)
  2813. (setq indent-line-function 'org-indent-line-function)
  2814. (org-update-radio-target-regexp)
  2815. ;; Comment characters
  2816. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2817. (org-set-local 'comment-padding " ")
  2818. ;; Align options lines
  2819. (org-set-local
  2820. 'align-mode-rules-list
  2821. '((org-in-buffer-settings
  2822. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2823. (modes . '(org-mode)))))
  2824. ;; Imenu
  2825. (org-set-local 'imenu-create-index-function
  2826. 'org-imenu-get-tree)
  2827. ;; Make isearch reveal context
  2828. (if (or (featurep 'xemacs)
  2829. (not (boundp 'outline-isearch-open-invisible-function)))
  2830. ;; Emacs 21 and XEmacs make use of the hook
  2831. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2832. ;; Emacs 22 deals with this through a special variable
  2833. (org-set-local 'outline-isearch-open-invisible-function
  2834. (lambda (&rest ignore) (org-show-context 'isearch))))
  2835. ;; If empty file that did not turn on org-mode automatically, make it to.
  2836. (if (and org-insert-mode-line-in-empty-file
  2837. (interactive-p)
  2838. (= (point-min) (point-max)))
  2839. (insert "# -*- mode: org -*-\n\n"))
  2840. (unless org-inhibit-startup
  2841. (when org-startup-align-all-tables
  2842. (let ((bmp (buffer-modified-p)))
  2843. (org-table-map-tables 'org-table-align)
  2844. (set-buffer-modified-p bmp)))
  2845. (org-set-startup-visibility)))
  2846. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2847. (defun org-current-time ()
  2848. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2849. (if (> (car org-time-stamp-rounding-minutes) 1)
  2850. (let ((r (car org-time-stamp-rounding-minutes))
  2851. (time (decode-time)))
  2852. (apply 'encode-time
  2853. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2854. (nthcdr 2 time))))
  2855. (current-time)))
  2856. ;;;; Font-Lock stuff, including the activators
  2857. (defvar org-mouse-map (make-sparse-keymap))
  2858. (org-defkey org-mouse-map
  2859. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2860. (org-defkey org-mouse-map
  2861. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2862. (when org-mouse-1-follows-link
  2863. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2864. (when org-tab-follows-link
  2865. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2866. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2867. (when org-return-follows-link
  2868. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2869. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2870. (require 'font-lock)
  2871. (defconst org-non-link-chars "]\t\n\r<>")
  2872. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2873. "shell" "elisp"))
  2874. (defvar org-link-types-re nil
  2875. "Matches a link that has a url-like prefix like \"http:\"")
  2876. (defvar org-link-re-with-space nil
  2877. "Matches a link with spaces, optional angular brackets around it.")
  2878. (defvar org-link-re-with-space2 nil
  2879. "Matches a link with spaces, optional angular brackets around it.")
  2880. (defvar org-angle-link-re nil
  2881. "Matches link with angular brackets, spaces are allowed.")
  2882. (defvar org-plain-link-re nil
  2883. "Matches plain link, without spaces.")
  2884. (defvar org-bracket-link-regexp nil
  2885. "Matches a link in double brackets.")
  2886. (defvar org-bracket-link-analytic-regexp nil
  2887. "Regular expression used to analyze links.
  2888. Here is what the match groups contain after a match:
  2889. 1: http:
  2890. 2: http
  2891. 3: path
  2892. 4: [desc]
  2893. 5: desc")
  2894. (defvar org-any-link-re nil
  2895. "Regular expression matching any link.")
  2896. (defun org-make-link-regexps ()
  2897. "Update the link regular expressions.
  2898. This should be called after the variable `org-link-types' has changed."
  2899. (setq org-link-types-re
  2900. (concat
  2901. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2902. org-link-re-with-space
  2903. (concat
  2904. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2905. "\\([^" org-non-link-chars " ]"
  2906. "[^" org-non-link-chars "]*"
  2907. "[^" org-non-link-chars " ]\\)>?")
  2908. org-link-re-with-space2
  2909. (concat
  2910. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2911. "\\([^" org-non-link-chars " ]"
  2912. "[^]\t\n\r]*"
  2913. "[^" org-non-link-chars " ]\\)>?")
  2914. org-angle-link-re
  2915. (concat
  2916. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2917. "\\([^" org-non-link-chars " ]"
  2918. "[^" org-non-link-chars "]*"
  2919. "\\)>")
  2920. org-plain-link-re
  2921. (concat
  2922. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2923. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2924. org-bracket-link-regexp
  2925. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2926. org-bracket-link-analytic-regexp
  2927. (concat
  2928. "\\[\\["
  2929. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2930. "\\([^]]+\\)"
  2931. "\\]"
  2932. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2933. "\\]")
  2934. org-any-link-re
  2935. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  2936. org-angle-link-re "\\)\\|\\("
  2937. org-plain-link-re "\\)")))
  2938. (org-make-link-regexps)
  2939. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  2940. "Regular expression for fast time stamp matching.")
  2941. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  2942. "Regular expression for fast time stamp matching.")
  2943. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2944. "Regular expression matching time strings for analysis.
  2945. This one does not require the space after the date, so it can be used
  2946. on a string that terminates immediately after the date.")
  2947. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2948. "Regular expression matching time strings for analysis.")
  2949. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  2950. "Regular expression matching time stamps, with groups.")
  2951. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  2952. "Regular expression matching time stamps (also [..]), with groups.")
  2953. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2954. "Regular expression matching a time stamp range.")
  2955. (defconst org-tr-regexp-both
  2956. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  2957. "Regular expression matching a time stamp range.")
  2958. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2959. org-ts-regexp "\\)?")
  2960. "Regular expression matching a time stamp or time stamp range.")
  2961. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  2962. org-ts-regexp-both "\\)?")
  2963. "Regular expression matching a time stamp or time stamp range.
  2964. The time stamps may be either active or inactive.")
  2965. (defvar org-emph-face nil)
  2966. (defun org-do-emphasis-faces (limit)
  2967. "Run through the buffer and add overlays to links."
  2968. (let (rtn)
  2969. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  2970. (if (not (= (char-after (match-beginning 3))
  2971. (char-after (match-beginning 4))))
  2972. (progn
  2973. (setq rtn t)
  2974. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2975. 'face
  2976. (nth 1 (assoc (match-string 3)
  2977. org-emphasis-alist)))
  2978. (add-text-properties (match-beginning 2) (match-end 2)
  2979. '(font-lock-multiline t))
  2980. (when org-hide-emphasis-markers
  2981. (add-text-properties (match-end 4) (match-beginning 5)
  2982. '(invisible org-link))
  2983. (add-text-properties (match-beginning 3) (match-end 3)
  2984. '(invisible org-link)))))
  2985. (backward-char 1))
  2986. rtn))
  2987. (defun org-emphasize (&optional char)
  2988. "Insert or change an emphasis, i.e. a font like bold or italic.
  2989. If there is an active region, change that region to a new emphasis.
  2990. If there is no region, just insert the marker characters and position
  2991. the cursor between them.
  2992. CHAR should be either the marker character, or the first character of the
  2993. HTML tag associated with that emphasis. If CHAR is a space, the means
  2994. to remove the emphasis of the selected region.
  2995. If char is not given (for example in an interactive call) it
  2996. will be prompted for."
  2997. (interactive)
  2998. (let ((eal org-emphasis-alist) e det
  2999. (erc org-emphasis-regexp-components)
  3000. (prompt "")
  3001. (string "") beg end move tag c s)
  3002. (if (org-region-active-p)
  3003. (setq beg (region-beginning) end (region-end)
  3004. string (buffer-substring beg end))
  3005. (setq move t))
  3006. (while (setq e (pop eal))
  3007. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3008. c (aref tag 0))
  3009. (push (cons c (string-to-char (car e))) det)
  3010. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3011. (substring tag 1)))))
  3012. (unless char
  3013. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3014. (setq char (read-char-exclusive)))
  3015. (setq char (or (cdr (assoc char det)) char))
  3016. (if (equal char ?\ )
  3017. (setq s "" move nil)
  3018. (unless (assoc (char-to-string char) org-emphasis-alist)
  3019. (error "No such emphasis marker: \"%c\"" char))
  3020. (setq s (char-to-string char)))
  3021. (while (and (> (length string) 1)
  3022. (equal (substring string 0 1) (substring string -1))
  3023. (assoc (substring string 0 1) org-emphasis-alist))
  3024. (setq string (substring string 1 -1)))
  3025. (setq string (concat s string s))
  3026. (if beg (delete-region beg end))
  3027. (unless (or (bolp)
  3028. (string-match (concat "[" (nth 0 erc) "\n]")
  3029. (char-to-string (char-before (point)))))
  3030. (insert " "))
  3031. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3032. (char-to-string (char-after (point))))
  3033. (insert " ") (backward-char 1))
  3034. (insert string)
  3035. (and move (backward-char 1))))
  3036. (defconst org-nonsticky-props
  3037. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3038. (defun org-activate-plain-links (limit)
  3039. "Run through the buffer and add overlays to links."
  3040. (catch 'exit
  3041. (let (f)
  3042. (while (re-search-forward org-plain-link-re limit t)
  3043. (setq f (get-text-property (match-beginning 0) 'face))
  3044. (if (or (eq f 'org-tag)
  3045. (and (listp f) (memq 'org-tag f)))
  3046. nil
  3047. (add-text-properties (match-beginning 0) (match-end 0)
  3048. (list 'mouse-face 'highlight
  3049. 'rear-nonsticky org-nonsticky-props
  3050. 'keymap org-mouse-map
  3051. ))
  3052. (throw 'exit t))))))
  3053. (defun org-activate-code (limit)
  3054. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  3055. (unless (get-text-property (match-beginning 1) 'face)
  3056. (remove-text-properties (match-beginning 0) (match-end 0)
  3057. '(display t invisible t intangible t))
  3058. t)))
  3059. (defun org-activate-angle-links (limit)
  3060. "Run through the buffer and add overlays to links."
  3061. (if (re-search-forward org-angle-link-re limit t)
  3062. (progn
  3063. (add-text-properties (match-beginning 0) (match-end 0)
  3064. (list 'mouse-face 'highlight
  3065. 'rear-nonsticky org-nonsticky-props
  3066. 'keymap org-mouse-map
  3067. ))
  3068. t)))
  3069. (defun org-activate-bracket-links (limit)
  3070. "Run through the buffer and add overlays to bracketed links."
  3071. (if (re-search-forward org-bracket-link-regexp limit t)
  3072. (let* ((help (concat "LINK: "
  3073. (org-match-string-no-properties 1)))
  3074. ;; FIXME: above we should remove the escapes.
  3075. ;; but that requires another match, protecting match data,
  3076. ;; a lot of overhead for font-lock.
  3077. (ip (org-maybe-intangible
  3078. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3079. 'keymap org-mouse-map 'mouse-face 'highlight
  3080. 'font-lock-multiline t 'help-echo help)))
  3081. (vp (list 'rear-nonsticky org-nonsticky-props
  3082. 'keymap org-mouse-map 'mouse-face 'highlight
  3083. ' font-lock-multiline t 'help-echo help)))
  3084. ;; We need to remove the invisible property here. Table narrowing
  3085. ;; may have made some of this invisible.
  3086. (remove-text-properties (match-beginning 0) (match-end 0)
  3087. '(invisible nil))
  3088. (if (match-end 3)
  3089. (progn
  3090. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3091. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3092. (add-text-properties (match-end 3) (match-end 0) ip))
  3093. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3094. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3095. (add-text-properties (match-end 1) (match-end 0) ip))
  3096. t)))
  3097. (defun org-activate-dates (limit)
  3098. "Run through the buffer and add overlays to dates."
  3099. (if (re-search-forward org-tsr-regexp-both limit t)
  3100. (progn
  3101. (add-text-properties (match-beginning 0) (match-end 0)
  3102. (list 'mouse-face 'highlight
  3103. 'rear-nonsticky org-nonsticky-props
  3104. 'keymap org-mouse-map))
  3105. (when org-display-custom-times
  3106. (if (match-end 3)
  3107. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3108. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3109. t)))
  3110. (defvar org-target-link-regexp nil
  3111. "Regular expression matching radio targets in plain text.")
  3112. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3113. "Regular expression matching a link target.")
  3114. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3115. "Regular expression matching a radio target.")
  3116. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3117. "Regular expression matching any target.")
  3118. (defun org-activate-target-links (limit)
  3119. "Run through the buffer and add overlays to target matches."
  3120. (when org-target-link-regexp
  3121. (let ((case-fold-search t))
  3122. (if (re-search-forward org-target-link-regexp limit t)
  3123. (progn
  3124. (add-text-properties (match-beginning 0) (match-end 0)
  3125. (list 'mouse-face 'highlight
  3126. 'rear-nonsticky org-nonsticky-props
  3127. 'keymap org-mouse-map
  3128. 'help-echo "Radio target link"
  3129. 'org-linked-text t))
  3130. t)))))
  3131. (defun org-update-radio-target-regexp ()
  3132. "Find all radio targets in this file and update the regular expression."
  3133. (interactive)
  3134. (when (memq 'radio org-activate-links)
  3135. (setq org-target-link-regexp
  3136. (org-make-target-link-regexp (org-all-targets 'radio)))
  3137. (org-restart-font-lock)))
  3138. (defun org-hide-wide-columns (limit)
  3139. (let (s e)
  3140. (setq s (text-property-any (point) (or limit (point-max))
  3141. 'org-cwidth t))
  3142. (when s
  3143. (setq e (next-single-property-change s 'org-cwidth))
  3144. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3145. (goto-char e)
  3146. t)))
  3147. (defvar org-latex-and-specials-regexp nil
  3148. "Regular expression for highlighting export special stuff.")
  3149. (defvar org-match-substring-regexp)
  3150. (defvar org-match-substring-with-braces-regexp)
  3151. (defvar org-export-html-special-string-regexps)
  3152. (defun org-compute-latex-and-specials-regexp ()
  3153. "Compute regular expression for stuff treated specially by exporters."
  3154. (if (not org-highlight-latex-fragments-and-specials)
  3155. (org-set-local 'org-latex-and-specials-regexp nil)
  3156. (require 'org-exp)
  3157. (let*
  3158. ((matchers (plist-get org-format-latex-options :matchers))
  3159. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3160. org-latex-regexps)))
  3161. (options (org-combine-plists (org-default-export-plist)
  3162. (org-infile-export-plist)))
  3163. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3164. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3165. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3166. (org-export-html-expand (plist-get options :expand-quoted-html))
  3167. (org-export-with-special-strings (plist-get options :special-strings))
  3168. (re-sub
  3169. (cond
  3170. ((equal org-export-with-sub-superscripts '{})
  3171. (list org-match-substring-with-braces-regexp))
  3172. (org-export-with-sub-superscripts
  3173. (list org-match-substring-regexp))
  3174. (t nil)))
  3175. (re-latex
  3176. (if org-export-with-LaTeX-fragments
  3177. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3178. (re-macros
  3179. (if org-export-with-TeX-macros
  3180. (list (concat "\\\\"
  3181. (regexp-opt
  3182. (append (mapcar 'car org-html-entities)
  3183. (if (boundp 'org-latex-entities)
  3184. org-latex-entities nil))
  3185. 'words))) ; FIXME
  3186. ))
  3187. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3188. (re-special (if org-export-with-special-strings
  3189. (mapcar (lambda (x) (car x))
  3190. org-export-html-special-string-regexps)))
  3191. (re-rest
  3192. (delq nil
  3193. (list
  3194. (if org-export-html-expand "@<[^>\n]+>")
  3195. ))))
  3196. (org-set-local
  3197. 'org-latex-and-specials-regexp
  3198. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3199. re-rest) "\\|")))))
  3200. (defun org-do-latex-and-special-faces (limit)
  3201. "Run through the buffer and add overlays to links."
  3202. (when org-latex-and-specials-regexp
  3203. (let (rtn d)
  3204. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3205. limit t))
  3206. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3207. 'face))
  3208. '(org-code org-verbatim underline)))
  3209. (progn
  3210. (setq rtn t
  3211. d (cond ((member (char-after (1+ (match-beginning 0)))
  3212. '(?_ ?^)) 1)
  3213. (t 0)))
  3214. (font-lock-prepend-text-property
  3215. (+ d (match-beginning 0)) (match-end 0)
  3216. 'face 'org-latex-and-export-specials)
  3217. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3218. '(font-lock-multiline t)))))
  3219. rtn)))
  3220. (defun org-restart-font-lock ()
  3221. "Restart font-lock-mode, to force refontification."
  3222. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3223. (font-lock-mode -1)
  3224. (font-lock-mode 1)))
  3225. (defun org-all-targets (&optional radio)
  3226. "Return a list of all targets in this file.
  3227. With optional argument RADIO, only find radio targets."
  3228. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3229. rtn)
  3230. (save-excursion
  3231. (goto-char (point-min))
  3232. (while (re-search-forward re nil t)
  3233. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3234. rtn)))
  3235. (defun org-make-target-link-regexp (targets)
  3236. "Make regular expression matching all strings in TARGETS.
  3237. The regular expression finds the targets also if there is a line break
  3238. between words."
  3239. (and targets
  3240. (concat
  3241. "\\<\\("
  3242. (mapconcat
  3243. (lambda (x)
  3244. (while (string-match " +" x)
  3245. (setq x (replace-match "\\s-+" t t x)))
  3246. x)
  3247. targets
  3248. "\\|")
  3249. "\\)\\>")))
  3250. (defun org-activate-tags (limit)
  3251. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3252. (progn
  3253. (add-text-properties (match-beginning 1) (match-end 1)
  3254. (list 'mouse-face 'highlight
  3255. 'rear-nonsticky org-nonsticky-props
  3256. 'keymap org-mouse-map))
  3257. t)))
  3258. (defun org-outline-level ()
  3259. (save-excursion
  3260. (looking-at outline-regexp)
  3261. (if (match-beginning 1)
  3262. (+ (org-get-string-indentation (match-string 1)) 1000)
  3263. (1- (- (match-end 0) (match-beginning 0))))))
  3264. (defvar org-font-lock-keywords nil)
  3265. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3266. "Regular expression matching a property line.")
  3267. (defvar org-font-lock-hook nil
  3268. "Functions to be called for special font lock stuff.")
  3269. (defun org-font-lock-hook (limit)
  3270. (run-hook-with-args 'org-font-lock-hook limit))
  3271. (defun org-set-font-lock-defaults ()
  3272. (let* ((em org-fontify-emphasized-text)
  3273. (lk org-activate-links)
  3274. (org-font-lock-extra-keywords
  3275. (list
  3276. ;; Call the hook
  3277. '(org-font-lock-hook)
  3278. ;; Headlines
  3279. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3280. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3281. ;; Table lines
  3282. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3283. (1 'org-table t))
  3284. ;; Table internals
  3285. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3286. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3287. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3288. ;; Drawers
  3289. (list org-drawer-regexp '(0 'org-special-keyword t))
  3290. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3291. ;; Properties
  3292. (list org-property-re
  3293. '(1 'org-special-keyword t)
  3294. '(3 'org-property-value t))
  3295. (if org-format-transports-properties-p
  3296. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3297. ;; Links
  3298. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3299. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3300. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3301. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3302. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3303. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3304. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3305. '(org-hide-wide-columns (0 nil append))
  3306. ;; TODO lines
  3307. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3308. '(1 (org-get-todo-face 1) t))
  3309. ;; DONE
  3310. (if org-fontify-done-headline
  3311. (list (concat "^[*]+ +\\<\\("
  3312. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3313. "\\)\\(.*\\)")
  3314. '(2 'org-headline-done t))
  3315. nil)
  3316. ;; Priorities
  3317. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3318. ;; Special keywords
  3319. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3320. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3321. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3322. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3323. ;; Emphasis
  3324. (if em
  3325. (if (featurep 'xemacs)
  3326. '(org-do-emphasis-faces (0 nil append))
  3327. '(org-do-emphasis-faces)))
  3328. ;; Checkboxes
  3329. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3330. 2 'bold prepend)
  3331. (if org-provide-checkbox-statistics
  3332. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3333. (0 (org-get-checkbox-statistics-face) t)))
  3334. ;; Description list items
  3335. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3336. 2 'bold prepend)
  3337. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3338. '(1 'org-archived prepend))
  3339. ;; Specials
  3340. '(org-do-latex-and-special-faces)
  3341. ;; Code
  3342. '(org-activate-code (1 'org-code t))
  3343. ;; COMMENT
  3344. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3345. "\\|" org-quote-string "\\)\\>")
  3346. '(1 'org-special-keyword t))
  3347. '("^#.*" (0 'font-lock-comment-face t))
  3348. )))
  3349. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3350. ;; Now set the full font-lock-keywords
  3351. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3352. (org-set-local 'font-lock-defaults
  3353. '(org-font-lock-keywords t nil nil backward-paragraph))
  3354. (kill-local-variable 'font-lock-keywords) nil))
  3355. (defvar org-m nil)
  3356. (defvar org-l nil)
  3357. (defvar org-f nil)
  3358. (defun org-get-level-face (n)
  3359. "Get the right face for match N in font-lock matching of healdines."
  3360. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3361. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3362. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3363. (cond
  3364. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3365. ((eq n 2) org-f)
  3366. (t (if org-level-color-stars-only nil org-f))))
  3367. (defun org-get-todo-face (kwd)
  3368. "Get the right face for a TODO keyword KWD.
  3369. If KWD is a number, get the corresponding match group."
  3370. (if (numberp kwd) (setq kwd (match-string kwd)))
  3371. (or (cdr (assoc kwd org-todo-keyword-faces))
  3372. (and (member kwd org-done-keywords) 'org-done)
  3373. 'org-todo))
  3374. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3375. "Remove fontification and activation overlays from links."
  3376. (font-lock-default-unfontify-region beg end)
  3377. (let* ((buffer-undo-list t)
  3378. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3379. (inhibit-modification-hooks t)
  3380. deactivate-mark buffer-file-name buffer-file-truename)
  3381. (remove-text-properties beg end
  3382. '(mouse-face t keymap t org-linked-text t
  3383. invisible t intangible t))))
  3384. ;;;; Visibility cycling, including org-goto and indirect buffer
  3385. ;;; Cycling
  3386. (defvar org-cycle-global-status nil)
  3387. (make-variable-buffer-local 'org-cycle-global-status)
  3388. (defvar org-cycle-subtree-status nil)
  3389. (make-variable-buffer-local 'org-cycle-subtree-status)
  3390. ;;;###autoload
  3391. (defun org-cycle (&optional arg)
  3392. "Visibility cycling for Org-mode.
  3393. - When this function is called with a prefix argument, rotate the entire
  3394. buffer through 3 states (global cycling)
  3395. 1. OVERVIEW: Show only top-level headlines.
  3396. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3397. 3. SHOW ALL: Show everything.
  3398. When called with two C-c C-u prefixes, switch to the startup visibility,
  3399. determined by the variable `org-startup-folded', and by any VISIBILITY
  3400. properties in the buffer.
  3401. - When point is at the beginning of a headline, rotate the subtree started
  3402. by this line through 3 different states (local cycling)
  3403. 1. FOLDED: Only the main headline is shown.
  3404. 2. CHILDREN: The main headline and the direct children are shown.
  3405. From this state, you can move to one of the children
  3406. and zoom in further.
  3407. 3. SUBTREE: Show the entire subtree, including body text.
  3408. - When there is a numeric prefix, go up to a heading with level ARG, do
  3409. a `show-subtree' and return to the previous cursor position. If ARG
  3410. is negative, go up that many levels.
  3411. - When point is not at the beginning of a headline, execute the global
  3412. binding for TAB, which is re-indenting the line. See the option
  3413. `org-cycle-emulate-tab' for details.
  3414. - Special case: if point is at the beginning of the buffer and there is
  3415. no headline in line 1, this function will act as if called with prefix arg.
  3416. But only if also the variable `org-cycle-global-at-bob' is t."
  3417. (interactive "P")
  3418. (org-load-modules-maybe)
  3419. (let* ((outline-regexp
  3420. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3421. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3422. outline-regexp))
  3423. (bob-special (and org-cycle-global-at-bob (bobp)
  3424. (not (looking-at outline-regexp))))
  3425. (org-cycle-hook
  3426. (if bob-special
  3427. (delq 'org-optimize-window-after-visibility-change
  3428. (copy-sequence org-cycle-hook))
  3429. org-cycle-hook))
  3430. (pos (point)))
  3431. (if (or bob-special (equal arg '(4)))
  3432. ;; special case: use global cycling
  3433. (setq arg t))
  3434. (cond
  3435. ((equal arg '(16))
  3436. (org-set-startup-visibility)
  3437. (message "Startup visibility, plus VISIBILITY properties."))
  3438. ((org-at-table-p 'any)
  3439. ;; Enter the table or move to the next field in the table
  3440. (or (org-table-recognize-table.el)
  3441. (progn
  3442. (if arg (org-table-edit-field t)
  3443. (org-table-justify-field-maybe)
  3444. (call-interactively 'org-table-next-field)))))
  3445. ((eq arg t) ;; Global cycling
  3446. (cond
  3447. ((and (eq last-command this-command)
  3448. (eq org-cycle-global-status 'overview))
  3449. ;; We just created the overview - now do table of contents
  3450. ;; This can be slow in very large buffers, so indicate action
  3451. (message "CONTENTS...")
  3452. (org-content)
  3453. (message "CONTENTS...done")
  3454. (setq org-cycle-global-status 'contents)
  3455. (run-hook-with-args 'org-cycle-hook 'contents))
  3456. ((and (eq last-command this-command)
  3457. (eq org-cycle-global-status 'contents))
  3458. ;; We just showed the table of contents - now show everything
  3459. (show-all)
  3460. (message "SHOW ALL")
  3461. (setq org-cycle-global-status 'all)
  3462. (run-hook-with-args 'org-cycle-hook 'all))
  3463. (t
  3464. ;; Default action: go to overview
  3465. (org-overview)
  3466. (message "OVERVIEW")
  3467. (setq org-cycle-global-status 'overview)
  3468. (run-hook-with-args 'org-cycle-hook 'overview))))
  3469. ((and org-drawers org-drawer-regexp
  3470. (save-excursion
  3471. (beginning-of-line 1)
  3472. (looking-at org-drawer-regexp)))
  3473. ;; Toggle block visibility
  3474. (org-flag-drawer
  3475. (not (get-char-property (match-end 0) 'invisible))))
  3476. ((integerp arg)
  3477. ;; Show-subtree, ARG levels up from here.
  3478. (save-excursion
  3479. (org-back-to-heading)
  3480. (outline-up-heading (if (< arg 0) (- arg)
  3481. (- (funcall outline-level) arg)))
  3482. (org-show-subtree)))
  3483. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3484. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3485. ;; At a heading: rotate between three different views
  3486. (org-back-to-heading)
  3487. (let ((goal-column 0) eoh eol eos)
  3488. ;; First, some boundaries
  3489. (save-excursion
  3490. (org-back-to-heading)
  3491. (save-excursion
  3492. (beginning-of-line 2)
  3493. (while (and (not (eobp)) ;; this is like `next-line'
  3494. (get-char-property (1- (point)) 'invisible))
  3495. (beginning-of-line 2)) (setq eol (point)))
  3496. (outline-end-of-heading) (setq eoh (point))
  3497. (org-end-of-subtree t)
  3498. (unless (eobp)
  3499. (skip-chars-forward " \t\n")
  3500. (beginning-of-line 1) ; in case this is an item
  3501. )
  3502. (setq eos (1- (point))))
  3503. ;; Find out what to do next and set `this-command'
  3504. (cond
  3505. ((= eos eoh)
  3506. ;; Nothing is hidden behind this heading
  3507. (message "EMPTY ENTRY")
  3508. (setq org-cycle-subtree-status nil)
  3509. (save-excursion
  3510. (goto-char eos)
  3511. (outline-next-heading)
  3512. (if (org-invisible-p) (org-flag-heading nil))))
  3513. ((or (>= eol eos)
  3514. (not (string-match "\\S-" (buffer-substring eol eos))))
  3515. ;; Entire subtree is hidden in one line: open it
  3516. (org-show-entry)
  3517. (show-children)
  3518. (message "CHILDREN")
  3519. (save-excursion
  3520. (goto-char eos)
  3521. (outline-next-heading)
  3522. (if (org-invisible-p) (org-flag-heading nil)))
  3523. (setq org-cycle-subtree-status 'children)
  3524. (run-hook-with-args 'org-cycle-hook 'children))
  3525. ((and (eq last-command this-command)
  3526. (eq org-cycle-subtree-status 'children))
  3527. ;; We just showed the children, now show everything.
  3528. (org-show-subtree)
  3529. (message "SUBTREE")
  3530. (setq org-cycle-subtree-status 'subtree)
  3531. (run-hook-with-args 'org-cycle-hook 'subtree))
  3532. (t
  3533. ;; Default action: hide the subtree.
  3534. (hide-subtree)
  3535. (message "FOLDED")
  3536. (setq org-cycle-subtree-status 'folded)
  3537. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3538. ;; TAB emulation and template completion
  3539. (buffer-read-only (org-back-to-heading))
  3540. ((org-try-structure-completion))
  3541. ((org-try-cdlatex-tab))
  3542. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3543. (or (not (bolp))
  3544. (not (looking-at outline-regexp))))
  3545. (call-interactively (global-key-binding "\t")))
  3546. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3547. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3548. (or (and (eq org-cycle-emulate-tab 'white)
  3549. (= (match-end 0) (point-at-eol)))
  3550. (and (eq org-cycle-emulate-tab 'whitestart)
  3551. (>= (match-end 0) pos))))
  3552. t
  3553. (eq org-cycle-emulate-tab t))
  3554. (call-interactively (global-key-binding "\t")))
  3555. (t (save-excursion
  3556. (org-back-to-heading)
  3557. (org-cycle))))))
  3558. ;;;###autoload
  3559. (defun org-global-cycle (&optional arg)
  3560. "Cycle the global visibility. For details see `org-cycle'.
  3561. With C-u prefix arg, switch to startup visibility.
  3562. With a numeric prefix, show all headlines up to that level."
  3563. (interactive "P")
  3564. (let ((org-cycle-include-plain-lists
  3565. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3566. (cond
  3567. ((integerp arg)
  3568. (show-all)
  3569. (hide-sublevels arg)
  3570. (setq org-cycle-global-status 'contents))
  3571. ((equal arg '(4))
  3572. (org-set-startup-visibility)
  3573. (message "Startup visibility, plus VISIBILITY properties."))
  3574. (t
  3575. (org-cycle '(4))))))
  3576. (defun org-set-startup-visibility ()
  3577. "Set the visibility required by startup options and properties."
  3578. (cond
  3579. ((eq org-startup-folded t)
  3580. (org-cycle '(4)))
  3581. ((eq org-startup-folded 'content)
  3582. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3583. (org-cycle '(4)) (org-cycle '(4)))))
  3584. (org-set-visibility-according-to-property 'no-cleanup)
  3585. (org-cycle-hide-archived-subtrees 'all)
  3586. (org-cycle-hide-drawers 'all)
  3587. (org-cycle-show-empty-lines 'all))
  3588. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3589. "Switch subtree visibilities according to :VISIBILITY: property."
  3590. (interactive)
  3591. (let (state)
  3592. (save-excursion
  3593. (goto-char (point-min))
  3594. (while (re-search-forward
  3595. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3596. nil t)
  3597. (setq state (match-string 1))
  3598. (save-excursion
  3599. (org-back-to-heading t)
  3600. (hide-subtree)
  3601. (org-reveal)
  3602. (cond
  3603. ((equal state '("fold" "folded"))
  3604. (hide-subtree))
  3605. ((equal state "children")
  3606. (org-show-hidden-entry)
  3607. (show-children))
  3608. ((equal state "content")
  3609. (save-excursion
  3610. (save-restriction
  3611. (org-narrow-to-subtree)
  3612. (org-content))))
  3613. ((member state '("all" "showall"))
  3614. (show-subtree)))))
  3615. (unless no-cleanup
  3616. (org-cycle-hide-archived-subtrees 'all)
  3617. (org-cycle-hide-drawers 'all)
  3618. (org-cycle-show-empty-lines 'all)))))
  3619. (defun org-overview ()
  3620. "Switch to overview mode, shoing only top-level headlines.
  3621. Really, this shows all headlines with level equal or greater than the level
  3622. of the first headline in the buffer. This is important, because if the
  3623. first headline is not level one, then (hide-sublevels 1) gives confusing
  3624. results."
  3625. (interactive)
  3626. (let ((level (save-excursion
  3627. (goto-char (point-min))
  3628. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3629. (progn
  3630. (goto-char (match-beginning 0))
  3631. (funcall outline-level))))))
  3632. (and level (hide-sublevels level))))
  3633. (defun org-content (&optional arg)
  3634. "Show all headlines in the buffer, like a table of contents.
  3635. With numerical argument N, show content up to level N."
  3636. (interactive "P")
  3637. (save-excursion
  3638. ;; Visit all headings and show their offspring
  3639. (and (integerp arg) (org-overview))
  3640. (goto-char (point-max))
  3641. (catch 'exit
  3642. (while (and (progn (condition-case nil
  3643. (outline-previous-visible-heading 1)
  3644. (error (goto-char (point-min))))
  3645. t)
  3646. (looking-at outline-regexp))
  3647. (if (integerp arg)
  3648. (show-children (1- arg))
  3649. (show-branches))
  3650. (if (bobp) (throw 'exit nil))))))
  3651. (defun org-optimize-window-after-visibility-change (state)
  3652. "Adjust the window after a change in outline visibility.
  3653. This function is the default value of the hook `org-cycle-hook'."
  3654. (when (get-buffer-window (current-buffer))
  3655. (cond
  3656. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3657. ; ((eq state 'overview) (org-beginning-of-line))
  3658. ((eq state 'content) nil)
  3659. ((eq state 'all) nil)
  3660. ((eq state 'folded) nil)
  3661. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3662. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3663. (defun org-compact-display-after-subtree-move ()
  3664. (let (beg end)
  3665. (save-excursion
  3666. (if (org-up-heading-safe)
  3667. (progn
  3668. (hide-subtree)
  3669. (show-entry)
  3670. (show-children)
  3671. (org-cycle-show-empty-lines 'children)
  3672. (org-cycle-hide-drawers 'children))
  3673. (org-overview)))))
  3674. (defun org-cycle-show-empty-lines (state)
  3675. "Show empty lines above all visible headlines.
  3676. The region to be covered depends on STATE when called through
  3677. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3678. entire buffer covered. Note that an empty line is only shown if there
  3679. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3680. (when (> org-cycle-separator-lines 0)
  3681. (save-excursion
  3682. (let* ((n org-cycle-separator-lines)
  3683. (re (cond
  3684. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3685. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3686. (t (let ((ns (number-to-string (- n 2))))
  3687. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3688. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3689. beg end)
  3690. (cond
  3691. ((memq state '(overview contents t))
  3692. (setq beg (point-min) end (point-max)))
  3693. ((memq state '(children folded))
  3694. (setq beg (point) end (progn (org-end-of-subtree t t)
  3695. (beginning-of-line 2)
  3696. (point)))))
  3697. (when beg
  3698. (goto-char beg)
  3699. (while (re-search-forward re end t)
  3700. (if (not (get-char-property (match-end 1) 'invisible))
  3701. (outline-flag-region
  3702. (match-beginning 1) (match-end 1) nil)))))))
  3703. ;; Never hide empty lines at the end of the file.
  3704. (save-excursion
  3705. (goto-char (point-max))
  3706. (outline-previous-heading)
  3707. (outline-end-of-heading)
  3708. (if (and (looking-at "[ \t\n]+")
  3709. (= (match-end 0) (point-max)))
  3710. (outline-flag-region (point) (match-end 0) nil))))
  3711. (defun org-cycle-hide-drawers (state)
  3712. "Re-hide all drawers after a visibility state change."
  3713. (when (and (org-mode-p)
  3714. (not (memq state '(overview folded))))
  3715. (save-excursion
  3716. (let* ((globalp (memq state '(contents all)))
  3717. (beg (if globalp (point-min) (point)))
  3718. (end (if globalp (point-max) (org-end-of-subtree t))))
  3719. (goto-char beg)
  3720. (while (re-search-forward org-drawer-regexp end t)
  3721. (org-flag-drawer t))))))
  3722. (defun org-flag-drawer (flag)
  3723. (save-excursion
  3724. (beginning-of-line 1)
  3725. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3726. (let ((b (match-end 0))
  3727. (outline-regexp org-outline-regexp))
  3728. (if (re-search-forward
  3729. "^[ \t]*:END:"
  3730. (save-excursion (outline-next-heading) (point)) t)
  3731. (outline-flag-region b (point-at-eol) flag)
  3732. (error ":END: line missing"))))))
  3733. (defun org-subtree-end-visible-p ()
  3734. "Is the end of the current subtree visible?"
  3735. (pos-visible-in-window-p
  3736. (save-excursion (org-end-of-subtree t) (point))))
  3737. (defun org-first-headline-recenter (&optional N)
  3738. "Move cursor to the first headline and recenter the headline.
  3739. Optional argument N means, put the headline into the Nth line of the window."
  3740. (goto-char (point-min))
  3741. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3742. (beginning-of-line)
  3743. (recenter (prefix-numeric-value N))))
  3744. ;;; Org-goto
  3745. (defvar org-goto-window-configuration nil)
  3746. (defvar org-goto-marker nil)
  3747. (defvar org-goto-map
  3748. (let ((map (make-sparse-keymap)))
  3749. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3750. (while (setq cmd (pop cmds))
  3751. (substitute-key-definition cmd cmd map global-map)))
  3752. (suppress-keymap map)
  3753. (org-defkey map "\C-m" 'org-goto-ret)
  3754. (org-defkey map [(return)] 'org-goto-ret)
  3755. (org-defkey map [(left)] 'org-goto-left)
  3756. (org-defkey map [(right)] 'org-goto-right)
  3757. (org-defkey map [(control ?g)] 'org-goto-quit)
  3758. (org-defkey map "\C-i" 'org-cycle)
  3759. (org-defkey map [(tab)] 'org-cycle)
  3760. (org-defkey map [(down)] 'outline-next-visible-heading)
  3761. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3762. (if org-goto-auto-isearch
  3763. (if (fboundp 'define-key-after)
  3764. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3765. nil)
  3766. (org-defkey map "q" 'org-goto-quit)
  3767. (org-defkey map "n" 'outline-next-visible-heading)
  3768. (org-defkey map "p" 'outline-previous-visible-heading)
  3769. (org-defkey map "f" 'outline-forward-same-level)
  3770. (org-defkey map "b" 'outline-backward-same-level)
  3771. (org-defkey map "u" 'outline-up-heading))
  3772. (org-defkey map "/" 'org-occur)
  3773. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3774. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3775. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3776. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3777. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3778. map))
  3779. (defconst org-goto-help
  3780. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3781. RET=jump to location [Q]uit and return to previous location
  3782. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3783. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3784. ;; FIXME: Docstring doe not mention both interfaces
  3785. (defun org-goto (&optional alternative-interface)
  3786. "Look up a different location in the current file, keeping current visibility.
  3787. When you want look-up or go to a different location in a document, the
  3788. fastest way is often to fold the entire buffer and then dive into the tree.
  3789. This method has the disadvantage, that the previous location will be folded,
  3790. which may not be what you want.
  3791. This command works around this by showing a copy of the current buffer
  3792. in an indirect buffer, in overview mode. You can dive into the tree in
  3793. that copy, use org-occur and incremental search to find a location.
  3794. When pressing RET or `Q', the command returns to the original buffer in
  3795. which the visibility is still unchanged. After RET is will also jump to
  3796. the location selected in the indirect buffer and expose the
  3797. the headline hierarchy above."
  3798. (interactive "P")
  3799. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3800. (org-refile-use-outline-path t)
  3801. (interface
  3802. (if (not alternative-interface)
  3803. org-goto-interface
  3804. (if (eq org-goto-interface 'outline)
  3805. 'outline-path-completion
  3806. 'outline)))
  3807. (org-goto-start-pos (point))
  3808. (selected-point
  3809. (if (eq interface 'outline)
  3810. (car (org-get-location (current-buffer) org-goto-help))
  3811. (nth 3 (org-refile-get-location "Goto: ")))))
  3812. (if selected-point
  3813. (progn
  3814. (org-mark-ring-push org-goto-start-pos)
  3815. (goto-char selected-point)
  3816. (if (or (org-invisible-p) (org-invisible-p2))
  3817. (org-show-context 'org-goto)))
  3818. (message "Quit"))))
  3819. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3820. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3821. (defvar org-goto-local-auto-isearch-map) ; defined below
  3822. (defun org-get-location (buf help)
  3823. "Let the user select a location in the Org-mode buffer BUF.
  3824. This function uses a recursive edit. It returns the selected position
  3825. or nil."
  3826. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3827. (isearch-hide-immediately nil)
  3828. (isearch-search-fun-function
  3829. (lambda () 'org-goto-local-search-forward-headings))
  3830. (org-goto-selected-point org-goto-exit-command))
  3831. (save-excursion
  3832. (save-window-excursion
  3833. (delete-other-windows)
  3834. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3835. (switch-to-buffer
  3836. (condition-case nil
  3837. (make-indirect-buffer (current-buffer) "*org-goto*")
  3838. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3839. (with-output-to-temp-buffer "*Help*"
  3840. (princ help))
  3841. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3842. (setq buffer-read-only nil)
  3843. (let ((org-startup-truncated t)
  3844. (org-startup-folded nil)
  3845. (org-startup-align-all-tables nil))
  3846. (org-mode)
  3847. (org-overview))
  3848. (setq buffer-read-only t)
  3849. (if (and (boundp 'org-goto-start-pos)
  3850. (integer-or-marker-p org-goto-start-pos))
  3851. (let ((org-show-hierarchy-above t)
  3852. (org-show-siblings t)
  3853. (org-show-following-heading t))
  3854. (goto-char org-goto-start-pos)
  3855. (and (org-invisible-p) (org-show-context)))
  3856. (goto-char (point-min)))
  3857. (org-beginning-of-line)
  3858. (message "Select location and press RET")
  3859. (use-local-map org-goto-map)
  3860. (recursive-edit)
  3861. ))
  3862. (kill-buffer "*org-goto*")
  3863. (cons org-goto-selected-point org-goto-exit-command)))
  3864. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3865. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3866. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3867. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3868. (defun org-goto-local-search-forward-headings (string bound noerror)
  3869. "Search and make sure that anu matches are in headlines."
  3870. (catch 'return
  3871. (while (search-forward string bound noerror)
  3872. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3873. (and (member :headline context)
  3874. (not (member :tags context))))
  3875. (throw 'return (point))))))
  3876. (defun org-goto-local-auto-isearch ()
  3877. "Start isearch."
  3878. (interactive)
  3879. (goto-char (point-min))
  3880. (let ((keys (this-command-keys)))
  3881. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3882. (isearch-mode t)
  3883. (isearch-process-search-char (string-to-char keys)))))
  3884. (defun org-goto-ret (&optional arg)
  3885. "Finish `org-goto' by going to the new location."
  3886. (interactive "P")
  3887. (setq org-goto-selected-point (point)
  3888. org-goto-exit-command 'return)
  3889. (throw 'exit nil))
  3890. (defun org-goto-left ()
  3891. "Finish `org-goto' by going to the new location."
  3892. (interactive)
  3893. (if (org-on-heading-p)
  3894. (progn
  3895. (beginning-of-line 1)
  3896. (setq org-goto-selected-point (point)
  3897. org-goto-exit-command 'left)
  3898. (throw 'exit nil))
  3899. (error "Not on a heading")))
  3900. (defun org-goto-right ()
  3901. "Finish `org-goto' by going to the new location."
  3902. (interactive)
  3903. (if (org-on-heading-p)
  3904. (progn
  3905. (setq org-goto-selected-point (point)
  3906. org-goto-exit-command 'right)
  3907. (throw 'exit nil))
  3908. (error "Not on a heading")))
  3909. (defun org-goto-quit ()
  3910. "Finish `org-goto' without cursor motion."
  3911. (interactive)
  3912. (setq org-goto-selected-point nil)
  3913. (setq org-goto-exit-command 'quit)
  3914. (throw 'exit nil))
  3915. ;;; Indirect buffer display of subtrees
  3916. (defvar org-indirect-dedicated-frame nil
  3917. "This is the frame being used for indirect tree display.")
  3918. (defvar org-last-indirect-buffer nil)
  3919. (defun org-tree-to-indirect-buffer (&optional arg)
  3920. "Create indirect buffer and narrow it to current subtree.
  3921. With numerical prefix ARG, go up to this level and then take that tree.
  3922. If ARG is negative, go up that many levels.
  3923. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  3924. indirect buffer previously made with this command, to avoid proliferation of
  3925. indirect buffers. However, when you call the command with a `C-u' prefix, or
  3926. when `org-indirect-buffer-display' is `new-frame', the last buffer
  3927. is kept so that you can work with several indirect buffers at the same time.
  3928. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  3929. requests that a new frame be made for the new buffer, so that the dedicated
  3930. frame is not changed."
  3931. (interactive "P")
  3932. (let ((cbuf (current-buffer))
  3933. (cwin (selected-window))
  3934. (pos (point))
  3935. beg end level heading ibuf)
  3936. (save-excursion
  3937. (org-back-to-heading t)
  3938. (when (numberp arg)
  3939. (setq level (org-outline-level))
  3940. (if (< arg 0) (setq arg (+ level arg)))
  3941. (while (> (setq level (org-outline-level)) arg)
  3942. (outline-up-heading 1 t)))
  3943. (setq beg (point)
  3944. heading (org-get-heading))
  3945. (org-end-of-subtree t) (setq end (point)))
  3946. (if (and (buffer-live-p org-last-indirect-buffer)
  3947. (not (eq org-indirect-buffer-display 'new-frame))
  3948. (not arg))
  3949. (kill-buffer org-last-indirect-buffer))
  3950. (setq ibuf (org-get-indirect-buffer cbuf)
  3951. org-last-indirect-buffer ibuf)
  3952. (cond
  3953. ((or (eq org-indirect-buffer-display 'new-frame)
  3954. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  3955. (select-frame (make-frame))
  3956. (delete-other-windows)
  3957. (switch-to-buffer ibuf)
  3958. (org-set-frame-title heading))
  3959. ((eq org-indirect-buffer-display 'dedicated-frame)
  3960. (raise-frame
  3961. (select-frame (or (and org-indirect-dedicated-frame
  3962. (frame-live-p org-indirect-dedicated-frame)
  3963. org-indirect-dedicated-frame)
  3964. (setq org-indirect-dedicated-frame (make-frame)))))
  3965. (delete-other-windows)
  3966. (switch-to-buffer ibuf)
  3967. (org-set-frame-title (concat "Indirect: " heading)))
  3968. ((eq org-indirect-buffer-display 'current-window)
  3969. (switch-to-buffer ibuf))
  3970. ((eq org-indirect-buffer-display 'other-window)
  3971. (pop-to-buffer ibuf))
  3972. (t (error "Invalid value.")))
  3973. (if (featurep 'xemacs)
  3974. (save-excursion (org-mode) (turn-on-font-lock)))
  3975. (narrow-to-region beg end)
  3976. (show-all)
  3977. (goto-char pos)
  3978. (and (window-live-p cwin) (select-window cwin))))
  3979. (defun org-get-indirect-buffer (&optional buffer)
  3980. (setq buffer (or buffer (current-buffer)))
  3981. (let ((n 1) (base (buffer-name buffer)) bname)
  3982. (while (buffer-live-p
  3983. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  3984. (setq n (1+ n)))
  3985. (condition-case nil
  3986. (make-indirect-buffer buffer bname 'clone)
  3987. (error (make-indirect-buffer buffer bname)))))
  3988. (defun org-set-frame-title (title)
  3989. "Set the title of the current frame to the string TITLE."
  3990. ;; FIXME: how to name a single frame in XEmacs???
  3991. (unless (featurep 'xemacs)
  3992. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  3993. ;;;; Structure editing
  3994. ;;; Inserting headlines
  3995. (defun org-insert-heading (&optional force-heading)
  3996. "Insert a new heading or item with same depth at point.
  3997. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3998. If point is at the beginning of a headline, insert a sibling before the
  3999. current headline. If point is not at the beginning, do not split the line,
  4000. but create the new hedline after the current line."
  4001. (interactive "P")
  4002. (if (= (buffer-size) 0)
  4003. (insert "\n* ")
  4004. (when (or force-heading (not (org-insert-item)))
  4005. (let* ((head (save-excursion
  4006. (condition-case nil
  4007. (progn
  4008. (org-back-to-heading)
  4009. (match-string 0))
  4010. (error "*"))))
  4011. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4012. pos)
  4013. (cond
  4014. ((and (org-on-heading-p) (bolp)
  4015. (or (bobp)
  4016. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4017. ;; insert before the current line
  4018. (open-line (if blank 2 1)))
  4019. ((and (bolp)
  4020. (or (bobp)
  4021. (save-excursion
  4022. (backward-char 1) (not (org-invisible-p)))))
  4023. ;; insert right here
  4024. nil)
  4025. (t
  4026. ;; in the middle of the line
  4027. (org-show-entry)
  4028. (let ((split
  4029. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4030. tags pos)
  4031. (if (org-on-heading-p)
  4032. (progn
  4033. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4034. (setq tags (and (match-end 2) (match-string 2)))
  4035. (and (match-end 1)
  4036. (delete-region (match-beginning 1) (match-end 1)))
  4037. (setq pos (point-at-bol))
  4038. (or split (end-of-line 1))
  4039. (delete-horizontal-space)
  4040. (newline (if blank 2 1))
  4041. (when tags
  4042. (save-excursion
  4043. (goto-char pos)
  4044. (end-of-line 1)
  4045. (insert " " tags)
  4046. (org-set-tags nil 'align))))
  4047. (or split (end-of-line 1))
  4048. (newline (if blank 2 1))))))
  4049. (insert head) (just-one-space)
  4050. (setq pos (point))
  4051. (end-of-line 1)
  4052. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4053. (run-hooks 'org-insert-heading-hook)))))
  4054. (defun org-get-heading (&optional no-tags)
  4055. "Return the heading of the current entry, without the stars."
  4056. (save-excursion
  4057. (org-back-to-heading t)
  4058. (if (looking-at
  4059. (if no-tags
  4060. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4061. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4062. (match-string 1) "")))
  4063. (defun org-insert-heading-after-current ()
  4064. "Insert a new heading with same level as current, after current subtree."
  4065. (interactive)
  4066. (org-back-to-heading)
  4067. (org-insert-heading)
  4068. (org-move-subtree-down)
  4069. (end-of-line 1))
  4070. (defun org-insert-todo-heading (arg)
  4071. "Insert a new heading with the same level and TODO state as current heading.
  4072. If the heading has no TODO state, or if the state is DONE, use the first
  4073. state (TODO by default). Also with prefix arg, force first state."
  4074. (interactive "P")
  4075. (when (not (org-insert-item 'checkbox))
  4076. (org-insert-heading)
  4077. (save-excursion
  4078. (org-back-to-heading)
  4079. (outline-previous-heading)
  4080. (looking-at org-todo-line-regexp))
  4081. (if (or arg
  4082. (not (match-beginning 2))
  4083. (member (match-string 2) org-done-keywords))
  4084. (insert (car org-todo-keywords-1) " ")
  4085. (insert (match-string 2) " "))
  4086. (when org-provide-todo-statistics
  4087. (org-update-parent-todo-statistics))))
  4088. (defun org-insert-subheading (arg)
  4089. "Insert a new subheading and demote it.
  4090. Works for outline headings and for plain lists alike."
  4091. (interactive "P")
  4092. (org-insert-heading arg)
  4093. (cond
  4094. ((org-on-heading-p) (org-do-demote))
  4095. ((org-at-item-p) (org-indent-item 1))))
  4096. (defun org-insert-todo-subheading (arg)
  4097. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4098. Works for outline headings and for plain lists alike."
  4099. (interactive "P")
  4100. (org-insert-todo-heading arg)
  4101. (cond
  4102. ((org-on-heading-p) (org-do-demote))
  4103. ((org-at-item-p) (org-indent-item 1))))
  4104. ;;; Promotion and Demotion
  4105. (defun org-promote-subtree ()
  4106. "Promote the entire subtree.
  4107. See also `org-promote'."
  4108. (interactive)
  4109. (save-excursion
  4110. (org-map-tree 'org-promote))
  4111. (org-fix-position-after-promote))
  4112. (defun org-demote-subtree ()
  4113. "Demote the entire subtree. See `org-demote'.
  4114. See also `org-promote'."
  4115. (interactive)
  4116. (save-excursion
  4117. (org-map-tree 'org-demote))
  4118. (org-fix-position-after-promote))
  4119. (defun org-do-promote ()
  4120. "Promote the current heading higher up the tree.
  4121. If the region is active in `transient-mark-mode', promote all headings
  4122. in the region."
  4123. (interactive)
  4124. (save-excursion
  4125. (if (org-region-active-p)
  4126. (org-map-region 'org-promote (region-beginning) (region-end))
  4127. (org-promote)))
  4128. (org-fix-position-after-promote))
  4129. (defun org-do-demote ()
  4130. "Demote the current heading lower down the tree.
  4131. If the region is active in `transient-mark-mode', demote all headings
  4132. in the region."
  4133. (interactive)
  4134. (save-excursion
  4135. (if (org-region-active-p)
  4136. (org-map-region 'org-demote (region-beginning) (region-end))
  4137. (org-demote)))
  4138. (org-fix-position-after-promote))
  4139. (defun org-fix-position-after-promote ()
  4140. "Make sure that after pro/demotion cursor position is right."
  4141. (let ((pos (point)))
  4142. (when (save-excursion
  4143. (beginning-of-line 1)
  4144. (looking-at org-todo-line-regexp)
  4145. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4146. (cond ((eobp) (insert " "))
  4147. ((eolp) (insert " "))
  4148. ((equal (char-after) ?\ ) (forward-char 1))))))
  4149. (defun org-reduced-level (l)
  4150. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4151. (defun org-get-valid-level (level &optional change)
  4152. "Rectify a level change under the influence of `org-odd-levels-only'
  4153. LEVEL is a current level, CHANGE is by how much the level should be
  4154. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4155. even level numbers will become the next higher odd number."
  4156. (if org-odd-levels-only
  4157. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4158. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4159. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4160. (max 1 (+ level change))))
  4161. (if (boundp 'define-obsolete-function-alias)
  4162. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4163. (define-obsolete-function-alias 'org-get-legal-level
  4164. 'org-get-valid-level)
  4165. (define-obsolete-function-alias 'org-get-legal-level
  4166. 'org-get-valid-level "23.1")))
  4167. (defun org-promote ()
  4168. "Promote the current heading higher up the tree.
  4169. If the region is active in `transient-mark-mode', promote all headings
  4170. in the region."
  4171. (org-back-to-heading t)
  4172. (let* ((level (save-match-data (funcall outline-level)))
  4173. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4174. (diff (abs (- level (length up-head) -1))))
  4175. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4176. (replace-match up-head nil t)
  4177. ;; Fixup tag positioning
  4178. (and org-auto-align-tags (org-set-tags nil t))
  4179. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4180. (defun org-demote ()
  4181. "Demote the current heading lower down the tree.
  4182. If the region is active in `transient-mark-mode', demote all headings
  4183. in the region."
  4184. (org-back-to-heading t)
  4185. (let* ((level (save-match-data (funcall outline-level)))
  4186. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4187. (diff (abs (- level (length down-head) -1))))
  4188. (replace-match down-head nil t)
  4189. ;; Fixup tag positioning
  4190. (and org-auto-align-tags (org-set-tags nil t))
  4191. (if org-adapt-indentation (org-fixup-indentation diff))))
  4192. (defun org-map-tree (fun)
  4193. "Call FUN for every heading underneath the current one."
  4194. (org-back-to-heading)
  4195. (let ((level (funcall outline-level)))
  4196. (save-excursion
  4197. (funcall fun)
  4198. (while (and (progn
  4199. (outline-next-heading)
  4200. (> (funcall outline-level) level))
  4201. (not (eobp)))
  4202. (funcall fun)))))
  4203. (defun org-map-region (fun beg end)
  4204. "Call FUN for every heading between BEG and END."
  4205. (let ((org-ignore-region t))
  4206. (save-excursion
  4207. (setq end (copy-marker end))
  4208. (goto-char beg)
  4209. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4210. (< (point) end))
  4211. (funcall fun))
  4212. (while (and (progn
  4213. (outline-next-heading)
  4214. (< (point) end))
  4215. (not (eobp)))
  4216. (funcall fun)))))
  4217. (defun org-fixup-indentation (diff)
  4218. "Change the indentation in the current entry by DIFF
  4219. However, if any line in the current entry has no indentation, or if it
  4220. would end up with no indentation after the change, nothing at all is done."
  4221. (save-excursion
  4222. (let ((end (save-excursion (outline-next-heading)
  4223. (point-marker)))
  4224. (prohibit (if (> diff 0)
  4225. "^\\S-"
  4226. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4227. col)
  4228. (unless (save-excursion (end-of-line 1)
  4229. (re-search-forward prohibit end t))
  4230. (while (and (< (point) end)
  4231. (re-search-forward "^[ \t]+" end t))
  4232. (goto-char (match-end 0))
  4233. (setq col (current-column))
  4234. (if (< diff 0) (replace-match ""))
  4235. (indent-to (+ diff col))))
  4236. (move-marker end nil))))
  4237. (defun org-convert-to-odd-levels ()
  4238. "Convert an org-mode file with all levels allowed to one with odd levels.
  4239. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4240. level 5 etc."
  4241. (interactive)
  4242. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4243. (let ((org-odd-levels-only nil) n)
  4244. (save-excursion
  4245. (goto-char (point-min))
  4246. (while (re-search-forward "^\\*\\*+ " nil t)
  4247. (setq n (- (length (match-string 0)) 2))
  4248. (while (>= (setq n (1- n)) 0)
  4249. (org-demote))
  4250. (end-of-line 1))))))
  4251. (defun org-convert-to-oddeven-levels ()
  4252. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4253. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4254. section with an even level, conversion would destroy the structure of the file. An error
  4255. is signaled in this case."
  4256. (interactive)
  4257. (goto-char (point-min))
  4258. ;; First check if there are no even levels
  4259. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4260. (org-show-context t)
  4261. (error "Not all levels are odd in this file. Conversion not possible."))
  4262. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4263. (let ((org-odd-levels-only nil) n)
  4264. (save-excursion
  4265. (goto-char (point-min))
  4266. (while (re-search-forward "^\\*\\*+ " nil t)
  4267. (setq n (/ (1- (length (match-string 0))) 2))
  4268. (while (>= (setq n (1- n)) 0)
  4269. (org-promote))
  4270. (end-of-line 1))))))
  4271. (defun org-tr-level (n)
  4272. "Make N odd if required."
  4273. (if org-odd-levels-only (1+ (/ n 2)) n))
  4274. ;;; Vertical tree motion, cutting and pasting of subtrees
  4275. (defun org-move-subtree-up (&optional arg)
  4276. "Move the current subtree up past ARG headlines of the same level."
  4277. (interactive "p")
  4278. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4279. (defun org-move-subtree-down (&optional arg)
  4280. "Move the current subtree down past ARG headlines of the same level."
  4281. (interactive "p")
  4282. (setq arg (prefix-numeric-value arg))
  4283. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4284. 'outline-get-last-sibling))
  4285. (ins-point (make-marker))
  4286. (cnt (abs arg))
  4287. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4288. ;; Select the tree
  4289. (org-back-to-heading)
  4290. (setq beg0 (point))
  4291. (save-excursion
  4292. (setq ne-beg (org-back-over-empty-lines))
  4293. (setq beg (point)))
  4294. (save-match-data
  4295. (save-excursion (outline-end-of-heading)
  4296. (setq folded (org-invisible-p)))
  4297. (outline-end-of-subtree))
  4298. (outline-next-heading)
  4299. (setq ne-end (org-back-over-empty-lines))
  4300. (setq end (point))
  4301. (goto-char beg0)
  4302. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4303. ;; include less whitespace
  4304. (save-excursion
  4305. (goto-char beg)
  4306. (forward-line (- ne-beg ne-end))
  4307. (setq beg (point))))
  4308. ;; Find insertion point, with error handling
  4309. (while (> cnt 0)
  4310. (or (and (funcall movfunc) (looking-at outline-regexp))
  4311. (progn (goto-char beg0)
  4312. (error "Cannot move past superior level or buffer limit")))
  4313. (setq cnt (1- cnt)))
  4314. (if (> arg 0)
  4315. ;; Moving forward - still need to move over subtree
  4316. (progn (org-end-of-subtree t t)
  4317. (save-excursion
  4318. (org-back-over-empty-lines)
  4319. (or (bolp) (newline)))))
  4320. (setq ne-ins (org-back-over-empty-lines))
  4321. (move-marker ins-point (point))
  4322. (setq txt (buffer-substring beg end))
  4323. (org-save-markers-in-region beg end)
  4324. (delete-region beg end)
  4325. (outline-flag-region (1- beg) beg nil)
  4326. (outline-flag-region (1- (point)) (point) nil)
  4327. (let ((bbb (point)))
  4328. (insert-before-markers txt)
  4329. (org-reinstall-markers-in-region bbb)
  4330. (move-marker ins-point bbb))
  4331. (or (bolp) (insert "\n"))
  4332. (setq ins-end (point))
  4333. (goto-char ins-point)
  4334. (org-skip-whitespace)
  4335. (when (and (< arg 0)
  4336. (org-first-sibling-p)
  4337. (> ne-ins ne-beg))
  4338. ;; Move whitespace back to beginning
  4339. (save-excursion
  4340. (goto-char ins-end)
  4341. (let ((kill-whole-line t))
  4342. (kill-line (- ne-ins ne-beg)) (point)))
  4343. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4344. (move-marker ins-point nil)
  4345. (org-compact-display-after-subtree-move)
  4346. (unless folded
  4347. (org-show-entry)
  4348. (show-children)
  4349. (org-cycle-hide-drawers 'children))))
  4350. (defvar org-subtree-clip ""
  4351. "Clipboard for cut and paste of subtrees.
  4352. This is actually only a copy of the kill, because we use the normal kill
  4353. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4354. (defvar org-subtree-clip-folded nil
  4355. "Was the last copied subtree folded?
  4356. This is used to fold the tree back after pasting.")
  4357. (defun org-cut-subtree (&optional n)
  4358. "Cut the current subtree into the clipboard.
  4359. With prefix arg N, cut this many sequential subtrees.
  4360. This is a short-hand for marking the subtree and then cutting it."
  4361. (interactive "p")
  4362. (org-copy-subtree n 'cut))
  4363. (defun org-copy-subtree (&optional n cut force-store-markers)
  4364. "Cut the current subtree into the clipboard.
  4365. With prefix arg N, cut this many sequential subtrees.
  4366. This is a short-hand for marking the subtree and then copying it.
  4367. If CUT is non-nil, actually cut the subtree.
  4368. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4369. of some markers in the region, even if CUT is non-nil. This is
  4370. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4371. (interactive "p")
  4372. (let (beg end folded (beg0 (point)))
  4373. (if (interactive-p)
  4374. (org-back-to-heading nil) ; take what looks like a subtree
  4375. (org-back-to-heading t)) ; take what is really there
  4376. (org-back-over-empty-lines)
  4377. (setq beg (point))
  4378. (skip-chars-forward " \t\r\n")
  4379. (save-match-data
  4380. (save-excursion (outline-end-of-heading)
  4381. (setq folded (org-invisible-p)))
  4382. (condition-case nil
  4383. (outline-forward-same-level (1- n))
  4384. (error nil))
  4385. (org-end-of-subtree t t))
  4386. (org-back-over-empty-lines)
  4387. (setq end (point))
  4388. (goto-char beg0)
  4389. (when (> end beg)
  4390. (setq org-subtree-clip-folded folded)
  4391. (when (or cut force-store-markers)
  4392. (org-save-markers-in-region beg end))
  4393. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4394. (setq org-subtree-clip (current-kill 0))
  4395. (message "%s: Subtree(s) with %d characters"
  4396. (if cut "Cut" "Copied")
  4397. (length org-subtree-clip)))))
  4398. (defun org-paste-subtree (&optional level tree)
  4399. "Paste the clipboard as a subtree, with modification of headline level.
  4400. The entire subtree is promoted or demoted in order to match a new headline
  4401. level. By default, the new level is derived from the visible headings
  4402. before and after the insertion point, and taken to be the inferior headline
  4403. level of the two. So if the previous visible heading is level 3 and the
  4404. next is level 4 (or vice versa), level 4 will be used for insertion.
  4405. This makes sure that the subtree remains an independent subtree and does
  4406. not swallow low level entries.
  4407. You can also force a different level, either by using a numeric prefix
  4408. argument, or by inserting the heading marker by hand. For example, if the
  4409. cursor is after \"*****\", then the tree will be shifted to level 5.
  4410. If you want to insert the tree as is, just use \\[yank].
  4411. If optional TREE is given, use this text instead of the kill ring."
  4412. (interactive "P")
  4413. (unless (org-kill-is-subtree-p tree)
  4414. (error "%s"
  4415. (substitute-command-keys
  4416. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4417. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4418. (^re (concat "^\\(" outline-regexp "\\)"))
  4419. (re (concat "\\(" outline-regexp "\\)"))
  4420. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4421. (old-level (if (string-match ^re txt)
  4422. (- (match-end 0) (match-beginning 0) 1)
  4423. -1))
  4424. (force-level (cond (level (prefix-numeric-value level))
  4425. ((string-match
  4426. ^re_ (buffer-substring (point-at-bol) (point)))
  4427. (- (match-end 1) (match-beginning 1)))
  4428. (t nil)))
  4429. (previous-level (save-excursion
  4430. (condition-case nil
  4431. (progn
  4432. (outline-previous-visible-heading 1)
  4433. (if (looking-at re)
  4434. (- (match-end 0) (match-beginning 0) 1)
  4435. 1))
  4436. (error 1))))
  4437. (next-level (save-excursion
  4438. (condition-case nil
  4439. (progn
  4440. (or (looking-at outline-regexp)
  4441. (outline-next-visible-heading 1))
  4442. (if (looking-at re)
  4443. (- (match-end 0) (match-beginning 0) 1)
  4444. 1))
  4445. (error 1))))
  4446. (new-level (or force-level (max previous-level next-level)))
  4447. (shift (if (or (= old-level -1)
  4448. (= new-level -1)
  4449. (= old-level new-level))
  4450. 0
  4451. (- new-level old-level)))
  4452. (delta (if (> shift 0) -1 1))
  4453. (func (if (> shift 0) 'org-demote 'org-promote))
  4454. (org-odd-levels-only nil)
  4455. beg end)
  4456. ;; Remove the forced level indicator
  4457. (if force-level
  4458. (delete-region (point-at-bol) (point)))
  4459. ;; Paste
  4460. (beginning-of-line 1)
  4461. (org-back-over-empty-lines)
  4462. (setq beg (point))
  4463. (insert-before-markers txt)
  4464. (unless (string-match "\n\\'" txt) (insert "\n"))
  4465. (org-reinstall-markers-in-region beg)
  4466. (setq end (point))
  4467. (goto-char beg)
  4468. (skip-chars-forward " \t\n\r")
  4469. (setq beg (point))
  4470. ;; Shift if necessary
  4471. (unless (= shift 0)
  4472. (save-restriction
  4473. (narrow-to-region beg end)
  4474. (while (not (= shift 0))
  4475. (org-map-region func (point-min) (point-max))
  4476. (setq shift (+ delta shift)))
  4477. (goto-char (point-min))))
  4478. (when (interactive-p)
  4479. (message "Clipboard pasted as level %d subtree" new-level))
  4480. (if (and kill-ring
  4481. (eq org-subtree-clip (current-kill 0))
  4482. org-subtree-clip-folded)
  4483. ;; The tree was folded before it was killed/copied
  4484. (hide-subtree))))
  4485. (defun org-kill-is-subtree-p (&optional txt)
  4486. "Check if the current kill is an outline subtree, or a set of trees.
  4487. Returns nil if kill does not start with a headline, or if the first
  4488. headline level is not the largest headline level in the tree.
  4489. So this will actually accept several entries of equal levels as well,
  4490. which is OK for `org-paste-subtree'.
  4491. If optional TXT is given, check this string instead of the current kill."
  4492. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4493. (start-level (and kill
  4494. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4495. org-outline-regexp "\\)")
  4496. kill)
  4497. (- (match-end 2) (match-beginning 2) 1)))
  4498. (re (concat "^" org-outline-regexp))
  4499. (start (1+ (match-beginning 2))))
  4500. (if (not start-level)
  4501. (progn
  4502. nil) ;; does not even start with a heading
  4503. (catch 'exit
  4504. (while (setq start (string-match re kill (1+ start)))
  4505. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4506. (throw 'exit nil)))
  4507. t))))
  4508. (defvar org-markers-to-move nil
  4509. "Markers that should be moved with a cut-and-paste operation.
  4510. Those markers are stored together with their positions relative to
  4511. the start of the region.")
  4512. (defun org-save-markers-in-region (beg end)
  4513. "Check markers in region.
  4514. If these markers are between BEG and END, record their position relative
  4515. to BEG, so that after moving the block of text, we can put the markers back
  4516. into place.
  4517. This function gets called just before an entry or tree gets cut from the
  4518. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4519. called immediately, to move the markers with the entries."
  4520. (setq org-markers-to-move nil)
  4521. (when (featurep 'org-clock)
  4522. (org-clock-save-markers-for-cut-and-paste beg end))
  4523. (when (featurep 'org-agenda)
  4524. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4525. (defun org-check-and-save-marker (marker beg end)
  4526. "Check if MARKER is between BEG and END.
  4527. If yes, remember the marker and the distance to BEG."
  4528. (when (and (marker-buffer marker)
  4529. (equal (marker-buffer marker) (current-buffer)))
  4530. (if (and (>= marker beg) (< marker end))
  4531. (push (cons marker (- marker beg)) org-markers-to-move))))
  4532. (defun org-reinstall-markers-in-region (beg)
  4533. "Move all remembered markers to their position relative to BEG."
  4534. (mapc (lambda (x)
  4535. (move-marker (car x) (+ beg (cdr x))))
  4536. org-markers-to-move)
  4537. (setq org-markers-to-move nil))
  4538. (defun org-narrow-to-subtree ()
  4539. "Narrow buffer to the current subtree."
  4540. (interactive)
  4541. (save-excursion
  4542. (save-match-data
  4543. (narrow-to-region
  4544. (progn (org-back-to-heading) (point))
  4545. (progn (org-end-of-subtree t t) (point))))))
  4546. ;;; Outline Sorting
  4547. (defun org-sort (with-case)
  4548. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4549. Optional argument WITH-CASE means sort case-sensitively."
  4550. (interactive "P")
  4551. (if (org-at-table-p)
  4552. (org-call-with-arg 'org-table-sort-lines with-case)
  4553. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4554. (defun org-sort-remove-invisible (s)
  4555. (remove-text-properties 0 (length s) org-rm-props s)
  4556. (while (string-match org-bracket-link-regexp s)
  4557. (setq s (replace-match (if (match-end 2)
  4558. (match-string 3 s)
  4559. (match-string 1 s)) t t s)))
  4560. s)
  4561. (defvar org-priority-regexp) ; defined later in the file
  4562. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4563. "Sort entries on a certain level of an outline tree.
  4564. If there is an active region, the entries in the region are sorted.
  4565. Else, if the cursor is before the first entry, sort the top-level items.
  4566. Else, the children of the entry at point are sorted.
  4567. Sorting can be alphabetically, numerically, and by date/time as given by
  4568. the first time stamp in the entry. The command prompts for the sorting
  4569. type unless it has been given to the function through the SORTING-TYPE
  4570. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4571. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4572. called with point at the beginning of the record. It must return either
  4573. a string or a number that should serve as the sorting key for that record.
  4574. Comparing entries ignores case by default. However, with an optional argument
  4575. WITH-CASE, the sorting considers case as well."
  4576. (interactive "P")
  4577. (let ((case-func (if with-case 'identity 'downcase))
  4578. start beg end stars re re2
  4579. txt what tmp plain-list-p)
  4580. ;; Find beginning and end of region to sort
  4581. (cond
  4582. ((org-region-active-p)
  4583. ;; we will sort the region
  4584. (setq end (region-end)
  4585. what "region")
  4586. (goto-char (region-beginning))
  4587. (if (not (org-on-heading-p)) (outline-next-heading))
  4588. (setq start (point)))
  4589. ((org-at-item-p)
  4590. ;; we will sort this plain list
  4591. (org-beginning-of-item-list) (setq start (point))
  4592. (org-end-of-item-list) (setq end (point))
  4593. (goto-char start)
  4594. (setq plain-list-p t
  4595. what "plain list"))
  4596. ((or (org-on-heading-p)
  4597. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4598. ;; we will sort the children of the current headline
  4599. (org-back-to-heading)
  4600. (setq start (point)
  4601. end (progn (org-end-of-subtree t t)
  4602. (org-back-over-empty-lines)
  4603. (point))
  4604. what "children")
  4605. (goto-char start)
  4606. (show-subtree)
  4607. (outline-next-heading))
  4608. (t
  4609. ;; we will sort the top-level entries in this file
  4610. (goto-char (point-min))
  4611. (or (org-on-heading-p) (outline-next-heading))
  4612. (setq start (point) end (point-max) what "top-level")
  4613. (goto-char start)
  4614. (show-all)))
  4615. (setq beg (point))
  4616. (if (>= beg end) (error "Nothing to sort"))
  4617. (unless plain-list-p
  4618. (looking-at "\\(\\*+\\)")
  4619. (setq stars (match-string 1)
  4620. re (concat "^" (regexp-quote stars) " +")
  4621. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4622. txt (buffer-substring beg end))
  4623. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4624. (if (and (not (equal stars "*")) (string-match re2 txt))
  4625. (error "Region to sort contains a level above the first entry")))
  4626. (unless sorting-type
  4627. (message
  4628. (if plain-list-p
  4629. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4630. "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:")
  4631. what)
  4632. (setq sorting-type (read-char-exclusive))
  4633. (and (= (downcase sorting-type) ?f)
  4634. (setq getkey-func
  4635. (completing-read "Sort using function: "
  4636. obarray 'fboundp t nil nil))
  4637. (setq getkey-func (intern getkey-func)))
  4638. (and (= (downcase sorting-type) ?r)
  4639. (setq property
  4640. (completing-read "Property: "
  4641. (mapcar 'list (org-buffer-property-keys t))
  4642. nil t))))
  4643. (message "Sorting entries...")
  4644. (save-restriction
  4645. (narrow-to-region start end)
  4646. (let ((dcst (downcase sorting-type))
  4647. (now (current-time)))
  4648. (sort-subr
  4649. (/= dcst sorting-type)
  4650. ;; This function moves to the beginning character of the "record" to
  4651. ;; be sorted.
  4652. (if plain-list-p
  4653. (lambda nil
  4654. (if (org-at-item-p) t (goto-char (point-max))))
  4655. (lambda nil
  4656. (if (re-search-forward re nil t)
  4657. (goto-char (match-beginning 0))
  4658. (goto-char (point-max)))))
  4659. ;; This function moves to the last character of the "record" being
  4660. ;; sorted.
  4661. (if plain-list-p
  4662. 'org-end-of-item
  4663. (lambda nil
  4664. (save-match-data
  4665. (condition-case nil
  4666. (outline-forward-same-level 1)
  4667. (error
  4668. (goto-char (point-max)))))))
  4669. ;; This function returns the value that gets sorted against.
  4670. (if plain-list-p
  4671. (lambda nil
  4672. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4673. (cond
  4674. ((= dcst ?n)
  4675. (string-to-number (buffer-substring (match-end 0)
  4676. (point-at-eol))))
  4677. ((= dcst ?a)
  4678. (buffer-substring (match-end 0) (point-at-eol)))
  4679. ((= dcst ?t)
  4680. (if (re-search-forward org-ts-regexp
  4681. (point-at-eol) t)
  4682. (org-time-string-to-time (match-string 0))
  4683. now))
  4684. ((= dcst ?f)
  4685. (if getkey-func
  4686. (progn
  4687. (setq tmp (funcall getkey-func))
  4688. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4689. tmp)
  4690. (error "Invalid key function `%s'" getkey-func)))
  4691. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4692. (lambda nil
  4693. (cond
  4694. ((= dcst ?n)
  4695. (if (looking-at outline-regexp)
  4696. (string-to-number (buffer-substring (match-end 0)
  4697. (point-at-eol)))
  4698. nil))
  4699. ((= dcst ?a)
  4700. (funcall case-func (buffer-substring (point-at-bol)
  4701. (point-at-eol))))
  4702. ((= dcst ?t)
  4703. (if (re-search-forward org-ts-regexp
  4704. (save-excursion
  4705. (forward-line 2)
  4706. (point)) t)
  4707. (org-time-string-to-time (match-string 0))
  4708. now))
  4709. ((= dcst ?p)
  4710. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4711. (string-to-char (match-string 2))
  4712. org-default-priority))
  4713. ((= dcst ?r)
  4714. (or (org-entry-get nil property) ""))
  4715. ((= dcst ?o)
  4716. (if (looking-at org-complex-heading-regexp)
  4717. (- 9999 (length (member (match-string 2)
  4718. org-todo-keywords-1)))))
  4719. ((= dcst ?f)
  4720. (if getkey-func
  4721. (progn
  4722. (setq tmp (funcall getkey-func))
  4723. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4724. tmp)
  4725. (error "Invalid key function `%s'" getkey-func)))
  4726. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4727. nil
  4728. (cond
  4729. ((= dcst ?a) 'string<)
  4730. ((= dcst ?t) 'time-less-p)
  4731. (t nil)))))
  4732. (message "Sorting entries...done")))
  4733. (defun org-do-sort (table what &optional with-case sorting-type)
  4734. "Sort TABLE of WHAT according to SORTING-TYPE.
  4735. The user will be prompted for the SORTING-TYPE if the call to this
  4736. function does not specify it. WHAT is only for the prompt, to indicate
  4737. what is being sorted. The sorting key will be extracted from
  4738. the car of the elements of the table.
  4739. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4740. (unless sorting-type
  4741. (message
  4742. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4743. what)
  4744. (setq sorting-type (read-char-exclusive)))
  4745. (let ((dcst (downcase sorting-type))
  4746. extractfun comparefun)
  4747. ;; Define the appropriate functions
  4748. (cond
  4749. ((= dcst ?n)
  4750. (setq extractfun 'string-to-number
  4751. comparefun (if (= dcst sorting-type) '< '>)))
  4752. ((= dcst ?a)
  4753. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4754. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4755. comparefun (if (= dcst sorting-type)
  4756. 'string<
  4757. (lambda (a b) (and (not (string< a b))
  4758. (not (string= a b)))))))
  4759. ((= dcst ?t)
  4760. (setq extractfun
  4761. (lambda (x)
  4762. (if (string-match org-ts-regexp x)
  4763. (time-to-seconds
  4764. (org-time-string-to-time (match-string 0 x)))
  4765. 0))
  4766. comparefun (if (= dcst sorting-type) '< '>)))
  4767. (t (error "Invalid sorting type `%c'" sorting-type)))
  4768. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4769. table)
  4770. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4771. ;;;; Plain list items, including checkboxes
  4772. ;;; Plain list items
  4773. (defun org-at-item-p ()
  4774. "Is point in a line starting a hand-formatted item?"
  4775. (let ((llt org-plain-list-ordered-item-terminator))
  4776. (save-excursion
  4777. (goto-char (point-at-bol))
  4778. (looking-at
  4779. (cond
  4780. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4781. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4782. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4783. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4784. (defun org-in-item-p ()
  4785. "It the cursor inside a plain list item.
  4786. Does not have to be the first line."
  4787. (save-excursion
  4788. (condition-case nil
  4789. (progn
  4790. (org-beginning-of-item)
  4791. (org-at-item-p)
  4792. t)
  4793. (error nil))))
  4794. (defun org-insert-item (&optional checkbox)
  4795. "Insert a new item at the current level.
  4796. Return t when things worked, nil when we are not in an item."
  4797. (when (save-excursion
  4798. (condition-case nil
  4799. (progn
  4800. (org-beginning-of-item)
  4801. (org-at-item-p)
  4802. (if (org-invisible-p) (error "Invisible item"))
  4803. t)
  4804. (error nil)))
  4805. (let* ((bul (match-string 0))
  4806. (descp (save-excursion (goto-char (match-beginning 0))
  4807. (beginning-of-line 1)
  4808. (save-match-data
  4809. (looking-at "[ \t]*.*? ::"))))
  4810. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4811. (match-end 0)))
  4812. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4813. pos)
  4814. (if descp (setq checkbox nil))
  4815. (cond
  4816. ((and (org-at-item-p) (<= (point) eow))
  4817. ;; before the bullet
  4818. (beginning-of-line 1)
  4819. (open-line (if blank 2 1)))
  4820. ((<= (point) eow)
  4821. (beginning-of-line 1))
  4822. (t
  4823. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4824. (end-of-line 1)
  4825. (delete-horizontal-space))
  4826. (newline (if blank 2 1))))
  4827. (insert bul
  4828. (if checkbox "[ ]" "")
  4829. (if descp (concat (if checkbox " " "")
  4830. (read-string "Term: ") " :: ") ""))
  4831. (just-one-space)
  4832. (setq pos (point))
  4833. (end-of-line 1)
  4834. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4835. (org-maybe-renumber-ordered-list)
  4836. (and checkbox (org-update-checkbox-count-maybe))
  4837. t))
  4838. ;;; Checkboxes
  4839. (defun org-at-item-checkbox-p ()
  4840. "Is point at a line starting a plain-list item with a checklet?"
  4841. (and (org-at-item-p)
  4842. (save-excursion
  4843. (goto-char (match-end 0))
  4844. (skip-chars-forward " \t")
  4845. (looking-at "\\[[- X]\\]"))))
  4846. (defun org-toggle-checkbox (&optional arg)
  4847. "Toggle the checkbox in the current line."
  4848. (interactive "P")
  4849. (catch 'exit
  4850. (let (beg end status (firstnew 'unknown))
  4851. (cond
  4852. ((org-region-active-p)
  4853. (setq beg (region-beginning) end (region-end)))
  4854. ((org-on-heading-p)
  4855. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4856. ((org-at-item-checkbox-p)
  4857. (let ((pos (point)))
  4858. (replace-match
  4859. (cond (arg "[-]")
  4860. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4861. (t "[ ]"))
  4862. t t)
  4863. (goto-char pos))
  4864. (throw 'exit t))
  4865. (t (error "Not at a checkbox or heading, and no active region")))
  4866. (save-excursion
  4867. (goto-char beg)
  4868. (while (< (point) end)
  4869. (when (org-at-item-checkbox-p)
  4870. (setq status (equal (match-string 0) "[X]"))
  4871. (when (eq firstnew 'unknown)
  4872. (setq firstnew (not status)))
  4873. (replace-match
  4874. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4875. (beginning-of-line 2)))))
  4876. (org-update-checkbox-count-maybe))
  4877. (defun org-update-checkbox-count-maybe ()
  4878. "Update checkbox statistics unless turned off by user."
  4879. (when org-provide-checkbox-statistics
  4880. (org-update-checkbox-count)))
  4881. (defun org-update-checkbox-count (&optional all)
  4882. "Update the checkbox statistics in the current section.
  4883. This will find all statistic cookies like [57%] and [6/12] and update them
  4884. with the current numbers. With optional prefix argument ALL, do this for
  4885. the whole buffer."
  4886. (interactive "P")
  4887. (save-excursion
  4888. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4889. (beg (condition-case nil
  4890. (progn (outline-back-to-heading) (point))
  4891. (error (point-min))))
  4892. (end (move-marker (make-marker)
  4893. (progn (outline-next-heading) (point))))
  4894. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4895. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4896. (re-find (concat re "\\|" re-box))
  4897. beg-cookie end-cookie is-percent c-on c-off lim
  4898. eline curr-ind next-ind continue-from startsearch
  4899. (cstat 0)
  4900. )
  4901. (when all
  4902. (goto-char (point-min))
  4903. (outline-next-heading)
  4904. (setq beg (point) end (point-max)))
  4905. (goto-char end)
  4906. ;; find each statistic cookie
  4907. (while (re-search-backward re-find beg t)
  4908. (setq beg-cookie (match-beginning 1)
  4909. end-cookie (match-end 1)
  4910. cstat (+ cstat (if end-cookie 1 0))
  4911. startsearch (point-at-eol)
  4912. continue-from (point-at-bol)
  4913. is-percent (match-beginning 2)
  4914. lim (cond
  4915. ((org-on-heading-p) (outline-next-heading) (point))
  4916. ((org-at-item-p) (org-end-of-item) (point))
  4917. (t nil))
  4918. c-on 0
  4919. c-off 0)
  4920. (when lim
  4921. ;; find first checkbox for this cookie and gather
  4922. ;; statistics from all that are at this indentation level
  4923. (goto-char startsearch)
  4924. (if (re-search-forward re-box lim t)
  4925. (progn
  4926. (org-beginning-of-item)
  4927. (setq curr-ind (org-get-indentation))
  4928. (setq next-ind curr-ind)
  4929. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  4930. (save-excursion (end-of-line) (setq eline (point)))
  4931. (if (re-search-forward re-box eline t)
  4932. (if (member (match-string 2) '("[ ]" "[-]"))
  4933. (setq c-off (1+ c-off))
  4934. (setq c-on (1+ c-on))
  4935. )
  4936. )
  4937. (org-end-of-item)
  4938. (setq next-ind (org-get-indentation))
  4939. )))
  4940. (goto-char continue-from)
  4941. ;; update cookie
  4942. (when end-cookie
  4943. (delete-region beg-cookie end-cookie)
  4944. (goto-char beg-cookie)
  4945. (insert
  4946. (if is-percent
  4947. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4948. (format "[%d/%d]" c-on (+ c-on c-off)))))
  4949. ;; update items checkbox if it has one
  4950. (when (org-at-item-p)
  4951. (org-beginning-of-item)
  4952. (when (and (> (+ c-on c-off) 0)
  4953. (re-search-forward re-box (point-at-eol) t))
  4954. (setq beg-cookie (match-beginning 2)
  4955. end-cookie (match-end 2))
  4956. (delete-region beg-cookie end-cookie)
  4957. (goto-char beg-cookie)
  4958. (cond ((= c-off 0) (insert "[X]"))
  4959. ((= c-on 0) (insert "[ ]"))
  4960. (t (insert "[-]")))
  4961. )))
  4962. (goto-char continue-from))
  4963. (when (interactive-p)
  4964. (message "Checkbox satistics updated %s (%d places)"
  4965. (if all "in entire file" "in current outline entry") cstat)))))
  4966. (defun org-get-checkbox-statistics-face ()
  4967. "Select the face for checkbox statistics.
  4968. The face will be `org-done' when all relevant boxes are checked. Otherwise
  4969. it will be `org-todo'."
  4970. (if (match-end 1)
  4971. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  4972. (if (and (> (match-end 2) (match-beginning 2))
  4973. (equal (match-string 2) (match-string 3)))
  4974. 'org-done
  4975. 'org-todo)))
  4976. (defun org-get-indentation (&optional line)
  4977. "Get the indentation of the current line, interpreting tabs.
  4978. When LINE is given, assume it represents a line and compute its indentation."
  4979. (if line
  4980. (if (string-match "^ *" (org-remove-tabs line))
  4981. (match-end 0))
  4982. (save-excursion
  4983. (beginning-of-line 1)
  4984. (skip-chars-forward " \t")
  4985. (current-column))))
  4986. (defun org-remove-tabs (s &optional width)
  4987. "Replace tabulators in S with spaces.
  4988. Assumes that s is a single line, starting in column 0."
  4989. (setq width (or width tab-width))
  4990. (while (string-match "\t" s)
  4991. (setq s (replace-match
  4992. (make-string
  4993. (- (* width (/ (+ (match-beginning 0) width) width))
  4994. (match-beginning 0)) ?\ )
  4995. t t s)))
  4996. s)
  4997. (defun org-fix-indentation (line ind)
  4998. "Fix indentation in LINE.
  4999. IND is a cons cell with target and minimum indentation.
  5000. If the current indenation in LINE is smaller than the minimum,
  5001. leave it alone. If it is larger than ind, set it to the target."
  5002. (let* ((l (org-remove-tabs line))
  5003. (i (org-get-indentation l))
  5004. (i1 (car ind)) (i2 (cdr ind)))
  5005. (if (>= i i2) (setq l (substring line i2)))
  5006. (if (> i1 0)
  5007. (concat (make-string i1 ?\ ) l)
  5008. l)))
  5009. (defun org-beginning-of-item ()
  5010. "Go to the beginning of the current hand-formatted item.
  5011. If the cursor is not in an item, throw an error."
  5012. (interactive)
  5013. (let ((pos (point))
  5014. (limit (save-excursion
  5015. (condition-case nil
  5016. (progn
  5017. (org-back-to-heading)
  5018. (beginning-of-line 2) (point))
  5019. (error (point-min)))))
  5020. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5021. ind ind1)
  5022. (if (org-at-item-p)
  5023. (beginning-of-line 1)
  5024. (beginning-of-line 1)
  5025. (skip-chars-forward " \t")
  5026. (setq ind (current-column))
  5027. (if (catch 'exit
  5028. (while t
  5029. (beginning-of-line 0)
  5030. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5031. (if (looking-at "[ \t]*$")
  5032. (setq ind1 ind-empty)
  5033. (skip-chars-forward " \t")
  5034. (setq ind1 (current-column)))
  5035. (if (< ind1 ind)
  5036. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5037. nil
  5038. (goto-char pos)
  5039. (error "Not in an item")))))
  5040. (defun org-end-of-item ()
  5041. "Go to the end of the current hand-formatted item.
  5042. If the cursor is not in an item, throw an error."
  5043. (interactive)
  5044. (let* ((pos (point))
  5045. ind1
  5046. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5047. (limit (save-excursion (outline-next-heading) (point)))
  5048. (ind (save-excursion
  5049. (org-beginning-of-item)
  5050. (skip-chars-forward " \t")
  5051. (current-column)))
  5052. (end (catch 'exit
  5053. (while t
  5054. (beginning-of-line 2)
  5055. (if (eobp) (throw 'exit (point)))
  5056. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5057. (if (looking-at "[ \t]*$")
  5058. (setq ind1 ind-empty)
  5059. (skip-chars-forward " \t")
  5060. (setq ind1 (current-column)))
  5061. (if (<= ind1 ind)
  5062. (throw 'exit (point-at-bol)))))))
  5063. (if end
  5064. (goto-char end)
  5065. (goto-char pos)
  5066. (error "Not in an item"))))
  5067. (defun org-next-item ()
  5068. "Move to the beginning of the next item in the current plain list.
  5069. Error if not at a plain list, or if this is the last item in the list."
  5070. (interactive)
  5071. (let (ind ind1 (pos (point)))
  5072. (org-beginning-of-item)
  5073. (setq ind (org-get-indentation))
  5074. (org-end-of-item)
  5075. (setq ind1 (org-get-indentation))
  5076. (unless (and (org-at-item-p) (= ind ind1))
  5077. (goto-char pos)
  5078. (error "On last item"))))
  5079. (defun org-previous-item ()
  5080. "Move to the beginning of the previous item in the current plain list.
  5081. Error if not at a plain list, or if this is the first item in the list."
  5082. (interactive)
  5083. (let (beg ind ind1 (pos (point)))
  5084. (org-beginning-of-item)
  5085. (setq beg (point))
  5086. (setq ind (org-get-indentation))
  5087. (goto-char beg)
  5088. (catch 'exit
  5089. (while t
  5090. (beginning-of-line 0)
  5091. (if (looking-at "[ \t]*$")
  5092. nil
  5093. (if (<= (setq ind1 (org-get-indentation)) ind)
  5094. (throw 'exit t)))))
  5095. (condition-case nil
  5096. (if (or (not (org-at-item-p))
  5097. (< ind1 (1- ind)))
  5098. (error "")
  5099. (org-beginning-of-item))
  5100. (error (goto-char pos)
  5101. (error "On first item")))))
  5102. (defun org-first-list-item-p ()
  5103. "Is this heading the item in a plain list?"
  5104. (unless (org-at-item-p)
  5105. (error "Not at a plain list item"))
  5106. (org-beginning-of-item)
  5107. (= (point) (save-excursion (org-beginning-of-item-list))))
  5108. (defun org-move-item-down ()
  5109. "Move the plain list item at point down, i.e. swap with following item.
  5110. Subitems (items with larger indentation) are considered part of the item,
  5111. so this really moves item trees."
  5112. (interactive)
  5113. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5114. (org-beginning-of-item)
  5115. (setq beg0 (point))
  5116. (save-excursion
  5117. (setq ne-beg (org-back-over-empty-lines))
  5118. (setq beg (point)))
  5119. (goto-char beg0)
  5120. (setq ind (org-get-indentation))
  5121. (org-end-of-item)
  5122. (setq end0 (point))
  5123. (setq ind1 (org-get-indentation))
  5124. (setq ne-end (org-back-over-empty-lines))
  5125. (setq end (point))
  5126. (goto-char beg0)
  5127. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5128. ;; include less whitespace
  5129. (save-excursion
  5130. (goto-char beg)
  5131. (forward-line (- ne-beg ne-end))
  5132. (setq beg (point))))
  5133. (goto-char end0)
  5134. (if (and (org-at-item-p) (= ind ind1))
  5135. (progn
  5136. (org-end-of-item)
  5137. (org-back-over-empty-lines)
  5138. (setq txt (buffer-substring beg end))
  5139. (save-excursion
  5140. (delete-region beg end))
  5141. (setq pos (point))
  5142. (insert txt)
  5143. (goto-char pos) (org-skip-whitespace)
  5144. (org-maybe-renumber-ordered-list))
  5145. (goto-char pos)
  5146. (error "Cannot move this item further down"))))
  5147. (defun org-move-item-up (arg)
  5148. "Move the plain list item at point up, i.e. swap with previous item.
  5149. Subitems (items with larger indentation) are considered part of the item,
  5150. so this really moves item trees."
  5151. (interactive "p")
  5152. (let (beg beg0 end ind ind1 (pos (point)) txt
  5153. ne-beg ne-ins ins-end)
  5154. (org-beginning-of-item)
  5155. (setq beg0 (point))
  5156. (setq ind (org-get-indentation))
  5157. (save-excursion
  5158. (setq ne-beg (org-back-over-empty-lines))
  5159. (setq beg (point)))
  5160. (goto-char beg0)
  5161. (org-end-of-item)
  5162. (setq end (point))
  5163. (goto-char beg0)
  5164. (catch 'exit
  5165. (while t
  5166. (beginning-of-line 0)
  5167. (if (looking-at "[ \t]*$")
  5168. (if org-empty-line-terminates-plain-lists
  5169. (progn
  5170. (goto-char pos)
  5171. (error "Cannot move this item further up"))
  5172. nil)
  5173. (if (<= (setq ind1 (org-get-indentation)) ind)
  5174. (throw 'exit t)))))
  5175. (condition-case nil
  5176. (org-beginning-of-item)
  5177. (error (goto-char beg)
  5178. (error "Cannot move this item further up")))
  5179. (setq ind1 (org-get-indentation))
  5180. (if (and (org-at-item-p) (= ind ind1))
  5181. (progn
  5182. (setq ne-ins (org-back-over-empty-lines))
  5183. (setq txt (buffer-substring beg end))
  5184. (save-excursion
  5185. (delete-region beg end))
  5186. (setq pos (point))
  5187. (insert txt)
  5188. (setq ins-end (point))
  5189. (goto-char pos) (org-skip-whitespace)
  5190. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5191. ;; Move whitespace back to beginning
  5192. (save-excursion
  5193. (goto-char ins-end)
  5194. (let ((kill-whole-line t))
  5195. (kill-line (- ne-ins ne-beg)) (point)))
  5196. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5197. (org-maybe-renumber-ordered-list))
  5198. (goto-char pos)
  5199. (error "Cannot move this item further up"))))
  5200. (defun org-maybe-renumber-ordered-list ()
  5201. "Renumber the ordered list at point if setup allows it.
  5202. This tests the user option `org-auto-renumber-ordered-lists' before
  5203. doing the renumbering."
  5204. (interactive)
  5205. (when (and org-auto-renumber-ordered-lists
  5206. (org-at-item-p))
  5207. (if (match-beginning 3)
  5208. (org-renumber-ordered-list 1)
  5209. (org-fix-bullet-type))))
  5210. (defun org-maybe-renumber-ordered-list-safe ()
  5211. (condition-case nil
  5212. (save-excursion
  5213. (org-maybe-renumber-ordered-list))
  5214. (error nil)))
  5215. (defun org-cycle-list-bullet (&optional which)
  5216. "Cycle through the different itemize/enumerate bullets.
  5217. This cycle the entire list level through the sequence:
  5218. `-' -> `+' -> `*' -> `1.' -> `1)'
  5219. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5220. 0 meand `-', 1 means `+' etc."
  5221. (interactive "P")
  5222. (org-preserve-lc
  5223. (org-beginning-of-item-list)
  5224. (org-at-item-p)
  5225. (beginning-of-line 1)
  5226. (let ((current (match-string 0))
  5227. (prevp (eq which 'previous))
  5228. new)
  5229. (setq new (cond
  5230. ((and (numberp which)
  5231. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5232. ((string-match "-" current) (if prevp "1)" "+"))
  5233. ((string-match "\\+" current)
  5234. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5235. ((string-match "\\*" current) (if prevp "+" "1."))
  5236. ((string-match "\\." current) (if prevp "*" "1)"))
  5237. ((string-match ")" current) (if prevp "1." "-"))
  5238. (t (error "This should not happen"))))
  5239. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5240. (org-fix-bullet-type)
  5241. (org-maybe-renumber-ordered-list))))
  5242. (defun org-get-string-indentation (s)
  5243. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5244. (let ((n -1) (i 0) (w tab-width) c)
  5245. (catch 'exit
  5246. (while (< (setq n (1+ n)) (length s))
  5247. (setq c (aref s n))
  5248. (cond ((= c ?\ ) (setq i (1+ i)))
  5249. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5250. (t (throw 'exit t)))))
  5251. i))
  5252. (defun org-renumber-ordered-list (arg)
  5253. "Renumber an ordered plain list.
  5254. Cursor needs to be in the first line of an item, the line that starts
  5255. with something like \"1.\" or \"2)\"."
  5256. (interactive "p")
  5257. (unless (and (org-at-item-p)
  5258. (match-beginning 3))
  5259. (error "This is not an ordered list"))
  5260. (let ((line (org-current-line))
  5261. (col (current-column))
  5262. (ind (org-get-string-indentation
  5263. (buffer-substring (point-at-bol) (match-beginning 3))))
  5264. ;; (term (substring (match-string 3) -1))
  5265. ind1 (n (1- arg))
  5266. fmt)
  5267. ;; find where this list begins
  5268. (org-beginning-of-item-list)
  5269. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5270. (setq fmt (concat "%d" (match-string 1)))
  5271. (beginning-of-line 0)
  5272. ;; walk forward and replace these numbers
  5273. (catch 'exit
  5274. (while t
  5275. (catch 'next
  5276. (beginning-of-line 2)
  5277. (if (eobp) (throw 'exit nil))
  5278. (if (looking-at "[ \t]*$") (throw 'next nil))
  5279. (skip-chars-forward " \t") (setq ind1 (current-column))
  5280. (if (> ind1 ind) (throw 'next t))
  5281. (if (< ind1 ind) (throw 'exit t))
  5282. (if (not (org-at-item-p)) (throw 'exit nil))
  5283. (delete-region (match-beginning 2) (match-end 2))
  5284. (goto-char (match-beginning 2))
  5285. (insert (format fmt (setq n (1+ n)))))))
  5286. (goto-line line)
  5287. (org-move-to-column col)))
  5288. (defun org-fix-bullet-type ()
  5289. "Make sure all items in this list have the same bullet as the firsst item."
  5290. (interactive)
  5291. (unless (org-at-item-p) (error "This is not a list"))
  5292. (let ((line (org-current-line))
  5293. (col (current-column))
  5294. (ind (current-indentation))
  5295. ind1 bullet)
  5296. ;; find where this list begins
  5297. (org-beginning-of-item-list)
  5298. (beginning-of-line 1)
  5299. ;; find out what the bullet type is
  5300. (looking-at "[ \t]*\\(\\S-+\\)")
  5301. (setq bullet (match-string 1))
  5302. ;; walk forward and replace these numbers
  5303. (beginning-of-line 0)
  5304. (catch 'exit
  5305. (while t
  5306. (catch 'next
  5307. (beginning-of-line 2)
  5308. (if (eobp) (throw 'exit nil))
  5309. (if (looking-at "[ \t]*$") (throw 'next nil))
  5310. (skip-chars-forward " \t") (setq ind1 (current-column))
  5311. (if (> ind1 ind) (throw 'next t))
  5312. (if (< ind1 ind) (throw 'exit t))
  5313. (if (not (org-at-item-p)) (throw 'exit nil))
  5314. (skip-chars-forward " \t")
  5315. (looking-at "\\S-+")
  5316. (replace-match bullet))))
  5317. (goto-line line)
  5318. (org-move-to-column col)
  5319. (if (string-match "[0-9]" bullet)
  5320. (org-renumber-ordered-list 1))))
  5321. (defun org-beginning-of-item-list ()
  5322. "Go to the beginning of the current item list.
  5323. I.e. to the first item in this list."
  5324. (interactive)
  5325. (org-beginning-of-item)
  5326. (let ((pos (point-at-bol))
  5327. (ind (org-get-indentation))
  5328. ind1)
  5329. ;; find where this list begins
  5330. (catch 'exit
  5331. (while t
  5332. (catch 'next
  5333. (beginning-of-line 0)
  5334. (if (looking-at "[ \t]*$")
  5335. (throw (if (bobp) 'exit 'next) t))
  5336. (skip-chars-forward " \t") (setq ind1 (current-column))
  5337. (if (or (< ind1 ind)
  5338. (and (= ind1 ind)
  5339. (not (org-at-item-p)))
  5340. (bobp))
  5341. (throw 'exit t)
  5342. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5343. (goto-char pos)))
  5344. (defun org-end-of-item-list ()
  5345. "Go to the end of the current item list.
  5346. I.e. to the text after the last item."
  5347. (interactive)
  5348. (org-beginning-of-item)
  5349. (let ((pos (point-at-bol))
  5350. (ind (org-get-indentation))
  5351. ind1)
  5352. ;; find where this list begins
  5353. (catch 'exit
  5354. (while t
  5355. (catch 'next
  5356. (beginning-of-line 2)
  5357. (if (looking-at "[ \t]*$")
  5358. (throw (if (eobp) 'exit 'next) t))
  5359. (skip-chars-forward " \t") (setq ind1 (current-column))
  5360. (if (or (< ind1 ind)
  5361. (and (= ind1 ind)
  5362. (not (org-at-item-p)))
  5363. (eobp))
  5364. (progn
  5365. (setq pos (point-at-bol))
  5366. (throw 'exit t))))))
  5367. (goto-char pos)))
  5368. (defvar org-last-indent-begin-marker (make-marker))
  5369. (defvar org-last-indent-end-marker (make-marker))
  5370. (defun org-outdent-item (arg)
  5371. "Outdent a local list item."
  5372. (interactive "p")
  5373. (org-indent-item (- arg)))
  5374. (defun org-indent-item (arg)
  5375. "Indent a local list item."
  5376. (interactive "p")
  5377. (unless (org-at-item-p)
  5378. (error "Not on an item"))
  5379. (save-excursion
  5380. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5381. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5382. (setq beg org-last-indent-begin-marker
  5383. end org-last-indent-end-marker)
  5384. (org-beginning-of-item)
  5385. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5386. (org-end-of-item)
  5387. (setq end (move-marker org-last-indent-end-marker (point))))
  5388. (goto-char beg)
  5389. (setq tmp (org-item-indent-positions)
  5390. ind (car tmp)
  5391. ind-down (nth 2 tmp)
  5392. ind-up (nth 1 tmp)
  5393. delta (if (> arg 0)
  5394. (if ind-down (- ind-down ind) 2)
  5395. (if ind-up (- ind-up ind) -2)))
  5396. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5397. (while (< (point) end)
  5398. (beginning-of-line 1)
  5399. (skip-chars-forward " \t") (setq ind1 (current-column))
  5400. (delete-region (point-at-bol) (point))
  5401. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5402. (beginning-of-line 2))))
  5403. (org-fix-bullet-type)
  5404. (org-maybe-renumber-ordered-list-safe)
  5405. (save-excursion
  5406. (beginning-of-line 0)
  5407. (condition-case nil (org-beginning-of-item) (error nil))
  5408. (org-maybe-renumber-ordered-list-safe)))
  5409. (defun org-item-indent-positions ()
  5410. "Return indentation for plain list items.
  5411. This returns a list with three values: The current indentation, the
  5412. parent indentation and the indentation a child should habe.
  5413. Assumes cursor in item line."
  5414. (let* ((bolpos (point-at-bol))
  5415. (ind (org-get-indentation))
  5416. ind-down ind-up pos)
  5417. (save-excursion
  5418. (org-beginning-of-item-list)
  5419. (skip-chars-backward "\n\r \t")
  5420. (when (org-in-item-p)
  5421. (org-beginning-of-item)
  5422. (setq ind-up (org-get-indentation))))
  5423. (setq pos (point))
  5424. (save-excursion
  5425. (cond
  5426. ((and (condition-case nil (progn (org-previous-item) t)
  5427. (error nil))
  5428. (or (forward-char 1) t)
  5429. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5430. (setq ind-down (org-get-indentation)))
  5431. ((and (goto-char pos)
  5432. (org-at-item-p))
  5433. (goto-char (match-end 0))
  5434. (skip-chars-forward " \t")
  5435. (setq ind-down (current-column)))))
  5436. (list ind ind-up ind-down)))
  5437. ;;; The orgstruct minor mode
  5438. ;; Define a minor mode which can be used in other modes in order to
  5439. ;; integrate the org-mode structure editing commands.
  5440. ;; This is really a hack, because the org-mode structure commands use
  5441. ;; keys which normally belong to the major mode. Here is how it
  5442. ;; works: The minor mode defines all the keys necessary to operate the
  5443. ;; structure commands, but wraps the commands into a function which
  5444. ;; tests if the cursor is currently at a headline or a plain list
  5445. ;; item. If that is the case, the structure command is used,
  5446. ;; temporarily setting many Org-mode variables like regular
  5447. ;; expressions for filling etc. However, when any of those keys is
  5448. ;; used at a different location, function uses `key-binding' to look
  5449. ;; up if the key has an associated command in another currently active
  5450. ;; keymap (minor modes, major mode, global), and executes that
  5451. ;; command. There might be problems if any of the keys is otherwise
  5452. ;; used as a prefix key.
  5453. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5454. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5455. ;; addresses this by checking explicitly for both bindings.
  5456. (defvar orgstruct-mode-map (make-sparse-keymap)
  5457. "Keymap for the minor `orgstruct-mode'.")
  5458. (defvar org-local-vars nil
  5459. "List of local variables, for use by `orgstruct-mode'")
  5460. ;;;###autoload
  5461. (define-minor-mode orgstruct-mode
  5462. "Toggle the minor more `orgstruct-mode'.
  5463. This mode is for using Org-mode structure commands in other modes.
  5464. The following key behave as if Org-mode was active, if the cursor
  5465. is on a headline, or on a plain list item (both in the definition
  5466. of Org-mode).
  5467. M-up Move entry/item up
  5468. M-down Move entry/item down
  5469. M-left Promote
  5470. M-right Demote
  5471. M-S-up Move entry/item up
  5472. M-S-down Move entry/item down
  5473. M-S-left Promote subtree
  5474. M-S-right Demote subtree
  5475. M-q Fill paragraph and items like in Org-mode
  5476. C-c ^ Sort entries
  5477. C-c - Cycle list bullet
  5478. TAB Cycle item visibility
  5479. M-RET Insert new heading/item
  5480. S-M-RET Insert new TODO heading / Chekbox item
  5481. C-c C-c Set tags / toggle checkbox"
  5482. nil " OrgStruct" nil
  5483. (org-load-modules-maybe)
  5484. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5485. ;;;###autoload
  5486. (defun turn-on-orgstruct ()
  5487. "Unconditionally turn on `orgstruct-mode'."
  5488. (orgstruct-mode 1))
  5489. ;;;###autoload
  5490. (defun turn-on-orgstruct++ ()
  5491. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5492. In addition to setting orgstruct-mode, this also exports all indentation and
  5493. autofilling variables from org-mode into the buffer. Note that turning
  5494. off orgstruct-mode will *not* remove these additional settings."
  5495. (orgstruct-mode 1)
  5496. (let (var val)
  5497. (mapc
  5498. (lambda (x)
  5499. (when (string-match
  5500. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5501. (symbol-name (car x)))
  5502. (setq var (car x) val (nth 1 x))
  5503. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5504. org-local-vars)))
  5505. (defun orgstruct-error ()
  5506. "Error when there is no default binding for a structure key."
  5507. (interactive)
  5508. (error "This key has no function outside structure elements"))
  5509. (defun orgstruct-setup ()
  5510. "Setup orgstruct keymaps."
  5511. (let ((nfunc 0)
  5512. (bindings
  5513. (list
  5514. '([(meta up)] org-metaup)
  5515. '([(meta down)] org-metadown)
  5516. '([(meta left)] org-metaleft)
  5517. '([(meta right)] org-metaright)
  5518. '([(meta shift up)] org-shiftmetaup)
  5519. '([(meta shift down)] org-shiftmetadown)
  5520. '([(meta shift left)] org-shiftmetaleft)
  5521. '([(meta shift right)] org-shiftmetaright)
  5522. '([(shift up)] org-shiftup)
  5523. '([(shift down)] org-shiftdown)
  5524. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5525. '("\M-q" fill-paragraph)
  5526. '("\C-c^" org-sort)
  5527. '("\C-c-" org-cycle-list-bullet)))
  5528. elt key fun cmd)
  5529. (while (setq elt (pop bindings))
  5530. (setq nfunc (1+ nfunc))
  5531. (setq key (org-key (car elt))
  5532. fun (nth 1 elt)
  5533. cmd (orgstruct-make-binding fun nfunc key))
  5534. (org-defkey orgstruct-mode-map key cmd))
  5535. ;; Special treatment needed for TAB and RET
  5536. (org-defkey orgstruct-mode-map [(tab)]
  5537. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5538. (org-defkey orgstruct-mode-map "\C-i"
  5539. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5540. (org-defkey orgstruct-mode-map "\M-\C-m"
  5541. (orgstruct-make-binding 'org-insert-heading 105
  5542. "\M-\C-m" [(meta return)]))
  5543. (org-defkey orgstruct-mode-map [(meta return)]
  5544. (orgstruct-make-binding 'org-insert-heading 106
  5545. [(meta return)] "\M-\C-m"))
  5546. (org-defkey orgstruct-mode-map [(shift meta return)]
  5547. (orgstruct-make-binding 'org-insert-todo-heading 107
  5548. [(meta return)] "\M-\C-m"))
  5549. (unless org-local-vars
  5550. (setq org-local-vars (org-get-local-variables)))
  5551. t))
  5552. (defun orgstruct-make-binding (fun n &rest keys)
  5553. "Create a function for binding in the structure minor mode.
  5554. FUN is the command to call inside a table. N is used to create a unique
  5555. command name. KEYS are keys that should be checked in for a command
  5556. to execute outside of tables."
  5557. (eval
  5558. (list 'defun
  5559. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5560. '(arg)
  5561. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5562. "Outside of structure, run the binding of `"
  5563. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5564. "'.")
  5565. '(interactive "p")
  5566. (list 'if
  5567. '(org-context-p 'headline 'item)
  5568. (list 'org-run-like-in-org-mode (list 'quote fun))
  5569. (list 'let '(orgstruct-mode)
  5570. (list 'call-interactively
  5571. (append '(or)
  5572. (mapcar (lambda (k)
  5573. (list 'key-binding k))
  5574. keys)
  5575. '('orgstruct-error))))))))
  5576. (defun org-context-p (&rest contexts)
  5577. "Check if local context is and of CONTEXTS.
  5578. Possible values in the list of contexts are `table', `headline', and `item'."
  5579. (let ((pos (point)))
  5580. (goto-char (point-at-bol))
  5581. (prog1 (or (and (memq 'table contexts)
  5582. (looking-at "[ \t]*|"))
  5583. (and (memq 'headline contexts)
  5584. (looking-at "\\*+"))
  5585. (and (memq 'item contexts)
  5586. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5587. (goto-char pos))))
  5588. (defun org-get-local-variables ()
  5589. "Return a list of all local variables in an org-mode buffer."
  5590. (let (varlist)
  5591. (with-current-buffer (get-buffer-create "*Org tmp*")
  5592. (erase-buffer)
  5593. (org-mode)
  5594. (setq varlist (buffer-local-variables)))
  5595. (kill-buffer "*Org tmp*")
  5596. (delq nil
  5597. (mapcar
  5598. (lambda (x)
  5599. (setq x
  5600. (if (symbolp x)
  5601. (list x)
  5602. (list (car x) (list 'quote (cdr x)))))
  5603. (if (string-match
  5604. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5605. (symbol-name (car x)))
  5606. x nil))
  5607. varlist))))
  5608. ;;;###autoload
  5609. (defun org-run-like-in-org-mode (cmd)
  5610. (org-load-modules-maybe)
  5611. (unless org-local-vars
  5612. (setq org-local-vars (org-get-local-variables)))
  5613. (eval (list 'let org-local-vars
  5614. (list 'call-interactively (list 'quote cmd)))))
  5615. ;;;; Archiving
  5616. (defun org-get-category (&optional pos)
  5617. "Get the category applying to position POS."
  5618. (get-text-property (or pos (point)) 'org-category))
  5619. (defun org-refresh-category-properties ()
  5620. "Refresh category text properties in the buffer."
  5621. (let ((def-cat (cond
  5622. ((null org-category)
  5623. (if buffer-file-name
  5624. (file-name-sans-extension
  5625. (file-name-nondirectory buffer-file-name))
  5626. "???"))
  5627. ((symbolp org-category) (symbol-name org-category))
  5628. (t org-category)))
  5629. beg end cat pos optionp)
  5630. (org-unmodified
  5631. (save-excursion
  5632. (save-restriction
  5633. (widen)
  5634. (goto-char (point-min))
  5635. (put-text-property (point) (point-max) 'org-category def-cat)
  5636. (while (re-search-forward
  5637. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5638. (setq pos (match-end 0)
  5639. optionp (equal (char-after (match-beginning 0)) ?#)
  5640. cat (org-trim (match-string 2)))
  5641. (if optionp
  5642. (setq beg (point-at-bol) end (point-max))
  5643. (org-back-to-heading t)
  5644. (setq beg (point) end (org-end-of-subtree t t)))
  5645. (put-text-property beg end 'org-category cat)
  5646. (goto-char pos)))))))
  5647. ;;;; Link Stuff
  5648. ;;; Link abbreviations
  5649. (defun org-link-expand-abbrev (link)
  5650. "Apply replacements as defined in `org-link-abbrev-alist."
  5651. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5652. (let* ((key (match-string 1 link))
  5653. (as (or (assoc key org-link-abbrev-alist-local)
  5654. (assoc key org-link-abbrev-alist)))
  5655. (tag (and (match-end 2) (match-string 3 link)))
  5656. rpl)
  5657. (if (not as)
  5658. link
  5659. (setq rpl (cdr as))
  5660. (cond
  5661. ((symbolp rpl) (funcall rpl tag))
  5662. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5663. (t (concat rpl tag)))))
  5664. link))
  5665. ;;; Storing and inserting links
  5666. (defvar org-insert-link-history nil
  5667. "Minibuffer history for links inserted with `org-insert-link'.")
  5668. (defvar org-stored-links nil
  5669. "Contains the links stored with `org-store-link'.")
  5670. (defvar org-store-link-plist nil
  5671. "Plist with info about the most recently link created with `org-store-link'.")
  5672. (defvar org-link-protocols nil
  5673. "Link protocols added to Org-mode using `org-add-link-type'.")
  5674. (defvar org-store-link-functions nil
  5675. "List of functions that are called to create and store a link.
  5676. Each function will be called in turn until one returns a non-nil
  5677. value. Each function should check if it is responsible for creating
  5678. this link (for example by looking at the major mode).
  5679. If not, it must exit and return nil.
  5680. If yes, it should return a non-nil value after a calling
  5681. `org-store-link-props' with a list of properties and values.
  5682. Special properties are:
  5683. :type The link prefix. like \"http\". This must be given.
  5684. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5685. This is obligatory as well.
  5686. :description Optional default description for the second pair
  5687. of brackets in an Org-mode link. The user can still change
  5688. this when inserting this link into an Org-mode buffer.
  5689. In addition to these, any additional properties can be specified
  5690. and then used in remember templates.")
  5691. (defun org-add-link-type (type &optional follow export)
  5692. "Add TYPE to the list of `org-link-types'.
  5693. Re-compute all regular expressions depending on `org-link-types'
  5694. FOLLOW and EXPORT are two functions.
  5695. FOLLOW should take the link path as the single argument and do whatever
  5696. is necessary to follow the link, for example find a file or display
  5697. a mail message.
  5698. EXPORT should format the link path for export to one of the export formats.
  5699. It should be a function accepting three arguments:
  5700. path the path of the link, the text after the prefix (like \"http:\")
  5701. desc the description of the link, if any, nil if there was no descripton
  5702. format the export format, a symbol like `html' or `latex'.
  5703. The function may use the FORMAT information to return different values
  5704. depending on the format. The return value will be put literally into
  5705. the exported file.
  5706. Org-mode has a built-in default for exporting links. If you are happy with
  5707. this default, there is no need to define an export function for the link
  5708. type. For a simple example of an export function, see `org-bbdb.el'."
  5709. (add-to-list 'org-link-types type t)
  5710. (org-make-link-regexps)
  5711. (if (assoc type org-link-protocols)
  5712. (setcdr (assoc type org-link-protocols) (list follow export))
  5713. (push (list type follow export) org-link-protocols)))
  5714. ;;;###autoload
  5715. (defun org-store-link (arg)
  5716. "\\<org-mode-map>Store an org-link to the current location.
  5717. This link is added to `org-stored-links' and can later be inserted
  5718. into an org-buffer with \\[org-insert-link].
  5719. For some link types, a prefix arg is interpreted:
  5720. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5721. For file links, arg negates `org-context-in-file-links'."
  5722. (interactive "P")
  5723. (org-load-modules-maybe)
  5724. (setq org-store-link-plist nil) ; reset
  5725. (let (link cpltxt desc description search txt)
  5726. (cond
  5727. ((run-hook-with-args-until-success 'org-store-link-functions)
  5728. (setq link (plist-get org-store-link-plist :link)
  5729. desc (or (plist-get org-store-link-plist :description) link)))
  5730. ((eq major-mode 'calendar-mode)
  5731. (let ((cd (calendar-cursor-to-date)))
  5732. (setq link
  5733. (format-time-string
  5734. (car org-time-stamp-formats)
  5735. (apply 'encode-time
  5736. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5737. nil nil nil))))
  5738. (org-store-link-props :type "calendar" :date cd)))
  5739. ((eq major-mode 'w3-mode)
  5740. (setq cpltxt (url-view-url t)
  5741. link (org-make-link cpltxt))
  5742. (org-store-link-props :type "w3" :url (url-view-url t)))
  5743. ((eq major-mode 'w3m-mode)
  5744. (setq cpltxt (or w3m-current-title w3m-current-url)
  5745. link (org-make-link w3m-current-url))
  5746. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5747. ((setq search (run-hook-with-args-until-success
  5748. 'org-create-file-search-functions))
  5749. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5750. "::" search))
  5751. (setq cpltxt (or description link)))
  5752. ((eq major-mode 'image-mode)
  5753. (setq cpltxt (concat "file:"
  5754. (abbreviate-file-name buffer-file-name))
  5755. link (org-make-link cpltxt))
  5756. (org-store-link-props :type "image" :file buffer-file-name))
  5757. ((eq major-mode 'dired-mode)
  5758. ;; link to the file in the current line
  5759. (setq cpltxt (concat "file:"
  5760. (abbreviate-file-name
  5761. (expand-file-name
  5762. (dired-get-filename nil t))))
  5763. link (org-make-link cpltxt)))
  5764. ((and buffer-file-name (org-mode-p))
  5765. ;; Just link to current headline
  5766. (setq cpltxt (concat "file:"
  5767. (abbreviate-file-name buffer-file-name)))
  5768. ;; Add a context search string
  5769. (when (org-xor org-context-in-file-links arg)
  5770. ;; Check if we are on a target
  5771. (if (org-in-regexp "<<\\(.*?\\)>>")
  5772. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5773. (setq txt (cond
  5774. ((org-on-heading-p) nil)
  5775. ((org-region-active-p)
  5776. (buffer-substring (region-beginning) (region-end)))
  5777. (t nil)))
  5778. (when (or (null txt) (string-match "\\S-" txt))
  5779. (setq cpltxt
  5780. (concat cpltxt "::"
  5781. (condition-case nil
  5782. (org-make-org-heading-search-string txt)
  5783. (error "")))
  5784. desc "NONE"))))
  5785. (if (string-match "::\\'" cpltxt)
  5786. (setq cpltxt (substring cpltxt 0 -2)))
  5787. (setq link (org-make-link cpltxt)))
  5788. ((buffer-file-name (buffer-base-buffer))
  5789. ;; Just link to this file here.
  5790. (setq cpltxt (concat "file:"
  5791. (abbreviate-file-name
  5792. (buffer-file-name (buffer-base-buffer)))))
  5793. ;; Add a context string
  5794. (when (org-xor org-context-in-file-links arg)
  5795. (setq txt (if (org-region-active-p)
  5796. (buffer-substring (region-beginning) (region-end))
  5797. (buffer-substring (point-at-bol) (point-at-eol))))
  5798. ;; Only use search option if there is some text.
  5799. (when (string-match "\\S-" txt)
  5800. (setq cpltxt
  5801. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5802. desc "NONE")))
  5803. (setq link (org-make-link cpltxt)))
  5804. ((interactive-p)
  5805. (error "Cannot link to a buffer which is not visiting a file"))
  5806. (t (setq link nil)))
  5807. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5808. (setq link (or link cpltxt)
  5809. desc (or desc cpltxt))
  5810. (if (equal desc "NONE") (setq desc nil))
  5811. (if (and (interactive-p) link)
  5812. (progn
  5813. (setq org-stored-links
  5814. (cons (list link desc) org-stored-links))
  5815. (message "Stored: %s" (or desc link)))
  5816. (and link (org-make-link-string link desc)))))
  5817. (defun org-store-link-props (&rest plist)
  5818. "Store link properties, extract names and addresses."
  5819. (let (x adr)
  5820. (when (setq x (plist-get plist :from))
  5821. (setq adr (mail-extract-address-components x))
  5822. (plist-put plist :fromname (car adr))
  5823. (plist-put plist :fromaddress (nth 1 adr)))
  5824. (when (setq x (plist-get plist :to))
  5825. (setq adr (mail-extract-address-components x))
  5826. (plist-put plist :toname (car adr))
  5827. (plist-put plist :toaddress (nth 1 adr))))
  5828. (let ((from (plist-get plist :from))
  5829. (to (plist-get plist :to)))
  5830. (when (and from to org-from-is-user-regexp)
  5831. (plist-put plist :fromto
  5832. (if (string-match org-from-is-user-regexp from)
  5833. (concat "to %t")
  5834. (concat "from %f")))))
  5835. (setq org-store-link-plist plist))
  5836. (defun org-add-link-props (&rest plist)
  5837. "Add these properties to the link property list."
  5838. (let (key value)
  5839. (while plist
  5840. (setq key (pop plist) value (pop plist))
  5841. (setq org-store-link-plist
  5842. (plist-put org-store-link-plist key value)))))
  5843. (defun org-email-link-description (&optional fmt)
  5844. "Return the description part of an email link.
  5845. This takes information from `org-store-link-plist' and formats it
  5846. according to FMT (default from `org-email-link-description-format')."
  5847. (setq fmt (or fmt org-email-link-description-format))
  5848. (let* ((p org-store-link-plist)
  5849. (to (plist-get p :toaddress))
  5850. (from (plist-get p :fromaddress))
  5851. (table
  5852. (list
  5853. (cons "%c" (plist-get p :fromto))
  5854. (cons "%F" (plist-get p :from))
  5855. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5856. (cons "%T" (plist-get p :to))
  5857. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5858. (cons "%s" (plist-get p :subject))
  5859. (cons "%m" (plist-get p :message-id)))))
  5860. (when (string-match "%c" fmt)
  5861. ;; Check if the user wrote this message
  5862. (if (and org-from-is-user-regexp from to
  5863. (save-match-data (string-match org-from-is-user-regexp from)))
  5864. (setq fmt (replace-match "to %t" t t fmt))
  5865. (setq fmt (replace-match "from %f" t t fmt))))
  5866. (org-replace-escapes fmt table)))
  5867. (defun org-make-org-heading-search-string (&optional string heading)
  5868. "Make search string for STRING or current headline."
  5869. (interactive)
  5870. (let ((s (or string (org-get-heading))))
  5871. (unless (and string (not heading))
  5872. ;; We are using a headline, clean up garbage in there.
  5873. (if (string-match org-todo-regexp s)
  5874. (setq s (replace-match "" t t s)))
  5875. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5876. (setq s (replace-match "" t t s)))
  5877. (setq s (org-trim s))
  5878. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5879. org-comment-string "\\)") s)
  5880. (setq s (replace-match "" t t s)))
  5881. (while (string-match org-ts-regexp s)
  5882. (setq s (replace-match "" t t s))))
  5883. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5884. (setq s (replace-match " " t t s)))
  5885. (or string (setq s (concat "*" s))) ; Add * for headlines
  5886. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5887. (defun org-make-link (&rest strings)
  5888. "Concatenate STRINGS."
  5889. (apply 'concat strings))
  5890. (defun org-make-link-string (link &optional description)
  5891. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5892. (unless (string-match "\\S-" link)
  5893. (error "Empty link"))
  5894. (when (stringp description)
  5895. ;; Remove brackets from the description, they are fatal.
  5896. (while (string-match "\\[" description)
  5897. (setq description (replace-match "{" t t description)))
  5898. (while (string-match "\\]" description)
  5899. (setq description (replace-match "}" t t description))))
  5900. (when (equal (org-link-escape link) description)
  5901. ;; No description needed, it is identical
  5902. (setq description nil))
  5903. (when (and (not description)
  5904. (not (equal link (org-link-escape link))))
  5905. (setq description link))
  5906. (concat "[[" (org-link-escape link) "]"
  5907. (if description (concat "[" description "]") "")
  5908. "]"))
  5909. (defconst org-link-escape-chars
  5910. '((?\ . "%20")
  5911. (?\[ . "%5B")
  5912. (?\] . "%5D")
  5913. (?\340 . "%E0") ; `a
  5914. (?\342 . "%E2") ; ^a
  5915. (?\347 . "%E7") ; ,c
  5916. (?\350 . "%E8") ; `e
  5917. (?\351 . "%E9") ; 'e
  5918. (?\352 . "%EA") ; ^e
  5919. (?\356 . "%EE") ; ^i
  5920. (?\364 . "%F4") ; ^o
  5921. (?\371 . "%F9") ; `u
  5922. (?\373 . "%FB") ; ^u
  5923. (?\; . "%3B")
  5924. (?? . "%3F")
  5925. (?= . "%3D")
  5926. (?+ . "%2B")
  5927. )
  5928. "Association list of escapes for some characters problematic in links.
  5929. This is the list that is used for internal purposes.")
  5930. (defconst org-link-escape-chars-browser
  5931. '((?\ . "%20")) ; 32 for the SPC char
  5932. "Association list of escapes for some characters problematic in links.
  5933. This is the list that is used before handing over to the browser.")
  5934. (defun org-link-escape (text &optional table)
  5935. "Escape charaters in TEXT that are problematic for links."
  5936. (setq table (or table org-link-escape-chars))
  5937. (when text
  5938. (let ((re (mapconcat (lambda (x) (regexp-quote
  5939. (char-to-string (car x))))
  5940. table "\\|")))
  5941. (while (string-match re text)
  5942. (setq text
  5943. (replace-match
  5944. (cdr (assoc (string-to-char (match-string 0 text))
  5945. table))
  5946. t t text)))
  5947. text)))
  5948. (defun org-link-unescape (text &optional table)
  5949. "Reverse the action of `org-link-escape'."
  5950. (setq table (or table org-link-escape-chars))
  5951. (when text
  5952. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5953. table "\\|")))
  5954. (while (string-match re text)
  5955. (setq text
  5956. (replace-match
  5957. (char-to-string (car (rassoc (match-string 0 text) table)))
  5958. t t text)))
  5959. text)))
  5960. (defun org-xor (a b)
  5961. "Exclusive or."
  5962. (if a (not b) b))
  5963. (defun org-get-header (header)
  5964. "Find a header field in the current buffer."
  5965. (save-excursion
  5966. (goto-char (point-min))
  5967. (let ((case-fold-search t) s)
  5968. (cond
  5969. ((eq header 'from)
  5970. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5971. (setq s (match-string 1)))
  5972. (while (string-match "\"" s)
  5973. (setq s (replace-match "" t t s)))
  5974. (if (string-match "[<(].*" s)
  5975. (setq s (replace-match "" t t s))))
  5976. ((eq header 'message-id)
  5977. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5978. (setq s (match-string 1))))
  5979. ((eq header 'subject)
  5980. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5981. (setq s (match-string 1)))))
  5982. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5983. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5984. s)))
  5985. (defun org-fixup-message-id-for-http (s)
  5986. "Replace special characters in a message id, so it can be used in an http query."
  5987. (while (string-match "<" s)
  5988. (setq s (replace-match "%3C" t t s)))
  5989. (while (string-match ">" s)
  5990. (setq s (replace-match "%3E" t t s)))
  5991. (while (string-match "@" s)
  5992. (setq s (replace-match "%40" t t s)))
  5993. s)
  5994. ;;;###autoload
  5995. (defun org-insert-link-global ()
  5996. "Insert a link like Org-mode does.
  5997. This command can be called in any mode to insert a link in Org-mode syntax."
  5998. (interactive)
  5999. (org-load-modules-maybe)
  6000. (org-run-like-in-org-mode 'org-insert-link))
  6001. (defun org-insert-link (&optional complete-file link-location)
  6002. "Insert a link. At the prompt, enter the link.
  6003. Completion can be used to select a link previously stored with
  6004. `org-store-link'. When the empty string is entered (i.e. if you just
  6005. press RET at the prompt), the link defaults to the most recently
  6006. stored link. As SPC triggers completion in the minibuffer, you need to
  6007. use M-SPC or C-q SPC to force the insertion of a space character.
  6008. You will also be prompted for a description, and if one is given, it will
  6009. be displayed in the buffer instead of the link.
  6010. If there is already a link at point, this command will allow you to edit link
  6011. and description parts.
  6012. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6013. be selected using completion. The path to the file will be relative to the
  6014. current directory if the file is in the current directory or a subdirectory.
  6015. Otherwise, the link will be the absolute path as completed in the minibuffer
  6016. \(i.e. normally ~/path/to/file).
  6017. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6018. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  6019. of `org-keep-stored-link-after-insertion'.
  6020. If `org-make-link-description-function' is non-nil, this function will be
  6021. called with the link target, and the result will be the default
  6022. link description.
  6023. If the LINK-LOCATION parameter is non-nil, this value will be
  6024. used as the link location instead of reading one interactively."
  6025. (interactive "P")
  6026. (let* ((wcf (current-window-configuration))
  6027. (region (if (org-region-active-p)
  6028. (buffer-substring (region-beginning) (region-end))))
  6029. (remove (and region (list (region-beginning) (region-end))))
  6030. (desc region)
  6031. tmphist ; byte-compile incorrectly complains about this
  6032. (link link-location)
  6033. entry file)
  6034. (cond
  6035. (link-location) ; specified by arg, just use it.
  6036. ((org-in-regexp org-bracket-link-regexp 1)
  6037. ;; We do have a link at point, and we are going to edit it.
  6038. (setq remove (list (match-beginning 0) (match-end 0)))
  6039. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6040. (setq link (read-string "Link: "
  6041. (org-link-unescape
  6042. (org-match-string-no-properties 1)))))
  6043. ((or (org-in-regexp org-angle-link-re)
  6044. (org-in-regexp org-plain-link-re))
  6045. ;; Convert to bracket link
  6046. (setq remove (list (match-beginning 0) (match-end 0))
  6047. link (read-string "Link: "
  6048. (org-remove-angle-brackets (match-string 0)))))
  6049. ((equal complete-file '(4))
  6050. ;; Completing read for file names.
  6051. (setq file (read-file-name "File: "))
  6052. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6053. (pwd1 (file-name-as-directory (abbreviate-file-name
  6054. (expand-file-name ".")))))
  6055. (cond
  6056. ((equal complete-file '(16))
  6057. (setq link (org-make-link
  6058. "file:"
  6059. (abbreviate-file-name (expand-file-name file)))))
  6060. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6061. (setq link (org-make-link "file:" (match-string 1 file))))
  6062. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6063. (expand-file-name file))
  6064. (setq link (org-make-link
  6065. "file:" (match-string 1 (expand-file-name file)))))
  6066. (t (setq link (org-make-link "file:" file))))))
  6067. (t
  6068. ;; Read link, with completion for stored links.
  6069. (with-output-to-temp-buffer "*Org Links*"
  6070. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6071. (when org-stored-links
  6072. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6073. (princ (mapconcat
  6074. (lambda (x)
  6075. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6076. (reverse org-stored-links) "\n"))))
  6077. (let ((cw (selected-window)))
  6078. (select-window (get-buffer-window "*Org Links*"))
  6079. (shrink-window-if-larger-than-buffer)
  6080. (setq truncate-lines t)
  6081. (select-window cw))
  6082. ;; Fake a link history, containing the stored links.
  6083. (setq tmphist (append (mapcar 'car org-stored-links)
  6084. org-insert-link-history))
  6085. (unwind-protect
  6086. (setq link (org-completing-read
  6087. "Link: "
  6088. (append
  6089. (mapcar (lambda (x) (list (concat (car x) ":")))
  6090. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6091. (mapcar (lambda (x) (list (concat x ":")))
  6092. org-link-types))
  6093. nil nil nil
  6094. 'tmphist
  6095. (or (car (car org-stored-links)))))
  6096. (set-window-configuration wcf)
  6097. (kill-buffer "*Org Links*"))
  6098. (setq entry (assoc link org-stored-links))
  6099. (or entry (push link org-insert-link-history))
  6100. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6101. (not org-keep-stored-link-after-insertion))
  6102. (setq org-stored-links (delq (assoc link org-stored-links)
  6103. org-stored-links)))
  6104. (setq desc (or desc (nth 1 entry)))))
  6105. (if (string-match org-plain-link-re link)
  6106. ;; URL-like link, normalize the use of angular brackets.
  6107. (setq link (org-make-link (org-remove-angle-brackets link))))
  6108. ;; Check if we are linking to the current file with a search option
  6109. ;; If yes, simplify the link by using only the search option.
  6110. (when (and buffer-file-name
  6111. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6112. (let* ((path (match-string 1 link))
  6113. (case-fold-search nil)
  6114. (search (match-string 2 link)))
  6115. (save-match-data
  6116. (if (equal (file-truename buffer-file-name) (file-truename path))
  6117. ;; We are linking to this same file, with a search option
  6118. (setq link search)))))
  6119. ;; Check if we can/should use a relative path. If yes, simplify the link
  6120. (when (string-match "\\<file:\\(.*\\)" link)
  6121. (let* ((path (match-string 1 link))
  6122. (origpath path)
  6123. (case-fold-search nil))
  6124. (cond
  6125. ((eq org-link-file-path-type 'absolute)
  6126. (setq path (abbreviate-file-name (expand-file-name path))))
  6127. ((eq org-link-file-path-type 'noabbrev)
  6128. (setq path (expand-file-name path)))
  6129. ((eq org-link-file-path-type 'relative)
  6130. (setq path (file-relative-name path)))
  6131. (t
  6132. (save-match-data
  6133. (if (string-match (concat "^" (regexp-quote
  6134. (file-name-as-directory
  6135. (expand-file-name "."))))
  6136. (expand-file-name path))
  6137. ;; We are linking a file with relative path name.
  6138. (setq path (substring (expand-file-name path)
  6139. (match-end 0)))))))
  6140. (setq link (concat "file:" path))
  6141. (if (equal desc origpath)
  6142. (setq desc path))))
  6143. (if org-make-link-description-function
  6144. (setq desc (funcall org-make-link-description-function link desc)))
  6145. (setq desc (read-string "Description: " desc))
  6146. (unless (string-match "\\S-" desc) (setq desc nil))
  6147. (if remove (apply 'delete-region remove))
  6148. (insert (org-make-link-string link desc))))
  6149. (defun org-completing-read (&rest args)
  6150. (let ((minibuffer-local-completion-map
  6151. (copy-keymap minibuffer-local-completion-map)))
  6152. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6153. (apply 'completing-read args)))
  6154. ;;; Opening/following a link
  6155. (defvar org-link-search-failed nil)
  6156. (defun org-next-link ()
  6157. "Move forward to the next link.
  6158. If the link is in hidden text, expose it."
  6159. (interactive)
  6160. (when (and org-link-search-failed (eq this-command last-command))
  6161. (goto-char (point-min))
  6162. (message "Link search wrapped back to beginning of buffer"))
  6163. (setq org-link-search-failed nil)
  6164. (let* ((pos (point))
  6165. (ct (org-context))
  6166. (a (assoc :link ct)))
  6167. (if a (goto-char (nth 2 a)))
  6168. (if (re-search-forward org-any-link-re nil t)
  6169. (progn
  6170. (goto-char (match-beginning 0))
  6171. (if (org-invisible-p) (org-show-context)))
  6172. (goto-char pos)
  6173. (setq org-link-search-failed t)
  6174. (error "No further link found"))))
  6175. (defun org-previous-link ()
  6176. "Move backward to the previous link.
  6177. If the link is in hidden text, expose it."
  6178. (interactive)
  6179. (when (and org-link-search-failed (eq this-command last-command))
  6180. (goto-char (point-max))
  6181. (message "Link search wrapped back to end of buffer"))
  6182. (setq org-link-search-failed nil)
  6183. (let* ((pos (point))
  6184. (ct (org-context))
  6185. (a (assoc :link ct)))
  6186. (if a (goto-char (nth 1 a)))
  6187. (if (re-search-backward org-any-link-re nil t)
  6188. (progn
  6189. (goto-char (match-beginning 0))
  6190. (if (org-invisible-p) (org-show-context)))
  6191. (goto-char pos)
  6192. (setq org-link-search-failed t)
  6193. (error "No further link found"))))
  6194. (defun org-find-file-at-mouse (ev)
  6195. "Open file link or URL at mouse."
  6196. (interactive "e")
  6197. (mouse-set-point ev)
  6198. (org-open-at-point 'in-emacs))
  6199. (defun org-open-at-mouse (ev)
  6200. "Open file link or URL at mouse."
  6201. (interactive "e")
  6202. (mouse-set-point ev)
  6203. (org-open-at-point))
  6204. (defvar org-window-config-before-follow-link nil
  6205. "The window configuration before following a link.
  6206. This is saved in case the need arises to restore it.")
  6207. (defvar org-open-link-marker (make-marker)
  6208. "Marker pointing to the location where `org-open-at-point; was called.")
  6209. ;;;###autoload
  6210. (defun org-open-at-point-global ()
  6211. "Follow a link like Org-mode does.
  6212. This command can be called in any mode to follow a link that has
  6213. Org-mode syntax."
  6214. (interactive)
  6215. (org-run-like-in-org-mode 'org-open-at-point))
  6216. ;;;###autoload
  6217. (defun org-open-link-from-string (s &optional arg)
  6218. "Open a link in the string S, as if it was in Org-mode."
  6219. (interactive "sLink: \nP")
  6220. (with-temp-buffer
  6221. (let ((org-inhibit-startup t))
  6222. (org-mode)
  6223. (insert s)
  6224. (goto-char (point-min))
  6225. (org-open-at-point arg))))
  6226. (defun org-open-at-point (&optional in-emacs)
  6227. "Open link at or after point.
  6228. If there is no link at point, this function will search forward up to
  6229. the end of the current subtree.
  6230. Normally, files will be opened by an appropriate application. If the
  6231. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6232. (interactive "P")
  6233. (org-load-modules-maybe)
  6234. (move-marker org-open-link-marker (point))
  6235. (setq org-window-config-before-follow-link (current-window-configuration))
  6236. (org-remove-occur-highlights nil nil t)
  6237. (if (org-at-timestamp-p t)
  6238. (org-follow-timestamp-link)
  6239. (let (type path link line search (pos (point)))
  6240. (catch 'match
  6241. (save-excursion
  6242. (skip-chars-forward "^]\n\r")
  6243. (when (org-in-regexp org-bracket-link-regexp)
  6244. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6245. (while (string-match " *\n *" link)
  6246. (setq link (replace-match " " t t link)))
  6247. (setq link (org-link-expand-abbrev link))
  6248. (if (string-match org-link-re-with-space2 link)
  6249. (setq type (match-string 1 link) path (match-string 2 link))
  6250. (setq type "thisfile" path link))
  6251. (throw 'match t)))
  6252. (when (get-text-property (point) 'org-linked-text)
  6253. (setq type "thisfile"
  6254. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6255. (1+ (point)) (point))
  6256. path (buffer-substring
  6257. (previous-single-property-change pos 'org-linked-text)
  6258. (next-single-property-change pos 'org-linked-text)))
  6259. (throw 'match t))
  6260. (save-excursion
  6261. (when (or (org-in-regexp org-angle-link-re)
  6262. (org-in-regexp org-plain-link-re))
  6263. (setq type (match-string 1) path (match-string 2))
  6264. (throw 'match t)))
  6265. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6266. (setq type "tree-match"
  6267. path (match-string 1))
  6268. (throw 'match t))
  6269. (save-excursion
  6270. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6271. (setq type "tags"
  6272. path (match-string 1))
  6273. (while (string-match ":" path)
  6274. (setq path (replace-match "+" t t path)))
  6275. (throw 'match t))))
  6276. (unless path
  6277. (error "No link found"))
  6278. ;; Remove any trailing spaces in path
  6279. (if (string-match " +\\'" path)
  6280. (setq path (replace-match "" t t path)))
  6281. (cond
  6282. ((assoc type org-link-protocols)
  6283. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6284. ((equal type "mailto")
  6285. (let ((cmd (car org-link-mailto-program))
  6286. (args (cdr org-link-mailto-program)) args1
  6287. (address path) (subject "") a)
  6288. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6289. (setq address (match-string 1 path)
  6290. subject (org-link-escape (match-string 2 path))))
  6291. (while args
  6292. (cond
  6293. ((not (stringp (car args))) (push (pop args) args1))
  6294. (t (setq a (pop args))
  6295. (if (string-match "%a" a)
  6296. (setq a (replace-match address t t a)))
  6297. (if (string-match "%s" a)
  6298. (setq a (replace-match subject t t a)))
  6299. (push a args1))))
  6300. (apply cmd (nreverse args1))))
  6301. ((member type '("http" "https" "ftp" "news"))
  6302. (browse-url (concat type ":" (org-link-escape
  6303. path org-link-escape-chars-browser))))
  6304. ((member type '("message"))
  6305. (browse-url (concat type ":" path)))
  6306. ((string= type "tags")
  6307. (org-tags-view in-emacs path))
  6308. ((string= type "thisfile")
  6309. (if in-emacs
  6310. (switch-to-buffer-other-window
  6311. (org-get-buffer-for-internal-link (current-buffer)))
  6312. (org-mark-ring-push))
  6313. (let ((cmd `(org-link-search
  6314. ,path
  6315. ,(cond ((equal in-emacs '(4)) 'occur)
  6316. ((equal in-emacs '(16)) 'org-occur)
  6317. (t nil))
  6318. ,pos)))
  6319. (condition-case nil (eval cmd)
  6320. (error (progn (widen) (eval cmd))))))
  6321. ((string= type "tree-match")
  6322. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6323. ((string= type "file")
  6324. (if (string-match "::\\([0-9]+\\)\\'" path)
  6325. (setq line (string-to-number (match-string 1 path))
  6326. path (substring path 0 (match-beginning 0)))
  6327. (if (string-match "::\\(.+\\)\\'" path)
  6328. (setq search (match-string 1 path)
  6329. path (substring path 0 (match-beginning 0)))))
  6330. (if (string-match "[*?{]" (file-name-nondirectory path))
  6331. (dired path)
  6332. (org-open-file path in-emacs line search)))
  6333. ((string= type "news")
  6334. (require 'org-gnus)
  6335. (org-gnus-follow-link path))
  6336. ((string= type "shell")
  6337. (let ((cmd path))
  6338. (if (or (not org-confirm-shell-link-function)
  6339. (funcall org-confirm-shell-link-function
  6340. (format "Execute \"%s\" in shell? "
  6341. (org-add-props cmd nil
  6342. 'face 'org-warning))))
  6343. (progn
  6344. (message "Executing %s" cmd)
  6345. (shell-command cmd))
  6346. (error "Abort"))))
  6347. ((string= type "elisp")
  6348. (let ((cmd path))
  6349. (if (or (not org-confirm-elisp-link-function)
  6350. (funcall org-confirm-elisp-link-function
  6351. (format "Execute \"%s\" as elisp? "
  6352. (org-add-props cmd nil
  6353. 'face 'org-warning))))
  6354. (message "%s => %s" cmd (eval (read cmd)))
  6355. (error "Abort"))))
  6356. (t
  6357. (browse-url-at-point)))))
  6358. (move-marker org-open-link-marker nil)
  6359. (run-hook-with-args 'org-follow-link-hook))
  6360. ;;;; Time estimates
  6361. (defun org-get-effort (&optional pom)
  6362. "Get the effort estimate for the current entry."
  6363. (org-entry-get pom org-effort-property))
  6364. ;;; File search
  6365. (defvar org-create-file-search-functions nil
  6366. "List of functions to construct the right search string for a file link.
  6367. These functions are called in turn with point at the location to
  6368. which the link should point.
  6369. A function in the hook should first test if it would like to
  6370. handle this file type, for example by checking the major-mode or
  6371. the file extension. If it decides not to handle this file, it
  6372. should just return nil to give other functions a chance. If it
  6373. does handle the file, it must return the search string to be used
  6374. when following the link. The search string will be part of the
  6375. file link, given after a double colon, and `org-open-at-point'
  6376. will automatically search for it. If special measures must be
  6377. taken to make the search successful, another function should be
  6378. added to the companion hook `org-execute-file-search-functions',
  6379. which see.
  6380. A function in this hook may also use `setq' to set the variable
  6381. `description' to provide a suggestion for the descriptive text to
  6382. be used for this link when it gets inserted into an Org-mode
  6383. buffer with \\[org-insert-link].")
  6384. (defvar org-execute-file-search-functions nil
  6385. "List of functions to execute a file search triggered by a link.
  6386. Functions added to this hook must accept a single argument, the
  6387. search string that was part of the file link, the part after the
  6388. double colon. The function must first check if it would like to
  6389. handle this search, for example by checking the major-mode or the
  6390. file extension. If it decides not to handle this search, it
  6391. should just return nil to give other functions a chance. If it
  6392. does handle the search, it must return a non-nil value to keep
  6393. other functions from trying.
  6394. Each function can access the current prefix argument through the
  6395. variable `current-prefix-argument'. Note that a single prefix is
  6396. used to force opening a link in Emacs, so it may be good to only
  6397. use a numeric or double prefix to guide the search function.
  6398. In case this is needed, a function in this hook can also restore
  6399. the window configuration before `org-open-at-point' was called using:
  6400. (set-window-configuration org-window-config-before-follow-link)")
  6401. (defun org-link-search (s &optional type avoid-pos)
  6402. "Search for a link search option.
  6403. If S is surrounded by forward slashes, it is interpreted as a
  6404. regular expression. In org-mode files, this will create an `org-occur'
  6405. sparse tree. In ordinary files, `occur' will be used to list matches.
  6406. If the current buffer is in `dired-mode', grep will be used to search
  6407. in all files. If AVOID-POS is given, ignore matches near that position."
  6408. (let ((case-fold-search t)
  6409. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6410. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6411. (append '(("") (" ") ("\t") ("\n"))
  6412. org-emphasis-alist)
  6413. "\\|") "\\)"))
  6414. (pos (point))
  6415. (pre nil) (post nil)
  6416. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6417. (cond
  6418. ;; First check if there are any special
  6419. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6420. ;; Now try the builtin stuff
  6421. ((save-excursion
  6422. (goto-char (point-min))
  6423. (and
  6424. (re-search-forward
  6425. (concat "<<" (regexp-quote s0) ">>") nil t)
  6426. (setq type 'dedicated
  6427. pos (match-beginning 0))))
  6428. ;; There is an exact target for this
  6429. (goto-char pos))
  6430. ((string-match "^/\\(.*\\)/$" s)
  6431. ;; A regular expression
  6432. (cond
  6433. ((org-mode-p)
  6434. (org-occur (match-string 1 s)))
  6435. ;;((eq major-mode 'dired-mode)
  6436. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6437. (t (org-do-occur (match-string 1 s)))))
  6438. (t
  6439. ;; A normal search strings
  6440. (when (equal (string-to-char s) ?*)
  6441. ;; Anchor on headlines, post may include tags.
  6442. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6443. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6444. s (substring s 1)))
  6445. (remove-text-properties
  6446. 0 (length s)
  6447. '(face nil mouse-face nil keymap nil fontified nil) s)
  6448. ;; Make a series of regular expressions to find a match
  6449. (setq words (org-split-string s "[ \n\r\t]+")
  6450. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6451. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6452. "\\)" markers)
  6453. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6454. re2a (concat "[ \t\r\n]" re2a_)
  6455. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6456. re4 (concat "[^a-zA-Z_]" re4_)
  6457. re1 (concat pre re2 post)
  6458. re3 (concat pre (if pre re4_ re4) post)
  6459. re5 (concat pre ".*" re4)
  6460. re2 (concat pre re2)
  6461. re2a (concat pre (if pre re2a_ re2a))
  6462. re4 (concat pre (if pre re4_ re4))
  6463. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6464. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6465. re5 "\\)"
  6466. ))
  6467. (cond
  6468. ((eq type 'org-occur) (org-occur reall))
  6469. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6470. (t (goto-char (point-min))
  6471. (setq type 'fuzzy)
  6472. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6473. (org-search-not-self 1 re1 nil t)
  6474. (org-search-not-self 1 re2 nil t)
  6475. (org-search-not-self 1 re2a nil t)
  6476. (org-search-not-self 1 re3 nil t)
  6477. (org-search-not-self 1 re4 nil t)
  6478. (org-search-not-self 1 re5 nil t)
  6479. )
  6480. (goto-char (match-beginning 1))
  6481. (goto-char pos)
  6482. (error "No match")))))
  6483. (t
  6484. ;; Normal string-search
  6485. (goto-char (point-min))
  6486. (if (search-forward s nil t)
  6487. (goto-char (match-beginning 0))
  6488. (error "No match"))))
  6489. (and (org-mode-p) (org-show-context 'link-search))
  6490. type))
  6491. (defun org-search-not-self (group &rest args)
  6492. "Execute `re-search-forward', but only accept matches that do not
  6493. enclose the position of `org-open-link-marker'."
  6494. (let ((m org-open-link-marker))
  6495. (catch 'exit
  6496. (while (apply 're-search-forward args)
  6497. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6498. (goto-char (match-end group))
  6499. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6500. (> (match-beginning 0) (marker-position m))
  6501. (< (match-end 0) (marker-position m)))
  6502. (save-match-data
  6503. (or (not (org-in-regexp
  6504. org-bracket-link-analytic-regexp 1))
  6505. (not (match-end 4)) ; no description
  6506. (and (<= (match-beginning 4) (point))
  6507. (>= (match-end 4) (point))))))
  6508. (throw 'exit (point))))))))
  6509. (defun org-get-buffer-for-internal-link (buffer)
  6510. "Return a buffer to be used for displaying the link target of internal links."
  6511. (cond
  6512. ((not org-display-internal-link-with-indirect-buffer)
  6513. buffer)
  6514. ((string-match "(Clone)$" (buffer-name buffer))
  6515. (message "Buffer is already a clone, not making another one")
  6516. ;; we also do not modify visibility in this case
  6517. buffer)
  6518. (t ; make a new indirect buffer for displaying the link
  6519. (let* ((bn (buffer-name buffer))
  6520. (ibn (concat bn "(Clone)"))
  6521. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6522. (with-current-buffer ib (org-overview))
  6523. ib))))
  6524. (defun org-do-occur (regexp &optional cleanup)
  6525. "Call the Emacs command `occur'.
  6526. If CLEANUP is non-nil, remove the printout of the regular expression
  6527. in the *Occur* buffer. This is useful if the regex is long and not useful
  6528. to read."
  6529. (occur regexp)
  6530. (when cleanup
  6531. (let ((cwin (selected-window)) win beg end)
  6532. (when (setq win (get-buffer-window "*Occur*"))
  6533. (select-window win))
  6534. (goto-char (point-min))
  6535. (when (re-search-forward "match[a-z]+" nil t)
  6536. (setq beg (match-end 0))
  6537. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6538. (setq end (1- (match-beginning 0)))))
  6539. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6540. (goto-char (point-min))
  6541. (select-window cwin))))
  6542. ;;; The mark ring for links jumps
  6543. (defvar org-mark-ring nil
  6544. "Mark ring for positions before jumps in Org-mode.")
  6545. (defvar org-mark-ring-last-goto nil
  6546. "Last position in the mark ring used to go back.")
  6547. ;; Fill and close the ring
  6548. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6549. (loop for i from 1 to org-mark-ring-length do
  6550. (push (make-marker) org-mark-ring))
  6551. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6552. org-mark-ring)
  6553. (defun org-mark-ring-push (&optional pos buffer)
  6554. "Put the current position or POS into the mark ring and rotate it."
  6555. (interactive)
  6556. (setq pos (or pos (point)))
  6557. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6558. (move-marker (car org-mark-ring)
  6559. (or pos (point))
  6560. (or buffer (current-buffer)))
  6561. (message "%s"
  6562. (substitute-command-keys
  6563. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6564. (defun org-mark-ring-goto (&optional n)
  6565. "Jump to the previous position in the mark ring.
  6566. With prefix arg N, jump back that many stored positions. When
  6567. called several times in succession, walk through the entire ring.
  6568. Org-mode commands jumping to a different position in the current file,
  6569. or to another Org-mode file, automatically push the old position
  6570. onto the ring."
  6571. (interactive "p")
  6572. (let (p m)
  6573. (if (eq last-command this-command)
  6574. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6575. (setq p org-mark-ring))
  6576. (setq org-mark-ring-last-goto p)
  6577. (setq m (car p))
  6578. (switch-to-buffer (marker-buffer m))
  6579. (goto-char m)
  6580. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6581. (defun org-remove-angle-brackets (s)
  6582. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6583. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6584. s)
  6585. (defun org-add-angle-brackets (s)
  6586. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6587. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6588. s)
  6589. (defun org-remove-double-quotes (s)
  6590. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6591. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6592. s)
  6593. ;;; Following specific links
  6594. (defun org-follow-timestamp-link ()
  6595. (cond
  6596. ((org-at-date-range-p t)
  6597. (let ((org-agenda-start-on-weekday)
  6598. (t1 (match-string 1))
  6599. (t2 (match-string 2)))
  6600. (setq t1 (time-to-days (org-time-string-to-time t1))
  6601. t2 (time-to-days (org-time-string-to-time t2)))
  6602. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6603. ((org-at-timestamp-p t)
  6604. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6605. (substring (match-string 1) 0 10)))
  6606. 1))
  6607. (t (error "This should not happen"))))
  6608. ;;; Following file links
  6609. (defvar org-wait nil)
  6610. (defun org-open-file (path &optional in-emacs line search)
  6611. "Open the file at PATH.
  6612. First, this expands any special file name abbreviations. Then the
  6613. configuration variable `org-file-apps' is checked if it contains an
  6614. entry for this file type, and if yes, the corresponding command is launched.
  6615. If no application is found, Emacs simply visits the file.
  6616. With optional argument IN-EMACS, Emacs will visit the file.
  6617. Optional LINE specifies a line to go to, optional SEARCH a string to
  6618. search for. If LINE or SEARCH is given, the file will always be
  6619. opened in Emacs.
  6620. If the file does not exist, an error is thrown."
  6621. (setq in-emacs (or in-emacs line search))
  6622. (let* ((file (if (equal path "")
  6623. buffer-file-name
  6624. (substitute-in-file-name (expand-file-name path))))
  6625. (apps (append org-file-apps (org-default-apps)))
  6626. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6627. (dirp (if remp nil (file-directory-p file)))
  6628. (dfile (downcase file))
  6629. (old-buffer (current-buffer))
  6630. (old-pos (point))
  6631. (old-mode major-mode)
  6632. ext cmd)
  6633. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6634. (setq ext (match-string 1 dfile))
  6635. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6636. (setq ext (match-string 1 dfile))))
  6637. (if in-emacs
  6638. (setq cmd 'emacs)
  6639. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6640. (and dirp (cdr (assoc 'directory apps)))
  6641. (cdr (assoc ext apps))
  6642. (cdr (assoc t apps)))))
  6643. (when (eq cmd 'mailcap)
  6644. (require 'mailcap)
  6645. (mailcap-parse-mailcaps)
  6646. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6647. (command (mailcap-mime-info mime-type)))
  6648. (if (stringp command)
  6649. (setq cmd command)
  6650. (setq cmd 'emacs))))
  6651. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6652. (not (file-exists-p file))
  6653. (not org-open-non-existing-files))
  6654. (error "No such file: %s" file))
  6655. (cond
  6656. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6657. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6658. (while (string-match "['\"]%s['\"]" cmd)
  6659. (setq cmd (replace-match "%s" t t cmd)))
  6660. (while (string-match "%s" cmd)
  6661. (setq cmd (replace-match
  6662. (save-match-data (shell-quote-argument file))
  6663. t t cmd)))
  6664. (save-window-excursion
  6665. (start-process-shell-command cmd nil cmd)
  6666. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6667. ))
  6668. ((or (stringp cmd)
  6669. (eq cmd 'emacs))
  6670. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6671. (widen)
  6672. (if line (goto-line line)
  6673. (if search (org-link-search search))))
  6674. ((consp cmd)
  6675. (eval cmd))
  6676. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6677. (and (org-mode-p) (eq old-mode 'org-mode)
  6678. (or (not (equal old-buffer (current-buffer)))
  6679. (not (equal old-pos (point))))
  6680. (org-mark-ring-push old-pos old-buffer))))
  6681. (defun org-default-apps ()
  6682. "Return the default applications for this operating system."
  6683. (cond
  6684. ((eq system-type 'darwin)
  6685. org-file-apps-defaults-macosx)
  6686. ((eq system-type 'windows-nt)
  6687. org-file-apps-defaults-windowsnt)
  6688. (t org-file-apps-defaults-gnu)))
  6689. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6690. (defun org-file-remote-p (file)
  6691. "Test whether FILE specifies a location on a remote system.
  6692. Return non-nil if the location is indeed remote.
  6693. For example, the filename \"/user@host:/foo\" specifies a location
  6694. on the system \"/user@host:\"."
  6695. (cond ((fboundp 'file-remote-p)
  6696. (file-remote-p file))
  6697. ((fboundp 'tramp-handle-file-remote-p)
  6698. (tramp-handle-file-remote-p file))
  6699. ((and (boundp 'ange-ftp-name-format)
  6700. (string-match (car ange-ftp-name-format) file))
  6701. t)
  6702. (t nil)))
  6703. ;;;; Refiling
  6704. (defun org-get-org-file ()
  6705. "Read a filename, with default directory `org-directory'."
  6706. (let ((default (or org-default-notes-file remember-data-file)))
  6707. (read-file-name (format "File name [%s]: " default)
  6708. (file-name-as-directory org-directory)
  6709. default)))
  6710. (defun org-notes-order-reversed-p ()
  6711. "Check if the current file should receive notes in reversed order."
  6712. (cond
  6713. ((not org-reverse-note-order) nil)
  6714. ((eq t org-reverse-note-order) t)
  6715. ((not (listp org-reverse-note-order)) nil)
  6716. (t (catch 'exit
  6717. (let ((all org-reverse-note-order)
  6718. entry)
  6719. (while (setq entry (pop all))
  6720. (if (string-match (car entry) buffer-file-name)
  6721. (throw 'exit (cdr entry))))
  6722. nil)))))
  6723. (defvar org-refile-target-table nil
  6724. "The list of refile targets, created by `org-refile'.")
  6725. (defvar org-agenda-new-buffers nil
  6726. "Buffers created to visit agenda files.")
  6727. (defun org-get-refile-targets (&optional default-buffer)
  6728. "Produce a table with refile targets."
  6729. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6730. targets txt re files f desc descre)
  6731. (with-current-buffer (or default-buffer (current-buffer))
  6732. (while (setq entry (pop entries))
  6733. (setq files (car entry) desc (cdr entry))
  6734. (cond
  6735. ((null files) (setq files (list (current-buffer))))
  6736. ((eq files 'org-agenda-files)
  6737. (setq files (org-agenda-files 'unrestricted)))
  6738. ((and (symbolp files) (fboundp files))
  6739. (setq files (funcall files)))
  6740. ((and (symbolp files) (boundp files))
  6741. (setq files (symbol-value files))))
  6742. (if (stringp files) (setq files (list files)))
  6743. (cond
  6744. ((eq (car desc) :tag)
  6745. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6746. ((eq (car desc) :todo)
  6747. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6748. ((eq (car desc) :regexp)
  6749. (setq descre (cdr desc)))
  6750. ((eq (car desc) :level)
  6751. (setq descre (concat "^\\*\\{" (number-to-string
  6752. (if org-odd-levels-only
  6753. (1- (* 2 (cdr desc)))
  6754. (cdr desc)))
  6755. "\\}[ \t]")))
  6756. ((eq (car desc) :maxlevel)
  6757. (setq descre (concat "^\\*\\{1," (number-to-string
  6758. (if org-odd-levels-only
  6759. (1- (* 2 (cdr desc)))
  6760. (cdr desc)))
  6761. "\\}[ \t]")))
  6762. (t (error "Bad refiling target description %s" desc)))
  6763. (while (setq f (pop files))
  6764. (save-excursion
  6765. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6766. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6767. (save-excursion
  6768. (save-restriction
  6769. (widen)
  6770. (goto-char (point-min))
  6771. (while (re-search-forward descre nil t)
  6772. (goto-char (point-at-bol))
  6773. (when (looking-at org-complex-heading-regexp)
  6774. (setq txt (match-string 4)
  6775. re (concat "^" (regexp-quote
  6776. (buffer-substring (match-beginning 1)
  6777. (match-end 4)))))
  6778. (if (match-end 5) (setq re (concat re "[ \t]+"
  6779. (regexp-quote
  6780. (match-string 5)))))
  6781. (setq re (concat re "[ \t]*$"))
  6782. (when org-refile-use-outline-path
  6783. (setq txt (mapconcat 'identity
  6784. (append
  6785. (if (eq org-refile-use-outline-path 'file)
  6786. (list (file-name-nondirectory
  6787. (buffer-file-name (buffer-base-buffer))))
  6788. (if (eq org-refile-use-outline-path 'full-file-path)
  6789. (list (buffer-file-name (buffer-base-buffer)))))
  6790. (org-get-outline-path)
  6791. (list txt))
  6792. "/")))
  6793. (push (list txt f re (point)) targets))
  6794. (goto-char (point-at-eol))))))))
  6795. (nreverse targets))))
  6796. (defun org-get-outline-path ()
  6797. "Return the outline path to the current entry, as a list."
  6798. (let (rtn)
  6799. (save-excursion
  6800. (while (org-up-heading-safe)
  6801. (when (looking-at org-complex-heading-regexp)
  6802. (push (org-match-string-no-properties 4) rtn)))
  6803. rtn)))
  6804. (defvar org-refile-history nil
  6805. "History for refiling operations.")
  6806. (defun org-refile (&optional goto default-buffer)
  6807. "Move the entry at point to another heading.
  6808. The list of target headings is compiled using the information in
  6809. `org-refile-targets', which see. This list is created before each use
  6810. and will therefore always be up-to-date.
  6811. At the target location, the entry is filed as a subitem of the target heading.
  6812. Depending on `org-reverse-note-order', the new subitem will either be the
  6813. first of the last subitem.
  6814. With prefix arg GOTO, the command will only visit the target location,
  6815. not actually move anything.
  6816. With a double prefix `C-c C-c', go to the location where the last refiling
  6817. operation has put the subtree."
  6818. (interactive "P")
  6819. (let* ((cbuf (current-buffer))
  6820. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6821. pos it nbuf file re level reversed)
  6822. (if (equal goto '(16))
  6823. (org-refile-goto-last-stored)
  6824. (when (setq it (org-refile-get-location
  6825. (if goto "Goto: " "Refile to: ") default-buffer))
  6826. (setq file (nth 1 it)
  6827. re (nth 2 it)
  6828. pos (nth 3 it))
  6829. (setq nbuf (or (find-buffer-visiting file)
  6830. (find-file-noselect file)))
  6831. (if goto
  6832. (progn
  6833. (switch-to-buffer nbuf)
  6834. (goto-char pos)
  6835. (org-show-context 'org-goto))
  6836. (org-copy-subtree 1 nil t)
  6837. (save-excursion
  6838. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6839. (find-file-noselect file))))
  6840. (setq reversed (org-notes-order-reversed-p))
  6841. (save-excursion
  6842. (save-restriction
  6843. (widen)
  6844. (goto-char pos)
  6845. (looking-at outline-regexp)
  6846. (setq level (org-get-valid-level (funcall outline-level) 1))
  6847. (goto-char
  6848. (if reversed
  6849. (outline-next-heading)
  6850. (or (save-excursion (outline-get-next-sibling))
  6851. (org-end-of-subtree t t)
  6852. (point-max))))
  6853. (bookmark-set "org-refile-last-stored")
  6854. (org-paste-subtree level))))
  6855. (org-cut-subtree)
  6856. (setq org-markers-to-move nil)
  6857. (message "Entry refiled to \"%s\"" (car it)))))))
  6858. (defun org-refile-goto-last-stored ()
  6859. "Go to the location where the last refile was stored."
  6860. (interactive)
  6861. (bookmark-jump "org-refile-last-stored")
  6862. (message "This is the location of the last refile"))
  6863. (defun org-refile-get-location (&optional prompt default-buffer)
  6864. "Prompt the user for a refile location, using PROMPT."
  6865. (let ((org-refile-targets org-refile-targets)
  6866. (org-refile-use-outline-path org-refile-use-outline-path))
  6867. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6868. (unless org-refile-target-table
  6869. (error "No refile targets"))
  6870. (let* ((cbuf (current-buffer))
  6871. (cfunc (if org-refile-use-outline-path
  6872. 'org-olpath-completing-read
  6873. 'completing-read))
  6874. (extra (if org-refile-use-outline-path "/" ""))
  6875. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6876. (fname (and filename (file-truename filename)))
  6877. (tbl (mapcar
  6878. (lambda (x)
  6879. (if (not (equal fname (file-truename (nth 1 x))))
  6880. (cons (concat (car x) extra " ("
  6881. (file-name-nondirectory (nth 1 x)) ")")
  6882. (cdr x))
  6883. (cons (concat (car x) extra) (cdr x))))
  6884. org-refile-target-table))
  6885. (completion-ignore-case t))
  6886. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6887. tbl)))
  6888. (defun org-olpath-completing-read (prompt collection &rest args)
  6889. "Read an outline path like a file name."
  6890. (let ((thetable collection))
  6891. (apply
  6892. 'completing-read prompt
  6893. (lambda (string predicate &optional flag)
  6894. (let (rtn r s f (l (length string)))
  6895. (cond
  6896. ((eq flag nil)
  6897. ;; try completion
  6898. (try-completion string thetable))
  6899. ((eq flag t)
  6900. ;; all-completions
  6901. (setq rtn (all-completions string thetable predicate))
  6902. (mapcar
  6903. (lambda (x)
  6904. (setq r (substring x l))
  6905. (if (string-match " ([^)]*)$" x)
  6906. (setq f (match-string 0 x))
  6907. (setq f ""))
  6908. (if (string-match "/" r)
  6909. (concat string (substring r 0 (match-end 0)) f)
  6910. x))
  6911. rtn))
  6912. ((eq flag 'lambda)
  6913. ;; exact match?
  6914. (assoc string thetable)))
  6915. ))
  6916. args)))
  6917. ;;;; Dynamic blocks
  6918. (defun org-find-dblock (name)
  6919. "Find the first dynamic block with name NAME in the buffer.
  6920. If not found, stay at current position and return nil."
  6921. (let (pos)
  6922. (save-excursion
  6923. (goto-char (point-min))
  6924. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6925. nil t)
  6926. (match-beginning 0))))
  6927. (if pos (goto-char pos))
  6928. pos))
  6929. (defconst org-dblock-start-re
  6930. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6931. "Matches the startline of a dynamic block, with parameters.")
  6932. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6933. "Matches the end of a dyhamic block.")
  6934. (defun org-create-dblock (plist)
  6935. "Create a dynamic block section, with parameters taken from PLIST.
  6936. PLIST must containe a :name entry which is used as name of the block."
  6937. (unless (bolp) (newline))
  6938. (let ((name (plist-get plist :name)))
  6939. (insert "#+BEGIN: " name)
  6940. (while plist
  6941. (if (eq (car plist) :name)
  6942. (setq plist (cddr plist))
  6943. (insert " " (prin1-to-string (pop plist)))))
  6944. (insert "\n\n#+END:\n")
  6945. (beginning-of-line -2)))
  6946. (defun org-prepare-dblock ()
  6947. "Prepare dynamic block for refresh.
  6948. This empties the block, puts the cursor at the insert position and returns
  6949. the property list including an extra property :name with the block name."
  6950. (unless (looking-at org-dblock-start-re)
  6951. (error "Not at a dynamic block"))
  6952. (let* ((begdel (1+ (match-end 0)))
  6953. (name (org-no-properties (match-string 1)))
  6954. (params (append (list :name name)
  6955. (read (concat "(" (match-string 3) ")")))))
  6956. (unless (re-search-forward org-dblock-end-re nil t)
  6957. (error "Dynamic block not terminated"))
  6958. (setq params
  6959. (append params
  6960. (list :content (buffer-substring
  6961. begdel (match-beginning 0)))))
  6962. (delete-region begdel (match-beginning 0))
  6963. (goto-char begdel)
  6964. (open-line 1)
  6965. params))
  6966. (defun org-map-dblocks (&optional command)
  6967. "Apply COMMAND to all dynamic blocks in the current buffer.
  6968. If COMMAND is not given, use `org-update-dblock'."
  6969. (let ((cmd (or command 'org-update-dblock))
  6970. pos)
  6971. (save-excursion
  6972. (goto-char (point-min))
  6973. (while (re-search-forward org-dblock-start-re nil t)
  6974. (goto-char (setq pos (match-beginning 0)))
  6975. (condition-case nil
  6976. (funcall cmd)
  6977. (error (message "Error during update of dynamic block")))
  6978. (goto-char pos)
  6979. (unless (re-search-forward org-dblock-end-re nil t)
  6980. (error "Dynamic block not terminated"))))))
  6981. (defun org-dblock-update (&optional arg)
  6982. "User command for updating dynamic blocks.
  6983. Update the dynamic block at point. With prefix ARG, update all dynamic
  6984. blocks in the buffer."
  6985. (interactive "P")
  6986. (if arg
  6987. (org-update-all-dblocks)
  6988. (or (looking-at org-dblock-start-re)
  6989. (org-beginning-of-dblock))
  6990. (org-update-dblock)))
  6991. (defun org-update-dblock ()
  6992. "Update the dynamic block at point
  6993. This means to empty the block, parse for parameters and then call
  6994. the correct writing function."
  6995. (save-window-excursion
  6996. (let* ((pos (point))
  6997. (line (org-current-line))
  6998. (params (org-prepare-dblock))
  6999. (name (plist-get params :name))
  7000. (cmd (intern (concat "org-dblock-write:" name))))
  7001. (message "Updating dynamic block `%s' at line %d..." name line)
  7002. (funcall cmd params)
  7003. (message "Updating dynamic block `%s' at line %d...done" name line)
  7004. (goto-char pos))))
  7005. (defun org-beginning-of-dblock ()
  7006. "Find the beginning of the dynamic block at point.
  7007. Error if there is no scuh block at point."
  7008. (let ((pos (point))
  7009. beg)
  7010. (end-of-line 1)
  7011. (if (and (re-search-backward org-dblock-start-re nil t)
  7012. (setq beg (match-beginning 0))
  7013. (re-search-forward org-dblock-end-re nil t)
  7014. (> (match-end 0) pos))
  7015. (goto-char beg)
  7016. (goto-char pos)
  7017. (error "Not in a dynamic block"))))
  7018. (defun org-update-all-dblocks ()
  7019. "Update all dynamic blocks in the buffer.
  7020. This function can be used in a hook."
  7021. (when (org-mode-p)
  7022. (org-map-dblocks 'org-update-dblock)))
  7023. ;;;; Completion
  7024. (defconst org-additional-option-like-keywords
  7025. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7026. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7027. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  7028. (defcustom org-structure-template-alist
  7029. '(
  7030. ("s" "#+begin_src ?\n\n#+end_src"
  7031. "<src lang=\"?\">\n\n</src>")
  7032. ("e" "#+begin_example\n?\n#+end_example"
  7033. "<example>\n?\n</example>")
  7034. ("q" "#+begin_quote\n?\n#+end_quote"
  7035. "<quote>\n?\n</quote>")
  7036. ("v" "#+begin_verse\n?\n#+end_verse"
  7037. "<verse>\n?\n/verse>")
  7038. ("l" "#+begin_latex\n?\n#+end_latex"
  7039. "<literal style=\"latex\">\n?\n</literal>")
  7040. ("L" "#+latex: "
  7041. "<literal style=\"latex\">?</literal>")
  7042. ("h" "#+begin_html\n?\n#+end_html"
  7043. "<literal style=\"html\">\n?\n</literal>")
  7044. ("H" "#+html: "
  7045. "<literal style=\"html\">?</literal>")
  7046. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7047. ("A" "#+ascii: ")
  7048. ("i" "#+include %file ?"
  7049. "<include file=%file markup=\"?\">")
  7050. )
  7051. "Structure completion elements.
  7052. This is a list of abbreviation keys and values. The value gets inserted
  7053. it you type @samp{.} followed by the key and then the completion key,
  7054. usually `M-TAB'. %file will be replaced by a file name after prompting
  7055. for the file uning completion.
  7056. There are two templates for each key, the first uses the original Org syntax,
  7057. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7058. the default when the /org-mtags.el/ module has been loaded. See also the
  7059. variable `org-mtags-prefere-muse-templates'.
  7060. This is an experimental feature, it is undecided if it is going to stay in."
  7061. :group 'org-completion
  7062. :type '(repeat
  7063. (string :tag "Key")
  7064. (string :tag "Template")
  7065. (string :tag "Muse Template")))
  7066. (defun org-try-structure-completion ()
  7067. "Try to complete a structure template before point.
  7068. This looks for strings like \"<e\" on an otherwise empty line and
  7069. expands them."
  7070. (let ((l (buffer-substring (point-at-bol) (point)))
  7071. a)
  7072. (when (and (looking-at "[ \t]*$")
  7073. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7074. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7075. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7076. (match-beginning 1)) a)
  7077. t)))
  7078. (defun org-complete-expand-structure-template (start cell)
  7079. "Expand a structure template."
  7080. (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
  7081. (rpl (nth (if musep 2 1) cell)))
  7082. (delete-region start (point))
  7083. (when (string-match "\\`#\\+" rpl)
  7084. (cond
  7085. ((bolp))
  7086. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7087. (delete-region (point-at-bol) (point)))
  7088. (t (newline))))
  7089. (setq start (point))
  7090. (if (string-match "%file" rpl)
  7091. (setq rpl (replace-match
  7092. (concat
  7093. "\""
  7094. (save-match-data
  7095. (abbreviate-file-name (read-file-name "Include file: ")))
  7096. "\"")
  7097. t t rpl)))
  7098. (insert rpl)
  7099. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7100. (defun org-complete (&optional arg)
  7101. "Perform completion on word at point.
  7102. At the beginning of a headline, this completes TODO keywords as given in
  7103. `org-todo-keywords'.
  7104. If the current word is preceded by a backslash, completes the TeX symbols
  7105. that are supported for HTML support.
  7106. If the current word is preceded by \"#+\", completes special words for
  7107. setting file options.
  7108. In the line after \"#+STARTUP:, complete valid keywords.\"
  7109. At all other locations, this simply calls the value of
  7110. `org-completion-fallback-command'."
  7111. (interactive "P")
  7112. (org-without-partial-completion
  7113. (catch 'exit
  7114. (let* ((a nil)
  7115. (end (point))
  7116. (beg1 (save-excursion
  7117. (skip-chars-backward (org-re "[:alnum:]_@"))
  7118. (point)))
  7119. (beg (save-excursion
  7120. (skip-chars-backward "a-zA-Z0-9_:$")
  7121. (point)))
  7122. (confirm (lambda (x) (stringp (car x))))
  7123. (searchhead (equal (char-before beg) ?*))
  7124. (struct
  7125. (when (and (member (char-before beg1) '(?. ?<))
  7126. (setq a (assoc (buffer-substring beg1 (point))
  7127. org-structure-template-alist)))
  7128. (org-complete-expand-structure-template (1- beg1) a)
  7129. (throw 'exit t)))
  7130. (tag (and (equal (char-before beg1) ?:)
  7131. (equal (char-after (point-at-bol)) ?*)))
  7132. (prop (and (equal (char-before beg1) ?:)
  7133. (not (equal (char-after (point-at-bol)) ?*))))
  7134. (texp (equal (char-before beg) ?\\))
  7135. (link (equal (char-before beg) ?\[))
  7136. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7137. beg)
  7138. "#+"))
  7139. (startup (string-match "^#\\+STARTUP:.*"
  7140. (buffer-substring (point-at-bol) (point))))
  7141. (completion-ignore-case opt)
  7142. (type nil)
  7143. (tbl nil)
  7144. (table (cond
  7145. (opt
  7146. (setq type :opt)
  7147. (require 'org-exp)
  7148. (append
  7149. (mapcar
  7150. (lambda (x)
  7151. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7152. (cons (match-string 2 x) (match-string 1 x)))
  7153. (org-split-string (org-get-current-options) "\n"))
  7154. (mapcar 'list org-additional-option-like-keywords)))
  7155. (startup
  7156. (setq type :startup)
  7157. org-startup-options)
  7158. (link (append org-link-abbrev-alist-local
  7159. org-link-abbrev-alist))
  7160. (texp
  7161. (setq type :tex)
  7162. org-html-entities)
  7163. ((string-match "\\`\\*+[ \t]+\\'"
  7164. (buffer-substring (point-at-bol) beg))
  7165. (setq type :todo)
  7166. (mapcar 'list org-todo-keywords-1))
  7167. (searchhead
  7168. (setq type :searchhead)
  7169. (save-excursion
  7170. (goto-char (point-min))
  7171. (while (re-search-forward org-todo-line-regexp nil t)
  7172. (push (list
  7173. (org-make-org-heading-search-string
  7174. (match-string 3) t))
  7175. tbl)))
  7176. tbl)
  7177. (tag (setq type :tag beg beg1)
  7178. (or org-tag-alist (org-get-buffer-tags)))
  7179. (prop (setq type :prop beg beg1)
  7180. (mapcar 'list (org-buffer-property-keys nil t t)))
  7181. (t (progn
  7182. (call-interactively org-completion-fallback-command)
  7183. (throw 'exit nil)))))
  7184. (pattern (buffer-substring-no-properties beg end))
  7185. (completion (try-completion pattern table confirm)))
  7186. (cond ((eq completion t)
  7187. (if (not (assoc (upcase pattern) table))
  7188. (message "Already complete")
  7189. (if (and (equal type :opt)
  7190. (not (member (car (assoc (upcase pattern) table))
  7191. org-additional-option-like-keywords)))
  7192. (insert (substring (cdr (assoc (upcase pattern) table))
  7193. (length pattern)))
  7194. (if (memq type '(:tag :prop)) (insert ":")))))
  7195. ((null completion)
  7196. (message "Can't find completion for \"%s\"" pattern)
  7197. (ding))
  7198. ((not (string= pattern completion))
  7199. (delete-region beg end)
  7200. (if (string-match " +$" completion)
  7201. (setq completion (replace-match "" t t completion)))
  7202. (insert completion)
  7203. (if (get-buffer-window "*Completions*")
  7204. (delete-window (get-buffer-window "*Completions*")))
  7205. (if (assoc completion table)
  7206. (if (eq type :todo) (insert " ")
  7207. (if (memq type '(:tag :prop)) (insert ":"))))
  7208. (if (and (equal type :opt) (assoc completion table))
  7209. (message "%s" (substitute-command-keys
  7210. "Press \\[org-complete] again to insert example settings"))))
  7211. (t
  7212. (message "Making completion list...")
  7213. (let ((list (sort (all-completions pattern table confirm)
  7214. 'string<)))
  7215. (with-output-to-temp-buffer "*Completions*"
  7216. (condition-case nil
  7217. ;; Protection needed for XEmacs and emacs 21
  7218. (display-completion-list list pattern)
  7219. (error (display-completion-list list)))))
  7220. (message "Making completion list...%s" "done")))))))
  7221. ;;;; TODO, DEADLINE, Comments
  7222. (defun org-toggle-comment ()
  7223. "Change the COMMENT state of an entry."
  7224. (interactive)
  7225. (save-excursion
  7226. (org-back-to-heading)
  7227. (let (case-fold-search)
  7228. (if (looking-at (concat outline-regexp
  7229. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7230. (replace-match "" t t nil 1)
  7231. (if (looking-at outline-regexp)
  7232. (progn
  7233. (goto-char (match-end 0))
  7234. (insert org-comment-string " ")))))))
  7235. (defvar org-last-todo-state-is-todo nil
  7236. "This is non-nil when the last TODO state change led to a TODO state.
  7237. If the last change removed the TODO tag or switched to DONE, then
  7238. this is nil.")
  7239. (defvar org-setting-tags nil) ; dynamically skiped
  7240. (defun org-parse-local-options (string var)
  7241. "Parse STRING for startup setting relevant for variable VAR."
  7242. (let ((rtn (symbol-value var))
  7243. e opts)
  7244. (save-match-data
  7245. (if (or (not string) (not (string-match "\\S-" string)))
  7246. rtn
  7247. (setq opts (delq nil (mapcar (lambda (x)
  7248. (setq e (assoc x org-startup-options))
  7249. (if (eq (nth 1 e) var) e nil))
  7250. (org-split-string string "[ \t]+"))))
  7251. (if (not opts)
  7252. rtn
  7253. (setq rtn nil)
  7254. (while (setq e (pop opts))
  7255. (if (not (nth 3 e))
  7256. (setq rtn (nth 2 e))
  7257. (if (not (listp rtn)) (setq rtn nil))
  7258. (push (nth 2 e) rtn)))
  7259. rtn)))))
  7260. (defvar org-blocker-hook nil
  7261. "Hook for functions that are allowed to block a state change.
  7262. Each function gets as its single argument a property list, see
  7263. `org-trigger-hook' for more information about this list.
  7264. If any of the functions in this hook returns nil, the state change
  7265. is blocked.")
  7266. (defvar org-trigger-hook nil
  7267. "Hook for functions that are triggered by a state change.
  7268. Each function gets as its single argument a property list with at least
  7269. the following elements:
  7270. (:type type-of-change :position pos-at-entry-start
  7271. :from old-state :to new-state)
  7272. Depending on the type, more properties may be present.
  7273. This mechanism is currently implemented for:
  7274. TODO state changes
  7275. ------------------
  7276. :type todo-state-change
  7277. :from previous state (keyword as a string), or nil
  7278. :to new state (keyword as a string), or nil")
  7279. (defun org-todo (&optional arg)
  7280. "Change the TODO state of an item.
  7281. The state of an item is given by a keyword at the start of the heading,
  7282. like
  7283. *** TODO Write paper
  7284. *** DONE Call mom
  7285. The different keywords are specified in the variable `org-todo-keywords'.
  7286. By default the available states are \"TODO\" and \"DONE\".
  7287. So for this example: when the item starts with TODO, it is changed to DONE.
  7288. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7289. DONE are present, add TODO at the beginning of the heading.
  7290. With C-u prefix arg, use completion to determine the new state.
  7291. With numeric prefix arg, switch to that state.
  7292. For calling through lisp, arg is also interpreted in the following way:
  7293. 'none -> empty state
  7294. \"\"(empty string) -> switch to empty state
  7295. 'done -> switch to DONE
  7296. 'nextset -> switch to the next set of keywords
  7297. 'previousset -> switch to the previous set of keywords
  7298. \"WAITING\" -> switch to the specified keyword, but only if it
  7299. really is a member of `org-todo-keywords'."
  7300. (interactive "P")
  7301. (save-excursion
  7302. (catch 'exit
  7303. (org-back-to-heading)
  7304. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7305. (or (looking-at (concat " +" org-todo-regexp " *"))
  7306. (looking-at " *"))
  7307. (let* ((match-data (match-data))
  7308. (startpos (point-at-bol))
  7309. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7310. (org-log-done org-log-done)
  7311. (org-log-repeat org-log-repeat)
  7312. (org-todo-log-states org-todo-log-states)
  7313. (this (match-string 1))
  7314. (hl-pos (match-beginning 0))
  7315. (head (org-get-todo-sequence-head this))
  7316. (ass (assoc head org-todo-kwd-alist))
  7317. (interpret (nth 1 ass))
  7318. (done-word (nth 3 ass))
  7319. (final-done-word (nth 4 ass))
  7320. (last-state (or this ""))
  7321. (completion-ignore-case t)
  7322. (member (member this org-todo-keywords-1))
  7323. (tail (cdr member))
  7324. (state (cond
  7325. ((and org-todo-key-trigger
  7326. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7327. (and (not arg) org-use-fast-todo-selection
  7328. (not (eq org-use-fast-todo-selection 'prefix)))))
  7329. ;; Use fast selection
  7330. (org-fast-todo-selection))
  7331. ((and (equal arg '(4))
  7332. (or (not org-use-fast-todo-selection)
  7333. (not org-todo-key-trigger)))
  7334. ;; Read a state with completion
  7335. (completing-read "State: " (mapcar (lambda(x) (list x))
  7336. org-todo-keywords-1)
  7337. nil t))
  7338. ((eq arg 'right)
  7339. (if this
  7340. (if tail (car tail) nil)
  7341. (car org-todo-keywords-1)))
  7342. ((eq arg 'left)
  7343. (if (equal member org-todo-keywords-1)
  7344. nil
  7345. (if this
  7346. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7347. org-todo-keywords-1)
  7348. (org-last org-todo-keywords-1))))
  7349. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7350. (setq arg nil))) ; hack to fall back to cycling
  7351. (arg
  7352. ;; user or caller requests a specific state
  7353. (cond
  7354. ((equal arg "") nil)
  7355. ((eq arg 'none) nil)
  7356. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7357. ((eq arg 'nextset)
  7358. (or (car (cdr (member head org-todo-heads)))
  7359. (car org-todo-heads)))
  7360. ((eq arg 'previousset)
  7361. (let ((org-todo-heads (reverse org-todo-heads)))
  7362. (or (car (cdr (member head org-todo-heads)))
  7363. (car org-todo-heads))))
  7364. ((car (member arg org-todo-keywords-1)))
  7365. ((nth (1- (prefix-numeric-value arg))
  7366. org-todo-keywords-1))))
  7367. ((null member) (or head (car org-todo-keywords-1)))
  7368. ((equal this final-done-word) nil) ;; -> make empty
  7369. ((null tail) nil) ;; -> first entry
  7370. ((eq interpret 'sequence)
  7371. (car tail))
  7372. ((memq interpret '(type priority))
  7373. (if (eq this-command last-command)
  7374. (car tail)
  7375. (if (> (length tail) 0)
  7376. (or done-word (car org-done-keywords))
  7377. nil)))
  7378. (t nil)))
  7379. (next (if state (concat " " state " ") " "))
  7380. (change-plist (list :type 'todo-state-change :from this :to state
  7381. :position startpos))
  7382. dolog now-done-p)
  7383. (when org-blocker-hook
  7384. (unless (save-excursion
  7385. (save-match-data
  7386. (run-hook-with-args-until-failure
  7387. 'org-blocker-hook change-plist)))
  7388. (if (interactive-p)
  7389. (error "TODO state change from %s to %s blocked" this state)
  7390. ;; fail silently
  7391. (message "TODO state change from %s to %s blocked" this state)
  7392. (throw 'exit nil))))
  7393. (store-match-data match-data)
  7394. (replace-match next t t)
  7395. (unless (pos-visible-in-window-p hl-pos)
  7396. (message "TODO state changed to %s" (org-trim next)))
  7397. (unless head
  7398. (setq head (org-get-todo-sequence-head state)
  7399. ass (assoc head org-todo-kwd-alist)
  7400. interpret (nth 1 ass)
  7401. done-word (nth 3 ass)
  7402. final-done-word (nth 4 ass)))
  7403. (when (memq arg '(nextset previousset))
  7404. (message "Keyword-Set %d/%d: %s"
  7405. (- (length org-todo-sets) -1
  7406. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7407. (length org-todo-sets)
  7408. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7409. (setq org-last-todo-state-is-todo
  7410. (not (member state org-done-keywords)))
  7411. (setq now-done-p (and (member state org-done-keywords)
  7412. (not (member this org-done-keywords))))
  7413. (and logging (org-local-logging logging))
  7414. (when (and (or org-todo-log-states org-log-done)
  7415. (not (memq arg '(nextset previousset))))
  7416. ;; we need to look at recording a time and note
  7417. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7418. (nth 2 (assoc this org-todo-log-states))))
  7419. (when (and state
  7420. (member state org-not-done-keywords)
  7421. (not (member this org-not-done-keywords)))
  7422. ;; This is now a todo state and was not one before
  7423. ;; If there was a CLOSED time stamp, get rid of it.
  7424. (org-add-planning-info nil nil 'closed))
  7425. (when (and now-done-p org-log-done)
  7426. ;; It is now done, and it was not done before
  7427. (org-add-planning-info 'closed (org-current-time))
  7428. (if (and (not dolog) (eq 'note org-log-done))
  7429. (org-add-log-setup 'done state 'findpos 'note)))
  7430. (when (and state dolog)
  7431. ;; This is a non-nil state, and we need to log it
  7432. (org-add-log-setup 'state state 'findpos dolog)))
  7433. ;; Fixup tag positioning
  7434. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7435. (when org-provide-todo-statistics
  7436. (org-update-parent-todo-statistics))
  7437. (run-hooks 'org-after-todo-state-change-hook)
  7438. (if (and arg (not (member state org-done-keywords)))
  7439. (setq head (org-get-todo-sequence-head state)))
  7440. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7441. ;; Do we need to trigger a repeat?
  7442. (when now-done-p (org-auto-repeat-maybe state))
  7443. ;; Fixup cursor location if close to the keyword
  7444. (if (and (outline-on-heading-p)
  7445. (not (bolp))
  7446. (save-excursion (beginning-of-line 1)
  7447. (looking-at org-todo-line-regexp))
  7448. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7449. (progn
  7450. (goto-char (or (match-end 2) (match-end 1)))
  7451. (just-one-space)))
  7452. (when org-trigger-hook
  7453. (save-excursion
  7454. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7455. (defun org-update-parent-todo-statistics ()
  7456. "Update any statistics cookie in the parent of the current headline."
  7457. (interactive)
  7458. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7459. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7460. (catch 'exit
  7461. (save-excursion
  7462. (setq level (org-up-heading-safe))
  7463. (unless (and level
  7464. (re-search-forward box-re (point-at-eol) t))
  7465. (throw 'exit nil))
  7466. (setq is-percent (match-end 2))
  7467. (save-match-data
  7468. (unless (outline-next-heading) (throw 'exit nil))
  7469. (while (looking-at org-todo-line-regexp)
  7470. (setq kwd (match-string 2))
  7471. (and kwd (setq cnt-all (1+ cnt-all)))
  7472. (and (member kwd org-done-keywords)
  7473. (setq cnt-done (1+ cnt-done)))
  7474. (condition-case nil
  7475. (outline-forward-same-level 1)
  7476. (error (end-of-line 1)))))
  7477. (replace-match
  7478. (if is-percent
  7479. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7480. (format "[%d/%d]" cnt-done cnt-all)))
  7481. (run-hook-with-args 'org-after-todo-statistics-hook
  7482. cnt-done (- cnt-all cnt-done))))))
  7483. (defvar org-after-todo-statistics-hook nil
  7484. "Hook that is called after a TODO statistics cookie has been updated.
  7485. Each function is called with two arguments: the number of not-done entries
  7486. and the number of done entries.
  7487. For example, the following function, when added to this hook, will switch
  7488. an entry to DONE when all children are done, and back to TODO when new
  7489. entries are set to a TODO status. Note that this hook is only called
  7490. when there is a statistics cookie in the headline!
  7491. (defun org-summary-todo (n-done n-not-done)
  7492. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7493. (let (org-log-done org-log-states) ; turn off logging
  7494. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7495. ")
  7496. (defun org-local-logging (value)
  7497. "Get logging settings from a property VALUE."
  7498. (let* (words w a)
  7499. ;; directly set the variables, they are already local.
  7500. (setq org-log-done nil
  7501. org-log-repeat nil
  7502. org-todo-log-states nil)
  7503. (setq words (org-split-string value))
  7504. (while (setq w (pop words))
  7505. (cond
  7506. ((setq a (assoc w org-startup-options))
  7507. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7508. (set (nth 1 a) (nth 2 a))))
  7509. ((setq a (org-extract-log-state-settings w))
  7510. (and (member (car a) org-todo-keywords-1)
  7511. (push a org-todo-log-states)))))))
  7512. (defun org-get-todo-sequence-head (kwd)
  7513. "Return the head of the TODO sequence to which KWD belongs.
  7514. If KWD is not set, check if there is a text property remembering the
  7515. right sequence."
  7516. (let (p)
  7517. (cond
  7518. ((not kwd)
  7519. (or (get-text-property (point-at-bol) 'org-todo-head)
  7520. (progn
  7521. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7522. nil (point-at-eol)))
  7523. (get-text-property p 'org-todo-head))))
  7524. ((not (member kwd org-todo-keywords-1))
  7525. (car org-todo-keywords-1))
  7526. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7527. (defun org-fast-todo-selection ()
  7528. "Fast TODO keyword selection with single keys.
  7529. Returns the new TODO keyword, or nil if no state change should occur."
  7530. (let* ((fulltable org-todo-key-alist)
  7531. (done-keywords org-done-keywords) ;; needed for the faces.
  7532. (maxlen (apply 'max (mapcar
  7533. (lambda (x)
  7534. (if (stringp (car x)) (string-width (car x)) 0))
  7535. fulltable)))
  7536. (expert nil)
  7537. (fwidth (+ maxlen 3 1 3))
  7538. (ncol (/ (- (window-width) 4) fwidth))
  7539. tg cnt e c tbl
  7540. groups ingroup)
  7541. (save-window-excursion
  7542. (if expert
  7543. (set-buffer (get-buffer-create " *Org todo*"))
  7544. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7545. (erase-buffer)
  7546. (org-set-local 'org-done-keywords done-keywords)
  7547. (setq tbl fulltable cnt 0)
  7548. (while (setq e (pop tbl))
  7549. (cond
  7550. ((equal e '(:startgroup))
  7551. (push '() groups) (setq ingroup t)
  7552. (when (not (= cnt 0))
  7553. (setq cnt 0)
  7554. (insert "\n"))
  7555. (insert "{ "))
  7556. ((equal e '(:endgroup))
  7557. (setq ingroup nil cnt 0)
  7558. (insert "}\n"))
  7559. (t
  7560. (setq tg (car e) c (cdr e))
  7561. (if ingroup (push tg (car groups)))
  7562. (setq tg (org-add-props tg nil 'face
  7563. (org-get-todo-face tg)))
  7564. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7565. (insert "[" c "] " tg (make-string
  7566. (- fwidth 4 (length tg)) ?\ ))
  7567. (when (= (setq cnt (1+ cnt)) ncol)
  7568. (insert "\n")
  7569. (if ingroup (insert " "))
  7570. (setq cnt 0)))))
  7571. (insert "\n")
  7572. (goto-char (point-min))
  7573. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7574. (fit-window-to-buffer))
  7575. (message "[a-z..]:Set [SPC]:clear")
  7576. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7577. (cond
  7578. ((or (= c ?\C-g)
  7579. (and (= c ?q) (not (rassoc c fulltable))))
  7580. (setq quit-flag t))
  7581. ((= c ?\ ) nil)
  7582. ((setq e (rassoc c fulltable) tg (car e))
  7583. tg)
  7584. (t (setq quit-flag t))))))
  7585. (defun org-entry-is-todo-p ()
  7586. (member (org-get-todo-state) org-not-done-keywords))
  7587. (defun org-entry-is-done-p ()
  7588. (member (org-get-todo-state) org-done-keywords))
  7589. (defun org-get-todo-state ()
  7590. (save-excursion
  7591. (org-back-to-heading t)
  7592. (and (looking-at org-todo-line-regexp)
  7593. (match-end 2)
  7594. (match-string 2))))
  7595. (defun org-at-date-range-p (&optional inactive-ok)
  7596. "Is the cursor inside a date range?"
  7597. (interactive)
  7598. (save-excursion
  7599. (catch 'exit
  7600. (let ((pos (point)))
  7601. (skip-chars-backward "^[<\r\n")
  7602. (skip-chars-backward "<[")
  7603. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7604. (>= (match-end 0) pos)
  7605. (throw 'exit t))
  7606. (skip-chars-backward "^<[\r\n")
  7607. (skip-chars-backward "<[")
  7608. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7609. (>= (match-end 0) pos)
  7610. (throw 'exit t)))
  7611. nil)))
  7612. (defun org-get-repeat ()
  7613. "Check if tere is a deadline/schedule with repeater in this entry."
  7614. (save-match-data
  7615. (save-excursion
  7616. (org-back-to-heading t)
  7617. (if (re-search-forward
  7618. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7619. (match-string 1)))))
  7620. (defvar org-last-changed-timestamp)
  7621. (defvar org-log-post-message)
  7622. (defvar org-log-note-purpose)
  7623. (defvar org-log-note-how)
  7624. (defun org-auto-repeat-maybe (done-word)
  7625. "Check if the current headline contains a repeated deadline/schedule.
  7626. If yes, set TODO state back to what it was and change the base date
  7627. of repeating deadline/scheduled time stamps to new date.
  7628. This function is run automatically after each state change to a DONE state."
  7629. ;; last-state is dynamically scoped into this function
  7630. (let* ((repeat (org-get-repeat))
  7631. (aa (assoc last-state org-todo-kwd-alist))
  7632. (interpret (nth 1 aa))
  7633. (head (nth 2 aa))
  7634. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7635. (msg "Entry repeats: ")
  7636. (org-log-done nil)
  7637. (org-todo-log-states nil)
  7638. (nshiftmax 10) (nshift 0)
  7639. re type n what ts mb0 time)
  7640. (when repeat
  7641. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7642. (org-todo (if (eq interpret 'type) last-state head))
  7643. (when org-log-repeat
  7644. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7645. (memq 'org-add-log-note post-command-hook))
  7646. ;; OK, we are already setup for some record
  7647. (if (eq org-log-repeat 'note)
  7648. ;; make sure we take a note, not only a time stamp
  7649. (setq org-log-note-how 'note))
  7650. ;; Set up for taking a record
  7651. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7652. 'findpos org-log-repeat)))
  7653. (org-back-to-heading t)
  7654. (org-add-planning-info nil nil 'closed)
  7655. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7656. org-deadline-time-regexp "\\)\\|\\("
  7657. org-ts-regexp "\\)"))
  7658. (while (re-search-forward
  7659. re (save-excursion (outline-next-heading) (point)) t)
  7660. (setq type (if (match-end 1) org-scheduled-string
  7661. (if (match-end 3) org-deadline-string "Plain:"))
  7662. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7663. mb0 (match-beginning 0))
  7664. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7665. (setq n (string-to-number (match-string 2 ts))
  7666. what (match-string 3 ts))
  7667. (if (equal what "w") (setq n (* n 7) what "d"))
  7668. ;; Preparation, see if we need to modify the start date for the change
  7669. (when (match-end 1)
  7670. (setq time (save-match-data (org-time-string-to-time ts)))
  7671. (cond
  7672. ((equal (match-string 1 ts) ".")
  7673. ;; Shift starting date to today
  7674. (org-timestamp-change
  7675. (- (time-to-days (current-time)) (time-to-days time))
  7676. 'day))
  7677. ((equal (match-string 1 ts) "+")
  7678. (while (or (= nshift 0)
  7679. (<= (time-to-days time) (time-to-days (current-time))))
  7680. (when (= (incf nshift) nshiftmax)
  7681. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7682. (error "Abort")))
  7683. (org-timestamp-change n (cdr (assoc what whata)))
  7684. (org-at-timestamp-p t)
  7685. (setq ts (match-string 1))
  7686. (setq time (save-match-data (org-time-string-to-time ts))))
  7687. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7688. ;; rematch, so that we have everything in place for the real shift
  7689. (org-at-timestamp-p t)
  7690. (setq ts (match-string 1))
  7691. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7692. (org-timestamp-change n (cdr (assoc what whata)))
  7693. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7694. (setq org-log-post-message msg)
  7695. (message "%s" msg))))
  7696. (defun org-show-todo-tree (arg)
  7697. "Make a compact tree which shows all headlines marked with TODO.
  7698. The tree will show the lines where the regexp matches, and all higher
  7699. headlines above the match.
  7700. With a \\[universal-argument] prefix, also show the DONE entries.
  7701. With a numeric prefix N, construct a sparse tree for the Nth element
  7702. of `org-todo-keywords-1'."
  7703. (interactive "P")
  7704. (let ((case-fold-search nil)
  7705. (kwd-re
  7706. (cond ((null arg) org-not-done-regexp)
  7707. ((equal arg '(4))
  7708. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7709. (mapcar 'list org-todo-keywords-1))))
  7710. (concat "\\("
  7711. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7712. "\\)\\>")))
  7713. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7714. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7715. org-todo-keywords-1)))
  7716. (t (error "Invalid prefix argument: %s" arg)))))
  7717. (message "%d TODO entries found"
  7718. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7719. (defun org-deadline (&optional remove)
  7720. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7721. With argument REMOVE, remove any deadline from the item."
  7722. (interactive "P")
  7723. (if remove
  7724. (progn
  7725. (org-remove-timestamp-with-keyword org-deadline-string)
  7726. (message "Item no longer has a deadline."))
  7727. (org-add-planning-info 'deadline nil 'closed)))
  7728. (defun org-schedule (&optional remove)
  7729. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7730. With argument REMOVE, remove any scheduling date from the item."
  7731. (interactive "P")
  7732. (if remove
  7733. (progn
  7734. (org-remove-timestamp-with-keyword org-scheduled-string)
  7735. (message "Item is no longer scheduled."))
  7736. (org-add-planning-info 'scheduled nil 'closed)))
  7737. (defun org-remove-timestamp-with-keyword (keyword)
  7738. "Remove all time stamps with KEYWORD in the current entry."
  7739. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7740. beg)
  7741. (save-excursion
  7742. (org-back-to-heading t)
  7743. (setq beg (point))
  7744. (org-end-of-subtree t t)
  7745. (while (re-search-backward re beg t)
  7746. (replace-match "")
  7747. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7748. (equal (char-before) ?\ ))
  7749. (backward-delete-char 1)
  7750. (if (string-match "^[ \t]*$" (buffer-substring
  7751. (point-at-bol) (point-at-eol)))
  7752. (delete-region (point-at-bol)
  7753. (min (point-max) (1+ (point-at-eol))))))))))
  7754. (defun org-add-planning-info (what &optional time &rest remove)
  7755. "Insert new timestamp with keyword in the line directly after the headline.
  7756. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7757. If non is given, the user is prompted for a date.
  7758. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7759. be removed."
  7760. (interactive)
  7761. (let (org-time-was-given org-end-time-was-given ts
  7762. end default-time default-input)
  7763. (when (and (not time) (memq what '(scheduled deadline)))
  7764. ;; Try to get a default date/time from existing timestamp
  7765. (save-excursion
  7766. (org-back-to-heading t)
  7767. (setq end (save-excursion (outline-next-heading) (point)))
  7768. (when (re-search-forward (if (eq what 'scheduled)
  7769. org-scheduled-time-regexp
  7770. org-deadline-time-regexp)
  7771. end t)
  7772. (setq ts (match-string 1)
  7773. default-time
  7774. (apply 'encode-time (org-parse-time-string ts))
  7775. default-input (and ts (org-get-compact-tod ts))))))
  7776. (when what
  7777. ;; If necessary, get the time from the user
  7778. (setq time (or time (org-read-date nil 'to-time nil nil
  7779. default-time default-input))))
  7780. (when (and org-insert-labeled-timestamps-at-point
  7781. (member what '(scheduled deadline)))
  7782. (insert
  7783. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7784. (org-insert-time-stamp time org-time-was-given
  7785. nil nil nil (list org-end-time-was-given))
  7786. (setq what nil))
  7787. (save-excursion
  7788. (save-restriction
  7789. (let (col list elt ts buffer-invisibility-spec)
  7790. (org-back-to-heading t)
  7791. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7792. (goto-char (match-end 1))
  7793. (setq col (current-column))
  7794. (goto-char (match-end 0))
  7795. (if (eobp) (insert "\n") (forward-char 1))
  7796. (if (and (not (looking-at outline-regexp))
  7797. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7798. "[^\r\n]*"))
  7799. (not (equal (match-string 1) org-clock-string)))
  7800. (narrow-to-region (match-beginning 0) (match-end 0))
  7801. (insert-before-markers "\n")
  7802. (backward-char 1)
  7803. (narrow-to-region (point) (point))
  7804. (org-indent-to-column col))
  7805. ;; Check if we have to remove something.
  7806. (setq list (cons what remove))
  7807. (while list
  7808. (setq elt (pop list))
  7809. (goto-char (point-min))
  7810. (when (or (and (eq elt 'scheduled)
  7811. (re-search-forward org-scheduled-time-regexp nil t))
  7812. (and (eq elt 'deadline)
  7813. (re-search-forward org-deadline-time-regexp nil t))
  7814. (and (eq elt 'closed)
  7815. (re-search-forward org-closed-time-regexp nil t)))
  7816. (replace-match "")
  7817. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7818. (if (looking-at " +") (replace-match ""))))
  7819. (goto-char (point-max))
  7820. (when what
  7821. (insert
  7822. (if (not (equal (char-before) ?\ )) " " "")
  7823. (cond ((eq what 'scheduled) org-scheduled-string)
  7824. ((eq what 'deadline) org-deadline-string)
  7825. ((eq what 'closed) org-closed-string))
  7826. " ")
  7827. (setq ts (org-insert-time-stamp
  7828. time
  7829. (or org-time-was-given
  7830. (and (eq what 'closed) org-log-done-with-time))
  7831. (eq what 'closed)
  7832. nil nil (list org-end-time-was-given)))
  7833. (end-of-line 1))
  7834. (goto-char (point-min))
  7835. (widen)
  7836. (if (and (looking-at "[ \t]+\n")
  7837. (equal (char-before) ?\n))
  7838. (delete-region (1- (point)) (point-at-eol)))
  7839. ts)))))
  7840. (defvar org-log-note-marker (make-marker))
  7841. (defvar org-log-note-purpose nil)
  7842. (defvar org-log-note-state nil)
  7843. (defvar org-log-note-how nil)
  7844. (defvar org-log-note-window-configuration nil)
  7845. (defvar org-log-note-return-to (make-marker))
  7846. (defvar org-log-post-message nil
  7847. "Message to be displayed after a log note has been stored.
  7848. The auto-repeater uses this.")
  7849. (defun org-add-note ()
  7850. "Add a note to the current entry.
  7851. This is done in the same way as adding a state change note."
  7852. (interactive)
  7853. (org-add-log-setup 'note nil t nil))
  7854. (defun org-add-log-setup (&optional purpose state findpos how)
  7855. "Set up the post command hook to take a note.
  7856. If this is about to TODO state change, the new state is expected in STATE.
  7857. When FINDPOS is non-nil, find the correct position for the note in
  7858. the current entry. If not, assume that it can be inserted at point."
  7859. (save-excursion
  7860. (when findpos
  7861. (org-back-to-heading t)
  7862. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7863. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7864. "[^\r\n]*\\)?"))
  7865. (goto-char (match-end 0))
  7866. (unless org-log-states-order-reversed
  7867. (and (= (char-after) ?\n) (forward-char 1))
  7868. (org-skip-over-state-notes)
  7869. (skip-chars-backward " \t\n\r")))
  7870. (move-marker org-log-note-marker (point))
  7871. (setq org-log-note-purpose purpose
  7872. org-log-note-state state
  7873. org-log-note-how how)
  7874. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  7875. (defun org-skip-over-state-notes ()
  7876. "Skip past the list of State notes in an entry."
  7877. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7878. (while (looking-at "[ \t]*- State")
  7879. (condition-case nil
  7880. (org-next-item)
  7881. (error (org-end-of-item)))))
  7882. (defun org-add-log-note (&optional purpose)
  7883. "Pop up a window for taking a note, and add this note later at point."
  7884. (remove-hook 'post-command-hook 'org-add-log-note)
  7885. (setq org-log-note-window-configuration (current-window-configuration))
  7886. (delete-other-windows)
  7887. (move-marker org-log-note-return-to (point))
  7888. (switch-to-buffer (marker-buffer org-log-note-marker))
  7889. (goto-char org-log-note-marker)
  7890. (org-switch-to-buffer-other-window "*Org Note*")
  7891. (erase-buffer)
  7892. (if (memq org-log-note-how '(time state))
  7893. (org-store-log-note)
  7894. (let ((org-inhibit-startup t)) (org-mode))
  7895. (insert (format "# Insert note for %s.
  7896. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7897. (cond
  7898. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7899. ((eq org-log-note-purpose 'done) "closed todo item")
  7900. ((eq org-log-note-purpose 'state)
  7901. (format "state change to \"%s\"" org-log-note-state))
  7902. ((eq org-log-note-purpose 'note)
  7903. "this entry")
  7904. (t (error "This should not happen")))))
  7905. (org-set-local 'org-finish-function 'org-store-log-note)))
  7906. (defvar org-note-abort nil) ; dynamically scoped
  7907. (defun org-store-log-note ()
  7908. "Finish taking a log note, and insert it to where it belongs."
  7909. (let ((txt (buffer-string))
  7910. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7911. lines ind)
  7912. (kill-buffer (current-buffer))
  7913. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7914. (setq txt (replace-match "" t t txt)))
  7915. (if (string-match "\\s-+\\'" txt)
  7916. (setq txt (replace-match "" t t txt)))
  7917. (setq lines (org-split-string txt "\n"))
  7918. (when (and note (string-match "\\S-" note))
  7919. (setq note
  7920. (org-replace-escapes
  7921. note
  7922. (list (cons "%u" (user-login-name))
  7923. (cons "%U" user-full-name)
  7924. (cons "%t" (format-time-string
  7925. (org-time-stamp-format 'long 'inactive)
  7926. (current-time)))
  7927. (cons "%s" (if org-log-note-state
  7928. (concat "\"" org-log-note-state "\"")
  7929. "")))))
  7930. (if lines (setq note (concat note " \\\\")))
  7931. (push note lines))
  7932. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7933. (when lines
  7934. (save-excursion
  7935. (set-buffer (marker-buffer org-log-note-marker))
  7936. (save-excursion
  7937. (goto-char org-log-note-marker)
  7938. (move-marker org-log-note-marker nil)
  7939. (end-of-line 1)
  7940. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7941. (indent-relative nil)
  7942. (insert "- " (pop lines))
  7943. (org-indent-line-function)
  7944. (beginning-of-line 1)
  7945. (looking-at "[ \t]*")
  7946. (setq ind (concat (match-string 0) " "))
  7947. (end-of-line 1)
  7948. (while lines (insert "\n" ind (pop lines)))))))
  7949. (set-window-configuration org-log-note-window-configuration)
  7950. (with-current-buffer (marker-buffer org-log-note-return-to)
  7951. (goto-char org-log-note-return-to))
  7952. (move-marker org-log-note-return-to nil)
  7953. (and org-log-post-message (message "%s" org-log-post-message)))
  7954. (defun org-sparse-tree (&optional arg)
  7955. "Create a sparse tree, prompt for the details.
  7956. This command can create sparse trees. You first need to select the type
  7957. of match used to create the tree:
  7958. t Show entries with a specific TODO keyword.
  7959. T Show entries selected by a tags match.
  7960. p Enter a property name and its value (both with completion on existing
  7961. names/values) and show entries with that property.
  7962. r Show entries matching a regular expression
  7963. d Show deadlines due within `org-deadline-warning-days'."
  7964. (interactive "P")
  7965. (let (ans kwd value)
  7966. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7967. (setq ans (read-char-exclusive))
  7968. (cond
  7969. ((equal ans ?d)
  7970. (call-interactively 'org-check-deadlines))
  7971. ((equal ans ?b)
  7972. (call-interactively 'org-check-before-date))
  7973. ((equal ans ?t)
  7974. (org-show-todo-tree '(4)))
  7975. ((equal ans ?T)
  7976. (call-interactively 'org-tags-sparse-tree))
  7977. ((member ans '(?p ?P))
  7978. (setq kwd (completing-read "Property: "
  7979. (mapcar 'list (org-buffer-property-keys))))
  7980. (setq value (completing-read "Value: "
  7981. (mapcar 'list (org-property-values kwd))))
  7982. (unless (string-match "\\`{.*}\\'" value)
  7983. (setq value (concat "\"" value "\"")))
  7984. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7985. ((member ans '(?r ?R ?/))
  7986. (call-interactively 'org-occur))
  7987. (t (error "No such sparse tree command \"%c\"" ans)))))
  7988. (defvar org-occur-highlights nil
  7989. "List of overlays used for occur matches.")
  7990. (make-variable-buffer-local 'org-occur-highlights)
  7991. (defvar org-occur-parameters nil
  7992. "Parameters of the active org-occur calls.
  7993. This is a list, each call to org-occur pushes as cons cell,
  7994. containing the regular expression and the callback, onto the list.
  7995. The list can contain several entries if `org-occur' has been called
  7996. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7997. will only contain one set of parameters. When the highlights are
  7998. removed (for example with `C-c C-c', or with the next edit (depending
  7999. on `org-remove-highlights-with-change'), this variable is emptied
  8000. as well.")
  8001. (make-variable-buffer-local 'org-occur-parameters)
  8002. (defun org-occur (regexp &optional keep-previous callback)
  8003. "Make a compact tree which shows all matches of REGEXP.
  8004. The tree will show the lines where the regexp matches, and all higher
  8005. headlines above the match. It will also show the heading after the match,
  8006. to make sure editing the matching entry is easy.
  8007. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8008. call to `org-occur' will be kept, to allow stacking of calls to this
  8009. command.
  8010. If CALLBACK is non-nil, it is a function which is called to confirm
  8011. that the match should indeed be shown."
  8012. (interactive "sRegexp: \nP")
  8013. (unless keep-previous
  8014. (org-remove-occur-highlights nil nil t))
  8015. (push (cons regexp callback) org-occur-parameters)
  8016. (let ((cnt 0))
  8017. (save-excursion
  8018. (goto-char (point-min))
  8019. (if (or (not keep-previous) ; do not want to keep
  8020. (not org-occur-highlights)) ; no previous matches
  8021. ;; hide everything
  8022. (org-overview))
  8023. (while (re-search-forward regexp nil t)
  8024. (when (or (not callback)
  8025. (save-match-data (funcall callback)))
  8026. (setq cnt (1+ cnt))
  8027. (when org-highlight-sparse-tree-matches
  8028. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8029. (org-show-context 'occur-tree))))
  8030. (when org-remove-highlights-with-change
  8031. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8032. nil 'local))
  8033. (unless org-sparse-tree-open-archived-trees
  8034. (org-hide-archived-subtrees (point-min) (point-max)))
  8035. (run-hooks 'org-occur-hook)
  8036. (if (interactive-p)
  8037. (message "%d match(es) for regexp %s" cnt regexp))
  8038. cnt))
  8039. (defun org-show-context (&optional key)
  8040. "Make sure point and context and visible.
  8041. How much context is shown depends upon the variables
  8042. `org-show-hierarchy-above', `org-show-following-heading'. and
  8043. `org-show-siblings'."
  8044. (let ((heading-p (org-on-heading-p t))
  8045. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8046. (following-p (org-get-alist-option org-show-following-heading key))
  8047. (entry-p (org-get-alist-option org-show-entry-below key))
  8048. (siblings-p (org-get-alist-option org-show-siblings key)))
  8049. (catch 'exit
  8050. ;; Show heading or entry text
  8051. (if (and heading-p (not entry-p))
  8052. (org-flag-heading nil) ; only show the heading
  8053. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8054. (org-show-hidden-entry))) ; show entire entry
  8055. (when following-p
  8056. ;; Show next sibling, or heading below text
  8057. (save-excursion
  8058. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8059. (org-flag-heading nil))))
  8060. (when siblings-p (org-show-siblings))
  8061. (when hierarchy-p
  8062. ;; show all higher headings, possibly with siblings
  8063. (save-excursion
  8064. (while (and (condition-case nil
  8065. (progn (org-up-heading-all 1) t)
  8066. (error nil))
  8067. (not (bobp)))
  8068. (org-flag-heading nil)
  8069. (when siblings-p (org-show-siblings))))))))
  8070. (defun org-reveal (&optional siblings)
  8071. "Show current entry, hierarchy above it, and the following headline.
  8072. This can be used to show a consistent set of context around locations
  8073. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8074. not t for the search context.
  8075. With optional argument SIBLINGS, on each level of the hierarchy all
  8076. siblings are shown. This repairs the tree structure to what it would
  8077. look like when opened with hierarchical calls to `org-cycle'."
  8078. (interactive "P")
  8079. (let ((org-show-hierarchy-above t)
  8080. (org-show-following-heading t)
  8081. (org-show-siblings (if siblings t org-show-siblings)))
  8082. (org-show-context nil)))
  8083. (defun org-highlight-new-match (beg end)
  8084. "Highlight from BEG to END and mark the highlight is an occur headline."
  8085. (let ((ov (org-make-overlay beg end)))
  8086. (org-overlay-put ov 'face 'secondary-selection)
  8087. (push ov org-occur-highlights)))
  8088. (defun org-remove-occur-highlights (&optional beg end noremove)
  8089. "Remove the occur highlights from the buffer.
  8090. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8091. from the `before-change-functions' in the current buffer."
  8092. (interactive)
  8093. (unless org-inhibit-highlight-removal
  8094. (mapc 'org-delete-overlay org-occur-highlights)
  8095. (setq org-occur-highlights nil)
  8096. (setq org-occur-parameters nil)
  8097. (unless noremove
  8098. (remove-hook 'before-change-functions
  8099. 'org-remove-occur-highlights 'local))))
  8100. ;;;; Priorities
  8101. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8102. "Regular expression matching the priority indicator.")
  8103. (defvar org-remove-priority-next-time nil)
  8104. (defun org-priority-up ()
  8105. "Increase the priority of the current item."
  8106. (interactive)
  8107. (org-priority 'up))
  8108. (defun org-priority-down ()
  8109. "Decrease the priority of the current item."
  8110. (interactive)
  8111. (org-priority 'down))
  8112. (defun org-priority (&optional action)
  8113. "Change the priority of an item by ARG.
  8114. ACTION can be `set', `up', `down', or a character."
  8115. (interactive)
  8116. (setq action (or action 'set))
  8117. (let (current new news have remove)
  8118. (save-excursion
  8119. (org-back-to-heading)
  8120. (if (looking-at org-priority-regexp)
  8121. (setq current (string-to-char (match-string 2))
  8122. have t)
  8123. (setq current org-default-priority))
  8124. (cond
  8125. ((or (eq action 'set)
  8126. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8127. (if (not (eq action 'set))
  8128. (setq new action)
  8129. (message "Priority %c-%c, SPC to remove: "
  8130. org-highest-priority org-lowest-priority)
  8131. (setq new (read-char-exclusive)))
  8132. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8133. (= (upcase org-lowest-priority) org-lowest-priority))
  8134. (setq new (upcase new)))
  8135. (cond ((equal new ?\ ) (setq remove t))
  8136. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8137. (error "Priority must be between `%c' and `%c'"
  8138. org-highest-priority org-lowest-priority))))
  8139. ((eq action 'up)
  8140. (if (and (not have) (eq last-command this-command))
  8141. (setq new org-lowest-priority)
  8142. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8143. org-default-priority (1- current)))))
  8144. ((eq action 'down)
  8145. (if (and (not have) (eq last-command this-command))
  8146. (setq new org-highest-priority)
  8147. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8148. org-default-priority (1+ current)))))
  8149. (t (error "Invalid action")))
  8150. (if (or (< (upcase new) org-highest-priority)
  8151. (> (upcase new) org-lowest-priority))
  8152. (setq remove t))
  8153. (setq news (format "%c" new))
  8154. (if have
  8155. (if remove
  8156. (replace-match "" t t nil 1)
  8157. (replace-match news t t nil 2))
  8158. (if remove
  8159. (error "No priority cookie found in line")
  8160. (looking-at org-todo-line-regexp)
  8161. (if (match-end 2)
  8162. (progn
  8163. (goto-char (match-end 2))
  8164. (insert " [#" news "]"))
  8165. (goto-char (match-beginning 3))
  8166. (insert "[#" news "] ")))))
  8167. (org-preserve-lc (org-set-tags nil 'align))
  8168. (if remove
  8169. (message "Priority removed")
  8170. (message "Priority of current item set to %s" news))))
  8171. (defun org-get-priority (s)
  8172. "Find priority cookie and return priority."
  8173. (save-match-data
  8174. (if (not (string-match org-priority-regexp s))
  8175. (* 1000 (- org-lowest-priority org-default-priority))
  8176. (* 1000 (- org-lowest-priority
  8177. (string-to-char (match-string 2 s)))))))
  8178. ;;;; Tags
  8179. (defun org-scan-tags (action matcher &optional todo-only)
  8180. "Scan headline tags with inheritance and produce output ACTION.
  8181. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8182. evaluated, testing if a given set of tags qualifies a headline for
  8183. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8184. are included in the output."
  8185. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8186. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8187. (org-re
  8188. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8189. (props (list 'face nil
  8190. 'done-face 'org-done
  8191. 'undone-face nil
  8192. 'mouse-face 'highlight
  8193. 'org-not-done-regexp org-not-done-regexp
  8194. 'org-todo-regexp org-todo-regexp
  8195. 'keymap org-agenda-keymap
  8196. 'help-echo
  8197. (format "mouse-2 or RET jump to org file %s"
  8198. (abbreviate-file-name
  8199. (or (buffer-file-name (buffer-base-buffer))
  8200. (buffer-name (buffer-base-buffer)))))))
  8201. (case-fold-search nil)
  8202. lspos
  8203. tags tags-list tags-alist (llast 0) rtn level category i txt
  8204. todo marker entry priority)
  8205. (save-excursion
  8206. (goto-char (point-min))
  8207. (when (eq action 'sparse-tree)
  8208. (org-overview)
  8209. (org-remove-occur-highlights))
  8210. (while (re-search-forward re nil t)
  8211. (catch :skip
  8212. (setq todo (if (match-end 1) (match-string 2))
  8213. tags (if (match-end 4) (match-string 4)))
  8214. (goto-char (setq lspos (1+ (match-beginning 0))))
  8215. (setq level (org-reduced-level (funcall outline-level))
  8216. category (org-get-category))
  8217. (setq i llast llast level)
  8218. ;; remove tag lists from same and sublevels
  8219. (while (>= i level)
  8220. (when (setq entry (assoc i tags-alist))
  8221. (setq tags-alist (delete entry tags-alist)))
  8222. (setq i (1- i)))
  8223. ;; add the next tags
  8224. (when tags
  8225. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8226. tags-alist
  8227. (cons (cons level tags) tags-alist)))
  8228. ;; compile tags for current headline
  8229. (setq tags-list
  8230. (if org-use-tag-inheritance
  8231. (apply 'append (mapcar 'cdr tags-alist))
  8232. tags))
  8233. (when (and tags org-use-tag-inheritance
  8234. (not (eq t org-use-tag-inheritance)))
  8235. ;; selective inheritance, remove uninherited ones
  8236. (setcdr (car tags-alist)
  8237. (org-remove-uniherited-tags (cdar tags-alist))))
  8238. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8239. (eval matcher)
  8240. (or (not org-agenda-skip-archived-trees)
  8241. (not (member org-archive-tag tags-list))))
  8242. (and (eq action 'agenda) (org-agenda-skip))
  8243. ;; list this headline
  8244. (if (eq action 'sparse-tree)
  8245. (progn
  8246. (and org-highlight-sparse-tree-matches
  8247. (org-get-heading) (match-end 0)
  8248. (org-highlight-new-match
  8249. (match-beginning 0) (match-beginning 1)))
  8250. (org-show-context 'tags-tree))
  8251. (setq txt (org-format-agenda-item
  8252. ""
  8253. (concat
  8254. (if org-tags-match-list-sublevels
  8255. (make-string (1- level) ?.) "")
  8256. (org-get-heading))
  8257. category tags-list)
  8258. priority (org-get-priority txt))
  8259. (goto-char lspos)
  8260. (setq marker (org-agenda-new-marker))
  8261. (org-add-props txt props
  8262. 'org-marker marker 'org-hd-marker marker 'org-category category
  8263. 'priority priority 'type "tagsmatch")
  8264. (push txt rtn))
  8265. ;; if we are to skip sublevels, jump to end of subtree
  8266. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8267. (when (and (eq action 'sparse-tree)
  8268. (not org-sparse-tree-open-archived-trees))
  8269. (org-hide-archived-subtrees (point-min) (point-max)))
  8270. (nreverse rtn)))
  8271. (defun org-remove-uniherited-tags (tags)
  8272. "Remove all tags that are not inherited from the list TAGS."
  8273. (cond
  8274. ((eq org-use-tag-inheritance t) tags)
  8275. ((not org-use-tag-inheritance) nil)
  8276. ((stringp org-use-tag-inheritance)
  8277. (delq nil (mapcar
  8278. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8279. tags)))
  8280. ((listp org-use-tag-inheritance)
  8281. (org-delete-all org-use-tag-inheritance tags))))
  8282. (defvar todo-only) ;; dynamically scoped
  8283. (defun org-tags-sparse-tree (&optional todo-only match)
  8284. "Create a sparse tree according to tags string MATCH.
  8285. MATCH can contain positive and negative selection of tags, like
  8286. \"+WORK+URGENT-WITHBOSS\".
  8287. If optional argument TODO_ONLY is non-nil, only select lines that are
  8288. also TODO lines."
  8289. (interactive "P")
  8290. (org-prepare-agenda-buffers (list (current-buffer)))
  8291. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8292. (defvar org-cached-props nil)
  8293. (defun org-cached-entry-get (pom property)
  8294. (if (or (eq t org-use-property-inheritance)
  8295. (and (stringp org-use-property-inheritance)
  8296. (string-match org-use-property-inheritance property))
  8297. (and (listp org-use-property-inheritance)
  8298. (member property org-use-property-inheritance)))
  8299. ;; Caching is not possible, check it directly
  8300. (org-entry-get pom property 'inherit)
  8301. ;; Get all properties, so that we can do complicated checks easily
  8302. (cdr (assoc property (or org-cached-props
  8303. (setq org-cached-props
  8304. (org-entry-properties pom)))))))
  8305. (defun org-global-tags-completion-table (&optional files)
  8306. "Return the list of all tags in all agenda buffer/files."
  8307. (save-excursion
  8308. (org-uniquify
  8309. (delq nil
  8310. (apply 'append
  8311. (mapcar
  8312. (lambda (file)
  8313. (set-buffer (find-file-noselect file))
  8314. (append (org-get-buffer-tags)
  8315. (mapcar (lambda (x) (if (stringp (car-safe x))
  8316. (list (car-safe x)) nil))
  8317. org-tag-alist)))
  8318. (if (and files (car files))
  8319. files
  8320. (org-agenda-files))))))))
  8321. (defun org-make-tags-matcher (match)
  8322. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8323. ;; todo-only is scoped dynamically into this function, and the function
  8324. ;; may change it it the matcher asksk for it.
  8325. (unless match
  8326. ;; Get a new match request, with completion
  8327. (let ((org-last-tags-completion-table
  8328. (org-global-tags-completion-table)))
  8329. (setq match (completing-read
  8330. "Match: " 'org-tags-completion-function nil nil nil
  8331. 'org-tags-history))))
  8332. ;; Parse the string and create a lisp form
  8333. (let ((match0 match)
  8334. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8335. minus tag mm
  8336. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8337. orterms term orlist re-p str-p level-p level-op
  8338. prop-p pn pv po cat-p gv)
  8339. (if (string-match "/+" match)
  8340. ;; match contains also a todo-matching request
  8341. (progn
  8342. (setq tagsmatch (substring match 0 (match-beginning 0))
  8343. todomatch (substring match (match-end 0)))
  8344. (if (string-match "^!" todomatch)
  8345. (setq todo-only t todomatch (substring todomatch 1)))
  8346. (if (string-match "^\\s-*$" todomatch)
  8347. (setq todomatch nil)))
  8348. ;; only matching tags
  8349. (setq tagsmatch match todomatch nil))
  8350. ;; Make the tags matcher
  8351. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8352. (setq tagsmatcher t)
  8353. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8354. (while (setq term (pop orterms))
  8355. (while (and (equal (substring term -1) "\\") orterms)
  8356. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8357. (while (string-match re term)
  8358. (setq minus (and (match-end 1)
  8359. (equal (match-string 1 term) "-"))
  8360. tag (match-string 2 term)
  8361. re-p (equal (string-to-char tag) ?{)
  8362. level-p (match-end 4)
  8363. prop-p (match-end 5)
  8364. mm (cond
  8365. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8366. (level-p
  8367. (setq level-op (org-op-to-function (match-string 3 term)))
  8368. `(,level-op level ,(string-to-number
  8369. (match-string 4 term))))
  8370. (prop-p
  8371. (setq pn (match-string 5 term)
  8372. po (match-string 6 term)
  8373. pv (match-string 7 term)
  8374. cat-p (equal pn "CATEGORY")
  8375. re-p (equal (string-to-char pv) ?{)
  8376. str-p (equal (string-to-char pv) ?\")
  8377. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8378. (setq po (org-op-to-function po str-p))
  8379. (if (equal pn "CATEGORY")
  8380. (setq gv '(get-text-property (point) 'org-category))
  8381. (setq gv `(org-cached-entry-get nil ,pn)))
  8382. (if re-p
  8383. (if (eq po 'org<>)
  8384. `(not (string-match ,pv (or ,gv "")))
  8385. `(string-match ,pv (or ,gv "")))
  8386. (if str-p
  8387. `(,po (or ,gv "") ,pv)
  8388. `(,po (string-to-number (or ,gv ""))
  8389. ,(string-to-number pv) ))))
  8390. (t `(member ,(downcase tag) tags-list)))
  8391. mm (if minus (list 'not mm) mm)
  8392. term (substring term (match-end 0)))
  8393. (push mm tagsmatcher))
  8394. (push (if (> (length tagsmatcher) 1)
  8395. (cons 'and tagsmatcher)
  8396. (car tagsmatcher))
  8397. orlist)
  8398. (setq tagsmatcher nil))
  8399. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8400. (setq tagsmatcher
  8401. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8402. ;; Make the todo matcher
  8403. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8404. (setq todomatcher t)
  8405. (setq orterms (org-split-string todomatch "|") orlist nil)
  8406. (while (setq term (pop orterms))
  8407. (while (string-match re term)
  8408. (setq minus (and (match-end 1)
  8409. (equal (match-string 1 term) "-"))
  8410. kwd (match-string 2 term)
  8411. re-p (equal (string-to-char kwd) ?{)
  8412. term (substring term (match-end 0))
  8413. mm (if re-p
  8414. `(string-match ,(substring kwd 1 -1) todo)
  8415. (list 'equal 'todo kwd))
  8416. mm (if minus (list 'not mm) mm))
  8417. (push mm todomatcher))
  8418. (push (if (> (length todomatcher) 1)
  8419. (cons 'and todomatcher)
  8420. (car todomatcher))
  8421. orlist)
  8422. (setq todomatcher nil))
  8423. (setq todomatcher (if (> (length orlist) 1)
  8424. (cons 'or orlist) (car orlist))))
  8425. ;; Return the string and lisp forms of the matcher
  8426. (setq matcher (if todomatcher
  8427. (list 'and tagsmatcher todomatcher)
  8428. tagsmatcher))
  8429. (cons match0 matcher)))
  8430. (defun org-op-to-function (op &optional stringp)
  8431. (setq op
  8432. (cond
  8433. ((equal op "<" ) '(< string< ))
  8434. ((equal op ">" ) '(> org-string> ))
  8435. ((member op '("<=" "=<")) '(<= org-string<= ))
  8436. ((member op '(">=" "=>")) '(>= org-string>= ))
  8437. ((member op '("=" "==")) '(= string= ))
  8438. ((member op '("<>" "!=")) '(org<> org-string<> ))))
  8439. (nth (if stringp 1 0) op))
  8440. (defun org<> (a b) (not (= a b)))
  8441. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8442. (defun org-string>= (a b) (not (string< a b)))
  8443. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8444. (defun org-string<> (a b) (not (string= a b)))
  8445. (defun org-match-any-p (re list)
  8446. "Does re match any element of list?"
  8447. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8448. (delq nil list))
  8449. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8450. (defvar org-tags-overlay (org-make-overlay 1 1))
  8451. (org-detach-overlay org-tags-overlay)
  8452. (defun org-get-tags-at (&optional pos)
  8453. "Get a list of all headline tags applicable at POS.
  8454. POS defaults to point. If tags are inherited, the list contains
  8455. the targets in the same sequence as the headlines appear, i.e.
  8456. sthe tags of the current headline come last."
  8457. (interactive)
  8458. (let (tags ltags lastpos parent)
  8459. (save-excursion
  8460. (save-restriction
  8461. (widen)
  8462. (goto-char (or pos (point)))
  8463. (save-match-data
  8464. (condition-case nil
  8465. (progn
  8466. (org-back-to-heading t)
  8467. (while (not (equal lastpos (point)))
  8468. (setq lastpos (point))
  8469. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8470. (setq ltags (org-split-string
  8471. (org-match-string-no-properties 1) ":"))
  8472. (setq tags (append (org-remove-uniherited-tags ltags)
  8473. tags)))
  8474. (or org-use-tag-inheritance (error ""))
  8475. (org-up-heading-all 1)
  8476. (setq parent t)))
  8477. (error nil))))
  8478. tags)))
  8479. (defun org-toggle-tag (tag &optional onoff)
  8480. "Toggle the tag TAG for the current line.
  8481. If ONOFF is `on' or `off', don't toggle but set to this state."
  8482. (unless (org-on-heading-p t) (error "Not on headling"))
  8483. (let (res current)
  8484. (save-excursion
  8485. (beginning-of-line)
  8486. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8487. (point-at-eol) t)
  8488. (progn
  8489. (setq current (match-string 1))
  8490. (replace-match ""))
  8491. (setq current ""))
  8492. (setq current (nreverse (org-split-string current ":")))
  8493. (cond
  8494. ((eq onoff 'on)
  8495. (setq res t)
  8496. (or (member tag current) (push tag current)))
  8497. ((eq onoff 'off)
  8498. (or (not (member tag current)) (setq current (delete tag current))))
  8499. (t (if (member tag current)
  8500. (setq current (delete tag current))
  8501. (setq res t)
  8502. (push tag current))))
  8503. (end-of-line 1)
  8504. (if current
  8505. (progn
  8506. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8507. (org-set-tags nil t))
  8508. (delete-horizontal-space))
  8509. (run-hooks 'org-after-tags-change-hook))
  8510. res))
  8511. (defun org-align-tags-here (to-col)
  8512. ;; Assumes that this is a headline
  8513. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8514. (beginning-of-line 1)
  8515. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8516. (< pos (match-beginning 2)))
  8517. (progn
  8518. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8519. (goto-char (match-beginning 1))
  8520. (insert " ")
  8521. (delete-region (point) (1+ (match-beginning 2)))
  8522. (setq ncol (max (1+ (current-column))
  8523. (1+ col)
  8524. (if (> to-col 0)
  8525. to-col
  8526. (- (abs to-col) tags-l))))
  8527. (setq p (point))
  8528. (insert (make-string (- ncol (current-column)) ?\ ))
  8529. (setq ncol (current-column))
  8530. (tabify p (point-at-eol))
  8531. (org-move-to-column (min ncol col) t))
  8532. (goto-char pos))))
  8533. (defun org-set-tags (&optional arg just-align)
  8534. "Set the tags for the current headline.
  8535. With prefix ARG, realign all tags in headings in the current buffer."
  8536. (interactive "P")
  8537. (let* ((re (concat "^" outline-regexp))
  8538. (current (org-get-tags-string))
  8539. (col (current-column))
  8540. (org-setting-tags t)
  8541. table current-tags inherited-tags ; computed below when needed
  8542. tags p0 c0 c1 rpl)
  8543. (if arg
  8544. (save-excursion
  8545. (goto-char (point-min))
  8546. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8547. (while (re-search-forward re nil t)
  8548. (org-set-tags nil t)
  8549. (end-of-line 1)))
  8550. (message "All tags realigned to column %d" org-tags-column))
  8551. (if just-align
  8552. (setq tags current)
  8553. ;; Get a new set of tags from the user
  8554. (save-excursion
  8555. (setq table (or org-tag-alist (org-get-buffer-tags))
  8556. org-last-tags-completion-table table
  8557. current-tags (org-split-string current ":")
  8558. inherited-tags (nreverse
  8559. (nthcdr (length current-tags)
  8560. (nreverse (org-get-tags-at))))
  8561. tags
  8562. (if (or (eq t org-use-fast-tag-selection)
  8563. (and org-use-fast-tag-selection
  8564. (delq nil (mapcar 'cdr table))))
  8565. (org-fast-tag-selection
  8566. current-tags inherited-tags table
  8567. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8568. (let ((org-add-colon-after-tag-completion t))
  8569. (org-trim
  8570. (org-without-partial-completion
  8571. (completing-read "Tags: " 'org-tags-completion-function
  8572. nil nil current 'org-tags-history)))))))
  8573. (while (string-match "[-+&]+" tags)
  8574. ;; No boolean logic, just a list
  8575. (setq tags (replace-match ":" t t tags))))
  8576. (if (string-match "\\`[\t ]*\\'" tags)
  8577. (setq tags "")
  8578. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8579. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8580. ;; Insert new tags at the correct column
  8581. (beginning-of-line 1)
  8582. (cond
  8583. ((and (equal current "") (equal tags "")))
  8584. ((re-search-forward
  8585. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8586. (point-at-eol) t)
  8587. (if (equal tags "")
  8588. (setq rpl "")
  8589. (goto-char (match-beginning 0))
  8590. (setq c0 (current-column) p0 (point)
  8591. c1 (max (1+ c0) (if (> org-tags-column 0)
  8592. org-tags-column
  8593. (- (- org-tags-column) (length tags))))
  8594. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8595. (replace-match rpl t t)
  8596. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8597. tags)
  8598. (t (error "Tags alignment failed")))
  8599. (org-move-to-column col)
  8600. (unless just-align
  8601. (run-hooks 'org-after-tags-change-hook)))))
  8602. (defun org-change-tag-in-region (beg end tag off)
  8603. "Add or remove TAG for each entry in the region.
  8604. This works in the agenda, and also in an org-mode buffer."
  8605. (interactive
  8606. (list (region-beginning) (region-end)
  8607. (let ((org-last-tags-completion-table
  8608. (if (org-mode-p)
  8609. (org-get-buffer-tags)
  8610. (org-global-tags-completion-table))))
  8611. (completing-read
  8612. "Tag: " 'org-tags-completion-function nil nil nil
  8613. 'org-tags-history))
  8614. (progn
  8615. (message "[s]et or [r]emove? ")
  8616. (equal (read-char-exclusive) ?r))))
  8617. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8618. (let ((agendap (equal major-mode 'org-agenda-mode))
  8619. l1 l2 m buf pos newhead (cnt 0))
  8620. (goto-char end)
  8621. (setq l2 (1- (org-current-line)))
  8622. (goto-char beg)
  8623. (setq l1 (org-current-line))
  8624. (loop for l from l1 to l2 do
  8625. (goto-line l)
  8626. (setq m (get-text-property (point) 'org-hd-marker))
  8627. (when (or (and (org-mode-p) (org-on-heading-p))
  8628. (and agendap m))
  8629. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8630. pos (if agendap m (point)))
  8631. (with-current-buffer buf
  8632. (save-excursion
  8633. (save-restriction
  8634. (goto-char pos)
  8635. (setq cnt (1+ cnt))
  8636. (org-toggle-tag tag (if off 'off 'on))
  8637. (setq newhead (org-get-heading)))))
  8638. (and agendap (org-agenda-change-all-lines newhead m))))
  8639. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8640. (defun org-tags-completion-function (string predicate &optional flag)
  8641. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8642. (confirm (lambda (x) (stringp (car x)))))
  8643. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8644. (setq s1 (match-string 1 string)
  8645. s2 (match-string 2 string))
  8646. (setq s1 "" s2 string))
  8647. (cond
  8648. ((eq flag nil)
  8649. ;; try completion
  8650. (setq rtn (try-completion s2 ctable confirm))
  8651. (if (stringp rtn)
  8652. (setq rtn
  8653. (concat s1 s2 (substring rtn (length s2))
  8654. (if (and org-add-colon-after-tag-completion
  8655. (assoc rtn ctable))
  8656. ":" ""))))
  8657. rtn)
  8658. ((eq flag t)
  8659. ;; all-completions
  8660. (all-completions s2 ctable confirm)
  8661. )
  8662. ((eq flag 'lambda)
  8663. ;; exact match?
  8664. (assoc s2 ctable)))
  8665. ))
  8666. (defun org-fast-tag-insert (kwd tags face &optional end)
  8667. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8668. (insert (format "%-12s" (concat kwd ":"))
  8669. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8670. (or end "")))
  8671. (defun org-fast-tag-show-exit (flag)
  8672. (save-excursion
  8673. (goto-line 3)
  8674. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8675. (replace-match ""))
  8676. (when flag
  8677. (end-of-line 1)
  8678. (org-move-to-column (- (window-width) 19) t)
  8679. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8680. (defun org-set-current-tags-overlay (current prefix)
  8681. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8682. (if (featurep 'xemacs)
  8683. (org-overlay-display org-tags-overlay (concat prefix s)
  8684. 'secondary-selection)
  8685. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8686. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8687. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8688. "Fast tag selection with single keys.
  8689. CURRENT is the current list of tags in the headline, INHERITED is the
  8690. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8691. possibly with grouping information. TODO-TABLE is a similar table with
  8692. TODO keywords, should these have keys assigned to them.
  8693. If the keys are nil, a-z are automatically assigned.
  8694. Returns the new tags string, or nil to not change the current settings."
  8695. (let* ((fulltable (append table todo-table))
  8696. (maxlen (apply 'max (mapcar
  8697. (lambda (x)
  8698. (if (stringp (car x)) (string-width (car x)) 0))
  8699. fulltable)))
  8700. (buf (current-buffer))
  8701. (expert (eq org-fast-tag-selection-single-key 'expert))
  8702. (buffer-tags nil)
  8703. (fwidth (+ maxlen 3 1 3))
  8704. (ncol (/ (- (window-width) 4) fwidth))
  8705. (i-face 'org-done)
  8706. (c-face 'org-todo)
  8707. tg cnt e c char c1 c2 ntable tbl rtn
  8708. ov-start ov-end ov-prefix
  8709. (exit-after-next org-fast-tag-selection-single-key)
  8710. (done-keywords org-done-keywords)
  8711. groups ingroup)
  8712. (save-excursion
  8713. (beginning-of-line 1)
  8714. (if (looking-at
  8715. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8716. (setq ov-start (match-beginning 1)
  8717. ov-end (match-end 1)
  8718. ov-prefix "")
  8719. (setq ov-start (1- (point-at-eol))
  8720. ov-end (1+ ov-start))
  8721. (skip-chars-forward "^\n\r")
  8722. (setq ov-prefix
  8723. (concat
  8724. (buffer-substring (1- (point)) (point))
  8725. (if (> (current-column) org-tags-column)
  8726. " "
  8727. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8728. (org-move-overlay org-tags-overlay ov-start ov-end)
  8729. (save-window-excursion
  8730. (if expert
  8731. (set-buffer (get-buffer-create " *Org tags*"))
  8732. (delete-other-windows)
  8733. (split-window-vertically)
  8734. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8735. (erase-buffer)
  8736. (org-set-local 'org-done-keywords done-keywords)
  8737. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8738. (org-fast-tag-insert "Current" current c-face "\n\n")
  8739. (org-fast-tag-show-exit exit-after-next)
  8740. (org-set-current-tags-overlay current ov-prefix)
  8741. (setq tbl fulltable char ?a cnt 0)
  8742. (while (setq e (pop tbl))
  8743. (cond
  8744. ((equal e '(:startgroup))
  8745. (push '() groups) (setq ingroup t)
  8746. (when (not (= cnt 0))
  8747. (setq cnt 0)
  8748. (insert "\n"))
  8749. (insert "{ "))
  8750. ((equal e '(:endgroup))
  8751. (setq ingroup nil cnt 0)
  8752. (insert "}\n"))
  8753. (t
  8754. (setq tg (car e) c2 nil)
  8755. (if (cdr e)
  8756. (setq c (cdr e))
  8757. ;; automatically assign a character.
  8758. (setq c1 (string-to-char
  8759. (downcase (substring
  8760. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8761. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8762. (while (or (rassoc char ntable) (rassoc char table))
  8763. (setq char (1+ char)))
  8764. (setq c2 c1))
  8765. (setq c (or c2 char)))
  8766. (if ingroup (push tg (car groups)))
  8767. (setq tg (org-add-props tg nil 'face
  8768. (cond
  8769. ((not (assoc tg table))
  8770. (org-get-todo-face tg))
  8771. ((member tg current) c-face)
  8772. ((member tg inherited) i-face)
  8773. (t nil))))
  8774. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8775. (insert "[" c "] " tg (make-string
  8776. (- fwidth 4 (length tg)) ?\ ))
  8777. (push (cons tg c) ntable)
  8778. (when (= (setq cnt (1+ cnt)) ncol)
  8779. (insert "\n")
  8780. (if ingroup (insert " "))
  8781. (setq cnt 0)))))
  8782. (setq ntable (nreverse ntable))
  8783. (insert "\n")
  8784. (goto-char (point-min))
  8785. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8786. (fit-window-to-buffer))
  8787. (setq rtn
  8788. (catch 'exit
  8789. (while t
  8790. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8791. (if groups " [!] no groups" " [!]groups")
  8792. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8793. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8794. (cond
  8795. ((= c ?\r) (throw 'exit t))
  8796. ((= c ?!)
  8797. (setq groups (not groups))
  8798. (goto-char (point-min))
  8799. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8800. ((= c ?\C-c)
  8801. (if (not expert)
  8802. (org-fast-tag-show-exit
  8803. (setq exit-after-next (not exit-after-next)))
  8804. (setq expert nil)
  8805. (delete-other-windows)
  8806. (split-window-vertically)
  8807. (org-switch-to-buffer-other-window " *Org tags*")
  8808. (and (fboundp 'fit-window-to-buffer)
  8809. (fit-window-to-buffer))))
  8810. ((or (= c ?\C-g)
  8811. (and (= c ?q) (not (rassoc c ntable))))
  8812. (org-detach-overlay org-tags-overlay)
  8813. (setq quit-flag t))
  8814. ((= c ?\ )
  8815. (setq current nil)
  8816. (if exit-after-next (setq exit-after-next 'now)))
  8817. ((= c ?\t)
  8818. (condition-case nil
  8819. (setq tg (completing-read
  8820. "Tag: "
  8821. (or buffer-tags
  8822. (with-current-buffer buf
  8823. (org-get-buffer-tags)))))
  8824. (quit (setq tg "")))
  8825. (when (string-match "\\S-" tg)
  8826. (add-to-list 'buffer-tags (list tg))
  8827. (if (member tg current)
  8828. (setq current (delete tg current))
  8829. (push tg current)))
  8830. (if exit-after-next (setq exit-after-next 'now)))
  8831. ((setq e (rassoc c todo-table) tg (car e))
  8832. (with-current-buffer buf
  8833. (save-excursion (org-todo tg)))
  8834. (if exit-after-next (setq exit-after-next 'now)))
  8835. ((setq e (rassoc c ntable) tg (car e))
  8836. (if (member tg current)
  8837. (setq current (delete tg current))
  8838. (loop for g in groups do
  8839. (if (member tg g)
  8840. (mapc (lambda (x)
  8841. (setq current (delete x current)))
  8842. g)))
  8843. (push tg current))
  8844. (if exit-after-next (setq exit-after-next 'now))))
  8845. ;; Create a sorted list
  8846. (setq current
  8847. (sort current
  8848. (lambda (a b)
  8849. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8850. (if (eq exit-after-next 'now) (throw 'exit t))
  8851. (goto-char (point-min))
  8852. (beginning-of-line 2)
  8853. (delete-region (point) (point-at-eol))
  8854. (org-fast-tag-insert "Current" current c-face)
  8855. (org-set-current-tags-overlay current ov-prefix)
  8856. (while (re-search-forward
  8857. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8858. (setq tg (match-string 1))
  8859. (add-text-properties
  8860. (match-beginning 1) (match-end 1)
  8861. (list 'face
  8862. (cond
  8863. ((member tg current) c-face)
  8864. ((member tg inherited) i-face)
  8865. (t (get-text-property (match-beginning 1) 'face))))))
  8866. (goto-char (point-min)))))
  8867. (org-detach-overlay org-tags-overlay)
  8868. (if rtn
  8869. (mapconcat 'identity current ":")
  8870. nil))))
  8871. (defun org-get-tags-string ()
  8872. "Get the TAGS string in the current headline."
  8873. (unless (org-on-heading-p t)
  8874. (error "Not on a heading"))
  8875. (save-excursion
  8876. (beginning-of-line 1)
  8877. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8878. (org-match-string-no-properties 1)
  8879. "")))
  8880. (defun org-get-tags ()
  8881. "Get the list of tags specified in the current headline."
  8882. (org-split-string (org-get-tags-string) ":"))
  8883. (defun org-get-buffer-tags ()
  8884. "Get a table of all tags used in the buffer, for completion."
  8885. (let (tags)
  8886. (save-excursion
  8887. (goto-char (point-min))
  8888. (while (re-search-forward
  8889. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8890. (when (equal (char-after (point-at-bol 0)) ?*)
  8891. (mapc (lambda (x) (add-to-list 'tags x))
  8892. (org-split-string (org-match-string-no-properties 1) ":")))))
  8893. (mapcar 'list tags)))
  8894. ;;;; Properties
  8895. ;;; Setting and retrieving properties
  8896. (defconst org-special-properties
  8897. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8898. "TIMESTAMP" "TIMESTAMP_IA")
  8899. "The special properties valid in Org-mode.
  8900. These are properties that are not defined in the property drawer,
  8901. but in some other way.")
  8902. (defconst org-default-properties
  8903. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8904. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  8905. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE")
  8906. "Some properties that are used by Org-mode for various purposes.
  8907. Being in this list makes sure that they are offered for completion.")
  8908. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8909. "Regular expression matching the first line of a property drawer.")
  8910. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8911. "Regular expression matching the first line of a property drawer.")
  8912. (defun org-property-action ()
  8913. "Do an action on properties."
  8914. (interactive)
  8915. (let (c)
  8916. (org-at-property-p)
  8917. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8918. (setq c (read-char-exclusive))
  8919. (cond
  8920. ((equal c ?s)
  8921. (call-interactively 'org-set-property))
  8922. ((equal c ?d)
  8923. (call-interactively 'org-delete-property))
  8924. ((equal c ?D)
  8925. (call-interactively 'org-delete-property-globally))
  8926. ((equal c ?c)
  8927. (call-interactively 'org-compute-property-at-point))
  8928. (t (error "No such property action %c" c)))))
  8929. (defun org-at-property-p ()
  8930. "Is the cursor in a property line?"
  8931. ;; FIXME: Does not check if we are actually in the drawer.
  8932. ;; FIXME: also returns true on any drawers.....
  8933. ;; This is used by C-c C-c for property action.
  8934. (save-excursion
  8935. (beginning-of-line 1)
  8936. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8937. (defun org-get-property-block (&optional beg end force)
  8938. "Return the (beg . end) range of the body of the property drawer.
  8939. BEG and END can be beginning and end of subtree, if not given
  8940. they will be found.
  8941. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8942. (catch 'exit
  8943. (save-excursion
  8944. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8945. (end (or end (progn (outline-next-heading) (point)))))
  8946. (goto-char beg)
  8947. (if (re-search-forward org-property-start-re end t)
  8948. (setq beg (1+ (match-end 0)))
  8949. (if force
  8950. (save-excursion
  8951. (org-insert-property-drawer)
  8952. (setq end (progn (outline-next-heading) (point))))
  8953. (throw 'exit nil))
  8954. (goto-char beg)
  8955. (if (re-search-forward org-property-start-re end t)
  8956. (setq beg (1+ (match-end 0)))))
  8957. (if (re-search-forward org-property-end-re end t)
  8958. (setq end (match-beginning 0))
  8959. (or force (throw 'exit nil))
  8960. (goto-char beg)
  8961. (setq end beg)
  8962. (org-indent-line-function)
  8963. (insert ":END:\n"))
  8964. (cons beg end)))))
  8965. (defun org-entry-properties (&optional pom which)
  8966. "Get all properties of the entry at point-or-marker POM.
  8967. This includes the TODO keyword, the tags, time strings for deadline,
  8968. scheduled, and clocking, and any additional properties defined in the
  8969. entry. The return value is an alist, keys may occur multiple times
  8970. if the property key was used several times.
  8971. POM may also be nil, in which case the current entry is used.
  8972. If WHICH is nil or `all', get all properties. If WHICH is
  8973. `special' or `standard', only get that subclass."
  8974. (setq which (or which 'all))
  8975. (org-with-point-at pom
  8976. (let ((clockstr (substring org-clock-string 0 -1))
  8977. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8978. beg end range props sum-props key value string clocksum)
  8979. (save-excursion
  8980. (when (condition-case nil (org-back-to-heading t) (error nil))
  8981. (setq beg (point))
  8982. (setq sum-props (get-text-property (point) 'org-summaries))
  8983. (setq clocksum (get-text-property (point) :org-clock-minutes))
  8984. (outline-next-heading)
  8985. (setq end (point))
  8986. (when (memq which '(all special))
  8987. ;; Get the special properties, like TODO and tags
  8988. (goto-char beg)
  8989. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  8990. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  8991. (when (looking-at org-priority-regexp)
  8992. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  8993. (when (and (setq value (org-get-tags-string))
  8994. (string-match "\\S-" value))
  8995. (push (cons "TAGS" value) props))
  8996. (when (setq value (org-get-tags-at))
  8997. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  8998. props))
  8999. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9000. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9001. string (if (equal key clockstr)
  9002. (org-no-properties
  9003. (org-trim
  9004. (buffer-substring
  9005. (match-beginning 3) (goto-char (point-at-eol)))))
  9006. (substring (org-match-string-no-properties 3) 1 -1)))
  9007. (unless key
  9008. (if (= (char-after (match-beginning 3)) ?\[)
  9009. (setq key "TIMESTAMP_IA")
  9010. (setq key "TIMESTAMP")))
  9011. (when (or (equal key clockstr) (not (assoc key props)))
  9012. (push (cons key string) props)))
  9013. )
  9014. (when (memq which '(all standard))
  9015. ;; Get the standard properties, like :PORP: ...
  9016. (setq range (org-get-property-block beg end))
  9017. (when range
  9018. (goto-char (car range))
  9019. (while (re-search-forward
  9020. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9021. (cdr range) t)
  9022. (setq key (org-match-string-no-properties 1)
  9023. value (org-trim (or (org-match-string-no-properties 2) "")))
  9024. (unless (member key excluded)
  9025. (push (cons key (or value "")) props)))))
  9026. (if clocksum
  9027. (push (cons "CLOCKSUM"
  9028. (org-columns-number-to-string (/ (float clocksum) 60.)
  9029. 'add_times))
  9030. props))
  9031. (append sum-props (nreverse props)))))))
  9032. (defun org-entry-get (pom property &optional inherit)
  9033. "Get value of PROPERTY for entry at point-or-marker POM.
  9034. If INHERIT is non-nil and the entry does not have the property,
  9035. then also check higher levels of the hierarchy.
  9036. If INHERIT is the symbol `selective', use inheritance only if the setting
  9037. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9038. If the property is present but empty, the return value is the empty string.
  9039. If the property is not present at all, nil is returned."
  9040. (org-with-point-at pom
  9041. (if (and inherit (if (eq inherit 'selective)
  9042. (org-property-inherit-p property)
  9043. t))
  9044. (org-entry-get-with-inheritance property)
  9045. (if (member property org-special-properties)
  9046. ;; We need a special property. Use brute force, get all properties.
  9047. (cdr (assoc property (org-entry-properties nil 'special)))
  9048. (let ((range (org-get-property-block)))
  9049. (if (and range
  9050. (goto-char (car range))
  9051. (re-search-forward
  9052. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9053. (cdr range) t))
  9054. ;; Found the property, return it.
  9055. (if (match-end 1)
  9056. (org-match-string-no-properties 1)
  9057. "")))))))
  9058. (defun org-property-or-variable-value (var &optional inherit)
  9059. "Check if there is a property fixing the value of VAR.
  9060. If yes, return this value. If not, return the current value of the variable."
  9061. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9062. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9063. (read prop)
  9064. (symbol-value var))))
  9065. (defun org-entry-delete (pom property)
  9066. "Delete the property PROPERTY from entry at point-or-marker POM."
  9067. (org-with-point-at pom
  9068. (if (member property org-special-properties)
  9069. nil ; cannot delete these properties.
  9070. (let ((range (org-get-property-block)))
  9071. (if (and range
  9072. (goto-char (car range))
  9073. (re-search-forward
  9074. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9075. (cdr range) t))
  9076. (progn
  9077. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9078. t)
  9079. nil)))))
  9080. ;; Multi-values properties are properties that contain multiple values
  9081. ;; These values are assumed to be single words, separated by whitespace.
  9082. (defun org-entry-add-to-multivalued-property (pom property value)
  9083. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9084. (let* ((old (org-entry-get pom property))
  9085. (values (and old (org-split-string old "[ \t]"))))
  9086. (unless (member value values)
  9087. (setq values (cons value values))
  9088. (org-entry-put pom property
  9089. (mapconcat 'identity values " ")))))
  9090. (defun org-entry-remove-from-multivalued-property (pom property value)
  9091. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9092. (let* ((old (org-entry-get pom property))
  9093. (values (and old (org-split-string old "[ \t]"))))
  9094. (when (member value values)
  9095. (setq values (delete value values))
  9096. (org-entry-put pom property
  9097. (mapconcat 'identity values " ")))))
  9098. (defun org-entry-member-in-multivalued-property (pom property value)
  9099. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9100. (let* ((old (org-entry-get pom property))
  9101. (values (and old (org-split-string old "[ \t]"))))
  9102. (member value values)))
  9103. (defvar org-entry-property-inherited-from (make-marker))
  9104. (defun org-entry-get-with-inheritance (property)
  9105. "Get entry property, and search higher levels if not present."
  9106. (let (tmp)
  9107. (save-excursion
  9108. (save-restriction
  9109. (widen)
  9110. (catch 'ex
  9111. (while t
  9112. (when (setq tmp (org-entry-get nil property))
  9113. (org-back-to-heading t)
  9114. (move-marker org-entry-property-inherited-from (point))
  9115. (throw 'ex tmp))
  9116. (or (org-up-heading-safe) (throw 'ex nil)))))
  9117. (or tmp
  9118. (cdr (assoc property org-local-properties))
  9119. (cdr (assoc property org-global-properties))
  9120. (cdr (assoc property org-global-properties-fixed))))))
  9121. (defun org-entry-put (pom property value)
  9122. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9123. (org-with-point-at pom
  9124. (org-back-to-heading t)
  9125. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9126. range)
  9127. (cond
  9128. ((equal property "TODO")
  9129. (when (and (stringp value) (string-match "\\S-" value)
  9130. (not (member value org-todo-keywords-1)))
  9131. (error "\"%s\" is not a valid TODO state" value))
  9132. (if (or (not value)
  9133. (not (string-match "\\S-" value)))
  9134. (setq value 'none))
  9135. (org-todo value)
  9136. (org-set-tags nil 'align))
  9137. ((equal property "PRIORITY")
  9138. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9139. (string-to-char value) ?\ ))
  9140. (org-set-tags nil 'align))
  9141. ((equal property "SCHEDULED")
  9142. (if (re-search-forward org-scheduled-time-regexp end t)
  9143. (cond
  9144. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9145. ((eq value 'later) (org-timestamp-change 1 'day))
  9146. (t (call-interactively 'org-schedule)))
  9147. (call-interactively 'org-schedule)))
  9148. ((equal property "DEADLINE")
  9149. (if (re-search-forward org-deadline-time-regexp end t)
  9150. (cond
  9151. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9152. ((eq value 'later) (org-timestamp-change 1 'day))
  9153. (t (call-interactively 'org-deadline)))
  9154. (call-interactively 'org-deadline)))
  9155. ((member property org-special-properties)
  9156. (error "The %s property can not yet be set with `org-entry-put'"
  9157. property))
  9158. (t ; a non-special property
  9159. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9160. (setq range (org-get-property-block beg end 'force))
  9161. (goto-char (car range))
  9162. (if (re-search-forward
  9163. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9164. (progn
  9165. (delete-region (match-beginning 1) (match-end 1))
  9166. (goto-char (match-beginning 1)))
  9167. (goto-char (cdr range))
  9168. (insert "\n")
  9169. (backward-char 1)
  9170. (org-indent-line-function)
  9171. (insert ":" property ":"))
  9172. (and value (insert " " value))
  9173. (org-indent-line-function)))))))
  9174. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9175. "Get all property keys in the current buffer.
  9176. With INCLUDE-SPECIALS, also list the special properties that relect things
  9177. like tags and TODO state.
  9178. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9179. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9180. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9181. formats in the current buffer."
  9182. (let (rtn range cfmt cols s p)
  9183. (save-excursion
  9184. (save-restriction
  9185. (widen)
  9186. (goto-char (point-min))
  9187. (while (re-search-forward org-property-start-re nil t)
  9188. (setq range (org-get-property-block))
  9189. (goto-char (car range))
  9190. (while (re-search-forward
  9191. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9192. (cdr range) t)
  9193. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9194. (outline-next-heading))))
  9195. (when include-specials
  9196. (setq rtn (append org-special-properties rtn)))
  9197. (when include-defaults
  9198. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9199. (when include-columns
  9200. (save-excursion
  9201. (save-restriction
  9202. (widen)
  9203. (goto-char (point-min))
  9204. (while (re-search-forward
  9205. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9206. nil t)
  9207. (setq cfmt (match-string 2) s 0)
  9208. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9209. cfmt s)
  9210. (setq s (match-end 0)
  9211. p (match-string 1 cfmt))
  9212. (unless (or (equal p "ITEM")
  9213. (member p org-special-properties))
  9214. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9215. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9216. (defun org-property-values (key)
  9217. "Return a list of all values of property KEY."
  9218. (save-excursion
  9219. (save-restriction
  9220. (widen)
  9221. (goto-char (point-min))
  9222. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9223. values)
  9224. (while (re-search-forward re nil t)
  9225. (add-to-list 'values (org-trim (match-string 1))))
  9226. (delete "" values)))))
  9227. (defun org-insert-property-drawer ()
  9228. "Insert a property drawer into the current entry."
  9229. (interactive)
  9230. (org-back-to-heading t)
  9231. (looking-at outline-regexp)
  9232. (let ((indent (- (match-end 0)(match-beginning 0)))
  9233. (beg (point))
  9234. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9235. end hiddenp)
  9236. (outline-next-heading)
  9237. (setq end (point))
  9238. (goto-char beg)
  9239. (while (re-search-forward re end t))
  9240. (setq hiddenp (org-invisible-p))
  9241. (end-of-line 1)
  9242. (and (equal (char-after) ?\n) (forward-char 1))
  9243. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9244. (beginning-of-line 2))
  9245. (org-skip-over-state-notes)
  9246. (skip-chars-backward " \t\n\r")
  9247. (if (eq (char-before) ?*) (forward-char 1))
  9248. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9249. (beginning-of-line 0)
  9250. (org-indent-to-column indent)
  9251. (beginning-of-line 2)
  9252. (org-indent-to-column indent)
  9253. (beginning-of-line 0)
  9254. (if hiddenp
  9255. (save-excursion
  9256. (org-back-to-heading t)
  9257. (hide-entry))
  9258. (org-flag-drawer t))))
  9259. (defun org-set-property (property value)
  9260. "In the current entry, set PROPERTY to VALUE.
  9261. When called interactively, this will prompt for a property name, offering
  9262. completion on existing and default properties. And then it will prompt
  9263. for a value, offering competion either on allowed values (via an inherited
  9264. xxx_ALL property) or on existing values in other instances of this property
  9265. in the current file."
  9266. (interactive
  9267. (let* ((completion-ignore-case t)
  9268. (keys (org-buffer-property-keys nil t t))
  9269. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9270. (prop (if (member prop0 keys)
  9271. prop0
  9272. (or (cdr (assoc (downcase prop0)
  9273. (mapcar (lambda (x) (cons (downcase x) x))
  9274. keys)))
  9275. prop0)))
  9276. (cur (org-entry-get nil prop))
  9277. (allowed (org-property-get-allowed-values nil prop 'table))
  9278. (existing (mapcar 'list (org-property-values prop)))
  9279. (val (if allowed
  9280. (org-completing-read "Value: " allowed nil 'req-match)
  9281. (org-completing-read
  9282. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9283. (concat "[" cur "]") "")
  9284. ": ")
  9285. existing nil nil "" nil cur))))
  9286. (list prop (if (equal val "") cur val))))
  9287. (unless (equal (org-entry-get nil property) value)
  9288. (org-entry-put nil property value)))
  9289. (defun org-delete-property (property)
  9290. "In the current entry, delete PROPERTY."
  9291. (interactive
  9292. (let* ((completion-ignore-case t)
  9293. (prop (completing-read
  9294. "Property: " (org-entry-properties nil 'standard))))
  9295. (list prop)))
  9296. (message "Property %s %s" property
  9297. (if (org-entry-delete nil property)
  9298. "deleted"
  9299. "was not present in the entry")))
  9300. (defun org-delete-property-globally (property)
  9301. "Remove PROPERTY globally, from all entries."
  9302. (interactive
  9303. (let* ((completion-ignore-case t)
  9304. (prop (completing-read
  9305. "Globally remove property: "
  9306. (mapcar 'list (org-buffer-property-keys)))))
  9307. (list prop)))
  9308. (save-excursion
  9309. (save-restriction
  9310. (widen)
  9311. (goto-char (point-min))
  9312. (let ((cnt 0))
  9313. (while (re-search-forward
  9314. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9315. nil t)
  9316. (setq cnt (1+ cnt))
  9317. (replace-match ""))
  9318. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9319. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9320. (defun org-compute-property-at-point ()
  9321. "Compute the property at point.
  9322. This looks for an enclosing column format, extracts the operator and
  9323. then applies it to the proerty in the column format's scope."
  9324. (interactive)
  9325. (unless (org-at-property-p)
  9326. (error "Not at a property"))
  9327. (let ((prop (org-match-string-no-properties 2)))
  9328. (org-columns-get-format-and-top-level)
  9329. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9330. (error "No operator defined for property %s" prop))
  9331. (org-columns-compute prop)))
  9332. (defun org-property-get-allowed-values (pom property &optional table)
  9333. "Get allowed values for the property PROPERTY.
  9334. When TABLE is non-nil, return an alist that can directly be used for
  9335. completion."
  9336. (let (vals)
  9337. (cond
  9338. ((equal property "TODO")
  9339. (setq vals (org-with-point-at pom
  9340. (append org-todo-keywords-1 '("")))))
  9341. ((equal property "PRIORITY")
  9342. (let ((n org-lowest-priority))
  9343. (while (>= n org-highest-priority)
  9344. (push (char-to-string n) vals)
  9345. (setq n (1- n)))))
  9346. ((member property org-special-properties))
  9347. (t
  9348. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9349. (when (and vals (string-match "\\S-" vals))
  9350. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9351. (setq vals (mapcar (lambda (x)
  9352. (cond ((stringp x) x)
  9353. ((numberp x) (number-to-string x))
  9354. ((symbolp x) (symbol-name x))
  9355. (t "???")))
  9356. vals)))))
  9357. (if table (mapcar 'list vals) vals)))
  9358. (defun org-property-previous-allowed-value (&optional previous)
  9359. "Switch to the next allowed value for this property."
  9360. (interactive)
  9361. (org-property-next-allowed-value t))
  9362. (defun org-property-next-allowed-value (&optional previous)
  9363. "Switch to the next allowed value for this property."
  9364. (interactive)
  9365. (unless (org-at-property-p)
  9366. (error "Not at a property"))
  9367. (let* ((key (match-string 2))
  9368. (value (match-string 3))
  9369. (allowed (or (org-property-get-allowed-values (point) key)
  9370. (and (member value '("[ ]" "[-]" "[X]"))
  9371. '("[ ]" "[X]"))))
  9372. nval)
  9373. (unless allowed
  9374. (error "Allowed values for this property have not been defined"))
  9375. (if previous (setq allowed (reverse allowed)))
  9376. (if (member value allowed)
  9377. (setq nval (car (cdr (member value allowed)))))
  9378. (setq nval (or nval (car allowed)))
  9379. (if (equal nval value)
  9380. (error "Only one allowed value for this property"))
  9381. (org-at-property-p)
  9382. (replace-match (concat " :" key ": " nval) t t)
  9383. (org-indent-line-function)
  9384. (beginning-of-line 1)
  9385. (skip-chars-forward " \t")))
  9386. (defun org-find-entry-with-id (ident)
  9387. "Locate the entry that contains the ID property with exact value IDENT.
  9388. IDENT can be a string, a symbol or a number, this function will search for
  9389. the string representation of it.
  9390. Return the position where this entry starts, or nil if there is no such entry."
  9391. (let ((id (cond
  9392. ((stringp ident) ident)
  9393. ((symbol-name ident) (symbol-name ident))
  9394. ((numberp ident) (number-to-string ident))
  9395. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9396. (case-fold-search nil))
  9397. (save-excursion
  9398. (save-restriction
  9399. (widen)
  9400. (goto-char (point-min))
  9401. (when (re-search-forward
  9402. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9403. nil t)
  9404. (org-back-to-heading)
  9405. (point))))))
  9406. ;;;; Timestamps
  9407. (defvar org-last-changed-timestamp nil)
  9408. (defvar org-time-was-given) ; dynamically scoped parameter
  9409. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9410. (defvar org-ts-what) ; dynamically scoped parameter
  9411. (defun org-time-stamp (arg)
  9412. "Prompt for a date/time and insert a time stamp.
  9413. If the user specifies a time like HH:MM, or if this command is called
  9414. with a prefix argument, the time stamp will contain date and time.
  9415. Otherwise, only the date will be included. All parts of a date not
  9416. specified by the user will be filled in from the current date/time.
  9417. So if you press just return without typing anything, the time stamp
  9418. will represent the current date/time. If there is already a timestamp
  9419. at the cursor, it will be modified."
  9420. (interactive "P")
  9421. (let* ((ts nil)
  9422. (default-time
  9423. ;; Default time is either today, or, when entering a range,
  9424. ;; the range start.
  9425. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9426. (save-excursion
  9427. (re-search-backward
  9428. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9429. (- (point) 20) t)))
  9430. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9431. (current-time)))
  9432. (default-input (and ts (org-get-compact-tod ts)))
  9433. org-time-was-given org-end-time-was-given time)
  9434. (cond
  9435. ((and (org-at-timestamp-p)
  9436. (eq last-command 'org-time-stamp)
  9437. (eq this-command 'org-time-stamp))
  9438. (insert "--")
  9439. (setq time (let ((this-command this-command))
  9440. (org-read-date arg 'totime nil nil default-time default-input)))
  9441. (org-insert-time-stamp time (or org-time-was-given arg)))
  9442. ((org-at-timestamp-p)
  9443. (setq time (let ((this-command this-command))
  9444. (org-read-date arg 'totime nil nil default-time default-input)))
  9445. (when (org-at-timestamp-p) ; just to get the match data
  9446. (replace-match "")
  9447. (setq org-last-changed-timestamp
  9448. (org-insert-time-stamp
  9449. time (or org-time-was-given arg)
  9450. nil nil nil (list org-end-time-was-given))))
  9451. (message "Timestamp updated"))
  9452. (t
  9453. (setq time (let ((this-command this-command))
  9454. (org-read-date arg 'totime nil nil default-time default-input)))
  9455. (org-insert-time-stamp time (or org-time-was-given arg)
  9456. nil nil nil (list org-end-time-was-given))))))
  9457. ;; FIXME: can we use this for something else, like computing time differences?
  9458. (defun org-get-compact-tod (s)
  9459. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9460. (let* ((t1 (match-string 1 s))
  9461. (h1 (string-to-number (match-string 2 s)))
  9462. (m1 (string-to-number (match-string 3 s)))
  9463. (t2 (and (match-end 4) (match-string 5 s)))
  9464. (h2 (and t2 (string-to-number (match-string 6 s))))
  9465. (m2 (and t2 (string-to-number (match-string 7 s))))
  9466. dh dm)
  9467. (if (not t2)
  9468. t1
  9469. (setq dh (- h2 h1) dm (- m2 m1))
  9470. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9471. (concat t1 "+" (number-to-string dh)
  9472. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9473. (defun org-time-stamp-inactive (&optional arg)
  9474. "Insert an inactive time stamp.
  9475. An inactive time stamp is enclosed in square brackets instead of angle
  9476. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9477. does not link to the calendar and cannot be changed with the S-cursor keys.
  9478. So these are more for recording a certain time/date."
  9479. (interactive "P")
  9480. (let (org-time-was-given org-end-time-was-given time)
  9481. (setq time (org-read-date arg 'totime))
  9482. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9483. nil nil (list org-end-time-was-given))))
  9484. (defvar org-date-ovl (org-make-overlay 1 1))
  9485. (org-overlay-put org-date-ovl 'face 'org-warning)
  9486. (org-detach-overlay org-date-ovl)
  9487. (defvar org-ans1) ; dynamically scoped parameter
  9488. (defvar org-ans2) ; dynamically scoped parameter
  9489. (defvar org-plain-time-of-day-regexp) ; defined below
  9490. (defvar org-read-date-overlay nil)
  9491. (defvar org-dcst nil) ; dynamically scoped
  9492. (defun org-read-date (&optional with-time to-time from-string prompt
  9493. default-time default-input)
  9494. "Read a date, possibly a time, and make things smooth for the user.
  9495. The prompt will suggest to enter an ISO date, but you can also enter anything
  9496. which will at least partially be understood by `parse-time-string'.
  9497. Unrecognized parts of the date will default to the current day, month, year,
  9498. hour and minute. If this command is called to replace a timestamp at point,
  9499. of to enter the second timestamp of a range, the default time is taken from the
  9500. existing stamp. For example,
  9501. 3-2-5 --> 2003-02-05
  9502. feb 15 --> currentyear-02-15
  9503. sep 12 9 --> 2009-09-12
  9504. 12:45 --> today 12:45
  9505. 22 sept 0:34 --> currentyear-09-22 0:34
  9506. 12 --> currentyear-currentmonth-12
  9507. Fri --> nearest Friday (today or later)
  9508. etc.
  9509. Furthermore you can specify a relative date by giving, as the *first* thing
  9510. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9511. change in days weeks, months, years.
  9512. With a single plus or minus, the date is relative to today. With a double
  9513. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9514. +4d --> four days from today
  9515. +4 --> same as above
  9516. +2w --> two weeks from today
  9517. ++5 --> five days from default date
  9518. The function understands only English month and weekday abbreviations,
  9519. but this can be configured with the variables `parse-time-months' and
  9520. `parse-time-weekdays'.
  9521. While prompting, a calendar is popped up - you can also select the
  9522. date with the mouse (button 1). The calendar shows a period of three
  9523. months. To scroll it to other months, use the keys `>' and `<'.
  9524. If you don't like the calendar, turn it off with
  9525. \(setq org-read-date-popup-calendar nil)
  9526. With optional argument TO-TIME, the date will immediately be converted
  9527. to an internal time.
  9528. With an optional argument WITH-TIME, the prompt will suggest to also
  9529. insert a time. Note that when WITH-TIME is not set, you can still
  9530. enter a time, and this function will inform the calling routine about
  9531. this change. The calling routine may then choose to change the format
  9532. used to insert the time stamp into the buffer to include the time.
  9533. With optional argument FROM-STRING, read from this string instead from
  9534. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9535. the time/date that is used for everything that is not specified by the
  9536. user."
  9537. (require 'parse-time)
  9538. (let* ((org-time-stamp-rounding-minutes
  9539. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9540. (org-dcst org-display-custom-times)
  9541. (ct (org-current-time))
  9542. (def (or default-time ct))
  9543. (defdecode (decode-time def))
  9544. (dummy (progn
  9545. (when (< (nth 2 defdecode) org-extend-today-until)
  9546. (setcar (nthcdr 2 defdecode) -1)
  9547. (setcar (nthcdr 1 defdecode) 59)
  9548. (setq def (apply 'encode-time defdecode)
  9549. defdecode (decode-time def)))))
  9550. (calendar-move-hook nil)
  9551. (calendar-view-diary-initially-flag nil)
  9552. (view-diary-entries-initially nil)
  9553. (calendar-view-holidays-initially-flag nil)
  9554. (view-calendar-holidays-initially nil)
  9555. (timestr (format-time-string
  9556. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9557. (prompt (concat (if prompt (concat prompt " ") "")
  9558. (format "Date+time [%s]: " timestr)))
  9559. ans (org-ans0 "") org-ans1 org-ans2 final)
  9560. (cond
  9561. (from-string (setq ans from-string))
  9562. (org-read-date-popup-calendar
  9563. (save-excursion
  9564. (save-window-excursion
  9565. (calendar)
  9566. (calendar-forward-day (- (time-to-days def)
  9567. (calendar-absolute-from-gregorian
  9568. (calendar-current-date))))
  9569. (org-eval-in-calendar nil t)
  9570. (let* ((old-map (current-local-map))
  9571. (map (copy-keymap calendar-mode-map))
  9572. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9573. (org-defkey map (kbd "RET") 'org-calendar-select)
  9574. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9575. 'org-calendar-select-mouse)
  9576. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9577. 'org-calendar-select-mouse)
  9578. (org-defkey minibuffer-local-map [(meta shift left)]
  9579. (lambda () (interactive)
  9580. (org-eval-in-calendar '(calendar-backward-month 1))))
  9581. (org-defkey minibuffer-local-map [(meta shift right)]
  9582. (lambda () (interactive)
  9583. (org-eval-in-calendar '(calendar-forward-month 1))))
  9584. (org-defkey minibuffer-local-map [(meta shift up)]
  9585. (lambda () (interactive)
  9586. (org-eval-in-calendar '(calendar-backward-year 1))))
  9587. (org-defkey minibuffer-local-map [(meta shift down)]
  9588. (lambda () (interactive)
  9589. (org-eval-in-calendar '(calendar-forward-year 1))))
  9590. (org-defkey minibuffer-local-map [(shift up)]
  9591. (lambda () (interactive)
  9592. (org-eval-in-calendar '(calendar-backward-week 1))))
  9593. (org-defkey minibuffer-local-map [(shift down)]
  9594. (lambda () (interactive)
  9595. (org-eval-in-calendar '(calendar-forward-week 1))))
  9596. (org-defkey minibuffer-local-map [(shift left)]
  9597. (lambda () (interactive)
  9598. (org-eval-in-calendar '(calendar-backward-day 1))))
  9599. (org-defkey minibuffer-local-map [(shift right)]
  9600. (lambda () (interactive)
  9601. (org-eval-in-calendar '(calendar-forward-day 1))))
  9602. (org-defkey minibuffer-local-map ">"
  9603. (lambda () (interactive)
  9604. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9605. (org-defkey minibuffer-local-map "<"
  9606. (lambda () (interactive)
  9607. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9608. (unwind-protect
  9609. (progn
  9610. (use-local-map map)
  9611. (add-hook 'post-command-hook 'org-read-date-display)
  9612. (setq org-ans0 (read-string prompt default-input nil nil))
  9613. ;; org-ans0: from prompt
  9614. ;; org-ans1: from mouse click
  9615. ;; org-ans2: from calendar motion
  9616. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9617. (remove-hook 'post-command-hook 'org-read-date-display)
  9618. (use-local-map old-map)
  9619. (when org-read-date-overlay
  9620. (org-delete-overlay org-read-date-overlay)
  9621. (setq org-read-date-overlay nil)))))))
  9622. (t ; Naked prompt only
  9623. (unwind-protect
  9624. (setq ans (read-string prompt default-input nil timestr))
  9625. (when org-read-date-overlay
  9626. (org-delete-overlay org-read-date-overlay)
  9627. (setq org-read-date-overlay nil)))))
  9628. (setq final (org-read-date-analyze ans def defdecode))
  9629. (if to-time
  9630. (apply 'encode-time final)
  9631. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9632. (format "%04d-%02d-%02d %02d:%02d"
  9633. (nth 5 final) (nth 4 final) (nth 3 final)
  9634. (nth 2 final) (nth 1 final))
  9635. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9636. (defvar def)
  9637. (defvar defdecode)
  9638. (defvar with-time)
  9639. (defun org-read-date-display ()
  9640. "Display the currrent date prompt interpretation in the minibuffer."
  9641. (when org-read-date-display-live
  9642. (when org-read-date-overlay
  9643. (org-delete-overlay org-read-date-overlay))
  9644. (let ((p (point)))
  9645. (end-of-line 1)
  9646. (while (not (equal (buffer-substring
  9647. (max (point-min) (- (point) 4)) (point))
  9648. " "))
  9649. (insert " "))
  9650. (goto-char p))
  9651. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9652. " " (or org-ans1 org-ans2)))
  9653. (org-end-time-was-given nil)
  9654. (f (org-read-date-analyze ans def defdecode))
  9655. (fmts (if org-dcst
  9656. org-time-stamp-custom-formats
  9657. org-time-stamp-formats))
  9658. (fmt (if (or with-time
  9659. (and (boundp 'org-time-was-given) org-time-was-given))
  9660. (cdr fmts)
  9661. (car fmts)))
  9662. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9663. (when (and org-end-time-was-given
  9664. (string-match org-plain-time-of-day-regexp txt))
  9665. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9666. org-end-time-was-given
  9667. (substring txt (match-end 0)))))
  9668. (setq org-read-date-overlay
  9669. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  9670. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9671. (defun org-read-date-analyze (ans def defdecode)
  9672. "Analyze the combined answer of the date prompt."
  9673. ;; FIXME: cleanup and comment
  9674. (let (delta deltan deltaw deltadef year month day
  9675. hour minute second wday pm h2 m2 tl wday1
  9676. iso-year iso-weekday iso-week iso-year iso-date)
  9677. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9678. (setq ans "+0"))
  9679. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9680. (setq ans (replace-match "" t t ans)
  9681. deltan (car delta)
  9682. deltaw (nth 1 delta)
  9683. deltadef (nth 2 delta)))
  9684. ;; Check if there is an iso week date in there
  9685. ;; If yes, sore the info and ostpone interpreting it until the rest
  9686. ;; of the parsing is done
  9687. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9688. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9689. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9690. iso-week (string-to-number (match-string 2 ans)))
  9691. (setq ans (replace-match "" t t ans)))
  9692. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9693. (when (string-match
  9694. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9695. (setq year (if (match-end 2)
  9696. (string-to-number (match-string 2 ans))
  9697. (string-to-number (format-time-string "%Y")))
  9698. month (string-to-number (match-string 3 ans))
  9699. day (string-to-number (match-string 4 ans)))
  9700. (if (< year 100) (setq year (+ 2000 year)))
  9701. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9702. t nil ans)))
  9703. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9704. ;; If there is a time with am/pm, and *no* time without it, we convert
  9705. ;; so that matching will be successful.
  9706. (loop for i from 1 to 2 do ; twice, for end time as well
  9707. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9708. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9709. (setq hour (string-to-number (match-string 1 ans))
  9710. minute (if (match-end 3)
  9711. (string-to-number (match-string 3 ans))
  9712. 0)
  9713. pm (equal ?p
  9714. (string-to-char (downcase (match-string 4 ans)))))
  9715. (if (and (= hour 12) (not pm))
  9716. (setq hour 0)
  9717. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9718. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9719. t t ans))))
  9720. ;; Check if a time range is given as a duration
  9721. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9722. (setq hour (string-to-number (match-string 1 ans))
  9723. h2 (+ hour (string-to-number (match-string 3 ans)))
  9724. minute (string-to-number (match-string 2 ans))
  9725. m2 (+ minute (if (match-end 5) (string-to-number
  9726. (match-string 5 ans))0)))
  9727. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9728. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9729. t t ans)))
  9730. ;; Check if there is a time range
  9731. (when (boundp 'org-end-time-was-given)
  9732. (setq org-time-was-given nil)
  9733. (when (and (string-match org-plain-time-of-day-regexp ans)
  9734. (match-end 8))
  9735. (setq org-end-time-was-given (match-string 8 ans))
  9736. (setq ans (concat (substring ans 0 (match-beginning 7))
  9737. (substring ans (match-end 7))))))
  9738. (setq tl (parse-time-string ans)
  9739. day (or (nth 3 tl) (nth 3 defdecode))
  9740. month (or (nth 4 tl)
  9741. (if (and org-read-date-prefer-future
  9742. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9743. (1+ (nth 4 defdecode))
  9744. (nth 4 defdecode)))
  9745. year (or (nth 5 tl)
  9746. (if (and org-read-date-prefer-future
  9747. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9748. (1+ (nth 5 defdecode))
  9749. (nth 5 defdecode)))
  9750. hour (or (nth 2 tl) (nth 2 defdecode))
  9751. minute (or (nth 1 tl) (nth 1 defdecode))
  9752. second (or (nth 0 tl) 0)
  9753. wday (nth 6 tl))
  9754. ;; Special date definitions below
  9755. (cond
  9756. (iso-week
  9757. ;; There was an iso week
  9758. (setq year (or iso-year year)
  9759. day (or iso-weekday wday 1)
  9760. wday nil ; to make sure that the trigger below does not match
  9761. iso-date (calendar-gregorian-from-absolute
  9762. (calendar-absolute-from-iso
  9763. (list iso-week day year))))
  9764. ; FIXME: Should we also push ISO weeks into the future?
  9765. ; (when (and org-read-date-prefer-future
  9766. ; (not iso-year)
  9767. ; (< (calendar-absolute-from-gregorian iso-date)
  9768. ; (time-to-days (current-time))))
  9769. ; (setq year (1+ year)
  9770. ; iso-date (calendar-gregorian-from-absolute
  9771. ; (calendar-absolute-from-iso
  9772. ; (list iso-week day year)))))
  9773. (setq month (car iso-date)
  9774. year (nth 2 iso-date)
  9775. day (nth 1 iso-date)))
  9776. (deltan
  9777. (unless deltadef
  9778. (let ((now (decode-time (current-time))))
  9779. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9780. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9781. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9782. ((equal deltaw "m") (setq month (+ month deltan)))
  9783. ((equal deltaw "y") (setq year (+ year deltan)))))
  9784. ((and wday (not (nth 3 tl)))
  9785. ;; Weekday was given, but no day, so pick that day in the week
  9786. ;; on or after the derived date.
  9787. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9788. (unless (equal wday wday1)
  9789. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9790. (if (and (boundp 'org-time-was-given)
  9791. (nth 2 tl))
  9792. (setq org-time-was-given t))
  9793. (if (< year 100) (setq year (+ 2000 year)))
  9794. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9795. (list second minute hour day month year)))
  9796. (defvar parse-time-weekdays)
  9797. (defun org-read-date-get-relative (s today default)
  9798. "Check string S for special relative date string.
  9799. TODAY and DEFAULT are internal times, for today and for a default.
  9800. Return shift list (N what def-flag)
  9801. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9802. N is the number of WHATs to shift.
  9803. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9804. the DEFAULT date rather than TODAY."
  9805. (when (and
  9806. (string-match
  9807. (concat
  9808. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9809. "\\([0-9]+\\)?"
  9810. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9811. "\\([ \t]\\|$\\)") s)
  9812. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  9813. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  9814. (string-to-char (substring (match-string 1 s) -1))
  9815. ?+))
  9816. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9817. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9818. (what (if (match-end 3) (match-string 3 s) "d"))
  9819. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9820. (date (if rel default today))
  9821. (wday (nth 6 (decode-time date)))
  9822. delta)
  9823. (if wday1
  9824. (progn
  9825. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9826. (if (= dir ?-) (setq delta (- delta 7)))
  9827. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9828. (list delta "d" rel))
  9829. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9830. (defun org-eval-in-calendar (form &optional keepdate)
  9831. "Eval FORM in the calendar window and return to current window.
  9832. Also, store the cursor date in variable org-ans2."
  9833. (let ((sw (selected-window)))
  9834. (select-window (get-buffer-window "*Calendar*"))
  9835. (eval form)
  9836. (when (and (not keepdate) (calendar-cursor-to-date))
  9837. (let* ((date (calendar-cursor-to-date))
  9838. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9839. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9840. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9841. (select-window sw)))
  9842. ; ;; Update the prompt to show new default date
  9843. ; (save-excursion
  9844. ; (goto-char (point-min))
  9845. ; (when (and org-ans2
  9846. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  9847. ; (get-text-property (match-end 0) 'field))
  9848. ; (let ((inhibit-read-only t))
  9849. ; (replace-match (concat "[" org-ans2 "]") t t)
  9850. ; (add-text-properties (point-min) (1+ (match-end 0))
  9851. ; (text-properties-at (1+ (point-min)))))))))
  9852. (defun org-calendar-select ()
  9853. "Return to `org-read-date' with the date currently selected.
  9854. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9855. (interactive)
  9856. (when (calendar-cursor-to-date)
  9857. (let* ((date (calendar-cursor-to-date))
  9858. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9859. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9860. (if (active-minibuffer-window) (exit-minibuffer))))
  9861. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9862. "Insert a date stamp for the date given by the internal TIME.
  9863. WITH-HM means, use the stamp format that includes the time of the day.
  9864. INACTIVE means use square brackets instead of angular ones, so that the
  9865. stamp will not contribute to the agenda.
  9866. PRE and POST are optional strings to be inserted before and after the
  9867. stamp.
  9868. The command returns the inserted time stamp."
  9869. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9870. stamp)
  9871. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9872. (insert-before-markers (or pre ""))
  9873. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9874. (when (listp extra)
  9875. (setq extra (car extra))
  9876. (if (and (stringp extra)
  9877. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9878. (setq extra (format "-%02d:%02d"
  9879. (string-to-number (match-string 1 extra))
  9880. (string-to-number (match-string 2 extra))))
  9881. (setq extra nil)))
  9882. (when extra
  9883. (backward-char 1)
  9884. (insert-before-markers extra)
  9885. (forward-char 1))
  9886. (insert-before-markers (or post ""))
  9887. stamp))
  9888. (defun org-toggle-time-stamp-overlays ()
  9889. "Toggle the use of custom time stamp formats."
  9890. (interactive)
  9891. (setq org-display-custom-times (not org-display-custom-times))
  9892. (unless org-display-custom-times
  9893. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9894. (while (setq p (next-single-property-change p 'display))
  9895. (if (and (get-text-property p 'display)
  9896. (eq (get-text-property p 'face) 'org-date))
  9897. (remove-text-properties
  9898. p (setq p (next-single-property-change p 'display))
  9899. '(display t))))
  9900. (set-buffer-modified-p bmp)))
  9901. (if (featurep 'xemacs)
  9902. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9903. (org-restart-font-lock)
  9904. (setq org-table-may-need-update t)
  9905. (if org-display-custom-times
  9906. (message "Time stamps are overlayed with custom format")
  9907. (message "Time stamp overlays removed")))
  9908. (defun org-display-custom-time (beg end)
  9909. "Overlay modified time stamp format over timestamp between BEG and END."
  9910. (let* ((ts (buffer-substring beg end))
  9911. t1 w1 with-hm tf time str w2 (off 0))
  9912. (save-match-data
  9913. (setq t1 (org-parse-time-string ts t))
  9914. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9915. (setq off (- (match-end 0) (match-beginning 0)))))
  9916. (setq end (- end off))
  9917. (setq w1 (- end beg)
  9918. with-hm (and (nth 1 t1) (nth 2 t1))
  9919. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9920. time (org-fix-decoded-time t1)
  9921. str (org-add-props
  9922. (format-time-string
  9923. (substring tf 1 -1) (apply 'encode-time time))
  9924. nil 'mouse-face 'highlight)
  9925. w2 (length str))
  9926. (if (not (= w2 w1))
  9927. (add-text-properties (1+ beg) (+ 2 beg)
  9928. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9929. (if (featurep 'xemacs)
  9930. (progn
  9931. (put-text-property beg end 'invisible t)
  9932. (put-text-property beg end 'end-glyph (make-glyph str)))
  9933. (put-text-property beg end 'display str))))
  9934. (defun org-translate-time (string)
  9935. "Translate all timestamps in STRING to custom format.
  9936. But do this only if the variable `org-display-custom-times' is set."
  9937. (when org-display-custom-times
  9938. (save-match-data
  9939. (let* ((start 0)
  9940. (re org-ts-regexp-both)
  9941. t1 with-hm inactive tf time str beg end)
  9942. (while (setq start (string-match re string start))
  9943. (setq beg (match-beginning 0)
  9944. end (match-end 0)
  9945. t1 (save-match-data
  9946. (org-parse-time-string (substring string beg end) t))
  9947. with-hm (and (nth 1 t1) (nth 2 t1))
  9948. inactive (equal (substring string beg (1+ beg)) "[")
  9949. tf (funcall (if with-hm 'cdr 'car)
  9950. org-time-stamp-custom-formats)
  9951. time (org-fix-decoded-time t1)
  9952. str (format-time-string
  9953. (concat
  9954. (if inactive "[" "<") (substring tf 1 -1)
  9955. (if inactive "]" ">"))
  9956. (apply 'encode-time time))
  9957. string (replace-match str t t string)
  9958. start (+ start (length str)))))))
  9959. string)
  9960. (defun org-fix-decoded-time (time)
  9961. "Set 0 instead of nil for the first 6 elements of time.
  9962. Don't touch the rest."
  9963. (let ((n 0))
  9964. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9965. (defun org-days-to-time (timestamp-string)
  9966. "Difference between TIMESTAMP-STRING and now in days."
  9967. (- (time-to-days (org-time-string-to-time timestamp-string))
  9968. (time-to-days (current-time))))
  9969. (defun org-deadline-close (timestamp-string &optional ndays)
  9970. "Is the time in TIMESTAMP-STRING close to the current date?"
  9971. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9972. (and (< (org-days-to-time timestamp-string) ndays)
  9973. (not (org-entry-is-done-p))))
  9974. (defun org-get-wdays (ts)
  9975. "Get the deadline lead time appropriate for timestring TS."
  9976. (cond
  9977. ((<= org-deadline-warning-days 0)
  9978. ;; 0 or negative, enforce this value no matter what
  9979. (- org-deadline-warning-days))
  9980. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9981. ;; lead time is specified.
  9982. (floor (* (string-to-number (match-string 1 ts))
  9983. (cdr (assoc (match-string 2 ts)
  9984. '(("d" . 1) ("w" . 7)
  9985. ("m" . 30.4) ("y" . 365.25)))))))
  9986. ;; go for the default.
  9987. (t org-deadline-warning-days)))
  9988. (defun org-calendar-select-mouse (ev)
  9989. "Return to `org-read-date' with the date currently selected.
  9990. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9991. (interactive "e")
  9992. (mouse-set-point ev)
  9993. (when (calendar-cursor-to-date)
  9994. (let* ((date (calendar-cursor-to-date))
  9995. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9996. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9997. (if (active-minibuffer-window) (exit-minibuffer))))
  9998. (defun org-check-deadlines (ndays)
  9999. "Check if there are any deadlines due or past due.
  10000. A deadline is considered due if it happens within `org-deadline-warning-days'
  10001. days from today's date. If the deadline appears in an entry marked DONE,
  10002. it is not shown. The prefix arg NDAYS can be used to test that many
  10003. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10004. (interactive "P")
  10005. (let* ((org-warn-days
  10006. (cond
  10007. ((equal ndays '(4)) 100000)
  10008. (ndays (prefix-numeric-value ndays))
  10009. (t (abs org-deadline-warning-days))))
  10010. (case-fold-search nil)
  10011. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10012. (callback
  10013. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10014. (message "%d deadlines past-due or due within %d days"
  10015. (org-occur regexp nil callback)
  10016. org-warn-days)))
  10017. (defun org-check-before-date (date)
  10018. "Check if there are deadlines or scheduled entries before DATE."
  10019. (interactive (list (org-read-date)))
  10020. (let ((case-fold-search nil)
  10021. (regexp (concat "\\<\\(" org-deadline-string
  10022. "\\|" org-scheduled-string
  10023. "\\) *<\\([^>]+\\)>"))
  10024. (callback
  10025. (lambda () (time-less-p
  10026. (org-time-string-to-time (match-string 2))
  10027. (org-time-string-to-time date)))))
  10028. (message "%d entries before %s"
  10029. (org-occur regexp nil callback) date)))
  10030. (defun org-evaluate-time-range (&optional to-buffer)
  10031. "Evaluate a time range by computing the difference between start and end.
  10032. Normally the result is just printed in the echo area, but with prefix arg
  10033. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10034. If the time range is actually in a table, the result is inserted into the
  10035. next column.
  10036. For time difference computation, a year is assumed to be exactly 365
  10037. days in order to avoid rounding problems."
  10038. (interactive "P")
  10039. (or
  10040. (org-clock-update-time-maybe)
  10041. (save-excursion
  10042. (unless (org-at-date-range-p t)
  10043. (goto-char (point-at-bol))
  10044. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10045. (if (not (org-at-date-range-p t))
  10046. (error "Not at a time-stamp range, and none found in current line")))
  10047. (let* ((ts1 (match-string 1))
  10048. (ts2 (match-string 2))
  10049. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10050. (match-end (match-end 0))
  10051. (time1 (org-time-string-to-time ts1))
  10052. (time2 (org-time-string-to-time ts2))
  10053. (t1 (time-to-seconds time1))
  10054. (t2 (time-to-seconds time2))
  10055. (diff (abs (- t2 t1)))
  10056. (negative (< (- t2 t1) 0))
  10057. ;; (ys (floor (* 365 24 60 60)))
  10058. (ds (* 24 60 60))
  10059. (hs (* 60 60))
  10060. (fy "%dy %dd %02d:%02d")
  10061. (fy1 "%dy %dd")
  10062. (fd "%dd %02d:%02d")
  10063. (fd1 "%dd")
  10064. (fh "%02d:%02d")
  10065. y d h m align)
  10066. (if havetime
  10067. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10068. y 0
  10069. d (floor (/ diff ds)) diff (mod diff ds)
  10070. h (floor (/ diff hs)) diff (mod diff hs)
  10071. m (floor (/ diff 60)))
  10072. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10073. y 0
  10074. d (floor (+ (/ diff ds) 0.5))
  10075. h 0 m 0))
  10076. (if (not to-buffer)
  10077. (message "%s" (org-make-tdiff-string y d h m))
  10078. (if (org-at-table-p)
  10079. (progn
  10080. (goto-char match-end)
  10081. (setq align t)
  10082. (and (looking-at " *|") (goto-char (match-end 0))))
  10083. (goto-char match-end))
  10084. (if (looking-at
  10085. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10086. (replace-match ""))
  10087. (if negative (insert " -"))
  10088. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10089. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10090. (insert " " (format fh h m))))
  10091. (if align (org-table-align))
  10092. (message "Time difference inserted")))))
  10093. (defun org-make-tdiff-string (y d h m)
  10094. (let ((fmt "")
  10095. (l nil))
  10096. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10097. l (push y l)))
  10098. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10099. l (push d l)))
  10100. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10101. l (push h l)))
  10102. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10103. l (push m l)))
  10104. (apply 'format fmt (nreverse l))))
  10105. (defun org-time-string-to-time (s)
  10106. (apply 'encode-time (org-parse-time-string s)))
  10107. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10108. "Convert a time stamp to an absolute day number.
  10109. If there is a specifyer for a cyclic time stamp, get the closest date to
  10110. DAYNR.
  10111. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  10112. (cond
  10113. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10114. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10115. daynr
  10116. (+ daynr 1000)))
  10117. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10118. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10119. (time-to-days (current-time))) (match-string 0 s)
  10120. prefer show-all))
  10121. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10122. (defun org-days-to-iso-week (days)
  10123. "Return the iso week number."
  10124. (require 'cal-iso)
  10125. (car (calendar-iso-from-absolute days)))
  10126. (defun org-small-year-to-year (year)
  10127. "Convert 2-digit years into 4-digit years.
  10128. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10129. The year 2000 cannot be abbreviated. Any year lager than 99
  10130. is retrned unchanged."
  10131. (if (< year 38)
  10132. (setq year (+ 2000 year))
  10133. (if (< year 100)
  10134. (setq year (+ 1900 year))))
  10135. year)
  10136. (defun org-time-from-absolute (d)
  10137. "Return the time corresponding to date D.
  10138. D may be an absolute day number, or a calendar-type list (month day year)."
  10139. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10140. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10141. (defun org-calendar-holiday ()
  10142. "List of holidays, for Diary display in Org-mode."
  10143. (require 'holidays)
  10144. (let ((hl (funcall
  10145. (if (fboundp 'calendar-check-holidays)
  10146. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10147. (if hl (mapconcat 'identity hl "; "))))
  10148. (defun org-diary-sexp-entry (sexp entry date)
  10149. "Process a SEXP diary ENTRY for DATE."
  10150. (require 'diary-lib)
  10151. (let ((result (if calendar-debug-sexp
  10152. (let ((stack-trace-on-error t))
  10153. (eval (car (read-from-string sexp))))
  10154. (condition-case nil
  10155. (eval (car (read-from-string sexp)))
  10156. (error
  10157. (beep)
  10158. (message "Bad sexp at line %d in %s: %s"
  10159. (org-current-line)
  10160. (buffer-file-name) sexp)
  10161. (sleep-for 2))))))
  10162. (cond ((stringp result) result)
  10163. ((and (consp result)
  10164. (stringp (cdr result))) (cdr result))
  10165. (result entry)
  10166. (t nil))))
  10167. (defun org-diary-to-ical-string (frombuf)
  10168. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10169. This uses the icalendar.el library."
  10170. (let* ((tmpdir (if (featurep 'xemacs)
  10171. (temp-directory)
  10172. temporary-file-directory))
  10173. (tmpfile (make-temp-name
  10174. (expand-file-name "orgics" tmpdir)))
  10175. buf rtn b e)
  10176. (save-excursion
  10177. (set-buffer frombuf)
  10178. (icalendar-export-region (point-min) (point-max) tmpfile)
  10179. (setq buf (find-buffer-visiting tmpfile))
  10180. (set-buffer buf)
  10181. (goto-char (point-min))
  10182. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10183. (setq b (match-beginning 0)))
  10184. (goto-char (point-max))
  10185. (if (re-search-backward "^END:VEVENT" nil t)
  10186. (setq e (match-end 0)))
  10187. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10188. (kill-buffer buf)
  10189. (kill-buffer frombuf)
  10190. (delete-file tmpfile)
  10191. rtn))
  10192. (defun org-closest-date (start current change prefer show-all)
  10193. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10194. When PREFER is `past' return a date that is either CURRENT or past.
  10195. When PREFER is `future', return a date that is either CURRENT or future.
  10196. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10197. ;; Make the proper lists from the dates
  10198. (catch 'exit
  10199. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10200. dn dw sday cday n1 n2
  10201. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10202. (setq start (org-date-to-gregorian start)
  10203. current (org-date-to-gregorian
  10204. (if show-all
  10205. current
  10206. (time-to-days (current-time))))
  10207. sday (calendar-absolute-from-gregorian start)
  10208. cday (calendar-absolute-from-gregorian current))
  10209. (if (<= cday sday) (throw 'exit sday))
  10210. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10211. (setq dn (string-to-number (match-string 1 change))
  10212. dw (cdr (assoc (match-string 2 change) a1)))
  10213. (error "Invalid change specifyer: %s" change))
  10214. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10215. (cond
  10216. ((eq dw 'day)
  10217. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10218. n2 (+ n1 dn)))
  10219. ((eq dw 'year)
  10220. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10221. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10222. (setq date1 (list m d y1)
  10223. n1 (calendar-absolute-from-gregorian date1)
  10224. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10225. n2 (calendar-absolute-from-gregorian date2)))
  10226. ((eq dw 'month)
  10227. ;; approx number of month between the tow dates
  10228. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10229. ;; How often does dn fit in there?
  10230. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10231. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10232. m (+ m nm)
  10233. ny (floor (/ m 12))
  10234. y (+ y ny)
  10235. m (- m (* ny 12)))
  10236. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10237. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10238. (setq m2 (+ m dn) y2 y)
  10239. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10240. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10241. (while (< n2 cday)
  10242. (setq n1 n2 m m2 y y2)
  10243. (setq m2 (+ m dn) y2 y)
  10244. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10245. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10246. (if show-all
  10247. (cond
  10248. ((eq prefer 'past) n1)
  10249. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10250. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10251. (cond
  10252. ((eq prefer 'past) n1)
  10253. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10254. (t (if (= cday n1) n1 n2)))))))
  10255. (defun org-date-to-gregorian (date)
  10256. "Turn any specification of DATE into a gregorian date for the calendar."
  10257. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10258. ((and (listp date) (= (length date) 3)) date)
  10259. ((stringp date)
  10260. (setq date (org-parse-time-string date))
  10261. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10262. ((listp date)
  10263. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10264. (defun org-parse-time-string (s &optional nodefault)
  10265. "Parse the standard Org-mode time string.
  10266. This should be a lot faster than the normal `parse-time-string'.
  10267. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10268. hour and minute fields will be nil if not given."
  10269. (if (string-match org-ts-regexp0 s)
  10270. (list 0
  10271. (if (or (match-beginning 8) (not nodefault))
  10272. (string-to-number (or (match-string 8 s) "0")))
  10273. (if (or (match-beginning 7) (not nodefault))
  10274. (string-to-number (or (match-string 7 s) "0")))
  10275. (string-to-number (match-string 4 s))
  10276. (string-to-number (match-string 3 s))
  10277. (string-to-number (match-string 2 s))
  10278. nil nil nil)
  10279. (make-list 9 0)))
  10280. (defun org-timestamp-up (&optional arg)
  10281. "Increase the date item at the cursor by one.
  10282. If the cursor is on the year, change the year. If it is on the month or
  10283. the day, change that.
  10284. With prefix ARG, change by that many units."
  10285. (interactive "p")
  10286. (org-timestamp-change (prefix-numeric-value arg)))
  10287. (defun org-timestamp-down (&optional arg)
  10288. "Decrease the date item at the cursor by one.
  10289. If the cursor is on the year, change the year. If it is on the month or
  10290. the day, change that.
  10291. With prefix ARG, change by that many units."
  10292. (interactive "p")
  10293. (org-timestamp-change (- (prefix-numeric-value arg))))
  10294. (defun org-timestamp-up-day (&optional arg)
  10295. "Increase the date in the time stamp by one day.
  10296. With prefix ARG, change that many days."
  10297. (interactive "p")
  10298. (if (and (not (org-at-timestamp-p t))
  10299. (org-on-heading-p))
  10300. (org-todo 'up)
  10301. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10302. (defun org-timestamp-down-day (&optional arg)
  10303. "Decrease the date in the time stamp by one day.
  10304. With prefix ARG, change that many days."
  10305. (interactive "p")
  10306. (if (and (not (org-at-timestamp-p t))
  10307. (org-on-heading-p))
  10308. (org-todo 'down)
  10309. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10310. (defun org-at-timestamp-p (&optional inactive-ok)
  10311. "Determine if the cursor is in or at a timestamp."
  10312. (interactive)
  10313. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10314. (pos (point))
  10315. (ans (or (looking-at tsr)
  10316. (save-excursion
  10317. (skip-chars-backward "^[<\n\r\t")
  10318. (if (> (point) (point-min)) (backward-char 1))
  10319. (and (looking-at tsr)
  10320. (> (- (match-end 0) pos) -1))))))
  10321. (and ans
  10322. (boundp 'org-ts-what)
  10323. (setq org-ts-what
  10324. (cond
  10325. ((= pos (match-beginning 0)) 'bracket)
  10326. ((= pos (1- (match-end 0))) 'bracket)
  10327. ((org-pos-in-match-range pos 2) 'year)
  10328. ((org-pos-in-match-range pos 3) 'month)
  10329. ((org-pos-in-match-range pos 7) 'hour)
  10330. ((org-pos-in-match-range pos 8) 'minute)
  10331. ((or (org-pos-in-match-range pos 4)
  10332. (org-pos-in-match-range pos 5)) 'day)
  10333. ((and (> pos (or (match-end 8) (match-end 5)))
  10334. (< pos (match-end 0)))
  10335. (- pos (or (match-end 8) (match-end 5))))
  10336. (t 'day))))
  10337. ans))
  10338. (defun org-toggle-timestamp-type ()
  10339. "Toggle the type (<active> or [inactive]) of a time stamp."
  10340. (interactive)
  10341. (when (org-at-timestamp-p t)
  10342. (save-excursion
  10343. (goto-char (match-beginning 0))
  10344. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10345. (goto-char (1- (match-end 0)))
  10346. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10347. (message "Timestamp is now %sactive"
  10348. (if (equal (char-before) ?>) "in" ""))))
  10349. (defun org-timestamp-change (n &optional what)
  10350. "Change the date in the time stamp at point.
  10351. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10352. `year', `minute', `second'. If WHAT is not given, the cursor position
  10353. in the timestamp determines what will be changed."
  10354. (let ((pos (point))
  10355. with-hm inactive
  10356. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10357. org-ts-what
  10358. extra rem
  10359. ts time time0)
  10360. (if (not (org-at-timestamp-p t))
  10361. (error "Not at a timestamp"))
  10362. (if (and (not what) (eq org-ts-what 'bracket))
  10363. (org-toggle-timestamp-type)
  10364. (if (and (not what) (not (eq org-ts-what 'day))
  10365. org-display-custom-times
  10366. (get-text-property (point) 'display)
  10367. (not (get-text-property (1- (point)) 'display)))
  10368. (setq org-ts-what 'day))
  10369. (setq org-ts-what (or what org-ts-what)
  10370. inactive (= (char-after (match-beginning 0)) ?\[)
  10371. ts (match-string 0))
  10372. (replace-match "")
  10373. (if (string-match
  10374. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10375. ts)
  10376. (setq extra (match-string 1 ts)))
  10377. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10378. (setq with-hm t))
  10379. (setq time0 (org-parse-time-string ts))
  10380. (when (and (eq org-ts-what 'minute)
  10381. (eq current-prefix-arg nil))
  10382. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10383. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10384. (setcar (cdr time0) (+ (nth 1 time0)
  10385. (if (> n 0) (- rem) (- dm rem))))))
  10386. (setq time
  10387. (encode-time (or (car time0) 0)
  10388. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10389. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10390. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10391. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10392. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10393. (nthcdr 6 time0)))
  10394. (when (integerp org-ts-what)
  10395. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10396. (if (eq what 'calendar)
  10397. (let ((cal-date (org-get-date-from-calendar)))
  10398. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10399. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10400. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10401. (setcar time0 (or (car time0) 0))
  10402. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10403. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10404. (setq time (apply 'encode-time time0))))
  10405. (setq org-last-changed-timestamp
  10406. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10407. (org-clock-update-time-maybe)
  10408. (goto-char pos)
  10409. ;; Try to recenter the calendar window, if any
  10410. (if (and org-calendar-follow-timestamp-change
  10411. (get-buffer-window "*Calendar*" t)
  10412. (memq org-ts-what '(day month year)))
  10413. (org-recenter-calendar (time-to-days time))))))
  10414. (defun org-modify-ts-extra (s pos n dm)
  10415. "Change the different parts of the lead-time and repeat fields in timestamp."
  10416. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10417. ng h m new rem)
  10418. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10419. (cond
  10420. ((or (org-pos-in-match-range pos 2)
  10421. (org-pos-in-match-range pos 3))
  10422. (setq m (string-to-number (match-string 3 s))
  10423. h (string-to-number (match-string 2 s)))
  10424. (if (org-pos-in-match-range pos 2)
  10425. (setq h (+ h n))
  10426. (setq n (* dm (org-no-warnings (signum n))))
  10427. (when (not (= 0 (setq rem (% m dm))))
  10428. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10429. (setq m (+ m n)))
  10430. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10431. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10432. (setq h (min 24 (max 0 h)))
  10433. (setq ng 1 new (format "-%02d:%02d" h m)))
  10434. ((org-pos-in-match-range pos 6)
  10435. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10436. ((org-pos-in-match-range pos 5)
  10437. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10438. ((org-pos-in-match-range pos 9)
  10439. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10440. ((org-pos-in-match-range pos 8)
  10441. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10442. (when ng
  10443. (setq s (concat
  10444. (substring s 0 (match-beginning ng))
  10445. new
  10446. (substring s (match-end ng))))))
  10447. s))
  10448. (defun org-recenter-calendar (date)
  10449. "If the calendar is visible, recenter it to DATE."
  10450. (let* ((win (selected-window))
  10451. (cwin (get-buffer-window "*Calendar*" t))
  10452. (calendar-move-hook nil))
  10453. (when cwin
  10454. (select-window cwin)
  10455. (calendar-goto-date (if (listp date) date
  10456. (calendar-gregorian-from-absolute date)))
  10457. (select-window win))))
  10458. (defun org-goto-calendar (&optional arg)
  10459. "Go to the Emacs calendar at the current date.
  10460. If there is a time stamp in the current line, go to that date.
  10461. A prefix ARG can be used to force the current date."
  10462. (interactive "P")
  10463. (let ((tsr org-ts-regexp) diff
  10464. (calendar-move-hook nil)
  10465. (calendar-view-holidays-initially-flag nil)
  10466. (view-calendar-holidays-initially nil)
  10467. (calendar-view-diary-initially-flag nil)
  10468. (view-diary-entries-initially nil))
  10469. (if (or (org-at-timestamp-p)
  10470. (save-excursion
  10471. (beginning-of-line 1)
  10472. (looking-at (concat ".*" tsr))))
  10473. (let ((d1 (time-to-days (current-time)))
  10474. (d2 (time-to-days
  10475. (org-time-string-to-time (match-string 1)))))
  10476. (setq diff (- d2 d1))))
  10477. (calendar)
  10478. (calendar-goto-today)
  10479. (if (and diff (not arg)) (calendar-forward-day diff))))
  10480. (defun org-get-date-from-calendar ()
  10481. "Return a list (month day year) of date at point in calendar."
  10482. (with-current-buffer "*Calendar*"
  10483. (save-match-data
  10484. (calendar-cursor-to-date))))
  10485. (defun org-date-from-calendar ()
  10486. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10487. If there is already a time stamp at the cursor position, update it."
  10488. (interactive)
  10489. (if (org-at-timestamp-p t)
  10490. (org-timestamp-change 0 'calendar)
  10491. (let ((cal-date (org-get-date-from-calendar)))
  10492. (org-insert-time-stamp
  10493. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10494. (defun org-minutes-to-hh:mm-string (m)
  10495. "Compute H:MM from a number of minutes."
  10496. (let ((h (/ m 60)))
  10497. (setq m (- m (* 60 h)))
  10498. (format org-time-clocksum-format h m)))
  10499. (defun org-hh:mm-string-to-minutes (s)
  10500. "Convert a string H:MM to a number of minutes."
  10501. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10502. (+ (* (string-to-number (match-string 1 s)) 60)
  10503. (string-to-number (match-string 2 s)))
  10504. 0))
  10505. ;;;; Agenda files
  10506. ;;;###autoload
  10507. (defun org-iswitchb (&optional arg)
  10508. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10509. With a prefix argument, restrict available to files.
  10510. With two prefix arguments, restrict available buffers to agenda files.
  10511. Due to some yet unresolved reason, global function
  10512. `iswitchb-mode' needs to be active for this function to work."
  10513. (interactive "P")
  10514. (require 'iswitchb)
  10515. (let ((enabled iswitchb-mode) blist)
  10516. (or enabled (iswitchb-mode 1))
  10517. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10518. ((equal arg '(16)) (org-buffer-list 'agenda))
  10519. (t (org-buffer-list))))
  10520. (unwind-protect
  10521. (let ((iswitchb-make-buflist-hook
  10522. (lambda ()
  10523. (setq iswitchb-temp-buflist
  10524. (mapcar 'buffer-name blist)))))
  10525. (switch-to-buffer
  10526. (iswitchb-read-buffer
  10527. "Switch-to: " nil t))
  10528. (or enabled (iswitchb-mode -1))))))
  10529. (defun org-buffer-list (&optional predicate tmp)
  10530. "Return a list of Org buffers.
  10531. PREDICATE can be either 'export, 'files or 'agenda.
  10532. 'export restrict the list to Export buffers.
  10533. 'files restrict the list to buffers visiting Org files.
  10534. 'agenda restrict the list to buffers visiting agenda files.
  10535. If TMP is non-nil, don't include temporary buffers."
  10536. (let (filter blist)
  10537. (setq filter
  10538. (cond ((eq predicate 'files) "\.org$")
  10539. ((eq predicate 'export) "\*Org .*Export")
  10540. (t "\*Org \\|\.org$")))
  10541. (setq blist
  10542. (mapcar
  10543. (lambda(b)
  10544. (let ((bname (buffer-name b))
  10545. (bfile (buffer-file-name b)))
  10546. (if (and (string-match filter bname)
  10547. (if (eq predicate 'agenda)
  10548. (member bfile
  10549. (mapcar (lambda(f) (file-truename f))
  10550. org-agenda-files)) t)
  10551. (if tmp (not (string-match "tmp" bname)) t)) b)))
  10552. (buffer-list)))
  10553. (delete nil blist)))
  10554. (defun org-agenda-files (&optional unrestricted ext)
  10555. "Get the list of agenda files.
  10556. Optional UNRESTRICTED means return the full list even if a restriction
  10557. is currently in place.
  10558. When EXT is non-nil, try to add all files that are created by adding EXT
  10559. to the file nemes. Basically, this is a way to add the archive files
  10560. to the list, by setting EXT to \"_archive\" If EXT is non-nil, but not
  10561. a string, \"_archive\" will be used."
  10562. (let ((files
  10563. (cond
  10564. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10565. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10566. ((listp org-agenda-files) org-agenda-files)
  10567. (t (error "Invalid value of `org-agenda-files'")))))
  10568. (setq files (apply 'append
  10569. (mapcar (lambda (f)
  10570. (if (file-directory-p f)
  10571. (directory-files
  10572. f t org-agenda-file-regexp)
  10573. (list f)))
  10574. files)))
  10575. (when org-agenda-skip-unavailable-files
  10576. (setq files (delq nil
  10577. (mapcar (function
  10578. (lambda (file)
  10579. (and (file-readable-p file) file)))
  10580. files))))
  10581. (when ext
  10582. (setq ext (if (and (stringp ext) (string-match "\\S-" ext))
  10583. ext "_archive"))
  10584. (setq files (apply 'append
  10585. (mapcar
  10586. (lambda (f)
  10587. (if (file-exists-p (concat f ext))
  10588. (list f (concat f ext))
  10589. (list f)))
  10590. files))))
  10591. files))
  10592. (defun org-edit-agenda-file-list ()
  10593. "Edit the list of agenda files.
  10594. Depending on setup, this either uses customize to edit the variable
  10595. `org-agenda-files', or it visits the file that is holding the list. In the
  10596. latter case, the buffer is set up in a way that saving it automatically kills
  10597. the buffer and restores the previous window configuration."
  10598. (interactive)
  10599. (if (stringp org-agenda-files)
  10600. (let ((cw (current-window-configuration)))
  10601. (find-file org-agenda-files)
  10602. (org-set-local 'org-window-configuration cw)
  10603. (org-add-hook 'after-save-hook
  10604. (lambda ()
  10605. (set-window-configuration
  10606. (prog1 org-window-configuration
  10607. (kill-buffer (current-buffer))))
  10608. (org-install-agenda-files-menu)
  10609. (message "New agenda file list installed"))
  10610. nil 'local)
  10611. (message "%s" (substitute-command-keys
  10612. "Edit list and finish with \\[save-buffer]")))
  10613. (customize-variable 'org-agenda-files)))
  10614. (defun org-store-new-agenda-file-list (list)
  10615. "Set new value for the agenda file list and save it correcly."
  10616. (if (stringp org-agenda-files)
  10617. (let ((f org-agenda-files) b)
  10618. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10619. (with-temp-file f
  10620. (insert (mapconcat 'identity list "\n") "\n")))
  10621. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10622. (setq org-agenda-files list)
  10623. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10624. (defun org-read-agenda-file-list ()
  10625. "Read the list of agenda files from a file."
  10626. (when (file-directory-p org-agenda-files)
  10627. (error "`org-agenda-files' cannot be a single directory"))
  10628. (when (stringp org-agenda-files)
  10629. (with-temp-buffer
  10630. (insert-file-contents org-agenda-files)
  10631. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10632. ;;;###autoload
  10633. (defun org-cycle-agenda-files ()
  10634. "Cycle through the files in `org-agenda-files'.
  10635. If the current buffer visits an agenda file, find the next one in the list.
  10636. If the current buffer does not, find the first agenda file."
  10637. (interactive)
  10638. (let* ((fs (org-agenda-files t))
  10639. (files (append fs (list (car fs))))
  10640. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10641. file)
  10642. (unless files (error "No agenda files"))
  10643. (catch 'exit
  10644. (while (setq file (pop files))
  10645. (if (equal (file-truename file) tcf)
  10646. (when (car files)
  10647. (find-file (car files))
  10648. (throw 'exit t))))
  10649. (find-file (car fs)))
  10650. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10651. (defun org-agenda-file-to-front (&optional to-end)
  10652. "Move/add the current file to the top of the agenda file list.
  10653. If the file is not present in the list, it is added to the front. If it is
  10654. present, it is moved there. With optional argument TO-END, add/move to the
  10655. end of the list."
  10656. (interactive "P")
  10657. (let ((org-agenda-skip-unavailable-files nil)
  10658. (file-alist (mapcar (lambda (x)
  10659. (cons (file-truename x) x))
  10660. (org-agenda-files t)))
  10661. (ctf (file-truename buffer-file-name))
  10662. x had)
  10663. (setq x (assoc ctf file-alist) had x)
  10664. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10665. (if to-end
  10666. (setq file-alist (append (delq x file-alist) (list x)))
  10667. (setq file-alist (cons x (delq x file-alist))))
  10668. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10669. (org-install-agenda-files-menu)
  10670. (message "File %s to %s of agenda file list"
  10671. (if had "moved" "added") (if to-end "end" "front"))))
  10672. (defun org-remove-file (&optional file)
  10673. "Remove current file from the list of files in variable `org-agenda-files'.
  10674. These are the files which are being checked for agenda entries.
  10675. Optional argument FILE means, use this file instead of the current."
  10676. (interactive)
  10677. (let* ((org-agenda-skip-unavailable-files nil)
  10678. (file (or file buffer-file-name))
  10679. (true-file (file-truename file))
  10680. (afile (abbreviate-file-name file))
  10681. (files (delq nil (mapcar
  10682. (lambda (x)
  10683. (if (equal true-file
  10684. (file-truename x))
  10685. nil x))
  10686. (org-agenda-files t)))))
  10687. (if (not (= (length files) (length (org-agenda-files t))))
  10688. (progn
  10689. (org-store-new-agenda-file-list files)
  10690. (org-install-agenda-files-menu)
  10691. (message "Removed file: %s" afile))
  10692. (message "File was not in list: %s (not removed)" afile))))
  10693. (defun org-file-menu-entry (file)
  10694. (vector file (list 'find-file file) t))
  10695. (defun org-check-agenda-file (file)
  10696. "Make sure FILE exists. If not, ask user what to do."
  10697. (when (not (file-exists-p file))
  10698. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10699. (abbreviate-file-name file))
  10700. (let ((r (downcase (read-char-exclusive))))
  10701. (cond
  10702. ((equal r ?r)
  10703. (org-remove-file file)
  10704. (throw 'nextfile t))
  10705. (t (error "Abort"))))))
  10706. (defun org-get-agenda-file-buffer (file)
  10707. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10708. it to the list of buffers which might be released later."
  10709. (let ((buf (org-find-base-buffer-visiting file)))
  10710. (if buf
  10711. buf ; just return it
  10712. ;; Make a new buffer and remember it
  10713. (setq buf (find-file-noselect file))
  10714. (if buf (push buf org-agenda-new-buffers))
  10715. buf)))
  10716. (defun org-release-buffers (blist)
  10717. "Release all buffers in list, asking the user for confirmation when needed.
  10718. When a buffer is unmodified, it is just killed. When modified, it is saved
  10719. \(if the user agrees) and then killed."
  10720. (let (buf file)
  10721. (while (setq buf (pop blist))
  10722. (setq file (buffer-file-name buf))
  10723. (when (and (buffer-modified-p buf)
  10724. file
  10725. (y-or-n-p (format "Save file %s? " file)))
  10726. (with-current-buffer buf (save-buffer)))
  10727. (kill-buffer buf))))
  10728. (defun org-prepare-agenda-buffers (files)
  10729. "Create buffers for all agenda files, protect archived trees and comments."
  10730. (interactive)
  10731. (let ((pa '(:org-archived t))
  10732. (pc '(:org-comment t))
  10733. (pall '(:org-archived t :org-comment t))
  10734. (inhibit-read-only t)
  10735. (rea (concat ":" org-archive-tag ":"))
  10736. bmp file re)
  10737. (save-excursion
  10738. (save-restriction
  10739. (while (setq file (pop files))
  10740. (if (bufferp file)
  10741. (set-buffer file)
  10742. (org-check-agenda-file file)
  10743. (set-buffer (org-get-agenda-file-buffer file)))
  10744. (widen)
  10745. (setq bmp (buffer-modified-p))
  10746. (org-refresh-category-properties)
  10747. (setq org-todo-keywords-for-agenda
  10748. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10749. (setq org-done-keywords-for-agenda
  10750. (append org-done-keywords-for-agenda org-done-keywords))
  10751. (save-excursion
  10752. (remove-text-properties (point-min) (point-max) pall)
  10753. (when org-agenda-skip-archived-trees
  10754. (goto-char (point-min))
  10755. (while (re-search-forward rea nil t)
  10756. (if (org-on-heading-p t)
  10757. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10758. (goto-char (point-min))
  10759. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10760. (while (re-search-forward re nil t)
  10761. (add-text-properties
  10762. (match-beginning 0) (org-end-of-subtree t) pc)))
  10763. (set-buffer-modified-p bmp))))))
  10764. ;;;; Embedded LaTeX
  10765. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10766. "Keymap for the minor `org-cdlatex-mode'.")
  10767. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10768. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10769. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10770. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10771. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10772. (defvar org-cdlatex-texmathp-advice-is-done nil
  10773. "Flag remembering if we have applied the advice to texmathp already.")
  10774. (define-minor-mode org-cdlatex-mode
  10775. "Toggle the minor `org-cdlatex-mode'.
  10776. This mode supports entering LaTeX environment and math in LaTeX fragments
  10777. in Org-mode.
  10778. \\{org-cdlatex-mode-map}"
  10779. nil " OCDL" nil
  10780. (when org-cdlatex-mode (require 'cdlatex))
  10781. (unless org-cdlatex-texmathp-advice-is-done
  10782. (setq org-cdlatex-texmathp-advice-is-done t)
  10783. (defadvice texmathp (around org-math-always-on activate)
  10784. "Always return t in org-mode buffers.
  10785. This is because we want to insert math symbols without dollars even outside
  10786. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10787. en embedded LaTeX fragement, let texmathp do its job.
  10788. \\[org-cdlatex-mode-map]"
  10789. (interactive)
  10790. (let (p)
  10791. (cond
  10792. ((not (org-mode-p)) ad-do-it)
  10793. ((eq this-command 'cdlatex-math-symbol)
  10794. (setq ad-return-value t
  10795. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10796. (t
  10797. (let ((p (org-inside-LaTeX-fragment-p)))
  10798. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10799. (setq ad-return-value t
  10800. texmathp-why '("Org-mode embedded math" . 0))
  10801. (if p ad-do-it)))))))))
  10802. (defun turn-on-org-cdlatex ()
  10803. "Unconditionally turn on `org-cdlatex-mode'."
  10804. (org-cdlatex-mode 1))
  10805. (defun org-inside-LaTeX-fragment-p ()
  10806. "Test if point is inside a LaTeX fragment.
  10807. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10808. sequence appearing also before point.
  10809. Even though the matchers for math are configurable, this function assumes
  10810. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10811. delimiters are skipped when they have been removed by customization.
  10812. The return value is nil, or a cons cell with the delimiter and
  10813. and the position of this delimiter.
  10814. This function does a reasonably good job, but can locally be fooled by
  10815. for example currency specifications. For example it will assume being in
  10816. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10817. fragments that are properly closed, but during editing, we have to live
  10818. with the uncertainty caused by missing closing delimiters. This function
  10819. looks only before point, not after."
  10820. (catch 'exit
  10821. (let ((pos (point))
  10822. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10823. (lim (progn
  10824. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10825. (point)))
  10826. dd-on str (start 0) m re)
  10827. (goto-char pos)
  10828. (when dodollar
  10829. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10830. re (nth 1 (assoc "$" org-latex-regexps)))
  10831. (while (string-match re str start)
  10832. (cond
  10833. ((= (match-end 0) (length str))
  10834. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10835. ((= (match-end 0) (- (length str) 5))
  10836. (throw 'exit nil))
  10837. (t (setq start (match-end 0))))))
  10838. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10839. (goto-char pos)
  10840. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10841. (and (match-beginning 2) (throw 'exit nil))
  10842. ;; count $$
  10843. (while (re-search-backward "\\$\\$" lim t)
  10844. (setq dd-on (not dd-on)))
  10845. (goto-char pos)
  10846. (if dd-on (cons "$$" m))))))
  10847. (defun org-try-cdlatex-tab ()
  10848. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10849. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10850. - inside a LaTeX fragment, or
  10851. - after the first word in a line, where an abbreviation expansion could
  10852. insert a LaTeX environment."
  10853. (when org-cdlatex-mode
  10854. (cond
  10855. ((save-excursion
  10856. (skip-chars-backward "a-zA-Z0-9*")
  10857. (skip-chars-backward " \t")
  10858. (bolp))
  10859. (cdlatex-tab) t)
  10860. ((org-inside-LaTeX-fragment-p)
  10861. (cdlatex-tab) t)
  10862. (t nil))))
  10863. (defun org-cdlatex-underscore-caret (&optional arg)
  10864. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10865. Revert to the normal definition outside of these fragments."
  10866. (interactive "P")
  10867. (if (org-inside-LaTeX-fragment-p)
  10868. (call-interactively 'cdlatex-sub-superscript)
  10869. (let (org-cdlatex-mode)
  10870. (call-interactively (key-binding (vector last-input-event))))))
  10871. (defun org-cdlatex-math-modify (&optional arg)
  10872. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10873. Revert to the normal definition outside of these fragments."
  10874. (interactive "P")
  10875. (if (org-inside-LaTeX-fragment-p)
  10876. (call-interactively 'cdlatex-math-modify)
  10877. (let (org-cdlatex-mode)
  10878. (call-interactively (key-binding (vector last-input-event))))))
  10879. (defvar org-latex-fragment-image-overlays nil
  10880. "List of overlays carrying the images of latex fragments.")
  10881. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10882. (defun org-remove-latex-fragment-image-overlays ()
  10883. "Remove all overlays with LaTeX fragment images in current buffer."
  10884. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10885. (setq org-latex-fragment-image-overlays nil))
  10886. (defun org-preview-latex-fragment (&optional subtree)
  10887. "Preview the LaTeX fragment at point, or all locally or globally.
  10888. If the cursor is in a LaTeX fragment, create the image and overlay
  10889. it over the source code. If there is no fragment at point, display
  10890. all fragments in the current text, from one headline to the next. With
  10891. prefix SUBTREE, display all fragments in the current subtree. With a
  10892. double prefix `C-u C-u', or when the cursor is before the first headline,
  10893. display all fragments in the buffer.
  10894. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10895. (interactive "P")
  10896. (org-remove-latex-fragment-image-overlays)
  10897. (save-excursion
  10898. (save-restriction
  10899. (let (beg end at msg)
  10900. (cond
  10901. ((or (equal subtree '(16))
  10902. (not (save-excursion
  10903. (re-search-backward (concat "^" outline-regexp) nil t))))
  10904. (setq beg (point-min) end (point-max)
  10905. msg "Creating images for buffer...%s"))
  10906. ((equal subtree '(4))
  10907. (org-back-to-heading)
  10908. (setq beg (point) end (org-end-of-subtree t)
  10909. msg "Creating images for subtree...%s"))
  10910. (t
  10911. (if (setq at (org-inside-LaTeX-fragment-p))
  10912. (goto-char (max (point-min) (- (cdr at) 2)))
  10913. (org-back-to-heading))
  10914. (setq beg (point) end (progn (outline-next-heading) (point))
  10915. msg (if at "Creating image...%s"
  10916. "Creating images for entry...%s"))))
  10917. (message msg "")
  10918. (narrow-to-region beg end)
  10919. (goto-char beg)
  10920. (org-format-latex
  10921. (concat "ltxpng/" (file-name-sans-extension
  10922. (file-name-nondirectory
  10923. buffer-file-name)))
  10924. default-directory 'overlays msg at 'forbuffer)
  10925. (message msg "done. Use `C-c C-c' to remove images.")))))
  10926. (defvar org-latex-regexps
  10927. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10928. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10929. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10930. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10931. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10932. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10933. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10934. "Regular expressions for matching embedded LaTeX.")
  10935. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10936. "Replace LaTeX fragments with links to an image, and produce images."
  10937. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10938. (let* ((prefixnodir (file-name-nondirectory prefix))
  10939. (absprefix (expand-file-name prefix dir))
  10940. (todir (file-name-directory absprefix))
  10941. (opt org-format-latex-options)
  10942. (matchers (plist-get opt :matchers))
  10943. (re-list org-latex-regexps)
  10944. (cnt 0) txt link beg end re e checkdir
  10945. m n block linkfile movefile ov)
  10946. ;; Check if there are old images files with this prefix, and remove them
  10947. (when (file-directory-p todir)
  10948. (mapc 'delete-file
  10949. (directory-files
  10950. todir 'full
  10951. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10952. ;; Check the different regular expressions
  10953. (while (setq e (pop re-list))
  10954. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10955. block (if (nth 3 e) "\n\n" ""))
  10956. (when (member m matchers)
  10957. (goto-char (point-min))
  10958. (while (re-search-forward re nil t)
  10959. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10960. (setq txt (match-string n)
  10961. beg (match-beginning n) end (match-end n)
  10962. cnt (1+ cnt)
  10963. linkfile (format "%s_%04d.png" prefix cnt)
  10964. movefile (format "%s_%04d.png" absprefix cnt)
  10965. link (concat block "[[file:" linkfile "]]" block))
  10966. (if msg (message msg cnt))
  10967. (goto-char beg)
  10968. (unless checkdir ; make sure the directory exists
  10969. (setq checkdir t)
  10970. (or (file-directory-p todir) (make-directory todir)))
  10971. (org-create-formula-image
  10972. txt movefile opt forbuffer)
  10973. (if overlays
  10974. (progn
  10975. (setq ov (org-make-overlay beg end))
  10976. (if (featurep 'xemacs)
  10977. (progn
  10978. (org-overlay-put ov 'invisible t)
  10979. (org-overlay-put
  10980. ov 'end-glyph
  10981. (make-glyph (vector 'png :file movefile))))
  10982. (org-overlay-put
  10983. ov 'display
  10984. (list 'image :type 'png :file movefile :ascent 'center)))
  10985. (push ov org-latex-fragment-image-overlays)
  10986. (goto-char end))
  10987. (delete-region beg end)
  10988. (insert link))))))))
  10989. ;; This function borrows from Ganesh Swami's latex2png.el
  10990. (defun org-create-formula-image (string tofile options buffer)
  10991. (let* ((tmpdir (if (featurep 'xemacs)
  10992. (temp-directory)
  10993. temporary-file-directory))
  10994. (texfilebase (make-temp-name
  10995. (expand-file-name "orgtex" tmpdir)))
  10996. (texfile (concat texfilebase ".tex"))
  10997. (dvifile (concat texfilebase ".dvi"))
  10998. (pngfile (concat texfilebase ".png"))
  10999. (fnh (if (featurep 'xemacs)
  11000. (font-height (get-face-font 'default))
  11001. (face-attribute 'default :height nil)))
  11002. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11003. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11004. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11005. "Black"))
  11006. (bg (or (plist-get options (if buffer :background :html-background))
  11007. "Transparent")))
  11008. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11009. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11010. (with-temp-file texfile
  11011. (insert org-format-latex-header
  11012. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11013. (let ((dir default-directory))
  11014. (condition-case nil
  11015. (progn
  11016. (cd tmpdir)
  11017. (call-process "latex" nil nil nil texfile))
  11018. (error nil))
  11019. (cd dir))
  11020. (if (not (file-exists-p dvifile))
  11021. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11022. (call-process "dvipng" nil nil nil
  11023. "-E" "-fg" fg "-bg" bg
  11024. "-D" dpi
  11025. ;;"-x" scale "-y" scale
  11026. "-T" "tight"
  11027. "-o" pngfile
  11028. dvifile)
  11029. (if (not (file-exists-p pngfile))
  11030. (progn (message "Failed to create png file from %s" texfile) nil)
  11031. ;; Use the requested file name and clean up
  11032. (copy-file pngfile tofile 'replace)
  11033. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11034. (delete-file (concat texfilebase e)))
  11035. pngfile))))
  11036. (defun org-dvipng-color (attr)
  11037. "Return an rgb color specification for dvipng."
  11038. (apply 'format "rgb %s %s %s"
  11039. (mapcar 'org-normalize-color
  11040. (color-values (face-attribute 'default attr nil)))))
  11041. (defun org-normalize-color (value)
  11042. "Return string to be used as color value for an RGB component."
  11043. (format "%g" (/ value 65535.0)))
  11044. ;;;; Key bindings
  11045. ;; Make `C-c C-x' a prefix key
  11046. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11047. ;; TAB key with modifiers
  11048. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11049. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11050. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11051. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11052. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11053. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11054. ;; The following line is necessary under Suse GNU/Linux
  11055. (unless (featurep 'xemacs)
  11056. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11057. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11058. (define-key org-mode-map [backtab] 'org-shifttab)
  11059. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11060. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11061. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11062. ;; Cursor keys with modifiers
  11063. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11064. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11065. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11066. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11067. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11068. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11069. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11070. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11071. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11072. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11073. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11074. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11075. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11076. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11077. ;;; Extra keys for tty access.
  11078. ;; We only set them when really needed because otherwise the
  11079. ;; menus don't show the simple keys
  11080. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11081. (not window-system))
  11082. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11083. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11084. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11085. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11086. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11087. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11088. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11089. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11090. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11091. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11092. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11093. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11094. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11095. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11096. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11097. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11098. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11099. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11100. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11101. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11102. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11103. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11104. ;; All the other keys
  11105. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11106. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11107. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  11108. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11109. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11110. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11111. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11112. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11113. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11114. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11115. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11116. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11117. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11118. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11119. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11120. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11121. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11122. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11123. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11124. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11125. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  11126. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11127. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11128. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11129. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11130. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11131. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11132. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11133. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11134. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11135. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11136. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11137. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11138. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11139. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11140. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11141. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11142. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11143. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11144. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11145. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11146. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11147. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11148. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11149. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11150. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11151. (org-defkey org-mode-map "\C-m" 'org-return)
  11152. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11153. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11154. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11155. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11156. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11157. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  11158. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11159. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11160. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11161. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11162. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  11163. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11164. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11165. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11166. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11167. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11168. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  11169. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11170. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11171. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11172. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11173. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11174. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11175. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11176. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11177. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11178. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11179. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11180. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11181. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11182. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11183. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  11184. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11185. (when (featurep 'xemacs)
  11186. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11187. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11188. (defun org-self-insert-command (N)
  11189. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11190. If the cursor is in a table looking at whitespace, the whitespace is
  11191. overwritten, and the table is not marked as requiring realignment."
  11192. (interactive "p")
  11193. (if (and (org-table-p)
  11194. (progn
  11195. ;; check if we blank the field, and if that triggers align
  11196. (and (featurep 'org-table) org-table-auto-blank-field
  11197. (member last-command
  11198. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11199. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11200. ;; got extra space, this field does not determine column width
  11201. (let (org-table-may-need-update) (org-table-blank-field))
  11202. ;; no extra space, this field may determine column width
  11203. (org-table-blank-field)))
  11204. t)
  11205. (eq N 1)
  11206. (looking-at "[^|\n]* |"))
  11207. (let (org-table-may-need-update)
  11208. (goto-char (1- (match-end 0)))
  11209. (delete-backward-char 1)
  11210. (goto-char (match-beginning 0))
  11211. (self-insert-command N))
  11212. (setq org-table-may-need-update t)
  11213. (self-insert-command N)
  11214. (org-fix-tags-on-the-fly)))
  11215. (defun org-fix-tags-on-the-fly ()
  11216. (when (and (equal (char-after (point-at-bol)) ?*)
  11217. (org-on-heading-p))
  11218. (org-align-tags-here org-tags-column)))
  11219. (defun org-delete-backward-char (N)
  11220. "Like `delete-backward-char', insert whitespace at field end in tables.
  11221. When deleting backwards, in tables this function will insert whitespace in
  11222. front of the next \"|\" separator, to keep the table aligned. The table will
  11223. still be marked for re-alignment if the field did fill the entire column,
  11224. because, in this case the deletion might narrow the column."
  11225. (interactive "p")
  11226. (if (and (org-table-p)
  11227. (eq N 1)
  11228. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11229. (looking-at ".*?|"))
  11230. (let ((pos (point))
  11231. (noalign (looking-at "[^|\n\r]* |"))
  11232. (c org-table-may-need-update))
  11233. (backward-delete-char N)
  11234. (skip-chars-forward "^|")
  11235. (insert " ")
  11236. (goto-char (1- pos))
  11237. ;; noalign: if there were two spaces at the end, this field
  11238. ;; does not determine the width of the column.
  11239. (if noalign (setq org-table-may-need-update c)))
  11240. (backward-delete-char N)
  11241. (org-fix-tags-on-the-fly)))
  11242. (defun org-delete-char (N)
  11243. "Like `delete-char', but insert whitespace at field end in tables.
  11244. When deleting characters, in tables this function will insert whitespace in
  11245. front of the next \"|\" separator, to keep the table aligned. The table will
  11246. still be marked for re-alignment if the field did fill the entire column,
  11247. because, in this case the deletion might narrow the column."
  11248. (interactive "p")
  11249. (if (and (org-table-p)
  11250. (not (bolp))
  11251. (not (= (char-after) ?|))
  11252. (eq N 1))
  11253. (if (looking-at ".*?|")
  11254. (let ((pos (point))
  11255. (noalign (looking-at "[^|\n\r]* |"))
  11256. (c org-table-may-need-update))
  11257. (replace-match (concat
  11258. (substring (match-string 0) 1 -1)
  11259. " |"))
  11260. (goto-char pos)
  11261. ;; noalign: if there were two spaces at the end, this field
  11262. ;; does not determine the width of the column.
  11263. (if noalign (setq org-table-may-need-update c)))
  11264. (delete-char N))
  11265. (delete-char N)
  11266. (org-fix-tags-on-the-fly)))
  11267. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11268. (put 'org-self-insert-command 'delete-selection t)
  11269. (put 'orgtbl-self-insert-command 'delete-selection t)
  11270. (put 'org-delete-char 'delete-selection 'supersede)
  11271. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11272. ;; Make `flyspell-mode' delay after some commands
  11273. (put 'org-self-insert-command 'flyspell-delayed t)
  11274. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11275. (put 'org-delete-char 'flyspell-delayed t)
  11276. (put 'org-delete-backward-char 'flyspell-delayed t)
  11277. ;; Make pabbrev-mode expand after org-mode commands
  11278. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11279. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11280. ;; How to do this: Measure non-white length of current string
  11281. ;; If equal to column width, we should realign.
  11282. (defun org-remap (map &rest commands)
  11283. "In MAP, remap the functions given in COMMANDS.
  11284. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11285. (let (new old)
  11286. (while commands
  11287. (setq old (pop commands) new (pop commands))
  11288. (if (fboundp 'command-remapping)
  11289. (org-defkey map (vector 'remap old) new)
  11290. (substitute-key-definition old new map global-map)))))
  11291. (when (eq org-enable-table-editor 'optimized)
  11292. ;; If the user wants maximum table support, we need to hijack
  11293. ;; some standard editing functions
  11294. (org-remap org-mode-map
  11295. 'self-insert-command 'org-self-insert-command
  11296. 'delete-char 'org-delete-char
  11297. 'delete-backward-char 'org-delete-backward-char)
  11298. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11299. (defun org-shiftcursor-error ()
  11300. "Throw an error because Shift-Cursor command was applied in wrong context."
  11301. (error "This command is active in special context like tables, headlines or timestamps"))
  11302. (defun org-shifttab (&optional arg)
  11303. "Global visibility cycling or move to previous table field.
  11304. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11305. on context.
  11306. See the individual commands for more information."
  11307. (interactive "P")
  11308. (cond
  11309. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11310. ((integerp arg)
  11311. (message "Content view to level: %d" arg)
  11312. (org-content (prefix-numeric-value arg))
  11313. (setq org-cycle-global-status 'overview))
  11314. (t (call-interactively 'org-global-cycle))))
  11315. (defun org-shiftmetaleft ()
  11316. "Promote subtree or delete table column.
  11317. Calls `org-promote-subtree', `org-outdent-item',
  11318. or `org-table-delete-column', depending on context.
  11319. See the individual commands for more information."
  11320. (interactive)
  11321. (cond
  11322. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11323. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11324. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11325. (t (org-shiftcursor-error))))
  11326. (defun org-shiftmetaright ()
  11327. "Demote subtree or insert table column.
  11328. Calls `org-demote-subtree', `org-indent-item',
  11329. or `org-table-insert-column', depending on context.
  11330. See the individual commands for more information."
  11331. (interactive)
  11332. (cond
  11333. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11334. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11335. ((org-at-item-p) (call-interactively 'org-indent-item))
  11336. (t (org-shiftcursor-error))))
  11337. (defun org-shiftmetaup (&optional arg)
  11338. "Move subtree up or kill table row.
  11339. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11340. `org-move-item-up' depending on context. See the individual commands
  11341. for more information."
  11342. (interactive "P")
  11343. (cond
  11344. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11345. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11346. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11347. (t (org-shiftcursor-error))))
  11348. (defun org-shiftmetadown (&optional arg)
  11349. "Move subtree down or insert table row.
  11350. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11351. `org-move-item-down', depending on context. See the individual
  11352. commands for more information."
  11353. (interactive "P")
  11354. (cond
  11355. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11356. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11357. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11358. (t (org-shiftcursor-error))))
  11359. (defun org-metaleft (&optional arg)
  11360. "Promote heading or move table column to left.
  11361. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11362. With no specific context, calls the Emacs default `backward-word'.
  11363. See the individual commands for more information."
  11364. (interactive "P")
  11365. (cond
  11366. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11367. ((or (org-on-heading-p) (org-region-active-p))
  11368. (call-interactively 'org-do-promote))
  11369. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11370. (t (call-interactively 'backward-word))))
  11371. (defun org-metaright (&optional arg)
  11372. "Demote subtree or move table column to right.
  11373. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11374. With no specific context, calls the Emacs default `forward-word'.
  11375. See the individual commands for more information."
  11376. (interactive "P")
  11377. (cond
  11378. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11379. ((or (org-on-heading-p) (org-region-active-p))
  11380. (call-interactively 'org-do-demote))
  11381. ((org-at-item-p) (call-interactively 'org-indent-item))
  11382. (t (call-interactively 'forward-word))))
  11383. (defun org-metaup (&optional arg)
  11384. "Move subtree up or move table row up.
  11385. Calls `org-move-subtree-up' or `org-table-move-row' or
  11386. `org-move-item-up', depending on context. See the individual commands
  11387. for more information."
  11388. (interactive "P")
  11389. (cond
  11390. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11391. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11392. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11393. (t (transpose-lines 1) (beginning-of-line -1))))
  11394. (defun org-metadown (&optional arg)
  11395. "Move subtree down or move table row down.
  11396. Calls `org-move-subtree-down' or `org-table-move-row' or
  11397. `org-move-item-down', depending on context. See the individual
  11398. commands for more information."
  11399. (interactive "P")
  11400. (cond
  11401. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11402. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11403. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11404. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11405. (defun org-shiftup (&optional arg)
  11406. "Increase item in timestamp or increase priority of current headline.
  11407. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11408. depending on context. See the individual commands for more information."
  11409. (interactive "P")
  11410. (cond
  11411. ((org-at-timestamp-p t)
  11412. (call-interactively (if org-edit-timestamp-down-means-later
  11413. 'org-timestamp-down 'org-timestamp-up)))
  11414. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11415. ((org-at-item-p) (call-interactively 'org-previous-item))
  11416. ((org-clocktable-try-shift 'up arg))
  11417. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11418. (defun org-shiftdown (&optional arg)
  11419. "Decrease item in timestamp or decrease priority of current headline.
  11420. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11421. depending on context. See the individual commands for more information."
  11422. (interactive "P")
  11423. (cond
  11424. ((org-at-timestamp-p t)
  11425. (call-interactively (if org-edit-timestamp-down-means-later
  11426. 'org-timestamp-up 'org-timestamp-down)))
  11427. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11428. ((org-clocktable-try-shift 'down arg))
  11429. (t (call-interactively 'org-next-item))))
  11430. (defun org-shiftright (&optional arg)
  11431. "Next TODO keyword or timestamp one day later, depending on context."
  11432. (interactive "P")
  11433. (cond
  11434. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11435. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11436. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11437. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11438. ((org-clocktable-try-shift 'right arg))
  11439. (t (org-shiftcursor-error))))
  11440. (defun org-shiftleft (&optional arg)
  11441. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11442. (interactive "P")
  11443. (cond
  11444. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11445. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11446. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11447. ((org-at-property-p)
  11448. (call-interactively 'org-property-previous-allowed-value))
  11449. ((org-clocktable-try-shift 'left arg))
  11450. (t (org-shiftcursor-error))))
  11451. (defun org-shiftcontrolright ()
  11452. "Switch to next TODO set."
  11453. (interactive)
  11454. (cond
  11455. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11456. (t (org-shiftcursor-error))))
  11457. (defun org-shiftcontrolleft ()
  11458. "Switch to previous TODO set."
  11459. (interactive)
  11460. (cond
  11461. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11462. (t (org-shiftcursor-error))))
  11463. (defun org-ctrl-c-ret ()
  11464. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11465. (interactive)
  11466. (cond
  11467. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11468. (t (call-interactively 'org-insert-heading))))
  11469. (defun org-copy-special ()
  11470. "Copy region in table or copy current subtree.
  11471. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11472. See the individual commands for more information."
  11473. (interactive)
  11474. (call-interactively
  11475. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11476. (defun org-cut-special ()
  11477. "Cut region in table or cut current subtree.
  11478. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11479. See the individual commands for more information."
  11480. (interactive)
  11481. (call-interactively
  11482. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11483. (defun org-paste-special (arg)
  11484. "Paste rectangular region into table, or past subtree relative to level.
  11485. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11486. See the individual commands for more information."
  11487. (interactive "P")
  11488. (if (org-at-table-p)
  11489. (org-table-paste-rectangle)
  11490. (org-paste-subtree arg)))
  11491. (defun org-ctrl-c-ctrl-c (&optional arg)
  11492. "Set tags in headline, or update according to changed information at point.
  11493. This command does many different things, depending on context:
  11494. - If the cursor is in a headline, prompt for tags and insert them
  11495. into the current line, aligned to `org-tags-column'. When called
  11496. with prefix arg, realign all tags in the current buffer.
  11497. - If the cursor is in one of the special #+KEYWORD lines, this
  11498. triggers scanning the buffer for these lines and updating the
  11499. information.
  11500. - If the cursor is inside a table, realign the table. This command
  11501. works even if the automatic table editor has been turned off.
  11502. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11503. the entire table.
  11504. - If the cursor is a the beginning of a dynamic block, update it.
  11505. - If the cursor is inside a table created by the table.el package,
  11506. activate that table.
  11507. - If the current buffer is a remember buffer, close note and file it.
  11508. with a prefix argument, file it without further interaction to the default
  11509. location.
  11510. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11511. links in this buffer.
  11512. - If the cursor is on a numbered item in a plain list, renumber the
  11513. ordered list.
  11514. - If the cursor is on a checkbox, toggle it."
  11515. (interactive "P")
  11516. (let ((org-enable-table-editor t))
  11517. (cond
  11518. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11519. org-occur-highlights
  11520. org-latex-fragment-image-overlays)
  11521. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11522. (org-remove-occur-highlights)
  11523. (org-remove-latex-fragment-image-overlays)
  11524. (message "Temporary highlights/overlays removed from current buffer"))
  11525. ((and (local-variable-p 'org-finish-function (current-buffer))
  11526. (fboundp org-finish-function))
  11527. (funcall org-finish-function))
  11528. ((org-at-property-p)
  11529. (call-interactively 'org-property-action))
  11530. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11531. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11532. ((org-at-table.el-p)
  11533. (require 'table)
  11534. (beginning-of-line 1)
  11535. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11536. (call-interactively 'table-recognize-table))
  11537. ((org-at-table-p)
  11538. (org-table-maybe-eval-formula)
  11539. (if arg
  11540. (call-interactively 'org-table-recalculate)
  11541. (org-table-maybe-recalculate-line))
  11542. (call-interactively 'org-table-align))
  11543. ((org-at-item-checkbox-p)
  11544. (call-interactively 'org-toggle-checkbox))
  11545. ((org-at-item-p)
  11546. (call-interactively 'org-maybe-renumber-ordered-list))
  11547. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11548. ;; Dynamic block
  11549. (beginning-of-line 1)
  11550. (org-update-dblock))
  11551. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11552. (cond
  11553. ((equal (match-string 1) "TBLFM")
  11554. ;; Recalculate the table before this line
  11555. (save-excursion
  11556. (beginning-of-line 1)
  11557. (skip-chars-backward " \r\n\t")
  11558. (if (org-at-table-p)
  11559. (org-call-with-arg 'org-table-recalculate t))))
  11560. (t
  11561. ; (org-set-regexps-and-options)
  11562. ; (org-restart-font-lock)
  11563. (let ((org-inhibit-startup t)) (org-mode-restart))
  11564. (message "Local setup has been refreshed"))))
  11565. (t (error "C-c C-c can do nothing useful at this location.")))))
  11566. (defun org-mode-restart ()
  11567. "Restart Org-mode, to scan again for special lines.
  11568. Also updates the keyword regular expressions."
  11569. (interactive)
  11570. (org-mode)
  11571. (message "Org-mode restarted"))
  11572. (defun org-kill-note-or-show-branches ()
  11573. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11574. (interactive)
  11575. (if (not org-finish-function)
  11576. (call-interactively 'show-branches)
  11577. (let ((org-note-abort t))
  11578. (funcall org-finish-function))))
  11579. (defun org-return (&optional indent)
  11580. "Goto next table row or insert a newline.
  11581. Calls `org-table-next-row' or `newline', depending on context.
  11582. See the individual commands for more information."
  11583. (interactive)
  11584. (cond
  11585. ((bobp) (if indent (newline-and-indent) (newline)))
  11586. ((and (org-at-heading-p)
  11587. (looking-at
  11588. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11589. (org-show-entry)
  11590. (end-of-line 1)
  11591. (newline))
  11592. ((org-at-table-p)
  11593. (org-table-justify-field-maybe)
  11594. (call-interactively 'org-table-next-row))
  11595. (t (if indent (newline-and-indent) (newline)))))
  11596. (defun org-return-indent ()
  11597. "Goto next table row or insert a newline and indent.
  11598. Calls `org-table-next-row' or `newline-and-indent', depending on
  11599. context. See the individual commands for more information."
  11600. (interactive)
  11601. (org-return t))
  11602. (defun org-ctrl-c-star ()
  11603. "Compute table, or change heading status of lines.
  11604. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  11605. depending on context. This will also turn a plain list item or a normal
  11606. line into a subheading."
  11607. (interactive)
  11608. (cond
  11609. ((org-at-table-p)
  11610. (call-interactively 'org-table-recalculate))
  11611. ((org-region-active-p)
  11612. ;; Convert all lines in region to list items
  11613. (call-interactively 'org-toggle-region-headings))
  11614. ((org-on-heading-p)
  11615. (org-toggle-region-headings (point-at-bol)
  11616. (min (1+ (point-at-eol)) (point-max))))
  11617. ((org-at-item-p)
  11618. ;; Convert to heading
  11619. (let ((level (save-match-data
  11620. (save-excursion
  11621. (condition-case nil
  11622. (progn
  11623. (org-back-to-heading t)
  11624. (funcall outline-level))
  11625. (error 0))))))
  11626. (replace-match
  11627. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11628. (t (org-toggle-region-headings (point-at-bol)
  11629. (min (1+ (point-at-eol)) (point-max))))))
  11630. (defun org-ctrl-c-minus ()
  11631. "Insert separator line in table or modify bullet status of line.
  11632. Also turns a plain line or a region of lines into list items.
  11633. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11634. `org-cycle-list-bullet', depending on context."
  11635. (interactive)
  11636. (cond
  11637. ((org-at-table-p)
  11638. (call-interactively 'org-table-insert-hline))
  11639. ((org-on-heading-p)
  11640. ;; Convert to item
  11641. (save-excursion
  11642. (beginning-of-line 1)
  11643. (if (looking-at "\\*+ ")
  11644. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11645. ((org-region-active-p)
  11646. ;; Convert all lines in region to list items
  11647. (call-interactively 'org-toggle-region-items))
  11648. ((org-in-item-p)
  11649. (call-interactively 'org-cycle-list-bullet))
  11650. (t (org-toggle-region-items (point-at-bol)
  11651. (min (1+ (point-at-eol)) (point-max))))))
  11652. (defun org-toggle-region-items (beg end)
  11653. "Convert all lines in region to list items.
  11654. If the first line is already an item, convert all list items in the region
  11655. to normal lines."
  11656. (interactive "r")
  11657. (let (l2 l)
  11658. (save-excursion
  11659. (goto-char end)
  11660. (setq l2 (org-current-line))
  11661. (goto-char beg)
  11662. (beginning-of-line 1)
  11663. (setq l (1- (org-current-line)))
  11664. (if (org-at-item-p)
  11665. ;; We already have items, de-itemize
  11666. (while (< (setq l (1+ l)) l2)
  11667. (when (org-at-item-p)
  11668. (goto-char (match-beginning 2))
  11669. (delete-region (match-beginning 2) (match-end 2))
  11670. (and (looking-at "[ \t]+") (replace-match "")))
  11671. (beginning-of-line 2))
  11672. (while (< (setq l (1+ l)) l2)
  11673. (unless (org-at-item-p)
  11674. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11675. (replace-match "\\1- \\2")))
  11676. (beginning-of-line 2))))))
  11677. (defun org-toggle-region-headings (beg end)
  11678. "Convert all lines in region to list items.
  11679. If the first line is already an item, convert all list items in the region
  11680. to normal lines."
  11681. (interactive "r")
  11682. (let (l2 l)
  11683. (save-excursion
  11684. (goto-char end)
  11685. (setq l2 (org-current-line))
  11686. (goto-char beg)
  11687. (beginning-of-line 1)
  11688. (setq l (1- (org-current-line)))
  11689. (if (org-on-heading-p)
  11690. ;; We already have headlines, de-star them
  11691. (while (< (setq l (1+ l)) l2)
  11692. (when (org-on-heading-p t)
  11693. (and (looking-at outline-regexp) (replace-match "")))
  11694. (beginning-of-line 2))
  11695. (let* ((stars (save-excursion
  11696. (re-search-backward org-complex-heading-regexp nil t)
  11697. (or (match-string 1) "*")))
  11698. (add-stars (if org-odd-levels-only "**" "*"))
  11699. (rpl (concat stars add-stars " \\2")))
  11700. (while (< (setq l (1+ l)) l2)
  11701. (unless (org-on-heading-p)
  11702. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11703. (replace-match rpl)))
  11704. (beginning-of-line 2)))))))
  11705. (defun org-meta-return (&optional arg)
  11706. "Insert a new heading or wrap a region in a table.
  11707. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11708. See the individual commands for more information."
  11709. (interactive "P")
  11710. (cond
  11711. ((org-at-table-p)
  11712. (call-interactively 'org-table-wrap-region))
  11713. (t (call-interactively 'org-insert-heading))))
  11714. ;;; Menu entries
  11715. ;; Define the Org-mode menus
  11716. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11717. '("Tbl"
  11718. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11719. ["Next Field" org-cycle (org-at-table-p)]
  11720. ["Previous Field" org-shifttab (org-at-table-p)]
  11721. ["Next Row" org-return (org-at-table-p)]
  11722. "--"
  11723. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11724. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11725. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11726. "--"
  11727. ("Column"
  11728. ["Move Column Left" org-metaleft (org-at-table-p)]
  11729. ["Move Column Right" org-metaright (org-at-table-p)]
  11730. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11731. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11732. ("Row"
  11733. ["Move Row Up" org-metaup (org-at-table-p)]
  11734. ["Move Row Down" org-metadown (org-at-table-p)]
  11735. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11736. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11737. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11738. "--"
  11739. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11740. ("Rectangle"
  11741. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11742. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11743. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11744. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11745. "--"
  11746. ("Calculate"
  11747. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11748. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11749. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  11750. "--"
  11751. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11752. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11753. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11754. "--"
  11755. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11756. "--"
  11757. ["Sum Column/Rectangle" org-table-sum
  11758. (or (org-at-table-p) (org-region-active-p))]
  11759. ["Which Column?" org-table-current-column (org-at-table-p)])
  11760. ["Debug Formulas"
  11761. org-table-toggle-formula-debugger
  11762. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11763. ["Show Col/Row Numbers"
  11764. org-table-toggle-coordinate-overlays
  11765. :style toggle
  11766. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11767. "--"
  11768. ["Create" org-table-create (and (not (org-at-table-p))
  11769. org-enable-table-editor)]
  11770. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11771. ["Import from File" org-table-import (not (org-at-table-p))]
  11772. ["Export to File" org-table-export (org-at-table-p)]
  11773. "--"
  11774. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11775. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11776. '("Org"
  11777. ("Show/Hide"
  11778. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11779. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11780. ["Sparse Tree..." org-sparse-tree t]
  11781. ["Reveal Context" org-reveal t]
  11782. ["Show All" show-all t]
  11783. "--"
  11784. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11785. "--"
  11786. ["New Heading" org-insert-heading t]
  11787. ("Navigate Headings"
  11788. ["Up" outline-up-heading t]
  11789. ["Next" outline-next-visible-heading t]
  11790. ["Previous" outline-previous-visible-heading t]
  11791. ["Next Same Level" outline-forward-same-level t]
  11792. ["Previous Same Level" outline-backward-same-level t]
  11793. "--"
  11794. ["Jump" org-goto t])
  11795. ("Edit Structure"
  11796. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11797. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11798. "--"
  11799. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11800. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11801. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11802. "--"
  11803. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11804. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11805. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11806. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11807. "--"
  11808. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11809. "--"
  11810. ["Convert to odd levels" org-convert-to-odd-levels t]
  11811. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11812. ("Editing"
  11813. ["Emphasis..." org-emphasize t])
  11814. ("Archive"
  11815. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11816. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11817. ; :active t :keys "C-u C-c C-x C-a"]
  11818. ["Sparse trees open ARCHIVE trees"
  11819. (setq org-sparse-tree-open-archived-trees
  11820. (not org-sparse-tree-open-archived-trees))
  11821. :style toggle :selected org-sparse-tree-open-archived-trees]
  11822. ["Cycling opens ARCHIVE trees"
  11823. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11824. :style toggle :selected org-cycle-open-archived-trees]
  11825. ["Agenda includes ARCHIVE trees"
  11826. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  11827. :style toggle :selected (not org-agenda-skip-archived-trees)]
  11828. "--"
  11829. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11830. ; ["Check and Move Children" (org-archive-subtree '(4))
  11831. ; :active t :keys "C-u C-c C-x C-s"]
  11832. )
  11833. "--"
  11834. ("TODO Lists"
  11835. ["TODO/DONE/-" org-todo t]
  11836. ("Select keyword"
  11837. ["Next keyword" org-shiftright (org-on-heading-p)]
  11838. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11839. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11840. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11841. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11842. ["Show TODO Tree" org-show-todo-tree t]
  11843. ["Global TODO list" org-todo-list t]
  11844. "--"
  11845. ["Set Priority" org-priority t]
  11846. ["Priority Up" org-shiftup t]
  11847. ["Priority Down" org-shiftdown t])
  11848. ("TAGS and Properties"
  11849. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  11850. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11851. "--"
  11852. ["Set property" 'org-set-property t]
  11853. ["Column view of properties" org-columns t]
  11854. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11855. ("Dates and Scheduling"
  11856. ["Timestamp" org-time-stamp t]
  11857. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11858. ("Change Date"
  11859. ["1 Day Later" org-shiftright t]
  11860. ["1 Day Earlier" org-shiftleft t]
  11861. ["1 ... Later" org-shiftup t]
  11862. ["1 ... Earlier" org-shiftdown t])
  11863. ["Compute Time Range" org-evaluate-time-range t]
  11864. ["Schedule Item" org-schedule t]
  11865. ["Deadline" org-deadline t]
  11866. "--"
  11867. ["Custom time format" org-toggle-time-stamp-overlays
  11868. :style radio :selected org-display-custom-times]
  11869. "--"
  11870. ["Goto Calendar" org-goto-calendar t]
  11871. ["Date from Calendar" org-date-from-calendar t])
  11872. ("Logging work"
  11873. ["Clock in" org-clock-in t]
  11874. ["Clock out" org-clock-out t]
  11875. ["Clock cancel" org-clock-cancel t]
  11876. ["Goto running clock" org-clock-goto t]
  11877. ["Display times" org-clock-display t]
  11878. ["Create clock table" org-clock-report t]
  11879. "--"
  11880. ["Record DONE time"
  11881. (progn (setq org-log-done (not org-log-done))
  11882. (message "Switching to %s will %s record a timestamp"
  11883. (car org-done-keywords)
  11884. (if org-log-done "automatically" "not")))
  11885. :style toggle :selected org-log-done])
  11886. "--"
  11887. ["Agenda Command..." org-agenda t]
  11888. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11889. ("File List for Agenda")
  11890. ("Special views current file"
  11891. ["TODO Tree" org-show-todo-tree t]
  11892. ["Check Deadlines" org-check-deadlines t]
  11893. ["Timeline" org-timeline t]
  11894. ["Tags Tree" org-tags-sparse-tree t])
  11895. "--"
  11896. ("Hyperlinks"
  11897. ["Store Link (Global)" org-store-link t]
  11898. ["Insert Link" org-insert-link t]
  11899. ["Follow Link" org-open-at-point t]
  11900. "--"
  11901. ["Next link" org-next-link t]
  11902. ["Previous link" org-previous-link t]
  11903. "--"
  11904. ["Descriptive Links"
  11905. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11906. :style radio
  11907. :selected (member '(org-link) buffer-invisibility-spec)]
  11908. ["Literal Links"
  11909. (progn
  11910. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11911. :style radio
  11912. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11913. "--"
  11914. ["Export/Publish..." org-export t]
  11915. ("LaTeX"
  11916. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11917. :selected org-cdlatex-mode]
  11918. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11919. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11920. ["Modify math symbol" org-cdlatex-math-modify
  11921. (org-inside-LaTeX-fragment-p)]
  11922. ["Export LaTeX fragments as images"
  11923. (if (featurep 'org-exp)
  11924. (setq org-export-with-LaTeX-fragments
  11925. (not org-export-with-LaTeX-fragments))
  11926. (require 'org-exp))
  11927. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11928. org-export-with-LaTeX-fragments)])
  11929. "--"
  11930. ("Documentation"
  11931. ["Show Version" org-version t]
  11932. ["Info Documentation" org-info t])
  11933. ("Customize"
  11934. ["Browse Org Group" org-customize t]
  11935. "--"
  11936. ["Expand This Menu" org-create-customize-menu
  11937. (fboundp 'customize-menu-create)])
  11938. "--"
  11939. ["Refresh setup" org-mode-restart t]
  11940. ))
  11941. (defun org-info (&optional node)
  11942. "Read documentation for Org-mode in the info system.
  11943. With optional NODE, go directly to that node."
  11944. (interactive)
  11945. (info (format "(org)%s" (or node ""))))
  11946. (defun org-install-agenda-files-menu ()
  11947. (let ((bl (buffer-list)))
  11948. (save-excursion
  11949. (while bl
  11950. (set-buffer (pop bl))
  11951. (if (org-mode-p) (setq bl nil)))
  11952. (when (org-mode-p)
  11953. (easy-menu-change
  11954. '("Org") "File List for Agenda"
  11955. (append
  11956. (list
  11957. ["Edit File List" (org-edit-agenda-file-list) t]
  11958. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11959. ["Remove Current File from List" org-remove-file t]
  11960. ["Cycle through agenda files" org-cycle-agenda-files t]
  11961. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11962. "--")
  11963. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11964. ;;;; Documentation
  11965. (defun org-require-autoloaded-modules ()
  11966. (interactive)
  11967. (mapc 'require
  11968. '(org-agenda org-archive org-clock org-colview
  11969. org-exp org-export-latex org-publish
  11970. org-remember org-table)))
  11971. (defun org-customize ()
  11972. "Call the customize function with org as argument."
  11973. (interactive)
  11974. (org-load-modules-maybe)
  11975. (org-require-autoloaded-modules)
  11976. (customize-browse 'org))
  11977. (defun org-create-customize-menu ()
  11978. "Create a full customization menu for Org-mode, insert it into the menu."
  11979. (interactive)
  11980. (org-load-modules-maybe)
  11981. (org-require-autoloaded-modules)
  11982. (if (fboundp 'customize-menu-create)
  11983. (progn
  11984. (easy-menu-change
  11985. '("Org") "Customize"
  11986. `(["Browse Org group" org-customize t]
  11987. "--"
  11988. ,(customize-menu-create 'org)
  11989. ["Set" Custom-set t]
  11990. ["Save" Custom-save t]
  11991. ["Reset to Current" Custom-reset-current t]
  11992. ["Reset to Saved" Custom-reset-saved t]
  11993. ["Reset to Standard Settings" Custom-reset-standard t]))
  11994. (message "\"Org\"-menu now contains full customization menu"))
  11995. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  11996. ;;;; Miscellaneous stuff
  11997. ;;; Generally useful functions
  11998. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  11999. "Display the given MESSAGE as a warning."
  12000. (if (fboundp 'display-warning)
  12001. (display-warning 'org message
  12002. (if (featurep 'xemacs)
  12003. 'warning
  12004. :warning))
  12005. (let ((buf (get-buffer-create "*Org warnings*")))
  12006. (with-current-buffer buf
  12007. (goto-char (point-max))
  12008. (insert "Warning (Org): " message)
  12009. (unless (bolp)
  12010. (newline)))
  12011. (display-buffer buf)
  12012. (sit-for 0))))
  12013. (defun org-quote-csv-field (s)
  12014. "Quote field for inclusion in CSV material."
  12015. (if (string-match "[\",]" s)
  12016. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12017. s))
  12018. (defun org-plist-delete (plist property)
  12019. "Delete PROPERTY from PLIST.
  12020. This is in contrast to merely setting it to 0."
  12021. (let (p)
  12022. (while plist
  12023. (if (not (eq property (car plist)))
  12024. (setq p (plist-put p (car plist) (nth 1 plist))))
  12025. (setq plist (cddr plist)))
  12026. p))
  12027. (defun org-force-self-insert (N)
  12028. "Needed to enforce self-insert under remapping."
  12029. (interactive "p")
  12030. (self-insert-command N))
  12031. (defun org-string-width (s)
  12032. "Compute width of string, ignoring invisible characters.
  12033. This ignores character with invisibility property `org-link', and also
  12034. characters with property `org-cwidth', because these will become invisible
  12035. upon the next fontification round."
  12036. (let (b l)
  12037. (when (or (eq t buffer-invisibility-spec)
  12038. (assq 'org-link buffer-invisibility-spec))
  12039. (while (setq b (text-property-any 0 (length s)
  12040. 'invisible 'org-link s))
  12041. (setq s (concat (substring s 0 b)
  12042. (substring s (or (next-single-property-change
  12043. b 'invisible s) (length s)))))))
  12044. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12045. (setq s (concat (substring s 0 b)
  12046. (substring s (or (next-single-property-change
  12047. b 'org-cwidth s) (length s))))))
  12048. (setq l (string-width s) b -1)
  12049. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12050. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12051. l))
  12052. (defun org-base-buffer (buffer)
  12053. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12054. (if (not buffer)
  12055. buffer
  12056. (or (buffer-base-buffer buffer)
  12057. buffer)))
  12058. (defun org-trim (s)
  12059. "Remove whitespace at beginning and end of string."
  12060. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12061. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12062. s)
  12063. (defun org-wrap (string &optional width lines)
  12064. "Wrap string to either a number of lines, or a width in characters.
  12065. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12066. that costs. If there is a word longer than WIDTH, the text is actually
  12067. wrapped to the length of that word.
  12068. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12069. many lines, whatever width that takes.
  12070. The return value is a list of lines, without newlines at the end."
  12071. (let* ((words (org-split-string string "[ \t\n]+"))
  12072. (maxword (apply 'max (mapcar 'org-string-width words)))
  12073. w ll)
  12074. (cond (width
  12075. (org-do-wrap words (max maxword width)))
  12076. (lines
  12077. (setq w maxword)
  12078. (setq ll (org-do-wrap words maxword))
  12079. (if (<= (length ll) lines)
  12080. ll
  12081. (setq ll words)
  12082. (while (> (length ll) lines)
  12083. (setq w (1+ w))
  12084. (setq ll (org-do-wrap words w)))
  12085. ll))
  12086. (t (error "Cannot wrap this")))))
  12087. (defun org-do-wrap (words width)
  12088. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12089. (let (lines line)
  12090. (while words
  12091. (setq line (pop words))
  12092. (while (and words (< (+ (length line) (length (car words))) width))
  12093. (setq line (concat line " " (pop words))))
  12094. (setq lines (push line lines)))
  12095. (nreverse lines)))
  12096. (defun org-split-string (string &optional separators)
  12097. "Splits STRING into substrings at SEPARATORS.
  12098. No empty strings are returned if there are matches at the beginning
  12099. and end of string."
  12100. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12101. (start 0)
  12102. notfirst
  12103. (list nil))
  12104. (while (and (string-match rexp string
  12105. (if (and notfirst
  12106. (= start (match-beginning 0))
  12107. (< start (length string)))
  12108. (1+ start) start))
  12109. (< (match-beginning 0) (length string)))
  12110. (setq notfirst t)
  12111. (or (eq (match-beginning 0) 0)
  12112. (and (eq (match-beginning 0) (match-end 0))
  12113. (eq (match-beginning 0) start))
  12114. (setq list
  12115. (cons (substring string start (match-beginning 0))
  12116. list)))
  12117. (setq start (match-end 0)))
  12118. (or (eq start (length string))
  12119. (setq list
  12120. (cons (substring string start)
  12121. list)))
  12122. (nreverse list)))
  12123. (defun org-context ()
  12124. "Return a list of contexts of the current cursor position.
  12125. If several contexts apply, all are returned.
  12126. Each context entry is a list with a symbol naming the context, and
  12127. two positions indicating start and end of the context. Possible
  12128. contexts are:
  12129. :headline anywhere in a headline
  12130. :headline-stars on the leading stars in a headline
  12131. :todo-keyword on a TODO keyword (including DONE) in a headline
  12132. :tags on the TAGS in a headline
  12133. :priority on the priority cookie in a headline
  12134. :item on the first line of a plain list item
  12135. :item-bullet on the bullet/number of a plain list item
  12136. :checkbox on the checkbox in a plain list item
  12137. :table in an org-mode table
  12138. :table-special on a special filed in a table
  12139. :table-table in a table.el table
  12140. :link on a hyperlink
  12141. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12142. :target on a <<target>>
  12143. :radio-target on a <<<radio-target>>>
  12144. :latex-fragment on a LaTeX fragment
  12145. :latex-preview on a LaTeX fragment with overlayed preview image
  12146. This function expects the position to be visible because it uses font-lock
  12147. faces as a help to recognize the following contexts: :table-special, :link,
  12148. and :keyword."
  12149. (let* ((f (get-text-property (point) 'face))
  12150. (faces (if (listp f) f (list f)))
  12151. (p (point)) clist o)
  12152. ;; First the large context
  12153. (cond
  12154. ((org-on-heading-p t)
  12155. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12156. (when (progn
  12157. (beginning-of-line 1)
  12158. (looking-at org-todo-line-tags-regexp))
  12159. (push (org-point-in-group p 1 :headline-stars) clist)
  12160. (push (org-point-in-group p 2 :todo-keyword) clist)
  12161. (push (org-point-in-group p 4 :tags) clist))
  12162. (goto-char p)
  12163. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12164. (if (looking-at "\\[#[A-Z0-9]\\]")
  12165. (push (org-point-in-group p 0 :priority) clist)))
  12166. ((org-at-item-p)
  12167. (push (org-point-in-group p 2 :item-bullet) clist)
  12168. (push (list :item (point-at-bol)
  12169. (save-excursion (org-end-of-item) (point)))
  12170. clist)
  12171. (and (org-at-item-checkbox-p)
  12172. (push (org-point-in-group p 0 :checkbox) clist)))
  12173. ((org-at-table-p)
  12174. (push (list :table (org-table-begin) (org-table-end)) clist)
  12175. (if (memq 'org-formula faces)
  12176. (push (list :table-special
  12177. (previous-single-property-change p 'face)
  12178. (next-single-property-change p 'face)) clist)))
  12179. ((org-at-table-p 'any)
  12180. (push (list :table-table) clist)))
  12181. (goto-char p)
  12182. ;; Now the small context
  12183. (cond
  12184. ((org-at-timestamp-p)
  12185. (push (org-point-in-group p 0 :timestamp) clist))
  12186. ((memq 'org-link faces)
  12187. (push (list :link
  12188. (previous-single-property-change p 'face)
  12189. (next-single-property-change p 'face)) clist))
  12190. ((memq 'org-special-keyword faces)
  12191. (push (list :keyword
  12192. (previous-single-property-change p 'face)
  12193. (next-single-property-change p 'face)) clist))
  12194. ((org-on-target-p)
  12195. (push (org-point-in-group p 0 :target) clist)
  12196. (goto-char (1- (match-beginning 0)))
  12197. (if (looking-at org-radio-target-regexp)
  12198. (push (org-point-in-group p 0 :radio-target) clist))
  12199. (goto-char p))
  12200. ((setq o (car (delq nil
  12201. (mapcar
  12202. (lambda (x)
  12203. (if (memq x org-latex-fragment-image-overlays) x))
  12204. (org-overlays-at (point))))))
  12205. (push (list :latex-fragment
  12206. (org-overlay-start o) (org-overlay-end o)) clist)
  12207. (push (list :latex-preview
  12208. (org-overlay-start o) (org-overlay-end o)) clist))
  12209. ((org-inside-LaTeX-fragment-p)
  12210. ;; FIXME: positions wrong.
  12211. (push (list :latex-fragment (point) (point)) clist)))
  12212. (setq clist (nreverse (delq nil clist)))
  12213. clist))
  12214. ;; FIXME: Compare with at-regexp-p Do we need both?
  12215. (defun org-in-regexp (re &optional nlines visually)
  12216. "Check if point is inside a match of regexp.
  12217. Normally only the current line is checked, but you can include NLINES extra
  12218. lines both before and after point into the search.
  12219. If VISUALLY is set, require that the cursor is not after the match but
  12220. really on, so that the block visually is on the match."
  12221. (catch 'exit
  12222. (let ((pos (point))
  12223. (eol (point-at-eol (+ 1 (or nlines 0))))
  12224. (inc (if visually 1 0)))
  12225. (save-excursion
  12226. (beginning-of-line (- 1 (or nlines 0)))
  12227. (while (re-search-forward re eol t)
  12228. (if (and (<= (match-beginning 0) pos)
  12229. (>= (+ inc (match-end 0)) pos))
  12230. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12231. (defun org-at-regexp-p (regexp)
  12232. "Is point inside a match of REGEXP in the current line?"
  12233. (catch 'exit
  12234. (save-excursion
  12235. (let ((pos (point)) (end (point-at-eol)))
  12236. (beginning-of-line 1)
  12237. (while (re-search-forward regexp end t)
  12238. (if (and (<= (match-beginning 0) pos)
  12239. (>= (match-end 0) pos))
  12240. (throw 'exit t)))
  12241. nil))))
  12242. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12243. "Call `multi-occur' with buffers for all agenda files."
  12244. (interactive "sOrg-files matching: \np")
  12245. (let* ((files (org-agenda-files))
  12246. (tnames (mapcar 'file-truename files))
  12247. (extra org-agenda-text-search-extra-files)
  12248. f)
  12249. (when (eq (car extra) 'agenda-archives)
  12250. (setq extra (cdr extra))
  12251. (setq files (org-add-archive-files files)))
  12252. (while (setq f (pop extra))
  12253. (unless (member (file-truename f) tnames)
  12254. (add-to-list 'files f 'append)
  12255. (add-to-list 'tnames (file-truename f) 'append)))
  12256. (multi-occur
  12257. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12258. regexp)))
  12259. (if (boundp 'occur-mode-find-occurrence-hook)
  12260. ;; Emacs 23
  12261. (add-hook 'occur-mode-find-occurrence-hook
  12262. (lambda ()
  12263. (when (org-mode-p)
  12264. (org-reveal))))
  12265. ;; Emacs 22
  12266. (defadvice occur-mode-goto-occurrence
  12267. (after org-occur-reveal activate)
  12268. (and (org-mode-p) (org-reveal)))
  12269. (defadvice occur-mode-goto-occurrence-other-window
  12270. (after org-occur-reveal activate)
  12271. (and (org-mode-p) (org-reveal)))
  12272. (defadvice occur-mode-display-occurrence
  12273. (after org-occur-reveal activate)
  12274. (when (org-mode-p)
  12275. (let ((pos (occur-mode-find-occurrence)))
  12276. (with-current-buffer (marker-buffer pos)
  12277. (save-excursion
  12278. (goto-char pos)
  12279. (org-reveal)))))))
  12280. (defun org-uniquify (list)
  12281. "Remove duplicate elements from LIST."
  12282. (let (res)
  12283. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12284. res))
  12285. (defun org-delete-all (elts list)
  12286. "Remove all elements in ELTS from LIST."
  12287. (while elts
  12288. (setq list (delete (pop elts) list)))
  12289. list)
  12290. (defun org-back-over-empty-lines ()
  12291. "Move backwards over witespace, to the beginning of the first empty line.
  12292. Returns the number of empty lines passed."
  12293. (let ((pos (point)))
  12294. (skip-chars-backward " \t\n\r")
  12295. (beginning-of-line 2)
  12296. (goto-char (min (point) pos))
  12297. (count-lines (point) pos)))
  12298. (defun org-skip-whitespace ()
  12299. (skip-chars-forward " \t\n\r"))
  12300. (defun org-point-in-group (point group &optional context)
  12301. "Check if POINT is in match-group GROUP.
  12302. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12303. match. If the match group does ot exist or point is not inside it,
  12304. return nil."
  12305. (and (match-beginning group)
  12306. (>= point (match-beginning group))
  12307. (<= point (match-end group))
  12308. (if context
  12309. (list context (match-beginning group) (match-end group))
  12310. t)))
  12311. (defun org-switch-to-buffer-other-window (&rest args)
  12312. "Switch to buffer in a second window on the current frame.
  12313. In particular, do not allow pop-up frames."
  12314. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12315. special-display-function)
  12316. (apply 'switch-to-buffer-other-window args)))
  12317. (defun org-combine-plists (&rest plists)
  12318. "Create a single property list from all plists in PLISTS.
  12319. The process starts by copying the first list, and then setting properties
  12320. from the other lists. Settings in the last list are the most significant
  12321. ones and overrule settings in the other lists."
  12322. (let ((rtn (copy-sequence (pop plists)))
  12323. p v ls)
  12324. (while plists
  12325. (setq ls (pop plists))
  12326. (while ls
  12327. (setq p (pop ls) v (pop ls))
  12328. (setq rtn (plist-put rtn p v))))
  12329. rtn))
  12330. (defun org-move-line-down (arg)
  12331. "Move the current line down. With prefix argument, move it past ARG lines."
  12332. (interactive "p")
  12333. (let ((col (current-column))
  12334. beg end pos)
  12335. (beginning-of-line 1) (setq beg (point))
  12336. (beginning-of-line 2) (setq end (point))
  12337. (beginning-of-line (+ 1 arg))
  12338. (setq pos (move-marker (make-marker) (point)))
  12339. (insert (delete-and-extract-region beg end))
  12340. (goto-char pos)
  12341. (org-move-to-column col)))
  12342. (defun org-move-line-up (arg)
  12343. "Move the current line up. With prefix argument, move it past ARG lines."
  12344. (interactive "p")
  12345. (let ((col (current-column))
  12346. beg end pos)
  12347. (beginning-of-line 1) (setq beg (point))
  12348. (beginning-of-line 2) (setq end (point))
  12349. (beginning-of-line (- arg))
  12350. (setq pos (move-marker (make-marker) (point)))
  12351. (insert (delete-and-extract-region beg end))
  12352. (goto-char pos)
  12353. (org-move-to-column col)))
  12354. (defun org-replace-escapes (string table)
  12355. "Replace %-escapes in STRING with values in TABLE.
  12356. TABLE is an association list with keys like \"%a\" and string values.
  12357. The sequences in STRING may contain normal field width and padding information,
  12358. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12359. so values can contain further %-escapes if they are define later in TABLE."
  12360. (let ((case-fold-search nil)
  12361. e re rpl)
  12362. (while (setq e (pop table))
  12363. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12364. (while (string-match re string)
  12365. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12366. (cdr e)))
  12367. (setq string (replace-match rpl t t string))))
  12368. string))
  12369. (defun org-sublist (list start end)
  12370. "Return a section of LIST, from START to END.
  12371. Counting starts at 1."
  12372. (let (rtn (c start))
  12373. (setq list (nthcdr (1- start) list))
  12374. (while (and list (<= c end))
  12375. (push (pop list) rtn)
  12376. (setq c (1+ c)))
  12377. (nreverse rtn)))
  12378. (defun org-find-base-buffer-visiting (file)
  12379. "Like `find-buffer-visiting' but alway return the base buffer and
  12380. not an indirect buffer."
  12381. (let ((buf (find-buffer-visiting file)))
  12382. (if buf
  12383. (or (buffer-base-buffer buf) buf)
  12384. nil)))
  12385. (defun org-image-file-name-regexp ()
  12386. "Return regexp matching the file names of images."
  12387. (if (fboundp 'image-file-name-regexp)
  12388. (image-file-name-regexp)
  12389. (let ((image-file-name-extensions
  12390. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12391. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12392. (concat "\\."
  12393. (regexp-opt (nconc (mapcar 'upcase
  12394. image-file-name-extensions)
  12395. image-file-name-extensions)
  12396. t)
  12397. "\\'"))))
  12398. (defun org-file-image-p (file)
  12399. "Return non-nil if FILE is an image."
  12400. (save-match-data
  12401. (string-match (org-image-file-name-regexp) file)))
  12402. ;;; Paragraph filling stuff.
  12403. ;; We want this to be just right, so use the full arsenal.
  12404. (defun org-indent-line-function ()
  12405. "Indent line like previous, but further if previous was headline or item."
  12406. (interactive)
  12407. (let* ((pos (point))
  12408. (itemp (org-at-item-p))
  12409. column bpos bcol tpos tcol bullet btype bullet-type)
  12410. ;; Find the previous relevant line
  12411. (beginning-of-line 1)
  12412. (cond
  12413. ((looking-at "#") (setq column 0))
  12414. ((looking-at "\\*+ ") (setq column 0))
  12415. (t
  12416. (beginning-of-line 0)
  12417. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12418. (beginning-of-line 0))
  12419. (cond
  12420. ((looking-at "\\*+[ \t]+")
  12421. (goto-char (match-end 0))
  12422. (setq column (current-column)))
  12423. ((org-in-item-p)
  12424. (org-beginning-of-item)
  12425. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12426. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12427. (setq bpos (match-beginning 1) tpos (match-end 0)
  12428. bcol (progn (goto-char bpos) (current-column))
  12429. tcol (progn (goto-char tpos) (current-column))
  12430. bullet (match-string 1)
  12431. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12432. (if (> tcol (+ bcol org-description-max-indent))
  12433. (setq tcol (+ bcol 5)))
  12434. (if (not itemp)
  12435. (setq column tcol)
  12436. (goto-char pos)
  12437. (beginning-of-line 1)
  12438. (if (looking-at "\\S-")
  12439. (progn
  12440. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12441. (setq bullet (match-string 1)
  12442. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12443. (setq column (if (equal btype bullet-type) bcol tcol)))
  12444. (setq column (org-get-indentation)))))
  12445. (t (setq column (org-get-indentation))))))
  12446. (goto-char pos)
  12447. (if (<= (current-column) (current-indentation))
  12448. (org-indent-line-to column)
  12449. (save-excursion (org-indent-line-to column)))
  12450. (setq column (current-column))
  12451. (beginning-of-line 1)
  12452. (if (looking-at
  12453. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12454. (replace-match (concat "\\1" (format org-property-format
  12455. (match-string 2) (match-string 3)))
  12456. t nil))
  12457. (org-move-to-column column)))
  12458. (defun org-set-autofill-regexps ()
  12459. (interactive)
  12460. ;; In the paragraph separator we include headlines, because filling
  12461. ;; text in a line directly attached to a headline would otherwise
  12462. ;; fill the headline as well.
  12463. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12464. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12465. ;; The paragraph starter includes hand-formatted lists.
  12466. (org-set-local 'paragraph-start
  12467. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12468. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12469. ;; But only if the user has not turned off tables or fixed-width regions
  12470. (org-set-local
  12471. 'auto-fill-inhibit-regexp
  12472. (concat "\\*+ \\|#\\+"
  12473. "\\|[ \t]*" org-keyword-time-regexp
  12474. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12475. (concat
  12476. "\\|[ \t]*["
  12477. (if org-enable-table-editor "|" "")
  12478. (if org-enable-fixed-width-editor ":" "")
  12479. "]"))))
  12480. ;; We use our own fill-paragraph function, to make sure that tables
  12481. ;; and fixed-width regions are not wrapped. That function will pass
  12482. ;; through to `fill-paragraph' when appropriate.
  12483. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12484. ; Adaptive filling: To get full control, first make sure that
  12485. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12486. (org-set-local 'adaptive-fill-regexp "\000")
  12487. (org-set-local 'adaptive-fill-function
  12488. 'org-adaptive-fill-function)
  12489. (org-set-local
  12490. 'align-mode-rules-list
  12491. '((org-in-buffer-settings
  12492. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12493. (modes . '(org-mode))))))
  12494. (defun org-fill-paragraph (&optional justify)
  12495. "Re-align a table, pass through to fill-paragraph if no table."
  12496. (let ((table-p (org-at-table-p))
  12497. (table.el-p (org-at-table.el-p)))
  12498. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12499. (save-excursion (goto-char (point-at-bol))
  12500. (looking-at outline-regexp)))
  12501. t) ; skip headlines
  12502. (table.el-p t) ; skip table.el tables
  12503. (table-p (org-table-align) t) ; align org-mode tables
  12504. (t nil)))) ; call paragraph-fill
  12505. ;; For reference, this is the default value of adaptive-fill-regexp
  12506. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12507. (defun org-adaptive-fill-function ()
  12508. "Return a fill prefix for org-mode files.
  12509. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12510. work correctly."
  12511. (cond ((looking-at "#[ \t]+")
  12512. (match-string 0))
  12513. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12514. (save-excursion
  12515. (if (> (match-end 1) (+ (match-beginning 1)
  12516. org-description-max-indent))
  12517. (goto-char (+ (match-beginning 1) 5))
  12518. (goto-char (match-end 0)))
  12519. (make-string (current-column) ?\ )))
  12520. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12521. (save-excursion
  12522. (goto-char (match-end 0))
  12523. (make-string (current-column) ?\ )))
  12524. (t nil)))
  12525. ;;; Other stuff.
  12526. (defun org-toggle-fixed-width-section (arg)
  12527. "Toggle the fixed-width export.
  12528. If there is no active region, the QUOTE keyword at the current headline is
  12529. inserted or removed. When present, it causes the text between this headline
  12530. and the next to be exported as fixed-width text, and unmodified.
  12531. If there is an active region, this command adds or removes a colon as the
  12532. first character of this line. If the first character of a line is a colon,
  12533. this line is also exported in fixed-width font."
  12534. (interactive "P")
  12535. (let* ((cc 0)
  12536. (regionp (org-region-active-p))
  12537. (beg (if regionp (region-beginning) (point)))
  12538. (end (if regionp (region-end)))
  12539. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12540. (case-fold-search nil)
  12541. (re "[ \t]*\\(:\\)")
  12542. off)
  12543. (if regionp
  12544. (save-excursion
  12545. (goto-char beg)
  12546. (setq cc (current-column))
  12547. (beginning-of-line 1)
  12548. (setq off (looking-at re))
  12549. (while (> nlines 0)
  12550. (setq nlines (1- nlines))
  12551. (beginning-of-line 1)
  12552. (cond
  12553. (arg
  12554. (org-move-to-column cc t)
  12555. (insert ":\n")
  12556. (forward-line -1))
  12557. ((and off (looking-at re))
  12558. (replace-match "" t t nil 1))
  12559. ((not off) (org-move-to-column cc t) (insert ":")))
  12560. (forward-line 1)))
  12561. (save-excursion
  12562. (org-back-to-heading)
  12563. (if (looking-at (concat outline-regexp
  12564. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12565. (replace-match "" t t nil 1)
  12566. (if (looking-at outline-regexp)
  12567. (progn
  12568. (goto-char (match-end 0))
  12569. (insert org-quote-string " "))))))))
  12570. ;;;; Functions extending outline functionality
  12571. (defun org-beginning-of-line (&optional arg)
  12572. "Go to the beginning of the current line. If that is invisible, continue
  12573. to a visible line beginning. This makes the function of C-a more intuitive.
  12574. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12575. first attempt, and only move to after the tags when the cursor is already
  12576. beyond the end of the headline."
  12577. (interactive "P")
  12578. (let ((pos (point)))
  12579. (beginning-of-line 1)
  12580. (if (bobp)
  12581. nil
  12582. (backward-char 1)
  12583. (if (org-invisible-p)
  12584. (while (and (not (bobp)) (org-invisible-p))
  12585. (backward-char 1)
  12586. (beginning-of-line 1))
  12587. (forward-char 1)))
  12588. (when org-special-ctrl-a/e
  12589. (cond
  12590. ((and (looking-at org-todo-line-regexp)
  12591. (= (char-after (match-end 1)) ?\ ))
  12592. (goto-char
  12593. (if (eq org-special-ctrl-a/e t)
  12594. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  12595. ((= pos (point)) (match-beginning 3))
  12596. (t (point)))
  12597. (cond ((> pos (point)) (point))
  12598. ((not (eq last-command this-command)) (point))
  12599. (t (match-beginning 3))))))
  12600. ((org-at-item-p)
  12601. (goto-char
  12602. (if (eq org-special-ctrl-a/e t)
  12603. (cond ((> pos (match-end 4)) (match-end 4))
  12604. ((= pos (point)) (match-end 4))
  12605. (t (point)))
  12606. (cond ((> pos (point)) (point))
  12607. ((not (eq last-command this-command)) (point))
  12608. (t (match-end 4))))))))))
  12609. (defun org-end-of-line (&optional arg)
  12610. "Go to the end of the line.
  12611. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12612. first attempt, and only move to after the tags when the cursor is already
  12613. beyond the end of the headline."
  12614. (interactive "P")
  12615. (if (or (not org-special-ctrl-a/e)
  12616. (not (org-on-heading-p)))
  12617. (end-of-line arg)
  12618. (let ((pos (point)))
  12619. (beginning-of-line 1)
  12620. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12621. (if (eq org-special-ctrl-a/e t)
  12622. (if (or (< pos (match-beginning 1))
  12623. (= pos (match-end 0)))
  12624. (goto-char (match-beginning 1))
  12625. (goto-char (match-end 0)))
  12626. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12627. (goto-char (match-end 0))
  12628. (goto-char (match-beginning 1))))
  12629. (end-of-line arg)))))
  12630. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12631. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12632. (defun org-kill-line (&optional arg)
  12633. "Kill line, to tags or end of line."
  12634. (interactive "P")
  12635. (cond
  12636. ((or (not org-special-ctrl-k)
  12637. (bolp)
  12638. (not (org-on-heading-p)))
  12639. (call-interactively 'kill-line))
  12640. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12641. (kill-region (point) (match-beginning 1))
  12642. (org-set-tags nil t))
  12643. (t (kill-region (point) (point-at-eol)))))
  12644. (define-key org-mode-map "\C-k" 'org-kill-line)
  12645. (defun org-invisible-p ()
  12646. "Check if point is at a character currently not visible."
  12647. ;; Early versions of noutline don't have `outline-invisible-p'.
  12648. (if (fboundp 'outline-invisible-p)
  12649. (outline-invisible-p)
  12650. (get-char-property (point) 'invisible)))
  12651. (defun org-invisible-p2 ()
  12652. "Check if point is at a character currently not visible."
  12653. (save-excursion
  12654. (if (and (eolp) (not (bobp))) (backward-char 1))
  12655. ;; Early versions of noutline don't have `outline-invisible-p'.
  12656. (if (fboundp 'outline-invisible-p)
  12657. (outline-invisible-p)
  12658. (get-char-property (point) 'invisible))))
  12659. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12660. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12661. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12662. (defun org-at-heading-or-item-p ()
  12663. (or (org-on-heading-p) (org-at-item-p)))
  12664. (defun org-on-target-p ()
  12665. (or (org-in-regexp org-radio-target-regexp)
  12666. (org-in-regexp org-target-regexp)))
  12667. (defun org-up-heading-all (arg)
  12668. "Move to the heading line of which the present line is a subheading.
  12669. This function considers both visible and invisible heading lines.
  12670. With argument, move up ARG levels."
  12671. (if (fboundp 'outline-up-heading-all)
  12672. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12673. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12674. (defun org-up-heading-safe ()
  12675. "Move to the heading line of which the present line is a subheading.
  12676. This version will not throw an error. It will return the level of the
  12677. headline found, or nil if no higher level is found."
  12678. (let ((pos (point)) start-level level
  12679. (re (concat "^" outline-regexp)))
  12680. (catch 'exit
  12681. (outline-back-to-heading t)
  12682. (setq start-level (funcall outline-level))
  12683. (if (equal start-level 1) (throw 'exit nil))
  12684. (while (re-search-backward re nil t)
  12685. (setq level (funcall outline-level))
  12686. (if (< level start-level) (throw 'exit level)))
  12687. nil)))
  12688. (defun org-first-sibling-p ()
  12689. "Is this heading the first child of its parents?"
  12690. (interactive)
  12691. (let ((re (concat "^" outline-regexp))
  12692. level l)
  12693. (unless (org-at-heading-p t)
  12694. (error "Not at a heading"))
  12695. (setq level (funcall outline-level))
  12696. (save-excursion
  12697. (if (not (re-search-backward re nil t))
  12698. t
  12699. (setq l (funcall outline-level))
  12700. (< l level)))))
  12701. (defun org-goto-sibling (&optional previous)
  12702. "Goto the next sibling, even if it is invisible.
  12703. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12704. when a sibling was found. When none is found, return nil and don't
  12705. move point."
  12706. (let ((fun (if previous 're-search-backward 're-search-forward))
  12707. (pos (point))
  12708. (re (concat "^" outline-regexp))
  12709. level l)
  12710. (when (condition-case nil (org-back-to-heading t) (error nil))
  12711. (setq level (funcall outline-level))
  12712. (catch 'exit
  12713. (or previous (forward-char 1))
  12714. (while (funcall fun re nil t)
  12715. (setq l (funcall outline-level))
  12716. (when (< l level) (goto-char pos) (throw 'exit nil))
  12717. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12718. (goto-char pos)
  12719. nil))))
  12720. (defun org-show-siblings ()
  12721. "Show all siblings of the current headline."
  12722. (save-excursion
  12723. (while (org-goto-sibling) (org-flag-heading nil)))
  12724. (save-excursion
  12725. (while (org-goto-sibling 'previous)
  12726. (org-flag-heading nil))))
  12727. (defun org-show-hidden-entry ()
  12728. "Show an entry where even the heading is hidden."
  12729. (save-excursion
  12730. (org-show-entry)))
  12731. (defun org-flag-heading (flag &optional entry)
  12732. "Flag the current heading. FLAG non-nil means make invisible.
  12733. When ENTRY is non-nil, show the entire entry."
  12734. (save-excursion
  12735. (org-back-to-heading t)
  12736. ;; Check if we should show the entire entry
  12737. (if entry
  12738. (progn
  12739. (org-show-entry)
  12740. (save-excursion
  12741. (and (outline-next-heading)
  12742. (org-flag-heading nil))))
  12743. (outline-flag-region (max (point-min) (1- (point)))
  12744. (save-excursion (outline-end-of-heading) (point))
  12745. flag))))
  12746. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12747. ;; This is an exact copy of the original function, but it uses
  12748. ;; `org-back-to-heading', to make it work also in invisible
  12749. ;; trees. And is uses an invisible-OK argument.
  12750. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12751. (org-back-to-heading invisible-OK)
  12752. (let ((first t)
  12753. (level (funcall outline-level)))
  12754. (while (and (not (eobp))
  12755. (or first (> (funcall outline-level) level)))
  12756. (setq first nil)
  12757. (outline-next-heading))
  12758. (unless to-heading
  12759. (if (memq (preceding-char) '(?\n ?\^M))
  12760. (progn
  12761. ;; Go to end of line before heading
  12762. (forward-char -1)
  12763. (if (memq (preceding-char) '(?\n ?\^M))
  12764. ;; leave blank line before heading
  12765. (forward-char -1))))))
  12766. (point))
  12767. (defun org-show-subtree ()
  12768. "Show everything after this heading at deeper levels."
  12769. (outline-flag-region
  12770. (point)
  12771. (save-excursion
  12772. (outline-end-of-subtree) (outline-next-heading) (point))
  12773. nil))
  12774. (defun org-show-entry ()
  12775. "Show the body directly following this heading.
  12776. Show the heading too, if it is currently invisible."
  12777. (interactive)
  12778. (save-excursion
  12779. (condition-case nil
  12780. (progn
  12781. (org-back-to-heading t)
  12782. (outline-flag-region
  12783. (max (point-min) (1- (point)))
  12784. (save-excursion
  12785. (re-search-forward
  12786. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12787. (or (match-beginning 1) (point-max)))
  12788. nil))
  12789. (error nil))))
  12790. (defun org-make-options-regexp (kwds)
  12791. "Make a regular expression for keyword lines."
  12792. (concat
  12793. "^"
  12794. "#?[ \t]*\\+\\("
  12795. (mapconcat 'regexp-quote kwds "\\|")
  12796. "\\):[ \t]*"
  12797. "\\(.+\\)"))
  12798. ;; Make isearch reveal the necessary context
  12799. (defun org-isearch-end ()
  12800. "Reveal context after isearch exits."
  12801. (when isearch-success ; only if search was successful
  12802. (if (featurep 'xemacs)
  12803. ;; Under XEmacs, the hook is run in the correct place,
  12804. ;; we directly show the context.
  12805. (org-show-context 'isearch)
  12806. ;; In Emacs the hook runs *before* restoring the overlays.
  12807. ;; So we have to use a one-time post-command-hook to do this.
  12808. ;; (Emacs 22 has a special variable, see function `org-mode')
  12809. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12810. isearch-mode-end-hook-quit)
  12811. ;; Only when the isearch was not quitted.
  12812. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12813. 'append 'local)))))
  12814. (defun org-isearch-post-command ()
  12815. "Remove self from hook, and show context."
  12816. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12817. (org-show-context 'isearch))
  12818. ;;;; Integration with and fixes for other packages
  12819. ;;; Imenu support
  12820. (defvar org-imenu-markers nil
  12821. "All markers currently used by Imenu.")
  12822. (make-variable-buffer-local 'org-imenu-markers)
  12823. (defun org-imenu-new-marker (&optional pos)
  12824. "Return a new marker for use by Imenu, and remember the marker."
  12825. (let ((m (make-marker)))
  12826. (move-marker m (or pos (point)))
  12827. (push m org-imenu-markers)
  12828. m))
  12829. (defun org-imenu-get-tree ()
  12830. "Produce the index for Imenu."
  12831. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12832. (setq org-imenu-markers nil)
  12833. (let* ((n org-imenu-depth)
  12834. (re (concat "^" outline-regexp))
  12835. (subs (make-vector (1+ n) nil))
  12836. (last-level 0)
  12837. m tree level head)
  12838. (save-excursion
  12839. (save-restriction
  12840. (widen)
  12841. (goto-char (point-max))
  12842. (while (re-search-backward re nil t)
  12843. (setq level (org-reduced-level (funcall outline-level)))
  12844. (when (<= level n)
  12845. (looking-at org-complex-heading-regexp)
  12846. (setq head (org-match-string-no-properties 4)
  12847. m (org-imenu-new-marker))
  12848. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12849. (if (>= level last-level)
  12850. (push (cons head m) (aref subs level))
  12851. (push (cons head (aref subs (1+ level))) (aref subs level))
  12852. (loop for i from (1+ level) to n do (aset subs i nil)))
  12853. (setq last-level level)))))
  12854. (aref subs 1)))
  12855. (eval-after-load "imenu"
  12856. '(progn
  12857. (add-hook 'imenu-after-jump-hook
  12858. (lambda () (org-show-context 'org-goto)))))
  12859. ;; Speedbar support
  12860. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12861. "Overlay marking the agenda restriction line in speedbar.")
  12862. (org-overlay-put org-speedbar-restriction-lock-overlay
  12863. 'face 'org-agenda-restriction-lock)
  12864. (org-overlay-put org-speedbar-restriction-lock-overlay
  12865. 'help-echo "Agendas are currently limited to this item.")
  12866. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  12867. (defun org-speedbar-set-agenda-restriction ()
  12868. "Restrict future agenda commands to the location at point in speedbar.
  12869. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  12870. (interactive)
  12871. (require 'org-agenda)
  12872. (let (p m tp np dir txt w)
  12873. (cond
  12874. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12875. 'org-imenu t))
  12876. (setq m (get-text-property p 'org-imenu-marker))
  12877. (save-excursion
  12878. (save-restriction
  12879. (set-buffer (marker-buffer m))
  12880. (goto-char m)
  12881. (org-agenda-set-restriction-lock 'subtree))))
  12882. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12883. 'speedbar-function 'speedbar-find-file))
  12884. (setq tp (previous-single-property-change
  12885. (1+ p) 'speedbar-function)
  12886. np (next-single-property-change
  12887. tp 'speedbar-function)
  12888. dir (speedbar-line-directory)
  12889. txt (buffer-substring-no-properties (or tp (point-min))
  12890. (or np (point-max))))
  12891. (save-excursion
  12892. (save-restriction
  12893. (set-buffer (find-file-noselect
  12894. (let ((default-directory dir))
  12895. (expand-file-name txt))))
  12896. (unless (org-mode-p)
  12897. (error "Cannot restrict to non-Org-mode file"))
  12898. (org-agenda-set-restriction-lock 'file))))
  12899. (t (error "Don't know how to restrict Org-mode's agenda")))
  12900. (org-move-overlay org-speedbar-restriction-lock-overlay
  12901. (point-at-bol) (point-at-eol))
  12902. (setq current-prefix-arg nil)
  12903. (org-agenda-maybe-redo)))
  12904. (eval-after-load "speedbar"
  12905. '(progn
  12906. (speedbar-add-supported-extension ".org")
  12907. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  12908. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  12909. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  12910. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12911. (add-hook 'speedbar-visiting-tag-hook
  12912. (lambda () (org-show-context 'org-goto)))))
  12913. ;;; Fixes and Hacks for problems with other packages
  12914. ;; Make flyspell not check words in links, to not mess up our keymap
  12915. (defun org-mode-flyspell-verify ()
  12916. "Don't let flyspell put overlays at active buttons."
  12917. (not (get-text-property (point) 'keymap)))
  12918. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12919. (eval-after-load "bookmark"
  12920. '(if (boundp 'bookmark-after-jump-hook)
  12921. ;; We can use the hook
  12922. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12923. ;; Hook not available, use advice
  12924. (defadvice bookmark-jump (after org-make-visible activate)
  12925. "Make the position visible."
  12926. (org-bookmark-jump-unhide))))
  12927. (defun org-bookmark-jump-unhide ()
  12928. "Unhide the current position, to show the bookmark location."
  12929. (and (org-mode-p)
  12930. (or (org-invisible-p)
  12931. (save-excursion (goto-char (max (point-min) (1- (point))))
  12932. (org-invisible-p)))
  12933. (org-show-context 'bookmark-jump)))
  12934. ;; Make session.el ignore our circular variable
  12935. (eval-after-load "session"
  12936. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  12937. ;;;; Experimental code
  12938. (defun org-closed-in-range ()
  12939. "Sparse tree of items closed in a certain time range.
  12940. Still experimental, may disappear in the future."
  12941. (interactive)
  12942. ;; Get the time interval from the user.
  12943. (let* ((time1 (time-to-seconds
  12944. (org-read-date nil 'to-time nil "Starting date: ")))
  12945. (time2 (time-to-seconds
  12946. (org-read-date nil 'to-time nil "End date:")))
  12947. ;; callback function
  12948. (callback (lambda ()
  12949. (let ((time
  12950. (time-to-seconds
  12951. (apply 'encode-time
  12952. (org-parse-time-string
  12953. (match-string 1))))))
  12954. ;; check if time in interval
  12955. (and (>= time time1) (<= time time2))))))
  12956. ;; make tree, check each match with the callback
  12957. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  12958. ;;;; Finish up
  12959. (provide 'org)
  12960. (run-hooks 'org-load-hook)
  12961. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  12962. ;;; org.el ends here