org.el 506 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776
  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.02b
  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, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  65. (defvar org-table-formula-constants-local nil
  66. "Local version of `org-table-formula-constants'.")
  67. (make-variable-buffer-local 'org-table-formula-constants-local)
  68. ;;;; Require other packages
  69. (eval-when-compile
  70. (require 'cl)
  71. (require 'gnus-sum)
  72. (require 'calendar))
  73. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  74. ;; the file noutline.el being loaded.
  75. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  76. ;; We require noutline, which might be provided in outline.el
  77. (require 'outline) (require 'noutline)
  78. ;; Other stuff we need.
  79. (require 'time-date)
  80. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  81. (require 'easymenu)
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. (require 'org-faces)
  85. ;;;; Customization variables
  86. ;;; Version
  87. (defconst org-version "6.02b"
  88. "The version number of the file org.el.")
  89. (defun org-version (&optional here)
  90. "Show the org-mode version in the echo area.
  91. With prefix arg HERE, insert it at point."
  92. (interactive "P")
  93. (let ((version (format "Org-mode version %s" org-version)))
  94. (message version)
  95. (if here
  96. (insert version))))
  97. ;;; Compatibility constants
  98. ;;; The custom variables
  99. (defgroup org nil
  100. "Outline-based notes management and organizer."
  101. :tag "Org"
  102. :group 'outlines
  103. :group 'hypermedia
  104. :group 'calendar)
  105. (defcustom org-load-hook nil
  106. "Hook that is run after org.el has been loaded."
  107. :group 'org
  108. :type 'hook)
  109. (defvar org-modules) ; defined below
  110. (defvar org-modules-loaded nil
  111. "Have the modules been loaded already?")
  112. (defun org-load-modules-maybe (&optional force)
  113. "Load all extensions listed in `org-default-extensions'."
  114. (when (or force (not org-modules-loaded))
  115. (mapc (lambda (ext)
  116. (condition-case nil (require ext)
  117. (error (message "Problems while trying to load feature `%s'" ext))))
  118. org-modules)
  119. (setq org-modules-loaded t)))
  120. (defun org-set-modules (var value)
  121. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  122. (set var value)
  123. (when (featurep 'org)
  124. (org-load-modules-maybe 'force)))
  125. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-infojs org-irc org-mew org-mhe org-rmail org-vm org-wl)
  126. "Modules that should always be loaded together with org.el.
  127. If a description starts with <C>, the file is not part of emacs
  128. and loading it will require that you have downloaded and properly installed
  129. the org-mode distribution.
  130. You can also use this system to load external packages (i.e. neither Org
  131. core modules, not modules from the CONTRIB directory). Just add symbols
  132. to the end of the list. If the package is called org-xyz.e, then you need
  133. to add the symbol `xyz', and the package must have a call to
  134. (provide 'org-xyz)"
  135. :group 'org
  136. :set 'org-set-modules
  137. :type
  138. '(set :greedy t
  139. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  140. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  141. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  142. (const :tag " info: Links to Info nodes" org-info)
  143. (const :tag " infojs: Set up Sebastian Rose's JavaScript org-info.js" org-infojs)
  144. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  145. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  146. (const :tag " mew Links to Mew folders/messages" org-mew)
  147. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  148. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  149. (const :tag " vm: Links to VM folders/messages" org-vm)
  150. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  151. (const :tag " mouse: Additional mouse support" org-mouse)
  152. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  153. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  154. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  155. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  156. (const :tag "C eval: Include command output as text" org-eval)
  157. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  158. (const :tag "C id: Global id's for identifying entries" org-id)
  159. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  160. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  161. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  162. (const :tag "C mew: Support for links to messages in Mew" org-mew)
  163. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  164. (const :tag "C registry: A registry for Org links" org-registry)
  165. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  166. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  167. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  168. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  169. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  170. (defgroup org-startup nil
  171. "Options concerning startup of Org-mode."
  172. :tag "Org Startup"
  173. :group 'org)
  174. (defcustom org-startup-folded t
  175. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  176. This can also be configured on a per-file basis by adding one of
  177. the following lines anywhere in the buffer:
  178. #+STARTUP: fold
  179. #+STARTUP: nofold
  180. #+STARTUP: content"
  181. :group 'org-startup
  182. :type '(choice
  183. (const :tag "nofold: show all" nil)
  184. (const :tag "fold: overview" t)
  185. (const :tag "content: all headlines" content)))
  186. (defcustom org-startup-truncated t
  187. "Non-nil means, entering Org-mode will set `truncate-lines'.
  188. This is useful since some lines containing links can be very long and
  189. uninteresting. Also tables look terrible when wrapped."
  190. :group 'org-startup
  191. :type 'boolean)
  192. (defcustom org-startup-align-all-tables nil
  193. "Non-nil means, align all tables when visiting a file.
  194. This is useful when the column width in tables is forced with <N> cookies
  195. in table fields. Such tables will look correct only after the first re-align.
  196. This can also be configured on a per-file basis by adding one of
  197. the following lines anywhere in the buffer:
  198. #+STARTUP: align
  199. #+STARTUP: noalign"
  200. :group 'org-startup
  201. :type 'boolean)
  202. (defcustom org-insert-mode-line-in-empty-file nil
  203. "Non-nil means insert the first line setting Org-mode in empty files.
  204. When the function `org-mode' is called interactively in an empty file, this
  205. normally means that the file name does not automatically trigger Org-mode.
  206. To ensure that the file will always be in Org-mode in the future, a
  207. line enforcing Org-mode will be inserted into the buffer, if this option
  208. has been set."
  209. :group 'org-startup
  210. :type 'boolean)
  211. (defcustom org-replace-disputed-keys nil
  212. "Non-nil means use alternative key bindings for some keys.
  213. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  214. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  215. If you want to use Org-mode together with one of these other modes,
  216. or more generally if you would like to move some Org-mode commands to
  217. other keys, set this variable and configure the keys with the variable
  218. `org-disputed-keys'.
  219. This option is only relevant at load-time of Org-mode, and must be set
  220. *before* org.el is loaded. Changing it requires a restart of Emacs to
  221. become effective."
  222. :group 'org-startup
  223. :type 'boolean)
  224. (if (fboundp 'defvaralias)
  225. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  226. (defcustom org-disputed-keys
  227. '(([(shift up)] . [(meta p)])
  228. ([(shift down)] . [(meta n)])
  229. ([(shift left)] . [(meta -)])
  230. ([(shift right)] . [(meta +)])
  231. ([(control shift right)] . [(meta shift +)])
  232. ([(control shift left)] . [(meta shift -)]))
  233. "Keys for which Org-mode and other modes compete.
  234. This is an alist, cars are the default keys, second element specifies
  235. the alternative to use when `org-replace-disputed-keys' is t.
  236. Keys can be specified in any syntax supported by `define-key'.
  237. The value of this option takes effect only at Org-mode's startup,
  238. therefore you'll have to restart Emacs to apply it after changing."
  239. :group 'org-startup
  240. :type 'alist)
  241. (defun org-key (key)
  242. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  243. Or return the original if not disputed."
  244. (if org-replace-disputed-keys
  245. (let* ((nkey (key-description key))
  246. (x (org-find-if (lambda (x)
  247. (equal (key-description (car x)) nkey))
  248. org-disputed-keys)))
  249. (if x (cdr x) key))
  250. key))
  251. (defun org-find-if (predicate seq)
  252. (catch 'exit
  253. (while seq
  254. (if (funcall predicate (car seq))
  255. (throw 'exit (car seq))
  256. (pop seq)))))
  257. (defun org-defkey (keymap key def)
  258. "Define a key, possibly translated, as returned by `org-key'."
  259. (define-key keymap (org-key key) def))
  260. (defcustom org-ellipsis nil
  261. "The ellipsis to use in the Org-mode outline.
  262. When nil, just use the standard three dots. When a string, use that instead,
  263. When a face, use the standart 3 dots, but with the specified face.
  264. The change affects only Org-mode (which will then use its own display table).
  265. Changing this requires executing `M-x org-mode' in a buffer to become
  266. effective."
  267. :group 'org-startup
  268. :type '(choice (const :tag "Default" nil)
  269. (face :tag "Face" :value org-warning)
  270. (string :tag "String" :value "...#")))
  271. (defvar org-display-table nil
  272. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  273. (defgroup org-keywords nil
  274. "Keywords in Org-mode."
  275. :tag "Org Keywords"
  276. :group 'org)
  277. (defcustom org-deadline-string "DEADLINE:"
  278. "String to mark deadline entries.
  279. A deadline is this string, followed by a time stamp. Should be a word,
  280. terminated by a colon. You can insert a schedule keyword and
  281. a timestamp with \\[org-deadline].
  282. Changes become only effective after restarting Emacs."
  283. :group 'org-keywords
  284. :type 'string)
  285. (defcustom org-scheduled-string "SCHEDULED:"
  286. "String to mark scheduled TODO entries.
  287. A schedule is this string, followed by a time stamp. Should be a word,
  288. terminated by a colon. You can insert a schedule keyword and
  289. a timestamp with \\[org-schedule].
  290. Changes become only effective after restarting Emacs."
  291. :group 'org-keywords
  292. :type 'string)
  293. (defcustom org-closed-string "CLOSED:"
  294. "String used as the prefix for timestamps logging closing a TODO entry."
  295. :group 'org-keywords
  296. :type 'string)
  297. (defcustom org-clock-string "CLOCK:"
  298. "String used as prefix for timestamps clocking work hours on an item."
  299. :group 'org-keywords
  300. :type 'string)
  301. (defcustom org-comment-string "COMMENT"
  302. "Entries starting with this keyword will never be exported.
  303. An entry can be toggled between COMMENT and normal with
  304. \\[org-toggle-comment].
  305. Changes become only effective after restarting Emacs."
  306. :group 'org-keywords
  307. :type 'string)
  308. (defcustom org-quote-string "QUOTE"
  309. "Entries starting with this keyword will be exported in fixed-width font.
  310. Quoting applies only to the text in the entry following the headline, and does
  311. not extend beyond the next headline, even if that is lower level.
  312. An entry can be toggled between QUOTE and normal with
  313. \\[org-toggle-fixed-width-section]."
  314. :group 'org-keywords
  315. :type 'string)
  316. (defconst org-repeat-re
  317. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  318. "Regular expression for specifying repeated events.
  319. After a match, group 1 contains the repeat expression.")
  320. (defgroup org-structure nil
  321. "Options concerning the general structure of Org-mode files."
  322. :tag "Org Structure"
  323. :group 'org)
  324. (defgroup org-reveal-location nil
  325. "Options about how to make context of a location visible."
  326. :tag "Org Reveal Location"
  327. :group 'org-structure)
  328. (defconst org-context-choice
  329. '(choice
  330. (const :tag "Always" t)
  331. (const :tag "Never" nil)
  332. (repeat :greedy t :tag "Individual contexts"
  333. (cons
  334. (choice :tag "Context"
  335. (const agenda)
  336. (const org-goto)
  337. (const occur-tree)
  338. (const tags-tree)
  339. (const link-search)
  340. (const mark-goto)
  341. (const bookmark-jump)
  342. (const isearch)
  343. (const default))
  344. (boolean))))
  345. "Contexts for the reveal options.")
  346. (defcustom org-show-hierarchy-above '((default . t))
  347. "Non-nil means, show full hierarchy when revealing a location.
  348. Org-mode often shows locations in an org-mode file which might have
  349. been invisible before. When this is set, the hierarchy of headings
  350. above the exposed location is shown.
  351. Turning this off for example for sparse trees makes them very compact.
  352. Instead of t, this can also be an alist specifying this option for different
  353. contexts. Valid contexts are
  354. agenda when exposing an entry from the agenda
  355. org-goto when using the command `org-goto' on key C-c C-j
  356. occur-tree when using the command `org-occur' on key C-c /
  357. tags-tree when constructing a sparse tree based on tags matches
  358. link-search when exposing search matches associated with a link
  359. mark-goto when exposing the jump goal of a mark
  360. bookmark-jump when exposing a bookmark location
  361. isearch when exiting from an incremental search
  362. default default for all contexts not set explicitly"
  363. :group 'org-reveal-location
  364. :type org-context-choice)
  365. (defcustom org-show-following-heading '((default . nil))
  366. "Non-nil means, show following heading when revealing a location.
  367. Org-mode often shows locations in an org-mode file which might have
  368. been invisible before. When this is set, the heading following the
  369. match is shown.
  370. Turning this off for example for sparse trees makes them very compact,
  371. but makes it harder to edit the location of the match. In such a case,
  372. use the command \\[org-reveal] to show more context.
  373. Instead of t, this can also be an alist specifying this option for different
  374. contexts. See `org-show-hierarchy-above' for valid contexts."
  375. :group 'org-reveal-location
  376. :type org-context-choice)
  377. (defcustom org-show-siblings '((default . nil) (isearch t))
  378. "Non-nil means, show all sibling heading when revealing a location.
  379. Org-mode often shows locations in an org-mode file which might have
  380. been invisible before. When this is set, the sibling of the current entry
  381. heading are all made visible. If `org-show-hierarchy-above' is t,
  382. the same happens on each level of the hierarchy above the current entry.
  383. By default this is on for the isearch context, off for all other contexts.
  384. Turning this off for example for sparse trees makes them very compact,
  385. but makes it harder to edit the location of the match. In such a case,
  386. use the command \\[org-reveal] to show more context.
  387. Instead of t, this can also be an alist specifying this option for different
  388. contexts. See `org-show-hierarchy-above' for valid contexts."
  389. :group 'org-reveal-location
  390. :type org-context-choice)
  391. (defcustom org-show-entry-below '((default . nil))
  392. "Non-nil means, show the entry below a headline when revealing a location.
  393. Org-mode often shows locations in an org-mode file which might have
  394. been invisible before. When this is set, the text below the headline that is
  395. exposed is also shown.
  396. By default this is off for all contexts.
  397. Instead of t, this can also be an alist specifying this option for different
  398. contexts. See `org-show-hierarchy-above' for valid contexts."
  399. :group 'org-reveal-location
  400. :type org-context-choice)
  401. (defcustom org-indirect-buffer-display 'other-window
  402. "How should indirect tree buffers be displayed?
  403. This applies to indirect buffers created with the commands
  404. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  405. Valid values are:
  406. current-window Display in the current window
  407. other-window Just display in another window.
  408. dedicated-frame Create one new frame, and re-use it each time.
  409. new-frame Make a new frame each time. Note that in this case
  410. previously-made indirect buffers are kept, and you need to
  411. kill these buffers yourself."
  412. :group 'org-structure
  413. :group 'org-agenda-windows
  414. :type '(choice
  415. (const :tag "In current window" current-window)
  416. (const :tag "In current frame, other window" other-window)
  417. (const :tag "Each time a new frame" new-frame)
  418. (const :tag "One dedicated frame" dedicated-frame)))
  419. (defgroup org-cycle nil
  420. "Options concerning visibility cycling in Org-mode."
  421. :tag "Org Cycle"
  422. :group 'org-structure)
  423. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  424. "Names of drawers. Drawers are not opened by cycling on the headline above.
  425. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  426. this:
  427. :DRAWERNAME:
  428. .....
  429. :END:
  430. The drawer \"PROPERTIES\" is special for capturing properties through
  431. the property API.
  432. Drawers can be defined on the per-file basis with a line like:
  433. #+DRAWERS: HIDDEN STATE PROPERTIES"
  434. :group 'org-structure
  435. :type '(repeat (string :tag "Drawer Name")))
  436. (defcustom org-cycle-global-at-bob nil
  437. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  438. This makes it possible to do global cycling without having to use S-TAB or
  439. C-u TAB. For this special case to work, the first line of the buffer
  440. must not be a headline - it may be empty ot some other text. When used in
  441. this way, `org-cycle-hook' is disables temporarily, to make sure the
  442. cursor stays at the beginning of the buffer.
  443. When this option is nil, don't do anything special at the beginning
  444. of the buffer."
  445. :group 'org-cycle
  446. :type 'boolean)
  447. (defcustom org-cycle-emulate-tab t
  448. "Where should `org-cycle' emulate TAB.
  449. nil Never
  450. white Only in completely white lines
  451. whitestart Only at the beginning of lines, before the first non-white char
  452. t Everywhere except in headlines
  453. exc-hl-bol Everywhere except at the start of a headline
  454. If TAB is used in a place where it does not emulate TAB, the current subtree
  455. visibility is cycled."
  456. :group 'org-cycle
  457. :type '(choice (const :tag "Never" nil)
  458. (const :tag "Only in completely white lines" white)
  459. (const :tag "Before first char in a line" whitestart)
  460. (const :tag "Everywhere except in headlines" t)
  461. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  462. ))
  463. (defcustom org-cycle-separator-lines 2
  464. "Number of empty lines needed to keep an empty line between collapsed trees.
  465. If you leave an empty line between the end of a subtree and the following
  466. headline, this empty line is hidden when the subtree is folded.
  467. Org-mode will leave (exactly) one empty line visible if the number of
  468. empty lines is equal or larger to the number given in this variable.
  469. So the default 2 means, at least 2 empty lines after the end of a subtree
  470. are needed to produce free space between a collapsed subtree and the
  471. following headline.
  472. Special case: when 0, never leave empty lines in collapsed view."
  473. :group 'org-cycle
  474. :type 'integer)
  475. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  476. org-cycle-hide-drawers
  477. org-cycle-show-empty-lines
  478. org-optimize-window-after-visibility-change)
  479. "Hook that is run after `org-cycle' has changed the buffer visibility.
  480. The function(s) in this hook must accept a single argument which indicates
  481. the new state that was set by the most recent `org-cycle' command. The
  482. argument is a symbol. After a global state change, it can have the values
  483. `overview', `content', or `all'. After a local state change, it can have
  484. the values `folded', `children', or `subtree'."
  485. :group 'org-cycle
  486. :type 'hook)
  487. (defgroup org-edit-structure nil
  488. "Options concerning structure editing in Org-mode."
  489. :tag "Org Edit Structure"
  490. :group 'org-structure)
  491. (defcustom org-odd-levels-only nil
  492. "Non-nil means, skip even levels and only use odd levels for the outline.
  493. This has the effect that two stars are being added/taken away in
  494. promotion/demotion commands. It also influences how levels are
  495. handled by the exporters.
  496. Changing it requires restart of `font-lock-mode' to become effective
  497. for fontification also in regions already fontified.
  498. You may also set this on a per-file basis by adding one of the following
  499. lines to the buffer:
  500. #+STARTUP: odd
  501. #+STARTUP: oddeven"
  502. :group 'org-edit-structure
  503. :group 'org-font-lock
  504. :type 'boolean)
  505. (defcustom org-adapt-indentation t
  506. "Non-nil means, adapt indentation when promoting and demoting.
  507. When this is set and the *entire* text in an entry is indented, the
  508. indentation is increased by one space in a demotion command, and
  509. decreased by one in a promotion command. If any line in the entry
  510. body starts at column 0, indentation is not changed at all."
  511. :group 'org-edit-structure
  512. :type 'boolean)
  513. (defcustom org-special-ctrl-a/e nil
  514. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  515. When t, `C-a' will bring back the cursor to the beginning of the
  516. headline text, i.e. after the stars and after a possible TODO keyword.
  517. In an item, this will be the position after the bullet.
  518. When the cursor is already at that position, another `C-a' will bring
  519. it to the beginning of the line.
  520. `C-e' will jump to the end of the headline, ignoring the presence of tags
  521. in the headline. A second `C-e' will then jump to the true end of the
  522. line, after any tags.
  523. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  524. and only a directly following, identical keypress will bring the cursor
  525. to the special positions."
  526. :group 'org-edit-structure
  527. :type '(choice
  528. (const :tag "off" nil)
  529. (const :tag "after bullet first" t)
  530. (const :tag "border first" reversed)))
  531. (if (fboundp 'defvaralias)
  532. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  533. (defcustom org-special-ctrl-k nil
  534. "Non-nil means `C-k' will behave specially in headlines.
  535. When nil, `C-k' will call the default `kill-line' command.
  536. When t, the following will happen while the cursor is in the headline:
  537. - When the cursor is at the beginning of a headline, kill the entire
  538. line and possible the folded subtree below the line.
  539. - When in the middle of the headline text, kill the headline up to the tags.
  540. - When after the headline text, kill the tags."
  541. :group 'org-edit-structure
  542. :type 'boolean)
  543. (defcustom org-M-RET-may-split-line '((default . t))
  544. "Non-nil means, M-RET will split the line at the cursor position.
  545. When nil, it will go to the end of the line before making a
  546. new line.
  547. You may also set this option in a different way for different
  548. contexts. Valid contexts are:
  549. headline when creating a new headline
  550. item when creating a new item
  551. table in a table field
  552. default the value to be used for all contexts not explicitly
  553. customized"
  554. :group 'org-structure
  555. :group 'org-table
  556. :type '(choice
  557. (const :tag "Always" t)
  558. (const :tag "Never" nil)
  559. (repeat :greedy t :tag "Individual contexts"
  560. (cons
  561. (choice :tag "Context"
  562. (const headline)
  563. (const item)
  564. (const table)
  565. (const default))
  566. (boolean)))))
  567. (defcustom org-blank-before-new-entry '((heading . nil)
  568. (plain-list-item . nil))
  569. "Should `org-insert-heading' leave a blank line before new heading/item?
  570. The value is an alist, with `heading' and `plain-list-item' as car,
  571. and a boolean flag as cdr."
  572. :group 'org-edit-structure
  573. :type '(list
  574. (cons (const heading) (boolean))
  575. (cons (const plain-list-item) (boolean))))
  576. (defcustom org-insert-heading-hook nil
  577. "Hook being run after inserting a new heading."
  578. :group 'org-edit-structure
  579. :type 'hook)
  580. (defcustom org-enable-fixed-width-editor t
  581. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  582. This currently only means, they are never auto-wrapped.
  583. When nil, such lines will be treated like ordinary lines.
  584. See also the QUOTE keyword."
  585. :group 'org-edit-structure
  586. :type 'boolean)
  587. (defcustom org-goto-auto-isearch t
  588. "Non-nil means, typing characters in org-goto starts incremental search."
  589. :group 'org-edit-structure
  590. :type 'boolean)
  591. (defgroup org-sparse-trees nil
  592. "Options concerning sparse trees in Org-mode."
  593. :tag "Org Sparse Trees"
  594. :group 'org-structure)
  595. (defcustom org-highlight-sparse-tree-matches t
  596. "Non-nil means, highlight all matches that define a sparse tree.
  597. The highlights will automatically disappear the next time the buffer is
  598. changed by an edit command."
  599. :group 'org-sparse-trees
  600. :type 'boolean)
  601. (defcustom org-remove-highlights-with-change t
  602. "Non-nil means, any change to the buffer will remove temporary highlights.
  603. Such highlights are created by `org-occur' and `org-clock-display'.
  604. When nil, `C-c C-c needs to be used to get rid of the highlights.
  605. The highlights created by `org-preview-latex-fragment' always need
  606. `C-c C-c' to be removed."
  607. :group 'org-sparse-trees
  608. :group 'org-time
  609. :type 'boolean)
  610. (defcustom org-occur-hook '(org-first-headline-recenter)
  611. "Hook that is run after `org-occur' has constructed a sparse tree.
  612. This can be used to recenter the window to show as much of the structure
  613. as possible."
  614. :group 'org-sparse-trees
  615. :type 'hook)
  616. (defgroup org-plain-lists nil
  617. "Options concerning plain lists in Org-mode."
  618. :tag "Org Plain lists"
  619. :group 'org-structure)
  620. (defcustom org-cycle-include-plain-lists nil
  621. "Non-nil means, include plain lists into visibility cycling.
  622. This means that during cycling, plain list items will *temporarily* be
  623. interpreted as outline headlines with a level given by 1000+i where i is the
  624. indentation of the bullet. In all other operations, plain list items are
  625. not seen as headlines. For example, you cannot assign a TODO keyword to
  626. such an item."
  627. :group 'org-plain-lists
  628. :type 'boolean)
  629. (defcustom org-plain-list-ordered-item-terminator t
  630. "The character that makes a line with leading number an ordered list item.
  631. Valid values are ?. and ?\). To get both terminators, use t. While
  632. ?. may look nicer, it creates the danger that a line with leading
  633. number may be incorrectly interpreted as an item. ?\) therefore is
  634. the safe choice."
  635. :group 'org-plain-lists
  636. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  637. (const :tag "paren like in \"2)\"" ?\))
  638. (const :tab "both" t)))
  639. (defcustom org-empty-line-terminates-plain-lists nil
  640. "Non-nil means, an empty line ends all plain list levels.
  641. When nil, empty lines are part of the preceeding item."
  642. :group 'org-plain-lists
  643. :type 'boolean)
  644. (defcustom org-auto-renumber-ordered-lists t
  645. "Non-nil means, automatically renumber ordered plain lists.
  646. Renumbering happens when the sequence have been changed with
  647. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  648. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  649. :group 'org-plain-lists
  650. :type 'boolean)
  651. (defcustom org-provide-checkbox-statistics t
  652. "Non-nil means, update checkbox statistics after insert and toggle.
  653. When this is set, checkbox statistics is updated each time you either insert
  654. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  655. with \\[org-ctrl-c-ctrl-c\\]."
  656. :group 'org-plain-lists
  657. :type 'boolean)
  658. (defgroup org-imenu-and-speedbar nil
  659. "Options concerning imenu and speedbar in Org-mode."
  660. :tag "Org Imenu and Speedbar"
  661. :group 'org-structure)
  662. (defcustom org-imenu-depth 2
  663. "The maximum level for Imenu access to Org-mode headlines.
  664. This also applied for speedbar access."
  665. :group 'org-imenu-and-speedbar
  666. :type 'number)
  667. (defgroup org-table nil
  668. "Options concerning tables in Org-mode."
  669. :tag "Org Table"
  670. :group 'org)
  671. (defcustom org-enable-table-editor 'optimized
  672. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  673. When nil, such lines will be treated like ordinary lines.
  674. When equal to the symbol `optimized', the table editor will be optimized to
  675. do the following:
  676. - Automatic overwrite mode in front of whitespace in table fields.
  677. This makes the structure of the table stay in tact as long as the edited
  678. field does not exceed the column width.
  679. - Minimize the number of realigns. Normally, the table is aligned each time
  680. TAB or RET are pressed to move to another field. With optimization this
  681. happens only if changes to a field might have changed the column width.
  682. Optimization requires replacing the functions `self-insert-command',
  683. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  684. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  685. very good at guessing when a re-align will be necessary, but you can always
  686. force one with \\[org-ctrl-c-ctrl-c].
  687. If you would like to use the optimized version in Org-mode, but the
  688. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  689. This variable can be used to turn on and off the table editor during a session,
  690. but in order to toggle optimization, a restart is required.
  691. See also the variable `org-table-auto-blank-field'."
  692. :group 'org-table
  693. :type '(choice
  694. (const :tag "off" nil)
  695. (const :tag "on" t)
  696. (const :tag "on, optimized" optimized)))
  697. (defcustom org-table-tab-recognizes-table.el t
  698. "Non-nil means, TAB will automatically notice a table.el table.
  699. When it sees such a table, it moves point into it and - if necessary -
  700. calls `table-recognize-table'."
  701. :group 'org-table-editing
  702. :type 'boolean)
  703. (defgroup org-link nil
  704. "Options concerning links in Org-mode."
  705. :tag "Org Link"
  706. :group 'org)
  707. (defvar org-link-abbrev-alist-local nil
  708. "Buffer-local version of `org-link-abbrev-alist', which see.
  709. The value of this is taken from the #+LINK lines.")
  710. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  711. (defcustom org-link-abbrev-alist nil
  712. "Alist of link abbreviations.
  713. The car of each element is a string, to be replaced at the start of a link.
  714. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  715. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  716. [[linkkey:tag][description]]
  717. If REPLACE is a string, the tag will simply be appended to create the link.
  718. If the string contains \"%s\", the tag will be inserted there.
  719. REPLACE may also be a function that will be called with the tag as the
  720. only argument to create the link, which should be returned as a string.
  721. See the manual for examples."
  722. :group 'org-link
  723. :type 'alist)
  724. (defcustom org-descriptive-links t
  725. "Non-nil means, hide link part and only show description of bracket links.
  726. Bracket links are like [[link][descritpion]]. This variable sets the initial
  727. state in new org-mode buffers. The setting can then be toggled on a
  728. per-buffer basis from the Org->Hyperlinks menu."
  729. :group 'org-link
  730. :type 'boolean)
  731. (defcustom org-link-file-path-type 'adaptive
  732. "How the path name in file links should be stored.
  733. Valid values are:
  734. relative Relative to the current directory, i.e. the directory of the file
  735. into which the link is being inserted.
  736. absolute Absolute path, if possible with ~ for home directory.
  737. noabbrev Absolute path, no abbreviation of home directory.
  738. adaptive Use relative path for files in the current directory and sub-
  739. directories of it. For other files, use an absolute path."
  740. :group 'org-link
  741. :type '(choice
  742. (const relative)
  743. (const absolute)
  744. (const noabbrev)
  745. (const adaptive)))
  746. (defcustom org-activate-links '(bracket angle plain radio tag date)
  747. "Types of links that should be activated in Org-mode files.
  748. This is a list of symbols, each leading to the activation of a certain link
  749. type. In principle, it does not hurt to turn on most link types - there may
  750. be a small gain when turning off unused link types. The types are:
  751. bracket The recommended [[link][description]] or [[link]] links with hiding.
  752. angular Links in angular brackes that may contain whitespace like
  753. <bbdb:Carsten Dominik>.
  754. plain Plain links in normal text, no whitespace, like http://google.com.
  755. radio Text that is matched by a radio target, see manual for details.
  756. tag Tag settings in a headline (link to tag search).
  757. date Time stamps (link to calendar).
  758. Changing this variable requires a restart of Emacs to become effective."
  759. :group 'org-link
  760. :type '(set (const :tag "Double bracket links (new style)" bracket)
  761. (const :tag "Angular bracket links (old style)" angular)
  762. (const :tag "Plain text links" plain)
  763. (const :tag "Radio target matches" radio)
  764. (const :tag "Tags" tag)
  765. (const :tag "Timestamps" date)))
  766. (defcustom org-make-link-description-function nil
  767. "Function to use to generate link descriptions from links. If
  768. nil the link location will be used. This function must take two
  769. parameters; the first is the link and the second the description
  770. org-insert-link has generated, and should return the description
  771. to use."
  772. :group 'org-link
  773. :type 'function)
  774. (defgroup org-link-store nil
  775. "Options concerning storing links in Org-mode."
  776. :tag "Org Store Link"
  777. :group 'org-link)
  778. (defcustom org-email-link-description-format "Email %c: %.30s"
  779. "Format of the description part of a link to an email or usenet message.
  780. The following %-excapes will be replaced by corresponding information:
  781. %F full \"From\" field
  782. %f name, taken from \"From\" field, address if no name
  783. %T full \"To\" field
  784. %t first name in \"To\" field, address if no name
  785. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  786. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  787. %s subject
  788. %m message-id.
  789. You may use normal field width specification between the % and the letter.
  790. This is for example useful to limit the length of the subject.
  791. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  792. :group 'org-link-store
  793. :type 'string)
  794. (defcustom org-from-is-user-regexp
  795. (let (r1 r2)
  796. (when (and user-mail-address (not (string= user-mail-address "")))
  797. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  798. (when (and user-full-name (not (string= user-full-name "")))
  799. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  800. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  801. "Regexp mached against the \"From:\" header of an email or usenet message.
  802. It should match if the message is from the user him/herself."
  803. :group 'org-link-store
  804. :type 'regexp)
  805. (defcustom org-context-in-file-links t
  806. "Non-nil means, file links from `org-store-link' contain context.
  807. A search string will be added to the file name with :: as separator and
  808. used to find the context when the link is activated by the command
  809. `org-open-at-point'.
  810. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  811. negates this setting for the duration of the command."
  812. :group 'org-link-store
  813. :type 'boolean)
  814. (defcustom org-keep-stored-link-after-insertion nil
  815. "Non-nil means, keep link in list for entire session.
  816. The command `org-store-link' adds a link pointing to the current
  817. location to an internal list. These links accumulate during a session.
  818. The command `org-insert-link' can be used to insert links into any
  819. Org-mode file (offering completion for all stored links). When this
  820. option is nil, every link which has been inserted once using \\[org-insert-link]
  821. will be removed from the list, to make completing the unused links
  822. more efficient."
  823. :group 'org-link-store
  824. :type 'boolean)
  825. (defgroup org-link-follow nil
  826. "Options concerning following links in Org-mode."
  827. :tag "Org Follow Link"
  828. :group 'org-link)
  829. (defcustom org-follow-link-hook nil
  830. "Hook that is run after a link has been followed."
  831. :group 'org-link-follow
  832. :type 'hook)
  833. (defcustom org-tab-follows-link nil
  834. "Non-nil means, on links TAB will follow the link.
  835. Needs to be set before org.el is loaded."
  836. :group 'org-link-follow
  837. :type 'boolean)
  838. (defcustom org-return-follows-link nil
  839. "Non-nil means, on links RET will follow the link.
  840. Needs to be set before org.el is loaded."
  841. :group 'org-link-follow
  842. :type 'boolean)
  843. (defcustom org-mouse-1-follows-link
  844. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  845. "Non-nil means, mouse-1 on a link will follow the link.
  846. A longer mouse click will still set point. Does not work on XEmacs.
  847. Needs to be set before org.el is loaded."
  848. :group 'org-link-follow
  849. :type 'boolean)
  850. (defcustom org-mark-ring-length 4
  851. "Number of different positions to be recorded in the ring
  852. Changing this requires a restart of Emacs to work correctly."
  853. :group 'org-link-follow
  854. :type 'interger)
  855. (defcustom org-link-frame-setup
  856. '((vm . vm-visit-folder-other-frame)
  857. (gnus . gnus-other-frame)
  858. (file . find-file-other-window))
  859. "Setup the frame configuration for following links.
  860. When following a link with Emacs, it may often be useful to display
  861. this link in another window or frame. This variable can be used to
  862. set this up for the different types of links.
  863. For VM, use any of
  864. `vm-visit-folder'
  865. `vm-visit-folder-other-frame'
  866. For Gnus, use any of
  867. `gnus'
  868. `gnus-other-frame'
  869. For FILE, use any of
  870. `find-file'
  871. `find-file-other-window'
  872. `find-file-other-frame'
  873. For the calendar, use the variable `calendar-setup'.
  874. For BBDB, it is currently only possible to display the matches in
  875. another window."
  876. :group 'org-link-follow
  877. :type '(list
  878. (cons (const vm)
  879. (choice
  880. (const vm-visit-folder)
  881. (const vm-visit-folder-other-window)
  882. (const vm-visit-folder-other-frame)))
  883. (cons (const gnus)
  884. (choice
  885. (const gnus)
  886. (const gnus-other-frame)))
  887. (cons (const file)
  888. (choice
  889. (const find-file)
  890. (const find-file-other-window)
  891. (const find-file-other-frame)))))
  892. (defcustom org-display-internal-link-with-indirect-buffer nil
  893. "Non-nil means, use indirect buffer to display infile links.
  894. Activating internal links (from one location in a file to another location
  895. in the same file) normally just jumps to the location. When the link is
  896. activated with a C-u prefix (or with mouse-3), the link is displayed in
  897. another window. When this option is set, the other window actually displays
  898. an indirect buffer clone of the current buffer, to avoid any visibility
  899. changes to the current buffer."
  900. :group 'org-link-follow
  901. :type 'boolean)
  902. (defcustom org-open-non-existing-files nil
  903. "Non-nil means, `org-open-file' will open non-existing files.
  904. When nil, an error will be generated."
  905. :group 'org-link-follow
  906. :type 'boolean)
  907. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  908. "Function and arguments to call for following mailto links.
  909. This is a list with the first element being a lisp function, and the
  910. remaining elements being arguments to the function. In string arguments,
  911. %a will be replaced by the address, and %s will be replaced by the subject
  912. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  913. :group 'org-link-follow
  914. :type '(choice
  915. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  916. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  917. (const :tag "message-mail" (message-mail "%a" "%s"))
  918. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  919. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  920. "Non-nil means, ask for confirmation before executing shell links.
  921. Shell links can be dangerous: just think about a link
  922. [[shell:rm -rf ~/*][Google Search]]
  923. This link would show up in your Org-mode document as \"Google Search\",
  924. but really it would remove your entire home directory.
  925. Therefore we advise against setting this variable to nil.
  926. Just change it to `y-or-n-p' of you want to confirm with a
  927. single keystroke rather than having to type \"yes\"."
  928. :group 'org-link-follow
  929. :type '(choice
  930. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  931. (const :tag "with y-or-n (faster)" y-or-n-p)
  932. (const :tag "no confirmation (dangerous)" nil)))
  933. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  934. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  935. Elisp links can be dangerous: just think about a link
  936. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  937. This link would show up in your Org-mode document as \"Google Search\",
  938. but really it would remove your entire home directory.
  939. Therefore we advise against setting this variable to nil.
  940. Just change it to `y-or-n-p' of you want to confirm with a
  941. single keystroke rather than having to type \"yes\"."
  942. :group 'org-link-follow
  943. :type '(choice
  944. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  945. (const :tag "with y-or-n (faster)" y-or-n-p)
  946. (const :tag "no confirmation (dangerous)" nil)))
  947. (defconst org-file-apps-defaults-gnu
  948. '((remote . emacs)
  949. (t . mailcap))
  950. "Default file applications on a UNIX or GNU/Linux system.
  951. See `org-file-apps'.")
  952. (defconst org-file-apps-defaults-macosx
  953. '((remote . emacs)
  954. (t . "open %s")
  955. ("ps" . "gv %s")
  956. ("ps.gz" . "gv %s")
  957. ("eps" . "gv %s")
  958. ("eps.gz" . "gv %s")
  959. ("dvi" . "xdvi %s")
  960. ("fig" . "xfig %s"))
  961. "Default file applications on a MacOS X system.
  962. The system \"open\" is known as a default, but we use X11 applications
  963. for some files for which the OS does not have a good default.
  964. See `org-file-apps'.")
  965. (defconst org-file-apps-defaults-windowsnt
  966. (list
  967. '(remote . emacs)
  968. (cons t
  969. (list (if (featurep 'xemacs)
  970. 'mswindows-shell-execute
  971. 'w32-shell-execute)
  972. "open" 'file)))
  973. "Default file applications on a Windows NT system.
  974. The system \"open\" is used for most files.
  975. See `org-file-apps'.")
  976. (defcustom org-file-apps
  977. '(
  978. ("txt" . emacs)
  979. ("tex" . emacs)
  980. ("ltx" . emacs)
  981. ("org" . emacs)
  982. ("el" . emacs)
  983. ("bib" . emacs)
  984. )
  985. "External applications for opening `file:path' items in a document.
  986. Org-mode uses system defaults for different file types, but
  987. you can use this variable to set the application for a given file
  988. extension. The entries in this list are cons cells where the car identifies
  989. files and the cdr the corresponding command. Possible values for the
  990. file identifier are
  991. \"ext\" A string identifying an extension
  992. `directory' Matches a directory
  993. `remote' Matches a remote file, accessible through tramp or efs.
  994. Remote files most likely should be visited through Emacs
  995. because external applications cannot handle such paths.
  996. t Default for all remaining files
  997. Possible values for the command are:
  998. `emacs' The file will be visited by the current Emacs process.
  999. `default' Use the default application for this file type.
  1000. string A command to be executed by a shell; %s will be replaced
  1001. by the path to the file.
  1002. sexp A Lisp form which will be evaluated. The file path will
  1003. be available in the Lisp variable `file'.
  1004. For more examples, see the system specific constants
  1005. `org-file-apps-defaults-macosx'
  1006. `org-file-apps-defaults-windowsnt'
  1007. `org-file-apps-defaults-gnu'."
  1008. :group 'org-link-follow
  1009. :type '(repeat
  1010. (cons (choice :value ""
  1011. (string :tag "Extension")
  1012. (const :tag "Default for unrecognized files" t)
  1013. (const :tag "Remote file" remote)
  1014. (const :tag "Links to a directory" directory))
  1015. (choice :value ""
  1016. (const :tag "Visit with Emacs" emacs)
  1017. (const :tag "Use system default" default)
  1018. (string :tag "Command")
  1019. (sexp :tag "Lisp form")))))
  1020. (defgroup org-refile nil
  1021. "Options concerning refiling entries in Org-mode."
  1022. :tag "Org Remember"
  1023. :group 'org)
  1024. (defcustom org-directory "~/org"
  1025. "Directory with org files.
  1026. This directory will be used as default to prompt for org files.
  1027. Used by the hooks for remember.el."
  1028. :group 'org-refile
  1029. :group 'org-remember
  1030. :type 'directory)
  1031. (defcustom org-default-notes-file "~/.notes"
  1032. "Default target for storing notes.
  1033. Used by the hooks for remember.el. This can be a string, or nil to mean
  1034. the value of `remember-data-file'.
  1035. You can set this on a per-template basis with the variable
  1036. `org-remember-templates'."
  1037. :group 'org-refile
  1038. :group 'org-remember
  1039. :type '(choice
  1040. (const :tag "Default from remember-data-file" nil)
  1041. file))
  1042. (defcustom org-goto-interface 'outline
  1043. "The default interface to be used for `org-goto'.
  1044. Allowed vaues are:
  1045. outline The interface shows an outline of the relevant file
  1046. and the correct heading is found by moving through
  1047. the outline or by searching with incremental search.
  1048. outline-path-completion Headlines in the current buffer are offered via
  1049. completion."
  1050. :group 'org-refile
  1051. :type '(choice
  1052. (const :tag "Outline" outline)
  1053. (const :tag "Outline-path-completion" outline-path-completion)))
  1054. (defcustom org-reverse-note-order nil
  1055. "Non-nil means, store new notes at the beginning of a file or entry.
  1056. When nil, new notes will be filed to the end of a file or entry.
  1057. This can also be a list with cons cells of regular expressions that
  1058. are matched against file names, and values."
  1059. :group 'org-remember
  1060. :type '(choice
  1061. (const :tag "Reverse always" t)
  1062. (const :tag "Reverse never" nil)
  1063. (repeat :tag "By file name regexp"
  1064. (cons regexp boolean))))
  1065. (defcustom org-refile-targets nil
  1066. "Targets for refiling entries with \\[org-refile].
  1067. This is list of cons cells. Each cell contains:
  1068. - a specification of the files to be considered, either a list of files,
  1069. or a symbol whose function or variable value will be used to retrieve
  1070. a file name or a list of file names. Nil means, refile to a different
  1071. heading in the current buffer.
  1072. - A specification of how to find candidate refile targets. This may be
  1073. any of
  1074. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1075. This tag has to be present in all target headlines, inheritance will
  1076. not be considered.
  1077. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1078. todo keyword.
  1079. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1080. headlines that are refiling targets.
  1081. - a cons cell (:level . N). Any headline of level N is considered a target.
  1082. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1083. :group 'org-remember
  1084. :type '(repeat
  1085. (cons
  1086. (choice :value org-agenda-files
  1087. (const :tag "All agenda files" org-agenda-files)
  1088. (const :tag "Current buffer" nil)
  1089. (function) (variable) (file))
  1090. (choice :tag "Identify target headline by"
  1091. (cons :tag "Specific tag" (const :tag) (string))
  1092. (cons :tag "TODO keyword" (const :todo) (string))
  1093. (cons :tag "Regular expression" (const :regexp) (regexp))
  1094. (cons :tag "Level number" (const :level) (integer))
  1095. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1096. (defcustom org-refile-use-outline-path nil
  1097. "Non-nil means, provide refile targets as paths.
  1098. So a level 3 headline will be available as level1/level2/level3.
  1099. When the value is `file', also include the file name (without directory)
  1100. into the path. When `full-file-path', include the full file path."
  1101. :group 'org-remember
  1102. :type '(choice
  1103. (const :tag "Not" nil)
  1104. (const :tag "Yes" t)
  1105. (const :tag "Start with file name" file)
  1106. (const :tag "Start with full file path" full-file-path)))
  1107. (defgroup org-todo nil
  1108. "Options concerning TODO items in Org-mode."
  1109. :tag "Org TODO"
  1110. :group 'org)
  1111. (defgroup org-progress nil
  1112. "Options concerning Progress logging in Org-mode."
  1113. :tag "Org Progress"
  1114. :group 'org-time)
  1115. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1116. "List of TODO entry keyword sequences and their interpretation.
  1117. \\<org-mode-map>This is a list of sequences.
  1118. Each sequence starts with a symbol, either `sequence' or `type',
  1119. indicating if the keywords should be interpreted as a sequence of
  1120. action steps, or as different types of TODO items. The first
  1121. keywords are states requiring action - these states will select a headline
  1122. for inclusion into the global TODO list Org-mode produces. If one of
  1123. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1124. signify that no further action is necessary. If \"|\" is not found,
  1125. the last keyword is treated as the only DONE state of the sequence.
  1126. The command \\[org-todo] cycles an entry through these states, and one
  1127. additional state where no keyword is present. For details about this
  1128. cycling, see the manual.
  1129. TODO keywords and interpretation can also be set on a per-file basis with
  1130. the special #+SEQ_TODO and #+TYP_TODO lines.
  1131. Each keyword can optionally specify a character for fast state selection
  1132. \(in combination with the variable `org-use-fast-todo-selection')
  1133. and specifiers for state change logging, using the same syntax
  1134. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1135. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1136. indicates to record a time stamp each time this state is selected.
  1137. Each keyword may also specify if a timestamp or a note should be
  1138. recorded when entering or leaving the state, by adding additional
  1139. characters in the parenthesis after the keyword. This looks like this:
  1140. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1141. record only the time of the state change. With X and Y being either
  1142. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1143. Y when leaving the state if and only if the *target* state does not
  1144. define X. You may omit any of the fast-selection key or X or /Y,
  1145. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1146. For backward compatibility, this variable may also be just a list
  1147. of keywords - in this case the interptetation (sequence or type) will be
  1148. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1149. :group 'org-todo
  1150. :group 'org-keywords
  1151. :type '(choice
  1152. (repeat :tag "Old syntax, just keywords"
  1153. (string :tag "Keyword"))
  1154. (repeat :tag "New syntax"
  1155. (cons
  1156. (choice
  1157. :tag "Interpretation"
  1158. (const :tag "Sequence (cycling hits every state)" sequence)
  1159. (const :tag "Type (cycling directly to DONE)" type))
  1160. (repeat
  1161. (string :tag "Keyword"))))))
  1162. (defvar org-todo-keywords-1 nil
  1163. "All TODO and DONE keywords active in a buffer.")
  1164. (make-variable-buffer-local 'org-todo-keywords-1)
  1165. (defvar org-todo-keywords-for-agenda nil)
  1166. (defvar org-done-keywords-for-agenda nil)
  1167. (defvar org-agenda-contributing-files nil)
  1168. (defvar org-not-done-keywords nil)
  1169. (make-variable-buffer-local 'org-not-done-keywords)
  1170. (defvar org-done-keywords nil)
  1171. (make-variable-buffer-local 'org-done-keywords)
  1172. (defvar org-todo-heads nil)
  1173. (make-variable-buffer-local 'org-todo-heads)
  1174. (defvar org-todo-sets nil)
  1175. (make-variable-buffer-local 'org-todo-sets)
  1176. (defvar org-todo-log-states nil)
  1177. (make-variable-buffer-local 'org-todo-log-states)
  1178. (defvar org-todo-kwd-alist nil)
  1179. (make-variable-buffer-local 'org-todo-kwd-alist)
  1180. (defvar org-todo-key-alist nil)
  1181. (make-variable-buffer-local 'org-todo-key-alist)
  1182. (defvar org-todo-key-trigger nil)
  1183. (make-variable-buffer-local 'org-todo-key-trigger)
  1184. (defcustom org-todo-interpretation 'sequence
  1185. "Controls how TODO keywords are interpreted.
  1186. This variable is in principle obsolete and is only used for
  1187. backward compatibility, if the interpretation of todo keywords is
  1188. not given already in `org-todo-keywords'. See that variable for
  1189. more information."
  1190. :group 'org-todo
  1191. :group 'org-keywords
  1192. :type '(choice (const sequence)
  1193. (const type)))
  1194. (defcustom org-use-fast-todo-selection 'prefix
  1195. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1196. This variable describes if and under what circumstances the cycling
  1197. mechanism for TODO keywords will be replaced by a single-key, direct
  1198. selection scheme.
  1199. When nil, fast selection is never used.
  1200. When the symbol `prefix', it will be used when `org-todo' is called with
  1201. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1202. in an agenda buffer.
  1203. When t, fast selection is used by default. In this case, the prefix
  1204. argument forces cycling instead.
  1205. In all cases, the special interface is only used if access keys have actually
  1206. been assigned by the user, i.e. if keywords in the configuration are followed
  1207. by a letter in parenthesis, like TODO(t)."
  1208. :group 'org-todo
  1209. :type '(choice
  1210. (const :tag "Never" nil)
  1211. (const :tag "By default" t)
  1212. (const :tag "Only with C-u C-c C-t" prefix)))
  1213. (defcustom org-after-todo-state-change-hook nil
  1214. "Hook which is run after the state of a TODO item was changed.
  1215. The new state (a string with a TODO keyword, or nil) is available in the
  1216. Lisp variable `state'."
  1217. :group 'org-todo
  1218. :type 'hook)
  1219. (defcustom org-log-done nil
  1220. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1221. When equal to the list (done), also prompt for a closing note.
  1222. This can also be configured on a per-file basis by adding one of
  1223. the following lines anywhere in the buffer:
  1224. #+STARTUP: logdone
  1225. #+STARTUP: lognotedone
  1226. #+STARTUP: nologdone"
  1227. :group 'org-todo
  1228. :group 'org-progress
  1229. :type '(choice
  1230. (const :tag "No logging" nil)
  1231. (const :tag "Record CLOSED timestamp" time)
  1232. (const :tag "Record CLOSED timestamp with closing note." note)))
  1233. ;; Normalize old uses of org-log-done.
  1234. (cond
  1235. ((eq org-log-done t) (setq org-log-done 'time))
  1236. ((and (listp org-log-done) (memq 'done org-log-done))
  1237. (setq org-log-done 'note)))
  1238. (defcustom org-log-note-clock-out nil
  1239. "Non-nil means, recored a note when clocking out of an item.
  1240. This can also be configured on a per-file basis by adding one of
  1241. the following lines anywhere in the buffer:
  1242. #+STARTUP: lognoteclock-out
  1243. #+STARTUP: nolognoteclock-out"
  1244. :group 'org-todo
  1245. :group 'org-progress
  1246. :type 'boolean)
  1247. (defcustom org-log-done-with-time t
  1248. "Non-nil means, the CLOSED time stamp will contain date and time.
  1249. When nil, only the date will be recorded."
  1250. :group 'org-progress
  1251. :type 'boolean)
  1252. (defcustom org-log-note-headings
  1253. '((done . "CLOSING NOTE %t")
  1254. (state . "State %-12s %t")
  1255. (note . "Note taken on %t")
  1256. (clock-out . ""))
  1257. "Headings for notes added to entries.
  1258. The value is an alist, with the car being a symbol indicating the note
  1259. context, and the cdr is the heading to be used. The heading may also be the
  1260. empty string.
  1261. %t in the heading will be replaced by a time stamp.
  1262. %s will be replaced by the new TODO state, in double quotes.
  1263. %u will be replaced by the user name.
  1264. %U will be replaced by the full user name."
  1265. :group 'org-todo
  1266. :group 'org-progress
  1267. :type '(list :greedy t
  1268. (cons (const :tag "Heading when closing an item" done) string)
  1269. (cons (const :tag
  1270. "Heading when changing todo state (todo sequence only)"
  1271. state) string)
  1272. (cons (const :tag "Heading when just taking a note" note) string)
  1273. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1274. (unless (assq 'note org-log-note-headings)
  1275. (push '(note . "%t") org-log-note-headings))
  1276. (defcustom org-log-states-order-reversed t
  1277. "Non-nil means, the latest state change note will be directly after heading.
  1278. When nil, the notes will be orderer according to time."
  1279. :group 'org-todo
  1280. :group 'org-progress
  1281. :type 'boolean)
  1282. (defcustom org-log-repeat 'time
  1283. "Non-nil means, record moving through the DONE state when triggering repeat.
  1284. An auto-repeating tasks is immediately switched back to TODO when marked
  1285. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1286. the TODO keyword definition, or recording a closing note by setting
  1287. `org-log-done', there will be no record of the task moving through DONE.
  1288. This variable forces taking a note anyway. Possible values are:
  1289. nil Don't force a record
  1290. time Record a time stamp
  1291. note Record a note
  1292. This option can also be set with on a per-file-basis with
  1293. #+STARTUP: logrepeat
  1294. #+STARTUP: lognoterepeat
  1295. #+STARTUP: nologrepeat
  1296. You can have local logging settings for a subtree by setting the LOGGING
  1297. property to one or more of these keywords."
  1298. :group 'org-todo
  1299. :group 'org-progress
  1300. :type '(choice
  1301. (const :tag "Don't force a record" nil)
  1302. (const :tag "Force recording the DONE state" time)
  1303. (const :tag "Force recording a note with the DONE state" note)))
  1304. (defgroup org-priorities nil
  1305. "Priorities in Org-mode."
  1306. :tag "Org Priorities"
  1307. :group 'org-todo)
  1308. (defcustom org-highest-priority ?A
  1309. "The highest priority of TODO items. A character like ?A, ?B etc.
  1310. Must have a smaller ASCII number than `org-lowest-priority'."
  1311. :group 'org-priorities
  1312. :type 'character)
  1313. (defcustom org-lowest-priority ?C
  1314. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1315. Must have a larger ASCII number than `org-highest-priority'."
  1316. :group 'org-priorities
  1317. :type 'character)
  1318. (defcustom org-default-priority ?B
  1319. "The default priority of TODO items.
  1320. This is the priority an item get if no explicit priority is given."
  1321. :group 'org-priorities
  1322. :type 'character)
  1323. (defcustom org-priority-start-cycle-with-default t
  1324. "Non-nil means, start with default priority when starting to cycle.
  1325. When this is nil, the first step in the cycle will be (depending on the
  1326. command used) one higher or lower that the default priority."
  1327. :group 'org-priorities
  1328. :type 'boolean)
  1329. (defgroup org-time nil
  1330. "Options concerning time stamps and deadlines in Org-mode."
  1331. :tag "Org Time"
  1332. :group 'org)
  1333. (defcustom org-insert-labeled-timestamps-at-point nil
  1334. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1335. When nil, these labeled time stamps are forces into the second line of an
  1336. entry, just after the headline. When scheduling from the global TODO list,
  1337. the time stamp will always be forced into the second line."
  1338. :group 'org-time
  1339. :type 'boolean)
  1340. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1341. "Formats for `format-time-string' which are used for time stamps.
  1342. It is not recommended to change this constant.")
  1343. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1344. "Number of minutes to round time stamps to.
  1345. These are two values, the first applies when first creating a time stamp.
  1346. The second applies when changing it with the commands `S-up' and `S-down'.
  1347. When changing the time stamp, this means that it will change in steps
  1348. of N minutes, as given by the second value.
  1349. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1350. numbers should be factors of 60, so for example 5, 10, 15.
  1351. When this is larger than 1, you can still force an exact time-stamp by using
  1352. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1353. and by using a prefix arg to `S-up/down' to specify the exact number
  1354. of minutes to shift."
  1355. :group 'org-time
  1356. :get '(lambda (var) ; Make sure all entries have 5 elements
  1357. (if (integerp (default-value var))
  1358. (list (default-value var) 5)
  1359. (default-value var)))
  1360. :type '(list
  1361. (integer :tag "when inserting times")
  1362. (integer :tag "when modifying times")))
  1363. ;; Normalize old customizations of this variable.
  1364. (when (integerp org-time-stamp-rounding-minutes)
  1365. (setq org-time-stamp-rounding-minutes
  1366. (list org-time-stamp-rounding-minutes
  1367. org-time-stamp-rounding-minutes)))
  1368. (defcustom org-display-custom-times nil
  1369. "Non-nil means, overlay custom formats over all time stamps.
  1370. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1371. To turn this on on a per-file basis, insert anywhere in the file:
  1372. #+STARTUP: customtime"
  1373. :group 'org-time
  1374. :set 'set-default
  1375. :type 'sexp)
  1376. (make-variable-buffer-local 'org-display-custom-times)
  1377. (defcustom org-time-stamp-custom-formats
  1378. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1379. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1380. These are overlayed over the default ISO format if the variable
  1381. `org-display-custom-times' is set. Time like %H:%M should be at the
  1382. end of the second format."
  1383. :group 'org-time
  1384. :type 'sexp)
  1385. (defun org-time-stamp-format (&optional long inactive)
  1386. "Get the right format for a time string."
  1387. (let ((f (if long (cdr org-time-stamp-formats)
  1388. (car org-time-stamp-formats))))
  1389. (if inactive
  1390. (concat "[" (substring f 1 -1) "]")
  1391. f)))
  1392. (defcustom org-deadline-warning-days 14
  1393. "No. of days before expiration during which a deadline becomes active.
  1394. This variable governs the display in sparse trees and in the agenda.
  1395. When 0 or negative, it means use this number (the absolute value of it)
  1396. even if a deadline has a different individual lead time specified."
  1397. :group 'org-time
  1398. :group 'org-agenda-daily/weekly
  1399. :type 'number)
  1400. (defcustom org-read-date-prefer-future t
  1401. "Non-nil means, assume future for incomplete date input from user.
  1402. This affects the following situations:
  1403. 1. The user gives a day, but no month.
  1404. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1405. read this as the third of *next* month. However, if you enter \"17\",
  1406. it will be considered as *this* month.
  1407. 2. The user gives a month but not a year.
  1408. For example, if it is april and you enter \"feb 2\", this will be read
  1409. as feb 2, *next* year. \"May 5\", however, will be this year.
  1410. Currently this does not work for ISO week specifications.
  1411. When this option is nil, the current month and year will always be used
  1412. as defaults."
  1413. :group 'org-time
  1414. :type 'boolean)
  1415. (defcustom org-read-date-display-live t
  1416. "Non-nil means, display current interpretation of date prompt live.
  1417. This display will be in an overlay, in the minibuffer."
  1418. :group 'org-time
  1419. :type 'boolean)
  1420. (defcustom org-read-date-popup-calendar t
  1421. "Non-nil means, pop up a calendar when prompting for a date.
  1422. In the calendar, the date can be selected with mouse-1. However, the
  1423. minibuffer will also be active, and you can simply enter the date as well.
  1424. When nil, only the minibuffer will be available."
  1425. :group 'org-time
  1426. :type 'boolean)
  1427. (if (fboundp 'defvaralias)
  1428. (defvaralias 'org-popup-calendar-for-date-prompt
  1429. 'org-read-date-popup-calendar))
  1430. (defcustom org-extend-today-until 0
  1431. "The hour when your day really ends.
  1432. This has influence for the following applications:
  1433. - When switching the agenda to \"today\". It it is still earlier than
  1434. the time given here, the day recognized as TODAY is actually yesterday.
  1435. - When a date is read from the user and it is still before the time given
  1436. here, the current date and time will be assumed to be yesterday, 23:59.
  1437. FIXME:
  1438. IMPORTANT: This is still a very experimental feature, it may disappear
  1439. again or it may be extended to mean more things."
  1440. :group 'org-time
  1441. :type 'number)
  1442. (defcustom org-edit-timestamp-down-means-later nil
  1443. "Non-nil means, S-down will increase the time in a time stamp.
  1444. When nil, S-up will increase."
  1445. :group 'org-time
  1446. :type 'boolean)
  1447. (defcustom org-calendar-follow-timestamp-change t
  1448. "Non-nil means, make the calendar window follow timestamp changes.
  1449. When a timestamp is modified and the calendar window is visible, it will be
  1450. moved to the new date."
  1451. :group 'org-time
  1452. :type 'boolean)
  1453. (defgroup org-tags nil
  1454. "Options concerning tags in Org-mode."
  1455. :tag "Org Tags"
  1456. :group 'org)
  1457. (defcustom org-tag-alist nil
  1458. "List of tags allowed in Org-mode files.
  1459. When this list is nil, Org-mode will base TAG input on what is already in the
  1460. buffer.
  1461. The value of this variable is an alist, the car of each entry must be a
  1462. keyword as a string, the cdr may be a character that is used to select
  1463. that tag through the fast-tag-selection interface.
  1464. See the manual for details."
  1465. :group 'org-tags
  1466. :type '(repeat
  1467. (choice
  1468. (cons (string :tag "Tag name")
  1469. (character :tag "Access char"))
  1470. (const :tag "Start radio group" (:startgroup))
  1471. (const :tag "End radio group" (:endgroup)))))
  1472. (defcustom org-use-fast-tag-selection 'auto
  1473. "Non-nil means, use fast tag selection scheme.
  1474. This is a special interface to select and deselect tags with single keys.
  1475. When nil, fast selection is never used.
  1476. When the symbol `auto', fast selection is used if and only if selection
  1477. characters for tags have been configured, either through the variable
  1478. `org-tag-alist' or through a #+TAGS line in the buffer.
  1479. When t, fast selection is always used and selection keys are assigned
  1480. automatically if necessary."
  1481. :group 'org-tags
  1482. :type '(choice
  1483. (const :tag "Always" t)
  1484. (const :tag "Never" nil)
  1485. (const :tag "When selection characters are configured" 'auto)))
  1486. (defcustom org-fast-tag-selection-single-key nil
  1487. "Non-nil means, fast tag selection exits after first change.
  1488. When nil, you have to press RET to exit it.
  1489. During fast tag selection, you can toggle this flag with `C-c'.
  1490. This variable can also have the value `expert'. In this case, the window
  1491. displaying the tags menu is not even shown, until you press C-c again."
  1492. :group 'org-tags
  1493. :type '(choice
  1494. (const :tag "No" nil)
  1495. (const :tag "Yes" t)
  1496. (const :tag "Expert" expert)))
  1497. (defvar org-fast-tag-selection-include-todo nil
  1498. "Non-nil means, fast tags selection interface will also offer TODO states.
  1499. This is an undocumented feature, you should not rely on it.")
  1500. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1501. "The column to which tags should be indented in a headline.
  1502. If this number is positive, it specifies the column. If it is negative,
  1503. it means that the tags should be flushright to that column. For example,
  1504. -80 works well for a normal 80 character screen."
  1505. :group 'org-tags
  1506. :type 'integer)
  1507. (defcustom org-auto-align-tags t
  1508. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1509. These operations change the length of a headline and therefore shift
  1510. the tags around. With this options turned on, after each such operation
  1511. the tags are again aligned to `org-tags-column'."
  1512. :group 'org-tags
  1513. :type 'boolean)
  1514. (defcustom org-use-tag-inheritance t
  1515. "Non-nil means, tags in levels apply also for sublevels.
  1516. When nil, only the tags directly given in a specific line apply there.
  1517. If you turn off this option, you very likely want to turn on the
  1518. companion option `org-tags-match-list-sublevels'.
  1519. This may also be a list of tags that should be inherited, or a regexp that
  1520. matches tags that should be inherited."
  1521. :group 'org-tags
  1522. :type '(choice
  1523. (const :tag "Not" nil)
  1524. (const :tag "Always" t)
  1525. (repeat :tag "Specific tags" (string :tag "Tag"))
  1526. (regexp :tag "Tags matched by regexp")))
  1527. (defun org-tag-inherit-p (tag)
  1528. "Check if TAG is one that should be inherited."
  1529. (cond
  1530. ((eq org-use-tag-inheritance t) t)
  1531. ((not org-use-tag-inheritance) nil)
  1532. ((stringp org-use-tag-inheritance)
  1533. (string-match org-use-tag-inheritance tag))
  1534. ((listp org-use-tag-inheritance)
  1535. (member tag org-use-tag-inheritance))
  1536. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1537. (defcustom org-tags-match-list-sublevels nil
  1538. "Non-nil means list also sublevels of headlines matching tag search.
  1539. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1540. the sublevels of a headline matching a tag search often also match
  1541. the same search. Listing all of them can create very long lists.
  1542. Setting this variable to nil causes subtrees of a match to be skipped.
  1543. This option is off by default, because inheritance in on. If you turn
  1544. inheritance off, you very likely want to turn this option on.
  1545. As a special case, if the tag search is restricted to TODO items, the
  1546. value of this variable is ignored and sublevels are always checked, to
  1547. make sure all corresponding TODO items find their way into the list."
  1548. :group 'org-tags
  1549. :type 'boolean)
  1550. (defvar org-tags-history nil
  1551. "History of minibuffer reads for tags.")
  1552. (defvar org-last-tags-completion-table nil
  1553. "The last used completion table for tags.")
  1554. (defvar org-after-tags-change-hook nil
  1555. "Hook that is run after the tags in a line have changed.")
  1556. (defgroup org-properties nil
  1557. "Options concerning properties in Org-mode."
  1558. :tag "Org Properties"
  1559. :group 'org)
  1560. (defcustom org-property-format "%-10s %s"
  1561. "How property key/value pairs should be formatted by `indent-line'.
  1562. When `indent-line' hits a property definition, it will format the line
  1563. according to this format, mainly to make sure that the values are
  1564. lined-up with respect to each other."
  1565. :group 'org-properties
  1566. :type 'string)
  1567. (defcustom org-use-property-inheritance nil
  1568. "Non-nil means, properties apply also for sublevels.
  1569. This setting is chiefly used during property searches. Turning it on can
  1570. cause significant overhead when doing a search, which is why it is not
  1571. on by default.
  1572. When nil, only the properties directly given in the current entry count.
  1573. When t, every property is inherited. The value may also be a list of
  1574. properties that should have inheritance, or a regular expression matching
  1575. properties that should be inherited.
  1576. However, note that some special properties use inheritance under special
  1577. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1578. and the properties ending in \"_ALL\" when they are used as descriptor
  1579. for valid values of a property.
  1580. Note for programmers:
  1581. When querying an entry with `org-entry-get', you can control if inheritance
  1582. should be used. By default, `org-entry-get' looks only at the local
  1583. properties. You can request inheritance by setting the inherit argument
  1584. to t (to force inheritance) or to `selective' (to respect the setting
  1585. in this variable)."
  1586. :group 'org-properties
  1587. :type '(choice
  1588. (const :tag "Not" nil)
  1589. (const :tag "Always" t)
  1590. (repeat :tag "Specific properties" (string :tag "Property"))
  1591. (regexp :tag "Properties matched by regexp")))
  1592. (defun org-property-inherit-p (property)
  1593. "Check if PROPERTY is one that should be inherited."
  1594. (cond
  1595. ((eq org-use-property-inheritance t) t)
  1596. ((not org-use-property-inheritance) nil)
  1597. ((stringp org-use-property-inheritance)
  1598. (string-match org-use-property-inheritance property))
  1599. ((listp org-use-property-inheritance)
  1600. (member property org-use-property-inheritance))
  1601. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1602. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1603. "The default column format, if no other format has been defined.
  1604. This variable can be set on the per-file basis by inserting a line
  1605. #+COLUMNS: %25ITEM ....."
  1606. :group 'org-properties
  1607. :type 'string)
  1608. (defcustom org-effort-property "Effort"
  1609. "The property that is being used to keep track of effort estimates.
  1610. Effort estimates given in this property need to have the format H:MM."
  1611. :group 'org-properties
  1612. :group 'org-progress
  1613. :type '(string :tag "Property"))
  1614. (defcustom org-global-properties nil
  1615. "List of property/value pairs that can be inherited by any entry.
  1616. You can set buffer-local values for this by adding lines like
  1617. #+PROPERTY: NAME VALUE"
  1618. :group 'org-properties
  1619. :type '(repeat
  1620. (cons (string :tag "Property")
  1621. (string :tag "Value"))))
  1622. (defvar org-local-properties nil
  1623. "List of property/value pairs that can be inherited by any entry.
  1624. Valid for the current buffer.
  1625. This variable is populated from #+PROPERTY lines.")
  1626. (defgroup org-agenda nil
  1627. "Options concerning agenda views in Org-mode."
  1628. :tag "Org Agenda"
  1629. :group 'org)
  1630. (defvar org-category nil
  1631. "Variable used by org files to set a category for agenda display.
  1632. Such files should use a file variable to set it, for example
  1633. # -*- mode: org; org-category: \"ELisp\"
  1634. or contain a special line
  1635. #+CATEGORY: ELisp
  1636. If the file does not specify a category, then file's base name
  1637. is used instead.")
  1638. (make-variable-buffer-local 'org-category)
  1639. (defcustom org-agenda-files nil
  1640. "The files to be used for agenda display.
  1641. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1642. \\[org-remove-file]. You can also use customize to edit the list.
  1643. If an entry is a directory, all files in that directory that are matched by
  1644. `org-agenda-file-regexp' will be part of the file list.
  1645. If the value of the variable is not a list but a single file name, then
  1646. the list of agenda files is actually stored and maintained in that file, one
  1647. agenda file per line."
  1648. :group 'org-agenda
  1649. :type '(choice
  1650. (repeat :tag "List of files and directories" file)
  1651. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1652. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1653. "Regular expression to match files for `org-agenda-files'.
  1654. If any element in the list in that variable contains a directory instead
  1655. of a normal file, all files in that directory that are matched by this
  1656. regular expression will be included."
  1657. :group 'org-agenda
  1658. :type 'regexp)
  1659. (defcustom org-agenda-text-search-extra-files nil
  1660. "List of extra files to be searched by text search commands.
  1661. These files will be search in addition to the agenda files by the
  1662. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1663. Note that these files will only be searched for text search commands,
  1664. not for the other agenda views like todo lists, tag searches or the weekly
  1665. agenda. This variable is intended to list notes and possibly archive files
  1666. that should also be searched by these two commands.
  1667. In fact, if the first element in the list is the symbol `agenda-archives',
  1668. than all archive files of all agenda files will be added to the search
  1669. scope."
  1670. :group 'org-agenda
  1671. :type '(set :greedy t
  1672. (const :tag "Agenda Archives" agenda-archives)
  1673. (repeat :inline t (file))))
  1674. (if (fboundp 'defvaralias)
  1675. (defvaralias 'org-agenda-multi-occur-extra-files
  1676. 'org-agenda-text-search-extra-files))
  1677. (defcustom org-agenda-skip-unavailable-files nil
  1678. "t means to just skip non-reachable files in `org-agenda-files'.
  1679. Nil means to remove them, after a query, from the list."
  1680. :group 'org-agenda
  1681. :type 'boolean)
  1682. (defcustom org-calendar-to-agenda-key [?c]
  1683. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1684. The command `org-calendar-goto-agenda' will be bound to this key. The
  1685. default is the character `c' because then `c' can be used to switch back and
  1686. forth between agenda and calendar."
  1687. :group 'org-agenda
  1688. :type 'sexp)
  1689. (eval-after-load "calendar"
  1690. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1691. 'org-calendar-goto-agenda))
  1692. (defgroup org-latex nil
  1693. "Options for embedding LaTeX code into Org-mode."
  1694. :tag "Org LaTeX"
  1695. :group 'org)
  1696. (defcustom org-format-latex-options
  1697. '(:foreground default :background default :scale 1.0
  1698. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1699. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1700. "Options for creating images from LaTeX fragments.
  1701. This is a property list with the following properties:
  1702. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  1703. `default' means use the forground of the default face.
  1704. :background the background color, or \"Transparent\".
  1705. `default' means use the background of the default face.
  1706. :scale a scaling factor for the size of the images
  1707. :html-foreground, :html-background, :html-scale
  1708. The same numbers for HTML export.
  1709. :matchers a list indicating which matchers should be used to
  1710. find LaTeX fragments. Valid members of this list are:
  1711. \"begin\" find environments
  1712. \"$\" find math expressions surrounded by $...$
  1713. \"$$\" find math expressions surrounded by $$....$$
  1714. \"\\(\" find math expressions surrounded by \\(...\\)
  1715. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1716. :group 'org-latex
  1717. :type 'plist)
  1718. (defcustom org-format-latex-header "\\documentclass{article}
  1719. \\usepackage{fullpage} % do not remove
  1720. \\usepackage{amssymb}
  1721. \\usepackage[usenames]{color}
  1722. \\usepackage{amsmath}
  1723. \\usepackage{latexsym}
  1724. \\usepackage[mathscr]{eucal}
  1725. \\pagestyle{empty} % do not remove"
  1726. "The document header used for processing LaTeX fragments."
  1727. :group 'org-latex
  1728. :type 'string)
  1729. (defgroup org-font-lock nil
  1730. "Font-lock settings for highlighting in Org-mode."
  1731. :tag "Org Font Lock"
  1732. :group 'org)
  1733. (defcustom org-level-color-stars-only nil
  1734. "Non-nil means fontify only the stars in each headline.
  1735. When nil, the entire headline is fontified.
  1736. Changing it requires restart of `font-lock-mode' to become effective
  1737. also in regions already fontified."
  1738. :group 'org-font-lock
  1739. :type 'boolean)
  1740. (defcustom org-hide-leading-stars nil
  1741. "Non-nil means, hide the first N-1 stars in a headline.
  1742. This works by using the face `org-hide' for these stars. This
  1743. face is white for a light background, and black for a dark
  1744. background. You may have to customize the face `org-hide' to
  1745. make this work.
  1746. Changing it requires restart of `font-lock-mode' to become effective
  1747. also in regions already fontified.
  1748. You may also set this on a per-file basis by adding one of the following
  1749. lines to the buffer:
  1750. #+STARTUP: hidestars
  1751. #+STARTUP: showstars"
  1752. :group 'org-font-lock
  1753. :type 'boolean)
  1754. (defcustom org-fontify-done-headline nil
  1755. "Non-nil means, change the face of a headline if it is marked DONE.
  1756. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1757. When this is non-nil, the headline after the keyword is set to the
  1758. `org-headline-done' as an additional indication."
  1759. :group 'org-font-lock
  1760. :type 'boolean)
  1761. (defcustom org-fontify-emphasized-text t
  1762. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1763. Changing this variable requires a restart of Emacs to take effect."
  1764. :group 'org-font-lock
  1765. :type 'boolean)
  1766. (defcustom org-highlight-latex-fragments-and-specials nil
  1767. "Non-nil means, fontify what is treated specially by the exporters."
  1768. :group 'org-font-lock
  1769. :type 'boolean)
  1770. (defcustom org-hide-emphasis-markers nil
  1771. "Non-nil mean font-lock should hide the emphasis marker characters."
  1772. :group 'org-font-lock
  1773. :type 'boolean)
  1774. (defvar org-emph-re nil
  1775. "Regular expression for matching emphasis.")
  1776. (defvar org-verbatim-re nil
  1777. "Regular expression for matching verbatim text.")
  1778. (defvar org-emphasis-regexp-components) ; defined just below
  1779. (defvar org-emphasis-alist) ; defined just below
  1780. (defun org-set-emph-re (var val)
  1781. "Set variable and compute the emphasis regular expression."
  1782. (set var val)
  1783. (when (and (boundp 'org-emphasis-alist)
  1784. (boundp 'org-emphasis-regexp-components)
  1785. org-emphasis-alist org-emphasis-regexp-components)
  1786. (let* ((e org-emphasis-regexp-components)
  1787. (pre (car e))
  1788. (post (nth 1 e))
  1789. (border (nth 2 e))
  1790. (body (nth 3 e))
  1791. (nl (nth 4 e))
  1792. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1793. (body1 (concat body "*?"))
  1794. (markers (mapconcat 'car org-emphasis-alist ""))
  1795. (vmarkers (mapconcat
  1796. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1797. org-emphasis-alist "")))
  1798. ;; make sure special characters appear at the right position in the class
  1799. (if (string-match "\\^" markers)
  1800. (setq markers (concat (replace-match "" t t markers) "^")))
  1801. (if (string-match "-" markers)
  1802. (setq markers (concat (replace-match "" t t markers) "-")))
  1803. (if (string-match "\\^" vmarkers)
  1804. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1805. (if (string-match "-" vmarkers)
  1806. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1807. (if (> nl 0)
  1808. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1809. (int-to-string nl) "\\}")))
  1810. ;; Make the regexp
  1811. (setq org-emph-re
  1812. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1813. "\\("
  1814. "\\([" markers "]\\)"
  1815. "\\("
  1816. "[^" border "]\\|"
  1817. "[^" border (if (and nil stacked) markers) "]"
  1818. body1
  1819. "[^" border (if (and nil stacked) markers) "]"
  1820. "\\)"
  1821. "\\3\\)"
  1822. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1823. (setq org-verbatim-re
  1824. (concat "\\([" pre "]\\|^\\)"
  1825. "\\("
  1826. "\\([" vmarkers "]\\)"
  1827. "\\("
  1828. "[^" border "]\\|"
  1829. "[^" border "]"
  1830. body1
  1831. "[^" border "]"
  1832. "\\)"
  1833. "\\3\\)"
  1834. "\\([" post "]\\|$\\)")))))
  1835. (defcustom org-emphasis-regexp-components
  1836. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1837. "Components used to build the regular expression for emphasis.
  1838. This is a list with 6 entries. Terminology: In an emphasis string
  1839. like \" *strong word* \", we call the initial space PREMATCH, the final
  1840. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1841. and \"trong wor\" is the body. The different components in this variable
  1842. specify what is allowed/forbidden in each part:
  1843. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1844. post Chars allowed as postmatch. End of line will be allowed too.
  1845. border The chars *forbidden* as border characters.
  1846. body-regexp A regexp like \".\" to match a body character. Don't use
  1847. non-shy groups here, and don't allow newline here.
  1848. newline The maximum number of newlines allowed in an emphasis exp.
  1849. Use customize to modify this, or restart Emacs after changing it."
  1850. :group 'org-font-lock
  1851. :set 'org-set-emph-re
  1852. :type '(list
  1853. (sexp :tag "Allowed chars in pre ")
  1854. (sexp :tag "Allowed chars in post ")
  1855. (sexp :tag "Forbidden chars in border ")
  1856. (sexp :tag "Regexp for body ")
  1857. (integer :tag "number of newlines allowed")
  1858. (option (boolean :tag "Stacking (DISABLED) "))))
  1859. (defcustom org-emphasis-alist
  1860. `(("*" bold "<b>" "</b>")
  1861. ("/" italic "<i>" "</i>")
  1862. ("_" underline "<u>" "</u>")
  1863. ("=" org-code "<code>" "</code>" verbatim)
  1864. ("~" org-verbatim "" "" verbatim)
  1865. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1866. "<del>" "</del>")
  1867. )
  1868. "Special syntax for emphasized text.
  1869. Text starting and ending with a special character will be emphasized, for
  1870. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1871. characters, the face to be used by font-lock for highlighting in Org-mode
  1872. Emacs buffers, and the HTML tags to be used for this.
  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 '(repeat
  1877. (list
  1878. (string :tag "Marker character")
  1879. (choice
  1880. (face :tag "Font-lock-face")
  1881. (plist :tag "Face property list"))
  1882. (string :tag "HTML start tag")
  1883. (string :tag "HTML end tag")
  1884. (option (const verbatim)))))
  1885. ;;; Miscellaneous options
  1886. (defgroup org-completion nil
  1887. "Completion in Org-mode."
  1888. :tag "Org Completion"
  1889. :group 'org)
  1890. (defcustom org-completion-fallback-command 'hippie-expand
  1891. "The expansion command called by \\[org-complete] in normal context.
  1892. Normal means, no org-mode-specific context."
  1893. :group 'org-completion
  1894. :type 'function)
  1895. ;;; Functions and variables from ther packages
  1896. ;; Declared here to avoid compiler warnings
  1897. ;; XEmacs only
  1898. (defvar outline-mode-menu-heading)
  1899. (defvar outline-mode-menu-show)
  1900. (defvar outline-mode-menu-hide)
  1901. (defvar zmacs-regions) ; XEmacs regions
  1902. ;; Emacs only
  1903. (defvar mark-active)
  1904. ;; Various packages
  1905. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  1906. (declare-function calendar-forward-day "cal-move" (arg))
  1907. (declare-function calendar-goto-date "cal-move" (date))
  1908. (declare-function calendar-goto-today "cal-move" ())
  1909. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  1910. (defvar calc-embedded-close-formula)
  1911. (defvar calc-embedded-open-formula)
  1912. (declare-function cdlatex-tab "ext:cdlatex" ())
  1913. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1914. (defvar font-lock-unfontify-region-function)
  1915. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1916. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  1917. (defvar iswitchb-temp-buflist)
  1918. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  1919. (declare-function org-agenda-skip "org-agenda" ())
  1920. (declare-function org-format-agenda-item "org-agenda"
  1921. (extra txt &optional category tags dotime noprefix remove-re))
  1922. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  1923. (declare-function org-agenda-change-all-lines "org-agenda"
  1924. (newhead hdmarker &optional fixface))
  1925. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  1926. (declare-function org-agenda-maybe-redo "org-agenda" ())
  1927. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  1928. (beg end))
  1929. (declare-function parse-time-string "parse-time" (string))
  1930. (declare-function remember "remember" (&optional initial))
  1931. (declare-function remember-buffer-desc "remember" ())
  1932. (declare-function remember-finalize "remember" ())
  1933. (defvar remember-save-after-remembering)
  1934. (defvar remember-data-file)
  1935. (defvar remember-register)
  1936. (defvar remember-buffer)
  1937. (defvar remember-handler-functions)
  1938. (defvar remember-annotation-functions)
  1939. (defvar texmathp-why)
  1940. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  1941. (declare-function table--at-cell-p "table" (position &optional object at-column))
  1942. (defvar w3m-current-url)
  1943. (defvar w3m-current-title)
  1944. (defvar org-latex-regexps)
  1945. ;;; Autoload and prepare some org modules
  1946. ;; Some table stuff that needs to be defined here, because it is used
  1947. ;; by the functions setting up org-mode or checking for table context.
  1948. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  1949. "Detects an org-type or table-type table.")
  1950. (defconst org-table-line-regexp "^[ \t]*|"
  1951. "Detects an org-type table line.")
  1952. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  1953. "Detects an org-type table line.")
  1954. (defconst org-table-hline-regexp "^[ \t]*|-"
  1955. "Detects an org-type table hline.")
  1956. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  1957. "Detects a table-type table hline.")
  1958. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  1959. "Searching from within a table (any type) this finds the first line
  1960. outside the table.")
  1961. ;; Autoload the functions in org-table.el that are needed by functions here.
  1962. (eval-and-compile
  1963. (org-autoload "org-table"
  1964. '(org-table-align org-table-begin org-table-blank-field
  1965. org-table-convert org-table-convert-region org-table-copy-down
  1966. org-table-copy-region org-table-create
  1967. org-table-create-or-convert-from-region
  1968. org-table-create-with-table.el org-table-current-dline
  1969. org-table-cut-region org-table-delete-column org-table-edit-field
  1970. org-table-edit-formulas org-table-end org-table-eval-formula
  1971. org-table-export org-table-field-info
  1972. org-table-get-stored-formulas org-table-goto-column
  1973. org-table-hline-and-move org-table-import org-table-insert-column
  1974. org-table-insert-hline org-table-insert-row org-table-iterate
  1975. org-table-justify-field-maybe org-table-kill-row
  1976. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  1977. org-table-move-column org-table-move-column-left
  1978. org-table-move-column-right org-table-move-row
  1979. org-table-move-row-down org-table-move-row-up
  1980. org-table-next-field org-table-next-row org-table-paste-rectangle
  1981. org-table-previous-field org-table-recalculate
  1982. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  1983. org-table-toggle-coordinate-overlays
  1984. org-table-toggle-formula-debugger org-table-wrap-region
  1985. orgtbl-mode turn-on-orgtbl)))
  1986. (defun org-at-table-p (&optional table-type)
  1987. "Return t if the cursor is inside an org-type table.
  1988. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  1989. (if org-enable-table-editor
  1990. (save-excursion
  1991. (beginning-of-line 1)
  1992. (looking-at (if table-type org-table-any-line-regexp
  1993. org-table-line-regexp)))
  1994. nil))
  1995. (defsubst org-table-p () (org-at-table-p))
  1996. (defun org-at-table.el-p ()
  1997. "Return t if and only if we are at a table.el table."
  1998. (and (org-at-table-p 'any)
  1999. (save-excursion
  2000. (goto-char (org-table-begin 'any))
  2001. (looking-at org-table1-hline-regexp))))
  2002. (defun org-table-recognize-table.el ()
  2003. "If there is a table.el table nearby, recognize it and move into it."
  2004. (if org-table-tab-recognizes-table.el
  2005. (if (org-at-table.el-p)
  2006. (progn
  2007. (beginning-of-line 1)
  2008. (if (looking-at org-table-dataline-regexp)
  2009. nil
  2010. (if (looking-at org-table1-hline-regexp)
  2011. (progn
  2012. (beginning-of-line 2)
  2013. (if (looking-at org-table-any-border-regexp)
  2014. (beginning-of-line -1)))))
  2015. (if (re-search-forward "|" (org-table-end t) t)
  2016. (progn
  2017. (require 'table)
  2018. (if (table--at-cell-p (point))
  2019. t
  2020. (message "recognizing table.el table...")
  2021. (table-recognize-table)
  2022. (message "recognizing table.el table...done")))
  2023. (error "This should not happen..."))
  2024. t)
  2025. nil)
  2026. nil))
  2027. (defun org-at-table-hline-p ()
  2028. "Return t if the cursor is inside a hline in a table."
  2029. (if org-enable-table-editor
  2030. (save-excursion
  2031. (beginning-of-line 1)
  2032. (looking-at org-table-hline-regexp))
  2033. nil))
  2034. (defvar org-table-clean-did-remove-column nil)
  2035. (defun org-table-map-tables (function)
  2036. "Apply FUNCTION to the start of all tables in the buffer."
  2037. (save-excursion
  2038. (save-restriction
  2039. (widen)
  2040. (goto-char (point-min))
  2041. (while (re-search-forward org-table-any-line-regexp nil t)
  2042. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2043. (beginning-of-line 1)
  2044. (if (looking-at org-table-line-regexp)
  2045. (save-excursion (funcall function)))
  2046. (re-search-forward org-table-any-border-regexp nil 1))))
  2047. (message "Mapping tables: done"))
  2048. ;; Declare and autoload functions from org-exp.el
  2049. (declare-function org-default-export-plist "org-exp")
  2050. (declare-function org-infile-export-plist "org-exp")
  2051. (declare-function org-get-current-options "org-exp")
  2052. (eval-and-compile
  2053. (org-autoload "org-exp"
  2054. '(org-export org-export-as-ascii org-export-visible
  2055. org-insert-export-options-template org-export-as-html-and-open
  2056. org-export-as-html-batch org-export-as-html-to-buffer
  2057. org-replace-region-by-html org-export-region-as-html
  2058. org-export-as-html org-export-icalendar-this-file
  2059. org-export-icalendar-all-agenda-files
  2060. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2061. ;; Declare and autoload functions from org-exp.el
  2062. (eval-and-compile
  2063. (org-autoload "org-exp"
  2064. '(org-agenda org-agenda-list org-search-view
  2065. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2066. org-diary org-agenda-to-appt)))
  2067. ;; Autoload org-remember
  2068. (eval-and-compile
  2069. (org-autoload "org-remember"
  2070. '(org-remember-insinuate org-remember-annotation
  2071. org-remember-apply-template org-remember org-remember-handler)))
  2072. ;; Autoload org-clock.el
  2073. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2074. (beg end))
  2075. (defvar org-clock-marker (make-marker)
  2076. "Marker recording the last clock-in.")
  2077. (eval-and-compile
  2078. (org-autoload
  2079. "org-clock"
  2080. '(org-clock-in org-clock-out org-clock-cancel
  2081. org-clock-goto org-clock-sum org-clock-display
  2082. org-remove-clock-overlays org-clock-report
  2083. org-clocktable-shift org-dblock-write:clocktable
  2084. org-get-clocktable)))
  2085. (defun org-clock-update-time-maybe ()
  2086. "If this is a CLOCK line, update it and return t.
  2087. Otherwise, return nil."
  2088. (interactive)
  2089. (save-excursion
  2090. (beginning-of-line 1)
  2091. (skip-chars-forward " \t")
  2092. (when (looking-at org-clock-string)
  2093. (let ((re (concat "[ \t]*" org-clock-string
  2094. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  2095. "\\([ \t]*=>.*\\)?"))
  2096. ts te h m s)
  2097. (if (not (looking-at re))
  2098. nil
  2099. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  2100. (end-of-line 1)
  2101. (setq ts (match-string 1)
  2102. te (match-string 2))
  2103. (setq s (- (time-to-seconds
  2104. (apply 'encode-time (org-parse-time-string te)))
  2105. (time-to-seconds
  2106. (apply 'encode-time (org-parse-time-string ts))))
  2107. h (floor (/ s 3600))
  2108. s (- s (* 3600 h))
  2109. m (floor (/ s 60))
  2110. s (- s (* 60 s)))
  2111. (insert " => " (format "%2d:%02d" h m))
  2112. t)))))
  2113. (defun org-check-running-clock ()
  2114. "Check if the current buffer contains the running clock.
  2115. If yes, offer to stop it and to save the buffer with the changes."
  2116. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2117. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2118. (buffer-name))))
  2119. (org-clock-out)
  2120. (when (y-or-n-p "Save changed buffer?")
  2121. (save-buffer))))
  2122. (defun org-clocktable-try-shift (dir n)
  2123. "Check if this line starts a clock table, if yes, shift the time block."
  2124. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2125. (org-clocktable-shift dir n)))
  2126. ;; Autoload archiving code
  2127. ;; The stuff that is needed for cycling and tags has to be defined here.
  2128. (defgroup org-archive nil
  2129. "Options concerning archiving in Org-mode."
  2130. :tag "Org Archive"
  2131. :group 'org-structure)
  2132. (defcustom org-archive-location "%s_archive::"
  2133. "The location where subtrees should be archived.
  2134. Otherwise, the value of this variable is a string, consisting of two
  2135. parts, separated by a double-colon.
  2136. The first part is a file name - when omitted, archiving happens in the same
  2137. file. %s will be replaced by the current file name (without directory part).
  2138. Archiving to a different file is useful to keep archived entries from
  2139. contributing to the Org-mode Agenda.
  2140. The part after the double colon is a headline. The archived entries will be
  2141. filed under that headline. When omitted, the subtrees are simply filed away
  2142. at the end of the file, as top-level entries.
  2143. Here are a few examples:
  2144. \"%s_archive::\"
  2145. If the current file is Projects.org, archive in file
  2146. Projects.org_archive, as top-level trees. This is the default.
  2147. \"::* Archived Tasks\"
  2148. Archive in the current file, under the top-level headline
  2149. \"* Archived Tasks\".
  2150. \"~/org/archive.org::\"
  2151. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2152. \"basement::** Finished Tasks\"
  2153. Archive in file ./basement (relative path), as level 3 trees
  2154. below the level 2 heading \"** Finished Tasks\".
  2155. You may set this option on a per-file basis by adding to the buffer a
  2156. line like
  2157. #+ARCHIVE: basement::** Finished Tasks
  2158. You may also define it locally for a subtree by setting an ARCHIVE property
  2159. in the entry. If such a property is found in an entry, or anywhere up
  2160. the hierarchy, it will be used."
  2161. :group 'org-archive
  2162. :type 'string)
  2163. (defcustom org-archive-tag "ARCHIVE"
  2164. "The tag that marks a subtree as archived.
  2165. An archived subtree does not open during visibility cycling, and does
  2166. not contribute to the agenda listings.
  2167. After changing this, font-lock must be restarted in the relevant buffers to
  2168. get the proper fontification."
  2169. :group 'org-archive
  2170. :group 'org-keywords
  2171. :type 'string)
  2172. (defcustom org-agenda-skip-archived-trees t
  2173. "Non-nil means, the agenda will skip any items located in archived trees.
  2174. An archived tree is a tree marked with the tag ARCHIVE."
  2175. :group 'org-archive
  2176. :group 'org-agenda-skip
  2177. :type 'boolean)
  2178. (defcustom org-cycle-open-archived-trees nil
  2179. "Non-nil means, `org-cycle' will open archived trees.
  2180. An archived tree is a tree marked with the tag ARCHIVE.
  2181. When nil, archived trees will stay folded. You can still open them with
  2182. normal outline commands like `show-all', but not with the cycling commands."
  2183. :group 'org-archive
  2184. :group 'org-cycle
  2185. :type 'boolean)
  2186. (defcustom org-sparse-tree-open-archived-trees nil
  2187. "Non-nil means sparse tree construction shows matches in archived trees.
  2188. When nil, matches in these trees are highlighted, but the trees are kept in
  2189. collapsed state."
  2190. :group 'org-archive
  2191. :group 'org-sparse-trees
  2192. :type 'boolean)
  2193. (defun org-cycle-hide-archived-subtrees (state)
  2194. "Re-hide all archived subtrees after a visibility state change."
  2195. (when (and (not org-cycle-open-archived-trees)
  2196. (not (memq state '(overview folded))))
  2197. (save-excursion
  2198. (let* ((globalp (memq state '(contents all)))
  2199. (beg (if globalp (point-min) (point)))
  2200. (end (if globalp (point-max) (org-end-of-subtree t))))
  2201. (org-hide-archived-subtrees beg end)
  2202. (goto-char beg)
  2203. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2204. (message "%s" (substitute-command-keys
  2205. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2206. (defun org-force-cycle-archived ()
  2207. "Cycle subtree even if it is archived."
  2208. (interactive)
  2209. (setq this-command 'org-cycle)
  2210. (let ((org-cycle-open-archived-trees t))
  2211. (call-interactively 'org-cycle)))
  2212. (defun org-hide-archived-subtrees (beg end)
  2213. "Re-hide all archived subtrees after a visibility state change."
  2214. (save-excursion
  2215. (let* ((re (concat ":" org-archive-tag ":")))
  2216. (goto-char beg)
  2217. (while (re-search-forward re end t)
  2218. (and (org-on-heading-p) (hide-subtree))
  2219. (org-end-of-subtree t)))))
  2220. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2221. (eval-and-compile
  2222. (org-autoload "org-archive"
  2223. '(org-add-archive-files org-archive-subtree
  2224. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2225. ;; Autoload Column View Code
  2226. (declare-function org-columns-number-to-string "org-colview")
  2227. (declare-function org-columns-get-format-and-top-level "org-colview")
  2228. (declare-function org-columns-compute "org-colview")
  2229. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2230. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2231. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2232. org-columns org-insert-columns-dblock))
  2233. ;;; Variables for pre-computed regular expressions, all buffer local
  2234. (defvar org-drawer-regexp nil
  2235. "Matches first line of a hidden block.")
  2236. (make-variable-buffer-local 'org-drawer-regexp)
  2237. (defvar org-todo-regexp nil
  2238. "Matches any of the TODO state keywords.")
  2239. (make-variable-buffer-local 'org-todo-regexp)
  2240. (defvar org-not-done-regexp nil
  2241. "Matches any of the TODO state keywords except the last one.")
  2242. (make-variable-buffer-local 'org-not-done-regexp)
  2243. (defvar org-todo-line-regexp nil
  2244. "Matches a headline and puts TODO state into group 2 if present.")
  2245. (make-variable-buffer-local 'org-todo-line-regexp)
  2246. (defvar org-complex-heading-regexp nil
  2247. "Matches a headline and puts everything into groups:
  2248. group 1: the stars
  2249. group 2: The todo keyword, maybe
  2250. group 3: Priority cookie
  2251. group 4: True headline
  2252. group 5: Tags")
  2253. (make-variable-buffer-local 'org-complex-heading-regexp)
  2254. (defvar org-todo-line-tags-regexp nil
  2255. "Matches a headline and puts TODO state into group 2 if present.
  2256. Also put tags into group 4 if tags are present.")
  2257. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2258. (defvar org-nl-done-regexp nil
  2259. "Matches newline followed by a headline with the DONE keyword.")
  2260. (make-variable-buffer-local 'org-nl-done-regexp)
  2261. (defvar org-looking-at-done-regexp nil
  2262. "Matches the DONE keyword a point.")
  2263. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2264. (defvar org-ds-keyword-length 12
  2265. "Maximum length of the Deadline and SCHEDULED keywords.")
  2266. (make-variable-buffer-local 'org-ds-keyword-length)
  2267. (defvar org-deadline-regexp nil
  2268. "Matches the DEADLINE keyword.")
  2269. (make-variable-buffer-local 'org-deadline-regexp)
  2270. (defvar org-deadline-time-regexp nil
  2271. "Matches the DEADLINE keyword together with a time stamp.")
  2272. (make-variable-buffer-local 'org-deadline-time-regexp)
  2273. (defvar org-deadline-line-regexp nil
  2274. "Matches the DEADLINE keyword and the rest of the line.")
  2275. (make-variable-buffer-local 'org-deadline-line-regexp)
  2276. (defvar org-scheduled-regexp nil
  2277. "Matches the SCHEDULED keyword.")
  2278. (make-variable-buffer-local 'org-scheduled-regexp)
  2279. (defvar org-scheduled-time-regexp nil
  2280. "Matches the SCHEDULED keyword together with a time stamp.")
  2281. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2282. (defvar org-closed-time-regexp nil
  2283. "Matches the CLOSED keyword together with a time stamp.")
  2284. (make-variable-buffer-local 'org-closed-time-regexp)
  2285. (defvar org-keyword-time-regexp nil
  2286. "Matches any of the 4 keywords, together with the time stamp.")
  2287. (make-variable-buffer-local 'org-keyword-time-regexp)
  2288. (defvar org-keyword-time-not-clock-regexp nil
  2289. "Matches any of the 3 keywords, together with the time stamp.")
  2290. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2291. (defvar org-maybe-keyword-time-regexp nil
  2292. "Matches a timestamp, possibly preceeded by a keyword.")
  2293. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2294. (defvar org-planning-or-clock-line-re nil
  2295. "Matches a line with planning or clock info.")
  2296. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2297. (defconst org-plain-time-of-day-regexp
  2298. (concat
  2299. "\\(\\<[012]?[0-9]"
  2300. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2301. "\\(--?"
  2302. "\\(\\<[012]?[0-9]"
  2303. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2304. "\\)?")
  2305. "Regular expression to match a plain time or time range.
  2306. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2307. groups carry important information:
  2308. 0 the full match
  2309. 1 the first time, range or not
  2310. 8 the second time, if it is a range.")
  2311. (defconst org-plain-time-extension-regexp
  2312. (concat
  2313. "\\(\\<[012]?[0-9]"
  2314. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2315. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2316. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2317. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2318. groups carry important information:
  2319. 0 the full match
  2320. 7 hours of duration
  2321. 9 minutes of duration")
  2322. (defconst org-stamp-time-of-day-regexp
  2323. (concat
  2324. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2325. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2326. "\\(--?"
  2327. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2328. "Regular expression to match a timestamp time or time range.
  2329. After a match, the following groups carry important information:
  2330. 0 the full match
  2331. 1 date plus weekday, for backreferencing to make sure both times on same day
  2332. 2 the first time, range or not
  2333. 4 the second time, if it is a range.")
  2334. (defconst org-startup-options
  2335. '(("fold" org-startup-folded t)
  2336. ("overview" org-startup-folded t)
  2337. ("nofold" org-startup-folded nil)
  2338. ("showall" org-startup-folded nil)
  2339. ("content" org-startup-folded content)
  2340. ("hidestars" org-hide-leading-stars t)
  2341. ("showstars" org-hide-leading-stars nil)
  2342. ("odd" org-odd-levels-only t)
  2343. ("oddeven" org-odd-levels-only nil)
  2344. ("align" org-startup-align-all-tables t)
  2345. ("noalign" org-startup-align-all-tables nil)
  2346. ("customtime" org-display-custom-times t)
  2347. ("logdone" org-log-done time)
  2348. ("lognotedone" org-log-done note)
  2349. ("nologdone" org-log-done nil)
  2350. ("lognoteclock-out" org-log-note-clock-out t)
  2351. ("nolognoteclock-out" org-log-note-clock-out nil)
  2352. ("logrepeat" org-log-repeat state)
  2353. ("lognoterepeat" org-log-repeat note)
  2354. ("nologrepeat" org-log-repeat nil)
  2355. ("constcgs" constants-unit-system cgs)
  2356. ("constSI" constants-unit-system SI))
  2357. "Variable associated with STARTUP options for org-mode.
  2358. Each element is a list of three items: The startup options as written
  2359. in the #+STARTUP line, the corresponding variable, and the value to
  2360. set this variable to if the option is found. An optional forth element PUSH
  2361. means to push this value onto the list in the variable.")
  2362. (defun org-set-regexps-and-options ()
  2363. "Precompute regular expressions for current buffer."
  2364. (when (org-mode-p)
  2365. (org-set-local 'org-todo-kwd-alist nil)
  2366. (org-set-local 'org-todo-key-alist nil)
  2367. (org-set-local 'org-todo-key-trigger nil)
  2368. (org-set-local 'org-todo-keywords-1 nil)
  2369. (org-set-local 'org-done-keywords nil)
  2370. (org-set-local 'org-todo-heads nil)
  2371. (org-set-local 'org-todo-sets nil)
  2372. (org-set-local 'org-todo-log-states nil)
  2373. (let ((re (org-make-options-regexp
  2374. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2375. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2376. "CONSTANTS" "PROPERTY" "DRAWERS")))
  2377. (splitre "[ \t]+")
  2378. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2379. tail sep kws1 prio props drawers)
  2380. (save-excursion
  2381. (save-restriction
  2382. (widen)
  2383. (goto-char (point-min))
  2384. (while (re-search-forward re nil t)
  2385. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2386. (cond
  2387. ((equal key "CATEGORY")
  2388. (if (string-match "[ \t]+$" value)
  2389. (setq value (replace-match "" t t value)))
  2390. (setq cat value))
  2391. ((member key '("SEQ_TODO" "TODO"))
  2392. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2393. ((equal key "TYP_TODO")
  2394. (push (cons 'type (org-split-string value splitre)) kwds))
  2395. ((equal key "TAGS")
  2396. (setq tags (append tags (org-split-string value splitre))))
  2397. ((equal key "COLUMNS")
  2398. (org-set-local 'org-columns-default-format value))
  2399. ((equal key "LINK")
  2400. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2401. (push (cons (match-string 1 value)
  2402. (org-trim (match-string 2 value)))
  2403. links)))
  2404. ((equal key "PRIORITIES")
  2405. (setq prio (org-split-string value " +")))
  2406. ((equal key "PROPERTY")
  2407. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2408. (push (cons (match-string 1 value) (match-string 2 value))
  2409. props)))
  2410. ((equal key "DRAWERS")
  2411. (setq drawers (org-split-string value splitre)))
  2412. ((equal key "CONSTANTS")
  2413. (setq const (append const (org-split-string value splitre))))
  2414. ((equal key "STARTUP")
  2415. (let ((opts (org-split-string value splitre))
  2416. l var val)
  2417. (while (setq l (pop opts))
  2418. (when (setq l (assoc l org-startup-options))
  2419. (setq var (nth 1 l) val (nth 2 l))
  2420. (if (not (nth 3 l))
  2421. (set (make-local-variable var) val)
  2422. (if (not (listp (symbol-value var)))
  2423. (set (make-local-variable var) nil))
  2424. (set (make-local-variable var) (symbol-value var))
  2425. (add-to-list var val))))))
  2426. ((equal key "ARCHIVE")
  2427. (string-match " *$" value)
  2428. (setq arch (replace-match "" t t value))
  2429. (remove-text-properties 0 (length arch)
  2430. '(face t fontified t) arch)))
  2431. )))
  2432. (when cat
  2433. (org-set-local 'org-category (intern cat))
  2434. (push (cons "CATEGORY" cat) props))
  2435. (when prio
  2436. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2437. (setq prio (mapcar 'string-to-char prio))
  2438. (org-set-local 'org-highest-priority (nth 0 prio))
  2439. (org-set-local 'org-lowest-priority (nth 1 prio))
  2440. (org-set-local 'org-default-priority (nth 2 prio)))
  2441. (and props (org-set-local 'org-local-properties (nreverse props)))
  2442. (and drawers (org-set-local 'org-drawers drawers))
  2443. (and arch (org-set-local 'org-archive-location arch))
  2444. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2445. ;; Process the TODO keywords
  2446. (unless kwds
  2447. ;; Use the global values as if they had been given locally.
  2448. (setq kwds (default-value 'org-todo-keywords))
  2449. (if (stringp (car kwds))
  2450. (setq kwds (list (cons org-todo-interpretation
  2451. (default-value 'org-todo-keywords)))))
  2452. (setq kwds (reverse kwds)))
  2453. (setq kwds (nreverse kwds))
  2454. (let (inter kws kw)
  2455. (while (setq kws (pop kwds))
  2456. (setq inter (pop kws) sep (member "|" kws)
  2457. kws0 (delete "|" (copy-sequence kws))
  2458. kwsa nil
  2459. kws1 (mapcar
  2460. (lambda (x)
  2461. ;; 1 2
  2462. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2463. (progn
  2464. (setq kw (match-string 1 x)
  2465. key (and (match-end 2) (match-string 2 x))
  2466. log (org-extract-log-state-settings x))
  2467. (push (cons kw (and key (string-to-char key))) kwsa)
  2468. (and log (push log org-todo-log-states))
  2469. kw)
  2470. (error "Invalid TODO keyword %s" x)))
  2471. kws0)
  2472. kwsa (if kwsa (append '((:startgroup))
  2473. (nreverse kwsa)
  2474. '((:endgroup))))
  2475. hw (car kws1)
  2476. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2477. tail (list inter hw (car dws) (org-last dws)))
  2478. (add-to-list 'org-todo-heads hw 'append)
  2479. (push kws1 org-todo-sets)
  2480. (setq org-done-keywords (append org-done-keywords dws nil))
  2481. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2482. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2483. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2484. (setq org-todo-sets (nreverse org-todo-sets)
  2485. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2486. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2487. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2488. ;; Process the constants
  2489. (when const
  2490. (let (e cst)
  2491. (while (setq e (pop const))
  2492. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2493. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2494. (setq org-table-formula-constants-local cst)))
  2495. ;; Process the tags.
  2496. (when tags
  2497. (let (e tgs)
  2498. (while (setq e (pop tags))
  2499. (cond
  2500. ((equal e "{") (push '(:startgroup) tgs))
  2501. ((equal e "}") (push '(:endgroup) tgs))
  2502. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2503. (push (cons (match-string 1 e)
  2504. (string-to-char (match-string 2 e)))
  2505. tgs))
  2506. (t (push (list e) tgs))))
  2507. (org-set-local 'org-tag-alist nil)
  2508. (while (setq e (pop tgs))
  2509. (or (and (stringp (car e))
  2510. (assoc (car e) org-tag-alist))
  2511. (push e org-tag-alist))))))
  2512. ;; Compute the regular expressions and other local variables
  2513. (if (not org-done-keywords)
  2514. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2515. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2516. (length org-scheduled-string)
  2517. (length org-clock-string)
  2518. (length org-closed-string)))
  2519. org-drawer-regexp
  2520. (concat "^[ \t]*:\\("
  2521. (mapconcat 'regexp-quote org-drawers "\\|")
  2522. "\\):[ \t]*$")
  2523. org-not-done-keywords
  2524. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2525. org-todo-regexp
  2526. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2527. "\\|") "\\)\\>")
  2528. org-not-done-regexp
  2529. (concat "\\<\\("
  2530. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2531. "\\)\\>")
  2532. org-todo-line-regexp
  2533. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2534. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2535. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2536. org-complex-heading-regexp
  2537. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2538. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2539. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2540. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2541. org-nl-done-regexp
  2542. (concat "\n\\*+[ \t]+"
  2543. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2544. "\\)" "\\>")
  2545. org-todo-line-tags-regexp
  2546. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2547. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2548. (org-re
  2549. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2550. org-looking-at-done-regexp
  2551. (concat "^" "\\(?:"
  2552. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2553. "\\>")
  2554. org-deadline-regexp (concat "\\<" org-deadline-string)
  2555. org-deadline-time-regexp
  2556. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2557. org-deadline-line-regexp
  2558. (concat "\\<\\(" org-deadline-string "\\).*")
  2559. org-scheduled-regexp
  2560. (concat "\\<" org-scheduled-string)
  2561. org-scheduled-time-regexp
  2562. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2563. org-closed-time-regexp
  2564. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2565. org-keyword-time-regexp
  2566. (concat "\\<\\(" org-scheduled-string
  2567. "\\|" org-deadline-string
  2568. "\\|" org-closed-string
  2569. "\\|" org-clock-string "\\)"
  2570. " *[[<]\\([^]>]+\\)[]>]")
  2571. org-keyword-time-not-clock-regexp
  2572. (concat "\\<\\(" org-scheduled-string
  2573. "\\|" org-deadline-string
  2574. "\\|" org-closed-string
  2575. "\\)"
  2576. " *[[<]\\([^]>]+\\)[]>]")
  2577. org-maybe-keyword-time-regexp
  2578. (concat "\\(\\<\\(" org-scheduled-string
  2579. "\\|" org-deadline-string
  2580. "\\|" org-closed-string
  2581. "\\|" org-clock-string "\\)\\)?"
  2582. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2583. org-planning-or-clock-line-re
  2584. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2585. "\\|" org-deadline-string
  2586. "\\|" org-closed-string "\\|" org-clock-string
  2587. "\\)\\>\\)")
  2588. )
  2589. (org-compute-latex-and-specials-regexp)
  2590. (org-set-font-lock-defaults)))
  2591. (defun org-extract-log-state-settings (x)
  2592. "Extract the log state setting from a TODO keyword string.
  2593. This will extract info from a string like \"WAIT(w@/!)\"."
  2594. (let (kw key log1 log2)
  2595. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2596. (setq kw (match-string 1 x)
  2597. key (and (match-end 2) (match-string 2 x))
  2598. log1 (and (match-end 3) (match-string 3 x))
  2599. log2 (and (match-end 4) (match-string 4 x)))
  2600. (and (or log1 log2)
  2601. (list kw
  2602. (and log1 (if (equal log1 "!") 'time 'note))
  2603. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2604. (defun org-remove-keyword-keys (list)
  2605. "Remove a pair of parenthesis at the end of each string in LIST."
  2606. (mapcar (lambda (x)
  2607. (if (string-match "(.*)$" x)
  2608. (substring x 0 (match-beginning 0))
  2609. x))
  2610. list))
  2611. ;; FIXME: this could be done much better, using second characters etc.
  2612. (defun org-assign-fast-keys (alist)
  2613. "Assign fast keys to a keyword-key alist.
  2614. Respect keys that are already there."
  2615. (let (new e k c c1 c2 (char ?a))
  2616. (while (setq e (pop alist))
  2617. (cond
  2618. ((equal e '(:startgroup)) (push e new))
  2619. ((equal e '(:endgroup)) (push e new))
  2620. (t
  2621. (setq k (car e) c2 nil)
  2622. (if (cdr e)
  2623. (setq c (cdr e))
  2624. ;; automatically assign a character.
  2625. (setq c1 (string-to-char
  2626. (downcase (substring
  2627. k (if (= (string-to-char k) ?@) 1 0)))))
  2628. (if (or (rassoc c1 new) (rassoc c1 alist))
  2629. (while (or (rassoc char new) (rassoc char alist))
  2630. (setq char (1+ char)))
  2631. (setq c2 c1))
  2632. (setq c (or c2 char)))
  2633. (push (cons k c) new))))
  2634. (nreverse new)))
  2635. ;;; Some variables used in various places
  2636. (defvar org-window-configuration nil
  2637. "Used in various places to store a window configuration.")
  2638. (defvar org-finish-function nil
  2639. "Function to be called when `C-c C-c' is used.
  2640. This is for getting out of special buffers like remember.")
  2641. ;; FIXME: Occasionally check by commenting these, to make sure
  2642. ;; no other functions uses these, forgetting to let-bind them.
  2643. (defvar entry)
  2644. (defvar state)
  2645. (defvar last-state)
  2646. (defvar date)
  2647. (defvar description)
  2648. ;; Defined somewhere in this file, but used before definition.
  2649. (defvar org-html-entities)
  2650. (defvar org-struct-menu)
  2651. (defvar org-org-menu)
  2652. (defvar org-tbl-menu)
  2653. (defvar org-agenda-keymap)
  2654. ;;;; Define the Org-mode
  2655. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2656. (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."))
  2657. ;; We use a before-change function to check if a table might need
  2658. ;; an update.
  2659. (defvar org-table-may-need-update t
  2660. "Indicates that a table might need an update.
  2661. This variable is set by `org-before-change-function'.
  2662. `org-table-align' sets it back to nil.")
  2663. (defun org-before-change-function (beg end)
  2664. "Every change indicates that a table might need an update."
  2665. (setq org-table-may-need-update t))
  2666. (defvar org-mode-map)
  2667. (defvar org-mode-hook nil)
  2668. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2669. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2670. (defvar org-table-buffer-is-an nil)
  2671. (defconst org-outline-regexp "\\*+ ")
  2672. ;;;###autoload
  2673. (define-derived-mode org-mode outline-mode "Org"
  2674. "Outline-based notes management and organizer, alias
  2675. \"Carsten's outline-mode for keeping track of everything.\"
  2676. Org-mode develops organizational tasks around a NOTES file which
  2677. contains information about projects as plain text. Org-mode is
  2678. implemented on top of outline-mode, which is ideal to keep the content
  2679. of large files well structured. It supports ToDo items, deadlines and
  2680. time stamps, which magically appear in the diary listing of the Emacs
  2681. calendar. Tables are easily created with a built-in table editor.
  2682. Plain text URL-like links connect to websites, emails (VM), Usenet
  2683. messages (Gnus), BBDB entries, and any files related to the project.
  2684. For printing and sharing of notes, an Org-mode file (or a part of it)
  2685. can be exported as a structured ASCII or HTML file.
  2686. The following commands are available:
  2687. \\{org-mode-map}"
  2688. ;; Get rid of Outline menus, they are not needed
  2689. ;; Need to do this here because define-derived-mode sets up
  2690. ;; the keymap so late. Still, it is a waste to call this each time
  2691. ;; we switch another buffer into org-mode.
  2692. (if (featurep 'xemacs)
  2693. (when (boundp 'outline-mode-menu-heading)
  2694. ;; Assume this is Greg's port, it used easymenu
  2695. (easy-menu-remove outline-mode-menu-heading)
  2696. (easy-menu-remove outline-mode-menu-show)
  2697. (easy-menu-remove outline-mode-menu-hide))
  2698. (define-key org-mode-map [menu-bar headings] 'undefined)
  2699. (define-key org-mode-map [menu-bar hide] 'undefined)
  2700. (define-key org-mode-map [menu-bar show] 'undefined))
  2701. (org-load-modules-maybe)
  2702. (easy-menu-add org-org-menu)
  2703. (easy-menu-add org-tbl-menu)
  2704. (org-install-agenda-files-menu)
  2705. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2706. (org-add-to-invisibility-spec '(org-cwidth))
  2707. (when (featurep 'xemacs)
  2708. (org-set-local 'line-move-ignore-invisible t))
  2709. (org-set-local 'outline-regexp org-outline-regexp)
  2710. (org-set-local 'outline-level 'org-outline-level)
  2711. (when (and org-ellipsis
  2712. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2713. (fboundp 'make-glyph-code))
  2714. (unless org-display-table
  2715. (setq org-display-table (make-display-table)))
  2716. (set-display-table-slot
  2717. org-display-table 4
  2718. (vconcat (mapcar
  2719. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2720. org-ellipsis)))
  2721. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2722. (setq buffer-display-table org-display-table))
  2723. (org-set-regexps-and-options)
  2724. ;; Calc embedded
  2725. (org-set-local 'calc-embedded-open-mode "# ")
  2726. (modify-syntax-entry ?# "<")
  2727. (modify-syntax-entry ?@ "w")
  2728. (if org-startup-truncated (setq truncate-lines t))
  2729. (org-set-local 'font-lock-unfontify-region-function
  2730. 'org-unfontify-region)
  2731. ;; Activate before-change-function
  2732. (org-set-local 'org-table-may-need-update t)
  2733. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2734. 'local)
  2735. ;; Check for running clock before killing a buffer
  2736. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2737. ;; Paragraphs and auto-filling
  2738. (org-set-autofill-regexps)
  2739. (setq indent-line-function 'org-indent-line-function)
  2740. (org-update-radio-target-regexp)
  2741. ;; Comment characters
  2742. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2743. (org-set-local 'comment-padding " ")
  2744. ;; Align options lines
  2745. (org-set-local
  2746. 'align-mode-rules-list
  2747. '((org-in-buffer-settings
  2748. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2749. (modes . '(org-mode)))))
  2750. ;; Imenu
  2751. (org-set-local 'imenu-create-index-function
  2752. 'org-imenu-get-tree)
  2753. ;; Make isearch reveal context
  2754. (if (or (featurep 'xemacs)
  2755. (not (boundp 'outline-isearch-open-invisible-function)))
  2756. ;; Emacs 21 and XEmacs make use of the hook
  2757. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2758. ;; Emacs 22 deals with this through a special variable
  2759. (org-set-local 'outline-isearch-open-invisible-function
  2760. (lambda (&rest ignore) (org-show-context 'isearch))))
  2761. ;; If empty file that did not turn on org-mode automatically, make it to.
  2762. (if (and org-insert-mode-line-in-empty-file
  2763. (interactive-p)
  2764. (= (point-min) (point-max)))
  2765. (insert "# -*- mode: org -*-\n\n"))
  2766. (unless org-inhibit-startup
  2767. (when org-startup-align-all-tables
  2768. (let ((bmp (buffer-modified-p)))
  2769. (org-table-map-tables 'org-table-align)
  2770. (set-buffer-modified-p bmp)))
  2771. (org-cycle-hide-drawers 'all)
  2772. (cond
  2773. ((eq org-startup-folded t)
  2774. (org-cycle '(4)))
  2775. ((eq org-startup-folded 'content)
  2776. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2777. (org-cycle '(4)) (org-cycle '(4)))))))
  2778. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2779. (defun org-current-time ()
  2780. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2781. (if (> (car org-time-stamp-rounding-minutes) 1)
  2782. (let ((r (car org-time-stamp-rounding-minutes))
  2783. (time (decode-time)))
  2784. (apply 'encode-time
  2785. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2786. (nthcdr 2 time))))
  2787. (current-time)))
  2788. ;;;; Font-Lock stuff, including the activators
  2789. (defvar org-mouse-map (make-sparse-keymap))
  2790. (org-defkey org-mouse-map
  2791. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2792. (org-defkey org-mouse-map
  2793. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2794. (when org-mouse-1-follows-link
  2795. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2796. (when org-tab-follows-link
  2797. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2798. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2799. (when org-return-follows-link
  2800. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2801. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2802. (require 'font-lock)
  2803. (defconst org-non-link-chars "]\t\n\r<>")
  2804. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2805. "shell" "elisp"))
  2806. (defvar org-link-types-re nil
  2807. "Matches a link that has a url-like prefix like \"http:\"")
  2808. (defvar org-link-re-with-space nil
  2809. "Matches a link with spaces, optional angular brackets around it.")
  2810. (defvar org-link-re-with-space2 nil
  2811. "Matches a link with spaces, optional angular brackets around it.")
  2812. (defvar org-angle-link-re nil
  2813. "Matches link with angular brackets, spaces are allowed.")
  2814. (defvar org-plain-link-re nil
  2815. "Matches plain link, without spaces.")
  2816. (defvar org-bracket-link-regexp nil
  2817. "Matches a link in double brackets.")
  2818. (defvar org-bracket-link-analytic-regexp nil
  2819. "Regular expression used to analyze links.
  2820. Here is what the match groups contain after a match:
  2821. 1: http:
  2822. 2: http
  2823. 3: path
  2824. 4: [desc]
  2825. 5: desc")
  2826. (defvar org-any-link-re nil
  2827. "Regular expression matching any link.")
  2828. (defun org-make-link-regexps ()
  2829. "Update the link regular expressions.
  2830. This should be called after the variable `org-link-types' has changed."
  2831. (setq org-link-types-re
  2832. (concat
  2833. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2834. org-link-re-with-space
  2835. (concat
  2836. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2837. "\\([^" org-non-link-chars " ]"
  2838. "[^" org-non-link-chars "]*"
  2839. "[^" org-non-link-chars " ]\\)>?")
  2840. org-link-re-with-space2
  2841. (concat
  2842. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2843. "\\([^" org-non-link-chars " ]"
  2844. "[^]\t\n\r]*"
  2845. "[^" org-non-link-chars " ]\\)>?")
  2846. org-angle-link-re
  2847. (concat
  2848. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2849. "\\([^" org-non-link-chars " ]"
  2850. "[^" org-non-link-chars "]*"
  2851. "\\)>")
  2852. org-plain-link-re
  2853. (concat
  2854. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2855. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2856. org-bracket-link-regexp
  2857. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2858. org-bracket-link-analytic-regexp
  2859. (concat
  2860. "\\[\\["
  2861. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2862. "\\([^]]+\\)"
  2863. "\\]"
  2864. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2865. "\\]")
  2866. org-any-link-re
  2867. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  2868. org-angle-link-re "\\)\\|\\("
  2869. org-plain-link-re "\\)")))
  2870. (org-make-link-regexps)
  2871. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  2872. "Regular expression for fast time stamp matching.")
  2873. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  2874. "Regular expression for fast time stamp matching.")
  2875. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2876. "Regular expression matching time strings for analysis.
  2877. This one does not require the space after the date, so it can be used
  2878. on a string that terminates immediately after the date.")
  2879. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2880. "Regular expression matching time strings for analysis.")
  2881. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  2882. "Regular expression matching time stamps, with groups.")
  2883. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  2884. "Regular expression matching time stamps (also [..]), with groups.")
  2885. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2886. "Regular expression matching a time stamp range.")
  2887. (defconst org-tr-regexp-both
  2888. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  2889. "Regular expression matching a time stamp range.")
  2890. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2891. org-ts-regexp "\\)?")
  2892. "Regular expression matching a time stamp or time stamp range.")
  2893. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  2894. org-ts-regexp-both "\\)?")
  2895. "Regular expression matching a time stamp or time stamp range.
  2896. The time stamps may be either active or inactive.")
  2897. (defvar org-emph-face nil)
  2898. (defun org-do-emphasis-faces (limit)
  2899. "Run through the buffer and add overlays to links."
  2900. (let (rtn)
  2901. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  2902. (if (not (= (char-after (match-beginning 3))
  2903. (char-after (match-beginning 4))))
  2904. (progn
  2905. (setq rtn t)
  2906. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2907. 'face
  2908. (nth 1 (assoc (match-string 3)
  2909. org-emphasis-alist)))
  2910. (add-text-properties (match-beginning 2) (match-end 2)
  2911. '(font-lock-multiline t))
  2912. (when org-hide-emphasis-markers
  2913. (add-text-properties (match-end 4) (match-beginning 5)
  2914. '(invisible org-link))
  2915. (add-text-properties (match-beginning 3) (match-end 3)
  2916. '(invisible org-link)))))
  2917. (backward-char 1))
  2918. rtn))
  2919. (defun org-emphasize (&optional char)
  2920. "Insert or change an emphasis, i.e. a font like bold or italic.
  2921. If there is an active region, change that region to a new emphasis.
  2922. If there is no region, just insert the marker characters and position
  2923. the cursor between them.
  2924. CHAR should be either the marker character, or the first character of the
  2925. HTML tag associated with that emphasis. If CHAR is a space, the means
  2926. to remove the emphasis of the selected region.
  2927. If char is not given (for example in an interactive call) it
  2928. will be prompted for."
  2929. (interactive)
  2930. (let ((eal org-emphasis-alist) e det
  2931. (erc org-emphasis-regexp-components)
  2932. (prompt "")
  2933. (string "") beg end move tag c s)
  2934. (if (org-region-active-p)
  2935. (setq beg (region-beginning) end (region-end)
  2936. string (buffer-substring beg end))
  2937. (setq move t))
  2938. (while (setq e (pop eal))
  2939. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  2940. c (aref tag 0))
  2941. (push (cons c (string-to-char (car e))) det)
  2942. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  2943. (substring tag 1)))))
  2944. (unless char
  2945. (message "%s" (concat "Emphasis marker or tag:" prompt))
  2946. (setq char (read-char-exclusive)))
  2947. (setq char (or (cdr (assoc char det)) char))
  2948. (if (equal char ?\ )
  2949. (setq s "" move nil)
  2950. (unless (assoc (char-to-string char) org-emphasis-alist)
  2951. (error "No such emphasis marker: \"%c\"" char))
  2952. (setq s (char-to-string char)))
  2953. (while (and (> (length string) 1)
  2954. (equal (substring string 0 1) (substring string -1))
  2955. (assoc (substring string 0 1) org-emphasis-alist))
  2956. (setq string (substring string 1 -1)))
  2957. (setq string (concat s string s))
  2958. (if beg (delete-region beg end))
  2959. (unless (or (bolp)
  2960. (string-match (concat "[" (nth 0 erc) "\n]")
  2961. (char-to-string (char-before (point)))))
  2962. (insert " "))
  2963. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  2964. (char-to-string (char-after (point))))
  2965. (insert " ") (backward-char 1))
  2966. (insert string)
  2967. (and move (backward-char 1))))
  2968. (defconst org-nonsticky-props
  2969. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  2970. (defun org-activate-plain-links (limit)
  2971. "Run through the buffer and add overlays to links."
  2972. (catch 'exit
  2973. (let (f)
  2974. (while (re-search-forward org-plain-link-re limit t)
  2975. (setq f (get-text-property (match-beginning 0) 'face))
  2976. (if (or (eq f 'org-tag)
  2977. (and (listp f) (memq 'org-tag f)))
  2978. nil
  2979. (add-text-properties (match-beginning 0) (match-end 0)
  2980. (list 'mouse-face 'highlight
  2981. 'rear-nonsticky org-nonsticky-props
  2982. 'keymap org-mouse-map
  2983. ))
  2984. (throw 'exit t))))))
  2985. (defun org-activate-code (limit)
  2986. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  2987. (unless (get-text-property (match-beginning 1) 'face)
  2988. (remove-text-properties (match-beginning 0) (match-end 0)
  2989. '(display t invisible t intangible t))
  2990. t)))
  2991. (defun org-activate-angle-links (limit)
  2992. "Run through the buffer and add overlays to links."
  2993. (if (re-search-forward org-angle-link-re limit t)
  2994. (progn
  2995. (add-text-properties (match-beginning 0) (match-end 0)
  2996. (list 'mouse-face 'highlight
  2997. 'rear-nonsticky org-nonsticky-props
  2998. 'keymap org-mouse-map
  2999. ))
  3000. t)))
  3001. (defun org-activate-bracket-links (limit)
  3002. "Run through the buffer and add overlays to bracketed links."
  3003. (if (re-search-forward org-bracket-link-regexp limit t)
  3004. (let* ((help (concat "LINK: "
  3005. (org-match-string-no-properties 1)))
  3006. ;; FIXME: above we should remove the escapes.
  3007. ;; but that requires another match, protecting match data,
  3008. ;; a lot of overhead for font-lock.
  3009. (ip (org-maybe-intangible
  3010. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3011. 'keymap org-mouse-map 'mouse-face 'highlight
  3012. 'font-lock-multiline t 'help-echo help)))
  3013. (vp (list 'rear-nonsticky org-nonsticky-props
  3014. 'keymap org-mouse-map 'mouse-face 'highlight
  3015. ' font-lock-multiline t 'help-echo help)))
  3016. ;; We need to remove the invisible property here. Table narrowing
  3017. ;; may have made some of this invisible.
  3018. (remove-text-properties (match-beginning 0) (match-end 0)
  3019. '(invisible nil))
  3020. (if (match-end 3)
  3021. (progn
  3022. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3023. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3024. (add-text-properties (match-end 3) (match-end 0) ip))
  3025. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3026. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3027. (add-text-properties (match-end 1) (match-end 0) ip))
  3028. t)))
  3029. (defun org-activate-dates (limit)
  3030. "Run through the buffer and add overlays to dates."
  3031. (if (re-search-forward org-tsr-regexp-both limit t)
  3032. (progn
  3033. (add-text-properties (match-beginning 0) (match-end 0)
  3034. (list 'mouse-face 'highlight
  3035. 'rear-nonsticky org-nonsticky-props
  3036. 'keymap org-mouse-map))
  3037. (when org-display-custom-times
  3038. (if (match-end 3)
  3039. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3040. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3041. t)))
  3042. (defvar org-target-link-regexp nil
  3043. "Regular expression matching radio targets in plain text.")
  3044. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3045. "Regular expression matching a link target.")
  3046. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3047. "Regular expression matching a radio target.")
  3048. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3049. "Regular expression matching any target.")
  3050. (defun org-activate-target-links (limit)
  3051. "Run through the buffer and add overlays to target matches."
  3052. (when org-target-link-regexp
  3053. (let ((case-fold-search t))
  3054. (if (re-search-forward org-target-link-regexp limit t)
  3055. (progn
  3056. (add-text-properties (match-beginning 0) (match-end 0)
  3057. (list 'mouse-face 'highlight
  3058. 'rear-nonsticky org-nonsticky-props
  3059. 'keymap org-mouse-map
  3060. 'help-echo "Radio target link"
  3061. 'org-linked-text t))
  3062. t)))))
  3063. (defun org-update-radio-target-regexp ()
  3064. "Find all radio targets in this file and update the regular expression."
  3065. (interactive)
  3066. (when (memq 'radio org-activate-links)
  3067. (setq org-target-link-regexp
  3068. (org-make-target-link-regexp (org-all-targets 'radio)))
  3069. (org-restart-font-lock)))
  3070. (defun org-hide-wide-columns (limit)
  3071. (let (s e)
  3072. (setq s (text-property-any (point) (or limit (point-max))
  3073. 'org-cwidth t))
  3074. (when s
  3075. (setq e (next-single-property-change s 'org-cwidth))
  3076. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3077. (goto-char e)
  3078. t)))
  3079. (defvar org-latex-and-specials-regexp nil
  3080. "Regular expression for highlighting export special stuff.")
  3081. (defvar org-match-substring-regexp)
  3082. (defvar org-match-substring-with-braces-regexp)
  3083. (defvar org-export-html-special-string-regexps)
  3084. (defun org-compute-latex-and-specials-regexp ()
  3085. "Compute regular expression for stuff treated specially by exporters."
  3086. (if (not org-highlight-latex-fragments-and-specials)
  3087. (org-set-local 'org-latex-and-specials-regexp nil)
  3088. (require 'org-exp)
  3089. (let*
  3090. ((matchers (plist-get org-format-latex-options :matchers))
  3091. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3092. org-latex-regexps)))
  3093. (options (org-combine-plists (org-default-export-plist)
  3094. (org-infile-export-plist)))
  3095. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3096. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3097. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3098. (org-export-html-expand (plist-get options :expand-quoted-html))
  3099. (org-export-with-special-strings (plist-get options :special-strings))
  3100. (re-sub
  3101. (cond
  3102. ((equal org-export-with-sub-superscripts '{})
  3103. (list org-match-substring-with-braces-regexp))
  3104. (org-export-with-sub-superscripts
  3105. (list org-match-substring-regexp))
  3106. (t nil)))
  3107. (re-latex
  3108. (if org-export-with-LaTeX-fragments
  3109. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3110. (re-macros
  3111. (if org-export-with-TeX-macros
  3112. (list (concat "\\\\"
  3113. (regexp-opt
  3114. (append (mapcar 'car org-html-entities)
  3115. (if (boundp 'org-latex-entities)
  3116. org-latex-entities nil))
  3117. 'words))) ; FIXME
  3118. ))
  3119. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3120. (re-special (if org-export-with-special-strings
  3121. (mapcar (lambda (x) (car x))
  3122. org-export-html-special-string-regexps)))
  3123. (re-rest
  3124. (delq nil
  3125. (list
  3126. (if org-export-html-expand "@<[^>\n]+>")
  3127. ))))
  3128. (org-set-local
  3129. 'org-latex-and-specials-regexp
  3130. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3131. re-rest) "\\|")))))
  3132. (defun org-do-latex-and-special-faces (limit)
  3133. "Run through the buffer and add overlays to links."
  3134. (when org-latex-and-specials-regexp
  3135. (let (rtn d)
  3136. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3137. limit t))
  3138. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3139. 'face))
  3140. '(org-code org-verbatim underline)))
  3141. (progn
  3142. (setq rtn t
  3143. d (cond ((member (char-after (1+ (match-beginning 0)))
  3144. '(?_ ?^)) 1)
  3145. (t 0)))
  3146. (font-lock-prepend-text-property
  3147. (+ d (match-beginning 0)) (match-end 0)
  3148. 'face 'org-latex-and-export-specials)
  3149. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3150. '(font-lock-multiline t)))))
  3151. rtn)))
  3152. (defun org-restart-font-lock ()
  3153. "Restart font-lock-mode, to force refontification."
  3154. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3155. (font-lock-mode -1)
  3156. (font-lock-mode 1)))
  3157. (defun org-all-targets (&optional radio)
  3158. "Return a list of all targets in this file.
  3159. With optional argument RADIO, only find radio targets."
  3160. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3161. rtn)
  3162. (save-excursion
  3163. (goto-char (point-min))
  3164. (while (re-search-forward re nil t)
  3165. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3166. rtn)))
  3167. (defun org-make-target-link-regexp (targets)
  3168. "Make regular expression matching all strings in TARGETS.
  3169. The regular expression finds the targets also if there is a line break
  3170. between words."
  3171. (and targets
  3172. (concat
  3173. "\\<\\("
  3174. (mapconcat
  3175. (lambda (x)
  3176. (while (string-match " +" x)
  3177. (setq x (replace-match "\\s-+" t t x)))
  3178. x)
  3179. targets
  3180. "\\|")
  3181. "\\)\\>")))
  3182. (defun org-activate-tags (limit)
  3183. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3184. (progn
  3185. (add-text-properties (match-beginning 1) (match-end 1)
  3186. (list 'mouse-face 'highlight
  3187. 'rear-nonsticky org-nonsticky-props
  3188. 'keymap org-mouse-map))
  3189. t)))
  3190. (defun org-outline-level ()
  3191. (save-excursion
  3192. (looking-at outline-regexp)
  3193. (if (match-beginning 1)
  3194. (+ (org-get-string-indentation (match-string 1)) 1000)
  3195. (1- (- (match-end 0) (match-beginning 0))))))
  3196. (defvar org-font-lock-keywords nil)
  3197. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3198. "Regular expression matching a property line.")
  3199. (defvar org-font-lock-hook nil
  3200. "Functions to be called for special font loch stuff.")
  3201. (defun org-font-lock-hook (limit)
  3202. (run-hook-with-args 'org-font-lock-hook limit))
  3203. (defun org-set-font-lock-defaults ()
  3204. (let* ((em org-fontify-emphasized-text)
  3205. (lk org-activate-links)
  3206. (org-font-lock-extra-keywords
  3207. (list
  3208. ;; Call the hook
  3209. '(org-font-lock-hook)
  3210. ;; Headlines
  3211. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3212. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3213. ;; Table lines
  3214. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3215. (1 'org-table t))
  3216. ;; Table internals
  3217. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3218. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3219. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3220. ;; Drawers
  3221. (list org-drawer-regexp '(0 'org-special-keyword t))
  3222. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3223. ;; Properties
  3224. (list org-property-re
  3225. '(1 'org-special-keyword t)
  3226. '(3 'org-property-value t))
  3227. (if org-format-transports-properties-p
  3228. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3229. ;; Links
  3230. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3231. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3232. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3233. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3234. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3235. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3236. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3237. '(org-hide-wide-columns (0 nil append))
  3238. ;; TODO lines
  3239. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3240. '(1 (org-get-todo-face 1) t))
  3241. ;; DONE
  3242. (if org-fontify-done-headline
  3243. (list (concat "^[*]+ +\\<\\("
  3244. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3245. "\\)\\(.*\\)")
  3246. '(2 'org-headline-done t))
  3247. nil)
  3248. ;; Priorities
  3249. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3250. ;; Special keywords
  3251. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3252. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3253. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3254. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3255. ;; Emphasis
  3256. (if em
  3257. (if (featurep 'xemacs)
  3258. '(org-do-emphasis-faces (0 nil append))
  3259. '(org-do-emphasis-faces)))
  3260. ;; Checkboxes
  3261. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3262. 2 'bold prepend)
  3263. (if org-provide-checkbox-statistics
  3264. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3265. (0 (org-get-checkbox-statistics-face) t)))
  3266. ;; Description list items
  3267. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*?\\) ::"
  3268. 2 'bold prepend)
  3269. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3270. '(1 'org-archived prepend))
  3271. ;; Specials
  3272. '(org-do-latex-and-special-faces)
  3273. ;; Code
  3274. '(org-activate-code (1 'org-code t))
  3275. ;; COMMENT
  3276. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3277. "\\|" org-quote-string "\\)\\>")
  3278. '(1 'org-special-keyword t))
  3279. '("^#.*" (0 'font-lock-comment-face t))
  3280. )))
  3281. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3282. ;; Now set the full font-lock-keywords
  3283. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3284. (org-set-local 'font-lock-defaults
  3285. '(org-font-lock-keywords t nil nil backward-paragraph))
  3286. (kill-local-variable 'font-lock-keywords) nil))
  3287. (defvar org-m nil)
  3288. (defvar org-l nil)
  3289. (defvar org-f nil)
  3290. (defun org-get-level-face (n)
  3291. "Get the right face for match N in font-lock matching of healdines."
  3292. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3293. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3294. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3295. (cond
  3296. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3297. ((eq n 2) org-f)
  3298. (t (if org-level-color-stars-only nil org-f))))
  3299. (defun org-get-todo-face (kwd)
  3300. "Get the right face for a TODO keyword KWD.
  3301. If KWD is a number, get the corresponding match group."
  3302. (if (numberp kwd) (setq kwd (match-string kwd)))
  3303. (or (cdr (assoc kwd org-todo-keyword-faces))
  3304. (and (member kwd org-done-keywords) 'org-done)
  3305. 'org-todo))
  3306. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3307. "Remove fontification and activation overlays from links."
  3308. (font-lock-default-unfontify-region beg end)
  3309. (let* ((buffer-undo-list t)
  3310. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3311. (inhibit-modification-hooks t)
  3312. deactivate-mark buffer-file-name buffer-file-truename)
  3313. (remove-text-properties beg end
  3314. '(mouse-face t keymap t org-linked-text t
  3315. invisible t intangible t))))
  3316. ;;;; Visibility cycling, including org-goto and indirect buffer
  3317. ;;; Cycling
  3318. (defvar org-cycle-global-status nil)
  3319. (make-variable-buffer-local 'org-cycle-global-status)
  3320. (defvar org-cycle-subtree-status nil)
  3321. (make-variable-buffer-local 'org-cycle-subtree-status)
  3322. ;;;###autoload
  3323. (defun org-cycle (&optional arg)
  3324. "Visibility cycling for Org-mode.
  3325. - When this function is called with a prefix argument, rotate the entire
  3326. buffer through 3 states (global cycling)
  3327. 1. OVERVIEW: Show only top-level headlines.
  3328. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3329. 3. SHOW ALL: Show everything.
  3330. - When point is at the beginning of a headline, rotate the subtree started
  3331. by this line through 3 different states (local cycling)
  3332. 1. FOLDED: Only the main headline is shown.
  3333. 2. CHILDREN: The main headline and the direct children are shown.
  3334. From this state, you can move to one of the children
  3335. and zoom in further.
  3336. 3. SUBTREE: Show the entire subtree, including body text.
  3337. - When there is a numeric prefix, go up to a heading with level ARG, do
  3338. a `show-subtree' and return to the previous cursor position. If ARG
  3339. is negative, go up that many levels.
  3340. - When point is not at the beginning of a headline, execute
  3341. `indent-relative', like TAB normally does. See the option
  3342. `org-cycle-emulate-tab' for details.
  3343. - Special case: if point is at the beginning of the buffer and there is
  3344. no headline in line 1, this function will act as if called with prefix arg.
  3345. But only if also the variable `org-cycle-global-at-bob' is t."
  3346. (interactive "P")
  3347. (org-load-modules-maybe)
  3348. (let* ((outline-regexp
  3349. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3350. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3351. outline-regexp))
  3352. (bob-special (and org-cycle-global-at-bob (bobp)
  3353. (not (looking-at outline-regexp))))
  3354. (org-cycle-hook
  3355. (if bob-special
  3356. (delq 'org-optimize-window-after-visibility-change
  3357. (copy-sequence org-cycle-hook))
  3358. org-cycle-hook))
  3359. (pos (point)))
  3360. (if (or bob-special (equal arg '(4)))
  3361. ;; special case: use global cycling
  3362. (setq arg t))
  3363. (cond
  3364. ((org-at-table-p 'any)
  3365. ;; Enter the table or move to the next field in the table
  3366. (or (org-table-recognize-table.el)
  3367. (progn
  3368. (if arg (org-table-edit-field t)
  3369. (org-table-justify-field-maybe)
  3370. (call-interactively 'org-table-next-field)))))
  3371. ((eq arg t) ;; Global cycling
  3372. (cond
  3373. ((and (eq last-command this-command)
  3374. (eq org-cycle-global-status 'overview))
  3375. ;; We just created the overview - now do table of contents
  3376. ;; This can be slow in very large buffers, so indicate action
  3377. (message "CONTENTS...")
  3378. (org-content)
  3379. (message "CONTENTS...done")
  3380. (setq org-cycle-global-status 'contents)
  3381. (run-hook-with-args 'org-cycle-hook 'contents))
  3382. ((and (eq last-command this-command)
  3383. (eq org-cycle-global-status 'contents))
  3384. ;; We just showed the table of contents - now show everything
  3385. (show-all)
  3386. (message "SHOW ALL")
  3387. (setq org-cycle-global-status 'all)
  3388. (run-hook-with-args 'org-cycle-hook 'all))
  3389. (t
  3390. ;; Default action: go to overview
  3391. (org-overview)
  3392. (message "OVERVIEW")
  3393. (setq org-cycle-global-status 'overview)
  3394. (run-hook-with-args 'org-cycle-hook 'overview))))
  3395. ((and org-drawers org-drawer-regexp
  3396. (save-excursion
  3397. (beginning-of-line 1)
  3398. (looking-at org-drawer-regexp)))
  3399. ;; Toggle block visibility
  3400. (org-flag-drawer
  3401. (not (get-char-property (match-end 0) 'invisible))))
  3402. ((integerp arg)
  3403. ;; Show-subtree, ARG levels up from here.
  3404. (save-excursion
  3405. (org-back-to-heading)
  3406. (outline-up-heading (if (< arg 0) (- arg)
  3407. (- (funcall outline-level) arg)))
  3408. (org-show-subtree)))
  3409. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3410. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3411. ;; At a heading: rotate between three different views
  3412. (org-back-to-heading)
  3413. (let ((goal-column 0) eoh eol eos)
  3414. ;; First, some boundaries
  3415. (save-excursion
  3416. (org-back-to-heading)
  3417. (save-excursion
  3418. (beginning-of-line 2)
  3419. (while (and (not (eobp)) ;; this is like `next-line'
  3420. (get-char-property (1- (point)) 'invisible))
  3421. (beginning-of-line 2)) (setq eol (point)))
  3422. (outline-end-of-heading) (setq eoh (point))
  3423. (org-end-of-subtree t)
  3424. (unless (eobp)
  3425. (skip-chars-forward " \t\n")
  3426. (beginning-of-line 1) ; in case this is an item
  3427. )
  3428. (setq eos (1- (point))))
  3429. ;; Find out what to do next and set `this-command'
  3430. (cond
  3431. ((= eos eoh)
  3432. ;; Nothing is hidden behind this heading
  3433. (message "EMPTY ENTRY")
  3434. (setq org-cycle-subtree-status nil)
  3435. (save-excursion
  3436. (goto-char eos)
  3437. (outline-next-heading)
  3438. (if (org-invisible-p) (org-flag-heading nil))))
  3439. ((or (>= eol eos)
  3440. (not (string-match "\\S-" (buffer-substring eol eos))))
  3441. ;; Entire subtree is hidden in one line: open it
  3442. (org-show-entry)
  3443. (show-children)
  3444. (message "CHILDREN")
  3445. (save-excursion
  3446. (goto-char eos)
  3447. (outline-next-heading)
  3448. (if (org-invisible-p) (org-flag-heading nil)))
  3449. (setq org-cycle-subtree-status 'children)
  3450. (run-hook-with-args 'org-cycle-hook 'children))
  3451. ((and (eq last-command this-command)
  3452. (eq org-cycle-subtree-status 'children))
  3453. ;; We just showed the children, now show everything.
  3454. (org-show-subtree)
  3455. (message "SUBTREE")
  3456. (setq org-cycle-subtree-status 'subtree)
  3457. (run-hook-with-args 'org-cycle-hook 'subtree))
  3458. (t
  3459. ;; Default action: hide the subtree.
  3460. (hide-subtree)
  3461. (message "FOLDED")
  3462. (setq org-cycle-subtree-status 'folded)
  3463. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3464. ;; TAB emulation
  3465. (buffer-read-only (org-back-to-heading))
  3466. ((org-try-cdlatex-tab))
  3467. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3468. (or (not (bolp))
  3469. (not (looking-at outline-regexp))))
  3470. (call-interactively (global-key-binding "\t")))
  3471. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3472. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3473. (or (and (eq org-cycle-emulate-tab 'white)
  3474. (= (match-end 0) (point-at-eol)))
  3475. (and (eq org-cycle-emulate-tab 'whitestart)
  3476. (>= (match-end 0) pos))))
  3477. t
  3478. (eq org-cycle-emulate-tab t))
  3479. (call-interactively (global-key-binding "\t")))
  3480. (t (save-excursion
  3481. (org-back-to-heading)
  3482. (org-cycle))))))
  3483. ;;;###autoload
  3484. (defun org-global-cycle (&optional arg)
  3485. "Cycle the global visibility. For details see `org-cycle'."
  3486. (interactive "P")
  3487. (let ((org-cycle-include-plain-lists
  3488. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3489. (if (integerp arg)
  3490. (progn
  3491. (show-all)
  3492. (hide-sublevels arg)
  3493. (setq org-cycle-global-status 'contents))
  3494. (org-cycle '(4)))))
  3495. (defun org-overview ()
  3496. "Switch to overview mode, shoing only top-level headlines.
  3497. Really, this shows all headlines with level equal or greater than the level
  3498. of the first headline in the buffer. This is important, because if the
  3499. first headline is not level one, then (hide-sublevels 1) gives confusing
  3500. results."
  3501. (interactive)
  3502. (let ((level (save-excursion
  3503. (goto-char (point-min))
  3504. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3505. (progn
  3506. (goto-char (match-beginning 0))
  3507. (funcall outline-level))))))
  3508. (and level (hide-sublevels level))))
  3509. (defun org-content (&optional arg)
  3510. "Show all headlines in the buffer, like a table of contents.
  3511. With numerical argument N, show content up to level N."
  3512. (interactive "P")
  3513. (save-excursion
  3514. ;; Visit all headings and show their offspring
  3515. (and (integerp arg) (org-overview))
  3516. (goto-char (point-max))
  3517. (catch 'exit
  3518. (while (and (progn (condition-case nil
  3519. (outline-previous-visible-heading 1)
  3520. (error (goto-char (point-min))))
  3521. t)
  3522. (looking-at outline-regexp))
  3523. (if (integerp arg)
  3524. (show-children (1- arg))
  3525. (show-branches))
  3526. (if (bobp) (throw 'exit nil))))))
  3527. (defun org-optimize-window-after-visibility-change (state)
  3528. "Adjust the window after a change in outline visibility.
  3529. This function is the default value of the hook `org-cycle-hook'."
  3530. (when (get-buffer-window (current-buffer))
  3531. (cond
  3532. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3533. ; ((eq state 'overview) (org-beginning-of-line))
  3534. ((eq state 'content) nil)
  3535. ((eq state 'all) nil)
  3536. ((eq state 'folded) nil)
  3537. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3538. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3539. (defun org-compact-display-after-subtree-move ()
  3540. (let (beg end)
  3541. (save-excursion
  3542. (if (org-up-heading-safe)
  3543. (progn
  3544. (hide-subtree)
  3545. (show-entry)
  3546. (show-children)
  3547. (org-cycle-show-empty-lines 'children)
  3548. (org-cycle-hide-drawers 'children))
  3549. (org-overview)))))
  3550. (defun org-cycle-show-empty-lines (state)
  3551. "Show empty lines above all visible headlines.
  3552. The region to be covered depends on STATE when called through
  3553. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3554. entire buffer covered. Note that an empty line is only shown if there
  3555. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3556. (when (> org-cycle-separator-lines 0)
  3557. (save-excursion
  3558. (let* ((n org-cycle-separator-lines)
  3559. (re (cond
  3560. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3561. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3562. (t (let ((ns (number-to-string (- n 2))))
  3563. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3564. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3565. beg end)
  3566. (cond
  3567. ((memq state '(overview contents t))
  3568. (setq beg (point-min) end (point-max)))
  3569. ((memq state '(children folded))
  3570. (setq beg (point) end (progn (org-end-of-subtree t t)
  3571. (beginning-of-line 2)
  3572. (point)))))
  3573. (when beg
  3574. (goto-char beg)
  3575. (while (re-search-forward re end t)
  3576. (if (not (get-char-property (match-end 1) 'invisible))
  3577. (outline-flag-region
  3578. (match-beginning 1) (match-end 1) nil)))))))
  3579. ;; Never hide empty lines at the end of the file.
  3580. (save-excursion
  3581. (goto-char (point-max))
  3582. (outline-previous-heading)
  3583. (outline-end-of-heading)
  3584. (if (and (looking-at "[ \t\n]+")
  3585. (= (match-end 0) (point-max)))
  3586. (outline-flag-region (point) (match-end 0) nil))))
  3587. (defun org-cycle-hide-drawers (state)
  3588. "Re-hide all drawers after a visibility state change."
  3589. (when (and (org-mode-p)
  3590. (not (memq state '(overview folded))))
  3591. (save-excursion
  3592. (let* ((globalp (memq state '(contents all)))
  3593. (beg (if globalp (point-min) (point)))
  3594. (end (if globalp (point-max) (org-end-of-subtree t))))
  3595. (goto-char beg)
  3596. (while (re-search-forward org-drawer-regexp end t)
  3597. (org-flag-drawer t))))))
  3598. (defun org-flag-drawer (flag)
  3599. (save-excursion
  3600. (beginning-of-line 1)
  3601. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3602. (let ((b (match-end 0))
  3603. (outline-regexp org-outline-regexp))
  3604. (if (re-search-forward
  3605. "^[ \t]*:END:"
  3606. (save-excursion (outline-next-heading) (point)) t)
  3607. (outline-flag-region b (point-at-eol) flag)
  3608. (error ":END: line missing"))))))
  3609. (defun org-subtree-end-visible-p ()
  3610. "Is the end of the current subtree visible?"
  3611. (pos-visible-in-window-p
  3612. (save-excursion (org-end-of-subtree t) (point))))
  3613. (defun org-first-headline-recenter (&optional N)
  3614. "Move cursor to the first headline and recenter the headline.
  3615. Optional argument N means, put the headline into the Nth line of the window."
  3616. (goto-char (point-min))
  3617. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3618. (beginning-of-line)
  3619. (recenter (prefix-numeric-value N))))
  3620. ;;; Org-goto
  3621. (defvar org-goto-window-configuration nil)
  3622. (defvar org-goto-marker nil)
  3623. (defvar org-goto-map
  3624. (let ((map (make-sparse-keymap)))
  3625. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3626. (while (setq cmd (pop cmds))
  3627. (substitute-key-definition cmd cmd map global-map)))
  3628. (suppress-keymap map)
  3629. (org-defkey map "\C-m" 'org-goto-ret)
  3630. (org-defkey map [(return)] 'org-goto-ret)
  3631. (org-defkey map [(left)] 'org-goto-left)
  3632. (org-defkey map [(right)] 'org-goto-right)
  3633. (org-defkey map [(control ?g)] 'org-goto-quit)
  3634. (org-defkey map "\C-i" 'org-cycle)
  3635. (org-defkey map [(tab)] 'org-cycle)
  3636. (org-defkey map [(down)] 'outline-next-visible-heading)
  3637. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3638. (if org-goto-auto-isearch
  3639. (if (fboundp 'define-key-after)
  3640. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3641. nil)
  3642. (org-defkey map "q" 'org-goto-quit)
  3643. (org-defkey map "n" 'outline-next-visible-heading)
  3644. (org-defkey map "p" 'outline-previous-visible-heading)
  3645. (org-defkey map "f" 'outline-forward-same-level)
  3646. (org-defkey map "b" 'outline-backward-same-level)
  3647. (org-defkey map "u" 'outline-up-heading))
  3648. (org-defkey map "/" 'org-occur)
  3649. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3650. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3651. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3652. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3653. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3654. map))
  3655. (defconst org-goto-help
  3656. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3657. RET=jump to location [Q]uit and return to previous location
  3658. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3659. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3660. (defun org-goto (&optional alternative-interface)
  3661. "Look up a different location in the current file, keeping current visibility.
  3662. When you want look-up or go to a different location in a document, the
  3663. fastest way is often to fold the entire buffer and then dive into the tree.
  3664. This method has the disadvantage, that the previous location will be folded,
  3665. which may not be what you want.
  3666. This command works around this by showing a copy of the current buffer
  3667. in an indirect buffer, in overview mode. You can dive into the tree in
  3668. that copy, use org-occur and incremental search to find a location.
  3669. When pressing RET or `Q', the command returns to the original buffer in
  3670. which the visibility is still unchanged. After RET is will also jump to
  3671. the location selected in the indirect buffer and expose the
  3672. the headline hierarchy above."
  3673. (interactive "P")
  3674. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3675. (org-refile-use-outline-path t)
  3676. (interface
  3677. (if (not alternative-interface)
  3678. org-goto-interface
  3679. (if (eq org-goto-interface 'outline)
  3680. 'outline-path-completion
  3681. 'outline)))
  3682. (org-goto-start-pos (point))
  3683. (selected-point
  3684. (if (eq interface 'outline)
  3685. (car (org-get-location (current-buffer) org-goto-help))
  3686. (nth 3 (org-refile-get-location "Goto: ")))))
  3687. (if selected-point
  3688. (progn
  3689. (org-mark-ring-push org-goto-start-pos)
  3690. (goto-char selected-point)
  3691. (if (or (org-invisible-p) (org-invisible-p2))
  3692. (org-show-context 'org-goto)))
  3693. (message "Quit"))))
  3694. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3695. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3696. (defvar org-goto-local-auto-isearch-map) ; defined below
  3697. (defun org-get-location (buf help)
  3698. "Let the user select a location in the Org-mode buffer BUF.
  3699. This function uses a recursive edit. It returns the selected position
  3700. or nil."
  3701. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3702. (isearch-hide-immediately nil)
  3703. (isearch-search-fun-function
  3704. (lambda () 'org-goto-local-search-forward-headings))
  3705. (org-goto-selected-point org-goto-exit-command))
  3706. (save-excursion
  3707. (save-window-excursion
  3708. (delete-other-windows)
  3709. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3710. (switch-to-buffer
  3711. (condition-case nil
  3712. (make-indirect-buffer (current-buffer) "*org-goto*")
  3713. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3714. (with-output-to-temp-buffer "*Help*"
  3715. (princ help))
  3716. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3717. (setq buffer-read-only nil)
  3718. (let ((org-startup-truncated t)
  3719. (org-startup-folded nil)
  3720. (org-startup-align-all-tables nil))
  3721. (org-mode)
  3722. (org-overview))
  3723. (setq buffer-read-only t)
  3724. (if (and (boundp 'org-goto-start-pos)
  3725. (integer-or-marker-p org-goto-start-pos))
  3726. (let ((org-show-hierarchy-above t)
  3727. (org-show-siblings t)
  3728. (org-show-following-heading t))
  3729. (goto-char org-goto-start-pos)
  3730. (and (org-invisible-p) (org-show-context)))
  3731. (goto-char (point-min)))
  3732. (org-beginning-of-line)
  3733. (message "Select location and press RET")
  3734. (use-local-map org-goto-map)
  3735. (recursive-edit)
  3736. ))
  3737. (kill-buffer "*org-goto*")
  3738. (cons org-goto-selected-point org-goto-exit-command)))
  3739. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3740. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3741. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3742. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3743. (defun org-goto-local-search-forward-headings (string bound noerror)
  3744. "Search and make sure that anu matches are in headlines."
  3745. (catch 'return
  3746. (while (search-forward string bound noerror)
  3747. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3748. (and (member :headline context)
  3749. (not (member :tags context))))
  3750. (throw 'return (point))))))
  3751. (defun org-goto-local-auto-isearch ()
  3752. "Start isearch."
  3753. (interactive)
  3754. (goto-char (point-min))
  3755. (let ((keys (this-command-keys)))
  3756. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3757. (isearch-mode t)
  3758. (isearch-process-search-char (string-to-char keys)))))
  3759. (defun org-goto-ret (&optional arg)
  3760. "Finish `org-goto' by going to the new location."
  3761. (interactive "P")
  3762. (setq org-goto-selected-point (point)
  3763. org-goto-exit-command 'return)
  3764. (throw 'exit nil))
  3765. (defun org-goto-left ()
  3766. "Finish `org-goto' by going to the new location."
  3767. (interactive)
  3768. (if (org-on-heading-p)
  3769. (progn
  3770. (beginning-of-line 1)
  3771. (setq org-goto-selected-point (point)
  3772. org-goto-exit-command 'left)
  3773. (throw 'exit nil))
  3774. (error "Not on a heading")))
  3775. (defun org-goto-right ()
  3776. "Finish `org-goto' by going to the new location."
  3777. (interactive)
  3778. (if (org-on-heading-p)
  3779. (progn
  3780. (setq org-goto-selected-point (point)
  3781. org-goto-exit-command 'right)
  3782. (throw 'exit nil))
  3783. (error "Not on a heading")))
  3784. (defun org-goto-quit ()
  3785. "Finish `org-goto' without cursor motion."
  3786. (interactive)
  3787. (setq org-goto-selected-point nil)
  3788. (setq org-goto-exit-command 'quit)
  3789. (throw 'exit nil))
  3790. ;;; Indirect buffer display of subtrees
  3791. (defvar org-indirect-dedicated-frame nil
  3792. "This is the frame being used for indirect tree display.")
  3793. (defvar org-last-indirect-buffer nil)
  3794. (defun org-tree-to-indirect-buffer (&optional arg)
  3795. "Create indirect buffer and narrow it to current subtree.
  3796. With numerical prefix ARG, go up to this level and then take that tree.
  3797. If ARG is negative, go up that many levels.
  3798. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  3799. indirect buffer previously made with this command, to avoid proliferation of
  3800. indirect buffers. However, when you call the command with a `C-u' prefix, or
  3801. when `org-indirect-buffer-display' is `new-frame', the last buffer
  3802. is kept so that you can work with several indirect buffers at the same time.
  3803. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  3804. requests that a new frame be made for the new buffer, so that the dedicated
  3805. frame is not changed."
  3806. (interactive "P")
  3807. (let ((cbuf (current-buffer))
  3808. (cwin (selected-window))
  3809. (pos (point))
  3810. beg end level heading ibuf)
  3811. (save-excursion
  3812. (org-back-to-heading t)
  3813. (when (numberp arg)
  3814. (setq level (org-outline-level))
  3815. (if (< arg 0) (setq arg (+ level arg)))
  3816. (while (> (setq level (org-outline-level)) arg)
  3817. (outline-up-heading 1 t)))
  3818. (setq beg (point)
  3819. heading (org-get-heading))
  3820. (org-end-of-subtree t) (setq end (point)))
  3821. (if (and (buffer-live-p org-last-indirect-buffer)
  3822. (not (eq org-indirect-buffer-display 'new-frame))
  3823. (not arg))
  3824. (kill-buffer org-last-indirect-buffer))
  3825. (setq ibuf (org-get-indirect-buffer cbuf)
  3826. org-last-indirect-buffer ibuf)
  3827. (cond
  3828. ((or (eq org-indirect-buffer-display 'new-frame)
  3829. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  3830. (select-frame (make-frame))
  3831. (delete-other-windows)
  3832. (switch-to-buffer ibuf)
  3833. (org-set-frame-title heading))
  3834. ((eq org-indirect-buffer-display 'dedicated-frame)
  3835. (raise-frame
  3836. (select-frame (or (and org-indirect-dedicated-frame
  3837. (frame-live-p org-indirect-dedicated-frame)
  3838. org-indirect-dedicated-frame)
  3839. (setq org-indirect-dedicated-frame (make-frame)))))
  3840. (delete-other-windows)
  3841. (switch-to-buffer ibuf)
  3842. (org-set-frame-title (concat "Indirect: " heading)))
  3843. ((eq org-indirect-buffer-display 'current-window)
  3844. (switch-to-buffer ibuf))
  3845. ((eq org-indirect-buffer-display 'other-window)
  3846. (pop-to-buffer ibuf))
  3847. (t (error "Invalid value.")))
  3848. (if (featurep 'xemacs)
  3849. (save-excursion (org-mode) (turn-on-font-lock)))
  3850. (narrow-to-region beg end)
  3851. (show-all)
  3852. (goto-char pos)
  3853. (and (window-live-p cwin) (select-window cwin))))
  3854. (defun org-get-indirect-buffer (&optional buffer)
  3855. (setq buffer (or buffer (current-buffer)))
  3856. (let ((n 1) (base (buffer-name buffer)) bname)
  3857. (while (buffer-live-p
  3858. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  3859. (setq n (1+ n)))
  3860. (condition-case nil
  3861. (make-indirect-buffer buffer bname 'clone)
  3862. (error (make-indirect-buffer buffer bname)))))
  3863. (defun org-set-frame-title (title)
  3864. "Set the title of the current frame to the string TITLE."
  3865. ;; FIXME: how to name a single frame in XEmacs???
  3866. (unless (featurep 'xemacs)
  3867. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  3868. ;;;; Structure editing
  3869. ;;; Inserting headlines
  3870. (defun org-insert-heading (&optional force-heading)
  3871. "Insert a new heading or item with same depth at point.
  3872. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3873. If point is at the beginning of a headline, insert a sibling before the
  3874. current headline. If point is not at the beginning, do not split the line,
  3875. but create the new hedline after the current line."
  3876. (interactive "P")
  3877. (if (= (buffer-size) 0)
  3878. (insert "\n* ")
  3879. (when (or force-heading (not (org-insert-item)))
  3880. (let* ((head (save-excursion
  3881. (condition-case nil
  3882. (progn
  3883. (org-back-to-heading)
  3884. (match-string 0))
  3885. (error "*"))))
  3886. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  3887. pos)
  3888. (cond
  3889. ((and (org-on-heading-p) (bolp)
  3890. (or (bobp)
  3891. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  3892. ;; insert before the current line
  3893. (open-line (if blank 2 1)))
  3894. ((and (bolp)
  3895. (or (bobp)
  3896. (save-excursion
  3897. (backward-char 1) (not (org-invisible-p)))))
  3898. ;; insert right here
  3899. nil)
  3900. (t
  3901. ;; in the middle of the line
  3902. (org-show-entry)
  3903. (let ((split
  3904. (org-get-alist-option org-M-RET-may-split-line 'headline))
  3905. tags pos)
  3906. (if (org-on-heading-p)
  3907. (progn
  3908. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3909. (setq tags (and (match-end 2) (match-string 2)))
  3910. (and (match-end 1)
  3911. (delete-region (match-beginning 1) (match-end 1)))
  3912. (setq pos (point-at-bol))
  3913. (or split (end-of-line 1))
  3914. (delete-horizontal-space)
  3915. (newline (if blank 2 1))
  3916. (when tags
  3917. (save-excursion
  3918. (goto-char pos)
  3919. (end-of-line 1)
  3920. (insert " " tags)
  3921. (org-set-tags nil 'align))))
  3922. (or split (end-of-line 1))
  3923. (newline (if blank 2 1))))))
  3924. (insert head) (just-one-space)
  3925. (setq pos (point))
  3926. (end-of-line 1)
  3927. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3928. (run-hooks 'org-insert-heading-hook)))))
  3929. (defun org-get-heading (&optional no-tags)
  3930. "Return the heading of the current entry, without the stars."
  3931. (save-excursion
  3932. (org-back-to-heading t)
  3933. (if (looking-at
  3934. (if no-tags
  3935. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  3936. "\\*+[ \t]+\\([^\r\n]*\\)"))
  3937. (match-string 1) "")))
  3938. (defun org-insert-heading-after-current ()
  3939. "Insert a new heading with same level as current, after current subtree."
  3940. (interactive)
  3941. (org-back-to-heading)
  3942. (org-insert-heading)
  3943. (org-move-subtree-down)
  3944. (end-of-line 1))
  3945. (defun org-insert-todo-heading (arg)
  3946. "Insert a new heading with the same level and TODO state as current heading.
  3947. If the heading has no TODO state, or if the state is DONE, use the first
  3948. state (TODO by default). Also with prefix arg, force first state."
  3949. (interactive "P")
  3950. (when (not (org-insert-item 'checkbox))
  3951. (org-insert-heading)
  3952. (save-excursion
  3953. (org-back-to-heading)
  3954. (outline-previous-heading)
  3955. (looking-at org-todo-line-regexp))
  3956. (if (or arg
  3957. (not (match-beginning 2))
  3958. (member (match-string 2) org-done-keywords))
  3959. (insert (car org-todo-keywords-1) " ")
  3960. (insert (match-string 2) " "))))
  3961. (defun org-insert-subheading (arg)
  3962. "Insert a new subheading and demote it.
  3963. Works for outline headings and for plain lists alike."
  3964. (interactive "P")
  3965. (org-insert-heading arg)
  3966. (cond
  3967. ((org-on-heading-p) (org-do-demote))
  3968. ((org-at-item-p) (org-indent-item 1))))
  3969. (defun org-insert-todo-subheading (arg)
  3970. "Insert a new subheading with TODO keyword or checkbox and demote it.
  3971. Works for outline headings and for plain lists alike."
  3972. (interactive "P")
  3973. (org-insert-todo-heading arg)
  3974. (cond
  3975. ((org-on-heading-p) (org-do-demote))
  3976. ((org-at-item-p) (org-indent-item 1))))
  3977. ;;; Promotion and Demotion
  3978. (defun org-promote-subtree ()
  3979. "Promote the entire subtree.
  3980. See also `org-promote'."
  3981. (interactive)
  3982. (save-excursion
  3983. (org-map-tree 'org-promote))
  3984. (org-fix-position-after-promote))
  3985. (defun org-demote-subtree ()
  3986. "Demote the entire subtree. See `org-demote'.
  3987. See also `org-promote'."
  3988. (interactive)
  3989. (save-excursion
  3990. (org-map-tree 'org-demote))
  3991. (org-fix-position-after-promote))
  3992. (defun org-do-promote ()
  3993. "Promote the current heading higher up the tree.
  3994. If the region is active in `transient-mark-mode', promote all headings
  3995. in the region."
  3996. (interactive)
  3997. (save-excursion
  3998. (if (org-region-active-p)
  3999. (org-map-region 'org-promote (region-beginning) (region-end))
  4000. (org-promote)))
  4001. (org-fix-position-after-promote))
  4002. (defun org-do-demote ()
  4003. "Demote the current heading lower down the tree.
  4004. If the region is active in `transient-mark-mode', demote all headings
  4005. in the region."
  4006. (interactive)
  4007. (save-excursion
  4008. (if (org-region-active-p)
  4009. (org-map-region 'org-demote (region-beginning) (region-end))
  4010. (org-demote)))
  4011. (org-fix-position-after-promote))
  4012. (defun org-fix-position-after-promote ()
  4013. "Make sure that after pro/demotion cursor position is right."
  4014. (let ((pos (point)))
  4015. (when (save-excursion
  4016. (beginning-of-line 1)
  4017. (looking-at org-todo-line-regexp)
  4018. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4019. (cond ((eobp) (insert " "))
  4020. ((eolp) (insert " "))
  4021. ((equal (char-after) ?\ ) (forward-char 1))))))
  4022. (defun org-reduced-level (l)
  4023. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4024. (defun org-get-valid-level (level &optional change)
  4025. "Rectify a level change under the influence of `org-odd-levels-only'
  4026. LEVEL is a current level, CHANGE is by how much the level should be
  4027. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4028. even level numbers will become the next higher odd number."
  4029. (if org-odd-levels-only
  4030. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4031. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4032. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4033. (max 1 (+ level change))))
  4034. (if (boundp 'define-obsolete-function-alias)
  4035. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4036. (define-obsolete-function-alias 'org-get-legal-level
  4037. 'org-get-valid-level)
  4038. (define-obsolete-function-alias 'org-get-legal-level
  4039. 'org-get-valid-level "23.1")))
  4040. (defun org-promote ()
  4041. "Promote the current heading higher up the tree.
  4042. If the region is active in `transient-mark-mode', promote all headings
  4043. in the region."
  4044. (org-back-to-heading t)
  4045. (let* ((level (save-match-data (funcall outline-level)))
  4046. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4047. (diff (abs (- level (length up-head) -1))))
  4048. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4049. (replace-match up-head nil t)
  4050. ;; Fixup tag positioning
  4051. (and org-auto-align-tags (org-set-tags nil t))
  4052. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4053. (defun org-demote ()
  4054. "Demote the current heading lower down the tree.
  4055. If the region is active in `transient-mark-mode', demote all headings
  4056. in the region."
  4057. (org-back-to-heading t)
  4058. (let* ((level (save-match-data (funcall outline-level)))
  4059. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4060. (diff (abs (- level (length down-head) -1))))
  4061. (replace-match down-head nil t)
  4062. ;; Fixup tag positioning
  4063. (and org-auto-align-tags (org-set-tags nil t))
  4064. (if org-adapt-indentation (org-fixup-indentation diff))))
  4065. (defun org-map-tree (fun)
  4066. "Call FUN for every heading underneath the current one."
  4067. (org-back-to-heading)
  4068. (let ((level (funcall outline-level)))
  4069. (save-excursion
  4070. (funcall fun)
  4071. (while (and (progn
  4072. (outline-next-heading)
  4073. (> (funcall outline-level) level))
  4074. (not (eobp)))
  4075. (funcall fun)))))
  4076. (defun org-map-region (fun beg end)
  4077. "Call FUN for every heading between BEG and END."
  4078. (let ((org-ignore-region t))
  4079. (save-excursion
  4080. (setq end (copy-marker end))
  4081. (goto-char beg)
  4082. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4083. (< (point) end))
  4084. (funcall fun))
  4085. (while (and (progn
  4086. (outline-next-heading)
  4087. (< (point) end))
  4088. (not (eobp)))
  4089. (funcall fun)))))
  4090. (defun org-fixup-indentation (diff)
  4091. "Change the indentation in the current entry by DIFF
  4092. However, if any line in the current entry has no indentation, or if it
  4093. would end up with no indentation after the change, nothing at all is done."
  4094. (save-excursion
  4095. (let ((end (save-excursion (outline-next-heading)
  4096. (point-marker)))
  4097. (prohibit (if (> diff 0)
  4098. "^\\S-"
  4099. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4100. col)
  4101. (unless (save-excursion (end-of-line 1)
  4102. (re-search-forward prohibit end t))
  4103. (while (and (< (point) end)
  4104. (re-search-forward "^[ \t]+" end t))
  4105. (goto-char (match-end 0))
  4106. (setq col (current-column))
  4107. (if (< diff 0) (replace-match ""))
  4108. (indent-to (+ diff col))))
  4109. (move-marker end nil))))
  4110. (defun org-convert-to-odd-levels ()
  4111. "Convert an org-mode file with all levels allowed to one with odd levels.
  4112. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4113. level 5 etc."
  4114. (interactive)
  4115. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4116. (let ((org-odd-levels-only nil) n)
  4117. (save-excursion
  4118. (goto-char (point-min))
  4119. (while (re-search-forward "^\\*\\*+ " nil t)
  4120. (setq n (- (length (match-string 0)) 2))
  4121. (while (>= (setq n (1- n)) 0)
  4122. (org-demote))
  4123. (end-of-line 1))))))
  4124. (defun org-convert-to-oddeven-levels ()
  4125. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4126. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4127. section with an even level, conversion would destroy the structure of the file. An error
  4128. is signaled in this case."
  4129. (interactive)
  4130. (goto-char (point-min))
  4131. ;; First check if there are no even levels
  4132. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4133. (org-show-context t)
  4134. (error "Not all levels are odd in this file. Conversion not possible."))
  4135. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4136. (let ((org-odd-levels-only nil) n)
  4137. (save-excursion
  4138. (goto-char (point-min))
  4139. (while (re-search-forward "^\\*\\*+ " nil t)
  4140. (setq n (/ (1- (length (match-string 0))) 2))
  4141. (while (>= (setq n (1- n)) 0)
  4142. (org-promote))
  4143. (end-of-line 1))))))
  4144. (defun org-tr-level (n)
  4145. "Make N odd if required."
  4146. (if org-odd-levels-only (1+ (/ n 2)) n))
  4147. ;;; Vertical tree motion, cutting and pasting of subtrees
  4148. (defun org-move-subtree-up (&optional arg)
  4149. "Move the current subtree up past ARG headlines of the same level."
  4150. (interactive "p")
  4151. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4152. (defun org-move-subtree-down (&optional arg)
  4153. "Move the current subtree down past ARG headlines of the same level."
  4154. (interactive "p")
  4155. (setq arg (prefix-numeric-value arg))
  4156. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4157. 'outline-get-last-sibling))
  4158. (ins-point (make-marker))
  4159. (cnt (abs arg))
  4160. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4161. ;; Select the tree
  4162. (org-back-to-heading)
  4163. (setq beg0 (point))
  4164. (save-excursion
  4165. (setq ne-beg (org-back-over-empty-lines))
  4166. (setq beg (point)))
  4167. (save-match-data
  4168. (save-excursion (outline-end-of-heading)
  4169. (setq folded (org-invisible-p)))
  4170. (outline-end-of-subtree))
  4171. (outline-next-heading)
  4172. (setq ne-end (org-back-over-empty-lines))
  4173. (setq end (point))
  4174. (goto-char beg0)
  4175. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4176. ;; include less whitespace
  4177. (save-excursion
  4178. (goto-char beg)
  4179. (forward-line (- ne-beg ne-end))
  4180. (setq beg (point))))
  4181. ;; Find insertion point, with error handling
  4182. (while (> cnt 0)
  4183. (or (and (funcall movfunc) (looking-at outline-regexp))
  4184. (progn (goto-char beg0)
  4185. (error "Cannot move past superior level or buffer limit")))
  4186. (setq cnt (1- cnt)))
  4187. (if (> arg 0)
  4188. ;; Moving forward - still need to move over subtree
  4189. (progn (org-end-of-subtree t t)
  4190. (save-excursion
  4191. (org-back-over-empty-lines)
  4192. (or (bolp) (newline)))))
  4193. (setq ne-ins (org-back-over-empty-lines))
  4194. (move-marker ins-point (point))
  4195. (setq txt (buffer-substring beg end))
  4196. (org-save-markers-in-region beg end)
  4197. (delete-region beg end)
  4198. (outline-flag-region (1- beg) beg nil)
  4199. (outline-flag-region (1- (point)) (point) nil)
  4200. (let ((bbb (point)))
  4201. (insert-before-markers txt)
  4202. (org-reinstall-markers-in-region bbb)
  4203. (move-marker ins-point bbb))
  4204. (or (bolp) (insert "\n"))
  4205. (setq ins-end (point))
  4206. (goto-char ins-point)
  4207. (org-skip-whitespace)
  4208. (when (and (< arg 0)
  4209. (org-first-sibling-p)
  4210. (> ne-ins ne-beg))
  4211. ;; Move whitespace back to beginning
  4212. (save-excursion
  4213. (goto-char ins-end)
  4214. (let ((kill-whole-line t))
  4215. (kill-line (- ne-ins ne-beg)) (point)))
  4216. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4217. (move-marker ins-point nil)
  4218. (org-compact-display-after-subtree-move)
  4219. (unless folded
  4220. (org-show-entry)
  4221. (show-children)
  4222. (org-cycle-hide-drawers 'children))))
  4223. (defvar org-subtree-clip ""
  4224. "Clipboard for cut and paste of subtrees.
  4225. This is actually only a copy of the kill, because we use the normal kill
  4226. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4227. (defvar org-subtree-clip-folded nil
  4228. "Was the last copied subtree folded?
  4229. This is used to fold the tree back after pasting.")
  4230. (defun org-cut-subtree (&optional n)
  4231. "Cut the current subtree into the clipboard.
  4232. With prefix arg N, cut this many sequential subtrees.
  4233. This is a short-hand for marking the subtree and then cutting it."
  4234. (interactive "p")
  4235. (org-copy-subtree n 'cut))
  4236. (defun org-copy-subtree (&optional n cut force-store-markers)
  4237. "Cut the current subtree into the clipboard.
  4238. With prefix arg N, cut this many sequential subtrees.
  4239. This is a short-hand for marking the subtree and then copying it.
  4240. If CUT is non-nil, actually cut the subtree.
  4241. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4242. of some markers in the region, even if CUT is non-nil. This is
  4243. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4244. (interactive "p")
  4245. (let (beg end folded (beg0 (point)))
  4246. (if (interactive-p)
  4247. (org-back-to-heading nil) ; take what looks like a subtree
  4248. (org-back-to-heading t)) ; take what is really there
  4249. (org-back-over-empty-lines)
  4250. (setq beg (point))
  4251. (skip-chars-forward " \t\r\n")
  4252. (save-match-data
  4253. (save-excursion (outline-end-of-heading)
  4254. (setq folded (org-invisible-p)))
  4255. (condition-case nil
  4256. (outline-forward-same-level (1- n))
  4257. (error nil))
  4258. (org-end-of-subtree t t))
  4259. (org-back-over-empty-lines)
  4260. (setq end (point))
  4261. (goto-char beg0)
  4262. (when (> end beg)
  4263. (setq org-subtree-clip-folded folded)
  4264. (when (or cut force-store-markers)
  4265. (org-save-markers-in-region beg end))
  4266. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4267. (setq org-subtree-clip (current-kill 0))
  4268. (message "%s: Subtree(s) with %d characters"
  4269. (if cut "Cut" "Copied")
  4270. (length org-subtree-clip)))))
  4271. (defun org-paste-subtree (&optional level tree)
  4272. "Paste the clipboard as a subtree, with modification of headline level.
  4273. The entire subtree is promoted or demoted in order to match a new headline
  4274. level. By default, the new level is derived from the visible headings
  4275. before and after the insertion point, and taken to be the inferior headline
  4276. level of the two. So if the previous visible heading is level 3 and the
  4277. next is level 4 (or vice versa), level 4 will be used for insertion.
  4278. This makes sure that the subtree remains an independent subtree and does
  4279. not swallow low level entries.
  4280. You can also force a different level, either by using a numeric prefix
  4281. argument, or by inserting the heading marker by hand. For example, if the
  4282. cursor is after \"*****\", then the tree will be shifted to level 5.
  4283. If you want to insert the tree as is, just use \\[yank].
  4284. If optional TREE is given, use this text instead of the kill ring."
  4285. (interactive "P")
  4286. (unless (org-kill-is-subtree-p tree)
  4287. (error "%s"
  4288. (substitute-command-keys
  4289. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4290. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4291. (^re (concat "^\\(" outline-regexp "\\)"))
  4292. (re (concat "\\(" outline-regexp "\\)"))
  4293. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4294. (old-level (if (string-match ^re txt)
  4295. (- (match-end 0) (match-beginning 0) 1)
  4296. -1))
  4297. (force-level (cond (level (prefix-numeric-value level))
  4298. ((string-match
  4299. ^re_ (buffer-substring (point-at-bol) (point)))
  4300. (- (match-end 1) (match-beginning 1)))
  4301. (t nil)))
  4302. (previous-level (save-excursion
  4303. (condition-case nil
  4304. (progn
  4305. (outline-previous-visible-heading 1)
  4306. (if (looking-at re)
  4307. (- (match-end 0) (match-beginning 0) 1)
  4308. 1))
  4309. (error 1))))
  4310. (next-level (save-excursion
  4311. (condition-case nil
  4312. (progn
  4313. (or (looking-at outline-regexp)
  4314. (outline-next-visible-heading 1))
  4315. (if (looking-at re)
  4316. (- (match-end 0) (match-beginning 0) 1)
  4317. 1))
  4318. (error 1))))
  4319. (new-level (or force-level (max previous-level next-level)))
  4320. (shift (if (or (= old-level -1)
  4321. (= new-level -1)
  4322. (= old-level new-level))
  4323. 0
  4324. (- new-level old-level)))
  4325. (delta (if (> shift 0) -1 1))
  4326. (func (if (> shift 0) 'org-demote 'org-promote))
  4327. (org-odd-levels-only nil)
  4328. beg end)
  4329. ;; Remove the forced level indicator
  4330. (if force-level
  4331. (delete-region (point-at-bol) (point)))
  4332. ;; Paste
  4333. (beginning-of-line 1)
  4334. (org-back-over-empty-lines)
  4335. (setq beg (point))
  4336. (insert-before-markers txt)
  4337. (unless (string-match "\n\\'" txt) (insert "\n"))
  4338. (org-reinstall-markers-in-region beg)
  4339. (setq end (point))
  4340. (goto-char beg)
  4341. (skip-chars-forward " \t\n\r")
  4342. (setq beg (point))
  4343. ;; Shift if necessary
  4344. (unless (= shift 0)
  4345. (save-restriction
  4346. (narrow-to-region beg end)
  4347. (while (not (= shift 0))
  4348. (org-map-region func (point-min) (point-max))
  4349. (setq shift (+ delta shift)))
  4350. (goto-char (point-min))))
  4351. (when (interactive-p)
  4352. (message "Clipboard pasted as level %d subtree" new-level))
  4353. (if (and kill-ring
  4354. (eq org-subtree-clip (current-kill 0))
  4355. org-subtree-clip-folded)
  4356. ;; The tree was folded before it was killed/copied
  4357. (hide-subtree))))
  4358. (defun org-kill-is-subtree-p (&optional txt)
  4359. "Check if the current kill is an outline subtree, or a set of trees.
  4360. Returns nil if kill does not start with a headline, or if the first
  4361. headline level is not the largest headline level in the tree.
  4362. So this will actually accept several entries of equal levels as well,
  4363. which is OK for `org-paste-subtree'.
  4364. If optional TXT is given, check this string instead of the current kill."
  4365. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4366. (start-level (and kill
  4367. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4368. org-outline-regexp "\\)")
  4369. kill)
  4370. (- (match-end 2) (match-beginning 2) 1)))
  4371. (re (concat "^" org-outline-regexp))
  4372. (start (1+ (match-beginning 2))))
  4373. (if (not start-level)
  4374. (progn
  4375. nil) ;; does not even start with a heading
  4376. (catch 'exit
  4377. (while (setq start (string-match re kill (1+ start)))
  4378. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4379. (throw 'exit nil)))
  4380. t))))
  4381. (defvar org-markers-to-move nil
  4382. "Markers that should be moved with a cut-and-paste operation.
  4383. Those markers are stored together with their positions relative to
  4384. the start of the region.")
  4385. (defun org-save-markers-in-region (beg end)
  4386. "Check markers in region.
  4387. If these markers are between BEG and END, record their position relative
  4388. to BEG, so that after moving the block of text, we can put the markers back
  4389. into place.
  4390. This function gets called just before an entry or tree gets cut from the
  4391. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4392. called immediately, to move the markers with the entries."
  4393. (setq org-markers-to-move nil)
  4394. (when (featurep 'org-clock)
  4395. (org-clock-save-markers-for-cut-and-paste beg end))
  4396. (when (featurep 'org-agenda)
  4397. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4398. (defun org-check-and-save-marker (marker beg end)
  4399. "Check if MARKER is between BEG and END.
  4400. If yes, remember the marker and the distance to BEG."
  4401. (when (and (marker-buffer marker)
  4402. (equal (marker-buffer marker) (current-buffer)))
  4403. (if (and (>= marker beg) (< marker end))
  4404. (push (cons marker (- marker beg)) org-markers-to-move))))
  4405. (defun org-reinstall-markers-in-region (beg)
  4406. "Move all remembered markers to their position relative to BEG."
  4407. (mapc (lambda (x)
  4408. (move-marker (car x) (+ beg (cdr x))))
  4409. org-markers-to-move)
  4410. (setq org-markers-to-move nil))
  4411. (defun org-narrow-to-subtree ()
  4412. "Narrow buffer to the current subtree."
  4413. (interactive)
  4414. (save-excursion
  4415. (save-match-data
  4416. (narrow-to-region
  4417. (progn (org-back-to-heading) (point))
  4418. (progn (org-end-of-subtree t t) (point))))))
  4419. ;;; Outline Sorting
  4420. (defun org-sort (with-case)
  4421. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4422. Optional argument WITH-CASE means sort case-sensitively."
  4423. (interactive "P")
  4424. (if (org-at-table-p)
  4425. (org-call-with-arg 'org-table-sort-lines with-case)
  4426. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4427. (defun org-sort-remove-invisible (s)
  4428. (remove-text-properties 0 (length s) org-rm-props s)
  4429. (while (string-match org-bracket-link-regexp s)
  4430. (setq s (replace-match (if (match-end 2)
  4431. (match-string 3 s)
  4432. (match-string 1 s)) t t s)))
  4433. s)
  4434. (defvar org-priority-regexp) ; defined later in the file
  4435. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4436. "Sort entries on a certain level of an outline tree.
  4437. If there is an active region, the entries in the region are sorted.
  4438. Else, if the cursor is before the first entry, sort the top-level items.
  4439. Else, the children of the entry at point are sorted.
  4440. Sorting can be alphabetically, numerically, and by date/time as given by
  4441. the first time stamp in the entry. The command prompts for the sorting
  4442. type unless it has been given to the function through the SORTING-TYPE
  4443. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4444. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4445. called with point at the beginning of the record. It must return either
  4446. a string or a number that should serve as the sorting key for that record.
  4447. Comparing entries ignores case by default. However, with an optional argument
  4448. WITH-CASE, the sorting considers case as well."
  4449. (interactive "P")
  4450. (let ((case-func (if with-case 'identity 'downcase))
  4451. start beg end stars re re2
  4452. txt what tmp plain-list-p)
  4453. ;; Find beginning and end of region to sort
  4454. (cond
  4455. ((org-region-active-p)
  4456. ;; we will sort the region
  4457. (setq end (region-end)
  4458. what "region")
  4459. (goto-char (region-beginning))
  4460. (if (not (org-on-heading-p)) (outline-next-heading))
  4461. (setq start (point)))
  4462. ((org-at-item-p)
  4463. ;; we will sort this plain list
  4464. (org-beginning-of-item-list) (setq start (point))
  4465. (org-end-of-item-list) (setq end (point))
  4466. (goto-char start)
  4467. (setq plain-list-p t
  4468. what "plain list"))
  4469. ((or (org-on-heading-p)
  4470. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4471. ;; we will sort the children of the current headline
  4472. (org-back-to-heading)
  4473. (setq start (point)
  4474. end (progn (org-end-of-subtree t t)
  4475. (org-back-over-empty-lines)
  4476. (point))
  4477. what "children")
  4478. (goto-char start)
  4479. (show-subtree)
  4480. (outline-next-heading))
  4481. (t
  4482. ;; we will sort the top-level entries in this file
  4483. (goto-char (point-min))
  4484. (or (org-on-heading-p) (outline-next-heading))
  4485. (setq start (point) end (point-max) what "top-level")
  4486. (goto-char start)
  4487. (show-all)))
  4488. (setq beg (point))
  4489. (if (>= beg end) (error "Nothing to sort"))
  4490. (unless plain-list-p
  4491. (looking-at "\\(\\*+\\)")
  4492. (setq stars (match-string 1)
  4493. re (concat "^" (regexp-quote stars) " +")
  4494. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4495. txt (buffer-substring beg end))
  4496. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4497. (if (and (not (equal stars "*")) (string-match re2 txt))
  4498. (error "Region to sort contains a level above the first entry")))
  4499. (unless sorting-type
  4500. (message
  4501. (if plain-list-p
  4502. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4503. "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:")
  4504. what)
  4505. (setq sorting-type (read-char-exclusive))
  4506. (and (= (downcase sorting-type) ?f)
  4507. (setq getkey-func
  4508. (completing-read "Sort using function: "
  4509. obarray 'fboundp t nil nil))
  4510. (setq getkey-func (intern getkey-func)))
  4511. (and (= (downcase sorting-type) ?r)
  4512. (setq property
  4513. (completing-read "Property: "
  4514. (mapcar 'list (org-buffer-property-keys t))
  4515. nil t))))
  4516. (message "Sorting entries...")
  4517. (save-restriction
  4518. (narrow-to-region start end)
  4519. (let ((dcst (downcase sorting-type))
  4520. (now (current-time)))
  4521. (sort-subr
  4522. (/= dcst sorting-type)
  4523. ;; This function moves to the beginning character of the "record" to
  4524. ;; be sorted.
  4525. (if plain-list-p
  4526. (lambda nil
  4527. (if (org-at-item-p) t (goto-char (point-max))))
  4528. (lambda nil
  4529. (if (re-search-forward re nil t)
  4530. (goto-char (match-beginning 0))
  4531. (goto-char (point-max)))))
  4532. ;; This function moves to the last character of the "record" being
  4533. ;; sorted.
  4534. (if plain-list-p
  4535. 'org-end-of-item
  4536. (lambda nil
  4537. (save-match-data
  4538. (condition-case nil
  4539. (outline-forward-same-level 1)
  4540. (error
  4541. (goto-char (point-max)))))))
  4542. ;; This function returns the value that gets sorted against.
  4543. (if plain-list-p
  4544. (lambda nil
  4545. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4546. (cond
  4547. ((= dcst ?n)
  4548. (string-to-number (buffer-substring (match-end 0)
  4549. (point-at-eol))))
  4550. ((= dcst ?a)
  4551. (buffer-substring (match-end 0) (point-at-eol)))
  4552. ((= dcst ?t)
  4553. (if (re-search-forward org-ts-regexp
  4554. (point-at-eol) t)
  4555. (org-time-string-to-time (match-string 0))
  4556. now))
  4557. ((= dcst ?f)
  4558. (if getkey-func
  4559. (progn
  4560. (setq tmp (funcall getkey-func))
  4561. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4562. tmp)
  4563. (error "Invalid key function `%s'" getkey-func)))
  4564. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4565. (lambda nil
  4566. (cond
  4567. ((= dcst ?n)
  4568. (if (looking-at outline-regexp)
  4569. (string-to-number (buffer-substring (match-end 0)
  4570. (point-at-eol)))
  4571. nil))
  4572. ((= dcst ?a)
  4573. (funcall case-func (buffer-substring (point-at-bol)
  4574. (point-at-eol))))
  4575. ((= dcst ?t)
  4576. (if (re-search-forward org-ts-regexp
  4577. (save-excursion
  4578. (forward-line 2)
  4579. (point)) t)
  4580. (org-time-string-to-time (match-string 0))
  4581. now))
  4582. ((= dcst ?p)
  4583. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4584. (string-to-char (match-string 2))
  4585. org-default-priority))
  4586. ((= dcst ?r)
  4587. (or (org-entry-get nil property) ""))
  4588. ((= dcst ?o)
  4589. (if (looking-at org-complex-heading-regexp)
  4590. (- 9999 (length (member (match-string 2)
  4591. org-todo-keywords-1)))))
  4592. ((= dcst ?f)
  4593. (if getkey-func
  4594. (progn
  4595. (setq tmp (funcall getkey-func))
  4596. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4597. tmp)
  4598. (error "Invalid key function `%s'" getkey-func)))
  4599. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4600. nil
  4601. (cond
  4602. ((= dcst ?a) 'string<)
  4603. ((= dcst ?t) 'time-less-p)
  4604. (t nil)))))
  4605. (message "Sorting entries...done")))
  4606. (defun org-do-sort (table what &optional with-case sorting-type)
  4607. "Sort TABLE of WHAT according to SORTING-TYPE.
  4608. The user will be prompted for the SORTING-TYPE if the call to this
  4609. function does not specify it. WHAT is only for the prompt, to indicate
  4610. what is being sorted. The sorting key will be extracted from
  4611. the car of the elements of the table.
  4612. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4613. (unless sorting-type
  4614. (message
  4615. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4616. what)
  4617. (setq sorting-type (read-char-exclusive)))
  4618. (let ((dcst (downcase sorting-type))
  4619. extractfun comparefun)
  4620. ;; Define the appropriate functions
  4621. (cond
  4622. ((= dcst ?n)
  4623. (setq extractfun 'string-to-number
  4624. comparefun (if (= dcst sorting-type) '< '>)))
  4625. ((= dcst ?a)
  4626. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4627. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4628. comparefun (if (= dcst sorting-type)
  4629. 'string<
  4630. (lambda (a b) (and (not (string< a b))
  4631. (not (string= a b)))))))
  4632. ((= dcst ?t)
  4633. (setq extractfun
  4634. (lambda (x)
  4635. (if (string-match org-ts-regexp x)
  4636. (time-to-seconds
  4637. (org-time-string-to-time (match-string 0 x)))
  4638. 0))
  4639. comparefun (if (= dcst sorting-type) '< '>)))
  4640. (t (error "Invalid sorting type `%c'" sorting-type)))
  4641. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4642. table)
  4643. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4644. ;;;; Plain list items, including checkboxes
  4645. ;;; Plain list items
  4646. (defun org-at-item-p ()
  4647. "Is point in a line starting a hand-formatted item?"
  4648. (let ((llt org-plain-list-ordered-item-terminator))
  4649. (save-excursion
  4650. (goto-char (point-at-bol))
  4651. (looking-at
  4652. (cond
  4653. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4654. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4655. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4656. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4657. (defun org-in-item-p ()
  4658. "It the cursor inside a plain list item.
  4659. Does not have to be the first line."
  4660. (save-excursion
  4661. (condition-case nil
  4662. (progn
  4663. (org-beginning-of-item)
  4664. (org-at-item-p)
  4665. t)
  4666. (error nil))))
  4667. (defun org-insert-item (&optional checkbox)
  4668. "Insert a new item at the current level.
  4669. Return t when things worked, nil when we are not in an item."
  4670. (when (save-excursion
  4671. (condition-case nil
  4672. (progn
  4673. (org-beginning-of-item)
  4674. (org-at-item-p)
  4675. (if (org-invisible-p) (error "Invisible item"))
  4676. t)
  4677. (error nil)))
  4678. (let* ((bul (match-string 0))
  4679. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4680. (match-end 0)))
  4681. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4682. pos)
  4683. (cond
  4684. ((and (org-at-item-p) (<= (point) eow))
  4685. ;; before the bullet
  4686. (beginning-of-line 1)
  4687. (open-line (if blank 2 1)))
  4688. ((<= (point) eow)
  4689. (beginning-of-line 1))
  4690. (t
  4691. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4692. (end-of-line 1)
  4693. (delete-horizontal-space))
  4694. (newline (if blank 2 1))))
  4695. (insert bul (if checkbox "[ ]" ""))
  4696. (just-one-space)
  4697. (setq pos (point))
  4698. (end-of-line 1)
  4699. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4700. (org-maybe-renumber-ordered-list)
  4701. (and checkbox (org-update-checkbox-count-maybe))
  4702. t))
  4703. ;;; Checkboxes
  4704. (defun org-at-item-checkbox-p ()
  4705. "Is point at a line starting a plain-list item with a checklet?"
  4706. (and (org-at-item-p)
  4707. (save-excursion
  4708. (goto-char (match-end 0))
  4709. (skip-chars-forward " \t")
  4710. (looking-at "\\[[- X]\\]"))))
  4711. (defun org-toggle-checkbox (&optional arg)
  4712. "Toggle the checkbox in the current line."
  4713. (interactive "P")
  4714. (catch 'exit
  4715. (let (beg end status (firstnew 'unknown))
  4716. (cond
  4717. ((org-region-active-p)
  4718. (setq beg (region-beginning) end (region-end)))
  4719. ((org-on-heading-p)
  4720. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4721. ((org-at-item-checkbox-p)
  4722. (let ((pos (point)))
  4723. (replace-match
  4724. (cond (arg "[-]")
  4725. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4726. (t "[ ]"))
  4727. t t)
  4728. (goto-char pos))
  4729. (throw 'exit t))
  4730. (t (error "Not at a checkbox or heading, and no active region")))
  4731. (save-excursion
  4732. (goto-char beg)
  4733. (while (< (point) end)
  4734. (when (org-at-item-checkbox-p)
  4735. (setq status (equal (match-string 0) "[X]"))
  4736. (when (eq firstnew 'unknown)
  4737. (setq firstnew (not status)))
  4738. (replace-match
  4739. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4740. (beginning-of-line 2)))))
  4741. (org-update-checkbox-count-maybe))
  4742. (defun org-update-checkbox-count-maybe ()
  4743. "Update checkbox statistics unless turned off by user."
  4744. (when org-provide-checkbox-statistics
  4745. (org-update-checkbox-count)))
  4746. (defun org-update-checkbox-count (&optional all)
  4747. "Update the checkbox statistics in the current section.
  4748. This will find all statistic cookies like [57%] and [6/12] and update them
  4749. with the current numbers. With optional prefix argument ALL, do this for
  4750. the whole buffer."
  4751. (interactive "P")
  4752. (save-excursion
  4753. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4754. (beg (condition-case nil
  4755. (progn (outline-back-to-heading) (point))
  4756. (error (point-min))))
  4757. (end (move-marker (make-marker)
  4758. (progn (outline-next-heading) (point))))
  4759. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4760. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4761. (re-find (concat re "\\|" re-box))
  4762. beg-cookie end-cookie is-percent c-on c-off lim
  4763. eline curr-ind next-ind continue-from startsearch
  4764. (cstat 0)
  4765. )
  4766. (when all
  4767. (goto-char (point-min))
  4768. (outline-next-heading)
  4769. (setq beg (point) end (point-max)))
  4770. (goto-char end)
  4771. ;; find each statistic cookie
  4772. (while (re-search-backward re-find beg t)
  4773. (setq beg-cookie (match-beginning 1)
  4774. end-cookie (match-end 1)
  4775. cstat (+ cstat (if end-cookie 1 0))
  4776. startsearch (point-at-eol)
  4777. continue-from (point-at-bol)
  4778. is-percent (match-beginning 2)
  4779. lim (cond
  4780. ((org-on-heading-p) (outline-next-heading) (point))
  4781. ((org-at-item-p) (org-end-of-item) (point))
  4782. (t nil))
  4783. c-on 0
  4784. c-off 0)
  4785. (when lim
  4786. ;; find first checkbox for this cookie and gather
  4787. ;; statistics from all that are at this indentation level
  4788. (goto-char startsearch)
  4789. (if (re-search-forward re-box lim t)
  4790. (progn
  4791. (org-beginning-of-item)
  4792. (setq curr-ind (org-get-indentation))
  4793. (setq next-ind curr-ind)
  4794. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  4795. (save-excursion (end-of-line) (setq eline (point)))
  4796. (if (re-search-forward re-box eline t)
  4797. (if (member (match-string 2) '("[ ]" "[-]"))
  4798. (setq c-off (1+ c-off))
  4799. (setq c-on (1+ c-on))
  4800. )
  4801. )
  4802. (org-end-of-item)
  4803. (setq next-ind (org-get-indentation))
  4804. )))
  4805. (goto-char continue-from)
  4806. ;; update cookie
  4807. (when end-cookie
  4808. (delete-region beg-cookie end-cookie)
  4809. (goto-char beg-cookie)
  4810. (insert
  4811. (if is-percent
  4812. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4813. (format "[%d/%d]" c-on (+ c-on c-off)))))
  4814. ;; update items checkbox if it has one
  4815. (when (org-at-item-p)
  4816. (org-beginning-of-item)
  4817. (when (and (> (+ c-on c-off) 0)
  4818. (re-search-forward re-box (point-at-eol) t))
  4819. (setq beg-cookie (match-beginning 2)
  4820. end-cookie (match-end 2))
  4821. (delete-region beg-cookie end-cookie)
  4822. (goto-char beg-cookie)
  4823. (cond ((= c-off 0) (insert "[X]"))
  4824. ((= c-on 0) (insert "[ ]"))
  4825. (t (insert "[-]")))
  4826. )))
  4827. (goto-char continue-from))
  4828. (when (interactive-p)
  4829. (message "Checkbox satistics updated %s (%d places)"
  4830. (if all "in entire file" "in current outline entry") cstat)))))
  4831. (defun org-get-checkbox-statistics-face ()
  4832. "Select the face for checkbox statistics.
  4833. The face will be `org-done' when all relevant boxes are checked. Otherwise
  4834. it will be `org-todo'."
  4835. (if (match-end 1)
  4836. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  4837. (if (and (> (match-end 2) (match-beginning 2))
  4838. (equal (match-string 2) (match-string 3)))
  4839. 'org-done
  4840. 'org-todo)))
  4841. (defun org-get-indentation (&optional line)
  4842. "Get the indentation of the current line, interpreting tabs.
  4843. When LINE is given, assume it represents a line and compute its indentation."
  4844. (if line
  4845. (if (string-match "^ *" (org-remove-tabs line))
  4846. (match-end 0))
  4847. (save-excursion
  4848. (beginning-of-line 1)
  4849. (skip-chars-forward " \t")
  4850. (current-column))))
  4851. (defun org-remove-tabs (s &optional width)
  4852. "Replace tabulators in S with spaces.
  4853. Assumes that s is a single line, starting in column 0."
  4854. (setq width (or width tab-width))
  4855. (while (string-match "\t" s)
  4856. (setq s (replace-match
  4857. (make-string
  4858. (- (* width (/ (+ (match-beginning 0) width) width))
  4859. (match-beginning 0)) ?\ )
  4860. t t s)))
  4861. s)
  4862. (defun org-fix-indentation (line ind)
  4863. "Fix indentation in LINE.
  4864. IND is a cons cell with target and minimum indentation.
  4865. If the current indenation in LINE is smaller than the minimum,
  4866. leave it alone. If it is larger than ind, set it to the target."
  4867. (let* ((l (org-remove-tabs line))
  4868. (i (org-get-indentation l))
  4869. (i1 (car ind)) (i2 (cdr ind)))
  4870. (if (>= i i2) (setq l (substring line i2)))
  4871. (if (> i1 0)
  4872. (concat (make-string i1 ?\ ) l)
  4873. l)))
  4874. (defun org-beginning-of-item ()
  4875. "Go to the beginning of the current hand-formatted item.
  4876. If the cursor is not in an item, throw an error."
  4877. (interactive)
  4878. (let ((pos (point))
  4879. (limit (save-excursion
  4880. (condition-case nil
  4881. (progn
  4882. (org-back-to-heading)
  4883. (beginning-of-line 2) (point))
  4884. (error (point-min)))))
  4885. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4886. ind ind1)
  4887. (if (org-at-item-p)
  4888. (beginning-of-line 1)
  4889. (beginning-of-line 1)
  4890. (skip-chars-forward " \t")
  4891. (setq ind (current-column))
  4892. (if (catch 'exit
  4893. (while t
  4894. (beginning-of-line 0)
  4895. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  4896. (if (looking-at "[ \t]*$")
  4897. (setq ind1 ind-empty)
  4898. (skip-chars-forward " \t")
  4899. (setq ind1 (current-column)))
  4900. (if (< ind1 ind)
  4901. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  4902. nil
  4903. (goto-char pos)
  4904. (error "Not in an item")))))
  4905. (defun org-end-of-item ()
  4906. "Go to the end of the current hand-formatted item.
  4907. If the cursor is not in an item, throw an error."
  4908. (interactive)
  4909. (let* ((pos (point))
  4910. ind1
  4911. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4912. (limit (save-excursion (outline-next-heading) (point)))
  4913. (ind (save-excursion
  4914. (org-beginning-of-item)
  4915. (skip-chars-forward " \t")
  4916. (current-column)))
  4917. (end (catch 'exit
  4918. (while t
  4919. (beginning-of-line 2)
  4920. (if (eobp) (throw 'exit (point)))
  4921. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  4922. (if (looking-at "[ \t]*$")
  4923. (setq ind1 ind-empty)
  4924. (skip-chars-forward " \t")
  4925. (setq ind1 (current-column)))
  4926. (if (<= ind1 ind)
  4927. (throw 'exit (point-at-bol)))))))
  4928. (if end
  4929. (goto-char end)
  4930. (goto-char pos)
  4931. (error "Not in an item"))))
  4932. (defun org-next-item ()
  4933. "Move to the beginning of the next item in the current plain list.
  4934. Error if not at a plain list, or if this is the last item in the list."
  4935. (interactive)
  4936. (let (ind ind1 (pos (point)))
  4937. (org-beginning-of-item)
  4938. (setq ind (org-get-indentation))
  4939. (org-end-of-item)
  4940. (setq ind1 (org-get-indentation))
  4941. (unless (and (org-at-item-p) (= ind ind1))
  4942. (goto-char pos)
  4943. (error "On last item"))))
  4944. (defun org-previous-item ()
  4945. "Move to the beginning of the previous item in the current plain list.
  4946. Error if not at a plain list, or if this is the first item in the list."
  4947. (interactive)
  4948. (let (beg ind ind1 (pos (point)))
  4949. (org-beginning-of-item)
  4950. (setq beg (point))
  4951. (setq ind (org-get-indentation))
  4952. (goto-char beg)
  4953. (catch 'exit
  4954. (while t
  4955. (beginning-of-line 0)
  4956. (if (looking-at "[ \t]*$")
  4957. nil
  4958. (if (<= (setq ind1 (org-get-indentation)) ind)
  4959. (throw 'exit t)))))
  4960. (condition-case nil
  4961. (if (or (not (org-at-item-p))
  4962. (< ind1 (1- ind)))
  4963. (error "")
  4964. (org-beginning-of-item))
  4965. (error (goto-char pos)
  4966. (error "On first item")))))
  4967. (defun org-first-list-item-p ()
  4968. "Is this heading the item in a plain list?"
  4969. (unless (org-at-item-p)
  4970. (error "Not at a plain list item"))
  4971. (org-beginning-of-item)
  4972. (= (point) (save-excursion (org-beginning-of-item-list))))
  4973. (defun org-move-item-down ()
  4974. "Move the plain list item at point down, i.e. swap with following item.
  4975. Subitems (items with larger indentation) are considered part of the item,
  4976. so this really moves item trees."
  4977. (interactive)
  4978. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  4979. (org-beginning-of-item)
  4980. (setq beg0 (point))
  4981. (save-excursion
  4982. (setq ne-beg (org-back-over-empty-lines))
  4983. (setq beg (point)))
  4984. (goto-char beg0)
  4985. (setq ind (org-get-indentation))
  4986. (org-end-of-item)
  4987. (setq end0 (point))
  4988. (setq ind1 (org-get-indentation))
  4989. (setq ne-end (org-back-over-empty-lines))
  4990. (setq end (point))
  4991. (goto-char beg0)
  4992. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  4993. ;; include less whitespace
  4994. (save-excursion
  4995. (goto-char beg)
  4996. (forward-line (- ne-beg ne-end))
  4997. (setq beg (point))))
  4998. (goto-char end0)
  4999. (if (and (org-at-item-p) (= ind ind1))
  5000. (progn
  5001. (org-end-of-item)
  5002. (org-back-over-empty-lines)
  5003. (setq txt (buffer-substring beg end))
  5004. (save-excursion
  5005. (delete-region beg end))
  5006. (setq pos (point))
  5007. (insert txt)
  5008. (goto-char pos) (org-skip-whitespace)
  5009. (org-maybe-renumber-ordered-list))
  5010. (goto-char pos)
  5011. (error "Cannot move this item further down"))))
  5012. (defun org-move-item-up (arg)
  5013. "Move the plain list item at point up, i.e. swap with previous item.
  5014. Subitems (items with larger indentation) are considered part of the item,
  5015. so this really moves item trees."
  5016. (interactive "p")
  5017. (let (beg beg0 end ind ind1 (pos (point)) txt
  5018. ne-beg ne-ins ins-end)
  5019. (org-beginning-of-item)
  5020. (setq beg0 (point))
  5021. (setq ind (org-get-indentation))
  5022. (save-excursion
  5023. (setq ne-beg (org-back-over-empty-lines))
  5024. (setq beg (point)))
  5025. (goto-char beg0)
  5026. (org-end-of-item)
  5027. (setq end (point))
  5028. (goto-char beg0)
  5029. (catch 'exit
  5030. (while t
  5031. (beginning-of-line 0)
  5032. (if (looking-at "[ \t]*$")
  5033. (if org-empty-line-terminates-plain-lists
  5034. (progn
  5035. (goto-char pos)
  5036. (error "Cannot move this item further up"))
  5037. nil)
  5038. (if (<= (setq ind1 (org-get-indentation)) ind)
  5039. (throw 'exit t)))))
  5040. (condition-case nil
  5041. (org-beginning-of-item)
  5042. (error (goto-char beg)
  5043. (error "Cannot move this item further up")))
  5044. (setq ind1 (org-get-indentation))
  5045. (if (and (org-at-item-p) (= ind ind1))
  5046. (progn
  5047. (setq ne-ins (org-back-over-empty-lines))
  5048. (setq txt (buffer-substring beg end))
  5049. (save-excursion
  5050. (delete-region beg end))
  5051. (setq pos (point))
  5052. (insert txt)
  5053. (setq ins-end (point))
  5054. (goto-char pos) (org-skip-whitespace)
  5055. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5056. ;; Move whitespace back to beginning
  5057. (save-excursion
  5058. (goto-char ins-end)
  5059. (let ((kill-whole-line t))
  5060. (kill-line (- ne-ins ne-beg)) (point)))
  5061. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5062. (org-maybe-renumber-ordered-list))
  5063. (goto-char pos)
  5064. (error "Cannot move this item further up"))))
  5065. (defun org-maybe-renumber-ordered-list ()
  5066. "Renumber the ordered list at point if setup allows it.
  5067. This tests the user option `org-auto-renumber-ordered-lists' before
  5068. doing the renumbering."
  5069. (interactive)
  5070. (when (and org-auto-renumber-ordered-lists
  5071. (org-at-item-p))
  5072. (if (match-beginning 3)
  5073. (org-renumber-ordered-list 1)
  5074. (org-fix-bullet-type))))
  5075. (defun org-maybe-renumber-ordered-list-safe ()
  5076. (condition-case nil
  5077. (save-excursion
  5078. (org-maybe-renumber-ordered-list))
  5079. (error nil)))
  5080. (defun org-cycle-list-bullet (&optional which)
  5081. "Cycle through the different itemize/enumerate bullets.
  5082. This cycle the entire list level through the sequence:
  5083. `-' -> `+' -> `*' -> `1.' -> `1)'
  5084. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5085. 0 meand `-', 1 means `+' etc."
  5086. (interactive "P")
  5087. (org-preserve-lc
  5088. (org-beginning-of-item-list)
  5089. (org-at-item-p)
  5090. (beginning-of-line 1)
  5091. (let ((current (match-string 0))
  5092. (prevp (eq which 'previous))
  5093. new)
  5094. (setq new (cond
  5095. ((and (numberp which)
  5096. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5097. ((string-match "-" current) (if prevp "1)" "+"))
  5098. ((string-match "\\+" current)
  5099. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5100. ((string-match "\\*" current) (if prevp "+" "1."))
  5101. ((string-match "\\." current) (if prevp "*" "1)"))
  5102. ((string-match ")" current) (if prevp "1." "-"))
  5103. (t (error "This should not happen"))))
  5104. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5105. (org-fix-bullet-type)
  5106. (org-maybe-renumber-ordered-list))))
  5107. (defun org-get-string-indentation (s)
  5108. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5109. (let ((n -1) (i 0) (w tab-width) c)
  5110. (catch 'exit
  5111. (while (< (setq n (1+ n)) (length s))
  5112. (setq c (aref s n))
  5113. (cond ((= c ?\ ) (setq i (1+ i)))
  5114. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5115. (t (throw 'exit t)))))
  5116. i))
  5117. (defun org-renumber-ordered-list (arg)
  5118. "Renumber an ordered plain list.
  5119. Cursor needs to be in the first line of an item, the line that starts
  5120. with something like \"1.\" or \"2)\"."
  5121. (interactive "p")
  5122. (unless (and (org-at-item-p)
  5123. (match-beginning 3))
  5124. (error "This is not an ordered list"))
  5125. (let ((line (org-current-line))
  5126. (col (current-column))
  5127. (ind (org-get-string-indentation
  5128. (buffer-substring (point-at-bol) (match-beginning 3))))
  5129. ;; (term (substring (match-string 3) -1))
  5130. ind1 (n (1- arg))
  5131. fmt)
  5132. ;; find where this list begins
  5133. (org-beginning-of-item-list)
  5134. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5135. (setq fmt (concat "%d" (match-string 1)))
  5136. (beginning-of-line 0)
  5137. ;; walk forward and replace these numbers
  5138. (catch 'exit
  5139. (while t
  5140. (catch 'next
  5141. (beginning-of-line 2)
  5142. (if (eobp) (throw 'exit nil))
  5143. (if (looking-at "[ \t]*$") (throw 'next nil))
  5144. (skip-chars-forward " \t") (setq ind1 (current-column))
  5145. (if (> ind1 ind) (throw 'next t))
  5146. (if (< ind1 ind) (throw 'exit t))
  5147. (if (not (org-at-item-p)) (throw 'exit nil))
  5148. (delete-region (match-beginning 2) (match-end 2))
  5149. (goto-char (match-beginning 2))
  5150. (insert (format fmt (setq n (1+ n)))))))
  5151. (goto-line line)
  5152. (org-move-to-column col)))
  5153. (defun org-fix-bullet-type ()
  5154. "Make sure all items in this list have the same bullet as the firsst item."
  5155. (interactive)
  5156. (unless (org-at-item-p) (error "This is not a list"))
  5157. (let ((line (org-current-line))
  5158. (col (current-column))
  5159. (ind (current-indentation))
  5160. ind1 bullet)
  5161. ;; find where this list begins
  5162. (org-beginning-of-item-list)
  5163. (beginning-of-line 1)
  5164. ;; find out what the bullet type is
  5165. (looking-at "[ \t]*\\(\\S-+\\)")
  5166. (setq bullet (match-string 1))
  5167. ;; walk forward and replace these numbers
  5168. (beginning-of-line 0)
  5169. (catch 'exit
  5170. (while t
  5171. (catch 'next
  5172. (beginning-of-line 2)
  5173. (if (eobp) (throw 'exit nil))
  5174. (if (looking-at "[ \t]*$") (throw 'next nil))
  5175. (skip-chars-forward " \t") (setq ind1 (current-column))
  5176. (if (> ind1 ind) (throw 'next t))
  5177. (if (< ind1 ind) (throw 'exit t))
  5178. (if (not (org-at-item-p)) (throw 'exit nil))
  5179. (skip-chars-forward " \t")
  5180. (looking-at "\\S-+")
  5181. (replace-match bullet))))
  5182. (goto-line line)
  5183. (org-move-to-column col)
  5184. (if (string-match "[0-9]" bullet)
  5185. (org-renumber-ordered-list 1))))
  5186. (defun org-beginning-of-item-list ()
  5187. "Go to the beginning of the current item list.
  5188. I.e. to the first item in this list."
  5189. (interactive)
  5190. (org-beginning-of-item)
  5191. (let ((pos (point-at-bol))
  5192. (ind (org-get-indentation))
  5193. ind1)
  5194. ;; find where this list begins
  5195. (catch 'exit
  5196. (while t
  5197. (catch 'next
  5198. (beginning-of-line 0)
  5199. (if (looking-at "[ \t]*$")
  5200. (throw (if (bobp) 'exit 'next) t))
  5201. (skip-chars-forward " \t") (setq ind1 (current-column))
  5202. (if (or (< ind1 ind)
  5203. (and (= ind1 ind)
  5204. (not (org-at-item-p)))
  5205. (bobp))
  5206. (throw 'exit t)
  5207. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5208. (goto-char pos)))
  5209. (defun org-end-of-item-list ()
  5210. "Go to the end of the current item list.
  5211. I.e. to the text after the last item."
  5212. (interactive)
  5213. (org-beginning-of-item)
  5214. (let ((pos (point-at-bol))
  5215. (ind (org-get-indentation))
  5216. ind1)
  5217. ;; find where this list begins
  5218. (catch 'exit
  5219. (while t
  5220. (catch 'next
  5221. (beginning-of-line 2)
  5222. (if (looking-at "[ \t]*$")
  5223. (throw (if (eobp) 'exit 'next) t))
  5224. (skip-chars-forward " \t") (setq ind1 (current-column))
  5225. (if (or (< ind1 ind)
  5226. (and (= ind1 ind)
  5227. (not (org-at-item-p)))
  5228. (eobp))
  5229. (progn
  5230. (setq pos (point-at-bol))
  5231. (throw 'exit t))))))
  5232. (goto-char pos)))
  5233. (defvar org-last-indent-begin-marker (make-marker))
  5234. (defvar org-last-indent-end-marker (make-marker))
  5235. (defun org-outdent-item (arg)
  5236. "Outdent a local list item."
  5237. (interactive "p")
  5238. (org-indent-item (- arg)))
  5239. (defun org-indent-item (arg)
  5240. "Indent a local list item."
  5241. (interactive "p")
  5242. (unless (org-at-item-p)
  5243. (error "Not on an item"))
  5244. (save-excursion
  5245. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5246. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5247. (setq beg org-last-indent-begin-marker
  5248. end org-last-indent-end-marker)
  5249. (org-beginning-of-item)
  5250. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5251. (org-end-of-item)
  5252. (setq end (move-marker org-last-indent-end-marker (point))))
  5253. (goto-char beg)
  5254. (setq tmp (org-item-indent-positions)
  5255. ind (car tmp)
  5256. ind-down (nth 2 tmp)
  5257. ind-up (nth 1 tmp)
  5258. delta (if (> arg 0)
  5259. (if ind-down (- ind-down ind) 2)
  5260. (if ind-up (- ind-up ind) -2)))
  5261. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5262. (while (< (point) end)
  5263. (beginning-of-line 1)
  5264. (skip-chars-forward " \t") (setq ind1 (current-column))
  5265. (delete-region (point-at-bol) (point))
  5266. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5267. (beginning-of-line 2))))
  5268. (org-fix-bullet-type)
  5269. (org-maybe-renumber-ordered-list-safe)
  5270. (save-excursion
  5271. (beginning-of-line 0)
  5272. (condition-case nil (org-beginning-of-item) (error nil))
  5273. (org-maybe-renumber-ordered-list-safe)))
  5274. (defun org-item-indent-positions ()
  5275. "Return indentation for plain list items.
  5276. This returns a list with three values: The current indentation, the
  5277. parent indentation and the indentation a child should habe.
  5278. Assumes cursor in item line."
  5279. (let* ((bolpos (point-at-bol))
  5280. (ind (org-get-indentation))
  5281. ind-down ind-up pos)
  5282. (save-excursion
  5283. (org-beginning-of-item-list)
  5284. (skip-chars-backward "\n\r \t")
  5285. (when (org-in-item-p)
  5286. (org-beginning-of-item)
  5287. (setq ind-up (org-get-indentation))))
  5288. (setq pos (point))
  5289. (save-excursion
  5290. (cond
  5291. ((and (condition-case nil (progn (org-previous-item) t)
  5292. (error nil))
  5293. (or (forward-char 1) t)
  5294. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5295. (setq ind-down (org-get-indentation)))
  5296. ((and (goto-char pos)
  5297. (org-at-item-p))
  5298. (goto-char (match-end 0))
  5299. (skip-chars-forward " \t")
  5300. (setq ind-down (current-column)))))
  5301. (list ind ind-up ind-down)))
  5302. ;;; The orgstruct minor mode
  5303. ;; Define a minor mode which can be used in other modes in order to
  5304. ;; integrate the org-mode structure editing commands.
  5305. ;; This is really a hack, because the org-mode structure commands use
  5306. ;; keys which normally belong to the major mode. Here is how it
  5307. ;; works: The minor mode defines all the keys necessary to operate the
  5308. ;; structure commands, but wraps the commands into a function which
  5309. ;; tests if the cursor is currently at a headline or a plain list
  5310. ;; item. If that is the case, the structure command is used,
  5311. ;; temporarily setting many Org-mode variables like regular
  5312. ;; expressions for filling etc. However, when any of those keys is
  5313. ;; used at a different location, function uses `key-binding' to look
  5314. ;; up if the key has an associated command in another currently active
  5315. ;; keymap (minor modes, major mode, global), and executes that
  5316. ;; command. There might be problems if any of the keys is otherwise
  5317. ;; used as a prefix key.
  5318. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5319. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5320. ;; addresses this by checking explicitly for both bindings.
  5321. (defvar orgstruct-mode-map (make-sparse-keymap)
  5322. "Keymap for the minor `orgstruct-mode'.")
  5323. (defvar org-local-vars nil
  5324. "List of local variables, for use by `orgstruct-mode'")
  5325. ;;;###autoload
  5326. (define-minor-mode orgstruct-mode
  5327. "Toggle the minor more `orgstruct-mode'.
  5328. This mode is for using Org-mode structure commands in other modes.
  5329. The following key behave as if Org-mode was active, if the cursor
  5330. is on a headline, or on a plain list item (both in the definition
  5331. of Org-mode).
  5332. M-up Move entry/item up
  5333. M-down Move entry/item down
  5334. M-left Promote
  5335. M-right Demote
  5336. M-S-up Move entry/item up
  5337. M-S-down Move entry/item down
  5338. M-S-left Promote subtree
  5339. M-S-right Demote subtree
  5340. M-q Fill paragraph and items like in Org-mode
  5341. C-c ^ Sort entries
  5342. C-c - Cycle list bullet
  5343. TAB Cycle item visibility
  5344. M-RET Insert new heading/item
  5345. S-M-RET Insert new TODO heading / Chekbox item
  5346. C-c C-c Set tags / toggle checkbox"
  5347. nil " OrgStruct" nil
  5348. (org-load-modules-maybe)
  5349. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5350. ;;;###autoload
  5351. (defun turn-on-orgstruct ()
  5352. "Unconditionally turn on `orgstruct-mode'."
  5353. (orgstruct-mode 1))
  5354. ;;;###autoload
  5355. (defun turn-on-orgstruct++ ()
  5356. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5357. In addition to setting orgstruct-mode, this also exports all indentation and
  5358. autofilling variables from org-mode into the buffer. Note that turning
  5359. off orgstruct-mode will *not* remove these additional settings."
  5360. (orgstruct-mode 1)
  5361. (let (var val)
  5362. (mapc
  5363. (lambda (x)
  5364. (when (string-match
  5365. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5366. (symbol-name (car x)))
  5367. (setq var (car x) val (nth 1 x))
  5368. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5369. org-local-vars)))
  5370. (defun orgstruct-error ()
  5371. "Error when there is no default binding for a structure key."
  5372. (interactive)
  5373. (error "This key has no function outside structure elements"))
  5374. (defun orgstruct-setup ()
  5375. "Setup orgstruct keymaps."
  5376. (let ((nfunc 0)
  5377. (bindings
  5378. (list
  5379. '([(meta up)] org-metaup)
  5380. '([(meta down)] org-metadown)
  5381. '([(meta left)] org-metaleft)
  5382. '([(meta right)] org-metaright)
  5383. '([(meta shift up)] org-shiftmetaup)
  5384. '([(meta shift down)] org-shiftmetadown)
  5385. '([(meta shift left)] org-shiftmetaleft)
  5386. '([(meta shift right)] org-shiftmetaright)
  5387. '([(shift up)] org-shiftup)
  5388. '([(shift down)] org-shiftdown)
  5389. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5390. '("\M-q" fill-paragraph)
  5391. '("\C-c^" org-sort)
  5392. '("\C-c-" org-cycle-list-bullet)))
  5393. elt key fun cmd)
  5394. (while (setq elt (pop bindings))
  5395. (setq nfunc (1+ nfunc))
  5396. (setq key (org-key (car elt))
  5397. fun (nth 1 elt)
  5398. cmd (orgstruct-make-binding fun nfunc key))
  5399. (org-defkey orgstruct-mode-map key cmd))
  5400. ;; Special treatment needed for TAB and RET
  5401. (org-defkey orgstruct-mode-map [(tab)]
  5402. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5403. (org-defkey orgstruct-mode-map "\C-i"
  5404. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5405. (org-defkey orgstruct-mode-map "\M-\C-m"
  5406. (orgstruct-make-binding 'org-insert-heading 105
  5407. "\M-\C-m" [(meta return)]))
  5408. (org-defkey orgstruct-mode-map [(meta return)]
  5409. (orgstruct-make-binding 'org-insert-heading 106
  5410. [(meta return)] "\M-\C-m"))
  5411. (org-defkey orgstruct-mode-map [(shift meta return)]
  5412. (orgstruct-make-binding 'org-insert-todo-heading 107
  5413. [(meta return)] "\M-\C-m"))
  5414. (unless org-local-vars
  5415. (setq org-local-vars (org-get-local-variables)))
  5416. t))
  5417. (defun orgstruct-make-binding (fun n &rest keys)
  5418. "Create a function for binding in the structure minor mode.
  5419. FUN is the command to call inside a table. N is used to create a unique
  5420. command name. KEYS are keys that should be checked in for a command
  5421. to execute outside of tables."
  5422. (eval
  5423. (list 'defun
  5424. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5425. '(arg)
  5426. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5427. "Outside of structure, run the binding of `"
  5428. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5429. "'.")
  5430. '(interactive "p")
  5431. (list 'if
  5432. '(org-context-p 'headline 'item)
  5433. (list 'org-run-like-in-org-mode (list 'quote fun))
  5434. (list 'let '(orgstruct-mode)
  5435. (list 'call-interactively
  5436. (append '(or)
  5437. (mapcar (lambda (k)
  5438. (list 'key-binding k))
  5439. keys)
  5440. '('orgstruct-error))))))))
  5441. (defun org-context-p (&rest contexts)
  5442. "Check if local context is and of CONTEXTS.
  5443. Possible values in the list of contexts are `table', `headline', and `item'."
  5444. (let ((pos (point)))
  5445. (goto-char (point-at-bol))
  5446. (prog1 (or (and (memq 'table contexts)
  5447. (looking-at "[ \t]*|"))
  5448. (and (memq 'headline contexts)
  5449. (looking-at "\\*+"))
  5450. (and (memq 'item contexts)
  5451. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5452. (goto-char pos))))
  5453. (defun org-get-local-variables ()
  5454. "Return a list of all local variables in an org-mode buffer."
  5455. (let (varlist)
  5456. (with-current-buffer (get-buffer-create "*Org tmp*")
  5457. (erase-buffer)
  5458. (org-mode)
  5459. (setq varlist (buffer-local-variables)))
  5460. (kill-buffer "*Org tmp*")
  5461. (delq nil
  5462. (mapcar
  5463. (lambda (x)
  5464. (setq x
  5465. (if (symbolp x)
  5466. (list x)
  5467. (list (car x) (list 'quote (cdr x)))))
  5468. (if (string-match
  5469. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5470. (symbol-name (car x)))
  5471. x nil))
  5472. varlist))))
  5473. ;;;###autoload
  5474. (defun org-run-like-in-org-mode (cmd)
  5475. (org-load-modules-maybe)
  5476. (unless org-local-vars
  5477. (setq org-local-vars (org-get-local-variables)))
  5478. (eval (list 'let org-local-vars
  5479. (list 'call-interactively (list 'quote cmd)))))
  5480. ;;;; Archiving
  5481. (defun org-get-category (&optional pos)
  5482. "Get the category applying to position POS."
  5483. (get-text-property (or pos (point)) 'org-category))
  5484. (defun org-refresh-category-properties ()
  5485. "Refresh category text properties in the buffer."
  5486. (let ((def-cat (cond
  5487. ((null org-category)
  5488. (if buffer-file-name
  5489. (file-name-sans-extension
  5490. (file-name-nondirectory buffer-file-name))
  5491. "???"))
  5492. ((symbolp org-category) (symbol-name org-category))
  5493. (t org-category)))
  5494. beg end cat pos optionp)
  5495. (org-unmodified
  5496. (save-excursion
  5497. (save-restriction
  5498. (widen)
  5499. (goto-char (point-min))
  5500. (put-text-property (point) (point-max) 'org-category def-cat)
  5501. (while (re-search-forward
  5502. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5503. (setq pos (match-end 0)
  5504. optionp (equal (char-after (match-beginning 0)) ?#)
  5505. cat (org-trim (match-string 2)))
  5506. (if optionp
  5507. (setq beg (point-at-bol) end (point-max))
  5508. (org-back-to-heading t)
  5509. (setq beg (point) end (org-end-of-subtree t t)))
  5510. (put-text-property beg end 'org-category cat)
  5511. (goto-char pos)))))))
  5512. ;;;; Link Stuff
  5513. ;;; Link abbreviations
  5514. (defun org-link-expand-abbrev (link)
  5515. "Apply replacements as defined in `org-link-abbrev-alist."
  5516. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5517. (let* ((key (match-string 1 link))
  5518. (as (or (assoc key org-link-abbrev-alist-local)
  5519. (assoc key org-link-abbrev-alist)))
  5520. (tag (and (match-end 2) (match-string 3 link)))
  5521. rpl)
  5522. (if (not as)
  5523. link
  5524. (setq rpl (cdr as))
  5525. (cond
  5526. ((symbolp rpl) (funcall rpl tag))
  5527. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5528. (t (concat rpl tag)))))
  5529. link))
  5530. ;;; Storing and inserting links
  5531. (defvar org-insert-link-history nil
  5532. "Minibuffer history for links inserted with `org-insert-link'.")
  5533. (defvar org-stored-links nil
  5534. "Contains the links stored with `org-store-link'.")
  5535. (defvar org-store-link-plist nil
  5536. "Plist with info about the most recently link created with `org-store-link'.")
  5537. (defvar org-link-protocols nil
  5538. "Link protocols added to Org-mode using `org-add-link-type'.")
  5539. (defvar org-store-link-functions nil
  5540. "List of functions that are called to create and store a link.
  5541. Each function will be called in turn until one returns a non-nil
  5542. value. Each function should check if it is responsible for creating
  5543. this link (for example by looking at the major mode).
  5544. If not, it must exit and return nil.
  5545. If yes, it should return a non-nil value after a calling
  5546. `org-store-link-props' with a list of properties and values.
  5547. Special properties are:
  5548. :type The link prefix. like \"http\". This must be given.
  5549. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5550. This is obligatory as well.
  5551. :description Optional default description for the second pair
  5552. of brackets in an Org-mode link. The user can still change
  5553. this when inserting this link into an Org-mode buffer.
  5554. In addition to these, any additional properties can be specified
  5555. and then used in remember templates.")
  5556. (defun org-add-link-type (type &optional follow export)
  5557. "Add TYPE to the list of `org-link-types'.
  5558. Re-compute all regular expressions depending on `org-link-types'
  5559. FOLLOW and EXPORT are two functions.
  5560. FOLLOW should take the link path as the single argument and do whatever
  5561. is necessary to follow the link, for example find a file or display
  5562. a mail message.
  5563. EXPORT should format the link path for export to one of the export formats.
  5564. It should be a function accepting three arguments:
  5565. path the path of the link, the text after the prefix (like \"http:\")
  5566. desc the description of the link, if any, nil if there was no descripton
  5567. format the export format, a symbol like `html' or `latex'.
  5568. The function may use the FORMAT information to return different values
  5569. depending on the format. The return value will be put literally into
  5570. the exported file.
  5571. Org-mode has a built-in default for exporting links. If you are happy with
  5572. this default, there is no need to define an export function for the link
  5573. type. For a simple example of an export function, see `org-bbdb.el'."
  5574. (add-to-list 'org-link-types type t)
  5575. (org-make-link-regexps)
  5576. (if (assoc type org-link-protocols)
  5577. (setcdr (assoc type org-link-protocols) (list follow export))
  5578. (push (list type follow export) org-link-protocols)))
  5579. ;;;###autoload
  5580. (defun org-store-link (arg)
  5581. "\\<org-mode-map>Store an org-link to the current location.
  5582. This link is added to `org-stored-links' and can later be inserted
  5583. into an org-buffer with \\[org-insert-link].
  5584. For some link types, a prefix arg is interpreted:
  5585. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5586. For file links, arg negates `org-context-in-file-links'."
  5587. (interactive "P")
  5588. (org-load-modules-maybe)
  5589. (setq org-store-link-plist nil) ; reset
  5590. (let (link cpltxt desc description search txt)
  5591. (cond
  5592. ((run-hook-with-args-until-success 'org-store-link-functions)
  5593. (setq link (plist-get org-store-link-plist :link)
  5594. desc (or (plist-get org-store-link-plist :description) link)))
  5595. ((eq major-mode 'calendar-mode)
  5596. (let ((cd (calendar-cursor-to-date)))
  5597. (setq link
  5598. (format-time-string
  5599. (car org-time-stamp-formats)
  5600. (apply 'encode-time
  5601. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5602. nil nil nil))))
  5603. (org-store-link-props :type "calendar" :date cd)))
  5604. ((eq major-mode 'w3-mode)
  5605. (setq cpltxt (url-view-url t)
  5606. link (org-make-link cpltxt))
  5607. (org-store-link-props :type "w3" :url (url-view-url t)))
  5608. ((eq major-mode 'w3m-mode)
  5609. (setq cpltxt (or w3m-current-title w3m-current-url)
  5610. link (org-make-link w3m-current-url))
  5611. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5612. ((setq search (run-hook-with-args-until-success
  5613. 'org-create-file-search-functions))
  5614. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5615. "::" search))
  5616. (setq cpltxt (or description link)))
  5617. ((eq major-mode 'image-mode)
  5618. (setq cpltxt (concat "file:"
  5619. (abbreviate-file-name buffer-file-name))
  5620. link (org-make-link cpltxt))
  5621. (org-store-link-props :type "image" :file buffer-file-name))
  5622. ((eq major-mode 'dired-mode)
  5623. ;; link to the file in the current line
  5624. (setq cpltxt (concat "file:"
  5625. (abbreviate-file-name
  5626. (expand-file-name
  5627. (dired-get-filename nil t))))
  5628. link (org-make-link cpltxt)))
  5629. ((and buffer-file-name (org-mode-p))
  5630. ;; Just link to current headline
  5631. (setq cpltxt (concat "file:"
  5632. (abbreviate-file-name buffer-file-name)))
  5633. ;; Add a context search string
  5634. (when (org-xor org-context-in-file-links arg)
  5635. ;; Check if we are on a target
  5636. (if (org-in-regexp "<<\\(.*?\\)>>")
  5637. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5638. (setq txt (cond
  5639. ((org-on-heading-p) nil)
  5640. ((org-region-active-p)
  5641. (buffer-substring (region-beginning) (region-end)))
  5642. (t nil)))
  5643. (when (or (null txt) (string-match "\\S-" txt))
  5644. (setq cpltxt
  5645. (concat cpltxt "::"
  5646. (condition-case nil
  5647. (org-make-org-heading-search-string txt)
  5648. (error "")))
  5649. desc "NONE"))))
  5650. (if (string-match "::\\'" cpltxt)
  5651. (setq cpltxt (substring cpltxt 0 -2)))
  5652. (setq link (org-make-link cpltxt)))
  5653. ((buffer-file-name (buffer-base-buffer))
  5654. ;; Just link to this file here.
  5655. (setq cpltxt (concat "file:"
  5656. (abbreviate-file-name
  5657. (buffer-file-name (buffer-base-buffer)))))
  5658. ;; Add a context string
  5659. (when (org-xor org-context-in-file-links arg)
  5660. (setq txt (if (org-region-active-p)
  5661. (buffer-substring (region-beginning) (region-end))
  5662. (buffer-substring (point-at-bol) (point-at-eol))))
  5663. ;; Only use search option if there is some text.
  5664. (when (string-match "\\S-" txt)
  5665. (setq cpltxt
  5666. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5667. desc "NONE")))
  5668. (setq link (org-make-link cpltxt)))
  5669. ((interactive-p)
  5670. (error "Cannot link to a buffer which is not visiting a file"))
  5671. (t (setq link nil)))
  5672. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5673. (setq link (or link cpltxt)
  5674. desc (or desc cpltxt))
  5675. (if (equal desc "NONE") (setq desc nil))
  5676. (if (and (interactive-p) link)
  5677. (progn
  5678. (setq org-stored-links
  5679. (cons (list link desc) org-stored-links))
  5680. (message "Stored: %s" (or desc link)))
  5681. (and link (org-make-link-string link desc)))))
  5682. (defun org-store-link-props (&rest plist)
  5683. "Store link properties, extract names and addresses."
  5684. (let (x adr)
  5685. (when (setq x (plist-get plist :from))
  5686. (setq adr (mail-extract-address-components x))
  5687. (plist-put plist :fromname (car adr))
  5688. (plist-put plist :fromaddress (nth 1 adr)))
  5689. (when (setq x (plist-get plist :to))
  5690. (setq adr (mail-extract-address-components x))
  5691. (plist-put plist :toname (car adr))
  5692. (plist-put plist :toaddress (nth 1 adr))))
  5693. (let ((from (plist-get plist :from))
  5694. (to (plist-get plist :to)))
  5695. (when (and from to org-from-is-user-regexp)
  5696. (plist-put plist :fromto
  5697. (if (string-match org-from-is-user-regexp from)
  5698. (concat "to %t")
  5699. (concat "from %f")))))
  5700. (setq org-store-link-plist plist))
  5701. (defun org-add-link-props (&rest plist)
  5702. "Add these properties to the link property list."
  5703. (let (key value)
  5704. (while plist
  5705. (setq key (pop plist) value (pop plist))
  5706. (setq org-store-link-plist
  5707. (plist-put org-store-link-plist key value)))))
  5708. (defun org-email-link-description (&optional fmt)
  5709. "Return the description part of an email link.
  5710. This takes information from `org-store-link-plist' and formats it
  5711. according to FMT (default from `org-email-link-description-format')."
  5712. (setq fmt (or fmt org-email-link-description-format))
  5713. (let* ((p org-store-link-plist)
  5714. (to (plist-get p :toaddress))
  5715. (from (plist-get p :fromaddress))
  5716. (table
  5717. (list
  5718. (cons "%c" (plist-get p :fromto))
  5719. (cons "%F" (plist-get p :from))
  5720. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5721. (cons "%T" (plist-get p :to))
  5722. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5723. (cons "%s" (plist-get p :subject))
  5724. (cons "%m" (plist-get p :message-id)))))
  5725. (when (string-match "%c" fmt)
  5726. ;; Check if the user wrote this message
  5727. (if (and org-from-is-user-regexp from to
  5728. (save-match-data (string-match org-from-is-user-regexp from)))
  5729. (setq fmt (replace-match "to %t" t t fmt))
  5730. (setq fmt (replace-match "from %f" t t fmt))))
  5731. (org-replace-escapes fmt table)))
  5732. (defun org-make-org-heading-search-string (&optional string heading)
  5733. "Make search string for STRING or current headline."
  5734. (interactive)
  5735. (let ((s (or string (org-get-heading))))
  5736. (unless (and string (not heading))
  5737. ;; We are using a headline, clean up garbage in there.
  5738. (if (string-match org-todo-regexp s)
  5739. (setq s (replace-match "" t t s)))
  5740. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5741. (setq s (replace-match "" t t s)))
  5742. (setq s (org-trim s))
  5743. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5744. org-comment-string "\\)") s)
  5745. (setq s (replace-match "" t t s)))
  5746. (while (string-match org-ts-regexp s)
  5747. (setq s (replace-match "" t t s))))
  5748. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5749. (setq s (replace-match " " t t s)))
  5750. (or string (setq s (concat "*" s))) ; Add * for headlines
  5751. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5752. (defun org-make-link (&rest strings)
  5753. "Concatenate STRINGS."
  5754. (apply 'concat strings))
  5755. (defun org-make-link-string (link &optional description)
  5756. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5757. (unless (string-match "\\S-" link)
  5758. (error "Empty link"))
  5759. (when (stringp description)
  5760. ;; Remove brackets from the description, they are fatal.
  5761. (while (string-match "\\[" description)
  5762. (setq description (replace-match "{" t t description)))
  5763. (while (string-match "\\]" description)
  5764. (setq description (replace-match "}" t t description))))
  5765. (when (equal (org-link-escape link) description)
  5766. ;; No description needed, it is identical
  5767. (setq description nil))
  5768. (when (and (not description)
  5769. (not (equal link (org-link-escape link))))
  5770. (setq description link))
  5771. (concat "[[" (org-link-escape link) "]"
  5772. (if description (concat "[" description "]") "")
  5773. "]"))
  5774. (defconst org-link-escape-chars
  5775. '((?\ . "%20")
  5776. (?\[ . "%5B")
  5777. (?\] . "%5D")
  5778. (?\340 . "%E0") ; `a
  5779. (?\342 . "%E2") ; ^a
  5780. (?\347 . "%E7") ; ,c
  5781. (?\350 . "%E8") ; `e
  5782. (?\351 . "%E9") ; 'e
  5783. (?\352 . "%EA") ; ^e
  5784. (?\356 . "%EE") ; ^i
  5785. (?\364 . "%F4") ; ^o
  5786. (?\371 . "%F9") ; `u
  5787. (?\373 . "%FB") ; ^u
  5788. (?\; . "%3B")
  5789. (?? . "%3F")
  5790. (?= . "%3D")
  5791. (?+ . "%2B")
  5792. )
  5793. "Association list of escapes for some characters problematic in links.
  5794. This is the list that is used for internal purposes.")
  5795. (defconst org-link-escape-chars-browser
  5796. '((?\ . "%20")) ; 32 for the SPC char
  5797. "Association list of escapes for some characters problematic in links.
  5798. This is the list that is used before handing over to the browser.")
  5799. (defun org-link-escape (text &optional table)
  5800. "Escape charaters in TEXT that are problematic for links."
  5801. (setq table (or table org-link-escape-chars))
  5802. (when text
  5803. (let ((re (mapconcat (lambda (x) (regexp-quote
  5804. (char-to-string (car x))))
  5805. table "\\|")))
  5806. (while (string-match re text)
  5807. (setq text
  5808. (replace-match
  5809. (cdr (assoc (string-to-char (match-string 0 text))
  5810. table))
  5811. t t text)))
  5812. text)))
  5813. (defun org-link-unescape (text &optional table)
  5814. "Reverse the action of `org-link-escape'."
  5815. (setq table (or table org-link-escape-chars))
  5816. (when text
  5817. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5818. table "\\|")))
  5819. (while (string-match re text)
  5820. (setq text
  5821. (replace-match
  5822. (char-to-string (car (rassoc (match-string 0 text) table)))
  5823. t t text)))
  5824. text)))
  5825. (defun org-xor (a b)
  5826. "Exclusive or."
  5827. (if a (not b) b))
  5828. (defun org-get-header (header)
  5829. "Find a header field in the current buffer."
  5830. (save-excursion
  5831. (goto-char (point-min))
  5832. (let ((case-fold-search t) s)
  5833. (cond
  5834. ((eq header 'from)
  5835. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5836. (setq s (match-string 1)))
  5837. (while (string-match "\"" s)
  5838. (setq s (replace-match "" t t s)))
  5839. (if (string-match "[<(].*" s)
  5840. (setq s (replace-match "" t t s))))
  5841. ((eq header 'message-id)
  5842. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5843. (setq s (match-string 1))))
  5844. ((eq header 'subject)
  5845. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5846. (setq s (match-string 1)))))
  5847. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5848. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5849. s)))
  5850. (defun org-fixup-message-id-for-http (s)
  5851. "Replace special characters in a message id, so it can be used in an http query."
  5852. (while (string-match "<" s)
  5853. (setq s (replace-match "%3C" t t s)))
  5854. (while (string-match ">" s)
  5855. (setq s (replace-match "%3E" t t s)))
  5856. (while (string-match "@" s)
  5857. (setq s (replace-match "%40" t t s)))
  5858. s)
  5859. ;;;###autoload
  5860. (defun org-insert-link-global ()
  5861. "Insert a link like Org-mode does.
  5862. This command can be called in any mode to insert a link in Org-mode syntax."
  5863. (interactive)
  5864. (org-load-modules-maybe)
  5865. (org-run-like-in-org-mode 'org-insert-link))
  5866. (defun org-insert-link (&optional complete-file link-location)
  5867. "Insert a link. At the prompt, enter the link.
  5868. Completion can be used to select a link previously stored with
  5869. `org-store-link'. When the empty string is entered (i.e. if you just
  5870. press RET at the prompt), the link defaults to the most recently
  5871. stored link. As SPC triggers completion in the minibuffer, you need to
  5872. use M-SPC or C-q SPC to force the insertion of a space character.
  5873. You will also be prompted for a description, and if one is given, it will
  5874. be displayed in the buffer instead of the link.
  5875. If there is already a link at point, this command will allow you to edit link
  5876. and description parts.
  5877. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5878. be selected using completion. The path to the file will be relative to the
  5879. current directory if the file is in the current directory or a subdirectory.
  5880. Otherwise, the link will be the absolute path as completed in the minibuffer
  5881. \(i.e. normally ~/path/to/file).
  5882. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5883. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  5884. of `org-keep-stored-link-after-insertion'.
  5885. If `org-make-link-description-function' is non-nil, this function will be
  5886. called with the link target, and the result will be the default
  5887. link description.
  5888. If the LINK-LOCATION parameter is non-nil, this value will be
  5889. used as the link location instead of reading one interactively."
  5890. (interactive "P")
  5891. (let* ((wcf (current-window-configuration))
  5892. (region (if (org-region-active-p)
  5893. (buffer-substring (region-beginning) (region-end))))
  5894. (remove (and region (list (region-beginning) (region-end))))
  5895. (desc region)
  5896. tmphist ; byte-compile incorrectly complains about this
  5897. (link link-location)
  5898. entry file)
  5899. (cond
  5900. (link-location) ; specified by arg, just use it.
  5901. ((org-in-regexp org-bracket-link-regexp 1)
  5902. ;; We do have a link at point, and we are going to edit it.
  5903. (setq remove (list (match-beginning 0) (match-end 0)))
  5904. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5905. (setq link (read-string "Link: "
  5906. (org-link-unescape
  5907. (org-match-string-no-properties 1)))))
  5908. ((or (org-in-regexp org-angle-link-re)
  5909. (org-in-regexp org-plain-link-re))
  5910. ;; Convert to bracket link
  5911. (setq remove (list (match-beginning 0) (match-end 0))
  5912. link (read-string "Link: "
  5913. (org-remove-angle-brackets (match-string 0)))))
  5914. ((equal complete-file '(4))
  5915. ;; Completing read for file names.
  5916. (setq file (read-file-name "File: "))
  5917. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5918. (pwd1 (file-name-as-directory (abbreviate-file-name
  5919. (expand-file-name ".")))))
  5920. (cond
  5921. ((equal complete-file '(16))
  5922. (setq link (org-make-link
  5923. "file:"
  5924. (abbreviate-file-name (expand-file-name file)))))
  5925. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5926. (setq link (org-make-link "file:" (match-string 1 file))))
  5927. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5928. (expand-file-name file))
  5929. (setq link (org-make-link
  5930. "file:" (match-string 1 (expand-file-name file)))))
  5931. (t (setq link (org-make-link "file:" file))))))
  5932. (t
  5933. ;; Read link, with completion for stored links.
  5934. (with-output-to-temp-buffer "*Org Links*"
  5935. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5936. (when org-stored-links
  5937. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5938. (princ (mapconcat
  5939. (lambda (x)
  5940. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5941. (reverse org-stored-links) "\n"))))
  5942. (let ((cw (selected-window)))
  5943. (select-window (get-buffer-window "*Org Links*"))
  5944. (shrink-window-if-larger-than-buffer)
  5945. (setq truncate-lines t)
  5946. (select-window cw))
  5947. ;; Fake a link history, containing the stored links.
  5948. (setq tmphist (append (mapcar 'car org-stored-links)
  5949. org-insert-link-history))
  5950. (unwind-protect
  5951. (setq link (org-completing-read
  5952. "Link: "
  5953. (append
  5954. (mapcar (lambda (x) (list (concat (car x) ":")))
  5955. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5956. (mapcar (lambda (x) (list (concat x ":")))
  5957. org-link-types))
  5958. nil nil nil
  5959. 'tmphist
  5960. (or (car (car org-stored-links)))))
  5961. (set-window-configuration wcf)
  5962. (kill-buffer "*Org Links*"))
  5963. (setq entry (assoc link org-stored-links))
  5964. (or entry (push link org-insert-link-history))
  5965. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5966. (not org-keep-stored-link-after-insertion))
  5967. (setq org-stored-links (delq (assoc link org-stored-links)
  5968. org-stored-links)))
  5969. (setq desc (or desc (nth 1 entry)))))
  5970. (if (string-match org-plain-link-re link)
  5971. ;; URL-like link, normalize the use of angular brackets.
  5972. (setq link (org-make-link (org-remove-angle-brackets link))))
  5973. ;; Check if we are linking to the current file with a search option
  5974. ;; If yes, simplify the link by using only the search option.
  5975. (when (and buffer-file-name
  5976. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  5977. (let* ((path (match-string 1 link))
  5978. (case-fold-search nil)
  5979. (search (match-string 2 link)))
  5980. (save-match-data
  5981. (if (equal (file-truename buffer-file-name) (file-truename path))
  5982. ;; We are linking to this same file, with a search option
  5983. (setq link search)))))
  5984. ;; Check if we can/should use a relative path. If yes, simplify the link
  5985. (when (string-match "\\<file:\\(.*\\)" link)
  5986. (let* ((path (match-string 1 link))
  5987. (origpath path)
  5988. (case-fold-search nil))
  5989. (cond
  5990. ((eq org-link-file-path-type 'absolute)
  5991. (setq path (abbreviate-file-name (expand-file-name path))))
  5992. ((eq org-link-file-path-type 'noabbrev)
  5993. (setq path (expand-file-name path)))
  5994. ((eq org-link-file-path-type 'relative)
  5995. (setq path (file-relative-name path)))
  5996. (t
  5997. (save-match-data
  5998. (if (string-match (concat "^" (regexp-quote
  5999. (file-name-as-directory
  6000. (expand-file-name "."))))
  6001. (expand-file-name path))
  6002. ;; We are linking a file with relative path name.
  6003. (setq path (substring (expand-file-name path)
  6004. (match-end 0)))))))
  6005. (setq link (concat "file:" path))
  6006. (if (equal desc origpath)
  6007. (setq desc path))))
  6008. (if org-make-link-description-function
  6009. (setq desc (funcall org-make-link-description-function link desc)))
  6010. (setq desc (read-string "Description: " desc))
  6011. (unless (string-match "\\S-" desc) (setq desc nil))
  6012. (if remove (apply 'delete-region remove))
  6013. (insert (org-make-link-string link desc))))
  6014. (defun org-completing-read (&rest args)
  6015. (let ((minibuffer-local-completion-map
  6016. (copy-keymap minibuffer-local-completion-map)))
  6017. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6018. (apply 'completing-read args)))
  6019. ;;; Opening/following a link
  6020. (defvar org-link-search-failed nil)
  6021. (defun org-next-link ()
  6022. "Move forward to the next link.
  6023. If the link is in hidden text, expose it."
  6024. (interactive)
  6025. (when (and org-link-search-failed (eq this-command last-command))
  6026. (goto-char (point-min))
  6027. (message "Link search wrapped back to beginning of buffer"))
  6028. (setq org-link-search-failed nil)
  6029. (let* ((pos (point))
  6030. (ct (org-context))
  6031. (a (assoc :link ct)))
  6032. (if a (goto-char (nth 2 a)))
  6033. (if (re-search-forward org-any-link-re nil t)
  6034. (progn
  6035. (goto-char (match-beginning 0))
  6036. (if (org-invisible-p) (org-show-context)))
  6037. (goto-char pos)
  6038. (setq org-link-search-failed t)
  6039. (error "No further link found"))))
  6040. (defun org-previous-link ()
  6041. "Move backward to the previous link.
  6042. If the link is in hidden text, expose it."
  6043. (interactive)
  6044. (when (and org-link-search-failed (eq this-command last-command))
  6045. (goto-char (point-max))
  6046. (message "Link search wrapped back to end of buffer"))
  6047. (setq org-link-search-failed nil)
  6048. (let* ((pos (point))
  6049. (ct (org-context))
  6050. (a (assoc :link ct)))
  6051. (if a (goto-char (nth 1 a)))
  6052. (if (re-search-backward org-any-link-re nil t)
  6053. (progn
  6054. (goto-char (match-beginning 0))
  6055. (if (org-invisible-p) (org-show-context)))
  6056. (goto-char pos)
  6057. (setq org-link-search-failed t)
  6058. (error "No further link found"))))
  6059. (defun org-find-file-at-mouse (ev)
  6060. "Open file link or URL at mouse."
  6061. (interactive "e")
  6062. (mouse-set-point ev)
  6063. (org-open-at-point 'in-emacs))
  6064. (defun org-open-at-mouse (ev)
  6065. "Open file link or URL at mouse."
  6066. (interactive "e")
  6067. (mouse-set-point ev)
  6068. (org-open-at-point))
  6069. (defvar org-window-config-before-follow-link nil
  6070. "The window configuration before following a link.
  6071. This is saved in case the need arises to restore it.")
  6072. (defvar org-open-link-marker (make-marker)
  6073. "Marker pointing to the location where `org-open-at-point; was called.")
  6074. ;;;###autoload
  6075. (defun org-open-at-point-global ()
  6076. "Follow a link like Org-mode does.
  6077. This command can be called in any mode to follow a link that has
  6078. Org-mode syntax."
  6079. (interactive)
  6080. (org-run-like-in-org-mode 'org-open-at-point))
  6081. ;;;###autoload
  6082. (defun org-open-link-from-string (s &optional arg)
  6083. "Open a link in the string S, as if it was in Org-mode."
  6084. (interactive "sLink: \nP")
  6085. (with-temp-buffer
  6086. (let ((org-inhibit-startup t))
  6087. (org-mode)
  6088. (insert s)
  6089. (goto-char (point-min))
  6090. (org-open-at-point arg))))
  6091. (defun org-open-at-point (&optional in-emacs)
  6092. "Open link at or after point.
  6093. If there is no link at point, this function will search forward up to
  6094. the end of the current subtree.
  6095. Normally, files will be opened by an appropriate application. If the
  6096. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6097. (interactive "P")
  6098. (org-load-modules-maybe)
  6099. (move-marker org-open-link-marker (point))
  6100. (setq org-window-config-before-follow-link (current-window-configuration))
  6101. (org-remove-occur-highlights nil nil t)
  6102. (if (org-at-timestamp-p t)
  6103. (org-follow-timestamp-link)
  6104. (let (type path link line search (pos (point)))
  6105. (catch 'match
  6106. (save-excursion
  6107. (skip-chars-forward "^]\n\r")
  6108. (when (org-in-regexp org-bracket-link-regexp)
  6109. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6110. (while (string-match " *\n *" link)
  6111. (setq link (replace-match " " t t link)))
  6112. (setq link (org-link-expand-abbrev link))
  6113. (if (string-match org-link-re-with-space2 link)
  6114. (setq type (match-string 1 link) path (match-string 2 link))
  6115. (setq type "thisfile" path link))
  6116. (throw 'match t)))
  6117. (when (get-text-property (point) 'org-linked-text)
  6118. (setq type "thisfile"
  6119. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6120. (1+ (point)) (point))
  6121. path (buffer-substring
  6122. (previous-single-property-change pos 'org-linked-text)
  6123. (next-single-property-change pos 'org-linked-text)))
  6124. (throw 'match t))
  6125. (save-excursion
  6126. (when (or (org-in-regexp org-angle-link-re)
  6127. (org-in-regexp org-plain-link-re))
  6128. (setq type (match-string 1) path (match-string 2))
  6129. (throw 'match t)))
  6130. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6131. (setq type "tree-match"
  6132. path (match-string 1))
  6133. (throw 'match t))
  6134. (save-excursion
  6135. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6136. (setq type "tags"
  6137. path (match-string 1))
  6138. (while (string-match ":" path)
  6139. (setq path (replace-match "+" t t path)))
  6140. (throw 'match t))))
  6141. (unless path
  6142. (error "No link found"))
  6143. ;; Remove any trailing spaces in path
  6144. (if (string-match " +\\'" path)
  6145. (setq path (replace-match "" t t path)))
  6146. (cond
  6147. ((assoc type org-link-protocols)
  6148. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6149. ((equal type "mailto")
  6150. (let ((cmd (car org-link-mailto-program))
  6151. (args (cdr org-link-mailto-program)) args1
  6152. (address path) (subject "") a)
  6153. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6154. (setq address (match-string 1 path)
  6155. subject (org-link-escape (match-string 2 path))))
  6156. (while args
  6157. (cond
  6158. ((not (stringp (car args))) (push (pop args) args1))
  6159. (t (setq a (pop args))
  6160. (if (string-match "%a" a)
  6161. (setq a (replace-match address t t a)))
  6162. (if (string-match "%s" a)
  6163. (setq a (replace-match subject t t a)))
  6164. (push a args1))))
  6165. (apply cmd (nreverse args1))))
  6166. ((member type '("http" "https" "ftp" "news"))
  6167. (browse-url (concat type ":" (org-link-escape
  6168. path org-link-escape-chars-browser))))
  6169. ((member type '("message"))
  6170. (browse-url (concat type ":" path)))
  6171. ((string= type "tags")
  6172. (org-tags-view in-emacs path))
  6173. ((string= type "thisfile")
  6174. (if in-emacs
  6175. (switch-to-buffer-other-window
  6176. (org-get-buffer-for-internal-link (current-buffer)))
  6177. (org-mark-ring-push))
  6178. (let ((cmd `(org-link-search
  6179. ,path
  6180. ,(cond ((equal in-emacs '(4)) 'occur)
  6181. ((equal in-emacs '(16)) 'org-occur)
  6182. (t nil))
  6183. ,pos)))
  6184. (condition-case nil (eval cmd)
  6185. (error (progn (widen) (eval cmd))))))
  6186. ((string= type "tree-match")
  6187. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6188. ((string= type "file")
  6189. (if (string-match "::\\([0-9]+\\)\\'" path)
  6190. (setq line (string-to-number (match-string 1 path))
  6191. path (substring path 0 (match-beginning 0)))
  6192. (if (string-match "::\\(.+\\)\\'" path)
  6193. (setq search (match-string 1 path)
  6194. path (substring path 0 (match-beginning 0)))))
  6195. (if (string-match "[*?{]" (file-name-nondirectory path))
  6196. (dired path)
  6197. (org-open-file path in-emacs line search)))
  6198. ((string= type "news")
  6199. (require 'org-gnus)
  6200. (org-gnus-follow-link path))
  6201. ((string= type "shell")
  6202. (let ((cmd path))
  6203. (if (or (not org-confirm-shell-link-function)
  6204. (funcall org-confirm-shell-link-function
  6205. (format "Execute \"%s\" in shell? "
  6206. (org-add-props cmd nil
  6207. 'face 'org-warning))))
  6208. (progn
  6209. (message "Executing %s" cmd)
  6210. (shell-command cmd))
  6211. (error "Abort"))))
  6212. ((string= type "elisp")
  6213. (let ((cmd path))
  6214. (if (or (not org-confirm-elisp-link-function)
  6215. (funcall org-confirm-elisp-link-function
  6216. (format "Execute \"%s\" as elisp? "
  6217. (org-add-props cmd nil
  6218. 'face 'org-warning))))
  6219. (message "%s => %s" cmd (eval (read cmd)))
  6220. (error "Abort"))))
  6221. (t
  6222. (browse-url-at-point)))))
  6223. (move-marker org-open-link-marker nil)
  6224. (run-hook-with-args 'org-follow-link-hook))
  6225. ;;;; Time estimates
  6226. (defun org-get-effort (&optional pom)
  6227. "Get the effort estimate for the current entry."
  6228. (org-entry-get pom org-effort-property))
  6229. ;;; File search
  6230. (defvar org-create-file-search-functions nil
  6231. "List of functions to construct the right search string for a file link.
  6232. These functions are called in turn with point at the location to
  6233. which the link should point.
  6234. A function in the hook should first test if it would like to
  6235. handle this file type, for example by checking the major-mode or
  6236. the file extension. If it decides not to handle this file, it
  6237. should just return nil to give other functions a chance. If it
  6238. does handle the file, it must return the search string to be used
  6239. when following the link. The search string will be part of the
  6240. file link, given after a double colon, and `org-open-at-point'
  6241. will automatically search for it. If special measures must be
  6242. taken to make the search successful, another function should be
  6243. added to the companion hook `org-execute-file-search-functions',
  6244. which see.
  6245. A function in this hook may also use `setq' to set the variable
  6246. `description' to provide a suggestion for the descriptive text to
  6247. be used for this link when it gets inserted into an Org-mode
  6248. buffer with \\[org-insert-link].")
  6249. (defvar org-execute-file-search-functions nil
  6250. "List of functions to execute a file search triggered by a link.
  6251. Functions added to this hook must accept a single argument, the
  6252. search string that was part of the file link, the part after the
  6253. double colon. The function must first check if it would like to
  6254. handle this search, for example by checking the major-mode or the
  6255. file extension. If it decides not to handle this search, it
  6256. should just return nil to give other functions a chance. If it
  6257. does handle the search, it must return a non-nil value to keep
  6258. other functions from trying.
  6259. Each function can access the current prefix argument through the
  6260. variable `current-prefix-argument'. Note that a single prefix is
  6261. used to force opening a link in Emacs, so it may be good to only
  6262. use a numeric or double prefix to guide the search function.
  6263. In case this is needed, a function in this hook can also restore
  6264. the window configuration before `org-open-at-point' was called using:
  6265. (set-window-configuration org-window-config-before-follow-link)")
  6266. (defun org-link-search (s &optional type avoid-pos)
  6267. "Search for a link search option.
  6268. If S is surrounded by forward slashes, it is interpreted as a
  6269. regular expression. In org-mode files, this will create an `org-occur'
  6270. sparse tree. In ordinary files, `occur' will be used to list matches.
  6271. If the current buffer is in `dired-mode', grep will be used to search
  6272. in all files. If AVOID-POS is given, ignore matches near that position."
  6273. (let ((case-fold-search t)
  6274. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6275. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6276. (append '(("") (" ") ("\t") ("\n"))
  6277. org-emphasis-alist)
  6278. "\\|") "\\)"))
  6279. (pos (point))
  6280. (pre nil) (post nil)
  6281. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6282. (cond
  6283. ;; First check if there are any special
  6284. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6285. ;; Now try the builtin stuff
  6286. ((save-excursion
  6287. (goto-char (point-min))
  6288. (and
  6289. (re-search-forward
  6290. (concat "<<" (regexp-quote s0) ">>") nil t)
  6291. (setq type 'dedicated
  6292. pos (match-beginning 0))))
  6293. ;; There is an exact target for this
  6294. (goto-char pos))
  6295. ((string-match "^/\\(.*\\)/$" s)
  6296. ;; A regular expression
  6297. (cond
  6298. ((org-mode-p)
  6299. (org-occur (match-string 1 s)))
  6300. ;;((eq major-mode 'dired-mode)
  6301. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6302. (t (org-do-occur (match-string 1 s)))))
  6303. (t
  6304. ;; A normal search strings
  6305. (when (equal (string-to-char s) ?*)
  6306. ;; Anchor on headlines, post may include tags.
  6307. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6308. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6309. s (substring s 1)))
  6310. (remove-text-properties
  6311. 0 (length s)
  6312. '(face nil mouse-face nil keymap nil fontified nil) s)
  6313. ;; Make a series of regular expressions to find a match
  6314. (setq words (org-split-string s "[ \n\r\t]+")
  6315. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6316. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6317. "\\)" markers)
  6318. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6319. re2a (concat "[ \t\r\n]" re2a_)
  6320. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6321. re4 (concat "[^a-zA-Z_]" re4_)
  6322. re1 (concat pre re2 post)
  6323. re3 (concat pre (if pre re4_ re4) post)
  6324. re5 (concat pre ".*" re4)
  6325. re2 (concat pre re2)
  6326. re2a (concat pre (if pre re2a_ re2a))
  6327. re4 (concat pre (if pre re4_ re4))
  6328. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6329. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6330. re5 "\\)"
  6331. ))
  6332. (cond
  6333. ((eq type 'org-occur) (org-occur reall))
  6334. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6335. (t (goto-char (point-min))
  6336. (setq type 'fuzzy)
  6337. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6338. (org-search-not-self 1 re1 nil t)
  6339. (org-search-not-self 1 re2 nil t)
  6340. (org-search-not-self 1 re2a nil t)
  6341. (org-search-not-self 1 re3 nil t)
  6342. (org-search-not-self 1 re4 nil t)
  6343. (org-search-not-self 1 re5 nil t)
  6344. )
  6345. (goto-char (match-beginning 1))
  6346. (goto-char pos)
  6347. (error "No match")))))
  6348. (t
  6349. ;; Normal string-search
  6350. (goto-char (point-min))
  6351. (if (search-forward s nil t)
  6352. (goto-char (match-beginning 0))
  6353. (error "No match"))))
  6354. (and (org-mode-p) (org-show-context 'link-search))
  6355. type))
  6356. (defun org-search-not-self (group &rest args)
  6357. "Execute `re-search-forward', but only accept matches that do not
  6358. enclose the position of `org-open-link-marker'."
  6359. (let ((m org-open-link-marker))
  6360. (catch 'exit
  6361. (while (apply 're-search-forward args)
  6362. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6363. (goto-char (match-end group))
  6364. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6365. (> (match-beginning 0) (marker-position m))
  6366. (< (match-end 0) (marker-position m)))
  6367. (save-match-data
  6368. (or (not (org-in-regexp
  6369. org-bracket-link-analytic-regexp 1))
  6370. (not (match-end 4)) ; no description
  6371. (and (<= (match-beginning 4) (point))
  6372. (>= (match-end 4) (point))))))
  6373. (throw 'exit (point))))))))
  6374. (defun org-get-buffer-for-internal-link (buffer)
  6375. "Return a buffer to be used for displaying the link target of internal links."
  6376. (cond
  6377. ((not org-display-internal-link-with-indirect-buffer)
  6378. buffer)
  6379. ((string-match "(Clone)$" (buffer-name buffer))
  6380. (message "Buffer is already a clone, not making another one")
  6381. ;; we also do not modify visibility in this case
  6382. buffer)
  6383. (t ; make a new indirect buffer for displaying the link
  6384. (let* ((bn (buffer-name buffer))
  6385. (ibn (concat bn "(Clone)"))
  6386. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6387. (with-current-buffer ib (org-overview))
  6388. ib))))
  6389. (defun org-do-occur (regexp &optional cleanup)
  6390. "Call the Emacs command `occur'.
  6391. If CLEANUP is non-nil, remove the printout of the regular expression
  6392. in the *Occur* buffer. This is useful if the regex is long and not useful
  6393. to read."
  6394. (occur regexp)
  6395. (when cleanup
  6396. (let ((cwin (selected-window)) win beg end)
  6397. (when (setq win (get-buffer-window "*Occur*"))
  6398. (select-window win))
  6399. (goto-char (point-min))
  6400. (when (re-search-forward "match[a-z]+" nil t)
  6401. (setq beg (match-end 0))
  6402. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6403. (setq end (1- (match-beginning 0)))))
  6404. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6405. (goto-char (point-min))
  6406. (select-window cwin))))
  6407. ;;; The mark ring for links jumps
  6408. (defvar org-mark-ring nil
  6409. "Mark ring for positions before jumps in Org-mode.")
  6410. (defvar org-mark-ring-last-goto nil
  6411. "Last position in the mark ring used to go back.")
  6412. ;; Fill and close the ring
  6413. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6414. (loop for i from 1 to org-mark-ring-length do
  6415. (push (make-marker) org-mark-ring))
  6416. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6417. org-mark-ring)
  6418. (defun org-mark-ring-push (&optional pos buffer)
  6419. "Put the current position or POS into the mark ring and rotate it."
  6420. (interactive)
  6421. (setq pos (or pos (point)))
  6422. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6423. (move-marker (car org-mark-ring)
  6424. (or pos (point))
  6425. (or buffer (current-buffer)))
  6426. (message "%s"
  6427. (substitute-command-keys
  6428. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6429. (defun org-mark-ring-goto (&optional n)
  6430. "Jump to the previous position in the mark ring.
  6431. With prefix arg N, jump back that many stored positions. When
  6432. called several times in succession, walk through the entire ring.
  6433. Org-mode commands jumping to a different position in the current file,
  6434. or to another Org-mode file, automatically push the old position
  6435. onto the ring."
  6436. (interactive "p")
  6437. (let (p m)
  6438. (if (eq last-command this-command)
  6439. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6440. (setq p org-mark-ring))
  6441. (setq org-mark-ring-last-goto p)
  6442. (setq m (car p))
  6443. (switch-to-buffer (marker-buffer m))
  6444. (goto-char m)
  6445. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6446. (defun org-remove-angle-brackets (s)
  6447. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6448. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6449. s)
  6450. (defun org-add-angle-brackets (s)
  6451. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6452. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6453. s)
  6454. ;;; Following specific links
  6455. (defun org-follow-timestamp-link ()
  6456. (cond
  6457. ((org-at-date-range-p t)
  6458. (let ((org-agenda-start-on-weekday)
  6459. (t1 (match-string 1))
  6460. (t2 (match-string 2)))
  6461. (setq t1 (time-to-days (org-time-string-to-time t1))
  6462. t2 (time-to-days (org-time-string-to-time t2)))
  6463. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6464. ((org-at-timestamp-p t)
  6465. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6466. (substring (match-string 1) 0 10)))
  6467. 1))
  6468. (t (error "This should not happen"))))
  6469. ;;; Following file links
  6470. (defvar org-wait nil)
  6471. (defun org-open-file (path &optional in-emacs line search)
  6472. "Open the file at PATH.
  6473. First, this expands any special file name abbreviations. Then the
  6474. configuration variable `org-file-apps' is checked if it contains an
  6475. entry for this file type, and if yes, the corresponding command is launched.
  6476. If no application is found, Emacs simply visits the file.
  6477. With optional argument IN-EMACS, Emacs will visit the file.
  6478. Optional LINE specifies a line to go to, optional SEARCH a string to
  6479. search for. If LINE or SEARCH is given, the file will always be
  6480. opened in Emacs.
  6481. If the file does not exist, an error is thrown."
  6482. (setq in-emacs (or in-emacs line search))
  6483. (let* ((file (if (equal path "")
  6484. buffer-file-name
  6485. (substitute-in-file-name (expand-file-name path))))
  6486. (apps (append org-file-apps (org-default-apps)))
  6487. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6488. (dirp (if remp nil (file-directory-p file)))
  6489. (dfile (downcase file))
  6490. (old-buffer (current-buffer))
  6491. (old-pos (point))
  6492. (old-mode major-mode)
  6493. ext cmd)
  6494. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6495. (setq ext (match-string 1 dfile))
  6496. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6497. (setq ext (match-string 1 dfile))))
  6498. (if in-emacs
  6499. (setq cmd 'emacs)
  6500. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6501. (and dirp (cdr (assoc 'directory apps)))
  6502. (cdr (assoc ext apps))
  6503. (cdr (assoc t apps)))))
  6504. (when (eq cmd 'mailcap)
  6505. (require 'mailcap)
  6506. (mailcap-parse-mailcaps)
  6507. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6508. (command (mailcap-mime-info mime-type)))
  6509. (if (stringp command)
  6510. (setq cmd command)
  6511. (setq cmd 'emacs))))
  6512. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6513. (not (file-exists-p file))
  6514. (not org-open-non-existing-files))
  6515. (error "No such file: %s" file))
  6516. (cond
  6517. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6518. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6519. (while (string-match "['\"]%s['\"]" cmd)
  6520. (setq cmd (replace-match "%s" t t cmd)))
  6521. (while (string-match "%s" cmd)
  6522. (setq cmd (replace-match
  6523. (save-match-data (shell-quote-argument file))
  6524. t t cmd)))
  6525. (save-window-excursion
  6526. (start-process-shell-command cmd nil cmd)
  6527. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6528. ))
  6529. ((or (stringp cmd)
  6530. (eq cmd 'emacs))
  6531. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6532. (widen)
  6533. (if line (goto-line line)
  6534. (if search (org-link-search search))))
  6535. ((consp cmd)
  6536. (eval cmd))
  6537. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6538. (and (org-mode-p) (eq old-mode 'org-mode)
  6539. (or (not (equal old-buffer (current-buffer)))
  6540. (not (equal old-pos (point))))
  6541. (org-mark-ring-push old-pos old-buffer))))
  6542. (defun org-default-apps ()
  6543. "Return the default applications for this operating system."
  6544. (cond
  6545. ((eq system-type 'darwin)
  6546. org-file-apps-defaults-macosx)
  6547. ((eq system-type 'windows-nt)
  6548. org-file-apps-defaults-windowsnt)
  6549. (t org-file-apps-defaults-gnu)))
  6550. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6551. (defun org-file-remote-p (file)
  6552. "Test whether FILE specifies a location on a remote system.
  6553. Return non-nil if the location is indeed remote.
  6554. For example, the filename \"/user@host:/foo\" specifies a location
  6555. on the system \"/user@host:\"."
  6556. (cond ((fboundp 'file-remote-p)
  6557. (file-remote-p file))
  6558. ((fboundp 'tramp-handle-file-remote-p)
  6559. (tramp-handle-file-remote-p file))
  6560. ((and (boundp 'ange-ftp-name-format)
  6561. (string-match (car ange-ftp-name-format) file))
  6562. t)
  6563. (t nil)))
  6564. ;;;; Refiling
  6565. (defun org-get-org-file ()
  6566. "Read a filename, with default directory `org-directory'."
  6567. (let ((default (or org-default-notes-file remember-data-file)))
  6568. (read-file-name (format "File name [%s]: " default)
  6569. (file-name-as-directory org-directory)
  6570. default)))
  6571. (defun org-notes-order-reversed-p ()
  6572. "Check if the current file should receive notes in reversed order."
  6573. (cond
  6574. ((not org-reverse-note-order) nil)
  6575. ((eq t org-reverse-note-order) t)
  6576. ((not (listp org-reverse-note-order)) nil)
  6577. (t (catch 'exit
  6578. (let ((all org-reverse-note-order)
  6579. entry)
  6580. (while (setq entry (pop all))
  6581. (if (string-match (car entry) buffer-file-name)
  6582. (throw 'exit (cdr entry))))
  6583. nil)))))
  6584. (defvar org-refile-target-table nil
  6585. "The list of refile targets, created by `org-refile'.")
  6586. (defvar org-agenda-new-buffers nil
  6587. "Buffers created to visit agenda files.")
  6588. (defun org-get-refile-targets (&optional default-buffer)
  6589. "Produce a table with refile targets."
  6590. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6591. targets txt re files f desc descre)
  6592. (with-current-buffer (or default-buffer (current-buffer))
  6593. (while (setq entry (pop entries))
  6594. (setq files (car entry) desc (cdr entry))
  6595. (cond
  6596. ((null files) (setq files (list (current-buffer))))
  6597. ((eq files 'org-agenda-files)
  6598. (setq files (org-agenda-files 'unrestricted)))
  6599. ((and (symbolp files) (fboundp files))
  6600. (setq files (funcall files)))
  6601. ((and (symbolp files) (boundp files))
  6602. (setq files (symbol-value files))))
  6603. (if (stringp files) (setq files (list files)))
  6604. (cond
  6605. ((eq (car desc) :tag)
  6606. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6607. ((eq (car desc) :todo)
  6608. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6609. ((eq (car desc) :regexp)
  6610. (setq descre (cdr desc)))
  6611. ((eq (car desc) :level)
  6612. (setq descre (concat "^\\*\\{" (number-to-string
  6613. (if org-odd-levels-only
  6614. (1- (* 2 (cdr desc)))
  6615. (cdr desc)))
  6616. "\\}[ \t]")))
  6617. ((eq (car desc) :maxlevel)
  6618. (setq descre (concat "^\\*\\{1," (number-to-string
  6619. (if org-odd-levels-only
  6620. (1- (* 2 (cdr desc)))
  6621. (cdr desc)))
  6622. "\\}[ \t]")))
  6623. (t (error "Bad refiling target description %s" desc)))
  6624. (while (setq f (pop files))
  6625. (save-excursion
  6626. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6627. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6628. (save-excursion
  6629. (save-restriction
  6630. (widen)
  6631. (goto-char (point-min))
  6632. (while (re-search-forward descre nil t)
  6633. (goto-char (point-at-bol))
  6634. (when (looking-at org-complex-heading-regexp)
  6635. (setq txt (match-string 4)
  6636. re (concat "^" (regexp-quote
  6637. (buffer-substring (match-beginning 1)
  6638. (match-end 4)))))
  6639. (if (match-end 5) (setq re (concat re "[ \t]+"
  6640. (regexp-quote
  6641. (match-string 5)))))
  6642. (setq re (concat re "[ \t]*$"))
  6643. (when org-refile-use-outline-path
  6644. (setq txt (mapconcat 'identity
  6645. (append
  6646. (if (eq org-refile-use-outline-path 'file)
  6647. (list (file-name-nondirectory
  6648. (buffer-file-name (buffer-base-buffer))))
  6649. (if (eq org-refile-use-outline-path 'full-file-path)
  6650. (list (buffer-file-name (buffer-base-buffer)))))
  6651. (org-get-outline-path)
  6652. (list txt))
  6653. "/")))
  6654. (push (list txt f re (point)) targets))
  6655. (goto-char (point-at-eol))))))))
  6656. (nreverse targets))))
  6657. (defun org-get-outline-path ()
  6658. "Return the outline path to the current entry, as a list."
  6659. (let (rtn)
  6660. (save-excursion
  6661. (while (org-up-heading-safe)
  6662. (when (looking-at org-complex-heading-regexp)
  6663. (push (org-match-string-no-properties 4) rtn)))
  6664. rtn)))
  6665. (defvar org-refile-history nil
  6666. "History for refiling operations.")
  6667. (defun org-refile (&optional goto default-buffer)
  6668. "Move the entry at point to another heading.
  6669. The list of target headings is compiled using the information in
  6670. `org-refile-targets', which see. This list is created before each use
  6671. and will therefore always be up-to-date.
  6672. At the target location, the entry is filed as a subitem of the target heading.
  6673. Depending on `org-reverse-note-order', the new subitem will either be the
  6674. first of the last subitem.
  6675. With prefix arg GOTO, the command will only visit the target location,
  6676. not actually move anything.
  6677. With a double prefix `C-c C-c', go to the location where the last refiling
  6678. operation has put the subtree."
  6679. (interactive "P")
  6680. (let* ((cbuf (current-buffer))
  6681. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6682. pos it nbuf file re level reversed)
  6683. (if (equal goto '(16))
  6684. (org-refile-goto-last-stored)
  6685. (when (setq it (org-refile-get-location
  6686. (if goto "Goto: " "Refile to: ") default-buffer))
  6687. (setq file (nth 1 it)
  6688. re (nth 2 it)
  6689. pos (nth 3 it))
  6690. (setq nbuf (or (find-buffer-visiting file)
  6691. (find-file-noselect file)))
  6692. (if goto
  6693. (progn
  6694. (switch-to-buffer nbuf)
  6695. (goto-char pos)
  6696. (org-show-context 'org-goto))
  6697. (org-copy-subtree 1 nil t)
  6698. (save-excursion
  6699. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6700. (find-file-noselect file))))
  6701. (setq reversed (org-notes-order-reversed-p))
  6702. (save-excursion
  6703. (save-restriction
  6704. (widen)
  6705. (goto-char pos)
  6706. (looking-at outline-regexp)
  6707. (setq level (org-get-valid-level (funcall outline-level) 1))
  6708. (goto-char
  6709. (if reversed
  6710. (outline-next-heading)
  6711. (or (save-excursion (outline-get-next-sibling))
  6712. (org-end-of-subtree t t)
  6713. (point-max))))
  6714. (bookmark-set "org-refile-last-stored")
  6715. (org-paste-subtree level))))
  6716. (org-cut-subtree)
  6717. (setq org-markers-to-move nil)
  6718. (message "Entry refiled to \"%s\"" (car it)))))))
  6719. (defun org-refile-goto-last-stored ()
  6720. "Go to the location where the last refile was stored."
  6721. (interactive)
  6722. (bookmark-jump "org-refile-last-stored")
  6723. (message "This is the location of the last refile"))
  6724. (defun org-refile-get-location (&optional prompt default-buffer)
  6725. "Prompt the user for a refile location, using PROMPT."
  6726. (let ((org-refile-targets org-refile-targets)
  6727. (org-refile-use-outline-path org-refile-use-outline-path))
  6728. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6729. (unless org-refile-target-table
  6730. (error "No refile targets"))
  6731. (let* ((cbuf (current-buffer))
  6732. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6733. (fname (and filename (file-truename filename)))
  6734. (tbl (mapcar
  6735. (lambda (x)
  6736. (if (not (equal fname (file-truename (nth 1 x))))
  6737. (cons (concat (car x) " (" (file-name-nondirectory
  6738. (nth 1 x)) ")")
  6739. (cdr x))
  6740. x))
  6741. org-refile-target-table))
  6742. (completion-ignore-case t))
  6743. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  6744. tbl)))
  6745. ;;;; Dynamic blocks
  6746. (defun org-find-dblock (name)
  6747. "Find the first dynamic block with name NAME in the buffer.
  6748. If not found, stay at current position and return nil."
  6749. (let (pos)
  6750. (save-excursion
  6751. (goto-char (point-min))
  6752. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6753. nil t)
  6754. (match-beginning 0))))
  6755. (if pos (goto-char pos))
  6756. pos))
  6757. (defconst org-dblock-start-re
  6758. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6759. "Matches the startline of a dynamic block, with parameters.")
  6760. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6761. "Matches the end of a dyhamic block.")
  6762. (defun org-create-dblock (plist)
  6763. "Create a dynamic block section, with parameters taken from PLIST.
  6764. PLIST must containe a :name entry which is used as name of the block."
  6765. (unless (bolp) (newline))
  6766. (let ((name (plist-get plist :name)))
  6767. (insert "#+BEGIN: " name)
  6768. (while plist
  6769. (if (eq (car plist) :name)
  6770. (setq plist (cddr plist))
  6771. (insert " " (prin1-to-string (pop plist)))))
  6772. (insert "\n\n#+END:\n")
  6773. (beginning-of-line -2)))
  6774. (defun org-prepare-dblock ()
  6775. "Prepare dynamic block for refresh.
  6776. This empties the block, puts the cursor at the insert position and returns
  6777. the property list including an extra property :name with the block name."
  6778. (unless (looking-at org-dblock-start-re)
  6779. (error "Not at a dynamic block"))
  6780. (let* ((begdel (1+ (match-end 0)))
  6781. (name (org-no-properties (match-string 1)))
  6782. (params (append (list :name name)
  6783. (read (concat "(" (match-string 3) ")")))))
  6784. (unless (re-search-forward org-dblock-end-re nil t)
  6785. (error "Dynamic block not terminated"))
  6786. (setq params
  6787. (append params
  6788. (list :content (buffer-substring
  6789. begdel (match-beginning 0)))))
  6790. (delete-region begdel (match-beginning 0))
  6791. (goto-char begdel)
  6792. (open-line 1)
  6793. params))
  6794. (defun org-map-dblocks (&optional command)
  6795. "Apply COMMAND to all dynamic blocks in the current buffer.
  6796. If COMMAND is not given, use `org-update-dblock'."
  6797. (let ((cmd (or command 'org-update-dblock))
  6798. pos)
  6799. (save-excursion
  6800. (goto-char (point-min))
  6801. (while (re-search-forward org-dblock-start-re nil t)
  6802. (goto-char (setq pos (match-beginning 0)))
  6803. (condition-case nil
  6804. (funcall cmd)
  6805. (error (message "Error during update of dynamic block")))
  6806. (goto-char pos)
  6807. (unless (re-search-forward org-dblock-end-re nil t)
  6808. (error "Dynamic block not terminated"))))))
  6809. (defun org-dblock-update (&optional arg)
  6810. "User command for updating dynamic blocks.
  6811. Update the dynamic block at point. With prefix ARG, update all dynamic
  6812. blocks in the buffer."
  6813. (interactive "P")
  6814. (if arg
  6815. (org-update-all-dblocks)
  6816. (or (looking-at org-dblock-start-re)
  6817. (org-beginning-of-dblock))
  6818. (org-update-dblock)))
  6819. (defun org-update-dblock ()
  6820. "Update the dynamic block at point
  6821. This means to empty the block, parse for parameters and then call
  6822. the correct writing function."
  6823. (save-window-excursion
  6824. (let* ((pos (point))
  6825. (line (org-current-line))
  6826. (params (org-prepare-dblock))
  6827. (name (plist-get params :name))
  6828. (cmd (intern (concat "org-dblock-write:" name))))
  6829. (message "Updating dynamic block `%s' at line %d..." name line)
  6830. (funcall cmd params)
  6831. (message "Updating dynamic block `%s' at line %d...done" name line)
  6832. (goto-char pos))))
  6833. (defun org-beginning-of-dblock ()
  6834. "Find the beginning of the dynamic block at point.
  6835. Error if there is no scuh block at point."
  6836. (let ((pos (point))
  6837. beg)
  6838. (end-of-line 1)
  6839. (if (and (re-search-backward org-dblock-start-re nil t)
  6840. (setq beg (match-beginning 0))
  6841. (re-search-forward org-dblock-end-re nil t)
  6842. (> (match-end 0) pos))
  6843. (goto-char beg)
  6844. (goto-char pos)
  6845. (error "Not in a dynamic block"))))
  6846. (defun org-update-all-dblocks ()
  6847. "Update all dynamic blocks in the buffer.
  6848. This function can be used in a hook."
  6849. (when (org-mode-p)
  6850. (org-map-dblocks 'org-update-dblock)))
  6851. ;;;; Completion
  6852. (defconst org-additional-option-like-keywords
  6853. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6854. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6855. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  6856. (defun org-complete (&optional arg)
  6857. "Perform completion on word at point.
  6858. At the beginning of a headline, this completes TODO keywords as given in
  6859. `org-todo-keywords'.
  6860. If the current word is preceded by a backslash, completes the TeX symbols
  6861. that are supported for HTML support.
  6862. If the current word is preceded by \"#+\", completes special words for
  6863. setting file options.
  6864. In the line after \"#+STARTUP:, complete valid keywords.\"
  6865. At all other locations, this simply calls the value of
  6866. `org-completion-fallback-command'."
  6867. (interactive "P")
  6868. (org-without-partial-completion
  6869. (catch 'exit
  6870. (let* ((end (point))
  6871. (beg1 (save-excursion
  6872. (skip-chars-backward (org-re "[:alnum:]_@"))
  6873. (point)))
  6874. (beg (save-excursion
  6875. (skip-chars-backward "a-zA-Z0-9_:$")
  6876. (point)))
  6877. (confirm (lambda (x) (stringp (car x))))
  6878. (searchhead (equal (char-before beg) ?*))
  6879. (tag (and (equal (char-before beg1) ?:)
  6880. (equal (char-after (point-at-bol)) ?*)))
  6881. (prop (and (equal (char-before beg1) ?:)
  6882. (not (equal (char-after (point-at-bol)) ?*))))
  6883. (texp (equal (char-before beg) ?\\))
  6884. (link (equal (char-before beg) ?\[))
  6885. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  6886. beg)
  6887. "#+"))
  6888. (startup (string-match "^#\\+STARTUP:.*"
  6889. (buffer-substring (point-at-bol) (point))))
  6890. (completion-ignore-case opt)
  6891. (type nil)
  6892. (tbl nil)
  6893. (table (cond
  6894. (opt
  6895. (setq type :opt)
  6896. (require 'org-exp)
  6897. (append
  6898. (mapcar
  6899. (lambda (x)
  6900. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  6901. (cons (match-string 2 x) (match-string 1 x)))
  6902. (org-split-string (org-get-current-options) "\n"))
  6903. (mapcar 'list org-additional-option-like-keywords)))
  6904. (startup
  6905. (setq type :startup)
  6906. org-startup-options)
  6907. (link (append org-link-abbrev-alist-local
  6908. org-link-abbrev-alist))
  6909. (texp
  6910. (setq type :tex)
  6911. org-html-entities)
  6912. ((string-match "\\`\\*+[ \t]+\\'"
  6913. (buffer-substring (point-at-bol) beg))
  6914. (setq type :todo)
  6915. (mapcar 'list org-todo-keywords-1))
  6916. (searchhead
  6917. (setq type :searchhead)
  6918. (save-excursion
  6919. (goto-char (point-min))
  6920. (while (re-search-forward org-todo-line-regexp nil t)
  6921. (push (list
  6922. (org-make-org-heading-search-string
  6923. (match-string 3) t))
  6924. tbl)))
  6925. tbl)
  6926. (tag (setq type :tag beg beg1)
  6927. (or org-tag-alist (org-get-buffer-tags)))
  6928. (prop (setq type :prop beg beg1)
  6929. (mapcar 'list (org-buffer-property-keys nil t t)))
  6930. (t (progn
  6931. (call-interactively org-completion-fallback-command)
  6932. (throw 'exit nil)))))
  6933. (pattern (buffer-substring-no-properties beg end))
  6934. (completion (try-completion pattern table confirm)))
  6935. (cond ((eq completion t)
  6936. (if (not (assoc (upcase pattern) table))
  6937. (message "Already complete")
  6938. (if (and (equal type :opt)
  6939. (not (member (car (assoc (upcase pattern) table))
  6940. org-additional-option-like-keywords)))
  6941. (insert (substring (cdr (assoc (upcase pattern) table))
  6942. (length pattern)))
  6943. (if (memq type '(:tag :prop)) (insert ":")))))
  6944. ((null completion)
  6945. (message "Can't find completion for \"%s\"" pattern)
  6946. (ding))
  6947. ((not (string= pattern completion))
  6948. (delete-region beg end)
  6949. (if (string-match " +$" completion)
  6950. (setq completion (replace-match "" t t completion)))
  6951. (insert completion)
  6952. (if (get-buffer-window "*Completions*")
  6953. (delete-window (get-buffer-window "*Completions*")))
  6954. (if (assoc completion table)
  6955. (if (eq type :todo) (insert " ")
  6956. (if (memq type '(:tag :prop)) (insert ":"))))
  6957. (if (and (equal type :opt) (assoc completion table))
  6958. (message "%s" (substitute-command-keys
  6959. "Press \\[org-complete] again to insert example settings"))))
  6960. (t
  6961. (message "Making completion list...")
  6962. (let ((list (sort (all-completions pattern table confirm)
  6963. 'string<)))
  6964. (with-output-to-temp-buffer "*Completions*"
  6965. (condition-case nil
  6966. ;; Protection needed for XEmacs and emacs 21
  6967. (display-completion-list list pattern)
  6968. (error (display-completion-list list)))))
  6969. (message "Making completion list...%s" "done")))))))
  6970. ;;;; TODO, DEADLINE, Comments
  6971. (defun org-toggle-comment ()
  6972. "Change the COMMENT state of an entry."
  6973. (interactive)
  6974. (save-excursion
  6975. (org-back-to-heading)
  6976. (let (case-fold-search)
  6977. (if (looking-at (concat outline-regexp
  6978. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  6979. (replace-match "" t t nil 1)
  6980. (if (looking-at outline-regexp)
  6981. (progn
  6982. (goto-char (match-end 0))
  6983. (insert org-comment-string " ")))))))
  6984. (defvar org-last-todo-state-is-todo nil
  6985. "This is non-nil when the last TODO state change led to a TODO state.
  6986. If the last change removed the TODO tag or switched to DONE, then
  6987. this is nil.")
  6988. (defvar org-setting-tags nil) ; dynamically skiped
  6989. (defun org-parse-local-options (string var)
  6990. "Parse STRING for startup setting relevant for variable VAR."
  6991. (let ((rtn (symbol-value var))
  6992. e opts)
  6993. (save-match-data
  6994. (if (or (not string) (not (string-match "\\S-" string)))
  6995. rtn
  6996. (setq opts (delq nil (mapcar (lambda (x)
  6997. (setq e (assoc x org-startup-options))
  6998. (if (eq (nth 1 e) var) e nil))
  6999. (org-split-string string "[ \t]+"))))
  7000. (if (not opts)
  7001. rtn
  7002. (setq rtn nil)
  7003. (while (setq e (pop opts))
  7004. (if (not (nth 3 e))
  7005. (setq rtn (nth 2 e))
  7006. (if (not (listp rtn)) (setq rtn nil))
  7007. (push (nth 2 e) rtn)))
  7008. rtn)))))
  7009. (defvar org-blocker-hook nil
  7010. "Hook for functions that are allowed to block a state change.
  7011. Each function gets as its single argument a property list, see
  7012. `org-trigger-hook' for more information about this list.
  7013. If any of the functions in this hook returns nil, the state change
  7014. is blocked.")
  7015. (defvar org-trigger-hook nil
  7016. "Hook for functions that are triggered by a state change.
  7017. Each function gets as its single argument a property list with at least
  7018. the following elements:
  7019. (:type type-of-change :position pos-at-entry-start
  7020. :from old-state :to new-state)
  7021. Depending on the type, more properties may be present.
  7022. This mechanism is currently implemented for:
  7023. TODO state changes
  7024. ------------------
  7025. :type todo-state-change
  7026. :from previous state (keyword as a string), or nil
  7027. :to new state (keyword as a string), or nil")
  7028. (defun org-todo (&optional arg)
  7029. "Change the TODO state of an item.
  7030. The state of an item is given by a keyword at the start of the heading,
  7031. like
  7032. *** TODO Write paper
  7033. *** DONE Call mom
  7034. The different keywords are specified in the variable `org-todo-keywords'.
  7035. By default the available states are \"TODO\" and \"DONE\".
  7036. So for this example: when the item starts with TODO, it is changed to DONE.
  7037. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7038. DONE are present, add TODO at the beginning of the heading.
  7039. With C-u prefix arg, use completion to determine the new state.
  7040. With numeric prefix arg, switch to that state.
  7041. For calling through lisp, arg is also interpreted in the following way:
  7042. 'none -> empty state
  7043. \"\"(empty string) -> switch to empty state
  7044. 'done -> switch to DONE
  7045. 'nextset -> switch to the next set of keywords
  7046. 'previousset -> switch to the previous set of keywords
  7047. \"WAITING\" -> switch to the specified keyword, but only if it
  7048. really is a member of `org-todo-keywords'."
  7049. (interactive "P")
  7050. (save-excursion
  7051. (catch 'exit
  7052. (org-back-to-heading)
  7053. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7054. (or (looking-at (concat " +" org-todo-regexp " *"))
  7055. (looking-at " *"))
  7056. (let* ((match-data (match-data))
  7057. (startpos (point-at-bol))
  7058. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7059. (org-log-done org-log-done)
  7060. (org-log-repeat org-log-repeat)
  7061. (org-todo-log-states org-todo-log-states)
  7062. (this (match-string 1))
  7063. (hl-pos (match-beginning 0))
  7064. (head (org-get-todo-sequence-head this))
  7065. (ass (assoc head org-todo-kwd-alist))
  7066. (interpret (nth 1 ass))
  7067. (done-word (nth 3 ass))
  7068. (final-done-word (nth 4 ass))
  7069. (last-state (or this ""))
  7070. (completion-ignore-case t)
  7071. (member (member this org-todo-keywords-1))
  7072. (tail (cdr member))
  7073. (state (cond
  7074. ((and org-todo-key-trigger
  7075. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7076. (and (not arg) org-use-fast-todo-selection
  7077. (not (eq org-use-fast-todo-selection 'prefix)))))
  7078. ;; Use fast selection
  7079. (org-fast-todo-selection))
  7080. ((and (equal arg '(4))
  7081. (or (not org-use-fast-todo-selection)
  7082. (not org-todo-key-trigger)))
  7083. ;; Read a state with completion
  7084. (completing-read "State: " (mapcar (lambda(x) (list x))
  7085. org-todo-keywords-1)
  7086. nil t))
  7087. ((eq arg 'right)
  7088. (if this
  7089. (if tail (car tail) nil)
  7090. (car org-todo-keywords-1)))
  7091. ((eq arg 'left)
  7092. (if (equal member org-todo-keywords-1)
  7093. nil
  7094. (if this
  7095. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7096. org-todo-keywords-1)
  7097. (org-last org-todo-keywords-1))))
  7098. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7099. (setq arg nil))) ; hack to fall back to cycling
  7100. (arg
  7101. ;; user or caller requests a specific state
  7102. (cond
  7103. ((equal arg "") nil)
  7104. ((eq arg 'none) nil)
  7105. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7106. ((eq arg 'nextset)
  7107. (or (car (cdr (member head org-todo-heads)))
  7108. (car org-todo-heads)))
  7109. ((eq arg 'previousset)
  7110. (let ((org-todo-heads (reverse org-todo-heads)))
  7111. (or (car (cdr (member head org-todo-heads)))
  7112. (car org-todo-heads))))
  7113. ((car (member arg org-todo-keywords-1)))
  7114. ((nth (1- (prefix-numeric-value arg))
  7115. org-todo-keywords-1))))
  7116. ((null member) (or head (car org-todo-keywords-1)))
  7117. ((equal this final-done-word) nil) ;; -> make empty
  7118. ((null tail) nil) ;; -> first entry
  7119. ((eq interpret 'sequence)
  7120. (car tail))
  7121. ((memq interpret '(type priority))
  7122. (if (eq this-command last-command)
  7123. (car tail)
  7124. (if (> (length tail) 0)
  7125. (or done-word (car org-done-keywords))
  7126. nil)))
  7127. (t nil)))
  7128. (next (if state (concat " " state " ") " "))
  7129. (change-plist (list :type 'todo-state-change :from this :to state
  7130. :position startpos))
  7131. dolog now-done-p)
  7132. (when org-blocker-hook
  7133. (unless (save-excursion
  7134. (save-match-data
  7135. (run-hook-with-args-until-failure
  7136. 'org-blocker-hook change-plist)))
  7137. (if (interactive-p)
  7138. (error "TODO state change from %s to %s blocked" this state)
  7139. ;; fail silently
  7140. (message "TODO state change from %s to %s blocked" this state)
  7141. (throw 'exit nil))))
  7142. (store-match-data match-data)
  7143. (replace-match next t t)
  7144. (unless (pos-visible-in-window-p hl-pos)
  7145. (message "TODO state changed to %s" (org-trim next)))
  7146. (unless head
  7147. (setq head (org-get-todo-sequence-head state)
  7148. ass (assoc head org-todo-kwd-alist)
  7149. interpret (nth 1 ass)
  7150. done-word (nth 3 ass)
  7151. final-done-word (nth 4 ass)))
  7152. (when (memq arg '(nextset previousset))
  7153. (message "Keyword-Set %d/%d: %s"
  7154. (- (length org-todo-sets) -1
  7155. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7156. (length org-todo-sets)
  7157. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7158. (setq org-last-todo-state-is-todo
  7159. (not (member state org-done-keywords)))
  7160. (setq now-done-p (and (member state org-done-keywords)
  7161. (not (member this org-done-keywords))))
  7162. (and logging (org-local-logging logging))
  7163. (when (and (or org-todo-log-states org-log-done)
  7164. (not (memq arg '(nextset previousset))))
  7165. ;; we need to look at recording a time and note
  7166. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7167. (nth 2 (assoc this org-todo-log-states))))
  7168. (when (and state
  7169. (member state org-not-done-keywords)
  7170. (not (member this org-not-done-keywords)))
  7171. ;; This is now a todo state and was not one before
  7172. ;; If there was a CLOSED time stamp, get rid of it.
  7173. (org-add-planning-info nil nil 'closed))
  7174. (when (and now-done-p org-log-done)
  7175. ;; It is now done, and it was not done before
  7176. (org-add-planning-info 'closed (org-current-time))
  7177. (if (and (not dolog) (eq 'note org-log-done))
  7178. (org-add-log-setup 'done state 'findpos 'note)))
  7179. (when (and state dolog)
  7180. ;; This is a non-nil state, and we need to log it
  7181. (org-add-log-setup 'state state 'findpos dolog)))
  7182. ;; Fixup tag positioning
  7183. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7184. (run-hooks 'org-after-todo-state-change-hook)
  7185. (if (and arg (not (member state org-done-keywords)))
  7186. (setq head (org-get-todo-sequence-head state)))
  7187. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7188. ;; Do we need to trigger a repeat?
  7189. (when now-done-p (org-auto-repeat-maybe state))
  7190. ;; Fixup cursor location if close to the keyword
  7191. (if (and (outline-on-heading-p)
  7192. (not (bolp))
  7193. (save-excursion (beginning-of-line 1)
  7194. (looking-at org-todo-line-regexp))
  7195. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7196. (progn
  7197. (goto-char (or (match-end 2) (match-end 1)))
  7198. (just-one-space)))
  7199. (when org-trigger-hook
  7200. (save-excursion
  7201. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7202. (defun org-local-logging (value)
  7203. "Get logging settings from a property VALUE."
  7204. (let* (words w a)
  7205. ;; directly set the variables, they are already local.
  7206. (setq org-log-done nil
  7207. org-log-repeat nil
  7208. org-todo-log-states nil)
  7209. (setq words (org-split-string value))
  7210. (while (setq w (pop words))
  7211. (cond
  7212. ((setq a (assoc w org-startup-options))
  7213. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7214. (set (nth 1 a) (nth 2 a))))
  7215. ((setq a (org-extract-log-state-settings w))
  7216. (and (member (car a) org-todo-keywords-1)
  7217. (push a org-todo-log-states)))))))
  7218. (defun org-get-todo-sequence-head (kwd)
  7219. "Return the head of the TODO sequence to which KWD belongs.
  7220. If KWD is not set, check if there is a text property remembering the
  7221. right sequence."
  7222. (let (p)
  7223. (cond
  7224. ((not kwd)
  7225. (or (get-text-property (point-at-bol) 'org-todo-head)
  7226. (progn
  7227. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7228. nil (point-at-eol)))
  7229. (get-text-property p 'org-todo-head))))
  7230. ((not (member kwd org-todo-keywords-1))
  7231. (car org-todo-keywords-1))
  7232. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7233. (defun org-fast-todo-selection ()
  7234. "Fast TODO keyword selection with single keys.
  7235. Returns the new TODO keyword, or nil if no state change should occur."
  7236. (let* ((fulltable org-todo-key-alist)
  7237. (done-keywords org-done-keywords) ;; needed for the faces.
  7238. (maxlen (apply 'max (mapcar
  7239. (lambda (x)
  7240. (if (stringp (car x)) (string-width (car x)) 0))
  7241. fulltable)))
  7242. (expert nil)
  7243. (fwidth (+ maxlen 3 1 3))
  7244. (ncol (/ (- (window-width) 4) fwidth))
  7245. tg cnt e c tbl
  7246. groups ingroup)
  7247. (save-window-excursion
  7248. (if expert
  7249. (set-buffer (get-buffer-create " *Org todo*"))
  7250. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7251. (erase-buffer)
  7252. (org-set-local 'org-done-keywords done-keywords)
  7253. (setq tbl fulltable cnt 0)
  7254. (while (setq e (pop tbl))
  7255. (cond
  7256. ((equal e '(:startgroup))
  7257. (push '() groups) (setq ingroup t)
  7258. (when (not (= cnt 0))
  7259. (setq cnt 0)
  7260. (insert "\n"))
  7261. (insert "{ "))
  7262. ((equal e '(:endgroup))
  7263. (setq ingroup nil cnt 0)
  7264. (insert "}\n"))
  7265. (t
  7266. (setq tg (car e) c (cdr e))
  7267. (if ingroup (push tg (car groups)))
  7268. (setq tg (org-add-props tg nil 'face
  7269. (org-get-todo-face tg)))
  7270. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7271. (insert "[" c "] " tg (make-string
  7272. (- fwidth 4 (length tg)) ?\ ))
  7273. (when (= (setq cnt (1+ cnt)) ncol)
  7274. (insert "\n")
  7275. (if ingroup (insert " "))
  7276. (setq cnt 0)))))
  7277. (insert "\n")
  7278. (goto-char (point-min))
  7279. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7280. (fit-window-to-buffer))
  7281. (message "[a-z..]:Set [SPC]:clear")
  7282. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7283. (cond
  7284. ((or (= c ?\C-g)
  7285. (and (= c ?q) (not (rassoc c fulltable))))
  7286. (setq quit-flag t))
  7287. ((= c ?\ ) nil)
  7288. ((setq e (rassoc c fulltable) tg (car e))
  7289. tg)
  7290. (t (setq quit-flag t))))))
  7291. (defun org-entry-is-todo-p ()
  7292. (member (org-get-todo-state) org-not-done-keywords))
  7293. (defun org-entry-is-done-p ()
  7294. (member (org-get-todo-state) org-done-keywords))
  7295. (defun org-get-todo-state ()
  7296. (save-excursion
  7297. (org-back-to-heading t)
  7298. (and (looking-at org-todo-line-regexp)
  7299. (match-end 2)
  7300. (match-string 2))))
  7301. (defun org-at-date-range-p (&optional inactive-ok)
  7302. "Is the cursor inside a date range?"
  7303. (interactive)
  7304. (save-excursion
  7305. (catch 'exit
  7306. (let ((pos (point)))
  7307. (skip-chars-backward "^[<\r\n")
  7308. (skip-chars-backward "<[")
  7309. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7310. (>= (match-end 0) pos)
  7311. (throw 'exit t))
  7312. (skip-chars-backward "^<[\r\n")
  7313. (skip-chars-backward "<[")
  7314. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7315. (>= (match-end 0) pos)
  7316. (throw 'exit t)))
  7317. nil)))
  7318. (defun org-get-repeat ()
  7319. "Check if tere is a deadline/schedule with repeater in this entry."
  7320. (save-match-data
  7321. (save-excursion
  7322. (org-back-to-heading t)
  7323. (if (re-search-forward
  7324. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7325. (match-string 1)))))
  7326. (defvar org-last-changed-timestamp)
  7327. (defvar org-log-post-message)
  7328. (defvar org-log-note-purpose)
  7329. (defvar org-log-note-how)
  7330. (defun org-auto-repeat-maybe (done-word)
  7331. "Check if the current headline contains a repeated deadline/schedule.
  7332. If yes, set TODO state back to what it was and change the base date
  7333. of repeating deadline/scheduled time stamps to new date.
  7334. This function is run automatically after each state change to a DONE state."
  7335. ;; last-state is dynamically scoped into this function
  7336. (let* ((repeat (org-get-repeat))
  7337. (aa (assoc last-state org-todo-kwd-alist))
  7338. (interpret (nth 1 aa))
  7339. (head (nth 2 aa))
  7340. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7341. (msg "Entry repeats: ")
  7342. (org-log-done nil)
  7343. (org-todo-log-states nil)
  7344. (nshiftmax 10) (nshift 0)
  7345. re type n what ts mb0 time)
  7346. (when repeat
  7347. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7348. (org-todo (if (eq interpret 'type) last-state head))
  7349. (when org-log-repeat
  7350. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7351. (memq 'org-add-log-note post-command-hook))
  7352. ;; OK, we are already setup for some record
  7353. (if (eq org-log-repeat 'note)
  7354. ;; make sure we take a note, not only a time stamp
  7355. (setq org-log-note-how 'note))
  7356. ;; Set up for taking a record
  7357. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7358. 'findpos org-log-repeat)))
  7359. (org-back-to-heading t)
  7360. (org-add-planning-info nil nil 'closed)
  7361. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7362. org-deadline-time-regexp "\\)\\|\\("
  7363. org-ts-regexp "\\)"))
  7364. (while (re-search-forward
  7365. re (save-excursion (outline-next-heading) (point)) t)
  7366. (setq type (if (match-end 1) org-scheduled-string
  7367. (if (match-end 3) org-deadline-string "Plain:"))
  7368. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7369. mb0 (match-beginning 0))
  7370. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7371. (setq n (string-to-number (match-string 2 ts))
  7372. what (match-string 3 ts))
  7373. (if (equal what "w") (setq n (* n 7) what "d"))
  7374. ;; Preparation, see if we need to modify the start date for the change
  7375. (when (match-end 1)
  7376. (setq time (save-match-data (org-time-string-to-time ts)))
  7377. (cond
  7378. ((equal (match-string 1 ts) ".")
  7379. ;; Shift starting date to today
  7380. (org-timestamp-change
  7381. (- (time-to-days (current-time)) (time-to-days time))
  7382. 'day))
  7383. ((equal (match-string 1 ts) "+")
  7384. (while (or (= nshift 0)
  7385. (<= (time-to-days time) (time-to-days (current-time))))
  7386. (when (= (incf nshift) nshiftmax)
  7387. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7388. (error "Abort")))
  7389. (org-timestamp-change n (cdr (assoc what whata)))
  7390. (org-at-timestamp-p t)
  7391. (setq ts (match-string 1))
  7392. (setq time (save-match-data (org-time-string-to-time ts))))
  7393. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7394. ;; rematch, so that we have everything in place for the real shift
  7395. (org-at-timestamp-p t)
  7396. (setq ts (match-string 1))
  7397. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7398. (org-timestamp-change n (cdr (assoc what whata)))
  7399. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7400. (setq org-log-post-message msg)
  7401. (message "%s" msg))))
  7402. (defun org-show-todo-tree (arg)
  7403. "Make a compact tree which shows all headlines marked with TODO.
  7404. The tree will show the lines where the regexp matches, and all higher
  7405. headlines above the match.
  7406. With a \\[universal-argument] prefix, also show the DONE entries.
  7407. With a numeric prefix N, construct a sparse tree for the Nth element
  7408. of `org-todo-keywords-1'."
  7409. (interactive "P")
  7410. (let ((case-fold-search nil)
  7411. (kwd-re
  7412. (cond ((null arg) org-not-done-regexp)
  7413. ((equal arg '(4))
  7414. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7415. (mapcar 'list org-todo-keywords-1))))
  7416. (concat "\\("
  7417. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7418. "\\)\\>")))
  7419. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7420. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7421. org-todo-keywords-1)))
  7422. (t (error "Invalid prefix argument: %s" arg)))))
  7423. (message "%d TODO entries found"
  7424. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7425. (defun org-deadline (&optional remove)
  7426. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7427. With argument REMOVE, remove any deadline from the item."
  7428. (interactive "P")
  7429. (if remove
  7430. (progn
  7431. (org-remove-timestamp-with-keyword org-deadline-string)
  7432. (message "Item no longer has a deadline."))
  7433. (org-add-planning-info 'deadline nil 'closed)))
  7434. (defun org-schedule (&optional remove)
  7435. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7436. With argument REMOVE, remove any scheduling date from the item."
  7437. (interactive "P")
  7438. (if remove
  7439. (progn
  7440. (org-remove-timestamp-with-keyword org-scheduled-string)
  7441. (message "Item is no longer scheduled."))
  7442. (org-add-planning-info 'scheduled nil 'closed)))
  7443. (defun org-remove-timestamp-with-keyword (keyword)
  7444. "Remove all time stamps with KEYWORD in the current entry."
  7445. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7446. beg)
  7447. (save-excursion
  7448. (org-back-to-heading t)
  7449. (setq beg (point))
  7450. (org-end-of-subtree t t)
  7451. (while (re-search-backward re beg t)
  7452. (replace-match "")
  7453. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7454. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  7455. (defun org-add-planning-info (what &optional time &rest remove)
  7456. "Insert new timestamp with keyword in the line directly after the headline.
  7457. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7458. If non is given, the user is prompted for a date.
  7459. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7460. be removed."
  7461. (interactive)
  7462. (let (org-time-was-given org-end-time-was-given ts
  7463. end default-time default-input)
  7464. (when (and (not time) (memq what '(scheduled deadline)))
  7465. ;; Try to get a default date/time from existing timestamp
  7466. (save-excursion
  7467. (org-back-to-heading t)
  7468. (setq end (save-excursion (outline-next-heading) (point)))
  7469. (when (re-search-forward (if (eq what 'scheduled)
  7470. org-scheduled-time-regexp
  7471. org-deadline-time-regexp)
  7472. end t)
  7473. (setq ts (match-string 1)
  7474. default-time
  7475. (apply 'encode-time (org-parse-time-string ts))
  7476. default-input (and ts (org-get-compact-tod ts))))))
  7477. (when what
  7478. ;; If necessary, get the time from the user
  7479. (setq time (or time (org-read-date nil 'to-time nil nil
  7480. default-time default-input))))
  7481. (when (and org-insert-labeled-timestamps-at-point
  7482. (member what '(scheduled deadline)))
  7483. (insert
  7484. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7485. (org-insert-time-stamp time org-time-was-given
  7486. nil nil nil (list org-end-time-was-given))
  7487. (setq what nil))
  7488. (save-excursion
  7489. (save-restriction
  7490. (let (col list elt ts buffer-invisibility-spec)
  7491. (org-back-to-heading t)
  7492. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7493. (goto-char (match-end 1))
  7494. (setq col (current-column))
  7495. (goto-char (match-end 0))
  7496. (if (eobp) (insert "\n") (forward-char 1))
  7497. (if (and (not (looking-at outline-regexp))
  7498. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7499. "[^\r\n]*"))
  7500. (not (equal (match-string 1) org-clock-string)))
  7501. (narrow-to-region (match-beginning 0) (match-end 0))
  7502. (insert-before-markers "\n")
  7503. (backward-char 1)
  7504. (narrow-to-region (point) (point))
  7505. (org-indent-to-column col))
  7506. ;; Check if we have to remove something.
  7507. (setq list (cons what remove))
  7508. (while list
  7509. (setq elt (pop list))
  7510. (goto-char (point-min))
  7511. (when (or (and (eq elt 'scheduled)
  7512. (re-search-forward org-scheduled-time-regexp nil t))
  7513. (and (eq elt 'deadline)
  7514. (re-search-forward org-deadline-time-regexp nil t))
  7515. (and (eq elt 'closed)
  7516. (re-search-forward org-closed-time-regexp nil t)))
  7517. (replace-match "")
  7518. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7519. (if (looking-at " +") (replace-match ""))))
  7520. (goto-char (point-max))
  7521. (when what
  7522. (insert
  7523. (if (not (equal (char-before) ?\ )) " " "")
  7524. (cond ((eq what 'scheduled) org-scheduled-string)
  7525. ((eq what 'deadline) org-deadline-string)
  7526. ((eq what 'closed) org-closed-string))
  7527. " ")
  7528. (setq ts (org-insert-time-stamp
  7529. time
  7530. (or org-time-was-given
  7531. (and (eq what 'closed) org-log-done-with-time))
  7532. (eq what 'closed)
  7533. nil nil (list org-end-time-was-given)))
  7534. (end-of-line 1))
  7535. (goto-char (point-min))
  7536. (widen)
  7537. (if (and (looking-at "[ \t]+\n")
  7538. (equal (char-before) ?\n))
  7539. (backward-delete-char 1))
  7540. ts)))))
  7541. (defvar org-log-note-marker (make-marker))
  7542. (defvar org-log-note-purpose nil)
  7543. (defvar org-log-note-state nil)
  7544. (defvar org-log-note-how nil)
  7545. (defvar org-log-note-window-configuration nil)
  7546. (defvar org-log-note-return-to (make-marker))
  7547. (defvar org-log-post-message nil
  7548. "Message to be displayed after a log note has been stored.
  7549. The auto-repeater uses this.")
  7550. (defun org-add-note ()
  7551. "Add a note to the current entry.
  7552. This is done in the same way as adding a state change note."
  7553. (interactive)
  7554. (org-add-log-setup 'note nil t nil))
  7555. (defun org-add-log-setup (&optional purpose state findpos how)
  7556. "Set up the post command hook to take a note.
  7557. If this is about to TODO state change, the new state is expected in STATE.
  7558. When FINDPOS is non-nil, find the correct position for the note in
  7559. the current entry. If not, assume that it can be inserted at point."
  7560. (save-excursion
  7561. (when findpos
  7562. (org-back-to-heading t)
  7563. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7564. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7565. "[^\r\n]*\\)?"))
  7566. (goto-char (match-end 0))
  7567. (unless org-log-states-order-reversed
  7568. (and (= (char-after) ?\n) (forward-char 1))
  7569. (org-skip-over-state-notes)
  7570. (skip-chars-backward " \t\n\r")))
  7571. (move-marker org-log-note-marker (point))
  7572. (setq org-log-note-purpose purpose
  7573. org-log-note-state state
  7574. org-log-note-how how)
  7575. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  7576. (defun org-skip-over-state-notes ()
  7577. "Skip past the list of State notes in an entry."
  7578. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7579. (while (looking-at "[ \t]*- State")
  7580. (condition-case nil
  7581. (org-next-item)
  7582. (error (org-end-of-item)))))
  7583. (defun org-add-log-note (&optional purpose)
  7584. "Pop up a window for taking a note, and add this note later at point."
  7585. (remove-hook 'post-command-hook 'org-add-log-note)
  7586. (setq org-log-note-window-configuration (current-window-configuration))
  7587. (delete-other-windows)
  7588. (move-marker org-log-note-return-to (point))
  7589. (switch-to-buffer (marker-buffer org-log-note-marker))
  7590. (goto-char org-log-note-marker)
  7591. (org-switch-to-buffer-other-window "*Org Note*")
  7592. (erase-buffer)
  7593. (if (memq org-log-note-how '(time state))
  7594. (org-store-log-note)
  7595. (let ((org-inhibit-startup t)) (org-mode))
  7596. (insert (format "# Insert note for %s.
  7597. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7598. (cond
  7599. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7600. ((eq org-log-note-purpose 'done) "closed todo item")
  7601. ((eq org-log-note-purpose 'state)
  7602. (format "state change to \"%s\"" org-log-note-state))
  7603. ((eq org-log-note-purpose 'note)
  7604. "this entry")
  7605. (t (error "This should not happen")))))
  7606. (org-set-local 'org-finish-function 'org-store-log-note)))
  7607. (defvar org-note-abort nil) ; dynamically scoped
  7608. (defun org-store-log-note ()
  7609. "Finish taking a log note, and insert it to where it belongs."
  7610. (let ((txt (buffer-string))
  7611. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7612. lines ind)
  7613. (kill-buffer (current-buffer))
  7614. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7615. (setq txt (replace-match "" t t txt)))
  7616. (if (string-match "\\s-+\\'" txt)
  7617. (setq txt (replace-match "" t t txt)))
  7618. (setq lines (org-split-string txt "\n"))
  7619. (when (and note (string-match "\\S-" note))
  7620. (setq note
  7621. (org-replace-escapes
  7622. note
  7623. (list (cons "%u" (user-login-name))
  7624. (cons "%U" user-full-name)
  7625. (cons "%t" (format-time-string
  7626. (org-time-stamp-format 'long 'inactive)
  7627. (current-time)))
  7628. (cons "%s" (if org-log-note-state
  7629. (concat "\"" org-log-note-state "\"")
  7630. "")))))
  7631. (if lines (setq note (concat note " \\\\")))
  7632. (push note lines))
  7633. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7634. (when lines
  7635. (save-excursion
  7636. (set-buffer (marker-buffer org-log-note-marker))
  7637. (save-excursion
  7638. (goto-char org-log-note-marker)
  7639. (move-marker org-log-note-marker nil)
  7640. (end-of-line 1)
  7641. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7642. (indent-relative nil)
  7643. (insert "- " (pop lines))
  7644. (org-indent-line-function)
  7645. (beginning-of-line 1)
  7646. (looking-at "[ \t]*")
  7647. (setq ind (concat (match-string 0) " "))
  7648. (end-of-line 1)
  7649. (while lines (insert "\n" ind (pop lines)))))))
  7650. (set-window-configuration org-log-note-window-configuration)
  7651. (with-current-buffer (marker-buffer org-log-note-return-to)
  7652. (goto-char org-log-note-return-to))
  7653. (move-marker org-log-note-return-to nil)
  7654. (and org-log-post-message (message "%s" org-log-post-message)))
  7655. (defun org-sparse-tree (&optional arg)
  7656. "Create a sparse tree, prompt for the details.
  7657. This command can create sparse trees. You first need to select the type
  7658. of match used to create the tree:
  7659. t Show entries with a specific TODO keyword.
  7660. T Show entries selected by a tags match.
  7661. p Enter a property name and its value (both with completion on existing
  7662. names/values) and show entries with that property.
  7663. r Show entries matching a regular expression
  7664. d Show deadlines due within `org-deadline-warning-days'."
  7665. (interactive "P")
  7666. (let (ans kwd value)
  7667. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7668. (setq ans (read-char-exclusive))
  7669. (cond
  7670. ((equal ans ?d)
  7671. (call-interactively 'org-check-deadlines))
  7672. ((equal ans ?b)
  7673. (call-interactively 'org-check-before-date))
  7674. ((equal ans ?t)
  7675. (org-show-todo-tree '(4)))
  7676. ((equal ans ?T)
  7677. (call-interactively 'org-tags-sparse-tree))
  7678. ((member ans '(?p ?P))
  7679. (setq kwd (completing-read "Property: "
  7680. (mapcar 'list (org-buffer-property-keys))))
  7681. (setq value (completing-read "Value: "
  7682. (mapcar 'list (org-property-values kwd))))
  7683. (unless (string-match "\\`{.*}\\'" value)
  7684. (setq value (concat "\"" value "\"")))
  7685. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7686. ((member ans '(?r ?R ?/))
  7687. (call-interactively 'org-occur))
  7688. (t (error "No such sparse tree command \"%c\"" ans)))))
  7689. (defvar org-occur-highlights nil
  7690. "List of overlays used for occur matches.")
  7691. (make-variable-buffer-local 'org-occur-highlights)
  7692. (defvar org-occur-parameters nil
  7693. "Parameters of the active org-occur calls.
  7694. This is a list, each call to org-occur pushes as cons cell,
  7695. containing the regular expression and the callback, onto the list.
  7696. The list can contain several entries if `org-occur' has been called
  7697. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7698. will only contain one set of parameters. When the highlights are
  7699. removed (for example with `C-c C-c', or with the next edit (depending
  7700. on `org-remove-highlights-with-change'), this variable is emptied
  7701. as well.")
  7702. (make-variable-buffer-local 'org-occur-parameters)
  7703. (defun org-occur (regexp &optional keep-previous callback)
  7704. "Make a compact tree which shows all matches of REGEXP.
  7705. The tree will show the lines where the regexp matches, and all higher
  7706. headlines above the match. It will also show the heading after the match,
  7707. to make sure editing the matching entry is easy.
  7708. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7709. call to `org-occur' will be kept, to allow stacking of calls to this
  7710. command.
  7711. If CALLBACK is non-nil, it is a function which is called to confirm
  7712. that the match should indeed be shown."
  7713. (interactive "sRegexp: \nP")
  7714. (unless keep-previous
  7715. (org-remove-occur-highlights nil nil t))
  7716. (push (cons regexp callback) org-occur-parameters)
  7717. (let ((cnt 0))
  7718. (save-excursion
  7719. (goto-char (point-min))
  7720. (if (or (not keep-previous) ; do not want to keep
  7721. (not org-occur-highlights)) ; no previous matches
  7722. ;; hide everything
  7723. (org-overview))
  7724. (while (re-search-forward regexp nil t)
  7725. (when (or (not callback)
  7726. (save-match-data (funcall callback)))
  7727. (setq cnt (1+ cnt))
  7728. (when org-highlight-sparse-tree-matches
  7729. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7730. (org-show-context 'occur-tree))))
  7731. (when org-remove-highlights-with-change
  7732. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7733. nil 'local))
  7734. (unless org-sparse-tree-open-archived-trees
  7735. (org-hide-archived-subtrees (point-min) (point-max)))
  7736. (run-hooks 'org-occur-hook)
  7737. (if (interactive-p)
  7738. (message "%d match(es) for regexp %s" cnt regexp))
  7739. cnt))
  7740. (defun org-show-context (&optional key)
  7741. "Make sure point and context and visible.
  7742. How much context is shown depends upon the variables
  7743. `org-show-hierarchy-above', `org-show-following-heading'. and
  7744. `org-show-siblings'."
  7745. (let ((heading-p (org-on-heading-p t))
  7746. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7747. (following-p (org-get-alist-option org-show-following-heading key))
  7748. (entry-p (org-get-alist-option org-show-entry-below key))
  7749. (siblings-p (org-get-alist-option org-show-siblings key)))
  7750. (catch 'exit
  7751. ;; Show heading or entry text
  7752. (if (and heading-p (not entry-p))
  7753. (org-flag-heading nil) ; only show the heading
  7754. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7755. (org-show-hidden-entry))) ; show entire entry
  7756. (when following-p
  7757. ;; Show next sibling, or heading below text
  7758. (save-excursion
  7759. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  7760. (org-flag-heading nil))))
  7761. (when siblings-p (org-show-siblings))
  7762. (when hierarchy-p
  7763. ;; show all higher headings, possibly with siblings
  7764. (save-excursion
  7765. (while (and (condition-case nil
  7766. (progn (org-up-heading-all 1) t)
  7767. (error nil))
  7768. (not (bobp)))
  7769. (org-flag-heading nil)
  7770. (when siblings-p (org-show-siblings))))))))
  7771. (defun org-reveal (&optional siblings)
  7772. "Show current entry, hierarchy above it, and the following headline.
  7773. This can be used to show a consistent set of context around locations
  7774. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  7775. not t for the search context.
  7776. With optional argument SIBLINGS, on each level of the hierarchy all
  7777. siblings are shown. This repairs the tree structure to what it would
  7778. look like when opened with hierarchical calls to `org-cycle'."
  7779. (interactive "P")
  7780. (let ((org-show-hierarchy-above t)
  7781. (org-show-following-heading t)
  7782. (org-show-siblings (if siblings t org-show-siblings)))
  7783. (org-show-context nil)))
  7784. (defun org-highlight-new-match (beg end)
  7785. "Highlight from BEG to END and mark the highlight is an occur headline."
  7786. (let ((ov (org-make-overlay beg end)))
  7787. (org-overlay-put ov 'face 'secondary-selection)
  7788. (push ov org-occur-highlights)))
  7789. (defun org-remove-occur-highlights (&optional beg end noremove)
  7790. "Remove the occur highlights from the buffer.
  7791. BEG and END are ignored. If NOREMOVE is nil, remove this function
  7792. from the `before-change-functions' in the current buffer."
  7793. (interactive)
  7794. (unless org-inhibit-highlight-removal
  7795. (mapc 'org-delete-overlay org-occur-highlights)
  7796. (setq org-occur-highlights nil)
  7797. (setq org-occur-parameters nil)
  7798. (unless noremove
  7799. (remove-hook 'before-change-functions
  7800. 'org-remove-occur-highlights 'local))))
  7801. ;;;; Priorities
  7802. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  7803. "Regular expression matching the priority indicator.")
  7804. (defvar org-remove-priority-next-time nil)
  7805. (defun org-priority-up ()
  7806. "Increase the priority of the current item."
  7807. (interactive)
  7808. (org-priority 'up))
  7809. (defun org-priority-down ()
  7810. "Decrease the priority of the current item."
  7811. (interactive)
  7812. (org-priority 'down))
  7813. (defun org-priority (&optional action)
  7814. "Change the priority of an item by ARG.
  7815. ACTION can be `set', `up', `down', or a character."
  7816. (interactive)
  7817. (setq action (or action 'set))
  7818. (let (current new news have remove)
  7819. (save-excursion
  7820. (org-back-to-heading)
  7821. (if (looking-at org-priority-regexp)
  7822. (setq current (string-to-char (match-string 2))
  7823. have t)
  7824. (setq current org-default-priority))
  7825. (cond
  7826. ((or (eq action 'set)
  7827. (if (featurep 'xemacs) (characterp action) (integerp action)))
  7828. (if (not (eq action 'set))
  7829. (setq new action)
  7830. (message "Priority %c-%c, SPC to remove: "
  7831. org-highest-priority org-lowest-priority)
  7832. (setq new (read-char-exclusive)))
  7833. (if (and (= (upcase org-highest-priority) org-highest-priority)
  7834. (= (upcase org-lowest-priority) org-lowest-priority))
  7835. (setq new (upcase new)))
  7836. (cond ((equal new ?\ ) (setq remove t))
  7837. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  7838. (error "Priority must be between `%c' and `%c'"
  7839. org-highest-priority org-lowest-priority))))
  7840. ((eq action 'up)
  7841. (if (and (not have) (eq last-command this-command))
  7842. (setq new org-lowest-priority)
  7843. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7844. org-default-priority (1- current)))))
  7845. ((eq action 'down)
  7846. (if (and (not have) (eq last-command this-command))
  7847. (setq new org-highest-priority)
  7848. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7849. org-default-priority (1+ current)))))
  7850. (t (error "Invalid action")))
  7851. (if (or (< (upcase new) org-highest-priority)
  7852. (> (upcase new) org-lowest-priority))
  7853. (setq remove t))
  7854. (setq news (format "%c" new))
  7855. (if have
  7856. (if remove
  7857. (replace-match "" t t nil 1)
  7858. (replace-match news t t nil 2))
  7859. (if remove
  7860. (error "No priority cookie found in line")
  7861. (looking-at org-todo-line-regexp)
  7862. (if (match-end 2)
  7863. (progn
  7864. (goto-char (match-end 2))
  7865. (insert " [#" news "]"))
  7866. (goto-char (match-beginning 3))
  7867. (insert "[#" news "] ")))))
  7868. (org-preserve-lc (org-set-tags nil 'align))
  7869. (if remove
  7870. (message "Priority removed")
  7871. (message "Priority of current item set to %s" news))))
  7872. (defun org-get-priority (s)
  7873. "Find priority cookie and return priority."
  7874. (save-match-data
  7875. (if (not (string-match org-priority-regexp s))
  7876. (* 1000 (- org-lowest-priority org-default-priority))
  7877. (* 1000 (- org-lowest-priority
  7878. (string-to-char (match-string 2 s)))))))
  7879. ;;;; Tags
  7880. (defun org-scan-tags (action matcher &optional todo-only)
  7881. "Scan headline tags with inheritance and produce output ACTION.
  7882. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  7883. evaluated, testing if a given set of tags qualifies a headline for
  7884. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  7885. are included in the output."
  7886. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  7887. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  7888. (org-re
  7889. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  7890. (props (list 'face nil
  7891. 'done-face 'org-done
  7892. 'undone-face nil
  7893. 'mouse-face 'highlight
  7894. 'org-not-done-regexp org-not-done-regexp
  7895. 'org-todo-regexp org-todo-regexp
  7896. 'keymap org-agenda-keymap
  7897. 'help-echo
  7898. (format "mouse-2 or RET jump to org file %s"
  7899. (abbreviate-file-name
  7900. (or (buffer-file-name (buffer-base-buffer))
  7901. (buffer-name (buffer-base-buffer)))))))
  7902. (case-fold-search nil)
  7903. lspos
  7904. tags tags-list tags-alist (llast 0) rtn level category i txt
  7905. todo marker entry priority)
  7906. (save-excursion
  7907. (goto-char (point-min))
  7908. (when (eq action 'sparse-tree)
  7909. (org-overview)
  7910. (org-remove-occur-highlights))
  7911. (while (re-search-forward re nil t)
  7912. (catch :skip
  7913. (setq todo (if (match-end 1) (match-string 2))
  7914. tags (if (match-end 4) (match-string 4)))
  7915. (goto-char (setq lspos (1+ (match-beginning 0))))
  7916. (setq level (org-reduced-level (funcall outline-level))
  7917. category (org-get-category))
  7918. (setq i llast llast level)
  7919. ;; remove tag lists from same and sublevels
  7920. (while (>= i level)
  7921. (when (setq entry (assoc i tags-alist))
  7922. (setq tags-alist (delete entry tags-alist)))
  7923. (setq i (1- i)))
  7924. ;; add the next tags
  7925. (when tags
  7926. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  7927. tags-alist
  7928. (cons (cons level tags) tags-alist)))
  7929. ;; compile tags for current headline
  7930. (setq tags-list
  7931. (if org-use-tag-inheritance
  7932. (apply 'append (mapcar 'cdr tags-alist))
  7933. tags))
  7934. (when (and tags org-use-tag-inheritance
  7935. (not (eq t org-use-tag-inheritance)))
  7936. ;; selective inheritance, remove uninherited ones
  7937. (setcdr (car tags-alist)
  7938. (org-remove-uniherited-tags (cdar tags-alist))))
  7939. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  7940. (eval matcher)
  7941. (or (not org-agenda-skip-archived-trees)
  7942. (not (member org-archive-tag tags-list))))
  7943. (and (eq action 'agenda) (org-agenda-skip))
  7944. ;; list this headline
  7945. (if (eq action 'sparse-tree)
  7946. (progn
  7947. (and org-highlight-sparse-tree-matches
  7948. (org-get-heading) (match-end 0)
  7949. (org-highlight-new-match
  7950. (match-beginning 0) (match-beginning 1)))
  7951. (org-show-context 'tags-tree))
  7952. (setq txt (org-format-agenda-item
  7953. ""
  7954. (concat
  7955. (if org-tags-match-list-sublevels
  7956. (make-string (1- level) ?.) "")
  7957. (org-get-heading))
  7958. category tags-list)
  7959. priority (org-get-priority txt))
  7960. (goto-char lspos)
  7961. (setq marker (org-agenda-new-marker))
  7962. (org-add-props txt props
  7963. 'org-marker marker 'org-hd-marker marker 'org-category category
  7964. 'priority priority 'type "tagsmatch")
  7965. (push txt rtn))
  7966. ;; if we are to skip sublevels, jump to end of subtree
  7967. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  7968. (when (and (eq action 'sparse-tree)
  7969. (not org-sparse-tree-open-archived-trees))
  7970. (org-hide-archived-subtrees (point-min) (point-max)))
  7971. (nreverse rtn)))
  7972. (defun org-remove-uniherited-tags (tags)
  7973. "Remove all tags that are not inherited from the list TAGS."
  7974. (cond
  7975. ((eq org-use-tag-inheritance t) tags)
  7976. ((not org-use-tag-inheritance) nil)
  7977. ((stringp org-use-tag-inheritance)
  7978. (delq nil (mapcar
  7979. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  7980. tags)))
  7981. ((listp org-use-tag-inheritance)
  7982. (org-delete-all org-use-tag-inheritance tags))))
  7983. (defvar todo-only) ;; dynamically scoped
  7984. (defun org-tags-sparse-tree (&optional todo-only match)
  7985. "Create a sparse tree according to tags string MATCH.
  7986. MATCH can contain positive and negative selection of tags, like
  7987. \"+WORK+URGENT-WITHBOSS\".
  7988. If optional argument TODO_ONLY is non-nil, only select lines that are
  7989. also TODO lines."
  7990. (interactive "P")
  7991. (org-prepare-agenda-buffers (list (current-buffer)))
  7992. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  7993. (defvar org-cached-props nil)
  7994. (defun org-cached-entry-get (pom property)
  7995. (if (or (eq t org-use-property-inheritance)
  7996. (and (stringp org-use-property-inheritance)
  7997. (string-match org-use-property-inheritance property))
  7998. (and (listp org-use-property-inheritance)
  7999. (member property org-use-property-inheritance)))
  8000. ;; Caching is not possible, check it directly
  8001. (org-entry-get pom property 'inherit)
  8002. ;; Get all properties, so that we can do complicated checks easily
  8003. (cdr (assoc property (or org-cached-props
  8004. (setq org-cached-props
  8005. (org-entry-properties pom)))))))
  8006. (defun org-global-tags-completion-table (&optional files)
  8007. "Return the list of all tags in all agenda buffer/files."
  8008. (save-excursion
  8009. (org-uniquify
  8010. (delq nil
  8011. (apply 'append
  8012. (mapcar
  8013. (lambda (file)
  8014. (set-buffer (find-file-noselect file))
  8015. (append (org-get-buffer-tags)
  8016. (mapcar (lambda (x) (if (stringp (car-safe x))
  8017. (list (car-safe x)) nil))
  8018. org-tag-alist)))
  8019. (if (and files (car files))
  8020. files
  8021. (org-agenda-files))))))))
  8022. (defun org-make-tags-matcher (match)
  8023. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8024. ;; todo-only is scoped dynamically into this function, and the function
  8025. ;; may change it it the matcher asksk for it.
  8026. (unless match
  8027. ;; Get a new match request, with completion
  8028. (let ((org-last-tags-completion-table
  8029. (org-global-tags-completion-table)))
  8030. (setq match (completing-read
  8031. "Match: " 'org-tags-completion-function nil nil nil
  8032. 'org-tags-history))))
  8033. ;; Parse the string and create a lisp form
  8034. (let ((match0 match)
  8035. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8036. minus tag mm
  8037. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8038. orterms term orlist re-p str-p level-p level-op
  8039. prop-p pn pv po cat-p gv)
  8040. (if (string-match "/+" match)
  8041. ;; match contains also a todo-matching request
  8042. (progn
  8043. (setq tagsmatch (substring match 0 (match-beginning 0))
  8044. todomatch (substring match (match-end 0)))
  8045. (if (string-match "^!" todomatch)
  8046. (setq todo-only t todomatch (substring todomatch 1)))
  8047. (if (string-match "^\\s-*$" todomatch)
  8048. (setq todomatch nil)))
  8049. ;; only matching tags
  8050. (setq tagsmatch match todomatch nil))
  8051. ;; Make the tags matcher
  8052. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8053. (setq tagsmatcher t)
  8054. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8055. (while (setq term (pop orterms))
  8056. (while (and (equal (substring term -1) "\\") orterms)
  8057. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8058. (while (string-match re term)
  8059. (setq minus (and (match-end 1)
  8060. (equal (match-string 1 term) "-"))
  8061. tag (match-string 2 term)
  8062. re-p (equal (string-to-char tag) ?{)
  8063. level-p (match-end 4)
  8064. prop-p (match-end 5)
  8065. mm (cond
  8066. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8067. (level-p
  8068. (setq level-op (org-op-to-function (match-string 3 term)))
  8069. `(,level-op level ,(string-to-number
  8070. (match-string 4 term))))
  8071. (prop-p
  8072. (setq pn (match-string 5 term)
  8073. po (match-string 6 term)
  8074. pv (match-string 7 term)
  8075. cat-p (equal pn "CATEGORY")
  8076. re-p (equal (string-to-char pv) ?{)
  8077. str-p (equal (string-to-char pv) ?\")
  8078. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8079. (setq po (org-op-to-function po str-p))
  8080. (if (equal pn "CATEGORY")
  8081. (setq gv '(get-text-property (point) 'org-category))
  8082. (setq gv `(org-cached-entry-get nil ,pn)))
  8083. (if re-p
  8084. (if (eq po 'org<>)
  8085. `(not (string-match ,pv (or ,gv "")))
  8086. `(string-match ,pv (or ,gv "")))
  8087. (if str-p
  8088. `(,po (or ,gv "") ,pv)
  8089. `(,po (string-to-number (or ,gv ""))
  8090. ,(string-to-number pv) ))))
  8091. (t `(member ,(downcase tag) tags-list)))
  8092. mm (if minus (list 'not mm) mm)
  8093. term (substring term (match-end 0)))
  8094. (push mm tagsmatcher))
  8095. (push (if (> (length tagsmatcher) 1)
  8096. (cons 'and tagsmatcher)
  8097. (car tagsmatcher))
  8098. orlist)
  8099. (setq tagsmatcher nil))
  8100. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8101. (setq tagsmatcher
  8102. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8103. ;; Make the todo matcher
  8104. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8105. (setq todomatcher t)
  8106. (setq orterms (org-split-string todomatch "|") orlist nil)
  8107. (while (setq term (pop orterms))
  8108. (while (string-match re term)
  8109. (setq minus (and (match-end 1)
  8110. (equal (match-string 1 term) "-"))
  8111. kwd (match-string 2 term)
  8112. re-p (equal (string-to-char kwd) ?{)
  8113. term (substring term (match-end 0))
  8114. mm (if re-p
  8115. `(string-match ,(substring kwd 1 -1) todo)
  8116. (list 'equal 'todo kwd))
  8117. mm (if minus (list 'not mm) mm))
  8118. (push mm todomatcher))
  8119. (push (if (> (length todomatcher) 1)
  8120. (cons 'and todomatcher)
  8121. (car todomatcher))
  8122. orlist)
  8123. (setq todomatcher nil))
  8124. (setq todomatcher (if (> (length orlist) 1)
  8125. (cons 'or orlist) (car orlist))))
  8126. ;; Return the string and lisp forms of the matcher
  8127. (setq matcher (if todomatcher
  8128. (list 'and tagsmatcher todomatcher)
  8129. tagsmatcher))
  8130. (cons match0 matcher)))
  8131. (defun org-op-to-function (op &optional stringp)
  8132. (setq op
  8133. (cond
  8134. ((equal op "<" ) '(< string< ))
  8135. ((equal op ">" ) '(> org-string> ))
  8136. ((member op '("<=" "=<")) '(<= org-string<= ))
  8137. ((member op '(">=" "=>")) '(>= org-string>= ))
  8138. ((member op '("=" "==")) '(= string= ))
  8139. ((member op '("<>" "!=")) '(org<> org-string<> ))))
  8140. (nth (if stringp 1 0) op))
  8141. (defun org<> (a b) (not (= a b)))
  8142. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8143. (defun org-string>= (a b) (not (string< a b)))
  8144. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8145. (defun org-string<> (a b) (not (string= a b)))
  8146. (defun org-match-any-p (re list)
  8147. "Does re match any element of list?"
  8148. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8149. (delq nil list))
  8150. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8151. (defvar org-tags-overlay (org-make-overlay 1 1))
  8152. (org-detach-overlay org-tags-overlay)
  8153. (defun org-get-tags-at (&optional pos)
  8154. "Get a list of all headline tags applicable at POS.
  8155. POS defaults to point. If tags are inherited, the list contains
  8156. the targets in the same sequence as the headlines appear, i.e.
  8157. sthe tags of the current headline come last."
  8158. (interactive)
  8159. (let (tags ltags lastpos parent)
  8160. (save-excursion
  8161. (save-restriction
  8162. (widen)
  8163. (goto-char (or pos (point)))
  8164. (save-match-data
  8165. (condition-case nil
  8166. (progn
  8167. (org-back-to-heading t)
  8168. (while (not (equal lastpos (point)))
  8169. (setq lastpos (point))
  8170. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8171. (setq ltags (org-split-string
  8172. (org-match-string-no-properties 1) ":"))
  8173. (setq tags (append (org-remove-uniherited-tags ltags)
  8174. tags)))
  8175. (or org-use-tag-inheritance (error ""))
  8176. (org-up-heading-all 1)
  8177. (setq parent t)))
  8178. (error nil))))
  8179. tags)))
  8180. (defun org-toggle-tag (tag &optional onoff)
  8181. "Toggle the tag TAG for the current line.
  8182. If ONOFF is `on' or `off', don't toggle but set to this state."
  8183. (unless (org-on-heading-p t) (error "Not on headling"))
  8184. (let (res current)
  8185. (save-excursion
  8186. (beginning-of-line)
  8187. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8188. (point-at-eol) t)
  8189. (progn
  8190. (setq current (match-string 1))
  8191. (replace-match ""))
  8192. (setq current ""))
  8193. (setq current (nreverse (org-split-string current ":")))
  8194. (cond
  8195. ((eq onoff 'on)
  8196. (setq res t)
  8197. (or (member tag current) (push tag current)))
  8198. ((eq onoff 'off)
  8199. (or (not (member tag current)) (setq current (delete tag current))))
  8200. (t (if (member tag current)
  8201. (setq current (delete tag current))
  8202. (setq res t)
  8203. (push tag current))))
  8204. (end-of-line 1)
  8205. (if current
  8206. (progn
  8207. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8208. (org-set-tags nil t))
  8209. (delete-horizontal-space))
  8210. (run-hooks 'org-after-tags-change-hook))
  8211. res))
  8212. (defun org-align-tags-here (to-col)
  8213. ;; Assumes that this is a headline
  8214. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8215. (beginning-of-line 1)
  8216. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8217. (< pos (match-beginning 2)))
  8218. (progn
  8219. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8220. (goto-char (match-beginning 1))
  8221. (insert " ")
  8222. (delete-region (point) (1+ (match-beginning 2)))
  8223. (setq ncol (max (1+ (current-column))
  8224. (1+ col)
  8225. (if (> to-col 0)
  8226. to-col
  8227. (- (abs to-col) tags-l))))
  8228. (setq p (point))
  8229. (insert (make-string (- ncol (current-column)) ?\ ))
  8230. (setq ncol (current-column))
  8231. (tabify p (point-at-eol))
  8232. (org-move-to-column (min ncol col) t))
  8233. (goto-char pos))))
  8234. (defun org-set-tags (&optional arg just-align)
  8235. "Set the tags for the current headline.
  8236. With prefix ARG, realign all tags in headings in the current buffer."
  8237. (interactive "P")
  8238. (let* ((re (concat "^" outline-regexp))
  8239. (current (org-get-tags-string))
  8240. (col (current-column))
  8241. (org-setting-tags t)
  8242. table current-tags inherited-tags ; computed below when needed
  8243. tags p0 c0 c1 rpl)
  8244. (if arg
  8245. (save-excursion
  8246. (goto-char (point-min))
  8247. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8248. (while (re-search-forward re nil t)
  8249. (org-set-tags nil t)
  8250. (end-of-line 1)))
  8251. (message "All tags realigned to column %d" org-tags-column))
  8252. (if just-align
  8253. (setq tags current)
  8254. ;; Get a new set of tags from the user
  8255. (save-excursion
  8256. (setq table (or org-tag-alist (org-get-buffer-tags))
  8257. org-last-tags-completion-table table
  8258. current-tags (org-split-string current ":")
  8259. inherited-tags (nreverse
  8260. (nthcdr (length current-tags)
  8261. (nreverse (org-get-tags-at))))
  8262. tags
  8263. (if (or (eq t org-use-fast-tag-selection)
  8264. (and org-use-fast-tag-selection
  8265. (delq nil (mapcar 'cdr table))))
  8266. (org-fast-tag-selection
  8267. current-tags inherited-tags table
  8268. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8269. (let ((org-add-colon-after-tag-completion t))
  8270. (org-trim
  8271. (org-without-partial-completion
  8272. (completing-read "Tags: " 'org-tags-completion-function
  8273. nil nil current 'org-tags-history)))))))
  8274. (while (string-match "[-+&]+" tags)
  8275. ;; No boolean logic, just a list
  8276. (setq tags (replace-match ":" t t tags))))
  8277. (if (string-match "\\`[\t ]*\\'" tags)
  8278. (setq tags "")
  8279. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8280. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8281. ;; Insert new tags at the correct column
  8282. (beginning-of-line 1)
  8283. (cond
  8284. ((and (equal current "") (equal tags "")))
  8285. ((re-search-forward
  8286. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8287. (point-at-eol) t)
  8288. (if (equal tags "")
  8289. (setq rpl "")
  8290. (goto-char (match-beginning 0))
  8291. (setq c0 (current-column) p0 (point)
  8292. c1 (max (1+ c0) (if (> org-tags-column 0)
  8293. org-tags-column
  8294. (- (- org-tags-column) (length tags))))
  8295. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8296. (replace-match rpl t t)
  8297. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8298. tags)
  8299. (t (error "Tags alignment failed")))
  8300. (org-move-to-column col)
  8301. (unless just-align
  8302. (run-hooks 'org-after-tags-change-hook)))))
  8303. (defun org-change-tag-in-region (beg end tag off)
  8304. "Add or remove TAG for each entry in the region.
  8305. This works in the agenda, and also in an org-mode buffer."
  8306. (interactive
  8307. (list (region-beginning) (region-end)
  8308. (let ((org-last-tags-completion-table
  8309. (if (org-mode-p)
  8310. (org-get-buffer-tags)
  8311. (org-global-tags-completion-table))))
  8312. (completing-read
  8313. "Tag: " 'org-tags-completion-function nil nil nil
  8314. 'org-tags-history))
  8315. (progn
  8316. (message "[s]et or [r]emove? ")
  8317. (equal (read-char-exclusive) ?r))))
  8318. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8319. (let ((agendap (equal major-mode 'org-agenda-mode))
  8320. l1 l2 m buf pos newhead (cnt 0))
  8321. (goto-char end)
  8322. (setq l2 (1- (org-current-line)))
  8323. (goto-char beg)
  8324. (setq l1 (org-current-line))
  8325. (loop for l from l1 to l2 do
  8326. (goto-line l)
  8327. (setq m (get-text-property (point) 'org-hd-marker))
  8328. (when (or (and (org-mode-p) (org-on-heading-p))
  8329. (and agendap m))
  8330. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8331. pos (if agendap m (point)))
  8332. (with-current-buffer buf
  8333. (save-excursion
  8334. (save-restriction
  8335. (goto-char pos)
  8336. (setq cnt (1+ cnt))
  8337. (org-toggle-tag tag (if off 'off 'on))
  8338. (setq newhead (org-get-heading)))))
  8339. (and agendap (org-agenda-change-all-lines newhead m))))
  8340. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8341. (defun org-tags-completion-function (string predicate &optional flag)
  8342. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8343. (confirm (lambda (x) (stringp (car x)))))
  8344. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8345. (setq s1 (match-string 1 string)
  8346. s2 (match-string 2 string))
  8347. (setq s1 "" s2 string))
  8348. (cond
  8349. ((eq flag nil)
  8350. ;; try completion
  8351. (setq rtn (try-completion s2 ctable confirm))
  8352. (if (stringp rtn)
  8353. (setq rtn
  8354. (concat s1 s2 (substring rtn (length s2))
  8355. (if (and org-add-colon-after-tag-completion
  8356. (assoc rtn ctable))
  8357. ":" ""))))
  8358. rtn)
  8359. ((eq flag t)
  8360. ;; all-completions
  8361. (all-completions s2 ctable confirm)
  8362. )
  8363. ((eq flag 'lambda)
  8364. ;; exact match?
  8365. (assoc s2 ctable)))
  8366. ))
  8367. (defun org-fast-tag-insert (kwd tags face &optional end)
  8368. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8369. (insert (format "%-12s" (concat kwd ":"))
  8370. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8371. (or end "")))
  8372. (defun org-fast-tag-show-exit (flag)
  8373. (save-excursion
  8374. (goto-line 3)
  8375. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8376. (replace-match ""))
  8377. (when flag
  8378. (end-of-line 1)
  8379. (org-move-to-column (- (window-width) 19) t)
  8380. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8381. (defun org-set-current-tags-overlay (current prefix)
  8382. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8383. (if (featurep 'xemacs)
  8384. (org-overlay-display org-tags-overlay (concat prefix s)
  8385. 'secondary-selection)
  8386. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8387. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8388. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8389. "Fast tag selection with single keys.
  8390. CURRENT is the current list of tags in the headline, INHERITED is the
  8391. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8392. possibly with grouping information. TODO-TABLE is a similar table with
  8393. TODO keywords, should these have keys assigned to them.
  8394. If the keys are nil, a-z are automatically assigned.
  8395. Returns the new tags string, or nil to not change the current settings."
  8396. (let* ((fulltable (append table todo-table))
  8397. (maxlen (apply 'max (mapcar
  8398. (lambda (x)
  8399. (if (stringp (car x)) (string-width (car x)) 0))
  8400. fulltable)))
  8401. (buf (current-buffer))
  8402. (expert (eq org-fast-tag-selection-single-key 'expert))
  8403. (buffer-tags nil)
  8404. (fwidth (+ maxlen 3 1 3))
  8405. (ncol (/ (- (window-width) 4) fwidth))
  8406. (i-face 'org-done)
  8407. (c-face 'org-todo)
  8408. tg cnt e c char c1 c2 ntable tbl rtn
  8409. ov-start ov-end ov-prefix
  8410. (exit-after-next org-fast-tag-selection-single-key)
  8411. (done-keywords org-done-keywords)
  8412. groups ingroup)
  8413. (save-excursion
  8414. (beginning-of-line 1)
  8415. (if (looking-at
  8416. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8417. (setq ov-start (match-beginning 1)
  8418. ov-end (match-end 1)
  8419. ov-prefix "")
  8420. (setq ov-start (1- (point-at-eol))
  8421. ov-end (1+ ov-start))
  8422. (skip-chars-forward "^\n\r")
  8423. (setq ov-prefix
  8424. (concat
  8425. (buffer-substring (1- (point)) (point))
  8426. (if (> (current-column) org-tags-column)
  8427. " "
  8428. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8429. (org-move-overlay org-tags-overlay ov-start ov-end)
  8430. (save-window-excursion
  8431. (if expert
  8432. (set-buffer (get-buffer-create " *Org tags*"))
  8433. (delete-other-windows)
  8434. (split-window-vertically)
  8435. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8436. (erase-buffer)
  8437. (org-set-local 'org-done-keywords done-keywords)
  8438. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8439. (org-fast-tag-insert "Current" current c-face "\n\n")
  8440. (org-fast-tag-show-exit exit-after-next)
  8441. (org-set-current-tags-overlay current ov-prefix)
  8442. (setq tbl fulltable char ?a cnt 0)
  8443. (while (setq e (pop tbl))
  8444. (cond
  8445. ((equal e '(:startgroup))
  8446. (push '() groups) (setq ingroup t)
  8447. (when (not (= cnt 0))
  8448. (setq cnt 0)
  8449. (insert "\n"))
  8450. (insert "{ "))
  8451. ((equal e '(:endgroup))
  8452. (setq ingroup nil cnt 0)
  8453. (insert "}\n"))
  8454. (t
  8455. (setq tg (car e) c2 nil)
  8456. (if (cdr e)
  8457. (setq c (cdr e))
  8458. ;; automatically assign a character.
  8459. (setq c1 (string-to-char
  8460. (downcase (substring
  8461. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8462. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8463. (while (or (rassoc char ntable) (rassoc char table))
  8464. (setq char (1+ char)))
  8465. (setq c2 c1))
  8466. (setq c (or c2 char)))
  8467. (if ingroup (push tg (car groups)))
  8468. (setq tg (org-add-props tg nil 'face
  8469. (cond
  8470. ((not (assoc tg table))
  8471. (org-get-todo-face tg))
  8472. ((member tg current) c-face)
  8473. ((member tg inherited) i-face)
  8474. (t nil))))
  8475. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8476. (insert "[" c "] " tg (make-string
  8477. (- fwidth 4 (length tg)) ?\ ))
  8478. (push (cons tg c) ntable)
  8479. (when (= (setq cnt (1+ cnt)) ncol)
  8480. (insert "\n")
  8481. (if ingroup (insert " "))
  8482. (setq cnt 0)))))
  8483. (setq ntable (nreverse ntable))
  8484. (insert "\n")
  8485. (goto-char (point-min))
  8486. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8487. (fit-window-to-buffer))
  8488. (setq rtn
  8489. (catch 'exit
  8490. (while t
  8491. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8492. (if groups " [!] no groups" " [!]groups")
  8493. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8494. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8495. (cond
  8496. ((= c ?\r) (throw 'exit t))
  8497. ((= c ?!)
  8498. (setq groups (not groups))
  8499. (goto-char (point-min))
  8500. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8501. ((= c ?\C-c)
  8502. (if (not expert)
  8503. (org-fast-tag-show-exit
  8504. (setq exit-after-next (not exit-after-next)))
  8505. (setq expert nil)
  8506. (delete-other-windows)
  8507. (split-window-vertically)
  8508. (org-switch-to-buffer-other-window " *Org tags*")
  8509. (and (fboundp 'fit-window-to-buffer)
  8510. (fit-window-to-buffer))))
  8511. ((or (= c ?\C-g)
  8512. (and (= c ?q) (not (rassoc c ntable))))
  8513. (org-detach-overlay org-tags-overlay)
  8514. (setq quit-flag t))
  8515. ((= c ?\ )
  8516. (setq current nil)
  8517. (if exit-after-next (setq exit-after-next 'now)))
  8518. ((= c ?\t)
  8519. (condition-case nil
  8520. (setq tg (completing-read
  8521. "Tag: "
  8522. (or buffer-tags
  8523. (with-current-buffer buf
  8524. (org-get-buffer-tags)))))
  8525. (quit (setq tg "")))
  8526. (when (string-match "\\S-" tg)
  8527. (add-to-list 'buffer-tags (list tg))
  8528. (if (member tg current)
  8529. (setq current (delete tg current))
  8530. (push tg current)))
  8531. (if exit-after-next (setq exit-after-next 'now)))
  8532. ((setq e (rassoc c todo-table) tg (car e))
  8533. (with-current-buffer buf
  8534. (save-excursion (org-todo tg)))
  8535. (if exit-after-next (setq exit-after-next 'now)))
  8536. ((setq e (rassoc c ntable) tg (car e))
  8537. (if (member tg current)
  8538. (setq current (delete tg current))
  8539. (loop for g in groups do
  8540. (if (member tg g)
  8541. (mapc (lambda (x)
  8542. (setq current (delete x current)))
  8543. g)))
  8544. (push tg current))
  8545. (if exit-after-next (setq exit-after-next 'now))))
  8546. ;; Create a sorted list
  8547. (setq current
  8548. (sort current
  8549. (lambda (a b)
  8550. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8551. (if (eq exit-after-next 'now) (throw 'exit t))
  8552. (goto-char (point-min))
  8553. (beginning-of-line 2)
  8554. (delete-region (point) (point-at-eol))
  8555. (org-fast-tag-insert "Current" current c-face)
  8556. (org-set-current-tags-overlay current ov-prefix)
  8557. (while (re-search-forward
  8558. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8559. (setq tg (match-string 1))
  8560. (add-text-properties
  8561. (match-beginning 1) (match-end 1)
  8562. (list 'face
  8563. (cond
  8564. ((member tg current) c-face)
  8565. ((member tg inherited) i-face)
  8566. (t (get-text-property (match-beginning 1) 'face))))))
  8567. (goto-char (point-min)))))
  8568. (org-detach-overlay org-tags-overlay)
  8569. (if rtn
  8570. (mapconcat 'identity current ":")
  8571. nil))))
  8572. (defun org-get-tags-string ()
  8573. "Get the TAGS string in the current headline."
  8574. (unless (org-on-heading-p t)
  8575. (error "Not on a heading"))
  8576. (save-excursion
  8577. (beginning-of-line 1)
  8578. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8579. (org-match-string-no-properties 1)
  8580. "")))
  8581. (defun org-get-tags ()
  8582. "Get the list of tags specified in the current headline."
  8583. (org-split-string (org-get-tags-string) ":"))
  8584. (defun org-get-buffer-tags ()
  8585. "Get a table of all tags used in the buffer, for completion."
  8586. (let (tags)
  8587. (save-excursion
  8588. (goto-char (point-min))
  8589. (while (re-search-forward
  8590. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8591. (when (equal (char-after (point-at-bol 0)) ?*)
  8592. (mapc (lambda (x) (add-to-list 'tags x))
  8593. (org-split-string (org-match-string-no-properties 1) ":")))))
  8594. (mapcar 'list tags)))
  8595. ;;;; Properties
  8596. ;;; Setting and retrieving properties
  8597. (defconst org-special-properties
  8598. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8599. "TIMESTAMP" "TIMESTAMP_IA")
  8600. "The special properties valid in Org-mode.
  8601. These are properties that are not defined in the property drawer,
  8602. but in some other way.")
  8603. (defconst org-default-properties
  8604. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8605. "LOCATION" "LOGGING" "COLUMNS")
  8606. "Some properties that are used by Org-mode for various purposes.
  8607. Being in this list makes sure that they are offered for completion.")
  8608. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8609. "Regular expression matching the first line of a property drawer.")
  8610. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8611. "Regular expression matching the first line of a property drawer.")
  8612. (defun org-property-action ()
  8613. "Do an action on properties."
  8614. (interactive)
  8615. (let (c)
  8616. (org-at-property-p)
  8617. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8618. (setq c (read-char-exclusive))
  8619. (cond
  8620. ((equal c ?s)
  8621. (call-interactively 'org-set-property))
  8622. ((equal c ?d)
  8623. (call-interactively 'org-delete-property))
  8624. ((equal c ?D)
  8625. (call-interactively 'org-delete-property-globally))
  8626. ((equal c ?c)
  8627. (call-interactively 'org-compute-property-at-point))
  8628. (t (error "No such property action %c" c)))))
  8629. (defun org-at-property-p ()
  8630. "Is the cursor in a property line?"
  8631. ;; FIXME: Does not check if we are actually in the drawer.
  8632. ;; FIXME: also returns true on any drawers.....
  8633. ;; This is used by C-c C-c for property action.
  8634. (save-excursion
  8635. (beginning-of-line 1)
  8636. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8637. (defun org-get-property-block (&optional beg end force)
  8638. "Return the (beg . end) range of the body of the property drawer.
  8639. BEG and END can be beginning and end of subtree, if not given
  8640. they will be found.
  8641. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8642. (catch 'exit
  8643. (save-excursion
  8644. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8645. (end (or end (progn (outline-next-heading) (point)))))
  8646. (goto-char beg)
  8647. (if (re-search-forward org-property-start-re end t)
  8648. (setq beg (1+ (match-end 0)))
  8649. (if force
  8650. (save-excursion
  8651. (org-insert-property-drawer)
  8652. (setq end (progn (outline-next-heading) (point))))
  8653. (throw 'exit nil))
  8654. (goto-char beg)
  8655. (if (re-search-forward org-property-start-re end t)
  8656. (setq beg (1+ (match-end 0)))))
  8657. (if (re-search-forward org-property-end-re end t)
  8658. (setq end (match-beginning 0))
  8659. (or force (throw 'exit nil))
  8660. (goto-char beg)
  8661. (setq end beg)
  8662. (org-indent-line-function)
  8663. (insert ":END:\n"))
  8664. (cons beg end)))))
  8665. (defun org-entry-properties (&optional pom which)
  8666. "Get all properties of the entry at point-or-marker POM.
  8667. This includes the TODO keyword, the tags, time strings for deadline,
  8668. scheduled, and clocking, and any additional properties defined in the
  8669. entry. The return value is an alist, keys may occur multiple times
  8670. if the property key was used several times.
  8671. POM may also be nil, in which case the current entry is used.
  8672. If WHICH is nil or `all', get all properties. If WHICH is
  8673. `special' or `standard', only get that subclass."
  8674. (setq which (or which 'all))
  8675. (org-with-point-at pom
  8676. (let ((clockstr (substring org-clock-string 0 -1))
  8677. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8678. beg end range props sum-props key value string clocksum)
  8679. (save-excursion
  8680. (when (condition-case nil (org-back-to-heading t) (error nil))
  8681. (setq beg (point))
  8682. (setq sum-props (get-text-property (point) 'org-summaries))
  8683. (setq clocksum (get-text-property (point) :org-clock-minutes))
  8684. (outline-next-heading)
  8685. (setq end (point))
  8686. (when (memq which '(all special))
  8687. ;; Get the special properties, like TODO and tags
  8688. (goto-char beg)
  8689. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  8690. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  8691. (when (looking-at org-priority-regexp)
  8692. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  8693. (when (and (setq value (org-get-tags-string))
  8694. (string-match "\\S-" value))
  8695. (push (cons "TAGS" value) props))
  8696. (when (setq value (org-get-tags-at))
  8697. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  8698. props))
  8699. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  8700. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  8701. string (if (equal key clockstr)
  8702. (org-no-properties
  8703. (org-trim
  8704. (buffer-substring
  8705. (match-beginning 3) (goto-char (point-at-eol)))))
  8706. (substring (org-match-string-no-properties 3) 1 -1)))
  8707. (unless key
  8708. (if (= (char-after (match-beginning 3)) ?\[)
  8709. (setq key "TIMESTAMP_IA")
  8710. (setq key "TIMESTAMP")))
  8711. (when (or (equal key clockstr) (not (assoc key props)))
  8712. (push (cons key string) props)))
  8713. )
  8714. (when (memq which '(all standard))
  8715. ;; Get the standard properties, like :PORP: ...
  8716. (setq range (org-get-property-block beg end))
  8717. (when range
  8718. (goto-char (car range))
  8719. (while (re-search-forward
  8720. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  8721. (cdr range) t)
  8722. (setq key (org-match-string-no-properties 1)
  8723. value (org-trim (or (org-match-string-no-properties 2) "")))
  8724. (unless (member key excluded)
  8725. (push (cons key (or value "")) props)))))
  8726. (if clocksum
  8727. (push (cons "CLOCKSUM"
  8728. (org-columns-number-to-string (/ (float clocksum) 60.)
  8729. 'add_times))
  8730. props))
  8731. (append sum-props (nreverse props)))))))
  8732. (defun org-entry-get (pom property &optional inherit)
  8733. "Get value of PROPERTY for entry at point-or-marker POM.
  8734. If INHERIT is non-nil and the entry does not have the property,
  8735. then also check higher levels of the hierarchy.
  8736. If INHERIT is the symbol `selective', use inheritance only if the setting
  8737. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  8738. If the property is present but empty, the return value is the empty string.
  8739. If the property is not present at all, nil is returned."
  8740. (org-with-point-at pom
  8741. (if (and inherit (if (eq inherit 'selective)
  8742. (org-property-inherit-p property)
  8743. t))
  8744. (org-entry-get-with-inheritance property)
  8745. (if (member property org-special-properties)
  8746. ;; We need a special property. Use brute force, get all properties.
  8747. (cdr (assoc property (org-entry-properties nil 'special)))
  8748. (let ((range (org-get-property-block)))
  8749. (if (and range
  8750. (goto-char (car range))
  8751. (re-search-forward
  8752. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  8753. (cdr range) t))
  8754. ;; Found the property, return it.
  8755. (if (match-end 1)
  8756. (org-match-string-no-properties 1)
  8757. "")))))))
  8758. (defun org-property-or-variable-value (var &optional inherit)
  8759. "Check if there is a property fixing the value of VAR.
  8760. If yes, return this value. If not, return the current value of the variable."
  8761. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  8762. (if (and prop (stringp prop) (string-match "\\S-" prop))
  8763. (read prop)
  8764. (symbol-value var))))
  8765. (defun org-entry-delete (pom property)
  8766. "Delete the property PROPERTY from entry at point-or-marker POM."
  8767. (org-with-point-at pom
  8768. (if (member property org-special-properties)
  8769. nil ; cannot delete these properties.
  8770. (let ((range (org-get-property-block)))
  8771. (if (and range
  8772. (goto-char (car range))
  8773. (re-search-forward
  8774. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  8775. (cdr range) t))
  8776. (progn
  8777. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  8778. t)
  8779. nil)))))
  8780. ;; Multi-values properties are properties that contain multiple values
  8781. ;; These values are assumed to be single words, separated by whitespace.
  8782. (defun org-entry-add-to-multivalued-property (pom property value)
  8783. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  8784. (let* ((old (org-entry-get pom property))
  8785. (values (and old (org-split-string old "[ \t]"))))
  8786. (unless (member value values)
  8787. (setq values (cons value values))
  8788. (org-entry-put pom property
  8789. (mapconcat 'identity values " ")))))
  8790. (defun org-entry-remove-from-multivalued-property (pom property value)
  8791. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  8792. (let* ((old (org-entry-get pom property))
  8793. (values (and old (org-split-string old "[ \t]"))))
  8794. (when (member value values)
  8795. (setq values (delete value values))
  8796. (org-entry-put pom property
  8797. (mapconcat 'identity values " ")))))
  8798. (defun org-entry-member-in-multivalued-property (pom property value)
  8799. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  8800. (let* ((old (org-entry-get pom property))
  8801. (values (and old (org-split-string old "[ \t]"))))
  8802. (member value values)))
  8803. (defvar org-entry-property-inherited-from (make-marker))
  8804. (defun org-entry-get-with-inheritance (property)
  8805. "Get entry property, and search higher levels if not present."
  8806. (let (tmp)
  8807. (save-excursion
  8808. (save-restriction
  8809. (widen)
  8810. (catch 'ex
  8811. (while t
  8812. (when (setq tmp (org-entry-get nil property))
  8813. (org-back-to-heading t)
  8814. (move-marker org-entry-property-inherited-from (point))
  8815. (throw 'ex tmp))
  8816. (or (org-up-heading-safe) (throw 'ex nil)))))
  8817. (or tmp (cdr (assoc property org-local-properties))
  8818. (cdr (assoc property org-global-properties))))))
  8819. (defun org-entry-put (pom property value)
  8820. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  8821. (org-with-point-at pom
  8822. (org-back-to-heading t)
  8823. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  8824. range)
  8825. (cond
  8826. ((equal property "TODO")
  8827. (when (and (stringp value) (string-match "\\S-" value)
  8828. (not (member value org-todo-keywords-1)))
  8829. (error "\"%s\" is not a valid TODO state" value))
  8830. (if (or (not value)
  8831. (not (string-match "\\S-" value)))
  8832. (setq value 'none))
  8833. (org-todo value)
  8834. (org-set-tags nil 'align))
  8835. ((equal property "PRIORITY")
  8836. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  8837. (string-to-char value) ?\ ))
  8838. (org-set-tags nil 'align))
  8839. ((equal property "SCHEDULED")
  8840. (if (re-search-forward org-scheduled-time-regexp end t)
  8841. (cond
  8842. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8843. ((eq value 'later) (org-timestamp-change 1 'day))
  8844. (t (call-interactively 'org-schedule)))
  8845. (call-interactively 'org-schedule)))
  8846. ((equal property "DEADLINE")
  8847. (if (re-search-forward org-deadline-time-regexp end t)
  8848. (cond
  8849. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8850. ((eq value 'later) (org-timestamp-change 1 'day))
  8851. (t (call-interactively 'org-deadline)))
  8852. (call-interactively 'org-deadline)))
  8853. ((member property org-special-properties)
  8854. (error "The %s property can not yet be set with `org-entry-put'"
  8855. property))
  8856. (t ; a non-special property
  8857. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  8858. (setq range (org-get-property-block beg end 'force))
  8859. (goto-char (car range))
  8860. (if (re-search-forward
  8861. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  8862. (progn
  8863. (delete-region (match-beginning 1) (match-end 1))
  8864. (goto-char (match-beginning 1)))
  8865. (goto-char (cdr range))
  8866. (insert "\n")
  8867. (backward-char 1)
  8868. (org-indent-line-function)
  8869. (insert ":" property ":"))
  8870. (and value (insert " " value))
  8871. (org-indent-line-function)))))))
  8872. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  8873. "Get all property keys in the current buffer.
  8874. With INCLUDE-SPECIALS, also list the special properties that relect things
  8875. like tags and TODO state.
  8876. With INCLUDE-DEFAULTS, also include properties that has special meaning
  8877. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  8878. With INCLUDE-COLUMNS, also include property names given in COLUMN
  8879. formats in the current buffer."
  8880. (let (rtn range cfmt cols s p)
  8881. (save-excursion
  8882. (save-restriction
  8883. (widen)
  8884. (goto-char (point-min))
  8885. (while (re-search-forward org-property-start-re nil t)
  8886. (setq range (org-get-property-block))
  8887. (goto-char (car range))
  8888. (while (re-search-forward
  8889. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  8890. (cdr range) t)
  8891. (add-to-list 'rtn (org-match-string-no-properties 1)))
  8892. (outline-next-heading))))
  8893. (when include-specials
  8894. (setq rtn (append org-special-properties rtn)))
  8895. (when include-defaults
  8896. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  8897. (when include-columns
  8898. (save-excursion
  8899. (save-restriction
  8900. (widen)
  8901. (goto-char (point-min))
  8902. (while (re-search-forward
  8903. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  8904. nil t)
  8905. (setq cfmt (match-string 2) s 0)
  8906. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  8907. cfmt s)
  8908. (setq s (match-end 0)
  8909. p (match-string 1 cfmt))
  8910. (unless (or (equal p "ITEM")
  8911. (member p org-special-properties))
  8912. (add-to-list 'rtn (match-string 1 cfmt))))))))
  8913. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  8914. (defun org-property-values (key)
  8915. "Return a list of all values of property KEY."
  8916. (save-excursion
  8917. (save-restriction
  8918. (widen)
  8919. (goto-char (point-min))
  8920. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  8921. values)
  8922. (while (re-search-forward re nil t)
  8923. (add-to-list 'values (org-trim (match-string 1))))
  8924. (delete "" values)))))
  8925. (defun org-insert-property-drawer ()
  8926. "Insert a property drawer into the current entry."
  8927. (interactive)
  8928. (org-back-to-heading t)
  8929. (looking-at outline-regexp)
  8930. (let ((indent (- (match-end 0)(match-beginning 0)))
  8931. (beg (point))
  8932. (re (concat "^[ \t]*" org-keyword-time-regexp))
  8933. end hiddenp)
  8934. (outline-next-heading)
  8935. (setq end (point))
  8936. (goto-char beg)
  8937. (while (re-search-forward re end t))
  8938. (setq hiddenp (org-invisible-p))
  8939. (end-of-line 1)
  8940. (and (equal (char-after) ?\n) (forward-char 1))
  8941. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  8942. (beginning-of-line 2))
  8943. (org-skip-over-state-notes)
  8944. (skip-chars-backward " \t\n\r")
  8945. (if (eq (char-before) ?*) (forward-char 1))
  8946. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  8947. (beginning-of-line 0)
  8948. (org-indent-to-column indent)
  8949. (beginning-of-line 2)
  8950. (org-indent-to-column indent)
  8951. (beginning-of-line 0)
  8952. (if hiddenp
  8953. (save-excursion
  8954. (org-back-to-heading t)
  8955. (hide-entry))
  8956. (org-flag-drawer t))))
  8957. (defun org-set-property (property value)
  8958. "In the current entry, set PROPERTY to VALUE.
  8959. When called interactively, this will prompt for a property name, offering
  8960. completion on existing and default properties. And then it will prompt
  8961. for a value, offering competion either on allowed values (via an inherited
  8962. xxx_ALL property) or on existing values in other instances of this property
  8963. in the current file."
  8964. (interactive
  8965. (let* ((prop (completing-read
  8966. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  8967. (cur (org-entry-get nil prop))
  8968. (allowed (org-property-get-allowed-values nil prop 'table))
  8969. (existing (mapcar 'list (org-property-values prop)))
  8970. (val (if allowed
  8971. (completing-read "Value: " allowed nil 'req-match)
  8972. (completing-read
  8973. (concat "Value" (if (and cur (string-match "\\S-" cur))
  8974. (concat "[" cur "]") "")
  8975. ": ")
  8976. existing nil nil "" nil cur))))
  8977. (list prop (if (equal val "") cur val))))
  8978. (unless (equal (org-entry-get nil property) value)
  8979. (org-entry-put nil property value)))
  8980. (defun org-delete-property (property)
  8981. "In the current entry, delete PROPERTY."
  8982. (interactive
  8983. (let* ((prop (completing-read
  8984. "Property: " (org-entry-properties nil 'standard))))
  8985. (list prop)))
  8986. (message "Property %s %s" property
  8987. (if (org-entry-delete nil property)
  8988. "deleted"
  8989. "was not present in the entry")))
  8990. (defun org-delete-property-globally (property)
  8991. "Remove PROPERTY globally, from all entries."
  8992. (interactive
  8993. (let* ((prop (completing-read
  8994. "Globally remove property: "
  8995. (mapcar 'list (org-buffer-property-keys)))))
  8996. (list prop)))
  8997. (save-excursion
  8998. (save-restriction
  8999. (widen)
  9000. (goto-char (point-min))
  9001. (let ((cnt 0))
  9002. (while (re-search-forward
  9003. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9004. nil t)
  9005. (setq cnt (1+ cnt))
  9006. (replace-match ""))
  9007. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9008. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9009. (defun org-compute-property-at-point ()
  9010. "Compute the property at point.
  9011. This looks for an enclosing column format, extracts the operator and
  9012. then applies it to the proerty in the column format's scope."
  9013. (interactive)
  9014. (unless (org-at-property-p)
  9015. (error "Not at a property"))
  9016. (let ((prop (org-match-string-no-properties 2)))
  9017. (org-columns-get-format-and-top-level)
  9018. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9019. (error "No operator defined for property %s" prop))
  9020. (org-columns-compute prop)))
  9021. (defun org-property-get-allowed-values (pom property &optional table)
  9022. "Get allowed values for the property PROPERTY.
  9023. When TABLE is non-nil, return an alist that can directly be used for
  9024. completion."
  9025. (let (vals)
  9026. (cond
  9027. ((equal property "TODO")
  9028. (setq vals (org-with-point-at pom
  9029. (append org-todo-keywords-1 '("")))))
  9030. ((equal property "PRIORITY")
  9031. (let ((n org-lowest-priority))
  9032. (while (>= n org-highest-priority)
  9033. (push (char-to-string n) vals)
  9034. (setq n (1- n)))))
  9035. ((member property org-special-properties))
  9036. (t
  9037. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9038. (when (and vals (string-match "\\S-" vals))
  9039. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9040. (setq vals (mapcar (lambda (x)
  9041. (cond ((stringp x) x)
  9042. ((numberp x) (number-to-string x))
  9043. ((symbolp x) (symbol-name x))
  9044. (t "???")))
  9045. vals)))))
  9046. (if table (mapcar 'list vals) vals)))
  9047. (defun org-property-previous-allowed-value (&optional previous)
  9048. "Switch to the next allowed value for this property."
  9049. (interactive)
  9050. (org-property-next-allowed-value t))
  9051. (defun org-property-next-allowed-value (&optional previous)
  9052. "Switch to the next allowed value for this property."
  9053. (interactive)
  9054. (unless (org-at-property-p)
  9055. (error "Not at a property"))
  9056. (let* ((key (match-string 2))
  9057. (value (match-string 3))
  9058. (allowed (or (org-property-get-allowed-values (point) key)
  9059. (and (member value '("[ ]" "[-]" "[X]"))
  9060. '("[ ]" "[X]"))))
  9061. nval)
  9062. (unless allowed
  9063. (error "Allowed values for this property have not been defined"))
  9064. (if previous (setq allowed (reverse allowed)))
  9065. (if (member value allowed)
  9066. (setq nval (car (cdr (member value allowed)))))
  9067. (setq nval (or nval (car allowed)))
  9068. (if (equal nval value)
  9069. (error "Only one allowed value for this property"))
  9070. (org-at-property-p)
  9071. (replace-match (concat " :" key ": " nval) t t)
  9072. (org-indent-line-function)
  9073. (beginning-of-line 1)
  9074. (skip-chars-forward " \t")))
  9075. (defun org-find-entry-with-id (ident)
  9076. "Locate the entry that contains the ID property with exact value IDENT.
  9077. IDENT can be a string, a symbol or a number, this function will search for
  9078. the string representation of it.
  9079. Return the position where this entry starts, or nil if there is no such entry."
  9080. (let ((id (cond
  9081. ((stringp ident) ident)
  9082. ((symbol-name ident) (symbol-name ident))
  9083. ((numberp ident) (number-to-string ident))
  9084. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9085. (case-fold-search nil))
  9086. (save-excursion
  9087. (save-restriction
  9088. (widen)
  9089. (goto-char (point-min))
  9090. (when (re-search-forward
  9091. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9092. nil t)
  9093. (org-back-to-heading)
  9094. (point))))))
  9095. ;;;; Timestamps
  9096. (defvar org-last-changed-timestamp nil)
  9097. (defvar org-time-was-given) ; dynamically scoped parameter
  9098. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9099. (defvar org-ts-what) ; dynamically scoped parameter
  9100. (defun org-time-stamp (arg)
  9101. "Prompt for a date/time and insert a time stamp.
  9102. If the user specifies a time like HH:MM, or if this command is called
  9103. with a prefix argument, the time stamp will contain date and time.
  9104. Otherwise, only the date will be included. All parts of a date not
  9105. specified by the user will be filled in from the current date/time.
  9106. So if you press just return without typing anything, the time stamp
  9107. will represent the current date/time. If there is already a timestamp
  9108. at the cursor, it will be modified."
  9109. (interactive "P")
  9110. (let* ((ts nil)
  9111. (default-time
  9112. ;; Default time is either today, or, when entering a range,
  9113. ;; the range start.
  9114. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9115. (save-excursion
  9116. (re-search-backward
  9117. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9118. (- (point) 20) t)))
  9119. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9120. (current-time)))
  9121. (default-input (and ts (org-get-compact-tod ts)))
  9122. org-time-was-given org-end-time-was-given time)
  9123. (cond
  9124. ((and (org-at-timestamp-p)
  9125. (eq last-command 'org-time-stamp)
  9126. (eq this-command 'org-time-stamp))
  9127. (insert "--")
  9128. (setq time (let ((this-command this-command))
  9129. (org-read-date arg 'totime nil nil default-time default-input)))
  9130. (org-insert-time-stamp time (or org-time-was-given arg)))
  9131. ((org-at-timestamp-p)
  9132. (setq time (let ((this-command this-command))
  9133. (org-read-date arg 'totime nil nil default-time default-input)))
  9134. (when (org-at-timestamp-p) ; just to get the match data
  9135. (replace-match "")
  9136. (setq org-last-changed-timestamp
  9137. (org-insert-time-stamp
  9138. time (or org-time-was-given arg)
  9139. nil nil nil (list org-end-time-was-given))))
  9140. (message "Timestamp updated"))
  9141. (t
  9142. (setq time (let ((this-command this-command))
  9143. (org-read-date arg 'totime nil nil default-time default-input)))
  9144. (org-insert-time-stamp time (or org-time-was-given arg)
  9145. nil nil nil (list org-end-time-was-given))))))
  9146. ;; FIXME: can we use this for something else, like computing time differences?
  9147. (defun org-get-compact-tod (s)
  9148. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9149. (let* ((t1 (match-string 1 s))
  9150. (h1 (string-to-number (match-string 2 s)))
  9151. (m1 (string-to-number (match-string 3 s)))
  9152. (t2 (and (match-end 4) (match-string 5 s)))
  9153. (h2 (and t2 (string-to-number (match-string 6 s))))
  9154. (m2 (and t2 (string-to-number (match-string 7 s))))
  9155. dh dm)
  9156. (if (not t2)
  9157. t1
  9158. (setq dh (- h2 h1) dm (- m2 m1))
  9159. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9160. (concat t1 "+" (number-to-string dh)
  9161. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9162. (defun org-time-stamp-inactive (&optional arg)
  9163. "Insert an inactive time stamp.
  9164. An inactive time stamp is enclosed in square brackets instead of angle
  9165. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9166. does not link to the calendar and cannot be changed with the S-cursor keys.
  9167. So these are more for recording a certain time/date."
  9168. (interactive "P")
  9169. (let (org-time-was-given org-end-time-was-given time)
  9170. (setq time (org-read-date arg 'totime))
  9171. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9172. nil nil (list org-end-time-was-given))))
  9173. (defvar org-date-ovl (org-make-overlay 1 1))
  9174. (org-overlay-put org-date-ovl 'face 'org-warning)
  9175. (org-detach-overlay org-date-ovl)
  9176. (defvar org-ans1) ; dynamically scoped parameter
  9177. (defvar org-ans2) ; dynamically scoped parameter
  9178. (defvar org-plain-time-of-day-regexp) ; defined below
  9179. (defvar org-read-date-overlay nil)
  9180. (defvar org-dcst nil) ; dynamically scoped
  9181. (defun org-read-date (&optional with-time to-time from-string prompt
  9182. default-time default-input)
  9183. "Read a date, possibly a time, and make things smooth for the user.
  9184. The prompt will suggest to enter an ISO date, but you can also enter anything
  9185. which will at least partially be understood by `parse-time-string'.
  9186. Unrecognized parts of the date will default to the current day, month, year,
  9187. hour and minute. If this command is called to replace a timestamp at point,
  9188. of to enter the second timestamp of a range, the default time is taken from the
  9189. existing stamp. For example,
  9190. 3-2-5 --> 2003-02-05
  9191. feb 15 --> currentyear-02-15
  9192. sep 12 9 --> 2009-09-12
  9193. 12:45 --> today 12:45
  9194. 22 sept 0:34 --> currentyear-09-22 0:34
  9195. 12 --> currentyear-currentmonth-12
  9196. Fri --> nearest Friday (today or later)
  9197. etc.
  9198. Furthermore you can specify a relative date by giving, as the *first* thing
  9199. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9200. change in days weeks, months, years.
  9201. With a single plus or minus, the date is relative to today. With a double
  9202. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9203. +4d --> four days from today
  9204. +4 --> same as above
  9205. +2w --> two weeks from today
  9206. ++5 --> five days from default date
  9207. The function understands only English month and weekday abbreviations,
  9208. but this can be configured with the variables `parse-time-months' and
  9209. `parse-time-weekdays'.
  9210. While prompting, a calendar is popped up - you can also select the
  9211. date with the mouse (button 1). The calendar shows a period of three
  9212. months. To scroll it to other months, use the keys `>' and `<'.
  9213. If you don't like the calendar, turn it off with
  9214. \(setq org-read-date-popup-calendar nil)
  9215. With optional argument TO-TIME, the date will immediately be converted
  9216. to an internal time.
  9217. With an optional argument WITH-TIME, the prompt will suggest to also
  9218. insert a time. Note that when WITH-TIME is not set, you can still
  9219. enter a time, and this function will inform the calling routine about
  9220. this change. The calling routine may then choose to change the format
  9221. used to insert the time stamp into the buffer to include the time.
  9222. With optional argument FROM-STRING, read from this string instead from
  9223. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9224. the time/date that is used for everything that is not specified by the
  9225. user."
  9226. (require 'parse-time)
  9227. (let* ((org-time-stamp-rounding-minutes
  9228. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9229. (org-dcst org-display-custom-times)
  9230. (ct (org-current-time))
  9231. (def (or default-time ct))
  9232. (defdecode (decode-time def))
  9233. (dummy (progn
  9234. (when (< (nth 2 defdecode) org-extend-today-until)
  9235. (setcar (nthcdr 2 defdecode) -1)
  9236. (setcar (nthcdr 1 defdecode) 59)
  9237. (setq def (apply 'encode-time defdecode)
  9238. defdecode (decode-time def)))))
  9239. (calendar-move-hook nil)
  9240. (calendar-view-diary-initially-flag nil)
  9241. (view-diary-entries-initially nil)
  9242. (calendar-view-holidays-initially-flag nil)
  9243. (view-calendar-holidays-initially nil)
  9244. (timestr (format-time-string
  9245. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9246. (prompt (concat (if prompt (concat prompt " ") "")
  9247. (format "Date+time [%s]: " timestr)))
  9248. ans (org-ans0 "") org-ans1 org-ans2 final)
  9249. (cond
  9250. (from-string (setq ans from-string))
  9251. (org-read-date-popup-calendar
  9252. (save-excursion
  9253. (save-window-excursion
  9254. (calendar)
  9255. (calendar-forward-day (- (time-to-days def)
  9256. (calendar-absolute-from-gregorian
  9257. (calendar-current-date))))
  9258. (org-eval-in-calendar nil t)
  9259. (let* ((old-map (current-local-map))
  9260. (map (copy-keymap calendar-mode-map))
  9261. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9262. (org-defkey map (kbd "RET") 'org-calendar-select)
  9263. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9264. 'org-calendar-select-mouse)
  9265. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9266. 'org-calendar-select-mouse)
  9267. (org-defkey minibuffer-local-map [(meta shift left)]
  9268. (lambda () (interactive)
  9269. (org-eval-in-calendar '(calendar-backward-month 1))))
  9270. (org-defkey minibuffer-local-map [(meta shift right)]
  9271. (lambda () (interactive)
  9272. (org-eval-in-calendar '(calendar-forward-month 1))))
  9273. (org-defkey minibuffer-local-map [(meta shift up)]
  9274. (lambda () (interactive)
  9275. (org-eval-in-calendar '(calendar-backward-year 1))))
  9276. (org-defkey minibuffer-local-map [(meta shift down)]
  9277. (lambda () (interactive)
  9278. (org-eval-in-calendar '(calendar-forward-year 1))))
  9279. (org-defkey minibuffer-local-map [(shift up)]
  9280. (lambda () (interactive)
  9281. (org-eval-in-calendar '(calendar-backward-week 1))))
  9282. (org-defkey minibuffer-local-map [(shift down)]
  9283. (lambda () (interactive)
  9284. (org-eval-in-calendar '(calendar-forward-week 1))))
  9285. (org-defkey minibuffer-local-map [(shift left)]
  9286. (lambda () (interactive)
  9287. (org-eval-in-calendar '(calendar-backward-day 1))))
  9288. (org-defkey minibuffer-local-map [(shift right)]
  9289. (lambda () (interactive)
  9290. (org-eval-in-calendar '(calendar-forward-day 1))))
  9291. (org-defkey minibuffer-local-map ">"
  9292. (lambda () (interactive)
  9293. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9294. (org-defkey minibuffer-local-map "<"
  9295. (lambda () (interactive)
  9296. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9297. (unwind-protect
  9298. (progn
  9299. (use-local-map map)
  9300. (add-hook 'post-command-hook 'org-read-date-display)
  9301. (setq org-ans0 (read-string prompt default-input nil nil))
  9302. ;; org-ans0: from prompt
  9303. ;; org-ans1: from mouse click
  9304. ;; org-ans2: from calendar motion
  9305. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9306. (remove-hook 'post-command-hook 'org-read-date-display)
  9307. (use-local-map old-map)
  9308. (when org-read-date-overlay
  9309. (org-delete-overlay org-read-date-overlay)
  9310. (setq org-read-date-overlay nil)))))))
  9311. (t ; Naked prompt only
  9312. (unwind-protect
  9313. (setq ans (read-string prompt default-input nil timestr))
  9314. (when org-read-date-overlay
  9315. (org-delete-overlay org-read-date-overlay)
  9316. (setq org-read-date-overlay nil)))))
  9317. (setq final (org-read-date-analyze ans def defdecode))
  9318. (if to-time
  9319. (apply 'encode-time final)
  9320. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9321. (format "%04d-%02d-%02d %02d:%02d"
  9322. (nth 5 final) (nth 4 final) (nth 3 final)
  9323. (nth 2 final) (nth 1 final))
  9324. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9325. (defvar def)
  9326. (defvar defdecode)
  9327. (defvar with-time)
  9328. (defun org-read-date-display ()
  9329. "Display the currrent date prompt interpretation in the minibuffer."
  9330. (when org-read-date-display-live
  9331. (when org-read-date-overlay
  9332. (org-delete-overlay org-read-date-overlay))
  9333. (let ((p (point)))
  9334. (end-of-line 1)
  9335. (while (not (equal (buffer-substring
  9336. (max (point-min) (- (point) 4)) (point))
  9337. " "))
  9338. (insert " "))
  9339. (goto-char p))
  9340. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9341. " " (or org-ans1 org-ans2)))
  9342. (org-end-time-was-given nil)
  9343. (f (org-read-date-analyze ans def defdecode))
  9344. (fmts (if org-dcst
  9345. org-time-stamp-custom-formats
  9346. org-time-stamp-formats))
  9347. (fmt (if (or with-time
  9348. (and (boundp 'org-time-was-given) org-time-was-given))
  9349. (cdr fmts)
  9350. (car fmts)))
  9351. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9352. (when (and org-end-time-was-given
  9353. (string-match org-plain-time-of-day-regexp txt))
  9354. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9355. org-end-time-was-given
  9356. (substring txt (match-end 0)))))
  9357. (setq org-read-date-overlay
  9358. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  9359. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9360. (defun org-read-date-analyze (ans def defdecode)
  9361. "Analyze the combined answer of the date prompt."
  9362. ;; FIXME: cleanup and comment
  9363. (let (delta deltan deltaw deltadef year month day
  9364. hour minute second wday pm h2 m2 tl wday1
  9365. iso-year iso-weekday iso-week iso-year iso-date)
  9366. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9367. (setq ans "+0"))
  9368. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9369. (setq ans (replace-match "" t t ans)
  9370. deltan (car delta)
  9371. deltaw (nth 1 delta)
  9372. deltadef (nth 2 delta)))
  9373. ;; Check if there is an iso week date in there
  9374. ;; If yes, sore the info and ostpone interpreting it until the rest
  9375. ;; of the parsing is done
  9376. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9377. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9378. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9379. iso-week (string-to-number (match-string 2 ans)))
  9380. (setq ans (replace-match "" t t ans)))
  9381. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9382. (when (string-match
  9383. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9384. (setq year (if (match-end 2)
  9385. (string-to-number (match-string 2 ans))
  9386. (string-to-number (format-time-string "%Y")))
  9387. month (string-to-number (match-string 3 ans))
  9388. day (string-to-number (match-string 4 ans)))
  9389. (if (< year 100) (setq year (+ 2000 year)))
  9390. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9391. t nil ans)))
  9392. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9393. ;; If there is a time with am/pm, and *no* time without it, we convert
  9394. ;; so that matching will be successful.
  9395. (loop for i from 1 to 2 do ; twice, for end time as well
  9396. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9397. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9398. (setq hour (string-to-number (match-string 1 ans))
  9399. minute (if (match-end 3)
  9400. (string-to-number (match-string 3 ans))
  9401. 0)
  9402. pm (equal ?p
  9403. (string-to-char (downcase (match-string 4 ans)))))
  9404. (if (and (= hour 12) (not pm))
  9405. (setq hour 0)
  9406. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9407. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9408. t t ans))))
  9409. ;; Check if a time range is given as a duration
  9410. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9411. (setq hour (string-to-number (match-string 1 ans))
  9412. h2 (+ hour (string-to-number (match-string 3 ans)))
  9413. minute (string-to-number (match-string 2 ans))
  9414. m2 (+ minute (if (match-end 5) (string-to-number
  9415. (match-string 5 ans))0)))
  9416. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9417. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9418. t t ans)))
  9419. ;; Check if there is a time range
  9420. (when (boundp 'org-end-time-was-given)
  9421. (setq org-time-was-given nil)
  9422. (when (and (string-match org-plain-time-of-day-regexp ans)
  9423. (match-end 8))
  9424. (setq org-end-time-was-given (match-string 8 ans))
  9425. (setq ans (concat (substring ans 0 (match-beginning 7))
  9426. (substring ans (match-end 7))))))
  9427. (setq tl (parse-time-string ans)
  9428. day (or (nth 3 tl) (nth 3 defdecode))
  9429. month (or (nth 4 tl)
  9430. (if (and org-read-date-prefer-future
  9431. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9432. (1+ (nth 4 defdecode))
  9433. (nth 4 defdecode)))
  9434. year (or (nth 5 tl)
  9435. (if (and org-read-date-prefer-future
  9436. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9437. (1+ (nth 5 defdecode))
  9438. (nth 5 defdecode)))
  9439. hour (or (nth 2 tl) (nth 2 defdecode))
  9440. minute (or (nth 1 tl) (nth 1 defdecode))
  9441. second (or (nth 0 tl) 0)
  9442. wday (nth 6 tl))
  9443. ;; Special date definitions below
  9444. (cond
  9445. (iso-week
  9446. ;; There was an iso week
  9447. (setq year (or iso-year year)
  9448. day (or iso-weekday wday 1)
  9449. wday nil ; to make sure that the trigger below does not match
  9450. iso-date (calendar-gregorian-from-absolute
  9451. (calendar-absolute-from-iso
  9452. (list iso-week day year))))
  9453. ; FIXME: Should we also push ISO weeks into the future?
  9454. ; (when (and org-read-date-prefer-future
  9455. ; (not iso-year)
  9456. ; (< (calendar-absolute-from-gregorian iso-date)
  9457. ; (time-to-days (current-time))))
  9458. ; (setq year (1+ year)
  9459. ; iso-date (calendar-gregorian-from-absolute
  9460. ; (calendar-absolute-from-iso
  9461. ; (list iso-week day year)))))
  9462. (setq month (car iso-date)
  9463. year (nth 2 iso-date)
  9464. day (nth 1 iso-date)))
  9465. (deltan
  9466. (unless deltadef
  9467. (let ((now (decode-time (current-time))))
  9468. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9469. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9470. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9471. ((equal deltaw "m") (setq month (+ month deltan)))
  9472. ((equal deltaw "y") (setq year (+ year deltan)))))
  9473. ((and wday (not (nth 3 tl)))
  9474. ;; Weekday was given, but no day, so pick that day in the week
  9475. ;; on or after the derived date.
  9476. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9477. (unless (equal wday wday1)
  9478. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9479. (if (and (boundp 'org-time-was-given)
  9480. (nth 2 tl))
  9481. (setq org-time-was-given t))
  9482. (if (< year 100) (setq year (+ 2000 year)))
  9483. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9484. (list second minute hour day month year)))
  9485. (defvar parse-time-weekdays)
  9486. (defun org-read-date-get-relative (s today default)
  9487. "Check string S for special relative date string.
  9488. TODAY and DEFAULT are internal times, for today and for a default.
  9489. Return shift list (N what def-flag)
  9490. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9491. N is the number of WHATs to shift.
  9492. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9493. the DEFAULT date rather than TODAY."
  9494. (when (string-match
  9495. (concat
  9496. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  9497. "\\([0-9]+\\)?"
  9498. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9499. "\\([ \t]\\|$\\)") s)
  9500. (let* ((dir (if (match-end 1)
  9501. (string-to-char (substring (match-string 1 s) -1))
  9502. ?+))
  9503. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9504. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9505. (what (if (match-end 3) (match-string 3 s) "d"))
  9506. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9507. (date (if rel default today))
  9508. (wday (nth 6 (decode-time date)))
  9509. delta)
  9510. (if wday1
  9511. (progn
  9512. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9513. (if (= dir ?-) (setq delta (- delta 7)))
  9514. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9515. (list delta "d" rel))
  9516. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9517. (defun org-eval-in-calendar (form &optional keepdate)
  9518. "Eval FORM in the calendar window and return to current window.
  9519. Also, store the cursor date in variable org-ans2."
  9520. (let ((sw (selected-window)))
  9521. (select-window (get-buffer-window "*Calendar*"))
  9522. (eval form)
  9523. (when (and (not keepdate) (calendar-cursor-to-date))
  9524. (let* ((date (calendar-cursor-to-date))
  9525. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9526. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9527. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9528. (select-window sw)))
  9529. ; ;; Update the prompt to show new default date
  9530. ; (save-excursion
  9531. ; (goto-char (point-min))
  9532. ; (when (and org-ans2
  9533. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  9534. ; (get-text-property (match-end 0) 'field))
  9535. ; (let ((inhibit-read-only t))
  9536. ; (replace-match (concat "[" org-ans2 "]") t t)
  9537. ; (add-text-properties (point-min) (1+ (match-end 0))
  9538. ; (text-properties-at (1+ (point-min)))))))))
  9539. (defun org-calendar-select ()
  9540. "Return to `org-read-date' with the date currently selected.
  9541. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9542. (interactive)
  9543. (when (calendar-cursor-to-date)
  9544. (let* ((date (calendar-cursor-to-date))
  9545. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9546. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9547. (if (active-minibuffer-window) (exit-minibuffer))))
  9548. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9549. "Insert a date stamp for the date given by the internal TIME.
  9550. WITH-HM means, use the stamp format that includes the time of the day.
  9551. INACTIVE means use square brackets instead of angular ones, so that the
  9552. stamp will not contribute to the agenda.
  9553. PRE and POST are optional strings to be inserted before and after the
  9554. stamp.
  9555. The command returns the inserted time stamp."
  9556. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9557. stamp)
  9558. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9559. (insert-before-markers (or pre ""))
  9560. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9561. (when (listp extra)
  9562. (setq extra (car extra))
  9563. (if (and (stringp extra)
  9564. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9565. (setq extra (format "-%02d:%02d"
  9566. (string-to-number (match-string 1 extra))
  9567. (string-to-number (match-string 2 extra))))
  9568. (setq extra nil)))
  9569. (when extra
  9570. (backward-char 1)
  9571. (insert-before-markers extra)
  9572. (forward-char 1))
  9573. (insert-before-markers (or post ""))
  9574. stamp))
  9575. (defun org-toggle-time-stamp-overlays ()
  9576. "Toggle the use of custom time stamp formats."
  9577. (interactive)
  9578. (setq org-display-custom-times (not org-display-custom-times))
  9579. (unless org-display-custom-times
  9580. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9581. (while (setq p (next-single-property-change p 'display))
  9582. (if (and (get-text-property p 'display)
  9583. (eq (get-text-property p 'face) 'org-date))
  9584. (remove-text-properties
  9585. p (setq p (next-single-property-change p 'display))
  9586. '(display t))))
  9587. (set-buffer-modified-p bmp)))
  9588. (if (featurep 'xemacs)
  9589. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9590. (org-restart-font-lock)
  9591. (setq org-table-may-need-update t)
  9592. (if org-display-custom-times
  9593. (message "Time stamps are overlayed with custom format")
  9594. (message "Time stamp overlays removed")))
  9595. (defun org-display-custom-time (beg end)
  9596. "Overlay modified time stamp format over timestamp between BEG and END."
  9597. (let* ((ts (buffer-substring beg end))
  9598. t1 w1 with-hm tf time str w2 (off 0))
  9599. (save-match-data
  9600. (setq t1 (org-parse-time-string ts t))
  9601. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9602. (setq off (- (match-end 0) (match-beginning 0)))))
  9603. (setq end (- end off))
  9604. (setq w1 (- end beg)
  9605. with-hm (and (nth 1 t1) (nth 2 t1))
  9606. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9607. time (org-fix-decoded-time t1)
  9608. str (org-add-props
  9609. (format-time-string
  9610. (substring tf 1 -1) (apply 'encode-time time))
  9611. nil 'mouse-face 'highlight)
  9612. w2 (length str))
  9613. (if (not (= w2 w1))
  9614. (add-text-properties (1+ beg) (+ 2 beg)
  9615. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9616. (if (featurep 'xemacs)
  9617. (progn
  9618. (put-text-property beg end 'invisible t)
  9619. (put-text-property beg end 'end-glyph (make-glyph str)))
  9620. (put-text-property beg end 'display str))))
  9621. (defun org-translate-time (string)
  9622. "Translate all timestamps in STRING to custom format.
  9623. But do this only if the variable `org-display-custom-times' is set."
  9624. (when org-display-custom-times
  9625. (save-match-data
  9626. (let* ((start 0)
  9627. (re org-ts-regexp-both)
  9628. t1 with-hm inactive tf time str beg end)
  9629. (while (setq start (string-match re string start))
  9630. (setq beg (match-beginning 0)
  9631. end (match-end 0)
  9632. t1 (save-match-data
  9633. (org-parse-time-string (substring string beg end) t))
  9634. with-hm (and (nth 1 t1) (nth 2 t1))
  9635. inactive (equal (substring string beg (1+ beg)) "[")
  9636. tf (funcall (if with-hm 'cdr 'car)
  9637. org-time-stamp-custom-formats)
  9638. time (org-fix-decoded-time t1)
  9639. str (format-time-string
  9640. (concat
  9641. (if inactive "[" "<") (substring tf 1 -1)
  9642. (if inactive "]" ">"))
  9643. (apply 'encode-time time))
  9644. string (replace-match str t t string)
  9645. start (+ start (length str)))))))
  9646. string)
  9647. (defun org-fix-decoded-time (time)
  9648. "Set 0 instead of nil for the first 6 elements of time.
  9649. Don't touch the rest."
  9650. (let ((n 0))
  9651. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9652. (defun org-days-to-time (timestamp-string)
  9653. "Difference between TIMESTAMP-STRING and now in days."
  9654. (- (time-to-days (org-time-string-to-time timestamp-string))
  9655. (time-to-days (current-time))))
  9656. (defun org-deadline-close (timestamp-string &optional ndays)
  9657. "Is the time in TIMESTAMP-STRING close to the current date?"
  9658. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9659. (and (< (org-days-to-time timestamp-string) ndays)
  9660. (not (org-entry-is-done-p))))
  9661. (defun org-get-wdays (ts)
  9662. "Get the deadline lead time appropriate for timestring TS."
  9663. (cond
  9664. ((<= org-deadline-warning-days 0)
  9665. ;; 0 or negative, enforce this value no matter what
  9666. (- org-deadline-warning-days))
  9667. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9668. ;; lead time is specified.
  9669. (floor (* (string-to-number (match-string 1 ts))
  9670. (cdr (assoc (match-string 2 ts)
  9671. '(("d" . 1) ("w" . 7)
  9672. ("m" . 30.4) ("y" . 365.25)))))))
  9673. ;; go for the default.
  9674. (t org-deadline-warning-days)))
  9675. (defun org-calendar-select-mouse (ev)
  9676. "Return to `org-read-date' with the date currently selected.
  9677. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9678. (interactive "e")
  9679. (mouse-set-point ev)
  9680. (when (calendar-cursor-to-date)
  9681. (let* ((date (calendar-cursor-to-date))
  9682. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9683. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9684. (if (active-minibuffer-window) (exit-minibuffer))))
  9685. (defun org-check-deadlines (ndays)
  9686. "Check if there are any deadlines due or past due.
  9687. A deadline is considered due if it happens within `org-deadline-warning-days'
  9688. days from today's date. If the deadline appears in an entry marked DONE,
  9689. it is not shown. The prefix arg NDAYS can be used to test that many
  9690. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  9691. (interactive "P")
  9692. (let* ((org-warn-days
  9693. (cond
  9694. ((equal ndays '(4)) 100000)
  9695. (ndays (prefix-numeric-value ndays))
  9696. (t (abs org-deadline-warning-days))))
  9697. (case-fold-search nil)
  9698. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  9699. (callback
  9700. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  9701. (message "%d deadlines past-due or due within %d days"
  9702. (org-occur regexp nil callback)
  9703. org-warn-days)))
  9704. (defun org-check-before-date (date)
  9705. "Check if there are deadlines or scheduled entries before DATE."
  9706. (interactive (list (org-read-date)))
  9707. (let ((case-fold-search nil)
  9708. (regexp (concat "\\<\\(" org-deadline-string
  9709. "\\|" org-scheduled-string
  9710. "\\) *<\\([^>]+\\)>"))
  9711. (callback
  9712. (lambda () (time-less-p
  9713. (org-time-string-to-time (match-string 2))
  9714. (org-time-string-to-time date)))))
  9715. (message "%d entries before %s"
  9716. (org-occur regexp nil callback) date)))
  9717. (defun org-evaluate-time-range (&optional to-buffer)
  9718. "Evaluate a time range by computing the difference between start and end.
  9719. Normally the result is just printed in the echo area, but with prefix arg
  9720. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  9721. If the time range is actually in a table, the result is inserted into the
  9722. next column.
  9723. For time difference computation, a year is assumed to be exactly 365
  9724. days in order to avoid rounding problems."
  9725. (interactive "P")
  9726. (or
  9727. (org-clock-update-time-maybe)
  9728. (save-excursion
  9729. (unless (org-at-date-range-p t)
  9730. (goto-char (point-at-bol))
  9731. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  9732. (if (not (org-at-date-range-p t))
  9733. (error "Not at a time-stamp range, and none found in current line")))
  9734. (let* ((ts1 (match-string 1))
  9735. (ts2 (match-string 2))
  9736. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  9737. (match-end (match-end 0))
  9738. (time1 (org-time-string-to-time ts1))
  9739. (time2 (org-time-string-to-time ts2))
  9740. (t1 (time-to-seconds time1))
  9741. (t2 (time-to-seconds time2))
  9742. (diff (abs (- t2 t1)))
  9743. (negative (< (- t2 t1) 0))
  9744. ;; (ys (floor (* 365 24 60 60)))
  9745. (ds (* 24 60 60))
  9746. (hs (* 60 60))
  9747. (fy "%dy %dd %02d:%02d")
  9748. (fy1 "%dy %dd")
  9749. (fd "%dd %02d:%02d")
  9750. (fd1 "%dd")
  9751. (fh "%02d:%02d")
  9752. y d h m align)
  9753. (if havetime
  9754. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9755. y 0
  9756. d (floor (/ diff ds)) diff (mod diff ds)
  9757. h (floor (/ diff hs)) diff (mod diff hs)
  9758. m (floor (/ diff 60)))
  9759. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9760. y 0
  9761. d (floor (+ (/ diff ds) 0.5))
  9762. h 0 m 0))
  9763. (if (not to-buffer)
  9764. (message "%s" (org-make-tdiff-string y d h m))
  9765. (if (org-at-table-p)
  9766. (progn
  9767. (goto-char match-end)
  9768. (setq align t)
  9769. (and (looking-at " *|") (goto-char (match-end 0))))
  9770. (goto-char match-end))
  9771. (if (looking-at
  9772. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  9773. (replace-match ""))
  9774. (if negative (insert " -"))
  9775. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  9776. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  9777. (insert " " (format fh h m))))
  9778. (if align (org-table-align))
  9779. (message "Time difference inserted")))))
  9780. (defun org-make-tdiff-string (y d h m)
  9781. (let ((fmt "")
  9782. (l nil))
  9783. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  9784. l (push y l)))
  9785. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  9786. l (push d l)))
  9787. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  9788. l (push h l)))
  9789. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  9790. l (push m l)))
  9791. (apply 'format fmt (nreverse l))))
  9792. (defun org-time-string-to-time (s)
  9793. (apply 'encode-time (org-parse-time-string s)))
  9794. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  9795. "Convert a time stamp to an absolute day number.
  9796. If there is a specifyer for a cyclic time stamp, get the closest date to
  9797. DAYNR.
  9798. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  9799. (cond
  9800. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  9801. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  9802. daynr
  9803. (+ daynr 1000)))
  9804. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  9805. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  9806. (time-to-days (current-time))) (match-string 0 s)
  9807. prefer show-all))
  9808. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  9809. (defun org-days-to-iso-week (days)
  9810. "Return the iso week number."
  9811. (require 'cal-iso)
  9812. (car (calendar-iso-from-absolute days)))
  9813. (defun org-small-year-to-year (year)
  9814. "Convert 2-digit years into 4-digit years.
  9815. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  9816. The year 2000 cannot be abbreviated. Any year lager than 99
  9817. is retrned unchanged."
  9818. (if (< year 38)
  9819. (setq year (+ 2000 year))
  9820. (if (< year 100)
  9821. (setq year (+ 1900 year))))
  9822. year)
  9823. (defun org-time-from-absolute (d)
  9824. "Return the time corresponding to date D.
  9825. D may be an absolute day number, or a calendar-type list (month day year)."
  9826. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  9827. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  9828. (defun org-calendar-holiday ()
  9829. "List of holidays, for Diary display in Org-mode."
  9830. (require 'holidays)
  9831. (let ((hl (funcall
  9832. (if (fboundp 'calendar-check-holidays)
  9833. 'calendar-check-holidays 'check-calendar-holidays) date)))
  9834. (if hl (mapconcat 'identity hl "; "))))
  9835. (defun org-diary-sexp-entry (sexp entry date)
  9836. "Process a SEXP diary ENTRY for DATE."
  9837. (require 'diary-lib)
  9838. (let ((result (if calendar-debug-sexp
  9839. (let ((stack-trace-on-error t))
  9840. (eval (car (read-from-string sexp))))
  9841. (condition-case nil
  9842. (eval (car (read-from-string sexp)))
  9843. (error
  9844. (beep)
  9845. (message "Bad sexp at line %d in %s: %s"
  9846. (org-current-line)
  9847. (buffer-file-name) sexp)
  9848. (sleep-for 2))))))
  9849. (cond ((stringp result) result)
  9850. ((and (consp result)
  9851. (stringp (cdr result))) (cdr result))
  9852. (result entry)
  9853. (t nil))))
  9854. (defun org-diary-to-ical-string (frombuf)
  9855. "Get iCalendar entries from diary entries in buffer FROMBUF.
  9856. This uses the icalendar.el library."
  9857. (let* ((tmpdir (if (featurep 'xemacs)
  9858. (temp-directory)
  9859. temporary-file-directory))
  9860. (tmpfile (make-temp-name
  9861. (expand-file-name "orgics" tmpdir)))
  9862. buf rtn b e)
  9863. (save-excursion
  9864. (set-buffer frombuf)
  9865. (icalendar-export-region (point-min) (point-max) tmpfile)
  9866. (setq buf (find-buffer-visiting tmpfile))
  9867. (set-buffer buf)
  9868. (goto-char (point-min))
  9869. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  9870. (setq b (match-beginning 0)))
  9871. (goto-char (point-max))
  9872. (if (re-search-backward "^END:VEVENT" nil t)
  9873. (setq e (match-end 0)))
  9874. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  9875. (kill-buffer buf)
  9876. (kill-buffer frombuf)
  9877. (delete-file tmpfile)
  9878. rtn))
  9879. (defun org-closest-date (start current change prefer show-all)
  9880. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  9881. When PREFER is `past' return a date that is either CURRENT or past.
  9882. When PREFER is `future', return a date that is either CURRENT or future.
  9883. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  9884. ;; Make the proper lists from the dates
  9885. (catch 'exit
  9886. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  9887. dn dw sday cday n1 n2
  9888. d m y y1 y2 date1 date2 nmonths nm ny m2)
  9889. (setq start (org-date-to-gregorian start)
  9890. current (org-date-to-gregorian
  9891. (if show-all
  9892. current
  9893. (time-to-days (current-time))))
  9894. sday (calendar-absolute-from-gregorian start)
  9895. cday (calendar-absolute-from-gregorian current))
  9896. (if (<= cday sday) (throw 'exit sday))
  9897. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  9898. (setq dn (string-to-number (match-string 1 change))
  9899. dw (cdr (assoc (match-string 2 change) a1)))
  9900. (error "Invalid change specifyer: %s" change))
  9901. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  9902. (cond
  9903. ((eq dw 'day)
  9904. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  9905. n2 (+ n1 dn)))
  9906. ((eq dw 'year)
  9907. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  9908. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  9909. (setq date1 (list m d y1)
  9910. n1 (calendar-absolute-from-gregorian date1)
  9911. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  9912. n2 (calendar-absolute-from-gregorian date2)))
  9913. ((eq dw 'month)
  9914. ;; approx number of month between the tow dates
  9915. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  9916. ;; How often does dn fit in there?
  9917. (setq d (nth 1 start) m (car start) y (nth 2 start)
  9918. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  9919. m (+ m nm)
  9920. ny (floor (/ m 12))
  9921. y (+ y ny)
  9922. m (- m (* ny 12)))
  9923. (while (> m 12) (setq m (- m 12) y (1+ y)))
  9924. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  9925. (setq m2 (+ m dn) y2 y)
  9926. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  9927. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  9928. (while (< n2 cday)
  9929. (setq n1 n2 m m2 y y2)
  9930. (setq m2 (+ m dn) y2 y)
  9931. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  9932. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  9933. (if show-all
  9934. (cond
  9935. ((eq prefer 'past) n1)
  9936. ((eq prefer 'future) (if (= cday n1) n1 n2))
  9937. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  9938. (cond
  9939. ((eq prefer 'past) n1)
  9940. ((eq prefer 'future) (if (= cday n1) n1 n2))
  9941. (t (if (= cday n1) n1 n2)))))))
  9942. (defun org-date-to-gregorian (date)
  9943. "Turn any specification of DATE into a gregorian date for the calendar."
  9944. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  9945. ((and (listp date) (= (length date) 3)) date)
  9946. ((stringp date)
  9947. (setq date (org-parse-time-string date))
  9948. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  9949. ((listp date)
  9950. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  9951. (defun org-parse-time-string (s &optional nodefault)
  9952. "Parse the standard Org-mode time string.
  9953. This should be a lot faster than the normal `parse-time-string'.
  9954. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  9955. hour and minute fields will be nil if not given."
  9956. (if (string-match org-ts-regexp0 s)
  9957. (list 0
  9958. (if (or (match-beginning 8) (not nodefault))
  9959. (string-to-number (or (match-string 8 s) "0")))
  9960. (if (or (match-beginning 7) (not nodefault))
  9961. (string-to-number (or (match-string 7 s) "0")))
  9962. (string-to-number (match-string 4 s))
  9963. (string-to-number (match-string 3 s))
  9964. (string-to-number (match-string 2 s))
  9965. nil nil nil)
  9966. (make-list 9 0)))
  9967. (defun org-timestamp-up (&optional arg)
  9968. "Increase the date item at the cursor by one.
  9969. If the cursor is on the year, change the year. If it is on the month or
  9970. the day, change that.
  9971. With prefix ARG, change by that many units."
  9972. (interactive "p")
  9973. (org-timestamp-change (prefix-numeric-value arg)))
  9974. (defun org-timestamp-down (&optional arg)
  9975. "Decrease the date item at the cursor by one.
  9976. If the cursor is on the year, change the year. If it is on the month or
  9977. the day, change that.
  9978. With prefix ARG, change by that many units."
  9979. (interactive "p")
  9980. (org-timestamp-change (- (prefix-numeric-value arg))))
  9981. (defun org-timestamp-up-day (&optional arg)
  9982. "Increase the date in the time stamp by one day.
  9983. With prefix ARG, change that many days."
  9984. (interactive "p")
  9985. (if (and (not (org-at-timestamp-p t))
  9986. (org-on-heading-p))
  9987. (org-todo 'up)
  9988. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  9989. (defun org-timestamp-down-day (&optional arg)
  9990. "Decrease the date in the time stamp by one day.
  9991. With prefix ARG, change that many days."
  9992. (interactive "p")
  9993. (if (and (not (org-at-timestamp-p t))
  9994. (org-on-heading-p))
  9995. (org-todo 'down)
  9996. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  9997. (defun org-at-timestamp-p (&optional inactive-ok)
  9998. "Determine if the cursor is in or at a timestamp."
  9999. (interactive)
  10000. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10001. (pos (point))
  10002. (ans (or (looking-at tsr)
  10003. (save-excursion
  10004. (skip-chars-backward "^[<\n\r\t")
  10005. (if (> (point) (point-min)) (backward-char 1))
  10006. (and (looking-at tsr)
  10007. (> (- (match-end 0) pos) -1))))))
  10008. (and ans
  10009. (boundp 'org-ts-what)
  10010. (setq org-ts-what
  10011. (cond
  10012. ((= pos (match-beginning 0)) 'bracket)
  10013. ((= pos (1- (match-end 0))) 'bracket)
  10014. ((org-pos-in-match-range pos 2) 'year)
  10015. ((org-pos-in-match-range pos 3) 'month)
  10016. ((org-pos-in-match-range pos 7) 'hour)
  10017. ((org-pos-in-match-range pos 8) 'minute)
  10018. ((or (org-pos-in-match-range pos 4)
  10019. (org-pos-in-match-range pos 5)) 'day)
  10020. ((and (> pos (or (match-end 8) (match-end 5)))
  10021. (< pos (match-end 0)))
  10022. (- pos (or (match-end 8) (match-end 5))))
  10023. (t 'day))))
  10024. ans))
  10025. (defun org-toggle-timestamp-type ()
  10026. "Toggle the type (<active> or [inactive]) of a time stamp."
  10027. (interactive)
  10028. (when (org-at-timestamp-p t)
  10029. (save-excursion
  10030. (goto-char (match-beginning 0))
  10031. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10032. (goto-char (1- (match-end 0)))
  10033. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10034. (message "Timestamp is now %sactive"
  10035. (if (equal (char-before) ?>) "in" ""))))
  10036. (defun org-timestamp-change (n &optional what)
  10037. "Change the date in the time stamp at point.
  10038. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10039. `year', `minute', `second'. If WHAT is not given, the cursor position
  10040. in the timestamp determines what will be changed."
  10041. (let ((pos (point))
  10042. with-hm inactive
  10043. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10044. org-ts-what
  10045. extra rem
  10046. ts time time0)
  10047. (if (not (org-at-timestamp-p t))
  10048. (error "Not at a timestamp"))
  10049. (if (and (not what) (eq org-ts-what 'bracket))
  10050. (org-toggle-timestamp-type)
  10051. (if (and (not what) (not (eq org-ts-what 'day))
  10052. org-display-custom-times
  10053. (get-text-property (point) 'display)
  10054. (not (get-text-property (1- (point)) 'display)))
  10055. (setq org-ts-what 'day))
  10056. (setq org-ts-what (or what org-ts-what)
  10057. inactive (= (char-after (match-beginning 0)) ?\[)
  10058. ts (match-string 0))
  10059. (replace-match "")
  10060. (if (string-match
  10061. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10062. ts)
  10063. (setq extra (match-string 1 ts)))
  10064. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10065. (setq with-hm t))
  10066. (setq time0 (org-parse-time-string ts))
  10067. (when (and (eq org-ts-what 'minute)
  10068. (eq current-prefix-arg nil))
  10069. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10070. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10071. (setcar (cdr time0) (+ (nth 1 time0)
  10072. (if (> n 0) (- rem) (- dm rem))))))
  10073. (setq time
  10074. (encode-time (or (car time0) 0)
  10075. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10076. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10077. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10078. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10079. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10080. (nthcdr 6 time0)))
  10081. (when (integerp org-ts-what)
  10082. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10083. (if (eq what 'calendar)
  10084. (let ((cal-date (org-get-date-from-calendar)))
  10085. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10086. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10087. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10088. (setcar time0 (or (car time0) 0))
  10089. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10090. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10091. (setq time (apply 'encode-time time0))))
  10092. (setq org-last-changed-timestamp
  10093. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10094. (org-clock-update-time-maybe)
  10095. (goto-char pos)
  10096. ;; Try to recenter the calendar window, if any
  10097. (if (and org-calendar-follow-timestamp-change
  10098. (get-buffer-window "*Calendar*" t)
  10099. (memq org-ts-what '(day month year)))
  10100. (org-recenter-calendar (time-to-days time))))))
  10101. (defun org-modify-ts-extra (s pos n dm)
  10102. "Change the different parts of the lead-time and repeat fields in timestamp."
  10103. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10104. ng h m new rem)
  10105. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10106. (cond
  10107. ((or (org-pos-in-match-range pos 2)
  10108. (org-pos-in-match-range pos 3))
  10109. (setq m (string-to-number (match-string 3 s))
  10110. h (string-to-number (match-string 2 s)))
  10111. (if (org-pos-in-match-range pos 2)
  10112. (setq h (+ h n))
  10113. (setq n (* dm (org-no-warnings (signum n))))
  10114. (when (not (= 0 (setq rem (% m dm))))
  10115. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10116. (setq m (+ m n)))
  10117. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10118. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10119. (setq h (min 24 (max 0 h)))
  10120. (setq ng 1 new (format "-%02d:%02d" h m)))
  10121. ((org-pos-in-match-range pos 6)
  10122. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10123. ((org-pos-in-match-range pos 5)
  10124. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10125. ((org-pos-in-match-range pos 9)
  10126. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10127. ((org-pos-in-match-range pos 8)
  10128. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10129. (when ng
  10130. (setq s (concat
  10131. (substring s 0 (match-beginning ng))
  10132. new
  10133. (substring s (match-end ng))))))
  10134. s))
  10135. (defun org-recenter-calendar (date)
  10136. "If the calendar is visible, recenter it to DATE."
  10137. (let* ((win (selected-window))
  10138. (cwin (get-buffer-window "*Calendar*" t))
  10139. (calendar-move-hook nil))
  10140. (when cwin
  10141. (select-window cwin)
  10142. (calendar-goto-date (if (listp date) date
  10143. (calendar-gregorian-from-absolute date)))
  10144. (select-window win))))
  10145. (defun org-goto-calendar (&optional arg)
  10146. "Go to the Emacs calendar at the current date.
  10147. If there is a time stamp in the current line, go to that date.
  10148. A prefix ARG can be used to force the current date."
  10149. (interactive "P")
  10150. (let ((tsr org-ts-regexp) diff
  10151. (calendar-move-hook nil)
  10152. (calendar-view-holidays-initially-flag nil)
  10153. (view-calendar-holidays-initially nil)
  10154. (calendar-view-diary-initially-flag nil)
  10155. (view-diary-entries-initially nil))
  10156. (if (or (org-at-timestamp-p)
  10157. (save-excursion
  10158. (beginning-of-line 1)
  10159. (looking-at (concat ".*" tsr))))
  10160. (let ((d1 (time-to-days (current-time)))
  10161. (d2 (time-to-days
  10162. (org-time-string-to-time (match-string 1)))))
  10163. (setq diff (- d2 d1))))
  10164. (calendar)
  10165. (calendar-goto-today)
  10166. (if (and diff (not arg)) (calendar-forward-day diff))))
  10167. (defun org-get-date-from-calendar ()
  10168. "Return a list (month day year) of date at point in calendar."
  10169. (with-current-buffer "*Calendar*"
  10170. (save-match-data
  10171. (calendar-cursor-to-date))))
  10172. (defun org-date-from-calendar ()
  10173. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10174. If there is already a time stamp at the cursor position, update it."
  10175. (interactive)
  10176. (if (org-at-timestamp-p t)
  10177. (org-timestamp-change 0 'calendar)
  10178. (let ((cal-date (org-get-date-from-calendar)))
  10179. (org-insert-time-stamp
  10180. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10181. (defun org-minutes-to-hh:mm-string (m)
  10182. "Compute H:MM from a number of minutes."
  10183. (let ((h (/ m 60)))
  10184. (setq m (- m (* 60 h)))
  10185. (format "%d:%02d" h m)))
  10186. (defun org-hh:mm-string-to-minutes (s)
  10187. "Convert a string H:MM to a number of minutes."
  10188. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10189. (+ (* (string-to-number (match-string 1 s)) 60)
  10190. (string-to-number (match-string 2 s)))
  10191. 0))
  10192. ;;;; Agenda files
  10193. ;;;###autoload
  10194. (defun org-iswitchb (&optional arg)
  10195. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10196. With a prefix argument, restrict available to files.
  10197. With two prefix arguments, restrict available buffers to agenda files.
  10198. Due to some yet unresolved reason, global function
  10199. `iswitchb-mode' needs to be active for this function to work."
  10200. (interactive "P")
  10201. (require 'iswitchb)
  10202. (let ((enabled iswitchb-mode) blist)
  10203. (or enabled (iswitchb-mode 1))
  10204. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10205. ((equal arg '(16)) (org-buffer-list 'agenda))
  10206. (t (org-buffer-list))))
  10207. (unwind-protect
  10208. (let ((iswitchb-make-buflist-hook
  10209. (lambda ()
  10210. (setq iswitchb-temp-buflist
  10211. (mapcar 'buffer-name blist)))))
  10212. (switch-to-buffer
  10213. (iswitchb-read-buffer
  10214. "Switch-to: " nil t))
  10215. (or enabled (iswitchb-mode -1))))))
  10216. (defun org-buffer-list (&optional predicate tmp)
  10217. "Return a list of Org buffers.
  10218. PREDICATE can be either 'export, 'files or 'agenda.
  10219. 'export restrict the list to Export buffers.
  10220. 'files restrict the list to buffers visiting Org files.
  10221. 'agenda restrict the list to buffers visiting agenda files.
  10222. If TMP is non-nil, don't include temporary buffers."
  10223. (let (filter blist)
  10224. (setq filter
  10225. (cond ((eq predicate 'files) "\.org$")
  10226. ((eq predicate 'export) "\*Org .*Export")
  10227. (t "\*Org \\|\.org$")))
  10228. (setq blist
  10229. (mapcar
  10230. (lambda(b)
  10231. (let ((bname (buffer-name b))
  10232. (bfile (buffer-file-name b)))
  10233. (if (and (string-match filter bname)
  10234. (if (eq predicate 'agenda)
  10235. (member bfile
  10236. (mapcar (lambda(f) (file-truename f))
  10237. org-agenda-files)) t)
  10238. (if tmp (not (string-match "tmp" bname)) t)) b)))
  10239. (buffer-list)))
  10240. (delete nil blist)))
  10241. (defun org-agenda-files (&optional unrestricted ext)
  10242. "Get the list of agenda files.
  10243. Optional UNRESTRICTED means return the full list even if a restriction
  10244. is currently in place.
  10245. When EXT is non-nil, try to add all files that are created by adding EXT
  10246. to the file nemes. Basically, this is a way to add the archive files
  10247. to the list, by setting EXT to \"_archive\" If EXT is non-nil, but not
  10248. a string, \"_archive\" will be used."
  10249. (let ((files
  10250. (cond
  10251. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10252. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10253. ((listp org-agenda-files) org-agenda-files)
  10254. (t (error "Invalid value of `org-agenda-files'")))))
  10255. (setq files (apply 'append
  10256. (mapcar (lambda (f)
  10257. (if (file-directory-p f)
  10258. (directory-files
  10259. f t org-agenda-file-regexp)
  10260. (list f)))
  10261. files)))
  10262. (when org-agenda-skip-unavailable-files
  10263. (setq files (delq nil
  10264. (mapcar (function
  10265. (lambda (file)
  10266. (and (file-readable-p file) file)))
  10267. files))))
  10268. (when ext
  10269. (setq ext (if (and (stringp ext) (string-match "\\S-" ext))
  10270. ext "_archive"))
  10271. (setq files (apply 'append
  10272. (mapcar
  10273. (lambda (f)
  10274. (if (file-exists-p (concat f ext))
  10275. (list f (concat f ext))
  10276. (list f)))
  10277. files))))
  10278. files))
  10279. (defun org-edit-agenda-file-list ()
  10280. "Edit the list of agenda files.
  10281. Depending on setup, this either uses customize to edit the variable
  10282. `org-agenda-files', or it visits the file that is holding the list. In the
  10283. latter case, the buffer is set up in a way that saving it automatically kills
  10284. the buffer and restores the previous window configuration."
  10285. (interactive)
  10286. (if (stringp org-agenda-files)
  10287. (let ((cw (current-window-configuration)))
  10288. (find-file org-agenda-files)
  10289. (org-set-local 'org-window-configuration cw)
  10290. (org-add-hook 'after-save-hook
  10291. (lambda ()
  10292. (set-window-configuration
  10293. (prog1 org-window-configuration
  10294. (kill-buffer (current-buffer))))
  10295. (org-install-agenda-files-menu)
  10296. (message "New agenda file list installed"))
  10297. nil 'local)
  10298. (message "%s" (substitute-command-keys
  10299. "Edit list and finish with \\[save-buffer]")))
  10300. (customize-variable 'org-agenda-files)))
  10301. (defun org-store-new-agenda-file-list (list)
  10302. "Set new value for the agenda file list and save it correcly."
  10303. (if (stringp org-agenda-files)
  10304. (let ((f org-agenda-files) b)
  10305. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10306. (with-temp-file f
  10307. (insert (mapconcat 'identity list "\n") "\n")))
  10308. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10309. (setq org-agenda-files list)
  10310. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10311. (defun org-read-agenda-file-list ()
  10312. "Read the list of agenda files from a file."
  10313. (when (file-directory-p org-agenda-files)
  10314. (error "`org-agenda-files' cannot be a single directory"))
  10315. (when (stringp org-agenda-files)
  10316. (with-temp-buffer
  10317. (insert-file-contents org-agenda-files)
  10318. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10319. ;;;###autoload
  10320. (defun org-cycle-agenda-files ()
  10321. "Cycle through the files in `org-agenda-files'.
  10322. If the current buffer visits an agenda file, find the next one in the list.
  10323. If the current buffer does not, find the first agenda file."
  10324. (interactive)
  10325. (let* ((fs (org-agenda-files t))
  10326. (files (append fs (list (car fs))))
  10327. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10328. file)
  10329. (unless files (error "No agenda files"))
  10330. (catch 'exit
  10331. (while (setq file (pop files))
  10332. (if (equal (file-truename file) tcf)
  10333. (when (car files)
  10334. (find-file (car files))
  10335. (throw 'exit t))))
  10336. (find-file (car fs)))
  10337. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10338. (defun org-agenda-file-to-front (&optional to-end)
  10339. "Move/add the current file to the top of the agenda file list.
  10340. If the file is not present in the list, it is added to the front. If it is
  10341. present, it is moved there. With optional argument TO-END, add/move to the
  10342. end of the list."
  10343. (interactive "P")
  10344. (let ((org-agenda-skip-unavailable-files nil)
  10345. (file-alist (mapcar (lambda (x)
  10346. (cons (file-truename x) x))
  10347. (org-agenda-files t)))
  10348. (ctf (file-truename buffer-file-name))
  10349. x had)
  10350. (setq x (assoc ctf file-alist) had x)
  10351. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10352. (if to-end
  10353. (setq file-alist (append (delq x file-alist) (list x)))
  10354. (setq file-alist (cons x (delq x file-alist))))
  10355. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10356. (org-install-agenda-files-menu)
  10357. (message "File %s to %s of agenda file list"
  10358. (if had "moved" "added") (if to-end "end" "front"))))
  10359. (defun org-remove-file (&optional file)
  10360. "Remove current file from the list of files in variable `org-agenda-files'.
  10361. These are the files which are being checked for agenda entries.
  10362. Optional argument FILE means, use this file instead of the current."
  10363. (interactive)
  10364. (let* ((org-agenda-skip-unavailable-files nil)
  10365. (file (or file buffer-file-name))
  10366. (true-file (file-truename file))
  10367. (afile (abbreviate-file-name file))
  10368. (files (delq nil (mapcar
  10369. (lambda (x)
  10370. (if (equal true-file
  10371. (file-truename x))
  10372. nil x))
  10373. (org-agenda-files t)))))
  10374. (if (not (= (length files) (length (org-agenda-files t))))
  10375. (progn
  10376. (org-store-new-agenda-file-list files)
  10377. (org-install-agenda-files-menu)
  10378. (message "Removed file: %s" afile))
  10379. (message "File was not in list: %s (not removed)" afile))))
  10380. (defun org-file-menu-entry (file)
  10381. (vector file (list 'find-file file) t))
  10382. (defun org-check-agenda-file (file)
  10383. "Make sure FILE exists. If not, ask user what to do."
  10384. (when (not (file-exists-p file))
  10385. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10386. (abbreviate-file-name file))
  10387. (let ((r (downcase (read-char-exclusive))))
  10388. (cond
  10389. ((equal r ?r)
  10390. (org-remove-file file)
  10391. (throw 'nextfile t))
  10392. (t (error "Abort"))))))
  10393. (defun org-get-agenda-file-buffer (file)
  10394. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10395. it to the list of buffers which might be released later."
  10396. (let ((buf (org-find-base-buffer-visiting file)))
  10397. (if buf
  10398. buf ; just return it
  10399. ;; Make a new buffer and remember it
  10400. (setq buf (find-file-noselect file))
  10401. (if buf (push buf org-agenda-new-buffers))
  10402. buf)))
  10403. (defun org-release-buffers (blist)
  10404. "Release all buffers in list, asking the user for confirmation when needed.
  10405. When a buffer is unmodified, it is just killed. When modified, it is saved
  10406. \(if the user agrees) and then killed."
  10407. (let (buf file)
  10408. (while (setq buf (pop blist))
  10409. (setq file (buffer-file-name buf))
  10410. (when (and (buffer-modified-p buf)
  10411. file
  10412. (y-or-n-p (format "Save file %s? " file)))
  10413. (with-current-buffer buf (save-buffer)))
  10414. (kill-buffer buf))))
  10415. (defun org-prepare-agenda-buffers (files)
  10416. "Create buffers for all agenda files, protect archived trees and comments."
  10417. (interactive)
  10418. (let ((pa '(:org-archived t))
  10419. (pc '(:org-comment t))
  10420. (pall '(:org-archived t :org-comment t))
  10421. (inhibit-read-only t)
  10422. (rea (concat ":" org-archive-tag ":"))
  10423. bmp file re)
  10424. (save-excursion
  10425. (save-restriction
  10426. (while (setq file (pop files))
  10427. (if (bufferp file)
  10428. (set-buffer file)
  10429. (org-check-agenda-file file)
  10430. (set-buffer (org-get-agenda-file-buffer file)))
  10431. (widen)
  10432. (setq bmp (buffer-modified-p))
  10433. (org-refresh-category-properties)
  10434. (setq org-todo-keywords-for-agenda
  10435. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10436. (setq org-done-keywords-for-agenda
  10437. (append org-done-keywords-for-agenda org-done-keywords))
  10438. (save-excursion
  10439. (remove-text-properties (point-min) (point-max) pall)
  10440. (when org-agenda-skip-archived-trees
  10441. (goto-char (point-min))
  10442. (while (re-search-forward rea nil t)
  10443. (if (org-on-heading-p t)
  10444. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10445. (goto-char (point-min))
  10446. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10447. (while (re-search-forward re nil t)
  10448. (add-text-properties
  10449. (match-beginning 0) (org-end-of-subtree t) pc)))
  10450. (set-buffer-modified-p bmp))))))
  10451. ;;;; Embedded LaTeX
  10452. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10453. "Keymap for the minor `org-cdlatex-mode'.")
  10454. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10455. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10456. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10457. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10458. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10459. (defvar org-cdlatex-texmathp-advice-is-done nil
  10460. "Flag remembering if we have applied the advice to texmathp already.")
  10461. (define-minor-mode org-cdlatex-mode
  10462. "Toggle the minor `org-cdlatex-mode'.
  10463. This mode supports entering LaTeX environment and math in LaTeX fragments
  10464. in Org-mode.
  10465. \\{org-cdlatex-mode-map}"
  10466. nil " OCDL" nil
  10467. (when org-cdlatex-mode (require 'cdlatex))
  10468. (unless org-cdlatex-texmathp-advice-is-done
  10469. (setq org-cdlatex-texmathp-advice-is-done t)
  10470. (defadvice texmathp (around org-math-always-on activate)
  10471. "Always return t in org-mode buffers.
  10472. This is because we want to insert math symbols without dollars even outside
  10473. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10474. en embedded LaTeX fragement, let texmathp do its job.
  10475. \\[org-cdlatex-mode-map]"
  10476. (interactive)
  10477. (let (p)
  10478. (cond
  10479. ((not (org-mode-p)) ad-do-it)
  10480. ((eq this-command 'cdlatex-math-symbol)
  10481. (setq ad-return-value t
  10482. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10483. (t
  10484. (let ((p (org-inside-LaTeX-fragment-p)))
  10485. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10486. (setq ad-return-value t
  10487. texmathp-why '("Org-mode embedded math" . 0))
  10488. (if p ad-do-it)))))))))
  10489. (defun turn-on-org-cdlatex ()
  10490. "Unconditionally turn on `org-cdlatex-mode'."
  10491. (org-cdlatex-mode 1))
  10492. (defun org-inside-LaTeX-fragment-p ()
  10493. "Test if point is inside a LaTeX fragment.
  10494. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10495. sequence appearing also before point.
  10496. Even though the matchers for math are configurable, this function assumes
  10497. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10498. delimiters are skipped when they have been removed by customization.
  10499. The return value is nil, or a cons cell with the delimiter and
  10500. and the position of this delimiter.
  10501. This function does a reasonably good job, but can locally be fooled by
  10502. for example currency specifications. For example it will assume being in
  10503. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10504. fragments that are properly closed, but during editing, we have to live
  10505. with the uncertainty caused by missing closing delimiters. This function
  10506. looks only before point, not after."
  10507. (catch 'exit
  10508. (let ((pos (point))
  10509. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10510. (lim (progn
  10511. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10512. (point)))
  10513. dd-on str (start 0) m re)
  10514. (goto-char pos)
  10515. (when dodollar
  10516. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10517. re (nth 1 (assoc "$" org-latex-regexps)))
  10518. (while (string-match re str start)
  10519. (cond
  10520. ((= (match-end 0) (length str))
  10521. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10522. ((= (match-end 0) (- (length str) 5))
  10523. (throw 'exit nil))
  10524. (t (setq start (match-end 0))))))
  10525. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10526. (goto-char pos)
  10527. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10528. (and (match-beginning 2) (throw 'exit nil))
  10529. ;; count $$
  10530. (while (re-search-backward "\\$\\$" lim t)
  10531. (setq dd-on (not dd-on)))
  10532. (goto-char pos)
  10533. (if dd-on (cons "$$" m))))))
  10534. (defun org-try-cdlatex-tab ()
  10535. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10536. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10537. - inside a LaTeX fragment, or
  10538. - after the first word in a line, where an abbreviation expansion could
  10539. insert a LaTeX environment."
  10540. (when org-cdlatex-mode
  10541. (cond
  10542. ((save-excursion
  10543. (skip-chars-backward "a-zA-Z0-9*")
  10544. (skip-chars-backward " \t")
  10545. (bolp))
  10546. (cdlatex-tab) t)
  10547. ((org-inside-LaTeX-fragment-p)
  10548. (cdlatex-tab) t)
  10549. (t nil))))
  10550. (defun org-cdlatex-underscore-caret (&optional arg)
  10551. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10552. Revert to the normal definition outside of these fragments."
  10553. (interactive "P")
  10554. (if (org-inside-LaTeX-fragment-p)
  10555. (call-interactively 'cdlatex-sub-superscript)
  10556. (let (org-cdlatex-mode)
  10557. (call-interactively (key-binding (vector last-input-event))))))
  10558. (defun org-cdlatex-math-modify (&optional arg)
  10559. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10560. Revert to the normal definition outside of these fragments."
  10561. (interactive "P")
  10562. (if (org-inside-LaTeX-fragment-p)
  10563. (call-interactively 'cdlatex-math-modify)
  10564. (let (org-cdlatex-mode)
  10565. (call-interactively (key-binding (vector last-input-event))))))
  10566. (defvar org-latex-fragment-image-overlays nil
  10567. "List of overlays carrying the images of latex fragments.")
  10568. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10569. (defun org-remove-latex-fragment-image-overlays ()
  10570. "Remove all overlays with LaTeX fragment images in current buffer."
  10571. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10572. (setq org-latex-fragment-image-overlays nil))
  10573. (defun org-preview-latex-fragment (&optional subtree)
  10574. "Preview the LaTeX fragment at point, or all locally or globally.
  10575. If the cursor is in a LaTeX fragment, create the image and overlay
  10576. it over the source code. If there is no fragment at point, display
  10577. all fragments in the current text, from one headline to the next. With
  10578. prefix SUBTREE, display all fragments in the current subtree. With a
  10579. double prefix `C-u C-u', or when the cursor is before the first headline,
  10580. display all fragments in the buffer.
  10581. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10582. (interactive "P")
  10583. (org-remove-latex-fragment-image-overlays)
  10584. (save-excursion
  10585. (save-restriction
  10586. (let (beg end at msg)
  10587. (cond
  10588. ((or (equal subtree '(16))
  10589. (not (save-excursion
  10590. (re-search-backward (concat "^" outline-regexp) nil t))))
  10591. (setq beg (point-min) end (point-max)
  10592. msg "Creating images for buffer...%s"))
  10593. ((equal subtree '(4))
  10594. (org-back-to-heading)
  10595. (setq beg (point) end (org-end-of-subtree t)
  10596. msg "Creating images for subtree...%s"))
  10597. (t
  10598. (if (setq at (org-inside-LaTeX-fragment-p))
  10599. (goto-char (max (point-min) (- (cdr at) 2)))
  10600. (org-back-to-heading))
  10601. (setq beg (point) end (progn (outline-next-heading) (point))
  10602. msg (if at "Creating image...%s"
  10603. "Creating images for entry...%s"))))
  10604. (message msg "")
  10605. (narrow-to-region beg end)
  10606. (goto-char beg)
  10607. (org-format-latex
  10608. (concat "ltxpng/" (file-name-sans-extension
  10609. (file-name-nondirectory
  10610. buffer-file-name)))
  10611. default-directory 'overlays msg at 'forbuffer)
  10612. (message msg "done. Use `C-c C-c' to remove images.")))))
  10613. (defvar org-latex-regexps
  10614. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10615. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10616. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10617. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10618. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10619. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10620. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10621. "Regular expressions for matching embedded LaTeX.")
  10622. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10623. "Replace LaTeX fragments with links to an image, and produce images."
  10624. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10625. (let* ((prefixnodir (file-name-nondirectory prefix))
  10626. (absprefix (expand-file-name prefix dir))
  10627. (todir (file-name-directory absprefix))
  10628. (opt org-format-latex-options)
  10629. (matchers (plist-get opt :matchers))
  10630. (re-list org-latex-regexps)
  10631. (cnt 0) txt link beg end re e checkdir
  10632. m n block linkfile movefile ov)
  10633. ;; Check if there are old images files with this prefix, and remove them
  10634. (when (file-directory-p todir)
  10635. (mapc 'delete-file
  10636. (directory-files
  10637. todir 'full
  10638. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10639. ;; Check the different regular expressions
  10640. (while (setq e (pop re-list))
  10641. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10642. block (if (nth 3 e) "\n\n" ""))
  10643. (when (member m matchers)
  10644. (goto-char (point-min))
  10645. (while (re-search-forward re nil t)
  10646. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10647. (setq txt (match-string n)
  10648. beg (match-beginning n) end (match-end n)
  10649. cnt (1+ cnt)
  10650. linkfile (format "%s_%04d.png" prefix cnt)
  10651. movefile (format "%s_%04d.png" absprefix cnt)
  10652. link (concat block "[[file:" linkfile "]]" block))
  10653. (if msg (message msg cnt))
  10654. (goto-char beg)
  10655. (unless checkdir ; make sure the directory exists
  10656. (setq checkdir t)
  10657. (or (file-directory-p todir) (make-directory todir)))
  10658. (org-create-formula-image
  10659. txt movefile opt forbuffer)
  10660. (if overlays
  10661. (progn
  10662. (setq ov (org-make-overlay beg end))
  10663. (if (featurep 'xemacs)
  10664. (progn
  10665. (org-overlay-put ov 'invisible t)
  10666. (org-overlay-put
  10667. ov 'end-glyph
  10668. (make-glyph (vector 'png :file movefile))))
  10669. (org-overlay-put
  10670. ov 'display
  10671. (list 'image :type 'png :file movefile :ascent 'center)))
  10672. (push ov org-latex-fragment-image-overlays)
  10673. (goto-char end))
  10674. (delete-region beg end)
  10675. (insert link))))))))
  10676. ;; This function borrows from Ganesh Swami's latex2png.el
  10677. (defun org-create-formula-image (string tofile options buffer)
  10678. (let* ((tmpdir (if (featurep 'xemacs)
  10679. (temp-directory)
  10680. temporary-file-directory))
  10681. (texfilebase (make-temp-name
  10682. (expand-file-name "orgtex" tmpdir)))
  10683. (texfile (concat texfilebase ".tex"))
  10684. (dvifile (concat texfilebase ".dvi"))
  10685. (pngfile (concat texfilebase ".png"))
  10686. (fnh (if (featurep 'xemacs)
  10687. (font-height (get-face-font 'default))
  10688. (face-attribute 'default :height nil)))
  10689. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  10690. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  10691. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  10692. "Black"))
  10693. (bg (or (plist-get options (if buffer :background :html-background))
  10694. "Transparent")))
  10695. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  10696. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  10697. (with-temp-file texfile
  10698. (insert org-format-latex-header
  10699. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  10700. (let ((dir default-directory))
  10701. (condition-case nil
  10702. (progn
  10703. (cd tmpdir)
  10704. (call-process "latex" nil nil nil texfile))
  10705. (error nil))
  10706. (cd dir))
  10707. (if (not (file-exists-p dvifile))
  10708. (progn (message "Failed to create dvi file from %s" texfile) nil)
  10709. (call-process "dvipng" nil nil nil
  10710. "-E" "-fg" fg "-bg" bg
  10711. "-D" dpi
  10712. ;;"-x" scale "-y" scale
  10713. "-T" "tight"
  10714. "-o" pngfile
  10715. dvifile)
  10716. (if (not (file-exists-p pngfile))
  10717. (progn (message "Failed to create png file from %s" texfile) nil)
  10718. ;; Use the requested file name and clean up
  10719. (copy-file pngfile tofile 'replace)
  10720. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  10721. (delete-file (concat texfilebase e)))
  10722. pngfile))))
  10723. (defun org-dvipng-color (attr)
  10724. "Return an rgb color specification for dvipng."
  10725. (apply 'format "rgb %s %s %s"
  10726. (mapcar 'org-normalize-color
  10727. (color-values (face-attribute 'default attr nil)))))
  10728. (defun org-normalize-color (value)
  10729. "Return string to be used as color value for an RGB component."
  10730. (format "%g" (/ value 65535.0)))
  10731. ;;;; Key bindings
  10732. ;; Make `C-c C-x' a prefix key
  10733. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  10734. ;; TAB key with modifiers
  10735. (org-defkey org-mode-map "\C-i" 'org-cycle)
  10736. (org-defkey org-mode-map [(tab)] 'org-cycle)
  10737. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  10738. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  10739. (org-defkey org-mode-map "\M-\t" 'org-complete)
  10740. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  10741. ;; The following line is necessary under Suse GNU/Linux
  10742. (unless (featurep 'xemacs)
  10743. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  10744. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  10745. (define-key org-mode-map [backtab] 'org-shifttab)
  10746. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  10747. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  10748. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  10749. ;; Cursor keys with modifiers
  10750. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  10751. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  10752. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  10753. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  10754. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  10755. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  10756. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  10757. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  10758. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  10759. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  10760. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  10761. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  10762. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  10763. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  10764. ;;; Extra keys for tty access.
  10765. ;; We only set them when really needed because otherwise the
  10766. ;; menus don't show the simple keys
  10767. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  10768. (not window-system))
  10769. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  10770. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  10771. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  10772. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  10773. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  10774. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  10775. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  10776. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  10777. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  10778. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  10779. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  10780. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  10781. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  10782. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  10783. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  10784. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  10785. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  10786. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  10787. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  10788. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  10789. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  10790. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  10791. ;; All the other keys
  10792. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  10793. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  10794. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  10795. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  10796. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  10797. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  10798. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  10799. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  10800. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  10801. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  10802. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  10803. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  10804. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  10805. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  10806. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  10807. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  10808. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  10809. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  10810. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  10811. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  10812. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  10813. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  10814. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  10815. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  10816. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  10817. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  10818. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  10819. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  10820. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  10821. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  10822. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  10823. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  10824. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  10825. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  10826. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  10827. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  10828. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  10829. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  10830. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  10831. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  10832. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  10833. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  10834. (org-defkey org-mode-map "\C-c^" 'org-sort)
  10835. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  10836. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  10837. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  10838. (org-defkey org-mode-map "\C-m" 'org-return)
  10839. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  10840. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  10841. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  10842. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  10843. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  10844. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  10845. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  10846. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  10847. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  10848. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  10849. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  10850. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  10851. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  10852. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  10853. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  10854. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  10855. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  10856. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  10857. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  10858. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  10859. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  10860. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  10861. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  10862. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  10863. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  10864. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  10865. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  10866. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  10867. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  10868. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  10869. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  10870. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  10871. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  10872. (when (featurep 'xemacs)
  10873. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  10874. (defvar org-table-auto-blank-field) ; defined in org-table.el
  10875. (defun org-self-insert-command (N)
  10876. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10877. If the cursor is in a table looking at whitespace, the whitespace is
  10878. overwritten, and the table is not marked as requiring realignment."
  10879. (interactive "p")
  10880. (if (and (org-table-p)
  10881. (progn
  10882. ;; check if we blank the field, and if that triggers align
  10883. (and (featurep 'org-table) org-table-auto-blank-field
  10884. (member last-command
  10885. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  10886. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  10887. ;; got extra space, this field does not determine column width
  10888. (let (org-table-may-need-update) (org-table-blank-field))
  10889. ;; no extra space, this field may determine column width
  10890. (org-table-blank-field)))
  10891. t)
  10892. (eq N 1)
  10893. (looking-at "[^|\n]* |"))
  10894. (let (org-table-may-need-update)
  10895. (goto-char (1- (match-end 0)))
  10896. (delete-backward-char 1)
  10897. (goto-char (match-beginning 0))
  10898. (self-insert-command N))
  10899. (setq org-table-may-need-update t)
  10900. (self-insert-command N)
  10901. (org-fix-tags-on-the-fly)))
  10902. (defun org-fix-tags-on-the-fly ()
  10903. (when (and (equal (char-after (point-at-bol)) ?*)
  10904. (org-on-heading-p))
  10905. (org-align-tags-here org-tags-column)))
  10906. (defun org-delete-backward-char (N)
  10907. "Like `delete-backward-char', insert whitespace at field end in tables.
  10908. When deleting backwards, in tables this function will insert whitespace in
  10909. front of the next \"|\" separator, to keep the table aligned. The table will
  10910. still be marked for re-alignment if the field did fill the entire column,
  10911. because, in this case the deletion might narrow the column."
  10912. (interactive "p")
  10913. (if (and (org-table-p)
  10914. (eq N 1)
  10915. (string-match "|" (buffer-substring (point-at-bol) (point)))
  10916. (looking-at ".*?|"))
  10917. (let ((pos (point))
  10918. (noalign (looking-at "[^|\n\r]* |"))
  10919. (c org-table-may-need-update))
  10920. (backward-delete-char N)
  10921. (skip-chars-forward "^|")
  10922. (insert " ")
  10923. (goto-char (1- pos))
  10924. ;; noalign: if there were two spaces at the end, this field
  10925. ;; does not determine the width of the column.
  10926. (if noalign (setq org-table-may-need-update c)))
  10927. (backward-delete-char N)
  10928. (org-fix-tags-on-the-fly)))
  10929. (defun org-delete-char (N)
  10930. "Like `delete-char', but insert whitespace at field end in tables.
  10931. When deleting characters, in tables this function will insert whitespace in
  10932. front of the next \"|\" separator, to keep the table aligned. The table will
  10933. still be marked for re-alignment if the field did fill the entire column,
  10934. because, in this case the deletion might narrow the column."
  10935. (interactive "p")
  10936. (if (and (org-table-p)
  10937. (not (bolp))
  10938. (not (= (char-after) ?|))
  10939. (eq N 1))
  10940. (if (looking-at ".*?|")
  10941. (let ((pos (point))
  10942. (noalign (looking-at "[^|\n\r]* |"))
  10943. (c org-table-may-need-update))
  10944. (replace-match (concat
  10945. (substring (match-string 0) 1 -1)
  10946. " |"))
  10947. (goto-char pos)
  10948. ;; noalign: if there were two spaces at the end, this field
  10949. ;; does not determine the width of the column.
  10950. (if noalign (setq org-table-may-need-update c)))
  10951. (delete-char N))
  10952. (delete-char N)
  10953. (org-fix-tags-on-the-fly)))
  10954. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  10955. (put 'org-self-insert-command 'delete-selection t)
  10956. (put 'orgtbl-self-insert-command 'delete-selection t)
  10957. (put 'org-delete-char 'delete-selection 'supersede)
  10958. (put 'org-delete-backward-char 'delete-selection 'supersede)
  10959. ;; Make `flyspell-mode' delay after some commands
  10960. (put 'org-self-insert-command 'flyspell-delayed t)
  10961. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  10962. (put 'org-delete-char 'flyspell-delayed t)
  10963. (put 'org-delete-backward-char 'flyspell-delayed t)
  10964. ;; Make pabbrev-mode expand after org-mode commands
  10965. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  10966. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  10967. ;; How to do this: Measure non-white length of current string
  10968. ;; If equal to column width, we should realign.
  10969. (defun org-remap (map &rest commands)
  10970. "In MAP, remap the functions given in COMMANDS.
  10971. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  10972. (let (new old)
  10973. (while commands
  10974. (setq old (pop commands) new (pop commands))
  10975. (if (fboundp 'command-remapping)
  10976. (org-defkey map (vector 'remap old) new)
  10977. (substitute-key-definition old new map global-map)))))
  10978. (when (eq org-enable-table-editor 'optimized)
  10979. ;; If the user wants maximum table support, we need to hijack
  10980. ;; some standard editing functions
  10981. (org-remap org-mode-map
  10982. 'self-insert-command 'org-self-insert-command
  10983. 'delete-char 'org-delete-char
  10984. 'delete-backward-char 'org-delete-backward-char)
  10985. (org-defkey org-mode-map "|" 'org-force-self-insert))
  10986. (defun org-shiftcursor-error ()
  10987. "Throw an error because Shift-Cursor command was applied in wrong context."
  10988. (error "This command is active in special context like tables, headlines or timestamps"))
  10989. (defun org-shifttab (&optional arg)
  10990. "Global visibility cycling or move to previous table field.
  10991. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  10992. on context.
  10993. See the individual commands for more information."
  10994. (interactive "P")
  10995. (cond
  10996. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  10997. (arg (message "Content view to level: ")
  10998. (org-content (prefix-numeric-value arg))
  10999. (setq org-cycle-global-status 'overview))
  11000. (t (call-interactively 'org-global-cycle))))
  11001. (defun org-shiftmetaleft ()
  11002. "Promote subtree or delete table column.
  11003. Calls `org-promote-subtree', `org-outdent-item',
  11004. or `org-table-delete-column', depending on context.
  11005. See the individual commands for more information."
  11006. (interactive)
  11007. (cond
  11008. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11009. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11010. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11011. (t (org-shiftcursor-error))))
  11012. (defun org-shiftmetaright ()
  11013. "Demote subtree or insert table column.
  11014. Calls `org-demote-subtree', `org-indent-item',
  11015. or `org-table-insert-column', depending on context.
  11016. See the individual commands for more information."
  11017. (interactive)
  11018. (cond
  11019. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11020. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11021. ((org-at-item-p) (call-interactively 'org-indent-item))
  11022. (t (org-shiftcursor-error))))
  11023. (defun org-shiftmetaup (&optional arg)
  11024. "Move subtree up or kill table row.
  11025. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11026. `org-move-item-up' depending on context. See the individual commands
  11027. for more information."
  11028. (interactive "P")
  11029. (cond
  11030. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11031. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11032. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11033. (t (org-shiftcursor-error))))
  11034. (defun org-shiftmetadown (&optional arg)
  11035. "Move subtree down or insert table row.
  11036. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11037. `org-move-item-down', depending on context. See the individual
  11038. commands for more information."
  11039. (interactive "P")
  11040. (cond
  11041. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11042. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11043. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11044. (t (org-shiftcursor-error))))
  11045. (defun org-metaleft (&optional arg)
  11046. "Promote heading or move table column to left.
  11047. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11048. With no specific context, calls the Emacs default `backward-word'.
  11049. See the individual commands for more information."
  11050. (interactive "P")
  11051. (cond
  11052. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11053. ((or (org-on-heading-p) (org-region-active-p))
  11054. (call-interactively 'org-do-promote))
  11055. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11056. (t (call-interactively 'backward-word))))
  11057. (defun org-metaright (&optional arg)
  11058. "Demote subtree or move table column to right.
  11059. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11060. With no specific context, calls the Emacs default `forward-word'.
  11061. See the individual commands for more information."
  11062. (interactive "P")
  11063. (cond
  11064. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11065. ((or (org-on-heading-p) (org-region-active-p))
  11066. (call-interactively 'org-do-demote))
  11067. ((org-at-item-p) (call-interactively 'org-indent-item))
  11068. (t (call-interactively 'forward-word))))
  11069. (defun org-metaup (&optional arg)
  11070. "Move subtree up or move table row up.
  11071. Calls `org-move-subtree-up' or `org-table-move-row' or
  11072. `org-move-item-up', depending on context. See the individual commands
  11073. for more information."
  11074. (interactive "P")
  11075. (cond
  11076. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11077. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11078. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11079. (t (transpose-lines 1) (beginning-of-line -1))))
  11080. (defun org-metadown (&optional arg)
  11081. "Move subtree down or move table row down.
  11082. Calls `org-move-subtree-down' or `org-table-move-row' or
  11083. `org-move-item-down', depending on context. See the individual
  11084. commands for more information."
  11085. (interactive "P")
  11086. (cond
  11087. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11088. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11089. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11090. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11091. (defun org-shiftup (&optional arg)
  11092. "Increase item in timestamp or increase priority of current headline.
  11093. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11094. depending on context. See the individual commands for more information."
  11095. (interactive "P")
  11096. (cond
  11097. ((org-at-timestamp-p t)
  11098. (call-interactively (if org-edit-timestamp-down-means-later
  11099. 'org-timestamp-down 'org-timestamp-up)))
  11100. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11101. ((org-at-item-p) (call-interactively 'org-previous-item))
  11102. ((org-clocktable-try-shift 'up arg))
  11103. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11104. (defun org-shiftdown (&optional arg)
  11105. "Decrease item in timestamp or decrease priority of current headline.
  11106. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11107. depending on context. See the individual commands for more information."
  11108. (interactive "P")
  11109. (cond
  11110. ((org-at-timestamp-p t)
  11111. (call-interactively (if org-edit-timestamp-down-means-later
  11112. 'org-timestamp-up 'org-timestamp-down)))
  11113. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11114. ((org-clocktable-try-shift 'down arg))
  11115. (t (call-interactively 'org-next-item))))
  11116. (defun org-shiftright (&optional arg)
  11117. "Next TODO keyword or timestamp one day later, depending on context."
  11118. (interactive "P")
  11119. (cond
  11120. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11121. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11122. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11123. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11124. ((org-clocktable-try-shift 'right arg))
  11125. (t (org-shiftcursor-error))))
  11126. (defun org-shiftleft (&optional arg)
  11127. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11128. (interactive "P")
  11129. (cond
  11130. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11131. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11132. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11133. ((org-at-property-p)
  11134. (call-interactively 'org-property-previous-allowed-value))
  11135. ((org-clocktable-try-shift 'left arg))
  11136. (t (org-shiftcursor-error))))
  11137. (defun org-shiftcontrolright ()
  11138. "Switch to next TODO set."
  11139. (interactive)
  11140. (cond
  11141. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11142. (t (org-shiftcursor-error))))
  11143. (defun org-shiftcontrolleft ()
  11144. "Switch to previous TODO set."
  11145. (interactive)
  11146. (cond
  11147. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11148. (t (org-shiftcursor-error))))
  11149. (defun org-ctrl-c-ret ()
  11150. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11151. (interactive)
  11152. (cond
  11153. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11154. (t (call-interactively 'org-insert-heading))))
  11155. (defun org-copy-special ()
  11156. "Copy region in table or copy current subtree.
  11157. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11158. See the individual commands for more information."
  11159. (interactive)
  11160. (call-interactively
  11161. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11162. (defun org-cut-special ()
  11163. "Cut region in table or cut current subtree.
  11164. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11165. See the individual commands for more information."
  11166. (interactive)
  11167. (call-interactively
  11168. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11169. (defun org-paste-special (arg)
  11170. "Paste rectangular region into table, or past subtree relative to level.
  11171. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11172. See the individual commands for more information."
  11173. (interactive "P")
  11174. (if (org-at-table-p)
  11175. (org-table-paste-rectangle)
  11176. (org-paste-subtree arg)))
  11177. (defun org-ctrl-c-ctrl-c (&optional arg)
  11178. "Set tags in headline, or update according to changed information at point.
  11179. This command does many different things, depending on context:
  11180. - If the cursor is in a headline, prompt for tags and insert them
  11181. into the current line, aligned to `org-tags-column'. When called
  11182. with prefix arg, realign all tags in the current buffer.
  11183. - If the cursor is in one of the special #+KEYWORD lines, this
  11184. triggers scanning the buffer for these lines and updating the
  11185. information.
  11186. - If the cursor is inside a table, realign the table. This command
  11187. works even if the automatic table editor has been turned off.
  11188. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11189. the entire table.
  11190. - If the cursor is a the beginning of a dynamic block, update it.
  11191. - If the cursor is inside a table created by the table.el package,
  11192. activate that table.
  11193. - If the current buffer is a remember buffer, close note and file it.
  11194. with a prefix argument, file it without further interaction to the default
  11195. location.
  11196. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11197. links in this buffer.
  11198. - If the cursor is on a numbered item in a plain list, renumber the
  11199. ordered list.
  11200. - If the cursor is on a checkbox, toggle it."
  11201. (interactive "P")
  11202. (let ((org-enable-table-editor t))
  11203. (cond
  11204. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11205. org-occur-highlights
  11206. org-latex-fragment-image-overlays)
  11207. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11208. (org-remove-occur-highlights)
  11209. (org-remove-latex-fragment-image-overlays)
  11210. (message "Temporary highlights/overlays removed from current buffer"))
  11211. ((and (local-variable-p 'org-finish-function (current-buffer))
  11212. (fboundp org-finish-function))
  11213. (funcall org-finish-function))
  11214. ((org-at-property-p)
  11215. (call-interactively 'org-property-action))
  11216. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11217. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11218. ((org-at-table.el-p)
  11219. (require 'table)
  11220. (beginning-of-line 1)
  11221. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11222. (call-interactively 'table-recognize-table))
  11223. ((org-at-table-p)
  11224. (org-table-maybe-eval-formula)
  11225. (if arg
  11226. (call-interactively 'org-table-recalculate)
  11227. (org-table-maybe-recalculate-line))
  11228. (call-interactively 'org-table-align))
  11229. ((org-at-item-checkbox-p)
  11230. (call-interactively 'org-toggle-checkbox))
  11231. ((org-at-item-p)
  11232. (call-interactively 'org-maybe-renumber-ordered-list))
  11233. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11234. ;; Dynamic block
  11235. (beginning-of-line 1)
  11236. (org-update-dblock))
  11237. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11238. (cond
  11239. ((equal (match-string 1) "TBLFM")
  11240. ;; Recalculate the table before this line
  11241. (save-excursion
  11242. (beginning-of-line 1)
  11243. (skip-chars-backward " \r\n\t")
  11244. (if (org-at-table-p)
  11245. (org-call-with-arg 'org-table-recalculate t))))
  11246. (t
  11247. (call-interactively 'org-mode-restart))))
  11248. (t (error "C-c C-c can do nothing useful at this location.")))))
  11249. (defun org-mode-restart ()
  11250. "Restart Org-mode, to scan again for special lines.
  11251. Also updates the keyword regular expressions."
  11252. (interactive)
  11253. (let ((org-inhibit-startup t)) (org-mode))
  11254. (message "Org-mode restarted to refresh keyword and special line setup"))
  11255. (defun org-kill-note-or-show-branches ()
  11256. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11257. (interactive)
  11258. (if (not org-finish-function)
  11259. (call-interactively 'show-branches)
  11260. (let ((org-note-abort t))
  11261. (funcall org-finish-function))))
  11262. (defun org-return (&optional indent)
  11263. "Goto next table row or insert a newline.
  11264. Calls `org-table-next-row' or `newline', depending on context.
  11265. See the individual commands for more information."
  11266. (interactive)
  11267. (cond
  11268. ((bobp) (if indent (newline-and-indent) (newline)))
  11269. ((and (org-at-heading-p)
  11270. (looking-at
  11271. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11272. (org-show-entry)
  11273. (end-of-line 1)
  11274. (newline))
  11275. ((org-at-table-p)
  11276. (org-table-justify-field-maybe)
  11277. (call-interactively 'org-table-next-row))
  11278. (t (if indent (newline-and-indent) (newline)))))
  11279. (defun org-return-indent ()
  11280. "Goto next table row or insert a newline and indent.
  11281. Calls `org-table-next-row' or `newline-and-indent', depending on
  11282. context. See the individual commands for more information."
  11283. (interactive)
  11284. (org-return t))
  11285. (defun org-ctrl-c-star ()
  11286. "Compute table, or change heading status of lines.
  11287. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  11288. depending on context. This will also turn a plain list item or a normal
  11289. line into a subheading."
  11290. (interactive)
  11291. (cond
  11292. ((org-at-table-p)
  11293. (call-interactively 'org-table-recalculate))
  11294. ((org-region-active-p)
  11295. ;; Convert all lines in region to list items
  11296. (call-interactively 'org-toggle-region-headings))
  11297. ((org-on-heading-p)
  11298. (org-toggle-region-headings (point-at-bol)
  11299. (min (1+ (point-at-eol)) (point-max))))
  11300. ((org-at-item-p)
  11301. ;; Convert to heading
  11302. (let ((level (save-match-data
  11303. (save-excursion
  11304. (condition-case nil
  11305. (progn
  11306. (org-back-to-heading t)
  11307. (funcall outline-level))
  11308. (error 0))))))
  11309. (replace-match
  11310. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11311. (t (org-toggle-region-headings (point-at-bol)
  11312. (min (1+ (point-at-eol)) (point-max))))))
  11313. (defun org-ctrl-c-minus ()
  11314. "Insert separator line in table or modify bullet status of line.
  11315. Also turns a plain line or a region of lines into list items.
  11316. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11317. `org-cycle-list-bullet', depending on context."
  11318. (interactive)
  11319. (cond
  11320. ((org-at-table-p)
  11321. (call-interactively 'org-table-insert-hline))
  11322. ((org-on-heading-p)
  11323. ;; Convert to item
  11324. (save-excursion
  11325. (beginning-of-line 1)
  11326. (if (looking-at "\\*+ ")
  11327. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11328. ((org-region-active-p)
  11329. ;; Convert all lines in region to list items
  11330. (call-interactively 'org-toggle-region-items))
  11331. ((org-in-item-p)
  11332. (call-interactively 'org-cycle-list-bullet))
  11333. (t (org-toggle-region-items (point-at-bol)
  11334. (min (1+ (point-at-eol)) (point-max))))))
  11335. (defun org-toggle-region-items (beg end)
  11336. "Convert all lines in region to list items.
  11337. If the first line is already an item, convert all list items in the region
  11338. to normal lines."
  11339. (interactive "r")
  11340. (let (l2 l)
  11341. (save-excursion
  11342. (goto-char end)
  11343. (setq l2 (org-current-line))
  11344. (goto-char beg)
  11345. (beginning-of-line 1)
  11346. (setq l (1- (org-current-line)))
  11347. (if (org-at-item-p)
  11348. ;; We already have items, de-itemize
  11349. (while (< (setq l (1+ l)) l2)
  11350. (when (org-at-item-p)
  11351. (goto-char (match-beginning 2))
  11352. (delete-region (match-beginning 2) (match-end 2))
  11353. (and (looking-at "[ \t]+") (replace-match "")))
  11354. (beginning-of-line 2))
  11355. (while (< (setq l (1+ l)) l2)
  11356. (unless (org-at-item-p)
  11357. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11358. (replace-match "\\1- \\2")))
  11359. (beginning-of-line 2))))))
  11360. (defun org-toggle-region-headings (beg end)
  11361. "Convert all lines in region to list items.
  11362. If the first line is already an item, convert all list items in the region
  11363. to normal lines."
  11364. (interactive "r")
  11365. (let (l2 l)
  11366. (save-excursion
  11367. (goto-char end)
  11368. (setq l2 (org-current-line))
  11369. (goto-char beg)
  11370. (beginning-of-line 1)
  11371. (setq l (1- (org-current-line)))
  11372. (if (org-on-heading-p)
  11373. ;; We already have headlines, de-star them
  11374. (while (< (setq l (1+ l)) l2)
  11375. (when (org-on-heading-p t)
  11376. (and (looking-at outline-regexp) (replace-match "")))
  11377. (beginning-of-line 2))
  11378. (let* ((stars (save-excursion
  11379. (re-search-backward org-complex-heading-regexp nil t)
  11380. (or (match-string 1) "*")))
  11381. (add-stars (if org-odd-levels-only "**" "*"))
  11382. (rpl (concat stars add-stars " \\2")))
  11383. (while (< (setq l (1+ l)) l2)
  11384. (unless (org-on-heading-p)
  11385. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11386. (replace-match rpl)))
  11387. (beginning-of-line 2)))))))
  11388. (defun org-meta-return (&optional arg)
  11389. "Insert a new heading or wrap a region in a table.
  11390. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11391. See the individual commands for more information."
  11392. (interactive "P")
  11393. (cond
  11394. ((org-at-table-p)
  11395. (call-interactively 'org-table-wrap-region))
  11396. (t (call-interactively 'org-insert-heading))))
  11397. ;;; Menu entries
  11398. ;; Define the Org-mode menus
  11399. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11400. '("Tbl"
  11401. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11402. ["Next Field" org-cycle (org-at-table-p)]
  11403. ["Previous Field" org-shifttab (org-at-table-p)]
  11404. ["Next Row" org-return (org-at-table-p)]
  11405. "--"
  11406. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11407. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11408. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11409. "--"
  11410. ("Column"
  11411. ["Move Column Left" org-metaleft (org-at-table-p)]
  11412. ["Move Column Right" org-metaright (org-at-table-p)]
  11413. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11414. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11415. ("Row"
  11416. ["Move Row Up" org-metaup (org-at-table-p)]
  11417. ["Move Row Down" org-metadown (org-at-table-p)]
  11418. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11419. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11420. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11421. "--"
  11422. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11423. ("Rectangle"
  11424. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11425. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11426. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11427. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11428. "--"
  11429. ("Calculate"
  11430. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11431. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11432. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  11433. "--"
  11434. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11435. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11436. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11437. "--"
  11438. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11439. "--"
  11440. ["Sum Column/Rectangle" org-table-sum
  11441. (or (org-at-table-p) (org-region-active-p))]
  11442. ["Which Column?" org-table-current-column (org-at-table-p)])
  11443. ["Debug Formulas"
  11444. org-table-toggle-formula-debugger
  11445. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11446. ["Show Col/Row Numbers"
  11447. org-table-toggle-coordinate-overlays
  11448. :style toggle
  11449. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11450. "--"
  11451. ["Create" org-table-create (and (not (org-at-table-p))
  11452. org-enable-table-editor)]
  11453. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11454. ["Import from File" org-table-import (not (org-at-table-p))]
  11455. ["Export to File" org-table-export (org-at-table-p)]
  11456. "--"
  11457. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11458. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11459. '("Org"
  11460. ("Show/Hide"
  11461. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11462. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11463. ["Sparse Tree..." org-sparse-tree t]
  11464. ["Reveal Context" org-reveal t]
  11465. ["Show All" show-all t]
  11466. "--"
  11467. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11468. "--"
  11469. ["New Heading" org-insert-heading t]
  11470. ("Navigate Headings"
  11471. ["Up" outline-up-heading t]
  11472. ["Next" outline-next-visible-heading t]
  11473. ["Previous" outline-previous-visible-heading t]
  11474. ["Next Same Level" outline-forward-same-level t]
  11475. ["Previous Same Level" outline-backward-same-level t]
  11476. "--"
  11477. ["Jump" org-goto t])
  11478. ("Edit Structure"
  11479. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11480. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11481. "--"
  11482. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11483. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11484. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11485. "--"
  11486. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11487. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11488. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11489. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11490. "--"
  11491. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11492. "--"
  11493. ["Convert to odd levels" org-convert-to-odd-levels t]
  11494. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11495. ("Editing"
  11496. ["Emphasis..." org-emphasize t])
  11497. ("Archive"
  11498. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11499. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11500. ; :active t :keys "C-u C-c C-x C-a"]
  11501. ["Sparse trees open ARCHIVE trees"
  11502. (setq org-sparse-tree-open-archived-trees
  11503. (not org-sparse-tree-open-archived-trees))
  11504. :style toggle :selected org-sparse-tree-open-archived-trees]
  11505. ["Cycling opens ARCHIVE trees"
  11506. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11507. :style toggle :selected org-cycle-open-archived-trees]
  11508. ["Agenda includes ARCHIVE trees"
  11509. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  11510. :style toggle :selected (not org-agenda-skip-archived-trees)]
  11511. "--"
  11512. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11513. ; ["Check and Move Children" (org-archive-subtree '(4))
  11514. ; :active t :keys "C-u C-c C-x C-s"]
  11515. )
  11516. "--"
  11517. ("TODO Lists"
  11518. ["TODO/DONE/-" org-todo t]
  11519. ("Select keyword"
  11520. ["Next keyword" org-shiftright (org-on-heading-p)]
  11521. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11522. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11523. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11524. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11525. ["Show TODO Tree" org-show-todo-tree t]
  11526. ["Global TODO list" org-todo-list t]
  11527. "--"
  11528. ["Set Priority" org-priority t]
  11529. ["Priority Up" org-shiftup t]
  11530. ["Priority Down" org-shiftdown t])
  11531. ("TAGS and Properties"
  11532. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  11533. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11534. "--"
  11535. ["Set property" 'org-set-property t]
  11536. ["Column view of properties" org-columns t]
  11537. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11538. ("Dates and Scheduling"
  11539. ["Timestamp" org-time-stamp t]
  11540. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11541. ("Change Date"
  11542. ["1 Day Later" org-shiftright t]
  11543. ["1 Day Earlier" org-shiftleft t]
  11544. ["1 ... Later" org-shiftup t]
  11545. ["1 ... Earlier" org-shiftdown t])
  11546. ["Compute Time Range" org-evaluate-time-range t]
  11547. ["Schedule Item" org-schedule t]
  11548. ["Deadline" org-deadline t]
  11549. "--"
  11550. ["Custom time format" org-toggle-time-stamp-overlays
  11551. :style radio :selected org-display-custom-times]
  11552. "--"
  11553. ["Goto Calendar" org-goto-calendar t]
  11554. ["Date from Calendar" org-date-from-calendar t])
  11555. ("Logging work"
  11556. ["Clock in" org-clock-in t]
  11557. ["Clock out" org-clock-out t]
  11558. ["Clock cancel" org-clock-cancel t]
  11559. ["Goto running clock" org-clock-goto t]
  11560. ["Display times" org-clock-display t]
  11561. ["Create clock table" org-clock-report t]
  11562. "--"
  11563. ["Record DONE time"
  11564. (progn (setq org-log-done (not org-log-done))
  11565. (message "Switching to %s will %s record a timestamp"
  11566. (car org-done-keywords)
  11567. (if org-log-done "automatically" "not")))
  11568. :style toggle :selected org-log-done])
  11569. "--"
  11570. ["Agenda Command..." org-agenda t]
  11571. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11572. ("File List for Agenda")
  11573. ("Special views current file"
  11574. ["TODO Tree" org-show-todo-tree t]
  11575. ["Check Deadlines" org-check-deadlines t]
  11576. ["Timeline" org-timeline t]
  11577. ["Tags Tree" org-tags-sparse-tree t])
  11578. "--"
  11579. ("Hyperlinks"
  11580. ["Store Link (Global)" org-store-link t]
  11581. ["Insert Link" org-insert-link t]
  11582. ["Follow Link" org-open-at-point t]
  11583. "--"
  11584. ["Next link" org-next-link t]
  11585. ["Previous link" org-previous-link t]
  11586. "--"
  11587. ["Descriptive Links"
  11588. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11589. :style radio
  11590. :selected (member '(org-link) buffer-invisibility-spec)]
  11591. ["Literal Links"
  11592. (progn
  11593. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11594. :style radio
  11595. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11596. "--"
  11597. ["Export/Publish..." org-export t]
  11598. ("LaTeX"
  11599. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11600. :selected org-cdlatex-mode]
  11601. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11602. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11603. ["Modify math symbol" org-cdlatex-math-modify
  11604. (org-inside-LaTeX-fragment-p)]
  11605. ["Export LaTeX fragments as images"
  11606. (if (featurep 'org-exp)
  11607. (setq org-export-with-LaTeX-fragments
  11608. (not org-export-with-LaTeX-fragments))
  11609. (require 'org-exp))
  11610. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11611. org-export-with-LaTeX-fragments)])
  11612. "--"
  11613. ("Documentation"
  11614. ["Show Version" org-version t]
  11615. ["Info Documentation" org-info t])
  11616. ("Customize"
  11617. ["Browse Org Group" org-customize t]
  11618. "--"
  11619. ["Expand This Menu" org-create-customize-menu
  11620. (fboundp 'customize-menu-create)])
  11621. "--"
  11622. ["Refresh setup" org-mode-restart t]
  11623. ))
  11624. (defun org-info (&optional node)
  11625. "Read documentation for Org-mode in the info system.
  11626. With optional NODE, go directly to that node."
  11627. (interactive)
  11628. (info (format "(org)%s" (or node ""))))
  11629. (defun org-install-agenda-files-menu ()
  11630. (let ((bl (buffer-list)))
  11631. (save-excursion
  11632. (while bl
  11633. (set-buffer (pop bl))
  11634. (if (org-mode-p) (setq bl nil)))
  11635. (when (org-mode-p)
  11636. (easy-menu-change
  11637. '("Org") "File List for Agenda"
  11638. (append
  11639. (list
  11640. ["Edit File List" (org-edit-agenda-file-list) t]
  11641. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11642. ["Remove Current File from List" org-remove-file t]
  11643. ["Cycle through agenda files" org-cycle-agenda-files t]
  11644. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11645. "--")
  11646. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11647. ;;;; Documentation
  11648. (defun org-require-autoloaded-modules ()
  11649. (interactive)
  11650. (mapc 'require
  11651. '(org-agenda org-archive org-clock org-colview
  11652. org-exp org-export-latex org-publish
  11653. org-remember org-table)))
  11654. (defun org-customize ()
  11655. "Call the customize function with org as argument."
  11656. (interactive)
  11657. (org-load-modules-maybe)
  11658. (org-require-autoloaded-modules)
  11659. (customize-browse 'org))
  11660. (defun org-create-customize-menu ()
  11661. "Create a full customization menu for Org-mode, insert it into the menu."
  11662. (interactive)
  11663. (org-load-modules-maybe)
  11664. (org-require-autoloaded-modules)
  11665. (if (fboundp 'customize-menu-create)
  11666. (progn
  11667. (easy-menu-change
  11668. '("Org") "Customize"
  11669. `(["Browse Org group" org-customize t]
  11670. "--"
  11671. ,(customize-menu-create 'org)
  11672. ["Set" Custom-set t]
  11673. ["Save" Custom-save t]
  11674. ["Reset to Current" Custom-reset-current t]
  11675. ["Reset to Saved" Custom-reset-saved t]
  11676. ["Reset to Standard Settings" Custom-reset-standard t]))
  11677. (message "\"Org\"-menu now contains full customization menu"))
  11678. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  11679. ;;;; Miscellaneous stuff
  11680. ;;; Generally useful functions
  11681. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  11682. "Display the given MESSAGE as a warning."
  11683. (if (fboundp 'display-warning)
  11684. (display-warning 'org message
  11685. (if (featurep 'xemacs)
  11686. 'warning
  11687. :warning))
  11688. (let ((buf (get-buffer-create "*Org warnings*")))
  11689. (with-current-buffer buf
  11690. (goto-char (point-max))
  11691. (insert "Warning (Org): " message)
  11692. (unless (bolp)
  11693. (newline)))
  11694. (display-buffer buf)
  11695. (sit-for 0))))
  11696. (defun org-plist-delete (plist property)
  11697. "Delete PROPERTY from PLIST.
  11698. This is in contrast to merely setting it to 0."
  11699. (let (p)
  11700. (while plist
  11701. (if (not (eq property (car plist)))
  11702. (setq p (plist-put p (car plist) (nth 1 plist))))
  11703. (setq plist (cddr plist)))
  11704. p))
  11705. (defun org-force-self-insert (N)
  11706. "Needed to enforce self-insert under remapping."
  11707. (interactive "p")
  11708. (self-insert-command N))
  11709. (defun org-string-width (s)
  11710. "Compute width of string, ignoring invisible characters.
  11711. This ignores character with invisibility property `org-link', and also
  11712. characters with property `org-cwidth', because these will become invisible
  11713. upon the next fontification round."
  11714. (let (b l)
  11715. (when (or (eq t buffer-invisibility-spec)
  11716. (assq 'org-link buffer-invisibility-spec))
  11717. (while (setq b (text-property-any 0 (length s)
  11718. 'invisible 'org-link s))
  11719. (setq s (concat (substring s 0 b)
  11720. (substring s (or (next-single-property-change
  11721. b 'invisible s) (length s)))))))
  11722. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  11723. (setq s (concat (substring s 0 b)
  11724. (substring s (or (next-single-property-change
  11725. b 'org-cwidth s) (length s))))))
  11726. (setq l (string-width s) b -1)
  11727. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  11728. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  11729. l))
  11730. (defun org-base-buffer (buffer)
  11731. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  11732. (if (not buffer)
  11733. buffer
  11734. (or (buffer-base-buffer buffer)
  11735. buffer)))
  11736. (defun org-trim (s)
  11737. "Remove whitespace at beginning and end of string."
  11738. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  11739. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  11740. s)
  11741. (defun org-wrap (string &optional width lines)
  11742. "Wrap string to either a number of lines, or a width in characters.
  11743. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  11744. that costs. If there is a word longer than WIDTH, the text is actually
  11745. wrapped to the length of that word.
  11746. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  11747. many lines, whatever width that takes.
  11748. The return value is a list of lines, without newlines at the end."
  11749. (let* ((words (org-split-string string "[ \t\n]+"))
  11750. (maxword (apply 'max (mapcar 'org-string-width words)))
  11751. w ll)
  11752. (cond (width
  11753. (org-do-wrap words (max maxword width)))
  11754. (lines
  11755. (setq w maxword)
  11756. (setq ll (org-do-wrap words maxword))
  11757. (if (<= (length ll) lines)
  11758. ll
  11759. (setq ll words)
  11760. (while (> (length ll) lines)
  11761. (setq w (1+ w))
  11762. (setq ll (org-do-wrap words w)))
  11763. ll))
  11764. (t (error "Cannot wrap this")))))
  11765. (defun org-do-wrap (words width)
  11766. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  11767. (let (lines line)
  11768. (while words
  11769. (setq line (pop words))
  11770. (while (and words (< (+ (length line) (length (car words))) width))
  11771. (setq line (concat line " " (pop words))))
  11772. (setq lines (push line lines)))
  11773. (nreverse lines)))
  11774. (defun org-split-string (string &optional separators)
  11775. "Splits STRING into substrings at SEPARATORS.
  11776. No empty strings are returned if there are matches at the beginning
  11777. and end of string."
  11778. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  11779. (start 0)
  11780. notfirst
  11781. (list nil))
  11782. (while (and (string-match rexp string
  11783. (if (and notfirst
  11784. (= start (match-beginning 0))
  11785. (< start (length string)))
  11786. (1+ start) start))
  11787. (< (match-beginning 0) (length string)))
  11788. (setq notfirst t)
  11789. (or (eq (match-beginning 0) 0)
  11790. (and (eq (match-beginning 0) (match-end 0))
  11791. (eq (match-beginning 0) start))
  11792. (setq list
  11793. (cons (substring string start (match-beginning 0))
  11794. list)))
  11795. (setq start (match-end 0)))
  11796. (or (eq start (length string))
  11797. (setq list
  11798. (cons (substring string start)
  11799. list)))
  11800. (nreverse list)))
  11801. (defun org-context ()
  11802. "Return a list of contexts of the current cursor position.
  11803. If several contexts apply, all are returned.
  11804. Each context entry is a list with a symbol naming the context, and
  11805. two positions indicating start and end of the context. Possible
  11806. contexts are:
  11807. :headline anywhere in a headline
  11808. :headline-stars on the leading stars in a headline
  11809. :todo-keyword on a TODO keyword (including DONE) in a headline
  11810. :tags on the TAGS in a headline
  11811. :priority on the priority cookie in a headline
  11812. :item on the first line of a plain list item
  11813. :item-bullet on the bullet/number of a plain list item
  11814. :checkbox on the checkbox in a plain list item
  11815. :table in an org-mode table
  11816. :table-special on a special filed in a table
  11817. :table-table in a table.el table
  11818. :link on a hyperlink
  11819. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  11820. :target on a <<target>>
  11821. :radio-target on a <<<radio-target>>>
  11822. :latex-fragment on a LaTeX fragment
  11823. :latex-preview on a LaTeX fragment with overlayed preview image
  11824. This function expects the position to be visible because it uses font-lock
  11825. faces as a help to recognize the following contexts: :table-special, :link,
  11826. and :keyword."
  11827. (let* ((f (get-text-property (point) 'face))
  11828. (faces (if (listp f) f (list f)))
  11829. (p (point)) clist o)
  11830. ;; First the large context
  11831. (cond
  11832. ((org-on-heading-p t)
  11833. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  11834. (when (progn
  11835. (beginning-of-line 1)
  11836. (looking-at org-todo-line-tags-regexp))
  11837. (push (org-point-in-group p 1 :headline-stars) clist)
  11838. (push (org-point-in-group p 2 :todo-keyword) clist)
  11839. (push (org-point-in-group p 4 :tags) clist))
  11840. (goto-char p)
  11841. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  11842. (if (looking-at "\\[#[A-Z0-9]\\]")
  11843. (push (org-point-in-group p 0 :priority) clist)))
  11844. ((org-at-item-p)
  11845. (push (org-point-in-group p 2 :item-bullet) clist)
  11846. (push (list :item (point-at-bol)
  11847. (save-excursion (org-end-of-item) (point)))
  11848. clist)
  11849. (and (org-at-item-checkbox-p)
  11850. (push (org-point-in-group p 0 :checkbox) clist)))
  11851. ((org-at-table-p)
  11852. (push (list :table (org-table-begin) (org-table-end)) clist)
  11853. (if (memq 'org-formula faces)
  11854. (push (list :table-special
  11855. (previous-single-property-change p 'face)
  11856. (next-single-property-change p 'face)) clist)))
  11857. ((org-at-table-p 'any)
  11858. (push (list :table-table) clist)))
  11859. (goto-char p)
  11860. ;; Now the small context
  11861. (cond
  11862. ((org-at-timestamp-p)
  11863. (push (org-point-in-group p 0 :timestamp) clist))
  11864. ((memq 'org-link faces)
  11865. (push (list :link
  11866. (previous-single-property-change p 'face)
  11867. (next-single-property-change p 'face)) clist))
  11868. ((memq 'org-special-keyword faces)
  11869. (push (list :keyword
  11870. (previous-single-property-change p 'face)
  11871. (next-single-property-change p 'face)) clist))
  11872. ((org-on-target-p)
  11873. (push (org-point-in-group p 0 :target) clist)
  11874. (goto-char (1- (match-beginning 0)))
  11875. (if (looking-at org-radio-target-regexp)
  11876. (push (org-point-in-group p 0 :radio-target) clist))
  11877. (goto-char p))
  11878. ((setq o (car (delq nil
  11879. (mapcar
  11880. (lambda (x)
  11881. (if (memq x org-latex-fragment-image-overlays) x))
  11882. (org-overlays-at (point))))))
  11883. (push (list :latex-fragment
  11884. (org-overlay-start o) (org-overlay-end o)) clist)
  11885. (push (list :latex-preview
  11886. (org-overlay-start o) (org-overlay-end o)) clist))
  11887. ((org-inside-LaTeX-fragment-p)
  11888. ;; FIXME: positions wrong.
  11889. (push (list :latex-fragment (point) (point)) clist)))
  11890. (setq clist (nreverse (delq nil clist)))
  11891. clist))
  11892. ;; FIXME: Compare with at-regexp-p Do we need both?
  11893. (defun org-in-regexp (re &optional nlines visually)
  11894. "Check if point is inside a match of regexp.
  11895. Normally only the current line is checked, but you can include NLINES extra
  11896. lines both before and after point into the search.
  11897. If VISUALLY is set, require that the cursor is not after the match but
  11898. really on, so that the block visually is on the match."
  11899. (catch 'exit
  11900. (let ((pos (point))
  11901. (eol (point-at-eol (+ 1 (or nlines 0))))
  11902. (inc (if visually 1 0)))
  11903. (save-excursion
  11904. (beginning-of-line (- 1 (or nlines 0)))
  11905. (while (re-search-forward re eol t)
  11906. (if (and (<= (match-beginning 0) pos)
  11907. (>= (+ inc (match-end 0)) pos))
  11908. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  11909. (defun org-at-regexp-p (regexp)
  11910. "Is point inside a match of REGEXP in the current line?"
  11911. (catch 'exit
  11912. (save-excursion
  11913. (let ((pos (point)) (end (point-at-eol)))
  11914. (beginning-of-line 1)
  11915. (while (re-search-forward regexp end t)
  11916. (if (and (<= (match-beginning 0) pos)
  11917. (>= (match-end 0) pos))
  11918. (throw 'exit t)))
  11919. nil))))
  11920. (defun org-occur-in-agenda-files (regexp &optional nlines)
  11921. "Call `multi-occur' with buffers for all agenda files."
  11922. (interactive "sOrg-files matching: \np")
  11923. (let* ((files (org-agenda-files))
  11924. (tnames (mapcar 'file-truename files))
  11925. (extra org-agenda-text-search-extra-files)
  11926. f)
  11927. (when (eq (car extra) 'agenda-archives)
  11928. (setq extra (cdr extra))
  11929. (setq files (org-add-archive-files files)))
  11930. (while (setq f (pop extra))
  11931. (unless (member (file-truename f) tnames)
  11932. (add-to-list 'files f 'append)
  11933. (add-to-list 'tnames (file-truename f) 'append)))
  11934. (multi-occur
  11935. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  11936. regexp)))
  11937. (if (boundp 'occur-mode-find-occurrence-hook)
  11938. ;; Emacs 23
  11939. (add-hook 'occur-mode-find-occurrence-hook
  11940. (lambda ()
  11941. (when (org-mode-p)
  11942. (org-reveal))))
  11943. ;; Emacs 22
  11944. (defadvice occur-mode-goto-occurrence
  11945. (after org-occur-reveal activate)
  11946. (and (org-mode-p) (org-reveal)))
  11947. (defadvice occur-mode-goto-occurrence-other-window
  11948. (after org-occur-reveal activate)
  11949. (and (org-mode-p) (org-reveal)))
  11950. (defadvice occur-mode-display-occurrence
  11951. (after org-occur-reveal activate)
  11952. (when (org-mode-p)
  11953. (let ((pos (occur-mode-find-occurrence)))
  11954. (with-current-buffer (marker-buffer pos)
  11955. (save-excursion
  11956. (goto-char pos)
  11957. (org-reveal)))))))
  11958. (defun org-uniquify (list)
  11959. "Remove duplicate elements from LIST."
  11960. (let (res)
  11961. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  11962. res))
  11963. (defun org-delete-all (elts list)
  11964. "Remove all elements in ELTS from LIST."
  11965. (while elts
  11966. (setq list (delete (pop elts) list)))
  11967. list)
  11968. (defun org-back-over-empty-lines ()
  11969. "Move backwards over witespace, to the beginning of the first empty line.
  11970. Returns the number of empty lines passed."
  11971. (let ((pos (point)))
  11972. (skip-chars-backward " \t\n\r")
  11973. (beginning-of-line 2)
  11974. (goto-char (min (point) pos))
  11975. (count-lines (point) pos)))
  11976. (defun org-skip-whitespace ()
  11977. (skip-chars-forward " \t\n\r"))
  11978. (defun org-point-in-group (point group &optional context)
  11979. "Check if POINT is in match-group GROUP.
  11980. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  11981. match. If the match group does ot exist or point is not inside it,
  11982. return nil."
  11983. (and (match-beginning group)
  11984. (>= point (match-beginning group))
  11985. (<= point (match-end group))
  11986. (if context
  11987. (list context (match-beginning group) (match-end group))
  11988. t)))
  11989. (defun org-switch-to-buffer-other-window (&rest args)
  11990. "Switch to buffer in a second window on the current frame.
  11991. In particular, do not allow pop-up frames."
  11992. (let (pop-up-frames special-display-buffer-names special-display-regexps
  11993. special-display-function)
  11994. (apply 'switch-to-buffer-other-window args)))
  11995. (defun org-combine-plists (&rest plists)
  11996. "Create a single property list from all plists in PLISTS.
  11997. The process starts by copying the first list, and then setting properties
  11998. from the other lists. Settings in the last list are the most significant
  11999. ones and overrule settings in the other lists."
  12000. (let ((rtn (copy-sequence (pop plists)))
  12001. p v ls)
  12002. (while plists
  12003. (setq ls (pop plists))
  12004. (while ls
  12005. (setq p (pop ls) v (pop ls))
  12006. (setq rtn (plist-put rtn p v))))
  12007. rtn))
  12008. (defun org-move-line-down (arg)
  12009. "Move the current line down. With prefix argument, move it past ARG lines."
  12010. (interactive "p")
  12011. (let ((col (current-column))
  12012. beg end pos)
  12013. (beginning-of-line 1) (setq beg (point))
  12014. (beginning-of-line 2) (setq end (point))
  12015. (beginning-of-line (+ 1 arg))
  12016. (setq pos (move-marker (make-marker) (point)))
  12017. (insert (delete-and-extract-region beg end))
  12018. (goto-char pos)
  12019. (org-move-to-column col)))
  12020. (defun org-move-line-up (arg)
  12021. "Move the current line up. With prefix argument, move it past ARG lines."
  12022. (interactive "p")
  12023. (let ((col (current-column))
  12024. beg end pos)
  12025. (beginning-of-line 1) (setq beg (point))
  12026. (beginning-of-line 2) (setq end (point))
  12027. (beginning-of-line (- arg))
  12028. (setq pos (move-marker (make-marker) (point)))
  12029. (insert (delete-and-extract-region beg end))
  12030. (goto-char pos)
  12031. (org-move-to-column col)))
  12032. (defun org-replace-escapes (string table)
  12033. "Replace %-escapes in STRING with values in TABLE.
  12034. TABLE is an association list with keys like \"%a\" and string values.
  12035. The sequences in STRING may contain normal field width and padding information,
  12036. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12037. so values can contain further %-escapes if they are define later in TABLE."
  12038. (let ((case-fold-search nil)
  12039. e re rpl)
  12040. (while (setq e (pop table))
  12041. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12042. (while (string-match re string)
  12043. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12044. (cdr e)))
  12045. (setq string (replace-match rpl t t string))))
  12046. string))
  12047. (defun org-sublist (list start end)
  12048. "Return a section of LIST, from START to END.
  12049. Counting starts at 1."
  12050. (let (rtn (c start))
  12051. (setq list (nthcdr (1- start) list))
  12052. (while (and list (<= c end))
  12053. (push (pop list) rtn)
  12054. (setq c (1+ c)))
  12055. (nreverse rtn)))
  12056. (defun org-find-base-buffer-visiting (file)
  12057. "Like `find-buffer-visiting' but alway return the base buffer and
  12058. not an indirect buffer."
  12059. (let ((buf (find-buffer-visiting file)))
  12060. (if buf
  12061. (or (buffer-base-buffer buf) buf)
  12062. nil)))
  12063. (defun org-image-file-name-regexp ()
  12064. "Return regexp matching the file names of images."
  12065. (if (fboundp 'image-file-name-regexp)
  12066. (image-file-name-regexp)
  12067. (let ((image-file-name-extensions
  12068. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12069. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12070. (concat "\\."
  12071. (regexp-opt (nconc (mapcar 'upcase
  12072. image-file-name-extensions)
  12073. image-file-name-extensions)
  12074. t)
  12075. "\\'"))))
  12076. (defun org-file-image-p (file)
  12077. "Return non-nil if FILE is an image."
  12078. (save-match-data
  12079. (string-match (org-image-file-name-regexp) file)))
  12080. ;;; Paragraph filling stuff.
  12081. ;; We want this to be just right, so use the full arsenal.
  12082. (defun org-indent-line-function ()
  12083. "Indent line like previous, but further if previous was headline or item."
  12084. (interactive)
  12085. (let* ((pos (point))
  12086. (itemp (org-at-item-p))
  12087. column bpos bcol tpos tcol bullet btype bullet-type)
  12088. ;; Find the previous relevant line
  12089. (beginning-of-line 1)
  12090. (cond
  12091. ((looking-at "#") (setq column 0))
  12092. ((looking-at "\\*+ ") (setq column 0))
  12093. (t
  12094. (beginning-of-line 0)
  12095. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12096. (beginning-of-line 0))
  12097. (cond
  12098. ((looking-at "\\*+[ \t]+")
  12099. (goto-char (match-end 0))
  12100. (setq column (current-column)))
  12101. ((org-in-item-p)
  12102. (org-beginning-of-item)
  12103. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12104. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  12105. (setq bpos (match-beginning 1) tpos (match-end 0)
  12106. bcol (progn (goto-char bpos) (current-column))
  12107. tcol (progn (goto-char tpos) (current-column))
  12108. bullet (match-string 1)
  12109. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12110. (if (not itemp)
  12111. (setq column tcol)
  12112. (goto-char pos)
  12113. (beginning-of-line 1)
  12114. (if (looking-at "\\S-")
  12115. (progn
  12116. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12117. (setq bullet (match-string 1)
  12118. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12119. (setq column (if (equal btype bullet-type) bcol tcol)))
  12120. (setq column (org-get-indentation)))))
  12121. (t (setq column (org-get-indentation))))))
  12122. (goto-char pos)
  12123. (if (<= (current-column) (current-indentation))
  12124. (org-indent-line-to column)
  12125. (save-excursion (org-indent-line-to column)))
  12126. (setq column (current-column))
  12127. (beginning-of-line 1)
  12128. (if (looking-at
  12129. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12130. (replace-match (concat "\\1" (format org-property-format
  12131. (match-string 2) (match-string 3)))
  12132. t nil))
  12133. (org-move-to-column column)))
  12134. (defun org-set-autofill-regexps ()
  12135. (interactive)
  12136. ;; In the paragraph separator we include headlines, because filling
  12137. ;; text in a line directly attached to a headline would otherwise
  12138. ;; fill the headline as well.
  12139. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12140. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12141. ;; The paragraph starter includes hand-formatted lists.
  12142. (org-set-local 'paragraph-start
  12143. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12144. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12145. ;; But only if the user has not turned off tables or fixed-width regions
  12146. (org-set-local
  12147. 'auto-fill-inhibit-regexp
  12148. (concat "\\*+ \\|#\\+"
  12149. "\\|[ \t]*" org-keyword-time-regexp
  12150. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12151. (concat
  12152. "\\|[ \t]*["
  12153. (if org-enable-table-editor "|" "")
  12154. (if org-enable-fixed-width-editor ":" "")
  12155. "]"))))
  12156. ;; We use our own fill-paragraph function, to make sure that tables
  12157. ;; and fixed-width regions are not wrapped. That function will pass
  12158. ;; through to `fill-paragraph' when appropriate.
  12159. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12160. ; Adaptive filling: To get full control, first make sure that
  12161. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12162. (org-set-local 'adaptive-fill-regexp "\000")
  12163. (org-set-local 'adaptive-fill-function
  12164. 'org-adaptive-fill-function)
  12165. (org-set-local
  12166. 'align-mode-rules-list
  12167. '((org-in-buffer-settings
  12168. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12169. (modes . '(org-mode))))))
  12170. (defun org-fill-paragraph (&optional justify)
  12171. "Re-align a table, pass through to fill-paragraph if no table."
  12172. (let ((table-p (org-at-table-p))
  12173. (table.el-p (org-at-table.el-p)))
  12174. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12175. (save-excursion (goto-char (point-at-bol))
  12176. (looking-at outline-regexp)))
  12177. t) ; skip headlines
  12178. (table.el-p t) ; skip table.el tables
  12179. (table-p (org-table-align) t) ; align org-mode tables
  12180. (t nil)))) ; call paragraph-fill
  12181. ;; For reference, this is the default value of adaptive-fill-regexp
  12182. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12183. (defun org-adaptive-fill-function ()
  12184. "Return a fill prefix for org-mode files.
  12185. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12186. work correctly."
  12187. (cond ((looking-at "#[ \t]+")
  12188. (match-string 0))
  12189. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12190. (save-excursion
  12191. (goto-char (match-end 0))
  12192. (make-string (current-column) ?\ )))
  12193. (t nil)))
  12194. ;;; Other stuff.
  12195. (defun org-toggle-fixed-width-section (arg)
  12196. "Toggle the fixed-width export.
  12197. If there is no active region, the QUOTE keyword at the current headline is
  12198. inserted or removed. When present, it causes the text between this headline
  12199. and the next to be exported as fixed-width text, and unmodified.
  12200. If there is an active region, this command adds or removes a colon as the
  12201. first character of this line. If the first character of a line is a colon,
  12202. this line is also exported in fixed-width font."
  12203. (interactive "P")
  12204. (let* ((cc 0)
  12205. (regionp (org-region-active-p))
  12206. (beg (if regionp (region-beginning) (point)))
  12207. (end (if regionp (region-end)))
  12208. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12209. (case-fold-search nil)
  12210. (re "[ \t]*\\(:\\)")
  12211. off)
  12212. (if regionp
  12213. (save-excursion
  12214. (goto-char beg)
  12215. (setq cc (current-column))
  12216. (beginning-of-line 1)
  12217. (setq off (looking-at re))
  12218. (while (> nlines 0)
  12219. (setq nlines (1- nlines))
  12220. (beginning-of-line 1)
  12221. (cond
  12222. (arg
  12223. (org-move-to-column cc t)
  12224. (insert ":\n")
  12225. (forward-line -1))
  12226. ((and off (looking-at re))
  12227. (replace-match "" t t nil 1))
  12228. ((not off) (org-move-to-column cc t) (insert ":")))
  12229. (forward-line 1)))
  12230. (save-excursion
  12231. (org-back-to-heading)
  12232. (if (looking-at (concat outline-regexp
  12233. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12234. (replace-match "" t t nil 1)
  12235. (if (looking-at outline-regexp)
  12236. (progn
  12237. (goto-char (match-end 0))
  12238. (insert org-quote-string " "))))))))
  12239. ;;;; Functions extending outline functionality
  12240. (defun org-beginning-of-line (&optional arg)
  12241. "Go to the beginning of the current line. If that is invisible, continue
  12242. to a visible line beginning. This makes the function of C-a more intuitive.
  12243. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12244. first attempt, and only move to after the tags when the cursor is already
  12245. beyond the end of the headline."
  12246. (interactive "P")
  12247. (let ((pos (point)))
  12248. (beginning-of-line 1)
  12249. (if (bobp)
  12250. nil
  12251. (backward-char 1)
  12252. (if (org-invisible-p)
  12253. (while (and (not (bobp)) (org-invisible-p))
  12254. (backward-char 1)
  12255. (beginning-of-line 1))
  12256. (forward-char 1)))
  12257. (when org-special-ctrl-a/e
  12258. (cond
  12259. ((and (looking-at org-todo-line-regexp)
  12260. (= (char-after (match-end 1)) ?\ ))
  12261. (goto-char
  12262. (if (eq org-special-ctrl-a/e t)
  12263. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  12264. ((= pos (point)) (match-beginning 3))
  12265. (t (point)))
  12266. (cond ((> pos (point)) (point))
  12267. ((not (eq last-command this-command)) (point))
  12268. (t (match-beginning 3))))))
  12269. ((org-at-item-p)
  12270. (goto-char
  12271. (if (eq org-special-ctrl-a/e t)
  12272. (cond ((> pos (match-end 4)) (match-end 4))
  12273. ((= pos (point)) (match-end 4))
  12274. (t (point)))
  12275. (cond ((> pos (point)) (point))
  12276. ((not (eq last-command this-command)) (point))
  12277. (t (match-end 4))))))))))
  12278. (defun org-end-of-line (&optional arg)
  12279. "Go to the end of the line.
  12280. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12281. first attempt, and only move to after the tags when the cursor is already
  12282. beyond the end of the headline."
  12283. (interactive "P")
  12284. (if (or (not org-special-ctrl-a/e)
  12285. (not (org-on-heading-p)))
  12286. (end-of-line arg)
  12287. (let ((pos (point)))
  12288. (beginning-of-line 1)
  12289. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12290. (if (eq org-special-ctrl-a/e t)
  12291. (if (or (< pos (match-beginning 1))
  12292. (= pos (match-end 0)))
  12293. (goto-char (match-beginning 1))
  12294. (goto-char (match-end 0)))
  12295. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12296. (goto-char (match-end 0))
  12297. (goto-char (match-beginning 1))))
  12298. (end-of-line arg)))))
  12299. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12300. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12301. (defun org-kill-line (&optional arg)
  12302. "Kill line, to tags or end of line."
  12303. (interactive "P")
  12304. (cond
  12305. ((or (not org-special-ctrl-k)
  12306. (bolp)
  12307. (not (org-on-heading-p)))
  12308. (call-interactively 'kill-line))
  12309. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12310. (kill-region (point) (match-beginning 1))
  12311. (org-set-tags nil t))
  12312. (t (kill-region (point) (point-at-eol)))))
  12313. (define-key org-mode-map "\C-k" 'org-kill-line)
  12314. (defun org-invisible-p ()
  12315. "Check if point is at a character currently not visible."
  12316. ;; Early versions of noutline don't have `outline-invisible-p'.
  12317. (if (fboundp 'outline-invisible-p)
  12318. (outline-invisible-p)
  12319. (get-char-property (point) 'invisible)))
  12320. (defun org-invisible-p2 ()
  12321. "Check if point is at a character currently not visible."
  12322. (save-excursion
  12323. (if (and (eolp) (not (bobp))) (backward-char 1))
  12324. ;; Early versions of noutline don't have `outline-invisible-p'.
  12325. (if (fboundp 'outline-invisible-p)
  12326. (outline-invisible-p)
  12327. (get-char-property (point) 'invisible))))
  12328. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12329. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12330. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12331. (defun org-at-heading-or-item-p ()
  12332. (or (org-on-heading-p) (org-at-item-p)))
  12333. (defun org-on-target-p ()
  12334. (or (org-in-regexp org-radio-target-regexp)
  12335. (org-in-regexp org-target-regexp)))
  12336. (defun org-up-heading-all (arg)
  12337. "Move to the heading line of which the present line is a subheading.
  12338. This function considers both visible and invisible heading lines.
  12339. With argument, move up ARG levels."
  12340. (if (fboundp 'outline-up-heading-all)
  12341. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12342. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12343. (defun org-up-heading-safe ()
  12344. "Move to the heading line of which the present line is a subheading.
  12345. This version will not throw an error. It will return the level of the
  12346. headline found, or nil if no higher level is found."
  12347. (let ((pos (point)) start-level level
  12348. (re (concat "^" outline-regexp)))
  12349. (catch 'exit
  12350. (outline-back-to-heading t)
  12351. (setq start-level (funcall outline-level))
  12352. (if (equal start-level 1) (throw 'exit nil))
  12353. (while (re-search-backward re nil t)
  12354. (setq level (funcall outline-level))
  12355. (if (< level start-level) (throw 'exit level)))
  12356. nil)))
  12357. (defun org-first-sibling-p ()
  12358. "Is this heading the first child of its parents?"
  12359. (interactive)
  12360. (let ((re (concat "^" outline-regexp))
  12361. level l)
  12362. (unless (org-at-heading-p t)
  12363. (error "Not at a heading"))
  12364. (setq level (funcall outline-level))
  12365. (save-excursion
  12366. (if (not (re-search-backward re nil t))
  12367. t
  12368. (setq l (funcall outline-level))
  12369. (< l level)))))
  12370. (defun org-goto-sibling (&optional previous)
  12371. "Goto the next sibling, even if it is invisible.
  12372. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12373. when a sibling was found. When none is found, return nil and don't
  12374. move point."
  12375. (let ((fun (if previous 're-search-backward 're-search-forward))
  12376. (pos (point))
  12377. (re (concat "^" outline-regexp))
  12378. level l)
  12379. (when (condition-case nil (org-back-to-heading t) (error nil))
  12380. (setq level (funcall outline-level))
  12381. (catch 'exit
  12382. (or previous (forward-char 1))
  12383. (while (funcall fun re nil t)
  12384. (setq l (funcall outline-level))
  12385. (when (< l level) (goto-char pos) (throw 'exit nil))
  12386. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12387. (goto-char pos)
  12388. nil))))
  12389. (defun org-show-siblings ()
  12390. "Show all siblings of the current headline."
  12391. (save-excursion
  12392. (while (org-goto-sibling) (org-flag-heading nil)))
  12393. (save-excursion
  12394. (while (org-goto-sibling 'previous)
  12395. (org-flag-heading nil))))
  12396. (defun org-show-hidden-entry ()
  12397. "Show an entry where even the heading is hidden."
  12398. (save-excursion
  12399. (org-show-entry)))
  12400. (defun org-flag-heading (flag &optional entry)
  12401. "Flag the current heading. FLAG non-nil means make invisible.
  12402. When ENTRY is non-nil, show the entire entry."
  12403. (save-excursion
  12404. (org-back-to-heading t)
  12405. ;; Check if we should show the entire entry
  12406. (if entry
  12407. (progn
  12408. (org-show-entry)
  12409. (save-excursion
  12410. (and (outline-next-heading)
  12411. (org-flag-heading nil))))
  12412. (outline-flag-region (max (point-min) (1- (point)))
  12413. (save-excursion (outline-end-of-heading) (point))
  12414. flag))))
  12415. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12416. ;; This is an exact copy of the original function, but it uses
  12417. ;; `org-back-to-heading', to make it work also in invisible
  12418. ;; trees. And is uses an invisible-OK argument.
  12419. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12420. (org-back-to-heading invisible-OK)
  12421. (let ((first t)
  12422. (level (funcall outline-level)))
  12423. (while (and (not (eobp))
  12424. (or first (> (funcall outline-level) level)))
  12425. (setq first nil)
  12426. (outline-next-heading))
  12427. (unless to-heading
  12428. (if (memq (preceding-char) '(?\n ?\^M))
  12429. (progn
  12430. ;; Go to end of line before heading
  12431. (forward-char -1)
  12432. (if (memq (preceding-char) '(?\n ?\^M))
  12433. ;; leave blank line before heading
  12434. (forward-char -1))))))
  12435. (point))
  12436. (defun org-show-subtree ()
  12437. "Show everything after this heading at deeper levels."
  12438. (outline-flag-region
  12439. (point)
  12440. (save-excursion
  12441. (outline-end-of-subtree) (outline-next-heading) (point))
  12442. nil))
  12443. (defun org-show-entry ()
  12444. "Show the body directly following this heading.
  12445. Show the heading too, if it is currently invisible."
  12446. (interactive)
  12447. (save-excursion
  12448. (condition-case nil
  12449. (progn
  12450. (org-back-to-heading t)
  12451. (outline-flag-region
  12452. (max (point-min) (1- (point)))
  12453. (save-excursion
  12454. (re-search-forward
  12455. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12456. (or (match-beginning 1) (point-max)))
  12457. nil))
  12458. (error nil))))
  12459. (defun org-make-options-regexp (kwds)
  12460. "Make a regular expression for keyword lines."
  12461. (concat
  12462. "^"
  12463. "#?[ \t]*\\+\\("
  12464. (mapconcat 'regexp-quote kwds "\\|")
  12465. "\\):[ \t]*"
  12466. "\\(.+\\)"))
  12467. ;; Make isearch reveal the necessary context
  12468. (defun org-isearch-end ()
  12469. "Reveal context after isearch exits."
  12470. (when isearch-success ; only if search was successful
  12471. (if (featurep 'xemacs)
  12472. ;; Under XEmacs, the hook is run in the correct place,
  12473. ;; we directly show the context.
  12474. (org-show-context 'isearch)
  12475. ;; In Emacs the hook runs *before* restoring the overlays.
  12476. ;; So we have to use a one-time post-command-hook to do this.
  12477. ;; (Emacs 22 has a special variable, see function `org-mode')
  12478. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12479. isearch-mode-end-hook-quit)
  12480. ;; Only when the isearch was not quitted.
  12481. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12482. 'append 'local)))))
  12483. (defun org-isearch-post-command ()
  12484. "Remove self from hook, and show context."
  12485. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12486. (org-show-context 'isearch))
  12487. ;;;; Integration with and fixes for other packages
  12488. ;;; Imenu support
  12489. (defvar org-imenu-markers nil
  12490. "All markers currently used by Imenu.")
  12491. (make-variable-buffer-local 'org-imenu-markers)
  12492. (defun org-imenu-new-marker (&optional pos)
  12493. "Return a new marker for use by Imenu, and remember the marker."
  12494. (let ((m (make-marker)))
  12495. (move-marker m (or pos (point)))
  12496. (push m org-imenu-markers)
  12497. m))
  12498. (defun org-imenu-get-tree ()
  12499. "Produce the index for Imenu."
  12500. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12501. (setq org-imenu-markers nil)
  12502. (let* ((n org-imenu-depth)
  12503. (re (concat "^" outline-regexp))
  12504. (subs (make-vector (1+ n) nil))
  12505. (last-level 0)
  12506. m tree level head)
  12507. (save-excursion
  12508. (save-restriction
  12509. (widen)
  12510. (goto-char (point-max))
  12511. (while (re-search-backward re nil t)
  12512. (setq level (org-reduced-level (funcall outline-level)))
  12513. (when (<= level n)
  12514. (looking-at org-complex-heading-regexp)
  12515. (setq head (org-match-string-no-properties 4)
  12516. m (org-imenu-new-marker))
  12517. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12518. (if (>= level last-level)
  12519. (push (cons head m) (aref subs level))
  12520. (push (cons head (aref subs (1+ level))) (aref subs level))
  12521. (loop for i from (1+ level) to n do (aset subs i nil)))
  12522. (setq last-level level)))))
  12523. (aref subs 1)))
  12524. (eval-after-load "imenu"
  12525. '(progn
  12526. (add-hook 'imenu-after-jump-hook
  12527. (lambda () (org-show-context 'org-goto)))))
  12528. ;; Speedbar support
  12529. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12530. "Overlay marking the agenda restriction line in speedbar.")
  12531. (org-overlay-put org-speedbar-restriction-lock-overlay
  12532. 'face 'org-agenda-restriction-lock)
  12533. (org-overlay-put org-speedbar-restriction-lock-overlay
  12534. 'help-echo "Agendas are currently limited to this item.")
  12535. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  12536. (defun org-speedbar-set-agenda-restriction ()
  12537. "Restrict future agenda commands to the location at point in speedbar.
  12538. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  12539. (interactive)
  12540. (require 'org-agenda)
  12541. (let (p m tp np dir txt w)
  12542. (cond
  12543. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12544. 'org-imenu t))
  12545. (setq m (get-text-property p 'org-imenu-marker))
  12546. (save-excursion
  12547. (save-restriction
  12548. (set-buffer (marker-buffer m))
  12549. (goto-char m)
  12550. (org-agenda-set-restriction-lock 'subtree))))
  12551. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12552. 'speedbar-function 'speedbar-find-file))
  12553. (setq tp (previous-single-property-change
  12554. (1+ p) 'speedbar-function)
  12555. np (next-single-property-change
  12556. tp 'speedbar-function)
  12557. dir (speedbar-line-directory)
  12558. txt (buffer-substring-no-properties (or tp (point-min))
  12559. (or np (point-max))))
  12560. (save-excursion
  12561. (save-restriction
  12562. (set-buffer (find-file-noselect
  12563. (let ((default-directory dir))
  12564. (expand-file-name txt))))
  12565. (unless (org-mode-p)
  12566. (error "Cannot restrict to non-Org-mode file"))
  12567. (org-agenda-set-restriction-lock 'file))))
  12568. (t (error "Don't know how to restrict Org-mode's agenda")))
  12569. (org-move-overlay org-speedbar-restriction-lock-overlay
  12570. (point-at-bol) (point-at-eol))
  12571. (setq current-prefix-arg nil)
  12572. (org-agenda-maybe-redo)))
  12573. (eval-after-load "speedbar"
  12574. '(progn
  12575. (speedbar-add-supported-extension ".org")
  12576. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  12577. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  12578. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  12579. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12580. (add-hook 'speedbar-visiting-tag-hook
  12581. (lambda () (org-show-context 'org-goto)))))
  12582. ;;; Fixes and Hacks for problems with other packages
  12583. ;; Make flyspell not check words in links, to not mess up our keymap
  12584. (defun org-mode-flyspell-verify ()
  12585. "Don't let flyspell put overlays at active buttons."
  12586. (not (get-text-property (point) 'keymap)))
  12587. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12588. (eval-after-load "bookmark"
  12589. '(if (boundp 'bookmark-after-jump-hook)
  12590. ;; We can use the hook
  12591. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12592. ;; Hook not available, use advice
  12593. (defadvice bookmark-jump (after org-make-visible activate)
  12594. "Make the position visible."
  12595. (org-bookmark-jump-unhide))))
  12596. (defun org-bookmark-jump-unhide ()
  12597. "Unhide the current position, to show the bookmark location."
  12598. (and (org-mode-p)
  12599. (or (org-invisible-p)
  12600. (save-excursion (goto-char (max (point-min) (1- (point))))
  12601. (org-invisible-p)))
  12602. (org-show-context 'bookmark-jump)))
  12603. ;; Make session.el ignore our circular variable
  12604. (eval-after-load "session"
  12605. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  12606. ;;;; Experimental code
  12607. (defun org-closed-in-range ()
  12608. "Sparse tree of items closed in a certain time range.
  12609. Still experimental, may disappear in the future."
  12610. (interactive)
  12611. ;; Get the time interval from the user.
  12612. (let* ((time1 (time-to-seconds
  12613. (org-read-date nil 'to-time nil "Starting date: ")))
  12614. (time2 (time-to-seconds
  12615. (org-read-date nil 'to-time nil "End date:")))
  12616. ;; callback function
  12617. (callback (lambda ()
  12618. (let ((time
  12619. (time-to-seconds
  12620. (apply 'encode-time
  12621. (org-parse-time-string
  12622. (match-string 1))))))
  12623. ;; check if time in interval
  12624. (and (>= time time1) (<= time time2))))))
  12625. ;; make tree, check each match with the callback
  12626. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  12627. ;;;; Finish up
  12628. (provide 'org)
  12629. (run-hooks 'org-load-hook)
  12630. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  12631. ;;; org.el ends here