org.el 512 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953
  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.03pre01
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. ;;;; Customization variables
  84. ;;; Version
  85. (defconst org-version "6.03pre01"
  86. "The version number of the file org.el.")
  87. (defun org-version (&optional here)
  88. "Show the org-mode version in the echo area.
  89. With prefix arg HERE, insert it at point."
  90. (interactive "P")
  91. (let ((version (format "Org-mode version %s" org-version)))
  92. (message version)
  93. (if here
  94. (insert version))))
  95. ;;; Compatibility constants
  96. ;;; The custom variables
  97. (defgroup org nil
  98. "Outline-based notes management and organizer."
  99. :tag "Org"
  100. :group 'outlines
  101. :group 'hypermedia
  102. :group 'calendar)
  103. (defcustom org-load-hook nil
  104. "Hook that is run after org.el has been loaded."
  105. :group 'org
  106. :type 'hook)
  107. (defvar org-modules) ; defined below
  108. (defvar org-modules-loaded nil
  109. "Have the modules been loaded already?")
  110. (defun org-load-modules-maybe (&optional force)
  111. "Load all extensions listed in `org-default-extensions'."
  112. (when (or force (not org-modules-loaded))
  113. (mapc (lambda (ext)
  114. (condition-case nil (require ext)
  115. (error (message "Problems while trying to load feature `%s'" ext))))
  116. org-modules)
  117. (setq org-modules-loaded t)))
  118. (defun org-set-modules (var value)
  119. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  120. (set var value)
  121. (when (featurep 'org)
  122. (org-load-modules-maybe 'force)))
  123. (when (org-bound-and-true-p org-modules)
  124. (let ((a (member 'org-infojs org-modules)))
  125. (and a (setcar a 'org-jsinfo))))
  126. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-wl)
  127. "Modules that should always be loaded together with org.el.
  128. If a description starts with <C>, the file is not part of Emacs
  129. and loading it will require that you have downloaded and properly installed
  130. the org-mode distribution.
  131. You can also use this system to load external packages (i.e. neither Org
  132. core modules, not modules from the CONTRIB directory). Just add symbols
  133. to the end of the list. If the package is called org-xyz.el, then you need
  134. to add the symbol `xyz', and the package must have a call to
  135. (provide 'org-xyz)"
  136. :group 'org
  137. :set 'org-set-modules
  138. :type
  139. '(set :greedy t
  140. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  141. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  142. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  143. (const :tag " info: Links to Info nodes" org-info)
  144. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  145. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  146. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  147. (const :tag " mew Links to Mew folders/messages" org-mew)
  148. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  149. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  150. (const :tag " vm: Links to VM folders/messages" org-vm)
  151. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  152. (const :tag " mouse: Additional mouse support" org-mouse)
  153. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  154. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  155. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  156. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  157. (const :tag "C eval: Include command output as text" org-eval)
  158. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  159. (const :tag "C id: Global id's for identifying entries" org-id)
  160. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  161. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  162. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  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. (defconst org-global-properties-fixed
  1615. '(("VISIBILITY_ALL" . "folded children content all"))
  1616. "List of property/value pairs that can be inherited by any entry.
  1617. These are fixed values, for the preset properties.")
  1618. (defcustom org-global-properties nil
  1619. "List of property/value pairs that can be inherited by any entry.
  1620. You can set buffer-local values for this by adding lines like
  1621. #+PROPERTY: NAME VALUE"
  1622. :group 'org-properties
  1623. :type '(repeat
  1624. (cons (string :tag "Property")
  1625. (string :tag "Value"))))
  1626. (defvar org-local-properties nil
  1627. "List of property/value pairs that can be inherited by any entry.
  1628. Valid for the current buffer.
  1629. This variable is populated from #+PROPERTY lines.")
  1630. (defgroup org-agenda nil
  1631. "Options concerning agenda views in Org-mode."
  1632. :tag "Org Agenda"
  1633. :group 'org)
  1634. (defvar org-category nil
  1635. "Variable used by org files to set a category for agenda display.
  1636. Such files should use a file variable to set it, for example
  1637. # -*- mode: org; org-category: \"ELisp\"
  1638. or contain a special line
  1639. #+CATEGORY: ELisp
  1640. If the file does not specify a category, then file's base name
  1641. is used instead.")
  1642. (make-variable-buffer-local 'org-category)
  1643. (defcustom org-agenda-files nil
  1644. "The files to be used for agenda display.
  1645. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1646. \\[org-remove-file]. You can also use customize to edit the list.
  1647. If an entry is a directory, all files in that directory that are matched by
  1648. `org-agenda-file-regexp' will be part of the file list.
  1649. If the value of the variable is not a list but a single file name, then
  1650. the list of agenda files is actually stored and maintained in that file, one
  1651. agenda file per line."
  1652. :group 'org-agenda
  1653. :type '(choice
  1654. (repeat :tag "List of files and directories" file)
  1655. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1656. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1657. "Regular expression to match files for `org-agenda-files'.
  1658. If any element in the list in that variable contains a directory instead
  1659. of a normal file, all files in that directory that are matched by this
  1660. regular expression will be included."
  1661. :group 'org-agenda
  1662. :type 'regexp)
  1663. (defcustom org-agenda-text-search-extra-files nil
  1664. "List of extra files to be searched by text search commands.
  1665. These files will be search in addition to the agenda files by the
  1666. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1667. Note that these files will only be searched for text search commands,
  1668. not for the other agenda views like todo lists, tag searches or the weekly
  1669. agenda. This variable is intended to list notes and possibly archive files
  1670. that should also be searched by these two commands.
  1671. In fact, if the first element in the list is the symbol `agenda-archives',
  1672. than all archive files of all agenda files will be added to the search
  1673. scope."
  1674. :group 'org-agenda
  1675. :type '(set :greedy t
  1676. (const :tag "Agenda Archives" agenda-archives)
  1677. (repeat :inline t (file))))
  1678. (if (fboundp 'defvaralias)
  1679. (defvaralias 'org-agenda-multi-occur-extra-files
  1680. 'org-agenda-text-search-extra-files))
  1681. (defcustom org-agenda-skip-unavailable-files nil
  1682. "t means to just skip non-reachable files in `org-agenda-files'.
  1683. Nil means to remove them, after a query, from the list."
  1684. :group 'org-agenda
  1685. :type 'boolean)
  1686. (defcustom org-calendar-to-agenda-key [?c]
  1687. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1688. The command `org-calendar-goto-agenda' will be bound to this key. The
  1689. default is the character `c' because then `c' can be used to switch back and
  1690. forth between agenda and calendar."
  1691. :group 'org-agenda
  1692. :type 'sexp)
  1693. (eval-after-load "calendar"
  1694. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1695. 'org-calendar-goto-agenda))
  1696. (defgroup org-latex nil
  1697. "Options for embedding LaTeX code into Org-mode."
  1698. :tag "Org LaTeX"
  1699. :group 'org)
  1700. (defcustom org-format-latex-options
  1701. '(:foreground default :background default :scale 1.0
  1702. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1703. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1704. "Options for creating images from LaTeX fragments.
  1705. This is a property list with the following properties:
  1706. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1707. `default' means use the foreground of the default face.
  1708. :background the background color, or \"Transparent\".
  1709. `default' means use the background of the default face.
  1710. :scale a scaling factor for the size of the images.
  1711. :html-foreground, :html-background, :html-scale
  1712. the same numbers for HTML export.
  1713. :matchers a list indicating which matchers should be used to
  1714. find LaTeX fragments. Valid members of this list are:
  1715. \"begin\" find environments
  1716. \"$\" find math expressions surrounded by $...$
  1717. \"$$\" find math expressions surrounded by $$....$$
  1718. \"\\(\" find math expressions surrounded by \\(...\\)
  1719. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1720. :group 'org-latex
  1721. :type 'plist)
  1722. (defcustom org-format-latex-header "\\documentclass{article}
  1723. \\usepackage{fullpage} % do not remove
  1724. \\usepackage{amssymb}
  1725. \\usepackage[usenames]{color}
  1726. \\usepackage{amsmath}
  1727. \\usepackage{latexsym}
  1728. \\usepackage[mathscr]{eucal}
  1729. \\pagestyle{empty} % do not remove"
  1730. "The document header used for processing LaTeX fragments."
  1731. :group 'org-latex
  1732. :type 'string)
  1733. (defgroup org-font-lock nil
  1734. "Font-lock settings for highlighting in Org-mode."
  1735. :tag "Org Font Lock"
  1736. :group 'org)
  1737. (defcustom org-level-color-stars-only nil
  1738. "Non-nil means fontify only the stars in each headline.
  1739. When nil, the entire headline is fontified.
  1740. Changing it requires restart of `font-lock-mode' to become effective
  1741. also in regions already fontified."
  1742. :group 'org-font-lock
  1743. :type 'boolean)
  1744. (defcustom org-hide-leading-stars nil
  1745. "Non-nil means, hide the first N-1 stars in a headline.
  1746. This works by using the face `org-hide' for these stars. This
  1747. face is white for a light background, and black for a dark
  1748. background. You may have to customize the face `org-hide' to
  1749. make this work.
  1750. Changing it requires restart of `font-lock-mode' to become effective
  1751. also in regions already fontified.
  1752. You may also set this on a per-file basis by adding one of the following
  1753. lines to the buffer:
  1754. #+STARTUP: hidestars
  1755. #+STARTUP: showstars"
  1756. :group 'org-font-lock
  1757. :type 'boolean)
  1758. (defcustom org-fontify-done-headline nil
  1759. "Non-nil means, change the face of a headline if it is marked DONE.
  1760. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1761. When this is non-nil, the headline after the keyword is set to the
  1762. `org-headline-done' as an additional indication."
  1763. :group 'org-font-lock
  1764. :type 'boolean)
  1765. (defcustom org-fontify-emphasized-text t
  1766. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1767. Changing this variable requires a restart of Emacs to take effect."
  1768. :group 'org-font-lock
  1769. :type 'boolean)
  1770. (defcustom org-highlight-latex-fragments-and-specials nil
  1771. "Non-nil means, fontify what is treated specially by the exporters."
  1772. :group 'org-font-lock
  1773. :type 'boolean)
  1774. (defcustom org-hide-emphasis-markers nil
  1775. "Non-nil mean font-lock should hide the emphasis marker characters."
  1776. :group 'org-font-lock
  1777. :type 'boolean)
  1778. (defvar org-emph-re nil
  1779. "Regular expression for matching emphasis.")
  1780. (defvar org-verbatim-re nil
  1781. "Regular expression for matching verbatim text.")
  1782. (defvar org-emphasis-regexp-components) ; defined just below
  1783. (defvar org-emphasis-alist) ; defined just below
  1784. (defun org-set-emph-re (var val)
  1785. "Set variable and compute the emphasis regular expression."
  1786. (set var val)
  1787. (when (and (boundp 'org-emphasis-alist)
  1788. (boundp 'org-emphasis-regexp-components)
  1789. org-emphasis-alist org-emphasis-regexp-components)
  1790. (let* ((e org-emphasis-regexp-components)
  1791. (pre (car e))
  1792. (post (nth 1 e))
  1793. (border (nth 2 e))
  1794. (body (nth 3 e))
  1795. (nl (nth 4 e))
  1796. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1797. (body1 (concat body "*?"))
  1798. (markers (mapconcat 'car org-emphasis-alist ""))
  1799. (vmarkers (mapconcat
  1800. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1801. org-emphasis-alist "")))
  1802. ;; make sure special characters appear at the right position in the class
  1803. (if (string-match "\\^" markers)
  1804. (setq markers (concat (replace-match "" t t markers) "^")))
  1805. (if (string-match "-" markers)
  1806. (setq markers (concat (replace-match "" t t markers) "-")))
  1807. (if (string-match "\\^" vmarkers)
  1808. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1809. (if (string-match "-" vmarkers)
  1810. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1811. (if (> nl 0)
  1812. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1813. (int-to-string nl) "\\}")))
  1814. ;; Make the regexp
  1815. (setq org-emph-re
  1816. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1817. "\\("
  1818. "\\([" markers "]\\)"
  1819. "\\("
  1820. "[^" border "]\\|"
  1821. "[^" border (if (and nil stacked) markers) "]"
  1822. body1
  1823. "[^" border (if (and nil stacked) markers) "]"
  1824. "\\)"
  1825. "\\3\\)"
  1826. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1827. (setq org-verbatim-re
  1828. (concat "\\([" pre "]\\|^\\)"
  1829. "\\("
  1830. "\\([" vmarkers "]\\)"
  1831. "\\("
  1832. "[^" border "]\\|"
  1833. "[^" border "]"
  1834. body1
  1835. "[^" border "]"
  1836. "\\)"
  1837. "\\3\\)"
  1838. "\\([" post "]\\|$\\)")))))
  1839. (defcustom org-emphasis-regexp-components
  1840. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1841. "Components used to build the regular expression for emphasis.
  1842. This is a list with 6 entries. Terminology: In an emphasis string
  1843. like \" *strong word* \", we call the initial space PREMATCH, the final
  1844. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1845. and \"trong wor\" is the body. The different components in this variable
  1846. specify what is allowed/forbidden in each part:
  1847. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1848. post Chars allowed as postmatch. End of line will be allowed too.
  1849. border The chars *forbidden* as border characters.
  1850. body-regexp A regexp like \".\" to match a body character. Don't use
  1851. non-shy groups here, and don't allow newline here.
  1852. newline The maximum number of newlines allowed in an emphasis exp.
  1853. Use customize to modify this, or restart Emacs after changing it."
  1854. :group 'org-font-lock
  1855. :set 'org-set-emph-re
  1856. :type '(list
  1857. (sexp :tag "Allowed chars in pre ")
  1858. (sexp :tag "Allowed chars in post ")
  1859. (sexp :tag "Forbidden chars in border ")
  1860. (sexp :tag "Regexp for body ")
  1861. (integer :tag "number of newlines allowed")
  1862. (option (boolean :tag "Stacking (DISABLED) "))))
  1863. (defcustom org-emphasis-alist
  1864. `(("*" bold "<b>" "</b>")
  1865. ("/" italic "<i>" "</i>")
  1866. ("_" underline "<u>" "</u>")
  1867. ("=" org-code "<code>" "</code>" verbatim)
  1868. ("~" org-verbatim "" "" verbatim)
  1869. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1870. "<del>" "</del>")
  1871. )
  1872. "Special syntax for emphasized text.
  1873. Text starting and ending with a special character will be emphasized, for
  1874. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1875. characters, the face to be used by font-lock for highlighting in Org-mode
  1876. Emacs buffers, and the HTML tags to be used for this.
  1877. Use customize to modify this, or restart Emacs after changing it."
  1878. :group 'org-font-lock
  1879. :set 'org-set-emph-re
  1880. :type '(repeat
  1881. (list
  1882. (string :tag "Marker character")
  1883. (choice
  1884. (face :tag "Font-lock-face")
  1885. (plist :tag "Face property list"))
  1886. (string :tag "HTML start tag")
  1887. (string :tag "HTML end tag")
  1888. (option (const verbatim)))))
  1889. ;;; Miscellaneous options
  1890. (defgroup org-completion nil
  1891. "Completion in Org-mode."
  1892. :tag "Org Completion"
  1893. :group 'org)
  1894. (defcustom org-completion-fallback-command 'hippie-expand
  1895. "The expansion command called by \\[org-complete] in normal context.
  1896. Normal means, no org-mode-specific context."
  1897. :group 'org-completion
  1898. :type 'function)
  1899. ;;; Functions and variables from ther packages
  1900. ;; Declared here to avoid compiler warnings
  1901. ;; XEmacs only
  1902. (defvar outline-mode-menu-heading)
  1903. (defvar outline-mode-menu-show)
  1904. (defvar outline-mode-menu-hide)
  1905. (defvar zmacs-regions) ; XEmacs regions
  1906. ;; Emacs only
  1907. (defvar mark-active)
  1908. ;; Various packages
  1909. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  1910. (declare-function calendar-forward-day "cal-move" (arg))
  1911. (declare-function calendar-goto-date "cal-move" (date))
  1912. (declare-function calendar-goto-today "cal-move" ())
  1913. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  1914. (defvar calc-embedded-close-formula)
  1915. (defvar calc-embedded-open-formula)
  1916. (declare-function cdlatex-tab "ext:cdlatex" ())
  1917. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1918. (defvar font-lock-unfontify-region-function)
  1919. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1920. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  1921. (defvar iswitchb-temp-buflist)
  1922. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  1923. (declare-function org-agenda-skip "org-agenda" ())
  1924. (declare-function org-format-agenda-item "org-agenda"
  1925. (extra txt &optional category tags dotime noprefix remove-re))
  1926. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  1927. (declare-function org-agenda-change-all-lines "org-agenda"
  1928. (newhead hdmarker &optional fixface))
  1929. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  1930. (declare-function org-agenda-maybe-redo "org-agenda" ())
  1931. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  1932. (beg end))
  1933. (declare-function parse-time-string "parse-time" (string))
  1934. (declare-function remember "remember" (&optional initial))
  1935. (declare-function remember-buffer-desc "remember" ())
  1936. (declare-function remember-finalize "remember" ())
  1937. (defvar remember-save-after-remembering)
  1938. (defvar remember-data-file)
  1939. (defvar remember-register)
  1940. (defvar remember-buffer)
  1941. (defvar remember-handler-functions)
  1942. (defvar remember-annotation-functions)
  1943. (defvar texmathp-why)
  1944. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  1945. (declare-function table--at-cell-p "table" (position &optional object at-column))
  1946. (defvar w3m-current-url)
  1947. (defvar w3m-current-title)
  1948. (defvar org-latex-regexps)
  1949. ;;; Autoload and prepare some org modules
  1950. ;; Some table stuff that needs to be defined here, because it is used
  1951. ;; by the functions setting up org-mode or checking for table context.
  1952. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  1953. "Detects an org-type or table-type table.")
  1954. (defconst org-table-line-regexp "^[ \t]*|"
  1955. "Detects an org-type table line.")
  1956. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  1957. "Detects an org-type table line.")
  1958. (defconst org-table-hline-regexp "^[ \t]*|-"
  1959. "Detects an org-type table hline.")
  1960. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  1961. "Detects a table-type table hline.")
  1962. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  1963. "Searching from within a table (any type) this finds the first line
  1964. outside the table.")
  1965. ;; Autoload the functions in org-table.el that are needed by functions here.
  1966. (eval-and-compile
  1967. (org-autoload "org-table"
  1968. '(org-table-align org-table-begin org-table-blank-field
  1969. org-table-convert org-table-convert-region org-table-copy-down
  1970. org-table-copy-region org-table-create
  1971. org-table-create-or-convert-from-region
  1972. org-table-create-with-table.el org-table-current-dline
  1973. org-table-cut-region org-table-delete-column org-table-edit-field
  1974. org-table-edit-formulas org-table-end org-table-eval-formula
  1975. org-table-export org-table-field-info
  1976. org-table-get-stored-formulas org-table-goto-column
  1977. org-table-hline-and-move org-table-import org-table-insert-column
  1978. org-table-insert-hline org-table-insert-row org-table-iterate
  1979. org-table-justify-field-maybe org-table-kill-row
  1980. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  1981. org-table-move-column org-table-move-column-left
  1982. org-table-move-column-right org-table-move-row
  1983. org-table-move-row-down org-table-move-row-up
  1984. org-table-next-field org-table-next-row org-table-paste-rectangle
  1985. org-table-previous-field org-table-recalculate
  1986. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  1987. org-table-toggle-coordinate-overlays
  1988. org-table-toggle-formula-debugger org-table-wrap-region
  1989. orgtbl-mode turn-on-orgtbl)))
  1990. (defun org-at-table-p (&optional table-type)
  1991. "Return t if the cursor is inside an org-type table.
  1992. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  1993. (if org-enable-table-editor
  1994. (save-excursion
  1995. (beginning-of-line 1)
  1996. (looking-at (if table-type org-table-any-line-regexp
  1997. org-table-line-regexp)))
  1998. nil))
  1999. (defsubst org-table-p () (org-at-table-p))
  2000. (defun org-at-table.el-p ()
  2001. "Return t if and only if we are at a table.el table."
  2002. (and (org-at-table-p 'any)
  2003. (save-excursion
  2004. (goto-char (org-table-begin 'any))
  2005. (looking-at org-table1-hline-regexp))))
  2006. (defun org-table-recognize-table.el ()
  2007. "If there is a table.el table nearby, recognize it and move into it."
  2008. (if org-table-tab-recognizes-table.el
  2009. (if (org-at-table.el-p)
  2010. (progn
  2011. (beginning-of-line 1)
  2012. (if (looking-at org-table-dataline-regexp)
  2013. nil
  2014. (if (looking-at org-table1-hline-regexp)
  2015. (progn
  2016. (beginning-of-line 2)
  2017. (if (looking-at org-table-any-border-regexp)
  2018. (beginning-of-line -1)))))
  2019. (if (re-search-forward "|" (org-table-end t) t)
  2020. (progn
  2021. (require 'table)
  2022. (if (table--at-cell-p (point))
  2023. t
  2024. (message "recognizing table.el table...")
  2025. (table-recognize-table)
  2026. (message "recognizing table.el table...done")))
  2027. (error "This should not happen..."))
  2028. t)
  2029. nil)
  2030. nil))
  2031. (defun org-at-table-hline-p ()
  2032. "Return t if the cursor is inside a hline in a table."
  2033. (if org-enable-table-editor
  2034. (save-excursion
  2035. (beginning-of-line 1)
  2036. (looking-at org-table-hline-regexp))
  2037. nil))
  2038. (defvar org-table-clean-did-remove-column nil)
  2039. (defun org-table-map-tables (function)
  2040. "Apply FUNCTION to the start of all tables in the buffer."
  2041. (save-excursion
  2042. (save-restriction
  2043. (widen)
  2044. (goto-char (point-min))
  2045. (while (re-search-forward org-table-any-line-regexp nil t)
  2046. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2047. (beginning-of-line 1)
  2048. (if (looking-at org-table-line-regexp)
  2049. (save-excursion (funcall function)))
  2050. (re-search-forward org-table-any-border-regexp nil 1))))
  2051. (message "Mapping tables: done"))
  2052. ;; Declare and autoload functions from org-exp.el
  2053. (declare-function org-default-export-plist "org-exp")
  2054. (declare-function org-infile-export-plist "org-exp")
  2055. (declare-function org-get-current-options "org-exp")
  2056. (eval-and-compile
  2057. (org-autoload "org-exp"
  2058. '(org-export org-export-as-ascii org-export-visible
  2059. org-insert-export-options-template org-export-as-html-and-open
  2060. org-export-as-html-batch org-export-as-html-to-buffer
  2061. org-replace-region-by-html org-export-region-as-html
  2062. org-export-as-html org-export-icalendar-this-file
  2063. org-export-icalendar-all-agenda-files
  2064. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2065. ;; Declare and autoload functions from org-exp.el
  2066. (eval-and-compile
  2067. (org-autoload "org-exp"
  2068. '(org-agenda org-agenda-list org-search-view
  2069. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2070. org-diary org-agenda-to-appt)))
  2071. ;; Autoload org-remember
  2072. (eval-and-compile
  2073. (org-autoload "org-remember"
  2074. '(org-remember-insinuate org-remember-annotation
  2075. org-remember-apply-template org-remember org-remember-handler)))
  2076. ;; Autoload org-clock.el
  2077. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2078. (beg end))
  2079. (defvar org-clock-marker (make-marker)
  2080. "Marker recording the last clock-in.")
  2081. (eval-and-compile
  2082. (org-autoload
  2083. "org-clock"
  2084. '(org-clock-in org-clock-out org-clock-cancel
  2085. org-clock-goto org-clock-sum org-clock-display
  2086. org-remove-clock-overlays org-clock-report
  2087. org-clocktable-shift org-dblock-write:clocktable
  2088. org-get-clocktable)))
  2089. (defun org-clock-update-time-maybe ()
  2090. "If this is a CLOCK line, update it and return t.
  2091. Otherwise, return nil."
  2092. (interactive)
  2093. (save-excursion
  2094. (beginning-of-line 1)
  2095. (skip-chars-forward " \t")
  2096. (when (looking-at org-clock-string)
  2097. (let ((re (concat "[ \t]*" org-clock-string
  2098. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  2099. "\\([ \t]*=>.*\\)?"))
  2100. ts te h m s)
  2101. (if (not (looking-at re))
  2102. nil
  2103. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  2104. (end-of-line 1)
  2105. (setq ts (match-string 1)
  2106. te (match-string 2))
  2107. (setq s (- (time-to-seconds
  2108. (apply 'encode-time (org-parse-time-string te)))
  2109. (time-to-seconds
  2110. (apply 'encode-time (org-parse-time-string ts))))
  2111. h (floor (/ s 3600))
  2112. s (- s (* 3600 h))
  2113. m (floor (/ s 60))
  2114. s (- s (* 60 s)))
  2115. (insert " => " (format "%2d:%02d" h m))
  2116. t)))))
  2117. (defun org-check-running-clock ()
  2118. "Check if the current buffer contains the running clock.
  2119. If yes, offer to stop it and to save the buffer with the changes."
  2120. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2121. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2122. (buffer-name))))
  2123. (org-clock-out)
  2124. (when (y-or-n-p "Save changed buffer?")
  2125. (save-buffer))))
  2126. (defun org-clocktable-try-shift (dir n)
  2127. "Check if this line starts a clock table, if yes, shift the time block."
  2128. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2129. (org-clocktable-shift dir n)))
  2130. ;; Autoload archiving code
  2131. ;; The stuff that is needed for cycling and tags has to be defined here.
  2132. (defgroup org-archive nil
  2133. "Options concerning archiving in Org-mode."
  2134. :tag "Org Archive"
  2135. :group 'org-structure)
  2136. (defcustom org-archive-location "%s_archive::"
  2137. "The location where subtrees should be archived.
  2138. Otherwise, the value of this variable is a string, consisting of two
  2139. parts, separated by a double-colon.
  2140. The first part is a file name - when omitted, archiving happens in the same
  2141. file. %s will be replaced by the current file name (without directory part).
  2142. Archiving to a different file is useful to keep archived entries from
  2143. contributing to the Org-mode Agenda.
  2144. The part after the double colon is a headline. The archived entries will be
  2145. filed under that headline. When omitted, the subtrees are simply filed away
  2146. at the end of the file, as top-level entries.
  2147. Here are a few examples:
  2148. \"%s_archive::\"
  2149. If the current file is Projects.org, archive in file
  2150. Projects.org_archive, as top-level trees. This is the default.
  2151. \"::* Archived Tasks\"
  2152. Archive in the current file, under the top-level headline
  2153. \"* Archived Tasks\".
  2154. \"~/org/archive.org::\"
  2155. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2156. \"basement::** Finished Tasks\"
  2157. Archive in file ./basement (relative path), as level 3 trees
  2158. below the level 2 heading \"** Finished Tasks\".
  2159. You may set this option on a per-file basis by adding to the buffer a
  2160. line like
  2161. #+ARCHIVE: basement::** Finished Tasks
  2162. You may also define it locally for a subtree by setting an ARCHIVE property
  2163. in the entry. If such a property is found in an entry, or anywhere up
  2164. the hierarchy, it will be used."
  2165. :group 'org-archive
  2166. :type 'string)
  2167. (defcustom org-archive-tag "ARCHIVE"
  2168. "The tag that marks a subtree as archived.
  2169. An archived subtree does not open during visibility cycling, and does
  2170. not contribute to the agenda listings.
  2171. After changing this, font-lock must be restarted in the relevant buffers to
  2172. get the proper fontification."
  2173. :group 'org-archive
  2174. :group 'org-keywords
  2175. :type 'string)
  2176. (defcustom org-agenda-skip-archived-trees t
  2177. "Non-nil means, the agenda will skip any items located in archived trees.
  2178. An archived tree is a tree marked with the tag ARCHIVE."
  2179. :group 'org-archive
  2180. :group 'org-agenda-skip
  2181. :type 'boolean)
  2182. (defcustom org-cycle-open-archived-trees nil
  2183. "Non-nil means, `org-cycle' will open archived trees.
  2184. An archived tree is a tree marked with the tag ARCHIVE.
  2185. When nil, archived trees will stay folded. You can still open them with
  2186. normal outline commands like `show-all', but not with the cycling commands."
  2187. :group 'org-archive
  2188. :group 'org-cycle
  2189. :type 'boolean)
  2190. (defcustom org-sparse-tree-open-archived-trees nil
  2191. "Non-nil means sparse tree construction shows matches in archived trees.
  2192. When nil, matches in these trees are highlighted, but the trees are kept in
  2193. collapsed state."
  2194. :group 'org-archive
  2195. :group 'org-sparse-trees
  2196. :type 'boolean)
  2197. (defun org-cycle-hide-archived-subtrees (state)
  2198. "Re-hide all archived subtrees after a visibility state change."
  2199. (when (and (not org-cycle-open-archived-trees)
  2200. (not (memq state '(overview folded))))
  2201. (save-excursion
  2202. (let* ((globalp (memq state '(contents all)))
  2203. (beg (if globalp (point-min) (point)))
  2204. (end (if globalp (point-max) (org-end-of-subtree t))))
  2205. (org-hide-archived-subtrees beg end)
  2206. (goto-char beg)
  2207. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2208. (message "%s" (substitute-command-keys
  2209. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2210. (defun org-force-cycle-archived ()
  2211. "Cycle subtree even if it is archived."
  2212. (interactive)
  2213. (setq this-command 'org-cycle)
  2214. (let ((org-cycle-open-archived-trees t))
  2215. (call-interactively 'org-cycle)))
  2216. (defun org-hide-archived-subtrees (beg end)
  2217. "Re-hide all archived subtrees after a visibility state change."
  2218. (save-excursion
  2219. (let* ((re (concat ":" org-archive-tag ":")))
  2220. (goto-char beg)
  2221. (while (re-search-forward re end t)
  2222. (and (org-on-heading-p) (hide-subtree))
  2223. (org-end-of-subtree t)))))
  2224. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2225. (eval-and-compile
  2226. (org-autoload "org-archive"
  2227. '(org-add-archive-files org-archive-subtree
  2228. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2229. ;; Autoload Column View Code
  2230. (declare-function org-columns-number-to-string "org-colview")
  2231. (declare-function org-columns-get-format-and-top-level "org-colview")
  2232. (declare-function org-columns-compute "org-colview")
  2233. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2234. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2235. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2236. org-columns org-insert-columns-dblock))
  2237. ;;; Variables for pre-computed regular expressions, all buffer local
  2238. (defvar org-drawer-regexp nil
  2239. "Matches first line of a hidden block.")
  2240. (make-variable-buffer-local 'org-drawer-regexp)
  2241. (defvar org-todo-regexp nil
  2242. "Matches any of the TODO state keywords.")
  2243. (make-variable-buffer-local 'org-todo-regexp)
  2244. (defvar org-not-done-regexp nil
  2245. "Matches any of the TODO state keywords except the last one.")
  2246. (make-variable-buffer-local 'org-not-done-regexp)
  2247. (defvar org-todo-line-regexp nil
  2248. "Matches a headline and puts TODO state into group 2 if present.")
  2249. (make-variable-buffer-local 'org-todo-line-regexp)
  2250. (defvar org-complex-heading-regexp nil
  2251. "Matches a headline and puts everything into groups:
  2252. group 1: the stars
  2253. group 2: The todo keyword, maybe
  2254. group 3: Priority cookie
  2255. group 4: True headline
  2256. group 5: Tags")
  2257. (make-variable-buffer-local 'org-complex-heading-regexp)
  2258. (defvar org-todo-line-tags-regexp nil
  2259. "Matches a headline and puts TODO state into group 2 if present.
  2260. Also put tags into group 4 if tags are present.")
  2261. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2262. (defvar org-nl-done-regexp nil
  2263. "Matches newline followed by a headline with the DONE keyword.")
  2264. (make-variable-buffer-local 'org-nl-done-regexp)
  2265. (defvar org-looking-at-done-regexp nil
  2266. "Matches the DONE keyword a point.")
  2267. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2268. (defvar org-ds-keyword-length 12
  2269. "Maximum length of the Deadline and SCHEDULED keywords.")
  2270. (make-variable-buffer-local 'org-ds-keyword-length)
  2271. (defvar org-deadline-regexp nil
  2272. "Matches the DEADLINE keyword.")
  2273. (make-variable-buffer-local 'org-deadline-regexp)
  2274. (defvar org-deadline-time-regexp nil
  2275. "Matches the DEADLINE keyword together with a time stamp.")
  2276. (make-variable-buffer-local 'org-deadline-time-regexp)
  2277. (defvar org-deadline-line-regexp nil
  2278. "Matches the DEADLINE keyword and the rest of the line.")
  2279. (make-variable-buffer-local 'org-deadline-line-regexp)
  2280. (defvar org-scheduled-regexp nil
  2281. "Matches the SCHEDULED keyword.")
  2282. (make-variable-buffer-local 'org-scheduled-regexp)
  2283. (defvar org-scheduled-time-regexp nil
  2284. "Matches the SCHEDULED keyword together with a time stamp.")
  2285. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2286. (defvar org-closed-time-regexp nil
  2287. "Matches the CLOSED keyword together with a time stamp.")
  2288. (make-variable-buffer-local 'org-closed-time-regexp)
  2289. (defvar org-keyword-time-regexp nil
  2290. "Matches any of the 4 keywords, together with the time stamp.")
  2291. (make-variable-buffer-local 'org-keyword-time-regexp)
  2292. (defvar org-keyword-time-not-clock-regexp nil
  2293. "Matches any of the 3 keywords, together with the time stamp.")
  2294. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2295. (defvar org-maybe-keyword-time-regexp nil
  2296. "Matches a timestamp, possibly preceeded by a keyword.")
  2297. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2298. (defvar org-planning-or-clock-line-re nil
  2299. "Matches a line with planning or clock info.")
  2300. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2301. (defconst org-plain-time-of-day-regexp
  2302. (concat
  2303. "\\(\\<[012]?[0-9]"
  2304. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2305. "\\(--?"
  2306. "\\(\\<[012]?[0-9]"
  2307. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2308. "\\)?")
  2309. "Regular expression to match a plain time or time range.
  2310. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2311. groups carry important information:
  2312. 0 the full match
  2313. 1 the first time, range or not
  2314. 8 the second time, if it is a range.")
  2315. (defconst org-plain-time-extension-regexp
  2316. (concat
  2317. "\\(\\<[012]?[0-9]"
  2318. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2319. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2320. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2321. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2322. groups carry important information:
  2323. 0 the full match
  2324. 7 hours of duration
  2325. 9 minutes of duration")
  2326. (defconst org-stamp-time-of-day-regexp
  2327. (concat
  2328. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2329. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2330. "\\(--?"
  2331. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2332. "Regular expression to match a timestamp time or time range.
  2333. After a match, the following groups carry important information:
  2334. 0 the full match
  2335. 1 date plus weekday, for backreferencing to make sure both times on same day
  2336. 2 the first time, range or not
  2337. 4 the second time, if it is a range.")
  2338. (defconst org-startup-options
  2339. '(("fold" org-startup-folded t)
  2340. ("overview" org-startup-folded t)
  2341. ("nofold" org-startup-folded nil)
  2342. ("showall" org-startup-folded nil)
  2343. ("content" org-startup-folded content)
  2344. ("hidestars" org-hide-leading-stars t)
  2345. ("showstars" org-hide-leading-stars nil)
  2346. ("odd" org-odd-levels-only t)
  2347. ("oddeven" org-odd-levels-only nil)
  2348. ("align" org-startup-align-all-tables t)
  2349. ("noalign" org-startup-align-all-tables nil)
  2350. ("customtime" org-display-custom-times t)
  2351. ("logdone" org-log-done time)
  2352. ("lognotedone" org-log-done note)
  2353. ("nologdone" org-log-done nil)
  2354. ("lognoteclock-out" org-log-note-clock-out t)
  2355. ("nolognoteclock-out" org-log-note-clock-out nil)
  2356. ("logrepeat" org-log-repeat state)
  2357. ("lognoterepeat" org-log-repeat note)
  2358. ("nologrepeat" org-log-repeat nil)
  2359. ("constcgs" constants-unit-system cgs)
  2360. ("constSI" constants-unit-system SI))
  2361. "Variable associated with STARTUP options for org-mode.
  2362. Each element is a list of three items: The startup options as written
  2363. in the #+STARTUP line, the corresponding variable, and the value to
  2364. set this variable to if the option is found. An optional forth element PUSH
  2365. means to push this value onto the list in the variable.")
  2366. (defun org-set-regexps-and-options ()
  2367. "Precompute regular expressions for current buffer."
  2368. (when (org-mode-p)
  2369. (org-set-local 'org-todo-kwd-alist nil)
  2370. (org-set-local 'org-todo-key-alist nil)
  2371. (org-set-local 'org-todo-key-trigger nil)
  2372. (org-set-local 'org-todo-keywords-1 nil)
  2373. (org-set-local 'org-done-keywords nil)
  2374. (org-set-local 'org-todo-heads nil)
  2375. (org-set-local 'org-todo-sets nil)
  2376. (org-set-local 'org-todo-log-states nil)
  2377. (let ((re (org-make-options-regexp
  2378. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2379. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2380. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2381. (splitre "[ \t]+")
  2382. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2383. tail sep kws1 prio props drawers
  2384. ext-setup-or-nil setup-contents (start 0))
  2385. (save-excursion
  2386. (save-restriction
  2387. (widen)
  2388. (goto-char (point-min))
  2389. (while (or (and ext-setup-or-nil
  2390. (string-match re ext-setup-or-nil start)
  2391. (setq start (match-end 0)))
  2392. (and (setq ext-setup-or-nil nil start 0)
  2393. (re-search-forward re nil t)))
  2394. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2395. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2396. (cond
  2397. ((equal key "CATEGORY")
  2398. (if (string-match "[ \t]+$" value)
  2399. (setq value (replace-match "" t t value)))
  2400. (setq cat value))
  2401. ((member key '("SEQ_TODO" "TODO"))
  2402. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2403. ((equal key "TYP_TODO")
  2404. (push (cons 'type (org-split-string value splitre)) kwds))
  2405. ((equal key "TAGS")
  2406. (setq tags (append tags (org-split-string value splitre))))
  2407. ((equal key "COLUMNS")
  2408. (org-set-local 'org-columns-default-format value))
  2409. ((equal key "LINK")
  2410. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2411. (push (cons (match-string 1 value)
  2412. (org-trim (match-string 2 value)))
  2413. links)))
  2414. ((equal key "PRIORITIES")
  2415. (setq prio (org-split-string value " +")))
  2416. ((equal key "PROPERTY")
  2417. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2418. (push (cons (match-string 1 value) (match-string 2 value))
  2419. props)))
  2420. ((equal key "DRAWERS")
  2421. (setq drawers (org-split-string value splitre)))
  2422. ((equal key "CONSTANTS")
  2423. (setq const (append const (org-split-string value splitre))))
  2424. ((equal key "STARTUP")
  2425. (let ((opts (org-split-string value splitre))
  2426. l var val)
  2427. (while (setq l (pop opts))
  2428. (when (setq l (assoc l org-startup-options))
  2429. (setq var (nth 1 l) val (nth 2 l))
  2430. (if (not (nth 3 l))
  2431. (set (make-local-variable var) val)
  2432. (if (not (listp (symbol-value var)))
  2433. (set (make-local-variable var) nil))
  2434. (set (make-local-variable var) (symbol-value var))
  2435. (add-to-list var val))))))
  2436. ((equal key "ARCHIVE")
  2437. (string-match " *$" value)
  2438. (setq arch (replace-match "" t t value))
  2439. (remove-text-properties 0 (length arch)
  2440. '(face t fontified t) arch))
  2441. ((equal key "SETUPFILE")
  2442. (setq setup-contents (org-file-contents
  2443. (expand-file-name
  2444. (org-remove-double-quotes value))
  2445. 'noerror))
  2446. (if (not ext-setup-or-nil)
  2447. (setq ext-setup-or-nil setup-contents start 0)
  2448. (setq ext-setup-or-nil
  2449. (concat (substring ext-setup-or-nil 0 start)
  2450. "\n" setup-contents "\n"
  2451. (substring ext-setup-or-nil start)))))
  2452. ))))
  2453. (when cat
  2454. (org-set-local 'org-category (intern cat))
  2455. (push (cons "CATEGORY" cat) props))
  2456. (when prio
  2457. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2458. (setq prio (mapcar 'string-to-char prio))
  2459. (org-set-local 'org-highest-priority (nth 0 prio))
  2460. (org-set-local 'org-lowest-priority (nth 1 prio))
  2461. (org-set-local 'org-default-priority (nth 2 prio)))
  2462. (and props (org-set-local 'org-local-properties (nreverse props)))
  2463. (and drawers (org-set-local 'org-drawers drawers))
  2464. (and arch (org-set-local 'org-archive-location arch))
  2465. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2466. ;; Process the TODO keywords
  2467. (unless kwds
  2468. ;; Use the global values as if they had been given locally.
  2469. (setq kwds (default-value 'org-todo-keywords))
  2470. (if (stringp (car kwds))
  2471. (setq kwds (list (cons org-todo-interpretation
  2472. (default-value 'org-todo-keywords)))))
  2473. (setq kwds (reverse kwds)))
  2474. (setq kwds (nreverse kwds))
  2475. (let (inter kws kw)
  2476. (while (setq kws (pop kwds))
  2477. (setq inter (pop kws) sep (member "|" kws)
  2478. kws0 (delete "|" (copy-sequence kws))
  2479. kwsa nil
  2480. kws1 (mapcar
  2481. (lambda (x)
  2482. ;; 1 2
  2483. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2484. (progn
  2485. (setq kw (match-string 1 x)
  2486. key (and (match-end 2) (match-string 2 x))
  2487. log (org-extract-log-state-settings x))
  2488. (push (cons kw (and key (string-to-char key))) kwsa)
  2489. (and log (push log org-todo-log-states))
  2490. kw)
  2491. (error "Invalid TODO keyword %s" x)))
  2492. kws0)
  2493. kwsa (if kwsa (append '((:startgroup))
  2494. (nreverse kwsa)
  2495. '((:endgroup))))
  2496. hw (car kws1)
  2497. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2498. tail (list inter hw (car dws) (org-last dws)))
  2499. (add-to-list 'org-todo-heads hw 'append)
  2500. (push kws1 org-todo-sets)
  2501. (setq org-done-keywords (append org-done-keywords dws nil))
  2502. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2503. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2504. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2505. (setq org-todo-sets (nreverse org-todo-sets)
  2506. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2507. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2508. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2509. ;; Process the constants
  2510. (when const
  2511. (let (e cst)
  2512. (while (setq e (pop const))
  2513. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2514. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2515. (setq org-table-formula-constants-local cst)))
  2516. ;; Process the tags.
  2517. (when tags
  2518. (let (e tgs)
  2519. (while (setq e (pop tags))
  2520. (cond
  2521. ((equal e "{") (push '(:startgroup) tgs))
  2522. ((equal e "}") (push '(:endgroup) tgs))
  2523. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2524. (push (cons (match-string 1 e)
  2525. (string-to-char (match-string 2 e)))
  2526. tgs))
  2527. (t (push (list e) tgs))))
  2528. (org-set-local 'org-tag-alist nil)
  2529. (while (setq e (pop tgs))
  2530. (or (and (stringp (car e))
  2531. (assoc (car e) org-tag-alist))
  2532. (push e org-tag-alist))))))
  2533. ;; Compute the regular expressions and other local variables
  2534. (if (not org-done-keywords)
  2535. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2536. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2537. (length org-scheduled-string)
  2538. (length org-clock-string)
  2539. (length org-closed-string)))
  2540. org-drawer-regexp
  2541. (concat "^[ \t]*:\\("
  2542. (mapconcat 'regexp-quote org-drawers "\\|")
  2543. "\\):[ \t]*$")
  2544. org-not-done-keywords
  2545. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2546. org-todo-regexp
  2547. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2548. "\\|") "\\)\\>")
  2549. org-not-done-regexp
  2550. (concat "\\<\\("
  2551. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2552. "\\)\\>")
  2553. org-todo-line-regexp
  2554. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2555. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2556. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2557. org-complex-heading-regexp
  2558. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2559. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2560. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2561. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2562. org-nl-done-regexp
  2563. (concat "\n\\*+[ \t]+"
  2564. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2565. "\\)" "\\>")
  2566. org-todo-line-tags-regexp
  2567. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2568. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2569. (org-re
  2570. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2571. org-looking-at-done-regexp
  2572. (concat "^" "\\(?:"
  2573. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2574. "\\>")
  2575. org-deadline-regexp (concat "\\<" org-deadline-string)
  2576. org-deadline-time-regexp
  2577. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2578. org-deadline-line-regexp
  2579. (concat "\\<\\(" org-deadline-string "\\).*")
  2580. org-scheduled-regexp
  2581. (concat "\\<" org-scheduled-string)
  2582. org-scheduled-time-regexp
  2583. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2584. org-closed-time-regexp
  2585. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2586. org-keyword-time-regexp
  2587. (concat "\\<\\(" org-scheduled-string
  2588. "\\|" org-deadline-string
  2589. "\\|" org-closed-string
  2590. "\\|" org-clock-string "\\)"
  2591. " *[[<]\\([^]>]+\\)[]>]")
  2592. org-keyword-time-not-clock-regexp
  2593. (concat "\\<\\(" org-scheduled-string
  2594. "\\|" org-deadline-string
  2595. "\\|" org-closed-string
  2596. "\\)"
  2597. " *[[<]\\([^]>]+\\)[]>]")
  2598. org-maybe-keyword-time-regexp
  2599. (concat "\\(\\<\\(" org-scheduled-string
  2600. "\\|" org-deadline-string
  2601. "\\|" org-closed-string
  2602. "\\|" org-clock-string "\\)\\)?"
  2603. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2604. org-planning-or-clock-line-re
  2605. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2606. "\\|" org-deadline-string
  2607. "\\|" org-closed-string "\\|" org-clock-string
  2608. "\\)\\>\\)")
  2609. )
  2610. (org-compute-latex-and-specials-regexp)
  2611. (org-set-font-lock-defaults)))
  2612. (defun org-file-contents (file &optional noerror)
  2613. "Return the contents of FILE, as a string."
  2614. (if (or (not file)
  2615. (not (file-readable-p file)))
  2616. (if noerror
  2617. (progn
  2618. (message "Cannot read file %s" file)
  2619. (ding) (sit-for 2)
  2620. "")
  2621. (error "Cannot read file %s" file))
  2622. (with-temp-buffer
  2623. (insert-file-contents file)
  2624. (buffer-string))))
  2625. (defun org-extract-log-state-settings (x)
  2626. "Extract the log state setting from a TODO keyword string.
  2627. This will extract info from a string like \"WAIT(w@/!)\"."
  2628. (let (kw key log1 log2)
  2629. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2630. (setq kw (match-string 1 x)
  2631. key (and (match-end 2) (match-string 2 x))
  2632. log1 (and (match-end 3) (match-string 3 x))
  2633. log2 (and (match-end 4) (match-string 4 x)))
  2634. (and (or log1 log2)
  2635. (list kw
  2636. (and log1 (if (equal log1 "!") 'time 'note))
  2637. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2638. (defun org-remove-keyword-keys (list)
  2639. "Remove a pair of parenthesis at the end of each string in LIST."
  2640. (mapcar (lambda (x)
  2641. (if (string-match "(.*)$" x)
  2642. (substring x 0 (match-beginning 0))
  2643. x))
  2644. list))
  2645. ;; FIXME: this could be done much better, using second characters etc.
  2646. (defun org-assign-fast-keys (alist)
  2647. "Assign fast keys to a keyword-key alist.
  2648. Respect keys that are already there."
  2649. (let (new e k c c1 c2 (char ?a))
  2650. (while (setq e (pop alist))
  2651. (cond
  2652. ((equal e '(:startgroup)) (push e new))
  2653. ((equal e '(:endgroup)) (push e new))
  2654. (t
  2655. (setq k (car e) c2 nil)
  2656. (if (cdr e)
  2657. (setq c (cdr e))
  2658. ;; automatically assign a character.
  2659. (setq c1 (string-to-char
  2660. (downcase (substring
  2661. k (if (= (string-to-char k) ?@) 1 0)))))
  2662. (if (or (rassoc c1 new) (rassoc c1 alist))
  2663. (while (or (rassoc char new) (rassoc char alist))
  2664. (setq char (1+ char)))
  2665. (setq c2 c1))
  2666. (setq c (or c2 char)))
  2667. (push (cons k c) new))))
  2668. (nreverse new)))
  2669. ;;; Some variables used in various places
  2670. (defvar org-window-configuration nil
  2671. "Used in various places to store a window configuration.")
  2672. (defvar org-finish-function nil
  2673. "Function to be called when `C-c C-c' is used.
  2674. This is for getting out of special buffers like remember.")
  2675. ;; FIXME: Occasionally check by commenting these, to make sure
  2676. ;; no other functions uses these, forgetting to let-bind them.
  2677. (defvar entry)
  2678. (defvar state)
  2679. (defvar last-state)
  2680. (defvar date)
  2681. (defvar description)
  2682. ;; Defined somewhere in this file, but used before definition.
  2683. (defvar org-html-entities)
  2684. (defvar org-struct-menu)
  2685. (defvar org-org-menu)
  2686. (defvar org-tbl-menu)
  2687. (defvar org-agenda-keymap)
  2688. ;;;; Define the Org-mode
  2689. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2690. (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."))
  2691. ;; We use a before-change function to check if a table might need
  2692. ;; an update.
  2693. (defvar org-table-may-need-update t
  2694. "Indicates that a table might need an update.
  2695. This variable is set by `org-before-change-function'.
  2696. `org-table-align' sets it back to nil.")
  2697. (defun org-before-change-function (beg end)
  2698. "Every change indicates that a table might need an update."
  2699. (setq org-table-may-need-update t))
  2700. (defvar org-mode-map)
  2701. (defvar org-mode-hook nil)
  2702. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2703. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2704. (defvar org-table-buffer-is-an nil)
  2705. (defconst org-outline-regexp "\\*+ ")
  2706. ;;;###autoload
  2707. (define-derived-mode org-mode outline-mode "Org"
  2708. "Outline-based notes management and organizer, alias
  2709. \"Carsten's outline-mode for keeping track of everything.\"
  2710. Org-mode develops organizational tasks around a NOTES file which
  2711. contains information about projects as plain text. Org-mode is
  2712. implemented on top of outline-mode, which is ideal to keep the content
  2713. of large files well structured. It supports ToDo items, deadlines and
  2714. time stamps, which magically appear in the diary listing of the Emacs
  2715. calendar. Tables are easily created with a built-in table editor.
  2716. Plain text URL-like links connect to websites, emails (VM), Usenet
  2717. messages (Gnus), BBDB entries, and any files related to the project.
  2718. For printing and sharing of notes, an Org-mode file (or a part of it)
  2719. can be exported as a structured ASCII or HTML file.
  2720. The following commands are available:
  2721. \\{org-mode-map}"
  2722. ;; Get rid of Outline menus, they are not needed
  2723. ;; Need to do this here because define-derived-mode sets up
  2724. ;; the keymap so late. Still, it is a waste to call this each time
  2725. ;; we switch another buffer into org-mode.
  2726. (if (featurep 'xemacs)
  2727. (when (boundp 'outline-mode-menu-heading)
  2728. ;; Assume this is Greg's port, it used easymenu
  2729. (easy-menu-remove outline-mode-menu-heading)
  2730. (easy-menu-remove outline-mode-menu-show)
  2731. (easy-menu-remove outline-mode-menu-hide))
  2732. (define-key org-mode-map [menu-bar headings] 'undefined)
  2733. (define-key org-mode-map [menu-bar hide] 'undefined)
  2734. (define-key org-mode-map [menu-bar show] 'undefined))
  2735. (org-load-modules-maybe)
  2736. (easy-menu-add org-org-menu)
  2737. (easy-menu-add org-tbl-menu)
  2738. (org-install-agenda-files-menu)
  2739. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2740. (org-add-to-invisibility-spec '(org-cwidth))
  2741. (when (featurep 'xemacs)
  2742. (org-set-local 'line-move-ignore-invisible t))
  2743. (org-set-local 'outline-regexp org-outline-regexp)
  2744. (org-set-local 'outline-level 'org-outline-level)
  2745. (when (and org-ellipsis
  2746. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2747. (fboundp 'make-glyph-code))
  2748. (unless org-display-table
  2749. (setq org-display-table (make-display-table)))
  2750. (set-display-table-slot
  2751. org-display-table 4
  2752. (vconcat (mapcar
  2753. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2754. org-ellipsis)))
  2755. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2756. (setq buffer-display-table org-display-table))
  2757. (org-set-regexps-and-options)
  2758. ;; Calc embedded
  2759. (org-set-local 'calc-embedded-open-mode "# ")
  2760. (modify-syntax-entry ?# "<")
  2761. (modify-syntax-entry ?@ "w")
  2762. (if org-startup-truncated (setq truncate-lines t))
  2763. (org-set-local 'font-lock-unfontify-region-function
  2764. 'org-unfontify-region)
  2765. ;; Activate before-change-function
  2766. (org-set-local 'org-table-may-need-update t)
  2767. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2768. 'local)
  2769. ;; Check for running clock before killing a buffer
  2770. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2771. ;; Paragraphs and auto-filling
  2772. (org-set-autofill-regexps)
  2773. (setq indent-line-function 'org-indent-line-function)
  2774. (org-update-radio-target-regexp)
  2775. ;; Comment characters
  2776. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2777. (org-set-local 'comment-padding " ")
  2778. ;; Align options lines
  2779. (org-set-local
  2780. 'align-mode-rules-list
  2781. '((org-in-buffer-settings
  2782. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2783. (modes . '(org-mode)))))
  2784. ;; Imenu
  2785. (org-set-local 'imenu-create-index-function
  2786. 'org-imenu-get-tree)
  2787. ;; Make isearch reveal context
  2788. (if (or (featurep 'xemacs)
  2789. (not (boundp 'outline-isearch-open-invisible-function)))
  2790. ;; Emacs 21 and XEmacs make use of the hook
  2791. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2792. ;; Emacs 22 deals with this through a special variable
  2793. (org-set-local 'outline-isearch-open-invisible-function
  2794. (lambda (&rest ignore) (org-show-context 'isearch))))
  2795. ;; If empty file that did not turn on org-mode automatically, make it to.
  2796. (if (and org-insert-mode-line-in-empty-file
  2797. (interactive-p)
  2798. (= (point-min) (point-max)))
  2799. (insert "# -*- mode: org -*-\n\n"))
  2800. (unless org-inhibit-startup
  2801. (when org-startup-align-all-tables
  2802. (let ((bmp (buffer-modified-p)))
  2803. (org-table-map-tables 'org-table-align)
  2804. (set-buffer-modified-p bmp)))
  2805. (org-set-startup-visibility)))
  2806. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2807. (defun org-current-time ()
  2808. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2809. (if (> (car org-time-stamp-rounding-minutes) 1)
  2810. (let ((r (car org-time-stamp-rounding-minutes))
  2811. (time (decode-time)))
  2812. (apply 'encode-time
  2813. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2814. (nthcdr 2 time))))
  2815. (current-time)))
  2816. ;;;; Font-Lock stuff, including the activators
  2817. (defvar org-mouse-map (make-sparse-keymap))
  2818. (org-defkey org-mouse-map
  2819. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2820. (org-defkey org-mouse-map
  2821. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2822. (when org-mouse-1-follows-link
  2823. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2824. (when org-tab-follows-link
  2825. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2826. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2827. (when org-return-follows-link
  2828. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2829. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2830. (require 'font-lock)
  2831. (defconst org-non-link-chars "]\t\n\r<>")
  2832. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2833. "shell" "elisp"))
  2834. (defvar org-link-types-re nil
  2835. "Matches a link that has a url-like prefix like \"http:\"")
  2836. (defvar org-link-re-with-space nil
  2837. "Matches a link with spaces, optional angular brackets around it.")
  2838. (defvar org-link-re-with-space2 nil
  2839. "Matches a link with spaces, optional angular brackets around it.")
  2840. (defvar org-angle-link-re nil
  2841. "Matches link with angular brackets, spaces are allowed.")
  2842. (defvar org-plain-link-re nil
  2843. "Matches plain link, without spaces.")
  2844. (defvar org-bracket-link-regexp nil
  2845. "Matches a link in double brackets.")
  2846. (defvar org-bracket-link-analytic-regexp nil
  2847. "Regular expression used to analyze links.
  2848. Here is what the match groups contain after a match:
  2849. 1: http:
  2850. 2: http
  2851. 3: path
  2852. 4: [desc]
  2853. 5: desc")
  2854. (defvar org-any-link-re nil
  2855. "Regular expression matching any link.")
  2856. (defun org-make-link-regexps ()
  2857. "Update the link regular expressions.
  2858. This should be called after the variable `org-link-types' has changed."
  2859. (setq org-link-types-re
  2860. (concat
  2861. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2862. org-link-re-with-space
  2863. (concat
  2864. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2865. "\\([^" org-non-link-chars " ]"
  2866. "[^" org-non-link-chars "]*"
  2867. "[^" org-non-link-chars " ]\\)>?")
  2868. org-link-re-with-space2
  2869. (concat
  2870. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2871. "\\([^" org-non-link-chars " ]"
  2872. "[^]\t\n\r]*"
  2873. "[^" org-non-link-chars " ]\\)>?")
  2874. org-angle-link-re
  2875. (concat
  2876. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2877. "\\([^" org-non-link-chars " ]"
  2878. "[^" org-non-link-chars "]*"
  2879. "\\)>")
  2880. org-plain-link-re
  2881. (concat
  2882. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2883. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2884. org-bracket-link-regexp
  2885. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2886. org-bracket-link-analytic-regexp
  2887. (concat
  2888. "\\[\\["
  2889. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2890. "\\([^]]+\\)"
  2891. "\\]"
  2892. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2893. "\\]")
  2894. org-any-link-re
  2895. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  2896. org-angle-link-re "\\)\\|\\("
  2897. org-plain-link-re "\\)")))
  2898. (org-make-link-regexps)
  2899. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  2900. "Regular expression for fast time stamp matching.")
  2901. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  2902. "Regular expression for fast time stamp matching.")
  2903. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2904. "Regular expression matching time strings for analysis.
  2905. This one does not require the space after the date, so it can be used
  2906. on a string that terminates immediately after the date.")
  2907. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2908. "Regular expression matching time strings for analysis.")
  2909. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  2910. "Regular expression matching time stamps, with groups.")
  2911. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  2912. "Regular expression matching time stamps (also [..]), with groups.")
  2913. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2914. "Regular expression matching a time stamp range.")
  2915. (defconst org-tr-regexp-both
  2916. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  2917. "Regular expression matching a time stamp range.")
  2918. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2919. org-ts-regexp "\\)?")
  2920. "Regular expression matching a time stamp or time stamp range.")
  2921. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  2922. org-ts-regexp-both "\\)?")
  2923. "Regular expression matching a time stamp or time stamp range.
  2924. The time stamps may be either active or inactive.")
  2925. (defvar org-emph-face nil)
  2926. (defun org-do-emphasis-faces (limit)
  2927. "Run through the buffer and add overlays to links."
  2928. (let (rtn)
  2929. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  2930. (if (not (= (char-after (match-beginning 3))
  2931. (char-after (match-beginning 4))))
  2932. (progn
  2933. (setq rtn t)
  2934. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2935. 'face
  2936. (nth 1 (assoc (match-string 3)
  2937. org-emphasis-alist)))
  2938. (add-text-properties (match-beginning 2) (match-end 2)
  2939. '(font-lock-multiline t))
  2940. (when org-hide-emphasis-markers
  2941. (add-text-properties (match-end 4) (match-beginning 5)
  2942. '(invisible org-link))
  2943. (add-text-properties (match-beginning 3) (match-end 3)
  2944. '(invisible org-link)))))
  2945. (backward-char 1))
  2946. rtn))
  2947. (defun org-emphasize (&optional char)
  2948. "Insert or change an emphasis, i.e. a font like bold or italic.
  2949. If there is an active region, change that region to a new emphasis.
  2950. If there is no region, just insert the marker characters and position
  2951. the cursor between them.
  2952. CHAR should be either the marker character, or the first character of the
  2953. HTML tag associated with that emphasis. If CHAR is a space, the means
  2954. to remove the emphasis of the selected region.
  2955. If char is not given (for example in an interactive call) it
  2956. will be prompted for."
  2957. (interactive)
  2958. (let ((eal org-emphasis-alist) e det
  2959. (erc org-emphasis-regexp-components)
  2960. (prompt "")
  2961. (string "") beg end move tag c s)
  2962. (if (org-region-active-p)
  2963. (setq beg (region-beginning) end (region-end)
  2964. string (buffer-substring beg end))
  2965. (setq move t))
  2966. (while (setq e (pop eal))
  2967. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  2968. c (aref tag 0))
  2969. (push (cons c (string-to-char (car e))) det)
  2970. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  2971. (substring tag 1)))))
  2972. (unless char
  2973. (message "%s" (concat "Emphasis marker or tag:" prompt))
  2974. (setq char (read-char-exclusive)))
  2975. (setq char (or (cdr (assoc char det)) char))
  2976. (if (equal char ?\ )
  2977. (setq s "" move nil)
  2978. (unless (assoc (char-to-string char) org-emphasis-alist)
  2979. (error "No such emphasis marker: \"%c\"" char))
  2980. (setq s (char-to-string char)))
  2981. (while (and (> (length string) 1)
  2982. (equal (substring string 0 1) (substring string -1))
  2983. (assoc (substring string 0 1) org-emphasis-alist))
  2984. (setq string (substring string 1 -1)))
  2985. (setq string (concat s string s))
  2986. (if beg (delete-region beg end))
  2987. (unless (or (bolp)
  2988. (string-match (concat "[" (nth 0 erc) "\n]")
  2989. (char-to-string (char-before (point)))))
  2990. (insert " "))
  2991. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  2992. (char-to-string (char-after (point))))
  2993. (insert " ") (backward-char 1))
  2994. (insert string)
  2995. (and move (backward-char 1))))
  2996. (defconst org-nonsticky-props
  2997. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  2998. (defun org-activate-plain-links (limit)
  2999. "Run through the buffer and add overlays to links."
  3000. (catch 'exit
  3001. (let (f)
  3002. (while (re-search-forward org-plain-link-re limit t)
  3003. (setq f (get-text-property (match-beginning 0) 'face))
  3004. (if (or (eq f 'org-tag)
  3005. (and (listp f) (memq 'org-tag f)))
  3006. nil
  3007. (add-text-properties (match-beginning 0) (match-end 0)
  3008. (list 'mouse-face 'highlight
  3009. 'rear-nonsticky org-nonsticky-props
  3010. 'keymap org-mouse-map
  3011. ))
  3012. (throw 'exit t))))))
  3013. (defun org-activate-code (limit)
  3014. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  3015. (unless (get-text-property (match-beginning 1) 'face)
  3016. (remove-text-properties (match-beginning 0) (match-end 0)
  3017. '(display t invisible t intangible t))
  3018. t)))
  3019. (defun org-activate-angle-links (limit)
  3020. "Run through the buffer and add overlays to links."
  3021. (if (re-search-forward org-angle-link-re limit t)
  3022. (progn
  3023. (add-text-properties (match-beginning 0) (match-end 0)
  3024. (list 'mouse-face 'highlight
  3025. 'rear-nonsticky org-nonsticky-props
  3026. 'keymap org-mouse-map
  3027. ))
  3028. t)))
  3029. (defun org-activate-bracket-links (limit)
  3030. "Run through the buffer and add overlays to bracketed links."
  3031. (if (re-search-forward org-bracket-link-regexp limit t)
  3032. (let* ((help (concat "LINK: "
  3033. (org-match-string-no-properties 1)))
  3034. ;; FIXME: above we should remove the escapes.
  3035. ;; but that requires another match, protecting match data,
  3036. ;; a lot of overhead for font-lock.
  3037. (ip (org-maybe-intangible
  3038. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3039. 'keymap org-mouse-map 'mouse-face 'highlight
  3040. 'font-lock-multiline t 'help-echo help)))
  3041. (vp (list 'rear-nonsticky org-nonsticky-props
  3042. 'keymap org-mouse-map 'mouse-face 'highlight
  3043. ' font-lock-multiline t 'help-echo help)))
  3044. ;; We need to remove the invisible property here. Table narrowing
  3045. ;; may have made some of this invisible.
  3046. (remove-text-properties (match-beginning 0) (match-end 0)
  3047. '(invisible nil))
  3048. (if (match-end 3)
  3049. (progn
  3050. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3051. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3052. (add-text-properties (match-end 3) (match-end 0) ip))
  3053. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3054. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3055. (add-text-properties (match-end 1) (match-end 0) ip))
  3056. t)))
  3057. (defun org-activate-dates (limit)
  3058. "Run through the buffer and add overlays to dates."
  3059. (if (re-search-forward org-tsr-regexp-both limit t)
  3060. (progn
  3061. (add-text-properties (match-beginning 0) (match-end 0)
  3062. (list 'mouse-face 'highlight
  3063. 'rear-nonsticky org-nonsticky-props
  3064. 'keymap org-mouse-map))
  3065. (when org-display-custom-times
  3066. (if (match-end 3)
  3067. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3068. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3069. t)))
  3070. (defvar org-target-link-regexp nil
  3071. "Regular expression matching radio targets in plain text.")
  3072. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3073. "Regular expression matching a link target.")
  3074. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3075. "Regular expression matching a radio target.")
  3076. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3077. "Regular expression matching any target.")
  3078. (defun org-activate-target-links (limit)
  3079. "Run through the buffer and add overlays to target matches."
  3080. (when org-target-link-regexp
  3081. (let ((case-fold-search t))
  3082. (if (re-search-forward org-target-link-regexp limit t)
  3083. (progn
  3084. (add-text-properties (match-beginning 0) (match-end 0)
  3085. (list 'mouse-face 'highlight
  3086. 'rear-nonsticky org-nonsticky-props
  3087. 'keymap org-mouse-map
  3088. 'help-echo "Radio target link"
  3089. 'org-linked-text t))
  3090. t)))))
  3091. (defun org-update-radio-target-regexp ()
  3092. "Find all radio targets in this file and update the regular expression."
  3093. (interactive)
  3094. (when (memq 'radio org-activate-links)
  3095. (setq org-target-link-regexp
  3096. (org-make-target-link-regexp (org-all-targets 'radio)))
  3097. (org-restart-font-lock)))
  3098. (defun org-hide-wide-columns (limit)
  3099. (let (s e)
  3100. (setq s (text-property-any (point) (or limit (point-max))
  3101. 'org-cwidth t))
  3102. (when s
  3103. (setq e (next-single-property-change s 'org-cwidth))
  3104. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3105. (goto-char e)
  3106. t)))
  3107. (defvar org-latex-and-specials-regexp nil
  3108. "Regular expression for highlighting export special stuff.")
  3109. (defvar org-match-substring-regexp)
  3110. (defvar org-match-substring-with-braces-regexp)
  3111. (defvar org-export-html-special-string-regexps)
  3112. (defun org-compute-latex-and-specials-regexp ()
  3113. "Compute regular expression for stuff treated specially by exporters."
  3114. (if (not org-highlight-latex-fragments-and-specials)
  3115. (org-set-local 'org-latex-and-specials-regexp nil)
  3116. (require 'org-exp)
  3117. (let*
  3118. ((matchers (plist-get org-format-latex-options :matchers))
  3119. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3120. org-latex-regexps)))
  3121. (options (org-combine-plists (org-default-export-plist)
  3122. (org-infile-export-plist)))
  3123. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3124. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3125. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3126. (org-export-html-expand (plist-get options :expand-quoted-html))
  3127. (org-export-with-special-strings (plist-get options :special-strings))
  3128. (re-sub
  3129. (cond
  3130. ((equal org-export-with-sub-superscripts '{})
  3131. (list org-match-substring-with-braces-regexp))
  3132. (org-export-with-sub-superscripts
  3133. (list org-match-substring-regexp))
  3134. (t nil)))
  3135. (re-latex
  3136. (if org-export-with-LaTeX-fragments
  3137. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3138. (re-macros
  3139. (if org-export-with-TeX-macros
  3140. (list (concat "\\\\"
  3141. (regexp-opt
  3142. (append (mapcar 'car org-html-entities)
  3143. (if (boundp 'org-latex-entities)
  3144. org-latex-entities nil))
  3145. 'words))) ; FIXME
  3146. ))
  3147. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3148. (re-special (if org-export-with-special-strings
  3149. (mapcar (lambda (x) (car x))
  3150. org-export-html-special-string-regexps)))
  3151. (re-rest
  3152. (delq nil
  3153. (list
  3154. (if org-export-html-expand "@<[^>\n]+>")
  3155. ))))
  3156. (org-set-local
  3157. 'org-latex-and-specials-regexp
  3158. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3159. re-rest) "\\|")))))
  3160. (defun org-do-latex-and-special-faces (limit)
  3161. "Run through the buffer and add overlays to links."
  3162. (when org-latex-and-specials-regexp
  3163. (let (rtn d)
  3164. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3165. limit t))
  3166. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3167. 'face))
  3168. '(org-code org-verbatim underline)))
  3169. (progn
  3170. (setq rtn t
  3171. d (cond ((member (char-after (1+ (match-beginning 0)))
  3172. '(?_ ?^)) 1)
  3173. (t 0)))
  3174. (font-lock-prepend-text-property
  3175. (+ d (match-beginning 0)) (match-end 0)
  3176. 'face 'org-latex-and-export-specials)
  3177. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3178. '(font-lock-multiline t)))))
  3179. rtn)))
  3180. (defun org-restart-font-lock ()
  3181. "Restart font-lock-mode, to force refontification."
  3182. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3183. (font-lock-mode -1)
  3184. (font-lock-mode 1)))
  3185. (defun org-all-targets (&optional radio)
  3186. "Return a list of all targets in this file.
  3187. With optional argument RADIO, only find radio targets."
  3188. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3189. rtn)
  3190. (save-excursion
  3191. (goto-char (point-min))
  3192. (while (re-search-forward re nil t)
  3193. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3194. rtn)))
  3195. (defun org-make-target-link-regexp (targets)
  3196. "Make regular expression matching all strings in TARGETS.
  3197. The regular expression finds the targets also if there is a line break
  3198. between words."
  3199. (and targets
  3200. (concat
  3201. "\\<\\("
  3202. (mapconcat
  3203. (lambda (x)
  3204. (while (string-match " +" x)
  3205. (setq x (replace-match "\\s-+" t t x)))
  3206. x)
  3207. targets
  3208. "\\|")
  3209. "\\)\\>")))
  3210. (defun org-activate-tags (limit)
  3211. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3212. (progn
  3213. (add-text-properties (match-beginning 1) (match-end 1)
  3214. (list 'mouse-face 'highlight
  3215. 'rear-nonsticky org-nonsticky-props
  3216. 'keymap org-mouse-map))
  3217. t)))
  3218. (defun org-outline-level ()
  3219. (save-excursion
  3220. (looking-at outline-regexp)
  3221. (if (match-beginning 1)
  3222. (+ (org-get-string-indentation (match-string 1)) 1000)
  3223. (1- (- (match-end 0) (match-beginning 0))))))
  3224. (defvar org-font-lock-keywords nil)
  3225. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3226. "Regular expression matching a property line.")
  3227. (defvar org-font-lock-hook nil
  3228. "Functions to be called for special font loch stuff.")
  3229. (defun org-font-lock-hook (limit)
  3230. (run-hook-with-args 'org-font-lock-hook limit))
  3231. (defun org-set-font-lock-defaults ()
  3232. (let* ((em org-fontify-emphasized-text)
  3233. (lk org-activate-links)
  3234. (org-font-lock-extra-keywords
  3235. (list
  3236. ;; Call the hook
  3237. '(org-font-lock-hook)
  3238. ;; Headlines
  3239. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3240. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3241. ;; Table lines
  3242. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3243. (1 'org-table t))
  3244. ;; Table internals
  3245. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3246. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3247. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3248. ;; Drawers
  3249. (list org-drawer-regexp '(0 'org-special-keyword t))
  3250. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3251. ;; Properties
  3252. (list org-property-re
  3253. '(1 'org-special-keyword t)
  3254. '(3 'org-property-value t))
  3255. (if org-format-transports-properties-p
  3256. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3257. ;; Links
  3258. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3259. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3260. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3261. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3262. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3263. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3264. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3265. '(org-hide-wide-columns (0 nil append))
  3266. ;; TODO lines
  3267. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3268. '(1 (org-get-todo-face 1) t))
  3269. ;; DONE
  3270. (if org-fontify-done-headline
  3271. (list (concat "^[*]+ +\\<\\("
  3272. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3273. "\\)\\(.*\\)")
  3274. '(2 'org-headline-done t))
  3275. nil)
  3276. ;; Priorities
  3277. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3278. ;; Special keywords
  3279. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3280. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3281. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3282. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3283. ;; Emphasis
  3284. (if em
  3285. (if (featurep 'xemacs)
  3286. '(org-do-emphasis-faces (0 nil append))
  3287. '(org-do-emphasis-faces)))
  3288. ;; Checkboxes
  3289. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3290. 2 'bold prepend)
  3291. (if org-provide-checkbox-statistics
  3292. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3293. (0 (org-get-checkbox-statistics-face) t)))
  3294. ;; Description list items
  3295. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*?\\) ::"
  3296. 2 'bold prepend)
  3297. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3298. '(1 'org-archived prepend))
  3299. ;; Specials
  3300. '(org-do-latex-and-special-faces)
  3301. ;; Code
  3302. '(org-activate-code (1 'org-code t))
  3303. ;; COMMENT
  3304. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3305. "\\|" org-quote-string "\\)\\>")
  3306. '(1 'org-special-keyword t))
  3307. '("^#.*" (0 'font-lock-comment-face t))
  3308. )))
  3309. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3310. ;; Now set the full font-lock-keywords
  3311. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3312. (org-set-local 'font-lock-defaults
  3313. '(org-font-lock-keywords t nil nil backward-paragraph))
  3314. (kill-local-variable 'font-lock-keywords) nil))
  3315. (defvar org-m nil)
  3316. (defvar org-l nil)
  3317. (defvar org-f nil)
  3318. (defun org-get-level-face (n)
  3319. "Get the right face for match N in font-lock matching of healdines."
  3320. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3321. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3322. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3323. (cond
  3324. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3325. ((eq n 2) org-f)
  3326. (t (if org-level-color-stars-only nil org-f))))
  3327. (defun org-get-todo-face (kwd)
  3328. "Get the right face for a TODO keyword KWD.
  3329. If KWD is a number, get the corresponding match group."
  3330. (if (numberp kwd) (setq kwd (match-string kwd)))
  3331. (or (cdr (assoc kwd org-todo-keyword-faces))
  3332. (and (member kwd org-done-keywords) 'org-done)
  3333. 'org-todo))
  3334. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3335. "Remove fontification and activation overlays from links."
  3336. (font-lock-default-unfontify-region beg end)
  3337. (let* ((buffer-undo-list t)
  3338. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3339. (inhibit-modification-hooks t)
  3340. deactivate-mark buffer-file-name buffer-file-truename)
  3341. (remove-text-properties beg end
  3342. '(mouse-face t keymap t org-linked-text t
  3343. invisible t intangible t))))
  3344. ;;;; Visibility cycling, including org-goto and indirect buffer
  3345. ;;; Cycling
  3346. (defvar org-cycle-global-status nil)
  3347. (make-variable-buffer-local 'org-cycle-global-status)
  3348. (defvar org-cycle-subtree-status nil)
  3349. (make-variable-buffer-local 'org-cycle-subtree-status)
  3350. ;;;###autoload
  3351. (defun org-cycle (&optional arg)
  3352. "Visibility cycling for Org-mode.
  3353. - When this function is called with a prefix argument, rotate the entire
  3354. buffer through 3 states (global cycling)
  3355. 1. OVERVIEW: Show only top-level headlines.
  3356. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3357. 3. SHOW ALL: Show everything.
  3358. When called with two C-c C-u prefixes, switch to the startup visibility,
  3359. determined by the variable `org-startup-folded', and by any VISIBILITY
  3360. properties in the buffer.
  3361. - When point is at the beginning of a headline, rotate the subtree started
  3362. by this line through 3 different states (local cycling)
  3363. 1. FOLDED: Only the main headline is shown.
  3364. 2. CHILDREN: The main headline and the direct children are shown.
  3365. From this state, you can move to one of the children
  3366. and zoom in further.
  3367. 3. SUBTREE: Show the entire subtree, including body text.
  3368. - When there is a numeric prefix, go up to a heading with level ARG, do
  3369. a `show-subtree' and return to the previous cursor position. If ARG
  3370. is negative, go up that many levels.
  3371. - When point is not at the beginning of a headline, execute the global
  3372. binding for TAB, which is re-indenting the line. See the option
  3373. `org-cycle-emulate-tab' for details.
  3374. - Special case: if point is at the beginning of the buffer and there is
  3375. no headline in line 1, this function will act as if called with prefix arg.
  3376. But only if also the variable `org-cycle-global-at-bob' is t."
  3377. (interactive "P")
  3378. (org-load-modules-maybe)
  3379. (let* ((outline-regexp
  3380. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3381. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3382. outline-regexp))
  3383. (bob-special (and org-cycle-global-at-bob (bobp)
  3384. (not (looking-at outline-regexp))))
  3385. (org-cycle-hook
  3386. (if bob-special
  3387. (delq 'org-optimize-window-after-visibility-change
  3388. (copy-sequence org-cycle-hook))
  3389. org-cycle-hook))
  3390. (pos (point)))
  3391. (if (or bob-special (equal arg '(4)))
  3392. ;; special case: use global cycling
  3393. (setq arg t))
  3394. (cond
  3395. ((equal arg '(16))
  3396. (org-set-startup-visibility)
  3397. (message "Startup visibility, plus VISIBILITY properties."))
  3398. ((org-at-table-p 'any)
  3399. ;; Enter the table or move to the next field in the table
  3400. (or (org-table-recognize-table.el)
  3401. (progn
  3402. (if arg (org-table-edit-field t)
  3403. (org-table-justify-field-maybe)
  3404. (call-interactively 'org-table-next-field)))))
  3405. ((eq arg t) ;; Global cycling
  3406. (cond
  3407. ((and (eq last-command this-command)
  3408. (eq org-cycle-global-status 'overview))
  3409. ;; We just created the overview - now do table of contents
  3410. ;; This can be slow in very large buffers, so indicate action
  3411. (message "CONTENTS...")
  3412. (org-content)
  3413. (message "CONTENTS...done")
  3414. (setq org-cycle-global-status 'contents)
  3415. (run-hook-with-args 'org-cycle-hook 'contents))
  3416. ((and (eq last-command this-command)
  3417. (eq org-cycle-global-status 'contents))
  3418. ;; We just showed the table of contents - now show everything
  3419. (show-all)
  3420. (message "SHOW ALL")
  3421. (setq org-cycle-global-status 'all)
  3422. (run-hook-with-args 'org-cycle-hook 'all))
  3423. (t
  3424. ;; Default action: go to overview
  3425. (org-overview)
  3426. (message "OVERVIEW")
  3427. (setq org-cycle-global-status 'overview)
  3428. (run-hook-with-args 'org-cycle-hook 'overview))))
  3429. ((and org-drawers org-drawer-regexp
  3430. (save-excursion
  3431. (beginning-of-line 1)
  3432. (looking-at org-drawer-regexp)))
  3433. ;; Toggle block visibility
  3434. (org-flag-drawer
  3435. (not (get-char-property (match-end 0) 'invisible))))
  3436. ((integerp arg)
  3437. ;; Show-subtree, ARG levels up from here.
  3438. (save-excursion
  3439. (org-back-to-heading)
  3440. (outline-up-heading (if (< arg 0) (- arg)
  3441. (- (funcall outline-level) arg)))
  3442. (org-show-subtree)))
  3443. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3444. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3445. ;; At a heading: rotate between three different views
  3446. (org-back-to-heading)
  3447. (let ((goal-column 0) eoh eol eos)
  3448. ;; First, some boundaries
  3449. (save-excursion
  3450. (org-back-to-heading)
  3451. (save-excursion
  3452. (beginning-of-line 2)
  3453. (while (and (not (eobp)) ;; this is like `next-line'
  3454. (get-char-property (1- (point)) 'invisible))
  3455. (beginning-of-line 2)) (setq eol (point)))
  3456. (outline-end-of-heading) (setq eoh (point))
  3457. (org-end-of-subtree t)
  3458. (unless (eobp)
  3459. (skip-chars-forward " \t\n")
  3460. (beginning-of-line 1) ; in case this is an item
  3461. )
  3462. (setq eos (1- (point))))
  3463. ;; Find out what to do next and set `this-command'
  3464. (cond
  3465. ((= eos eoh)
  3466. ;; Nothing is hidden behind this heading
  3467. (message "EMPTY ENTRY")
  3468. (setq org-cycle-subtree-status nil)
  3469. (save-excursion
  3470. (goto-char eos)
  3471. (outline-next-heading)
  3472. (if (org-invisible-p) (org-flag-heading nil))))
  3473. ((or (>= eol eos)
  3474. (not (string-match "\\S-" (buffer-substring eol eos))))
  3475. ;; Entire subtree is hidden in one line: open it
  3476. (org-show-entry)
  3477. (show-children)
  3478. (message "CHILDREN")
  3479. (save-excursion
  3480. (goto-char eos)
  3481. (outline-next-heading)
  3482. (if (org-invisible-p) (org-flag-heading nil)))
  3483. (setq org-cycle-subtree-status 'children)
  3484. (run-hook-with-args 'org-cycle-hook 'children))
  3485. ((and (eq last-command this-command)
  3486. (eq org-cycle-subtree-status 'children))
  3487. ;; We just showed the children, now show everything.
  3488. (org-show-subtree)
  3489. (message "SUBTREE")
  3490. (setq org-cycle-subtree-status 'subtree)
  3491. (run-hook-with-args 'org-cycle-hook 'subtree))
  3492. (t
  3493. ;; Default action: hide the subtree.
  3494. (hide-subtree)
  3495. (message "FOLDED")
  3496. (setq org-cycle-subtree-status 'folded)
  3497. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3498. ;; TAB emulation
  3499. (buffer-read-only (org-back-to-heading))
  3500. ((org-try-cdlatex-tab))
  3501. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3502. (or (not (bolp))
  3503. (not (looking-at outline-regexp))))
  3504. (call-interactively (global-key-binding "\t")))
  3505. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3506. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3507. (or (and (eq org-cycle-emulate-tab 'white)
  3508. (= (match-end 0) (point-at-eol)))
  3509. (and (eq org-cycle-emulate-tab 'whitestart)
  3510. (>= (match-end 0) pos))))
  3511. t
  3512. (eq org-cycle-emulate-tab t))
  3513. (call-interactively (global-key-binding "\t")))
  3514. (t (save-excursion
  3515. (org-back-to-heading)
  3516. (org-cycle))))))
  3517. ;;;###autoload
  3518. (defun org-global-cycle (&optional arg)
  3519. "Cycle the global visibility. For details see `org-cycle'.
  3520. With C-u prefix arg, switch to startup visibility.
  3521. With a numeric prefix, show all headlines up to that level."
  3522. (interactive "P")
  3523. (let ((org-cycle-include-plain-lists
  3524. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3525. (cond
  3526. ((integerp arg)
  3527. (show-all)
  3528. (hide-sublevels arg)
  3529. (setq org-cycle-global-status 'contents))
  3530. ((equal arg '(4))
  3531. (org-set-startup-visibility)
  3532. (message "Startup visibility, plus VISIBILITY properties."))
  3533. (t
  3534. (org-cycle '(4))))))
  3535. (defun org-set-startup-visibility ()
  3536. "Set the visibility required by startup options and properties."
  3537. (cond
  3538. ((eq org-startup-folded t)
  3539. (org-cycle '(4)))
  3540. ((eq org-startup-folded 'content)
  3541. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3542. (org-cycle '(4)) (org-cycle '(4)))))
  3543. (org-set-visibility-according-to-property 'no-cleanup)
  3544. (org-cycle-hide-archived-subtrees 'all)
  3545. (org-cycle-hide-drawers 'all)
  3546. (org-cycle-show-empty-lines 'all))
  3547. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3548. "Switch subtree visibilities according to :VISIBILITY: property."
  3549. (interactive)
  3550. (let (state)
  3551. (save-excursion
  3552. (goto-char (point-min))
  3553. (while (re-search-forward
  3554. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3555. nil t)
  3556. (setq state (match-string 1))
  3557. (save-excursion
  3558. (org-back-to-heading t)
  3559. (hide-subtree)
  3560. (org-reveal)
  3561. (cond
  3562. ((equal state '("fold" "folded"))
  3563. (hide-subtree))
  3564. ((equal state "children")
  3565. (org-show-hidden-entry)
  3566. (show-children))
  3567. ((equal state "content")
  3568. (save-excursion
  3569. (save-restriction
  3570. (org-narrow-to-subtree)
  3571. (org-content))))
  3572. ((member state '("all" "showall"))
  3573. (show-subtree)))))
  3574. (unless no-cleanup
  3575. (org-cycle-hide-archived-subtrees 'all)
  3576. (org-cycle-hide-drawers 'all)
  3577. (org-cycle-show-empty-lines 'all)))))
  3578. (defun org-overview ()
  3579. "Switch to overview mode, shoing only top-level headlines.
  3580. Really, this shows all headlines with level equal or greater than the level
  3581. of the first headline in the buffer. This is important, because if the
  3582. first headline is not level one, then (hide-sublevels 1) gives confusing
  3583. results."
  3584. (interactive)
  3585. (let ((level (save-excursion
  3586. (goto-char (point-min))
  3587. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3588. (progn
  3589. (goto-char (match-beginning 0))
  3590. (funcall outline-level))))))
  3591. (and level (hide-sublevels level))))
  3592. (defun org-content (&optional arg)
  3593. "Show all headlines in the buffer, like a table of contents.
  3594. With numerical argument N, show content up to level N."
  3595. (interactive "P")
  3596. (save-excursion
  3597. ;; Visit all headings and show their offspring
  3598. (and (integerp arg) (org-overview))
  3599. (goto-char (point-max))
  3600. (catch 'exit
  3601. (while (and (progn (condition-case nil
  3602. (outline-previous-visible-heading 1)
  3603. (error (goto-char (point-min))))
  3604. t)
  3605. (looking-at outline-regexp))
  3606. (if (integerp arg)
  3607. (show-children (1- arg))
  3608. (show-branches))
  3609. (if (bobp) (throw 'exit nil))))))
  3610. (defun org-optimize-window-after-visibility-change (state)
  3611. "Adjust the window after a change in outline visibility.
  3612. This function is the default value of the hook `org-cycle-hook'."
  3613. (when (get-buffer-window (current-buffer))
  3614. (cond
  3615. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3616. ; ((eq state 'overview) (org-beginning-of-line))
  3617. ((eq state 'content) nil)
  3618. ((eq state 'all) nil)
  3619. ((eq state 'folded) nil)
  3620. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3621. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3622. (defun org-compact-display-after-subtree-move ()
  3623. (let (beg end)
  3624. (save-excursion
  3625. (if (org-up-heading-safe)
  3626. (progn
  3627. (hide-subtree)
  3628. (show-entry)
  3629. (show-children)
  3630. (org-cycle-show-empty-lines 'children)
  3631. (org-cycle-hide-drawers 'children))
  3632. (org-overview)))))
  3633. (defun org-cycle-show-empty-lines (state)
  3634. "Show empty lines above all visible headlines.
  3635. The region to be covered depends on STATE when called through
  3636. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3637. entire buffer covered. Note that an empty line is only shown if there
  3638. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3639. (when (> org-cycle-separator-lines 0)
  3640. (save-excursion
  3641. (let* ((n org-cycle-separator-lines)
  3642. (re (cond
  3643. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3644. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3645. (t (let ((ns (number-to-string (- n 2))))
  3646. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3647. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3648. beg end)
  3649. (cond
  3650. ((memq state '(overview contents t))
  3651. (setq beg (point-min) end (point-max)))
  3652. ((memq state '(children folded))
  3653. (setq beg (point) end (progn (org-end-of-subtree t t)
  3654. (beginning-of-line 2)
  3655. (point)))))
  3656. (when beg
  3657. (goto-char beg)
  3658. (while (re-search-forward re end t)
  3659. (if (not (get-char-property (match-end 1) 'invisible))
  3660. (outline-flag-region
  3661. (match-beginning 1) (match-end 1) nil)))))))
  3662. ;; Never hide empty lines at the end of the file.
  3663. (save-excursion
  3664. (goto-char (point-max))
  3665. (outline-previous-heading)
  3666. (outline-end-of-heading)
  3667. (if (and (looking-at "[ \t\n]+")
  3668. (= (match-end 0) (point-max)))
  3669. (outline-flag-region (point) (match-end 0) nil))))
  3670. (defun org-cycle-hide-drawers (state)
  3671. "Re-hide all drawers after a visibility state change."
  3672. (when (and (org-mode-p)
  3673. (not (memq state '(overview folded))))
  3674. (save-excursion
  3675. (let* ((globalp (memq state '(contents all)))
  3676. (beg (if globalp (point-min) (point)))
  3677. (end (if globalp (point-max) (org-end-of-subtree t))))
  3678. (goto-char beg)
  3679. (while (re-search-forward org-drawer-regexp end t)
  3680. (org-flag-drawer t))))))
  3681. (defun org-flag-drawer (flag)
  3682. (save-excursion
  3683. (beginning-of-line 1)
  3684. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3685. (let ((b (match-end 0))
  3686. (outline-regexp org-outline-regexp))
  3687. (if (re-search-forward
  3688. "^[ \t]*:END:"
  3689. (save-excursion (outline-next-heading) (point)) t)
  3690. (outline-flag-region b (point-at-eol) flag)
  3691. (error ":END: line missing"))))))
  3692. (defun org-subtree-end-visible-p ()
  3693. "Is the end of the current subtree visible?"
  3694. (pos-visible-in-window-p
  3695. (save-excursion (org-end-of-subtree t) (point))))
  3696. (defun org-first-headline-recenter (&optional N)
  3697. "Move cursor to the first headline and recenter the headline.
  3698. Optional argument N means, put the headline into the Nth line of the window."
  3699. (goto-char (point-min))
  3700. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3701. (beginning-of-line)
  3702. (recenter (prefix-numeric-value N))))
  3703. ;;; Org-goto
  3704. (defvar org-goto-window-configuration nil)
  3705. (defvar org-goto-marker nil)
  3706. (defvar org-goto-map
  3707. (let ((map (make-sparse-keymap)))
  3708. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3709. (while (setq cmd (pop cmds))
  3710. (substitute-key-definition cmd cmd map global-map)))
  3711. (suppress-keymap map)
  3712. (org-defkey map "\C-m" 'org-goto-ret)
  3713. (org-defkey map [(return)] 'org-goto-ret)
  3714. (org-defkey map [(left)] 'org-goto-left)
  3715. (org-defkey map [(right)] 'org-goto-right)
  3716. (org-defkey map [(control ?g)] 'org-goto-quit)
  3717. (org-defkey map "\C-i" 'org-cycle)
  3718. (org-defkey map [(tab)] 'org-cycle)
  3719. (org-defkey map [(down)] 'outline-next-visible-heading)
  3720. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3721. (if org-goto-auto-isearch
  3722. (if (fboundp 'define-key-after)
  3723. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3724. nil)
  3725. (org-defkey map "q" 'org-goto-quit)
  3726. (org-defkey map "n" 'outline-next-visible-heading)
  3727. (org-defkey map "p" 'outline-previous-visible-heading)
  3728. (org-defkey map "f" 'outline-forward-same-level)
  3729. (org-defkey map "b" 'outline-backward-same-level)
  3730. (org-defkey map "u" 'outline-up-heading))
  3731. (org-defkey map "/" 'org-occur)
  3732. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3733. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3734. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3735. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3736. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3737. map))
  3738. (defconst org-goto-help
  3739. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3740. RET=jump to location [Q]uit and return to previous location
  3741. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3742. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3743. (defun org-goto (&optional alternative-interface)
  3744. "Look up a different location in the current file, keeping current visibility.
  3745. When you want look-up or go to a different location in a document, the
  3746. fastest way is often to fold the entire buffer and then dive into the tree.
  3747. This method has the disadvantage, that the previous location will be folded,
  3748. which may not be what you want.
  3749. This command works around this by showing a copy of the current buffer
  3750. in an indirect buffer, in overview mode. You can dive into the tree in
  3751. that copy, use org-occur and incremental search to find a location.
  3752. When pressing RET or `Q', the command returns to the original buffer in
  3753. which the visibility is still unchanged. After RET is will also jump to
  3754. the location selected in the indirect buffer and expose the
  3755. the headline hierarchy above."
  3756. (interactive "P")
  3757. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3758. (org-refile-use-outline-path t)
  3759. (interface
  3760. (if (not alternative-interface)
  3761. org-goto-interface
  3762. (if (eq org-goto-interface 'outline)
  3763. 'outline-path-completion
  3764. 'outline)))
  3765. (org-goto-start-pos (point))
  3766. (selected-point
  3767. (if (eq interface 'outline)
  3768. (car (org-get-location (current-buffer) org-goto-help))
  3769. (nth 3 (org-refile-get-location "Goto: ")))))
  3770. (if selected-point
  3771. (progn
  3772. (org-mark-ring-push org-goto-start-pos)
  3773. (goto-char selected-point)
  3774. (if (or (org-invisible-p) (org-invisible-p2))
  3775. (org-show-context 'org-goto)))
  3776. (message "Quit"))))
  3777. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3778. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3779. (defvar org-goto-local-auto-isearch-map) ; defined below
  3780. (defun org-get-location (buf help)
  3781. "Let the user select a location in the Org-mode buffer BUF.
  3782. This function uses a recursive edit. It returns the selected position
  3783. or nil."
  3784. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3785. (isearch-hide-immediately nil)
  3786. (isearch-search-fun-function
  3787. (lambda () 'org-goto-local-search-forward-headings))
  3788. (org-goto-selected-point org-goto-exit-command))
  3789. (save-excursion
  3790. (save-window-excursion
  3791. (delete-other-windows)
  3792. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3793. (switch-to-buffer
  3794. (condition-case nil
  3795. (make-indirect-buffer (current-buffer) "*org-goto*")
  3796. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3797. (with-output-to-temp-buffer "*Help*"
  3798. (princ help))
  3799. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3800. (setq buffer-read-only nil)
  3801. (let ((org-startup-truncated t)
  3802. (org-startup-folded nil)
  3803. (org-startup-align-all-tables nil))
  3804. (org-mode)
  3805. (org-overview))
  3806. (setq buffer-read-only t)
  3807. (if (and (boundp 'org-goto-start-pos)
  3808. (integer-or-marker-p org-goto-start-pos))
  3809. (let ((org-show-hierarchy-above t)
  3810. (org-show-siblings t)
  3811. (org-show-following-heading t))
  3812. (goto-char org-goto-start-pos)
  3813. (and (org-invisible-p) (org-show-context)))
  3814. (goto-char (point-min)))
  3815. (org-beginning-of-line)
  3816. (message "Select location and press RET")
  3817. (use-local-map org-goto-map)
  3818. (recursive-edit)
  3819. ))
  3820. (kill-buffer "*org-goto*")
  3821. (cons org-goto-selected-point org-goto-exit-command)))
  3822. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3823. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3824. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3825. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3826. (defun org-goto-local-search-forward-headings (string bound noerror)
  3827. "Search and make sure that anu matches are in headlines."
  3828. (catch 'return
  3829. (while (search-forward string bound noerror)
  3830. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3831. (and (member :headline context)
  3832. (not (member :tags context))))
  3833. (throw 'return (point))))))
  3834. (defun org-goto-local-auto-isearch ()
  3835. "Start isearch."
  3836. (interactive)
  3837. (goto-char (point-min))
  3838. (let ((keys (this-command-keys)))
  3839. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3840. (isearch-mode t)
  3841. (isearch-process-search-char (string-to-char keys)))))
  3842. (defun org-goto-ret (&optional arg)
  3843. "Finish `org-goto' by going to the new location."
  3844. (interactive "P")
  3845. (setq org-goto-selected-point (point)
  3846. org-goto-exit-command 'return)
  3847. (throw 'exit nil))
  3848. (defun org-goto-left ()
  3849. "Finish `org-goto' by going to the new location."
  3850. (interactive)
  3851. (if (org-on-heading-p)
  3852. (progn
  3853. (beginning-of-line 1)
  3854. (setq org-goto-selected-point (point)
  3855. org-goto-exit-command 'left)
  3856. (throw 'exit nil))
  3857. (error "Not on a heading")))
  3858. (defun org-goto-right ()
  3859. "Finish `org-goto' by going to the new location."
  3860. (interactive)
  3861. (if (org-on-heading-p)
  3862. (progn
  3863. (setq org-goto-selected-point (point)
  3864. org-goto-exit-command 'right)
  3865. (throw 'exit nil))
  3866. (error "Not on a heading")))
  3867. (defun org-goto-quit ()
  3868. "Finish `org-goto' without cursor motion."
  3869. (interactive)
  3870. (setq org-goto-selected-point nil)
  3871. (setq org-goto-exit-command 'quit)
  3872. (throw 'exit nil))
  3873. ;;; Indirect buffer display of subtrees
  3874. (defvar org-indirect-dedicated-frame nil
  3875. "This is the frame being used for indirect tree display.")
  3876. (defvar org-last-indirect-buffer nil)
  3877. (defun org-tree-to-indirect-buffer (&optional arg)
  3878. "Create indirect buffer and narrow it to current subtree.
  3879. With numerical prefix ARG, go up to this level and then take that tree.
  3880. If ARG is negative, go up that many levels.
  3881. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  3882. indirect buffer previously made with this command, to avoid proliferation of
  3883. indirect buffers. However, when you call the command with a `C-u' prefix, or
  3884. when `org-indirect-buffer-display' is `new-frame', the last buffer
  3885. is kept so that you can work with several indirect buffers at the same time.
  3886. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  3887. requests that a new frame be made for the new buffer, so that the dedicated
  3888. frame is not changed."
  3889. (interactive "P")
  3890. (let ((cbuf (current-buffer))
  3891. (cwin (selected-window))
  3892. (pos (point))
  3893. beg end level heading ibuf)
  3894. (save-excursion
  3895. (org-back-to-heading t)
  3896. (when (numberp arg)
  3897. (setq level (org-outline-level))
  3898. (if (< arg 0) (setq arg (+ level arg)))
  3899. (while (> (setq level (org-outline-level)) arg)
  3900. (outline-up-heading 1 t)))
  3901. (setq beg (point)
  3902. heading (org-get-heading))
  3903. (org-end-of-subtree t) (setq end (point)))
  3904. (if (and (buffer-live-p org-last-indirect-buffer)
  3905. (not (eq org-indirect-buffer-display 'new-frame))
  3906. (not arg))
  3907. (kill-buffer org-last-indirect-buffer))
  3908. (setq ibuf (org-get-indirect-buffer cbuf)
  3909. org-last-indirect-buffer ibuf)
  3910. (cond
  3911. ((or (eq org-indirect-buffer-display 'new-frame)
  3912. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  3913. (select-frame (make-frame))
  3914. (delete-other-windows)
  3915. (switch-to-buffer ibuf)
  3916. (org-set-frame-title heading))
  3917. ((eq org-indirect-buffer-display 'dedicated-frame)
  3918. (raise-frame
  3919. (select-frame (or (and org-indirect-dedicated-frame
  3920. (frame-live-p org-indirect-dedicated-frame)
  3921. org-indirect-dedicated-frame)
  3922. (setq org-indirect-dedicated-frame (make-frame)))))
  3923. (delete-other-windows)
  3924. (switch-to-buffer ibuf)
  3925. (org-set-frame-title (concat "Indirect: " heading)))
  3926. ((eq org-indirect-buffer-display 'current-window)
  3927. (switch-to-buffer ibuf))
  3928. ((eq org-indirect-buffer-display 'other-window)
  3929. (pop-to-buffer ibuf))
  3930. (t (error "Invalid value.")))
  3931. (if (featurep 'xemacs)
  3932. (save-excursion (org-mode) (turn-on-font-lock)))
  3933. (narrow-to-region beg end)
  3934. (show-all)
  3935. (goto-char pos)
  3936. (and (window-live-p cwin) (select-window cwin))))
  3937. (defun org-get-indirect-buffer (&optional buffer)
  3938. (setq buffer (or buffer (current-buffer)))
  3939. (let ((n 1) (base (buffer-name buffer)) bname)
  3940. (while (buffer-live-p
  3941. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  3942. (setq n (1+ n)))
  3943. (condition-case nil
  3944. (make-indirect-buffer buffer bname 'clone)
  3945. (error (make-indirect-buffer buffer bname)))))
  3946. (defun org-set-frame-title (title)
  3947. "Set the title of the current frame to the string TITLE."
  3948. ;; FIXME: how to name a single frame in XEmacs???
  3949. (unless (featurep 'xemacs)
  3950. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  3951. ;;;; Structure editing
  3952. ;;; Inserting headlines
  3953. (defun org-insert-heading (&optional force-heading)
  3954. "Insert a new heading or item with same depth at point.
  3955. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3956. If point is at the beginning of a headline, insert a sibling before the
  3957. current headline. If point is not at the beginning, do not split the line,
  3958. but create the new hedline after the current line."
  3959. (interactive "P")
  3960. (if (= (buffer-size) 0)
  3961. (insert "\n* ")
  3962. (when (or force-heading (not (org-insert-item)))
  3963. (let* ((head (save-excursion
  3964. (condition-case nil
  3965. (progn
  3966. (org-back-to-heading)
  3967. (match-string 0))
  3968. (error "*"))))
  3969. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  3970. pos)
  3971. (cond
  3972. ((and (org-on-heading-p) (bolp)
  3973. (or (bobp)
  3974. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  3975. ;; insert before the current line
  3976. (open-line (if blank 2 1)))
  3977. ((and (bolp)
  3978. (or (bobp)
  3979. (save-excursion
  3980. (backward-char 1) (not (org-invisible-p)))))
  3981. ;; insert right here
  3982. nil)
  3983. (t
  3984. ;; in the middle of the line
  3985. (org-show-entry)
  3986. (let ((split
  3987. (org-get-alist-option org-M-RET-may-split-line 'headline))
  3988. tags pos)
  3989. (if (org-on-heading-p)
  3990. (progn
  3991. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3992. (setq tags (and (match-end 2) (match-string 2)))
  3993. (and (match-end 1)
  3994. (delete-region (match-beginning 1) (match-end 1)))
  3995. (setq pos (point-at-bol))
  3996. (or split (end-of-line 1))
  3997. (delete-horizontal-space)
  3998. (newline (if blank 2 1))
  3999. (when tags
  4000. (save-excursion
  4001. (goto-char pos)
  4002. (end-of-line 1)
  4003. (insert " " tags)
  4004. (org-set-tags nil 'align))))
  4005. (or split (end-of-line 1))
  4006. (newline (if blank 2 1))))))
  4007. (insert head) (just-one-space)
  4008. (setq pos (point))
  4009. (end-of-line 1)
  4010. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4011. (run-hooks 'org-insert-heading-hook)))))
  4012. (defun org-get-heading (&optional no-tags)
  4013. "Return the heading of the current entry, without the stars."
  4014. (save-excursion
  4015. (org-back-to-heading t)
  4016. (if (looking-at
  4017. (if no-tags
  4018. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4019. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4020. (match-string 1) "")))
  4021. (defun org-insert-heading-after-current ()
  4022. "Insert a new heading with same level as current, after current subtree."
  4023. (interactive)
  4024. (org-back-to-heading)
  4025. (org-insert-heading)
  4026. (org-move-subtree-down)
  4027. (end-of-line 1))
  4028. (defun org-insert-todo-heading (arg)
  4029. "Insert a new heading with the same level and TODO state as current heading.
  4030. If the heading has no TODO state, or if the state is DONE, use the first
  4031. state (TODO by default). Also with prefix arg, force first state."
  4032. (interactive "P")
  4033. (when (not (org-insert-item 'checkbox))
  4034. (org-insert-heading)
  4035. (save-excursion
  4036. (org-back-to-heading)
  4037. (outline-previous-heading)
  4038. (looking-at org-todo-line-regexp))
  4039. (if (or arg
  4040. (not (match-beginning 2))
  4041. (member (match-string 2) org-done-keywords))
  4042. (insert (car org-todo-keywords-1) " ")
  4043. (insert (match-string 2) " "))))
  4044. (defun org-insert-subheading (arg)
  4045. "Insert a new subheading and demote it.
  4046. Works for outline headings and for plain lists alike."
  4047. (interactive "P")
  4048. (org-insert-heading arg)
  4049. (cond
  4050. ((org-on-heading-p) (org-do-demote))
  4051. ((org-at-item-p) (org-indent-item 1))))
  4052. (defun org-insert-todo-subheading (arg)
  4053. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4054. Works for outline headings and for plain lists alike."
  4055. (interactive "P")
  4056. (org-insert-todo-heading arg)
  4057. (cond
  4058. ((org-on-heading-p) (org-do-demote))
  4059. ((org-at-item-p) (org-indent-item 1))))
  4060. ;;; Promotion and Demotion
  4061. (defun org-promote-subtree ()
  4062. "Promote the entire subtree.
  4063. See also `org-promote'."
  4064. (interactive)
  4065. (save-excursion
  4066. (org-map-tree 'org-promote))
  4067. (org-fix-position-after-promote))
  4068. (defun org-demote-subtree ()
  4069. "Demote the entire subtree. See `org-demote'.
  4070. See also `org-promote'."
  4071. (interactive)
  4072. (save-excursion
  4073. (org-map-tree 'org-demote))
  4074. (org-fix-position-after-promote))
  4075. (defun org-do-promote ()
  4076. "Promote the current heading higher up the tree.
  4077. If the region is active in `transient-mark-mode', promote all headings
  4078. in the region."
  4079. (interactive)
  4080. (save-excursion
  4081. (if (org-region-active-p)
  4082. (org-map-region 'org-promote (region-beginning) (region-end))
  4083. (org-promote)))
  4084. (org-fix-position-after-promote))
  4085. (defun org-do-demote ()
  4086. "Demote the current heading lower down the tree.
  4087. If the region is active in `transient-mark-mode', demote all headings
  4088. in the region."
  4089. (interactive)
  4090. (save-excursion
  4091. (if (org-region-active-p)
  4092. (org-map-region 'org-demote (region-beginning) (region-end))
  4093. (org-demote)))
  4094. (org-fix-position-after-promote))
  4095. (defun org-fix-position-after-promote ()
  4096. "Make sure that after pro/demotion cursor position is right."
  4097. (let ((pos (point)))
  4098. (when (save-excursion
  4099. (beginning-of-line 1)
  4100. (looking-at org-todo-line-regexp)
  4101. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4102. (cond ((eobp) (insert " "))
  4103. ((eolp) (insert " "))
  4104. ((equal (char-after) ?\ ) (forward-char 1))))))
  4105. (defun org-reduced-level (l)
  4106. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4107. (defun org-get-valid-level (level &optional change)
  4108. "Rectify a level change under the influence of `org-odd-levels-only'
  4109. LEVEL is a current level, CHANGE is by how much the level should be
  4110. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4111. even level numbers will become the next higher odd number."
  4112. (if org-odd-levels-only
  4113. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4114. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4115. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4116. (max 1 (+ level change))))
  4117. (if (boundp 'define-obsolete-function-alias)
  4118. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4119. (define-obsolete-function-alias 'org-get-legal-level
  4120. 'org-get-valid-level)
  4121. (define-obsolete-function-alias 'org-get-legal-level
  4122. 'org-get-valid-level "23.1")))
  4123. (defun org-promote ()
  4124. "Promote the current heading higher up the tree.
  4125. If the region is active in `transient-mark-mode', promote all headings
  4126. in the region."
  4127. (org-back-to-heading t)
  4128. (let* ((level (save-match-data (funcall outline-level)))
  4129. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4130. (diff (abs (- level (length up-head) -1))))
  4131. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4132. (replace-match up-head nil t)
  4133. ;; Fixup tag positioning
  4134. (and org-auto-align-tags (org-set-tags nil t))
  4135. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4136. (defun org-demote ()
  4137. "Demote the current heading lower down the tree.
  4138. If the region is active in `transient-mark-mode', demote all headings
  4139. in the region."
  4140. (org-back-to-heading t)
  4141. (let* ((level (save-match-data (funcall outline-level)))
  4142. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4143. (diff (abs (- level (length down-head) -1))))
  4144. (replace-match down-head nil t)
  4145. ;; Fixup tag positioning
  4146. (and org-auto-align-tags (org-set-tags nil t))
  4147. (if org-adapt-indentation (org-fixup-indentation diff))))
  4148. (defun org-map-tree (fun)
  4149. "Call FUN for every heading underneath the current one."
  4150. (org-back-to-heading)
  4151. (let ((level (funcall outline-level)))
  4152. (save-excursion
  4153. (funcall fun)
  4154. (while (and (progn
  4155. (outline-next-heading)
  4156. (> (funcall outline-level) level))
  4157. (not (eobp)))
  4158. (funcall fun)))))
  4159. (defun org-map-region (fun beg end)
  4160. "Call FUN for every heading between BEG and END."
  4161. (let ((org-ignore-region t))
  4162. (save-excursion
  4163. (setq end (copy-marker end))
  4164. (goto-char beg)
  4165. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4166. (< (point) end))
  4167. (funcall fun))
  4168. (while (and (progn
  4169. (outline-next-heading)
  4170. (< (point) end))
  4171. (not (eobp)))
  4172. (funcall fun)))))
  4173. (defun org-fixup-indentation (diff)
  4174. "Change the indentation in the current entry by DIFF
  4175. However, if any line in the current entry has no indentation, or if it
  4176. would end up with no indentation after the change, nothing at all is done."
  4177. (save-excursion
  4178. (let ((end (save-excursion (outline-next-heading)
  4179. (point-marker)))
  4180. (prohibit (if (> diff 0)
  4181. "^\\S-"
  4182. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4183. col)
  4184. (unless (save-excursion (end-of-line 1)
  4185. (re-search-forward prohibit end t))
  4186. (while (and (< (point) end)
  4187. (re-search-forward "^[ \t]+" end t))
  4188. (goto-char (match-end 0))
  4189. (setq col (current-column))
  4190. (if (< diff 0) (replace-match ""))
  4191. (indent-to (+ diff col))))
  4192. (move-marker end nil))))
  4193. (defun org-convert-to-odd-levels ()
  4194. "Convert an org-mode file with all levels allowed to one with odd levels.
  4195. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4196. level 5 etc."
  4197. (interactive)
  4198. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4199. (let ((org-odd-levels-only nil) n)
  4200. (save-excursion
  4201. (goto-char (point-min))
  4202. (while (re-search-forward "^\\*\\*+ " nil t)
  4203. (setq n (- (length (match-string 0)) 2))
  4204. (while (>= (setq n (1- n)) 0)
  4205. (org-demote))
  4206. (end-of-line 1))))))
  4207. (defun org-convert-to-oddeven-levels ()
  4208. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4209. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4210. section with an even level, conversion would destroy the structure of the file. An error
  4211. is signaled in this case."
  4212. (interactive)
  4213. (goto-char (point-min))
  4214. ;; First check if there are no even levels
  4215. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4216. (org-show-context t)
  4217. (error "Not all levels are odd in this file. Conversion not possible."))
  4218. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4219. (let ((org-odd-levels-only nil) n)
  4220. (save-excursion
  4221. (goto-char (point-min))
  4222. (while (re-search-forward "^\\*\\*+ " nil t)
  4223. (setq n (/ (1- (length (match-string 0))) 2))
  4224. (while (>= (setq n (1- n)) 0)
  4225. (org-promote))
  4226. (end-of-line 1))))))
  4227. (defun org-tr-level (n)
  4228. "Make N odd if required."
  4229. (if org-odd-levels-only (1+ (/ n 2)) n))
  4230. ;;; Vertical tree motion, cutting and pasting of subtrees
  4231. (defun org-move-subtree-up (&optional arg)
  4232. "Move the current subtree up past ARG headlines of the same level."
  4233. (interactive "p")
  4234. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4235. (defun org-move-subtree-down (&optional arg)
  4236. "Move the current subtree down past ARG headlines of the same level."
  4237. (interactive "p")
  4238. (setq arg (prefix-numeric-value arg))
  4239. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4240. 'outline-get-last-sibling))
  4241. (ins-point (make-marker))
  4242. (cnt (abs arg))
  4243. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4244. ;; Select the tree
  4245. (org-back-to-heading)
  4246. (setq beg0 (point))
  4247. (save-excursion
  4248. (setq ne-beg (org-back-over-empty-lines))
  4249. (setq beg (point)))
  4250. (save-match-data
  4251. (save-excursion (outline-end-of-heading)
  4252. (setq folded (org-invisible-p)))
  4253. (outline-end-of-subtree))
  4254. (outline-next-heading)
  4255. (setq ne-end (org-back-over-empty-lines))
  4256. (setq end (point))
  4257. (goto-char beg0)
  4258. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4259. ;; include less whitespace
  4260. (save-excursion
  4261. (goto-char beg)
  4262. (forward-line (- ne-beg ne-end))
  4263. (setq beg (point))))
  4264. ;; Find insertion point, with error handling
  4265. (while (> cnt 0)
  4266. (or (and (funcall movfunc) (looking-at outline-regexp))
  4267. (progn (goto-char beg0)
  4268. (error "Cannot move past superior level or buffer limit")))
  4269. (setq cnt (1- cnt)))
  4270. (if (> arg 0)
  4271. ;; Moving forward - still need to move over subtree
  4272. (progn (org-end-of-subtree t t)
  4273. (save-excursion
  4274. (org-back-over-empty-lines)
  4275. (or (bolp) (newline)))))
  4276. (setq ne-ins (org-back-over-empty-lines))
  4277. (move-marker ins-point (point))
  4278. (setq txt (buffer-substring beg end))
  4279. (org-save-markers-in-region beg end)
  4280. (delete-region beg end)
  4281. (outline-flag-region (1- beg) beg nil)
  4282. (outline-flag-region (1- (point)) (point) nil)
  4283. (let ((bbb (point)))
  4284. (insert-before-markers txt)
  4285. (org-reinstall-markers-in-region bbb)
  4286. (move-marker ins-point bbb))
  4287. (or (bolp) (insert "\n"))
  4288. (setq ins-end (point))
  4289. (goto-char ins-point)
  4290. (org-skip-whitespace)
  4291. (when (and (< arg 0)
  4292. (org-first-sibling-p)
  4293. (> ne-ins ne-beg))
  4294. ;; Move whitespace back to beginning
  4295. (save-excursion
  4296. (goto-char ins-end)
  4297. (let ((kill-whole-line t))
  4298. (kill-line (- ne-ins ne-beg)) (point)))
  4299. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4300. (move-marker ins-point nil)
  4301. (org-compact-display-after-subtree-move)
  4302. (unless folded
  4303. (org-show-entry)
  4304. (show-children)
  4305. (org-cycle-hide-drawers 'children))))
  4306. (defvar org-subtree-clip ""
  4307. "Clipboard for cut and paste of subtrees.
  4308. This is actually only a copy of the kill, because we use the normal kill
  4309. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4310. (defvar org-subtree-clip-folded nil
  4311. "Was the last copied subtree folded?
  4312. This is used to fold the tree back after pasting.")
  4313. (defun org-cut-subtree (&optional n)
  4314. "Cut the current subtree into the clipboard.
  4315. With prefix arg N, cut this many sequential subtrees.
  4316. This is a short-hand for marking the subtree and then cutting it."
  4317. (interactive "p")
  4318. (org-copy-subtree n 'cut))
  4319. (defun org-copy-subtree (&optional n cut force-store-markers)
  4320. "Cut the current subtree into the clipboard.
  4321. With prefix arg N, cut this many sequential subtrees.
  4322. This is a short-hand for marking the subtree and then copying it.
  4323. If CUT is non-nil, actually cut the subtree.
  4324. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4325. of some markers in the region, even if CUT is non-nil. This is
  4326. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4327. (interactive "p")
  4328. (let (beg end folded (beg0 (point)))
  4329. (if (interactive-p)
  4330. (org-back-to-heading nil) ; take what looks like a subtree
  4331. (org-back-to-heading t)) ; take what is really there
  4332. (org-back-over-empty-lines)
  4333. (setq beg (point))
  4334. (skip-chars-forward " \t\r\n")
  4335. (save-match-data
  4336. (save-excursion (outline-end-of-heading)
  4337. (setq folded (org-invisible-p)))
  4338. (condition-case nil
  4339. (outline-forward-same-level (1- n))
  4340. (error nil))
  4341. (org-end-of-subtree t t))
  4342. (org-back-over-empty-lines)
  4343. (setq end (point))
  4344. (goto-char beg0)
  4345. (when (> end beg)
  4346. (setq org-subtree-clip-folded folded)
  4347. (when (or cut force-store-markers)
  4348. (org-save-markers-in-region beg end))
  4349. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4350. (setq org-subtree-clip (current-kill 0))
  4351. (message "%s: Subtree(s) with %d characters"
  4352. (if cut "Cut" "Copied")
  4353. (length org-subtree-clip)))))
  4354. (defun org-paste-subtree (&optional level tree)
  4355. "Paste the clipboard as a subtree, with modification of headline level.
  4356. The entire subtree is promoted or demoted in order to match a new headline
  4357. level. By default, the new level is derived from the visible headings
  4358. before and after the insertion point, and taken to be the inferior headline
  4359. level of the two. So if the previous visible heading is level 3 and the
  4360. next is level 4 (or vice versa), level 4 will be used for insertion.
  4361. This makes sure that the subtree remains an independent subtree and does
  4362. not swallow low level entries.
  4363. You can also force a different level, either by using a numeric prefix
  4364. argument, or by inserting the heading marker by hand. For example, if the
  4365. cursor is after \"*****\", then the tree will be shifted to level 5.
  4366. If you want to insert the tree as is, just use \\[yank].
  4367. If optional TREE is given, use this text instead of the kill ring."
  4368. (interactive "P")
  4369. (unless (org-kill-is-subtree-p tree)
  4370. (error "%s"
  4371. (substitute-command-keys
  4372. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4373. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4374. (^re (concat "^\\(" outline-regexp "\\)"))
  4375. (re (concat "\\(" outline-regexp "\\)"))
  4376. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4377. (old-level (if (string-match ^re txt)
  4378. (- (match-end 0) (match-beginning 0) 1)
  4379. -1))
  4380. (force-level (cond (level (prefix-numeric-value level))
  4381. ((string-match
  4382. ^re_ (buffer-substring (point-at-bol) (point)))
  4383. (- (match-end 1) (match-beginning 1)))
  4384. (t nil)))
  4385. (previous-level (save-excursion
  4386. (condition-case nil
  4387. (progn
  4388. (outline-previous-visible-heading 1)
  4389. (if (looking-at re)
  4390. (- (match-end 0) (match-beginning 0) 1)
  4391. 1))
  4392. (error 1))))
  4393. (next-level (save-excursion
  4394. (condition-case nil
  4395. (progn
  4396. (or (looking-at outline-regexp)
  4397. (outline-next-visible-heading 1))
  4398. (if (looking-at re)
  4399. (- (match-end 0) (match-beginning 0) 1)
  4400. 1))
  4401. (error 1))))
  4402. (new-level (or force-level (max previous-level next-level)))
  4403. (shift (if (or (= old-level -1)
  4404. (= new-level -1)
  4405. (= old-level new-level))
  4406. 0
  4407. (- new-level old-level)))
  4408. (delta (if (> shift 0) -1 1))
  4409. (func (if (> shift 0) 'org-demote 'org-promote))
  4410. (org-odd-levels-only nil)
  4411. beg end)
  4412. ;; Remove the forced level indicator
  4413. (if force-level
  4414. (delete-region (point-at-bol) (point)))
  4415. ;; Paste
  4416. (beginning-of-line 1)
  4417. (org-back-over-empty-lines)
  4418. (setq beg (point))
  4419. (insert-before-markers txt)
  4420. (unless (string-match "\n\\'" txt) (insert "\n"))
  4421. (org-reinstall-markers-in-region beg)
  4422. (setq end (point))
  4423. (goto-char beg)
  4424. (skip-chars-forward " \t\n\r")
  4425. (setq beg (point))
  4426. ;; Shift if necessary
  4427. (unless (= shift 0)
  4428. (save-restriction
  4429. (narrow-to-region beg end)
  4430. (while (not (= shift 0))
  4431. (org-map-region func (point-min) (point-max))
  4432. (setq shift (+ delta shift)))
  4433. (goto-char (point-min))))
  4434. (when (interactive-p)
  4435. (message "Clipboard pasted as level %d subtree" new-level))
  4436. (if (and kill-ring
  4437. (eq org-subtree-clip (current-kill 0))
  4438. org-subtree-clip-folded)
  4439. ;; The tree was folded before it was killed/copied
  4440. (hide-subtree))))
  4441. (defun org-kill-is-subtree-p (&optional txt)
  4442. "Check if the current kill is an outline subtree, or a set of trees.
  4443. Returns nil if kill does not start with a headline, or if the first
  4444. headline level is not the largest headline level in the tree.
  4445. So this will actually accept several entries of equal levels as well,
  4446. which is OK for `org-paste-subtree'.
  4447. If optional TXT is given, check this string instead of the current kill."
  4448. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4449. (start-level (and kill
  4450. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4451. org-outline-regexp "\\)")
  4452. kill)
  4453. (- (match-end 2) (match-beginning 2) 1)))
  4454. (re (concat "^" org-outline-regexp))
  4455. (start (1+ (match-beginning 2))))
  4456. (if (not start-level)
  4457. (progn
  4458. nil) ;; does not even start with a heading
  4459. (catch 'exit
  4460. (while (setq start (string-match re kill (1+ start)))
  4461. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4462. (throw 'exit nil)))
  4463. t))))
  4464. (defvar org-markers-to-move nil
  4465. "Markers that should be moved with a cut-and-paste operation.
  4466. Those markers are stored together with their positions relative to
  4467. the start of the region.")
  4468. (defun org-save-markers-in-region (beg end)
  4469. "Check markers in region.
  4470. If these markers are between BEG and END, record their position relative
  4471. to BEG, so that after moving the block of text, we can put the markers back
  4472. into place.
  4473. This function gets called just before an entry or tree gets cut from the
  4474. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4475. called immediately, to move the markers with the entries."
  4476. (setq org-markers-to-move nil)
  4477. (when (featurep 'org-clock)
  4478. (org-clock-save-markers-for-cut-and-paste beg end))
  4479. (when (featurep 'org-agenda)
  4480. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4481. (defun org-check-and-save-marker (marker beg end)
  4482. "Check if MARKER is between BEG and END.
  4483. If yes, remember the marker and the distance to BEG."
  4484. (when (and (marker-buffer marker)
  4485. (equal (marker-buffer marker) (current-buffer)))
  4486. (if (and (>= marker beg) (< marker end))
  4487. (push (cons marker (- marker beg)) org-markers-to-move))))
  4488. (defun org-reinstall-markers-in-region (beg)
  4489. "Move all remembered markers to their position relative to BEG."
  4490. (mapc (lambda (x)
  4491. (move-marker (car x) (+ beg (cdr x))))
  4492. org-markers-to-move)
  4493. (setq org-markers-to-move nil))
  4494. (defun org-narrow-to-subtree ()
  4495. "Narrow buffer to the current subtree."
  4496. (interactive)
  4497. (save-excursion
  4498. (save-match-data
  4499. (narrow-to-region
  4500. (progn (org-back-to-heading) (point))
  4501. (progn (org-end-of-subtree t t) (point))))))
  4502. ;;; Outline Sorting
  4503. (defun org-sort (with-case)
  4504. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4505. Optional argument WITH-CASE means sort case-sensitively."
  4506. (interactive "P")
  4507. (if (org-at-table-p)
  4508. (org-call-with-arg 'org-table-sort-lines with-case)
  4509. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4510. (defun org-sort-remove-invisible (s)
  4511. (remove-text-properties 0 (length s) org-rm-props s)
  4512. (while (string-match org-bracket-link-regexp s)
  4513. (setq s (replace-match (if (match-end 2)
  4514. (match-string 3 s)
  4515. (match-string 1 s)) t t s)))
  4516. s)
  4517. (defvar org-priority-regexp) ; defined later in the file
  4518. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4519. "Sort entries on a certain level of an outline tree.
  4520. If there is an active region, the entries in the region are sorted.
  4521. Else, if the cursor is before the first entry, sort the top-level items.
  4522. Else, the children of the entry at point are sorted.
  4523. Sorting can be alphabetically, numerically, and by date/time as given by
  4524. the first time stamp in the entry. The command prompts for the sorting
  4525. type unless it has been given to the function through the SORTING-TYPE
  4526. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4527. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4528. called with point at the beginning of the record. It must return either
  4529. a string or a number that should serve as the sorting key for that record.
  4530. Comparing entries ignores case by default. However, with an optional argument
  4531. WITH-CASE, the sorting considers case as well."
  4532. (interactive "P")
  4533. (let ((case-func (if with-case 'identity 'downcase))
  4534. start beg end stars re re2
  4535. txt what tmp plain-list-p)
  4536. ;; Find beginning and end of region to sort
  4537. (cond
  4538. ((org-region-active-p)
  4539. ;; we will sort the region
  4540. (setq end (region-end)
  4541. what "region")
  4542. (goto-char (region-beginning))
  4543. (if (not (org-on-heading-p)) (outline-next-heading))
  4544. (setq start (point)))
  4545. ((org-at-item-p)
  4546. ;; we will sort this plain list
  4547. (org-beginning-of-item-list) (setq start (point))
  4548. (org-end-of-item-list) (setq end (point))
  4549. (goto-char start)
  4550. (setq plain-list-p t
  4551. what "plain list"))
  4552. ((or (org-on-heading-p)
  4553. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4554. ;; we will sort the children of the current headline
  4555. (org-back-to-heading)
  4556. (setq start (point)
  4557. end (progn (org-end-of-subtree t t)
  4558. (org-back-over-empty-lines)
  4559. (point))
  4560. what "children")
  4561. (goto-char start)
  4562. (show-subtree)
  4563. (outline-next-heading))
  4564. (t
  4565. ;; we will sort the top-level entries in this file
  4566. (goto-char (point-min))
  4567. (or (org-on-heading-p) (outline-next-heading))
  4568. (setq start (point) end (point-max) what "top-level")
  4569. (goto-char start)
  4570. (show-all)))
  4571. (setq beg (point))
  4572. (if (>= beg end) (error "Nothing to sort"))
  4573. (unless plain-list-p
  4574. (looking-at "\\(\\*+\\)")
  4575. (setq stars (match-string 1)
  4576. re (concat "^" (regexp-quote stars) " +")
  4577. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4578. txt (buffer-substring beg end))
  4579. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4580. (if (and (not (equal stars "*")) (string-match re2 txt))
  4581. (error "Region to sort contains a level above the first entry")))
  4582. (unless sorting-type
  4583. (message
  4584. (if plain-list-p
  4585. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4586. "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:")
  4587. what)
  4588. (setq sorting-type (read-char-exclusive))
  4589. (and (= (downcase sorting-type) ?f)
  4590. (setq getkey-func
  4591. (completing-read "Sort using function: "
  4592. obarray 'fboundp t nil nil))
  4593. (setq getkey-func (intern getkey-func)))
  4594. (and (= (downcase sorting-type) ?r)
  4595. (setq property
  4596. (completing-read "Property: "
  4597. (mapcar 'list (org-buffer-property-keys t))
  4598. nil t))))
  4599. (message "Sorting entries...")
  4600. (save-restriction
  4601. (narrow-to-region start end)
  4602. (let ((dcst (downcase sorting-type))
  4603. (now (current-time)))
  4604. (sort-subr
  4605. (/= dcst sorting-type)
  4606. ;; This function moves to the beginning character of the "record" to
  4607. ;; be sorted.
  4608. (if plain-list-p
  4609. (lambda nil
  4610. (if (org-at-item-p) t (goto-char (point-max))))
  4611. (lambda nil
  4612. (if (re-search-forward re nil t)
  4613. (goto-char (match-beginning 0))
  4614. (goto-char (point-max)))))
  4615. ;; This function moves to the last character of the "record" being
  4616. ;; sorted.
  4617. (if plain-list-p
  4618. 'org-end-of-item
  4619. (lambda nil
  4620. (save-match-data
  4621. (condition-case nil
  4622. (outline-forward-same-level 1)
  4623. (error
  4624. (goto-char (point-max)))))))
  4625. ;; This function returns the value that gets sorted against.
  4626. (if plain-list-p
  4627. (lambda nil
  4628. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4629. (cond
  4630. ((= dcst ?n)
  4631. (string-to-number (buffer-substring (match-end 0)
  4632. (point-at-eol))))
  4633. ((= dcst ?a)
  4634. (buffer-substring (match-end 0) (point-at-eol)))
  4635. ((= dcst ?t)
  4636. (if (re-search-forward org-ts-regexp
  4637. (point-at-eol) t)
  4638. (org-time-string-to-time (match-string 0))
  4639. now))
  4640. ((= dcst ?f)
  4641. (if getkey-func
  4642. (progn
  4643. (setq tmp (funcall getkey-func))
  4644. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4645. tmp)
  4646. (error "Invalid key function `%s'" getkey-func)))
  4647. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4648. (lambda nil
  4649. (cond
  4650. ((= dcst ?n)
  4651. (if (looking-at outline-regexp)
  4652. (string-to-number (buffer-substring (match-end 0)
  4653. (point-at-eol)))
  4654. nil))
  4655. ((= dcst ?a)
  4656. (funcall case-func (buffer-substring (point-at-bol)
  4657. (point-at-eol))))
  4658. ((= dcst ?t)
  4659. (if (re-search-forward org-ts-regexp
  4660. (save-excursion
  4661. (forward-line 2)
  4662. (point)) t)
  4663. (org-time-string-to-time (match-string 0))
  4664. now))
  4665. ((= dcst ?p)
  4666. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4667. (string-to-char (match-string 2))
  4668. org-default-priority))
  4669. ((= dcst ?r)
  4670. (or (org-entry-get nil property) ""))
  4671. ((= dcst ?o)
  4672. (if (looking-at org-complex-heading-regexp)
  4673. (- 9999 (length (member (match-string 2)
  4674. org-todo-keywords-1)))))
  4675. ((= dcst ?f)
  4676. (if getkey-func
  4677. (progn
  4678. (setq tmp (funcall getkey-func))
  4679. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4680. tmp)
  4681. (error "Invalid key function `%s'" getkey-func)))
  4682. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4683. nil
  4684. (cond
  4685. ((= dcst ?a) 'string<)
  4686. ((= dcst ?t) 'time-less-p)
  4687. (t nil)))))
  4688. (message "Sorting entries...done")))
  4689. (defun org-do-sort (table what &optional with-case sorting-type)
  4690. "Sort TABLE of WHAT according to SORTING-TYPE.
  4691. The user will be prompted for the SORTING-TYPE if the call to this
  4692. function does not specify it. WHAT is only for the prompt, to indicate
  4693. what is being sorted. The sorting key will be extracted from
  4694. the car of the elements of the table.
  4695. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4696. (unless sorting-type
  4697. (message
  4698. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4699. what)
  4700. (setq sorting-type (read-char-exclusive)))
  4701. (let ((dcst (downcase sorting-type))
  4702. extractfun comparefun)
  4703. ;; Define the appropriate functions
  4704. (cond
  4705. ((= dcst ?n)
  4706. (setq extractfun 'string-to-number
  4707. comparefun (if (= dcst sorting-type) '< '>)))
  4708. ((= dcst ?a)
  4709. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4710. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4711. comparefun (if (= dcst sorting-type)
  4712. 'string<
  4713. (lambda (a b) (and (not (string< a b))
  4714. (not (string= a b)))))))
  4715. ((= dcst ?t)
  4716. (setq extractfun
  4717. (lambda (x)
  4718. (if (string-match org-ts-regexp x)
  4719. (time-to-seconds
  4720. (org-time-string-to-time (match-string 0 x)))
  4721. 0))
  4722. comparefun (if (= dcst sorting-type) '< '>)))
  4723. (t (error "Invalid sorting type `%c'" sorting-type)))
  4724. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4725. table)
  4726. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4727. ;;;; Plain list items, including checkboxes
  4728. ;;; Plain list items
  4729. (defun org-at-item-p ()
  4730. "Is point in a line starting a hand-formatted item?"
  4731. (let ((llt org-plain-list-ordered-item-terminator))
  4732. (save-excursion
  4733. (goto-char (point-at-bol))
  4734. (looking-at
  4735. (cond
  4736. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4737. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4738. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4739. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4740. (defun org-in-item-p ()
  4741. "It the cursor inside a plain list item.
  4742. Does not have to be the first line."
  4743. (save-excursion
  4744. (condition-case nil
  4745. (progn
  4746. (org-beginning-of-item)
  4747. (org-at-item-p)
  4748. t)
  4749. (error nil))))
  4750. (defun org-insert-item (&optional checkbox)
  4751. "Insert a new item at the current level.
  4752. Return t when things worked, nil when we are not in an item."
  4753. (when (save-excursion
  4754. (condition-case nil
  4755. (progn
  4756. (org-beginning-of-item)
  4757. (org-at-item-p)
  4758. (if (org-invisible-p) (error "Invisible item"))
  4759. t)
  4760. (error nil)))
  4761. (let* ((bul (match-string 0))
  4762. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4763. (match-end 0)))
  4764. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4765. pos)
  4766. (cond
  4767. ((and (org-at-item-p) (<= (point) eow))
  4768. ;; before the bullet
  4769. (beginning-of-line 1)
  4770. (open-line (if blank 2 1)))
  4771. ((<= (point) eow)
  4772. (beginning-of-line 1))
  4773. (t
  4774. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4775. (end-of-line 1)
  4776. (delete-horizontal-space))
  4777. (newline (if blank 2 1))))
  4778. (insert bul (if checkbox "[ ]" ""))
  4779. (just-one-space)
  4780. (setq pos (point))
  4781. (end-of-line 1)
  4782. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4783. (org-maybe-renumber-ordered-list)
  4784. (and checkbox (org-update-checkbox-count-maybe))
  4785. t))
  4786. ;;; Checkboxes
  4787. (defun org-at-item-checkbox-p ()
  4788. "Is point at a line starting a plain-list item with a checklet?"
  4789. (and (org-at-item-p)
  4790. (save-excursion
  4791. (goto-char (match-end 0))
  4792. (skip-chars-forward " \t")
  4793. (looking-at "\\[[- X]\\]"))))
  4794. (defun org-toggle-checkbox (&optional arg)
  4795. "Toggle the checkbox in the current line."
  4796. (interactive "P")
  4797. (catch 'exit
  4798. (let (beg end status (firstnew 'unknown))
  4799. (cond
  4800. ((org-region-active-p)
  4801. (setq beg (region-beginning) end (region-end)))
  4802. ((org-on-heading-p)
  4803. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4804. ((org-at-item-checkbox-p)
  4805. (let ((pos (point)))
  4806. (replace-match
  4807. (cond (arg "[-]")
  4808. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4809. (t "[ ]"))
  4810. t t)
  4811. (goto-char pos))
  4812. (throw 'exit t))
  4813. (t (error "Not at a checkbox or heading, and no active region")))
  4814. (save-excursion
  4815. (goto-char beg)
  4816. (while (< (point) end)
  4817. (when (org-at-item-checkbox-p)
  4818. (setq status (equal (match-string 0) "[X]"))
  4819. (when (eq firstnew 'unknown)
  4820. (setq firstnew (not status)))
  4821. (replace-match
  4822. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4823. (beginning-of-line 2)))))
  4824. (org-update-checkbox-count-maybe))
  4825. (defun org-update-checkbox-count-maybe ()
  4826. "Update checkbox statistics unless turned off by user."
  4827. (when org-provide-checkbox-statistics
  4828. (org-update-checkbox-count)))
  4829. (defun org-update-checkbox-count (&optional all)
  4830. "Update the checkbox statistics in the current section.
  4831. This will find all statistic cookies like [57%] and [6/12] and update them
  4832. with the current numbers. With optional prefix argument ALL, do this for
  4833. the whole buffer."
  4834. (interactive "P")
  4835. (save-excursion
  4836. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4837. (beg (condition-case nil
  4838. (progn (outline-back-to-heading) (point))
  4839. (error (point-min))))
  4840. (end (move-marker (make-marker)
  4841. (progn (outline-next-heading) (point))))
  4842. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4843. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4844. (re-find (concat re "\\|" re-box))
  4845. beg-cookie end-cookie is-percent c-on c-off lim
  4846. eline curr-ind next-ind continue-from startsearch
  4847. (cstat 0)
  4848. )
  4849. (when all
  4850. (goto-char (point-min))
  4851. (outline-next-heading)
  4852. (setq beg (point) end (point-max)))
  4853. (goto-char end)
  4854. ;; find each statistic cookie
  4855. (while (re-search-backward re-find beg t)
  4856. (setq beg-cookie (match-beginning 1)
  4857. end-cookie (match-end 1)
  4858. cstat (+ cstat (if end-cookie 1 0))
  4859. startsearch (point-at-eol)
  4860. continue-from (point-at-bol)
  4861. is-percent (match-beginning 2)
  4862. lim (cond
  4863. ((org-on-heading-p) (outline-next-heading) (point))
  4864. ((org-at-item-p) (org-end-of-item) (point))
  4865. (t nil))
  4866. c-on 0
  4867. c-off 0)
  4868. (when lim
  4869. ;; find first checkbox for this cookie and gather
  4870. ;; statistics from all that are at this indentation level
  4871. (goto-char startsearch)
  4872. (if (re-search-forward re-box lim t)
  4873. (progn
  4874. (org-beginning-of-item)
  4875. (setq curr-ind (org-get-indentation))
  4876. (setq next-ind curr-ind)
  4877. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  4878. (save-excursion (end-of-line) (setq eline (point)))
  4879. (if (re-search-forward re-box eline t)
  4880. (if (member (match-string 2) '("[ ]" "[-]"))
  4881. (setq c-off (1+ c-off))
  4882. (setq c-on (1+ c-on))
  4883. )
  4884. )
  4885. (org-end-of-item)
  4886. (setq next-ind (org-get-indentation))
  4887. )))
  4888. (goto-char continue-from)
  4889. ;; update cookie
  4890. (when end-cookie
  4891. (delete-region beg-cookie end-cookie)
  4892. (goto-char beg-cookie)
  4893. (insert
  4894. (if is-percent
  4895. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4896. (format "[%d/%d]" c-on (+ c-on c-off)))))
  4897. ;; update items checkbox if it has one
  4898. (when (org-at-item-p)
  4899. (org-beginning-of-item)
  4900. (when (and (> (+ c-on c-off) 0)
  4901. (re-search-forward re-box (point-at-eol) t))
  4902. (setq beg-cookie (match-beginning 2)
  4903. end-cookie (match-end 2))
  4904. (delete-region beg-cookie end-cookie)
  4905. (goto-char beg-cookie)
  4906. (cond ((= c-off 0) (insert "[X]"))
  4907. ((= c-on 0) (insert "[ ]"))
  4908. (t (insert "[-]")))
  4909. )))
  4910. (goto-char continue-from))
  4911. (when (interactive-p)
  4912. (message "Checkbox satistics updated %s (%d places)"
  4913. (if all "in entire file" "in current outline entry") cstat)))))
  4914. (defun org-get-checkbox-statistics-face ()
  4915. "Select the face for checkbox statistics.
  4916. The face will be `org-done' when all relevant boxes are checked. Otherwise
  4917. it will be `org-todo'."
  4918. (if (match-end 1)
  4919. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  4920. (if (and (> (match-end 2) (match-beginning 2))
  4921. (equal (match-string 2) (match-string 3)))
  4922. 'org-done
  4923. 'org-todo)))
  4924. (defun org-get-indentation (&optional line)
  4925. "Get the indentation of the current line, interpreting tabs.
  4926. When LINE is given, assume it represents a line and compute its indentation."
  4927. (if line
  4928. (if (string-match "^ *" (org-remove-tabs line))
  4929. (match-end 0))
  4930. (save-excursion
  4931. (beginning-of-line 1)
  4932. (skip-chars-forward " \t")
  4933. (current-column))))
  4934. (defun org-remove-tabs (s &optional width)
  4935. "Replace tabulators in S with spaces.
  4936. Assumes that s is a single line, starting in column 0."
  4937. (setq width (or width tab-width))
  4938. (while (string-match "\t" s)
  4939. (setq s (replace-match
  4940. (make-string
  4941. (- (* width (/ (+ (match-beginning 0) width) width))
  4942. (match-beginning 0)) ?\ )
  4943. t t s)))
  4944. s)
  4945. (defun org-fix-indentation (line ind)
  4946. "Fix indentation in LINE.
  4947. IND is a cons cell with target and minimum indentation.
  4948. If the current indenation in LINE is smaller than the minimum,
  4949. leave it alone. If it is larger than ind, set it to the target."
  4950. (let* ((l (org-remove-tabs line))
  4951. (i (org-get-indentation l))
  4952. (i1 (car ind)) (i2 (cdr ind)))
  4953. (if (>= i i2) (setq l (substring line i2)))
  4954. (if (> i1 0)
  4955. (concat (make-string i1 ?\ ) l)
  4956. l)))
  4957. (defun org-beginning-of-item ()
  4958. "Go to the beginning of the current hand-formatted item.
  4959. If the cursor is not in an item, throw an error."
  4960. (interactive)
  4961. (let ((pos (point))
  4962. (limit (save-excursion
  4963. (condition-case nil
  4964. (progn
  4965. (org-back-to-heading)
  4966. (beginning-of-line 2) (point))
  4967. (error (point-min)))))
  4968. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4969. ind ind1)
  4970. (if (org-at-item-p)
  4971. (beginning-of-line 1)
  4972. (beginning-of-line 1)
  4973. (skip-chars-forward " \t")
  4974. (setq ind (current-column))
  4975. (if (catch 'exit
  4976. (while t
  4977. (beginning-of-line 0)
  4978. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  4979. (if (looking-at "[ \t]*$")
  4980. (setq ind1 ind-empty)
  4981. (skip-chars-forward " \t")
  4982. (setq ind1 (current-column)))
  4983. (if (< ind1 ind)
  4984. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  4985. nil
  4986. (goto-char pos)
  4987. (error "Not in an item")))))
  4988. (defun org-end-of-item ()
  4989. "Go to the end of the current hand-formatted item.
  4990. If the cursor is not in an item, throw an error."
  4991. (interactive)
  4992. (let* ((pos (point))
  4993. ind1
  4994. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4995. (limit (save-excursion (outline-next-heading) (point)))
  4996. (ind (save-excursion
  4997. (org-beginning-of-item)
  4998. (skip-chars-forward " \t")
  4999. (current-column)))
  5000. (end (catch 'exit
  5001. (while t
  5002. (beginning-of-line 2)
  5003. (if (eobp) (throw 'exit (point)))
  5004. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5005. (if (looking-at "[ \t]*$")
  5006. (setq ind1 ind-empty)
  5007. (skip-chars-forward " \t")
  5008. (setq ind1 (current-column)))
  5009. (if (<= ind1 ind)
  5010. (throw 'exit (point-at-bol)))))))
  5011. (if end
  5012. (goto-char end)
  5013. (goto-char pos)
  5014. (error "Not in an item"))))
  5015. (defun org-next-item ()
  5016. "Move to the beginning of the next item in the current plain list.
  5017. Error if not at a plain list, or if this is the last item in the list."
  5018. (interactive)
  5019. (let (ind ind1 (pos (point)))
  5020. (org-beginning-of-item)
  5021. (setq ind (org-get-indentation))
  5022. (org-end-of-item)
  5023. (setq ind1 (org-get-indentation))
  5024. (unless (and (org-at-item-p) (= ind ind1))
  5025. (goto-char pos)
  5026. (error "On last item"))))
  5027. (defun org-previous-item ()
  5028. "Move to the beginning of the previous item in the current plain list.
  5029. Error if not at a plain list, or if this is the first item in the list."
  5030. (interactive)
  5031. (let (beg ind ind1 (pos (point)))
  5032. (org-beginning-of-item)
  5033. (setq beg (point))
  5034. (setq ind (org-get-indentation))
  5035. (goto-char beg)
  5036. (catch 'exit
  5037. (while t
  5038. (beginning-of-line 0)
  5039. (if (looking-at "[ \t]*$")
  5040. nil
  5041. (if (<= (setq ind1 (org-get-indentation)) ind)
  5042. (throw 'exit t)))))
  5043. (condition-case nil
  5044. (if (or (not (org-at-item-p))
  5045. (< ind1 (1- ind)))
  5046. (error "")
  5047. (org-beginning-of-item))
  5048. (error (goto-char pos)
  5049. (error "On first item")))))
  5050. (defun org-first-list-item-p ()
  5051. "Is this heading the item in a plain list?"
  5052. (unless (org-at-item-p)
  5053. (error "Not at a plain list item"))
  5054. (org-beginning-of-item)
  5055. (= (point) (save-excursion (org-beginning-of-item-list))))
  5056. (defun org-move-item-down ()
  5057. "Move the plain list item at point down, i.e. swap with following item.
  5058. Subitems (items with larger indentation) are considered part of the item,
  5059. so this really moves item trees."
  5060. (interactive)
  5061. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5062. (org-beginning-of-item)
  5063. (setq beg0 (point))
  5064. (save-excursion
  5065. (setq ne-beg (org-back-over-empty-lines))
  5066. (setq beg (point)))
  5067. (goto-char beg0)
  5068. (setq ind (org-get-indentation))
  5069. (org-end-of-item)
  5070. (setq end0 (point))
  5071. (setq ind1 (org-get-indentation))
  5072. (setq ne-end (org-back-over-empty-lines))
  5073. (setq end (point))
  5074. (goto-char beg0)
  5075. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5076. ;; include less whitespace
  5077. (save-excursion
  5078. (goto-char beg)
  5079. (forward-line (- ne-beg ne-end))
  5080. (setq beg (point))))
  5081. (goto-char end0)
  5082. (if (and (org-at-item-p) (= ind ind1))
  5083. (progn
  5084. (org-end-of-item)
  5085. (org-back-over-empty-lines)
  5086. (setq txt (buffer-substring beg end))
  5087. (save-excursion
  5088. (delete-region beg end))
  5089. (setq pos (point))
  5090. (insert txt)
  5091. (goto-char pos) (org-skip-whitespace)
  5092. (org-maybe-renumber-ordered-list))
  5093. (goto-char pos)
  5094. (error "Cannot move this item further down"))))
  5095. (defun org-move-item-up (arg)
  5096. "Move the plain list item at point up, i.e. swap with previous item.
  5097. Subitems (items with larger indentation) are considered part of the item,
  5098. so this really moves item trees."
  5099. (interactive "p")
  5100. (let (beg beg0 end ind ind1 (pos (point)) txt
  5101. ne-beg ne-ins ins-end)
  5102. (org-beginning-of-item)
  5103. (setq beg0 (point))
  5104. (setq ind (org-get-indentation))
  5105. (save-excursion
  5106. (setq ne-beg (org-back-over-empty-lines))
  5107. (setq beg (point)))
  5108. (goto-char beg0)
  5109. (org-end-of-item)
  5110. (setq end (point))
  5111. (goto-char beg0)
  5112. (catch 'exit
  5113. (while t
  5114. (beginning-of-line 0)
  5115. (if (looking-at "[ \t]*$")
  5116. (if org-empty-line-terminates-plain-lists
  5117. (progn
  5118. (goto-char pos)
  5119. (error "Cannot move this item further up"))
  5120. nil)
  5121. (if (<= (setq ind1 (org-get-indentation)) ind)
  5122. (throw 'exit t)))))
  5123. (condition-case nil
  5124. (org-beginning-of-item)
  5125. (error (goto-char beg)
  5126. (error "Cannot move this item further up")))
  5127. (setq ind1 (org-get-indentation))
  5128. (if (and (org-at-item-p) (= ind ind1))
  5129. (progn
  5130. (setq ne-ins (org-back-over-empty-lines))
  5131. (setq txt (buffer-substring beg end))
  5132. (save-excursion
  5133. (delete-region beg end))
  5134. (setq pos (point))
  5135. (insert txt)
  5136. (setq ins-end (point))
  5137. (goto-char pos) (org-skip-whitespace)
  5138. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5139. ;; Move whitespace back to beginning
  5140. (save-excursion
  5141. (goto-char ins-end)
  5142. (let ((kill-whole-line t))
  5143. (kill-line (- ne-ins ne-beg)) (point)))
  5144. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5145. (org-maybe-renumber-ordered-list))
  5146. (goto-char pos)
  5147. (error "Cannot move this item further up"))))
  5148. (defun org-maybe-renumber-ordered-list ()
  5149. "Renumber the ordered list at point if setup allows it.
  5150. This tests the user option `org-auto-renumber-ordered-lists' before
  5151. doing the renumbering."
  5152. (interactive)
  5153. (when (and org-auto-renumber-ordered-lists
  5154. (org-at-item-p))
  5155. (if (match-beginning 3)
  5156. (org-renumber-ordered-list 1)
  5157. (org-fix-bullet-type))))
  5158. (defun org-maybe-renumber-ordered-list-safe ()
  5159. (condition-case nil
  5160. (save-excursion
  5161. (org-maybe-renumber-ordered-list))
  5162. (error nil)))
  5163. (defun org-cycle-list-bullet (&optional which)
  5164. "Cycle through the different itemize/enumerate bullets.
  5165. This cycle the entire list level through the sequence:
  5166. `-' -> `+' -> `*' -> `1.' -> `1)'
  5167. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5168. 0 meand `-', 1 means `+' etc."
  5169. (interactive "P")
  5170. (org-preserve-lc
  5171. (org-beginning-of-item-list)
  5172. (org-at-item-p)
  5173. (beginning-of-line 1)
  5174. (let ((current (match-string 0))
  5175. (prevp (eq which 'previous))
  5176. new)
  5177. (setq new (cond
  5178. ((and (numberp which)
  5179. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5180. ((string-match "-" current) (if prevp "1)" "+"))
  5181. ((string-match "\\+" current)
  5182. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5183. ((string-match "\\*" current) (if prevp "+" "1."))
  5184. ((string-match "\\." current) (if prevp "*" "1)"))
  5185. ((string-match ")" current) (if prevp "1." "-"))
  5186. (t (error "This should not happen"))))
  5187. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5188. (org-fix-bullet-type)
  5189. (org-maybe-renumber-ordered-list))))
  5190. (defun org-get-string-indentation (s)
  5191. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5192. (let ((n -1) (i 0) (w tab-width) c)
  5193. (catch 'exit
  5194. (while (< (setq n (1+ n)) (length s))
  5195. (setq c (aref s n))
  5196. (cond ((= c ?\ ) (setq i (1+ i)))
  5197. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5198. (t (throw 'exit t)))))
  5199. i))
  5200. (defun org-renumber-ordered-list (arg)
  5201. "Renumber an ordered plain list.
  5202. Cursor needs to be in the first line of an item, the line that starts
  5203. with something like \"1.\" or \"2)\"."
  5204. (interactive "p")
  5205. (unless (and (org-at-item-p)
  5206. (match-beginning 3))
  5207. (error "This is not an ordered list"))
  5208. (let ((line (org-current-line))
  5209. (col (current-column))
  5210. (ind (org-get-string-indentation
  5211. (buffer-substring (point-at-bol) (match-beginning 3))))
  5212. ;; (term (substring (match-string 3) -1))
  5213. ind1 (n (1- arg))
  5214. fmt)
  5215. ;; find where this list begins
  5216. (org-beginning-of-item-list)
  5217. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5218. (setq fmt (concat "%d" (match-string 1)))
  5219. (beginning-of-line 0)
  5220. ;; walk forward and replace these numbers
  5221. (catch 'exit
  5222. (while t
  5223. (catch 'next
  5224. (beginning-of-line 2)
  5225. (if (eobp) (throw 'exit nil))
  5226. (if (looking-at "[ \t]*$") (throw 'next nil))
  5227. (skip-chars-forward " \t") (setq ind1 (current-column))
  5228. (if (> ind1 ind) (throw 'next t))
  5229. (if (< ind1 ind) (throw 'exit t))
  5230. (if (not (org-at-item-p)) (throw 'exit nil))
  5231. (delete-region (match-beginning 2) (match-end 2))
  5232. (goto-char (match-beginning 2))
  5233. (insert (format fmt (setq n (1+ n)))))))
  5234. (goto-line line)
  5235. (org-move-to-column col)))
  5236. (defun org-fix-bullet-type ()
  5237. "Make sure all items in this list have the same bullet as the firsst item."
  5238. (interactive)
  5239. (unless (org-at-item-p) (error "This is not a list"))
  5240. (let ((line (org-current-line))
  5241. (col (current-column))
  5242. (ind (current-indentation))
  5243. ind1 bullet)
  5244. ;; find where this list begins
  5245. (org-beginning-of-item-list)
  5246. (beginning-of-line 1)
  5247. ;; find out what the bullet type is
  5248. (looking-at "[ \t]*\\(\\S-+\\)")
  5249. (setq bullet (match-string 1))
  5250. ;; walk forward and replace these numbers
  5251. (beginning-of-line 0)
  5252. (catch 'exit
  5253. (while t
  5254. (catch 'next
  5255. (beginning-of-line 2)
  5256. (if (eobp) (throw 'exit nil))
  5257. (if (looking-at "[ \t]*$") (throw 'next nil))
  5258. (skip-chars-forward " \t") (setq ind1 (current-column))
  5259. (if (> ind1 ind) (throw 'next t))
  5260. (if (< ind1 ind) (throw 'exit t))
  5261. (if (not (org-at-item-p)) (throw 'exit nil))
  5262. (skip-chars-forward " \t")
  5263. (looking-at "\\S-+")
  5264. (replace-match bullet))))
  5265. (goto-line line)
  5266. (org-move-to-column col)
  5267. (if (string-match "[0-9]" bullet)
  5268. (org-renumber-ordered-list 1))))
  5269. (defun org-beginning-of-item-list ()
  5270. "Go to the beginning of the current item list.
  5271. I.e. to the first item in this list."
  5272. (interactive)
  5273. (org-beginning-of-item)
  5274. (let ((pos (point-at-bol))
  5275. (ind (org-get-indentation))
  5276. ind1)
  5277. ;; find where this list begins
  5278. (catch 'exit
  5279. (while t
  5280. (catch 'next
  5281. (beginning-of-line 0)
  5282. (if (looking-at "[ \t]*$")
  5283. (throw (if (bobp) 'exit 'next) t))
  5284. (skip-chars-forward " \t") (setq ind1 (current-column))
  5285. (if (or (< ind1 ind)
  5286. (and (= ind1 ind)
  5287. (not (org-at-item-p)))
  5288. (bobp))
  5289. (throw 'exit t)
  5290. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5291. (goto-char pos)))
  5292. (defun org-end-of-item-list ()
  5293. "Go to the end of the current item list.
  5294. I.e. to the text after the last item."
  5295. (interactive)
  5296. (org-beginning-of-item)
  5297. (let ((pos (point-at-bol))
  5298. (ind (org-get-indentation))
  5299. ind1)
  5300. ;; find where this list begins
  5301. (catch 'exit
  5302. (while t
  5303. (catch 'next
  5304. (beginning-of-line 2)
  5305. (if (looking-at "[ \t]*$")
  5306. (throw (if (eobp) 'exit 'next) t))
  5307. (skip-chars-forward " \t") (setq ind1 (current-column))
  5308. (if (or (< ind1 ind)
  5309. (and (= ind1 ind)
  5310. (not (org-at-item-p)))
  5311. (eobp))
  5312. (progn
  5313. (setq pos (point-at-bol))
  5314. (throw 'exit t))))))
  5315. (goto-char pos)))
  5316. (defvar org-last-indent-begin-marker (make-marker))
  5317. (defvar org-last-indent-end-marker (make-marker))
  5318. (defun org-outdent-item (arg)
  5319. "Outdent a local list item."
  5320. (interactive "p")
  5321. (org-indent-item (- arg)))
  5322. (defun org-indent-item (arg)
  5323. "Indent a local list item."
  5324. (interactive "p")
  5325. (unless (org-at-item-p)
  5326. (error "Not on an item"))
  5327. (save-excursion
  5328. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5329. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5330. (setq beg org-last-indent-begin-marker
  5331. end org-last-indent-end-marker)
  5332. (org-beginning-of-item)
  5333. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5334. (org-end-of-item)
  5335. (setq end (move-marker org-last-indent-end-marker (point))))
  5336. (goto-char beg)
  5337. (setq tmp (org-item-indent-positions)
  5338. ind (car tmp)
  5339. ind-down (nth 2 tmp)
  5340. ind-up (nth 1 tmp)
  5341. delta (if (> arg 0)
  5342. (if ind-down (- ind-down ind) 2)
  5343. (if ind-up (- ind-up ind) -2)))
  5344. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5345. (while (< (point) end)
  5346. (beginning-of-line 1)
  5347. (skip-chars-forward " \t") (setq ind1 (current-column))
  5348. (delete-region (point-at-bol) (point))
  5349. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5350. (beginning-of-line 2))))
  5351. (org-fix-bullet-type)
  5352. (org-maybe-renumber-ordered-list-safe)
  5353. (save-excursion
  5354. (beginning-of-line 0)
  5355. (condition-case nil (org-beginning-of-item) (error nil))
  5356. (org-maybe-renumber-ordered-list-safe)))
  5357. (defun org-item-indent-positions ()
  5358. "Return indentation for plain list items.
  5359. This returns a list with three values: The current indentation, the
  5360. parent indentation and the indentation a child should habe.
  5361. Assumes cursor in item line."
  5362. (let* ((bolpos (point-at-bol))
  5363. (ind (org-get-indentation))
  5364. ind-down ind-up pos)
  5365. (save-excursion
  5366. (org-beginning-of-item-list)
  5367. (skip-chars-backward "\n\r \t")
  5368. (when (org-in-item-p)
  5369. (org-beginning-of-item)
  5370. (setq ind-up (org-get-indentation))))
  5371. (setq pos (point))
  5372. (save-excursion
  5373. (cond
  5374. ((and (condition-case nil (progn (org-previous-item) t)
  5375. (error nil))
  5376. (or (forward-char 1) t)
  5377. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5378. (setq ind-down (org-get-indentation)))
  5379. ((and (goto-char pos)
  5380. (org-at-item-p))
  5381. (goto-char (match-end 0))
  5382. (skip-chars-forward " \t")
  5383. (setq ind-down (current-column)))))
  5384. (list ind ind-up ind-down)))
  5385. ;;; The orgstruct minor mode
  5386. ;; Define a minor mode which can be used in other modes in order to
  5387. ;; integrate the org-mode structure editing commands.
  5388. ;; This is really a hack, because the org-mode structure commands use
  5389. ;; keys which normally belong to the major mode. Here is how it
  5390. ;; works: The minor mode defines all the keys necessary to operate the
  5391. ;; structure commands, but wraps the commands into a function which
  5392. ;; tests if the cursor is currently at a headline or a plain list
  5393. ;; item. If that is the case, the structure command is used,
  5394. ;; temporarily setting many Org-mode variables like regular
  5395. ;; expressions for filling etc. However, when any of those keys is
  5396. ;; used at a different location, function uses `key-binding' to look
  5397. ;; up if the key has an associated command in another currently active
  5398. ;; keymap (minor modes, major mode, global), and executes that
  5399. ;; command. There might be problems if any of the keys is otherwise
  5400. ;; used as a prefix key.
  5401. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5402. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5403. ;; addresses this by checking explicitly for both bindings.
  5404. (defvar orgstruct-mode-map (make-sparse-keymap)
  5405. "Keymap for the minor `orgstruct-mode'.")
  5406. (defvar org-local-vars nil
  5407. "List of local variables, for use by `orgstruct-mode'")
  5408. ;;;###autoload
  5409. (define-minor-mode orgstruct-mode
  5410. "Toggle the minor more `orgstruct-mode'.
  5411. This mode is for using Org-mode structure commands in other modes.
  5412. The following key behave as if Org-mode was active, if the cursor
  5413. is on a headline, or on a plain list item (both in the definition
  5414. of Org-mode).
  5415. M-up Move entry/item up
  5416. M-down Move entry/item down
  5417. M-left Promote
  5418. M-right Demote
  5419. M-S-up Move entry/item up
  5420. M-S-down Move entry/item down
  5421. M-S-left Promote subtree
  5422. M-S-right Demote subtree
  5423. M-q Fill paragraph and items like in Org-mode
  5424. C-c ^ Sort entries
  5425. C-c - Cycle list bullet
  5426. TAB Cycle item visibility
  5427. M-RET Insert new heading/item
  5428. S-M-RET Insert new TODO heading / Chekbox item
  5429. C-c C-c Set tags / toggle checkbox"
  5430. nil " OrgStruct" nil
  5431. (org-load-modules-maybe)
  5432. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5433. ;;;###autoload
  5434. (defun turn-on-orgstruct ()
  5435. "Unconditionally turn on `orgstruct-mode'."
  5436. (orgstruct-mode 1))
  5437. ;;;###autoload
  5438. (defun turn-on-orgstruct++ ()
  5439. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5440. In addition to setting orgstruct-mode, this also exports all indentation and
  5441. autofilling variables from org-mode into the buffer. Note that turning
  5442. off orgstruct-mode will *not* remove these additional settings."
  5443. (orgstruct-mode 1)
  5444. (let (var val)
  5445. (mapc
  5446. (lambda (x)
  5447. (when (string-match
  5448. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5449. (symbol-name (car x)))
  5450. (setq var (car x) val (nth 1 x))
  5451. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5452. org-local-vars)))
  5453. (defun orgstruct-error ()
  5454. "Error when there is no default binding for a structure key."
  5455. (interactive)
  5456. (error "This key has no function outside structure elements"))
  5457. (defun orgstruct-setup ()
  5458. "Setup orgstruct keymaps."
  5459. (let ((nfunc 0)
  5460. (bindings
  5461. (list
  5462. '([(meta up)] org-metaup)
  5463. '([(meta down)] org-metadown)
  5464. '([(meta left)] org-metaleft)
  5465. '([(meta right)] org-metaright)
  5466. '([(meta shift up)] org-shiftmetaup)
  5467. '([(meta shift down)] org-shiftmetadown)
  5468. '([(meta shift left)] org-shiftmetaleft)
  5469. '([(meta shift right)] org-shiftmetaright)
  5470. '([(shift up)] org-shiftup)
  5471. '([(shift down)] org-shiftdown)
  5472. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5473. '("\M-q" fill-paragraph)
  5474. '("\C-c^" org-sort)
  5475. '("\C-c-" org-cycle-list-bullet)))
  5476. elt key fun cmd)
  5477. (while (setq elt (pop bindings))
  5478. (setq nfunc (1+ nfunc))
  5479. (setq key (org-key (car elt))
  5480. fun (nth 1 elt)
  5481. cmd (orgstruct-make-binding fun nfunc key))
  5482. (org-defkey orgstruct-mode-map key cmd))
  5483. ;; Special treatment needed for TAB and RET
  5484. (org-defkey orgstruct-mode-map [(tab)]
  5485. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5486. (org-defkey orgstruct-mode-map "\C-i"
  5487. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5488. (org-defkey orgstruct-mode-map "\M-\C-m"
  5489. (orgstruct-make-binding 'org-insert-heading 105
  5490. "\M-\C-m" [(meta return)]))
  5491. (org-defkey orgstruct-mode-map [(meta return)]
  5492. (orgstruct-make-binding 'org-insert-heading 106
  5493. [(meta return)] "\M-\C-m"))
  5494. (org-defkey orgstruct-mode-map [(shift meta return)]
  5495. (orgstruct-make-binding 'org-insert-todo-heading 107
  5496. [(meta return)] "\M-\C-m"))
  5497. (unless org-local-vars
  5498. (setq org-local-vars (org-get-local-variables)))
  5499. t))
  5500. (defun orgstruct-make-binding (fun n &rest keys)
  5501. "Create a function for binding in the structure minor mode.
  5502. FUN is the command to call inside a table. N is used to create a unique
  5503. command name. KEYS are keys that should be checked in for a command
  5504. to execute outside of tables."
  5505. (eval
  5506. (list 'defun
  5507. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5508. '(arg)
  5509. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5510. "Outside of structure, run the binding of `"
  5511. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5512. "'.")
  5513. '(interactive "p")
  5514. (list 'if
  5515. '(org-context-p 'headline 'item)
  5516. (list 'org-run-like-in-org-mode (list 'quote fun))
  5517. (list 'let '(orgstruct-mode)
  5518. (list 'call-interactively
  5519. (append '(or)
  5520. (mapcar (lambda (k)
  5521. (list 'key-binding k))
  5522. keys)
  5523. '('orgstruct-error))))))))
  5524. (defun org-context-p (&rest contexts)
  5525. "Check if local context is and of CONTEXTS.
  5526. Possible values in the list of contexts are `table', `headline', and `item'."
  5527. (let ((pos (point)))
  5528. (goto-char (point-at-bol))
  5529. (prog1 (or (and (memq 'table contexts)
  5530. (looking-at "[ \t]*|"))
  5531. (and (memq 'headline contexts)
  5532. (looking-at "\\*+"))
  5533. (and (memq 'item contexts)
  5534. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5535. (goto-char pos))))
  5536. (defun org-get-local-variables ()
  5537. "Return a list of all local variables in an org-mode buffer."
  5538. (let (varlist)
  5539. (with-current-buffer (get-buffer-create "*Org tmp*")
  5540. (erase-buffer)
  5541. (org-mode)
  5542. (setq varlist (buffer-local-variables)))
  5543. (kill-buffer "*Org tmp*")
  5544. (delq nil
  5545. (mapcar
  5546. (lambda (x)
  5547. (setq x
  5548. (if (symbolp x)
  5549. (list x)
  5550. (list (car x) (list 'quote (cdr x)))))
  5551. (if (string-match
  5552. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5553. (symbol-name (car x)))
  5554. x nil))
  5555. varlist))))
  5556. ;;;###autoload
  5557. (defun org-run-like-in-org-mode (cmd)
  5558. (org-load-modules-maybe)
  5559. (unless org-local-vars
  5560. (setq org-local-vars (org-get-local-variables)))
  5561. (eval (list 'let org-local-vars
  5562. (list 'call-interactively (list 'quote cmd)))))
  5563. ;;;; Archiving
  5564. (defun org-get-category (&optional pos)
  5565. "Get the category applying to position POS."
  5566. (get-text-property (or pos (point)) 'org-category))
  5567. (defun org-refresh-category-properties ()
  5568. "Refresh category text properties in the buffer."
  5569. (let ((def-cat (cond
  5570. ((null org-category)
  5571. (if buffer-file-name
  5572. (file-name-sans-extension
  5573. (file-name-nondirectory buffer-file-name))
  5574. "???"))
  5575. ((symbolp org-category) (symbol-name org-category))
  5576. (t org-category)))
  5577. beg end cat pos optionp)
  5578. (org-unmodified
  5579. (save-excursion
  5580. (save-restriction
  5581. (widen)
  5582. (goto-char (point-min))
  5583. (put-text-property (point) (point-max) 'org-category def-cat)
  5584. (while (re-search-forward
  5585. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5586. (setq pos (match-end 0)
  5587. optionp (equal (char-after (match-beginning 0)) ?#)
  5588. cat (org-trim (match-string 2)))
  5589. (if optionp
  5590. (setq beg (point-at-bol) end (point-max))
  5591. (org-back-to-heading t)
  5592. (setq beg (point) end (org-end-of-subtree t t)))
  5593. (put-text-property beg end 'org-category cat)
  5594. (goto-char pos)))))))
  5595. ;;;; Link Stuff
  5596. ;;; Link abbreviations
  5597. (defun org-link-expand-abbrev (link)
  5598. "Apply replacements as defined in `org-link-abbrev-alist."
  5599. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5600. (let* ((key (match-string 1 link))
  5601. (as (or (assoc key org-link-abbrev-alist-local)
  5602. (assoc key org-link-abbrev-alist)))
  5603. (tag (and (match-end 2) (match-string 3 link)))
  5604. rpl)
  5605. (if (not as)
  5606. link
  5607. (setq rpl (cdr as))
  5608. (cond
  5609. ((symbolp rpl) (funcall rpl tag))
  5610. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5611. (t (concat rpl tag)))))
  5612. link))
  5613. ;;; Storing and inserting links
  5614. (defvar org-insert-link-history nil
  5615. "Minibuffer history for links inserted with `org-insert-link'.")
  5616. (defvar org-stored-links nil
  5617. "Contains the links stored with `org-store-link'.")
  5618. (defvar org-store-link-plist nil
  5619. "Plist with info about the most recently link created with `org-store-link'.")
  5620. (defvar org-link-protocols nil
  5621. "Link protocols added to Org-mode using `org-add-link-type'.")
  5622. (defvar org-store-link-functions nil
  5623. "List of functions that are called to create and store a link.
  5624. Each function will be called in turn until one returns a non-nil
  5625. value. Each function should check if it is responsible for creating
  5626. this link (for example by looking at the major mode).
  5627. If not, it must exit and return nil.
  5628. If yes, it should return a non-nil value after a calling
  5629. `org-store-link-props' with a list of properties and values.
  5630. Special properties are:
  5631. :type The link prefix. like \"http\". This must be given.
  5632. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5633. This is obligatory as well.
  5634. :description Optional default description for the second pair
  5635. of brackets in an Org-mode link. The user can still change
  5636. this when inserting this link into an Org-mode buffer.
  5637. In addition to these, any additional properties can be specified
  5638. and then used in remember templates.")
  5639. (defun org-add-link-type (type &optional follow export)
  5640. "Add TYPE to the list of `org-link-types'.
  5641. Re-compute all regular expressions depending on `org-link-types'
  5642. FOLLOW and EXPORT are two functions.
  5643. FOLLOW should take the link path as the single argument and do whatever
  5644. is necessary to follow the link, for example find a file or display
  5645. a mail message.
  5646. EXPORT should format the link path for export to one of the export formats.
  5647. It should be a function accepting three arguments:
  5648. path the path of the link, the text after the prefix (like \"http:\")
  5649. desc the description of the link, if any, nil if there was no descripton
  5650. format the export format, a symbol like `html' or `latex'.
  5651. The function may use the FORMAT information to return different values
  5652. depending on the format. The return value will be put literally into
  5653. the exported file.
  5654. Org-mode has a built-in default for exporting links. If you are happy with
  5655. this default, there is no need to define an export function for the link
  5656. type. For a simple example of an export function, see `org-bbdb.el'."
  5657. (add-to-list 'org-link-types type t)
  5658. (org-make-link-regexps)
  5659. (if (assoc type org-link-protocols)
  5660. (setcdr (assoc type org-link-protocols) (list follow export))
  5661. (push (list type follow export) org-link-protocols)))
  5662. ;;;###autoload
  5663. (defun org-store-link (arg)
  5664. "\\<org-mode-map>Store an org-link to the current location.
  5665. This link is added to `org-stored-links' and can later be inserted
  5666. into an org-buffer with \\[org-insert-link].
  5667. For some link types, a prefix arg is interpreted:
  5668. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5669. For file links, arg negates `org-context-in-file-links'."
  5670. (interactive "P")
  5671. (org-load-modules-maybe)
  5672. (setq org-store-link-plist nil) ; reset
  5673. (let (link cpltxt desc description search txt)
  5674. (cond
  5675. ((run-hook-with-args-until-success 'org-store-link-functions)
  5676. (setq link (plist-get org-store-link-plist :link)
  5677. desc (or (plist-get org-store-link-plist :description) link)))
  5678. ((eq major-mode 'calendar-mode)
  5679. (let ((cd (calendar-cursor-to-date)))
  5680. (setq link
  5681. (format-time-string
  5682. (car org-time-stamp-formats)
  5683. (apply 'encode-time
  5684. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5685. nil nil nil))))
  5686. (org-store-link-props :type "calendar" :date cd)))
  5687. ((eq major-mode 'w3-mode)
  5688. (setq cpltxt (url-view-url t)
  5689. link (org-make-link cpltxt))
  5690. (org-store-link-props :type "w3" :url (url-view-url t)))
  5691. ((eq major-mode 'w3m-mode)
  5692. (setq cpltxt (or w3m-current-title w3m-current-url)
  5693. link (org-make-link w3m-current-url))
  5694. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5695. ((setq search (run-hook-with-args-until-success
  5696. 'org-create-file-search-functions))
  5697. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5698. "::" search))
  5699. (setq cpltxt (or description link)))
  5700. ((eq major-mode 'image-mode)
  5701. (setq cpltxt (concat "file:"
  5702. (abbreviate-file-name buffer-file-name))
  5703. link (org-make-link cpltxt))
  5704. (org-store-link-props :type "image" :file buffer-file-name))
  5705. ((eq major-mode 'dired-mode)
  5706. ;; link to the file in the current line
  5707. (setq cpltxt (concat "file:"
  5708. (abbreviate-file-name
  5709. (expand-file-name
  5710. (dired-get-filename nil t))))
  5711. link (org-make-link cpltxt)))
  5712. ((and buffer-file-name (org-mode-p))
  5713. ;; Just link to current headline
  5714. (setq cpltxt (concat "file:"
  5715. (abbreviate-file-name buffer-file-name)))
  5716. ;; Add a context search string
  5717. (when (org-xor org-context-in-file-links arg)
  5718. ;; Check if we are on a target
  5719. (if (org-in-regexp "<<\\(.*?\\)>>")
  5720. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5721. (setq txt (cond
  5722. ((org-on-heading-p) nil)
  5723. ((org-region-active-p)
  5724. (buffer-substring (region-beginning) (region-end)))
  5725. (t nil)))
  5726. (when (or (null txt) (string-match "\\S-" txt))
  5727. (setq cpltxt
  5728. (concat cpltxt "::"
  5729. (condition-case nil
  5730. (org-make-org-heading-search-string txt)
  5731. (error "")))
  5732. desc "NONE"))))
  5733. (if (string-match "::\\'" cpltxt)
  5734. (setq cpltxt (substring cpltxt 0 -2)))
  5735. (setq link (org-make-link cpltxt)))
  5736. ((buffer-file-name (buffer-base-buffer))
  5737. ;; Just link to this file here.
  5738. (setq cpltxt (concat "file:"
  5739. (abbreviate-file-name
  5740. (buffer-file-name (buffer-base-buffer)))))
  5741. ;; Add a context string
  5742. (when (org-xor org-context-in-file-links arg)
  5743. (setq txt (if (org-region-active-p)
  5744. (buffer-substring (region-beginning) (region-end))
  5745. (buffer-substring (point-at-bol) (point-at-eol))))
  5746. ;; Only use search option if there is some text.
  5747. (when (string-match "\\S-" txt)
  5748. (setq cpltxt
  5749. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5750. desc "NONE")))
  5751. (setq link (org-make-link cpltxt)))
  5752. ((interactive-p)
  5753. (error "Cannot link to a buffer which is not visiting a file"))
  5754. (t (setq link nil)))
  5755. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5756. (setq link (or link cpltxt)
  5757. desc (or desc cpltxt))
  5758. (if (equal desc "NONE") (setq desc nil))
  5759. (if (and (interactive-p) link)
  5760. (progn
  5761. (setq org-stored-links
  5762. (cons (list link desc) org-stored-links))
  5763. (message "Stored: %s" (or desc link)))
  5764. (and link (org-make-link-string link desc)))))
  5765. (defun org-store-link-props (&rest plist)
  5766. "Store link properties, extract names and addresses."
  5767. (let (x adr)
  5768. (when (setq x (plist-get plist :from))
  5769. (setq adr (mail-extract-address-components x))
  5770. (plist-put plist :fromname (car adr))
  5771. (plist-put plist :fromaddress (nth 1 adr)))
  5772. (when (setq x (plist-get plist :to))
  5773. (setq adr (mail-extract-address-components x))
  5774. (plist-put plist :toname (car adr))
  5775. (plist-put plist :toaddress (nth 1 adr))))
  5776. (let ((from (plist-get plist :from))
  5777. (to (plist-get plist :to)))
  5778. (when (and from to org-from-is-user-regexp)
  5779. (plist-put plist :fromto
  5780. (if (string-match org-from-is-user-regexp from)
  5781. (concat "to %t")
  5782. (concat "from %f")))))
  5783. (setq org-store-link-plist plist))
  5784. (defun org-add-link-props (&rest plist)
  5785. "Add these properties to the link property list."
  5786. (let (key value)
  5787. (while plist
  5788. (setq key (pop plist) value (pop plist))
  5789. (setq org-store-link-plist
  5790. (plist-put org-store-link-plist key value)))))
  5791. (defun org-email-link-description (&optional fmt)
  5792. "Return the description part of an email link.
  5793. This takes information from `org-store-link-plist' and formats it
  5794. according to FMT (default from `org-email-link-description-format')."
  5795. (setq fmt (or fmt org-email-link-description-format))
  5796. (let* ((p org-store-link-plist)
  5797. (to (plist-get p :toaddress))
  5798. (from (plist-get p :fromaddress))
  5799. (table
  5800. (list
  5801. (cons "%c" (plist-get p :fromto))
  5802. (cons "%F" (plist-get p :from))
  5803. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5804. (cons "%T" (plist-get p :to))
  5805. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5806. (cons "%s" (plist-get p :subject))
  5807. (cons "%m" (plist-get p :message-id)))))
  5808. (when (string-match "%c" fmt)
  5809. ;; Check if the user wrote this message
  5810. (if (and org-from-is-user-regexp from to
  5811. (save-match-data (string-match org-from-is-user-regexp from)))
  5812. (setq fmt (replace-match "to %t" t t fmt))
  5813. (setq fmt (replace-match "from %f" t t fmt))))
  5814. (org-replace-escapes fmt table)))
  5815. (defun org-make-org-heading-search-string (&optional string heading)
  5816. "Make search string for STRING or current headline."
  5817. (interactive)
  5818. (let ((s (or string (org-get-heading))))
  5819. (unless (and string (not heading))
  5820. ;; We are using a headline, clean up garbage in there.
  5821. (if (string-match org-todo-regexp s)
  5822. (setq s (replace-match "" t t s)))
  5823. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5824. (setq s (replace-match "" t t s)))
  5825. (setq s (org-trim s))
  5826. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5827. org-comment-string "\\)") s)
  5828. (setq s (replace-match "" t t s)))
  5829. (while (string-match org-ts-regexp s)
  5830. (setq s (replace-match "" t t s))))
  5831. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5832. (setq s (replace-match " " t t s)))
  5833. (or string (setq s (concat "*" s))) ; Add * for headlines
  5834. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5835. (defun org-make-link (&rest strings)
  5836. "Concatenate STRINGS."
  5837. (apply 'concat strings))
  5838. (defun org-make-link-string (link &optional description)
  5839. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5840. (unless (string-match "\\S-" link)
  5841. (error "Empty link"))
  5842. (when (stringp description)
  5843. ;; Remove brackets from the description, they are fatal.
  5844. (while (string-match "\\[" description)
  5845. (setq description (replace-match "{" t t description)))
  5846. (while (string-match "\\]" description)
  5847. (setq description (replace-match "}" t t description))))
  5848. (when (equal (org-link-escape link) description)
  5849. ;; No description needed, it is identical
  5850. (setq description nil))
  5851. (when (and (not description)
  5852. (not (equal link (org-link-escape link))))
  5853. (setq description link))
  5854. (concat "[[" (org-link-escape link) "]"
  5855. (if description (concat "[" description "]") "")
  5856. "]"))
  5857. (defconst org-link-escape-chars
  5858. '((?\ . "%20")
  5859. (?\[ . "%5B")
  5860. (?\] . "%5D")
  5861. (?\340 . "%E0") ; `a
  5862. (?\342 . "%E2") ; ^a
  5863. (?\347 . "%E7") ; ,c
  5864. (?\350 . "%E8") ; `e
  5865. (?\351 . "%E9") ; 'e
  5866. (?\352 . "%EA") ; ^e
  5867. (?\356 . "%EE") ; ^i
  5868. (?\364 . "%F4") ; ^o
  5869. (?\371 . "%F9") ; `u
  5870. (?\373 . "%FB") ; ^u
  5871. (?\; . "%3B")
  5872. (?? . "%3F")
  5873. (?= . "%3D")
  5874. (?+ . "%2B")
  5875. )
  5876. "Association list of escapes for some characters problematic in links.
  5877. This is the list that is used for internal purposes.")
  5878. (defconst org-link-escape-chars-browser
  5879. '((?\ . "%20")) ; 32 for the SPC char
  5880. "Association list of escapes for some characters problematic in links.
  5881. This is the list that is used before handing over to the browser.")
  5882. (defun org-link-escape (text &optional table)
  5883. "Escape charaters in TEXT that are problematic for links."
  5884. (setq table (or table org-link-escape-chars))
  5885. (when text
  5886. (let ((re (mapconcat (lambda (x) (regexp-quote
  5887. (char-to-string (car x))))
  5888. table "\\|")))
  5889. (while (string-match re text)
  5890. (setq text
  5891. (replace-match
  5892. (cdr (assoc (string-to-char (match-string 0 text))
  5893. table))
  5894. t t text)))
  5895. text)))
  5896. (defun org-link-unescape (text &optional table)
  5897. "Reverse the action of `org-link-escape'."
  5898. (setq table (or table org-link-escape-chars))
  5899. (when text
  5900. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5901. table "\\|")))
  5902. (while (string-match re text)
  5903. (setq text
  5904. (replace-match
  5905. (char-to-string (car (rassoc (match-string 0 text) table)))
  5906. t t text)))
  5907. text)))
  5908. (defun org-xor (a b)
  5909. "Exclusive or."
  5910. (if a (not b) b))
  5911. (defun org-get-header (header)
  5912. "Find a header field in the current buffer."
  5913. (save-excursion
  5914. (goto-char (point-min))
  5915. (let ((case-fold-search t) s)
  5916. (cond
  5917. ((eq header 'from)
  5918. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5919. (setq s (match-string 1)))
  5920. (while (string-match "\"" s)
  5921. (setq s (replace-match "" t t s)))
  5922. (if (string-match "[<(].*" s)
  5923. (setq s (replace-match "" t t s))))
  5924. ((eq header 'message-id)
  5925. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5926. (setq s (match-string 1))))
  5927. ((eq header 'subject)
  5928. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5929. (setq s (match-string 1)))))
  5930. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5931. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5932. s)))
  5933. (defun org-fixup-message-id-for-http (s)
  5934. "Replace special characters in a message id, so it can be used in an http query."
  5935. (while (string-match "<" s)
  5936. (setq s (replace-match "%3C" t t s)))
  5937. (while (string-match ">" s)
  5938. (setq s (replace-match "%3E" t t s)))
  5939. (while (string-match "@" s)
  5940. (setq s (replace-match "%40" t t s)))
  5941. s)
  5942. ;;;###autoload
  5943. (defun org-insert-link-global ()
  5944. "Insert a link like Org-mode does.
  5945. This command can be called in any mode to insert a link in Org-mode syntax."
  5946. (interactive)
  5947. (org-load-modules-maybe)
  5948. (org-run-like-in-org-mode 'org-insert-link))
  5949. (defun org-insert-link (&optional complete-file link-location)
  5950. "Insert a link. At the prompt, enter the link.
  5951. Completion can be used to select a link previously stored with
  5952. `org-store-link'. When the empty string is entered (i.e. if you just
  5953. press RET at the prompt), the link defaults to the most recently
  5954. stored link. As SPC triggers completion in the minibuffer, you need to
  5955. use M-SPC or C-q SPC to force the insertion of a space character.
  5956. You will also be prompted for a description, and if one is given, it will
  5957. be displayed in the buffer instead of the link.
  5958. If there is already a link at point, this command will allow you to edit link
  5959. and description parts.
  5960. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5961. be selected using completion. The path to the file will be relative to the
  5962. current directory if the file is in the current directory or a subdirectory.
  5963. Otherwise, the link will be the absolute path as completed in the minibuffer
  5964. \(i.e. normally ~/path/to/file).
  5965. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5966. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  5967. of `org-keep-stored-link-after-insertion'.
  5968. If `org-make-link-description-function' is non-nil, this function will be
  5969. called with the link target, and the result will be the default
  5970. link description.
  5971. If the LINK-LOCATION parameter is non-nil, this value will be
  5972. used as the link location instead of reading one interactively."
  5973. (interactive "P")
  5974. (let* ((wcf (current-window-configuration))
  5975. (region (if (org-region-active-p)
  5976. (buffer-substring (region-beginning) (region-end))))
  5977. (remove (and region (list (region-beginning) (region-end))))
  5978. (desc region)
  5979. tmphist ; byte-compile incorrectly complains about this
  5980. (link link-location)
  5981. entry file)
  5982. (cond
  5983. (link-location) ; specified by arg, just use it.
  5984. ((org-in-regexp org-bracket-link-regexp 1)
  5985. ;; We do have a link at point, and we are going to edit it.
  5986. (setq remove (list (match-beginning 0) (match-end 0)))
  5987. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5988. (setq link (read-string "Link: "
  5989. (org-link-unescape
  5990. (org-match-string-no-properties 1)))))
  5991. ((or (org-in-regexp org-angle-link-re)
  5992. (org-in-regexp org-plain-link-re))
  5993. ;; Convert to bracket link
  5994. (setq remove (list (match-beginning 0) (match-end 0))
  5995. link (read-string "Link: "
  5996. (org-remove-angle-brackets (match-string 0)))))
  5997. ((equal complete-file '(4))
  5998. ;; Completing read for file names.
  5999. (setq file (read-file-name "File: "))
  6000. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6001. (pwd1 (file-name-as-directory (abbreviate-file-name
  6002. (expand-file-name ".")))))
  6003. (cond
  6004. ((equal complete-file '(16))
  6005. (setq link (org-make-link
  6006. "file:"
  6007. (abbreviate-file-name (expand-file-name file)))))
  6008. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6009. (setq link (org-make-link "file:" (match-string 1 file))))
  6010. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6011. (expand-file-name file))
  6012. (setq link (org-make-link
  6013. "file:" (match-string 1 (expand-file-name file)))))
  6014. (t (setq link (org-make-link "file:" file))))))
  6015. (t
  6016. ;; Read link, with completion for stored links.
  6017. (with-output-to-temp-buffer "*Org Links*"
  6018. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6019. (when org-stored-links
  6020. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6021. (princ (mapconcat
  6022. (lambda (x)
  6023. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6024. (reverse org-stored-links) "\n"))))
  6025. (let ((cw (selected-window)))
  6026. (select-window (get-buffer-window "*Org Links*"))
  6027. (shrink-window-if-larger-than-buffer)
  6028. (setq truncate-lines t)
  6029. (select-window cw))
  6030. ;; Fake a link history, containing the stored links.
  6031. (setq tmphist (append (mapcar 'car org-stored-links)
  6032. org-insert-link-history))
  6033. (unwind-protect
  6034. (setq link (org-completing-read
  6035. "Link: "
  6036. (append
  6037. (mapcar (lambda (x) (list (concat (car x) ":")))
  6038. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6039. (mapcar (lambda (x) (list (concat x ":")))
  6040. org-link-types))
  6041. nil nil nil
  6042. 'tmphist
  6043. (or (car (car org-stored-links)))))
  6044. (set-window-configuration wcf)
  6045. (kill-buffer "*Org Links*"))
  6046. (setq entry (assoc link org-stored-links))
  6047. (or entry (push link org-insert-link-history))
  6048. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6049. (not org-keep-stored-link-after-insertion))
  6050. (setq org-stored-links (delq (assoc link org-stored-links)
  6051. org-stored-links)))
  6052. (setq desc (or desc (nth 1 entry)))))
  6053. (if (string-match org-plain-link-re link)
  6054. ;; URL-like link, normalize the use of angular brackets.
  6055. (setq link (org-make-link (org-remove-angle-brackets link))))
  6056. ;; Check if we are linking to the current file with a search option
  6057. ;; If yes, simplify the link by using only the search option.
  6058. (when (and buffer-file-name
  6059. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6060. (let* ((path (match-string 1 link))
  6061. (case-fold-search nil)
  6062. (search (match-string 2 link)))
  6063. (save-match-data
  6064. (if (equal (file-truename buffer-file-name) (file-truename path))
  6065. ;; We are linking to this same file, with a search option
  6066. (setq link search)))))
  6067. ;; Check if we can/should use a relative path. If yes, simplify the link
  6068. (when (string-match "\\<file:\\(.*\\)" link)
  6069. (let* ((path (match-string 1 link))
  6070. (origpath path)
  6071. (case-fold-search nil))
  6072. (cond
  6073. ((eq org-link-file-path-type 'absolute)
  6074. (setq path (abbreviate-file-name (expand-file-name path))))
  6075. ((eq org-link-file-path-type 'noabbrev)
  6076. (setq path (expand-file-name path)))
  6077. ((eq org-link-file-path-type 'relative)
  6078. (setq path (file-relative-name path)))
  6079. (t
  6080. (save-match-data
  6081. (if (string-match (concat "^" (regexp-quote
  6082. (file-name-as-directory
  6083. (expand-file-name "."))))
  6084. (expand-file-name path))
  6085. ;; We are linking a file with relative path name.
  6086. (setq path (substring (expand-file-name path)
  6087. (match-end 0)))))))
  6088. (setq link (concat "file:" path))
  6089. (if (equal desc origpath)
  6090. (setq desc path))))
  6091. (if org-make-link-description-function
  6092. (setq desc (funcall org-make-link-description-function link desc)))
  6093. (setq desc (read-string "Description: " desc))
  6094. (unless (string-match "\\S-" desc) (setq desc nil))
  6095. (if remove (apply 'delete-region remove))
  6096. (insert (org-make-link-string link desc))))
  6097. (defun org-completing-read (&rest args)
  6098. (let ((minibuffer-local-completion-map
  6099. (copy-keymap minibuffer-local-completion-map)))
  6100. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6101. (apply 'completing-read args)))
  6102. ;;; Opening/following a link
  6103. (defvar org-link-search-failed nil)
  6104. (defun org-next-link ()
  6105. "Move forward to the next link.
  6106. If the link is in hidden text, expose it."
  6107. (interactive)
  6108. (when (and org-link-search-failed (eq this-command last-command))
  6109. (goto-char (point-min))
  6110. (message "Link search wrapped back to beginning of buffer"))
  6111. (setq org-link-search-failed nil)
  6112. (let* ((pos (point))
  6113. (ct (org-context))
  6114. (a (assoc :link ct)))
  6115. (if a (goto-char (nth 2 a)))
  6116. (if (re-search-forward org-any-link-re nil t)
  6117. (progn
  6118. (goto-char (match-beginning 0))
  6119. (if (org-invisible-p) (org-show-context)))
  6120. (goto-char pos)
  6121. (setq org-link-search-failed t)
  6122. (error "No further link found"))))
  6123. (defun org-previous-link ()
  6124. "Move backward to the previous link.
  6125. If the link is in hidden text, expose it."
  6126. (interactive)
  6127. (when (and org-link-search-failed (eq this-command last-command))
  6128. (goto-char (point-max))
  6129. (message "Link search wrapped back to end of buffer"))
  6130. (setq org-link-search-failed nil)
  6131. (let* ((pos (point))
  6132. (ct (org-context))
  6133. (a (assoc :link ct)))
  6134. (if a (goto-char (nth 1 a)))
  6135. (if (re-search-backward org-any-link-re nil t)
  6136. (progn
  6137. (goto-char (match-beginning 0))
  6138. (if (org-invisible-p) (org-show-context)))
  6139. (goto-char pos)
  6140. (setq org-link-search-failed t)
  6141. (error "No further link found"))))
  6142. (defun org-find-file-at-mouse (ev)
  6143. "Open file link or URL at mouse."
  6144. (interactive "e")
  6145. (mouse-set-point ev)
  6146. (org-open-at-point 'in-emacs))
  6147. (defun org-open-at-mouse (ev)
  6148. "Open file link or URL at mouse."
  6149. (interactive "e")
  6150. (mouse-set-point ev)
  6151. (org-open-at-point))
  6152. (defvar org-window-config-before-follow-link nil
  6153. "The window configuration before following a link.
  6154. This is saved in case the need arises to restore it.")
  6155. (defvar org-open-link-marker (make-marker)
  6156. "Marker pointing to the location where `org-open-at-point; was called.")
  6157. ;;;###autoload
  6158. (defun org-open-at-point-global ()
  6159. "Follow a link like Org-mode does.
  6160. This command can be called in any mode to follow a link that has
  6161. Org-mode syntax."
  6162. (interactive)
  6163. (org-run-like-in-org-mode 'org-open-at-point))
  6164. ;;;###autoload
  6165. (defun org-open-link-from-string (s &optional arg)
  6166. "Open a link in the string S, as if it was in Org-mode."
  6167. (interactive "sLink: \nP")
  6168. (with-temp-buffer
  6169. (let ((org-inhibit-startup t))
  6170. (org-mode)
  6171. (insert s)
  6172. (goto-char (point-min))
  6173. (org-open-at-point arg))))
  6174. (defun org-open-at-point (&optional in-emacs)
  6175. "Open link at or after point.
  6176. If there is no link at point, this function will search forward up to
  6177. the end of the current subtree.
  6178. Normally, files will be opened by an appropriate application. If the
  6179. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6180. (interactive "P")
  6181. (org-load-modules-maybe)
  6182. (move-marker org-open-link-marker (point))
  6183. (setq org-window-config-before-follow-link (current-window-configuration))
  6184. (org-remove-occur-highlights nil nil t)
  6185. (if (org-at-timestamp-p t)
  6186. (org-follow-timestamp-link)
  6187. (let (type path link line search (pos (point)))
  6188. (catch 'match
  6189. (save-excursion
  6190. (skip-chars-forward "^]\n\r")
  6191. (when (org-in-regexp org-bracket-link-regexp)
  6192. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6193. (while (string-match " *\n *" link)
  6194. (setq link (replace-match " " t t link)))
  6195. (setq link (org-link-expand-abbrev link))
  6196. (if (string-match org-link-re-with-space2 link)
  6197. (setq type (match-string 1 link) path (match-string 2 link))
  6198. (setq type "thisfile" path link))
  6199. (throw 'match t)))
  6200. (when (get-text-property (point) 'org-linked-text)
  6201. (setq type "thisfile"
  6202. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6203. (1+ (point)) (point))
  6204. path (buffer-substring
  6205. (previous-single-property-change pos 'org-linked-text)
  6206. (next-single-property-change pos 'org-linked-text)))
  6207. (throw 'match t))
  6208. (save-excursion
  6209. (when (or (org-in-regexp org-angle-link-re)
  6210. (org-in-regexp org-plain-link-re))
  6211. (setq type (match-string 1) path (match-string 2))
  6212. (throw 'match t)))
  6213. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6214. (setq type "tree-match"
  6215. path (match-string 1))
  6216. (throw 'match t))
  6217. (save-excursion
  6218. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6219. (setq type "tags"
  6220. path (match-string 1))
  6221. (while (string-match ":" path)
  6222. (setq path (replace-match "+" t t path)))
  6223. (throw 'match t))))
  6224. (unless path
  6225. (error "No link found"))
  6226. ;; Remove any trailing spaces in path
  6227. (if (string-match " +\\'" path)
  6228. (setq path (replace-match "" t t path)))
  6229. (cond
  6230. ((assoc type org-link-protocols)
  6231. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6232. ((equal type "mailto")
  6233. (let ((cmd (car org-link-mailto-program))
  6234. (args (cdr org-link-mailto-program)) args1
  6235. (address path) (subject "") a)
  6236. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6237. (setq address (match-string 1 path)
  6238. subject (org-link-escape (match-string 2 path))))
  6239. (while args
  6240. (cond
  6241. ((not (stringp (car args))) (push (pop args) args1))
  6242. (t (setq a (pop args))
  6243. (if (string-match "%a" a)
  6244. (setq a (replace-match address t t a)))
  6245. (if (string-match "%s" a)
  6246. (setq a (replace-match subject t t a)))
  6247. (push a args1))))
  6248. (apply cmd (nreverse args1))))
  6249. ((member type '("http" "https" "ftp" "news"))
  6250. (browse-url (concat type ":" (org-link-escape
  6251. path org-link-escape-chars-browser))))
  6252. ((member type '("message"))
  6253. (browse-url (concat type ":" path)))
  6254. ((string= type "tags")
  6255. (org-tags-view in-emacs path))
  6256. ((string= type "thisfile")
  6257. (if in-emacs
  6258. (switch-to-buffer-other-window
  6259. (org-get-buffer-for-internal-link (current-buffer)))
  6260. (org-mark-ring-push))
  6261. (let ((cmd `(org-link-search
  6262. ,path
  6263. ,(cond ((equal in-emacs '(4)) 'occur)
  6264. ((equal in-emacs '(16)) 'org-occur)
  6265. (t nil))
  6266. ,pos)))
  6267. (condition-case nil (eval cmd)
  6268. (error (progn (widen) (eval cmd))))))
  6269. ((string= type "tree-match")
  6270. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6271. ((string= type "file")
  6272. (if (string-match "::\\([0-9]+\\)\\'" path)
  6273. (setq line (string-to-number (match-string 1 path))
  6274. path (substring path 0 (match-beginning 0)))
  6275. (if (string-match "::\\(.+\\)\\'" path)
  6276. (setq search (match-string 1 path)
  6277. path (substring path 0 (match-beginning 0)))))
  6278. (if (string-match "[*?{]" (file-name-nondirectory path))
  6279. (dired path)
  6280. (org-open-file path in-emacs line search)))
  6281. ((string= type "news")
  6282. (require 'org-gnus)
  6283. (org-gnus-follow-link path))
  6284. ((string= type "shell")
  6285. (let ((cmd path))
  6286. (if (or (not org-confirm-shell-link-function)
  6287. (funcall org-confirm-shell-link-function
  6288. (format "Execute \"%s\" in shell? "
  6289. (org-add-props cmd nil
  6290. 'face 'org-warning))))
  6291. (progn
  6292. (message "Executing %s" cmd)
  6293. (shell-command cmd))
  6294. (error "Abort"))))
  6295. ((string= type "elisp")
  6296. (let ((cmd path))
  6297. (if (or (not org-confirm-elisp-link-function)
  6298. (funcall org-confirm-elisp-link-function
  6299. (format "Execute \"%s\" as elisp? "
  6300. (org-add-props cmd nil
  6301. 'face 'org-warning))))
  6302. (message "%s => %s" cmd (eval (read cmd)))
  6303. (error "Abort"))))
  6304. (t
  6305. (browse-url-at-point)))))
  6306. (move-marker org-open-link-marker nil)
  6307. (run-hook-with-args 'org-follow-link-hook))
  6308. ;;;; Time estimates
  6309. (defun org-get-effort (&optional pom)
  6310. "Get the effort estimate for the current entry."
  6311. (org-entry-get pom org-effort-property))
  6312. ;;; File search
  6313. (defvar org-create-file-search-functions nil
  6314. "List of functions to construct the right search string for a file link.
  6315. These functions are called in turn with point at the location to
  6316. which the link should point.
  6317. A function in the hook should first test if it would like to
  6318. handle this file type, for example by checking the major-mode or
  6319. the file extension. If it decides not to handle this file, it
  6320. should just return nil to give other functions a chance. If it
  6321. does handle the file, it must return the search string to be used
  6322. when following the link. The search string will be part of the
  6323. file link, given after a double colon, and `org-open-at-point'
  6324. will automatically search for it. If special measures must be
  6325. taken to make the search successful, another function should be
  6326. added to the companion hook `org-execute-file-search-functions',
  6327. which see.
  6328. A function in this hook may also use `setq' to set the variable
  6329. `description' to provide a suggestion for the descriptive text to
  6330. be used for this link when it gets inserted into an Org-mode
  6331. buffer with \\[org-insert-link].")
  6332. (defvar org-execute-file-search-functions nil
  6333. "List of functions to execute a file search triggered by a link.
  6334. Functions added to this hook must accept a single argument, the
  6335. search string that was part of the file link, the part after the
  6336. double colon. The function must first check if it would like to
  6337. handle this search, for example by checking the major-mode or the
  6338. file extension. If it decides not to handle this search, it
  6339. should just return nil to give other functions a chance. If it
  6340. does handle the search, it must return a non-nil value to keep
  6341. other functions from trying.
  6342. Each function can access the current prefix argument through the
  6343. variable `current-prefix-argument'. Note that a single prefix is
  6344. used to force opening a link in Emacs, so it may be good to only
  6345. use a numeric or double prefix to guide the search function.
  6346. In case this is needed, a function in this hook can also restore
  6347. the window configuration before `org-open-at-point' was called using:
  6348. (set-window-configuration org-window-config-before-follow-link)")
  6349. (defun org-link-search (s &optional type avoid-pos)
  6350. "Search for a link search option.
  6351. If S is surrounded by forward slashes, it is interpreted as a
  6352. regular expression. In org-mode files, this will create an `org-occur'
  6353. sparse tree. In ordinary files, `occur' will be used to list matches.
  6354. If the current buffer is in `dired-mode', grep will be used to search
  6355. in all files. If AVOID-POS is given, ignore matches near that position."
  6356. (let ((case-fold-search t)
  6357. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6358. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6359. (append '(("") (" ") ("\t") ("\n"))
  6360. org-emphasis-alist)
  6361. "\\|") "\\)"))
  6362. (pos (point))
  6363. (pre nil) (post nil)
  6364. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6365. (cond
  6366. ;; First check if there are any special
  6367. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6368. ;; Now try the builtin stuff
  6369. ((save-excursion
  6370. (goto-char (point-min))
  6371. (and
  6372. (re-search-forward
  6373. (concat "<<" (regexp-quote s0) ">>") nil t)
  6374. (setq type 'dedicated
  6375. pos (match-beginning 0))))
  6376. ;; There is an exact target for this
  6377. (goto-char pos))
  6378. ((string-match "^/\\(.*\\)/$" s)
  6379. ;; A regular expression
  6380. (cond
  6381. ((org-mode-p)
  6382. (org-occur (match-string 1 s)))
  6383. ;;((eq major-mode 'dired-mode)
  6384. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6385. (t (org-do-occur (match-string 1 s)))))
  6386. (t
  6387. ;; A normal search strings
  6388. (when (equal (string-to-char s) ?*)
  6389. ;; Anchor on headlines, post may include tags.
  6390. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6391. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6392. s (substring s 1)))
  6393. (remove-text-properties
  6394. 0 (length s)
  6395. '(face nil mouse-face nil keymap nil fontified nil) s)
  6396. ;; Make a series of regular expressions to find a match
  6397. (setq words (org-split-string s "[ \n\r\t]+")
  6398. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6399. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6400. "\\)" markers)
  6401. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6402. re2a (concat "[ \t\r\n]" re2a_)
  6403. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6404. re4 (concat "[^a-zA-Z_]" re4_)
  6405. re1 (concat pre re2 post)
  6406. re3 (concat pre (if pre re4_ re4) post)
  6407. re5 (concat pre ".*" re4)
  6408. re2 (concat pre re2)
  6409. re2a (concat pre (if pre re2a_ re2a))
  6410. re4 (concat pre (if pre re4_ re4))
  6411. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6412. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6413. re5 "\\)"
  6414. ))
  6415. (cond
  6416. ((eq type 'org-occur) (org-occur reall))
  6417. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6418. (t (goto-char (point-min))
  6419. (setq type 'fuzzy)
  6420. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6421. (org-search-not-self 1 re1 nil t)
  6422. (org-search-not-self 1 re2 nil t)
  6423. (org-search-not-self 1 re2a nil t)
  6424. (org-search-not-self 1 re3 nil t)
  6425. (org-search-not-self 1 re4 nil t)
  6426. (org-search-not-self 1 re5 nil t)
  6427. )
  6428. (goto-char (match-beginning 1))
  6429. (goto-char pos)
  6430. (error "No match")))))
  6431. (t
  6432. ;; Normal string-search
  6433. (goto-char (point-min))
  6434. (if (search-forward s nil t)
  6435. (goto-char (match-beginning 0))
  6436. (error "No match"))))
  6437. (and (org-mode-p) (org-show-context 'link-search))
  6438. type))
  6439. (defun org-search-not-self (group &rest args)
  6440. "Execute `re-search-forward', but only accept matches that do not
  6441. enclose the position of `org-open-link-marker'."
  6442. (let ((m org-open-link-marker))
  6443. (catch 'exit
  6444. (while (apply 're-search-forward args)
  6445. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6446. (goto-char (match-end group))
  6447. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6448. (> (match-beginning 0) (marker-position m))
  6449. (< (match-end 0) (marker-position m)))
  6450. (save-match-data
  6451. (or (not (org-in-regexp
  6452. org-bracket-link-analytic-regexp 1))
  6453. (not (match-end 4)) ; no description
  6454. (and (<= (match-beginning 4) (point))
  6455. (>= (match-end 4) (point))))))
  6456. (throw 'exit (point))))))))
  6457. (defun org-get-buffer-for-internal-link (buffer)
  6458. "Return a buffer to be used for displaying the link target of internal links."
  6459. (cond
  6460. ((not org-display-internal-link-with-indirect-buffer)
  6461. buffer)
  6462. ((string-match "(Clone)$" (buffer-name buffer))
  6463. (message "Buffer is already a clone, not making another one")
  6464. ;; we also do not modify visibility in this case
  6465. buffer)
  6466. (t ; make a new indirect buffer for displaying the link
  6467. (let* ((bn (buffer-name buffer))
  6468. (ibn (concat bn "(Clone)"))
  6469. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6470. (with-current-buffer ib (org-overview))
  6471. ib))))
  6472. (defun org-do-occur (regexp &optional cleanup)
  6473. "Call the Emacs command `occur'.
  6474. If CLEANUP is non-nil, remove the printout of the regular expression
  6475. in the *Occur* buffer. This is useful if the regex is long and not useful
  6476. to read."
  6477. (occur regexp)
  6478. (when cleanup
  6479. (let ((cwin (selected-window)) win beg end)
  6480. (when (setq win (get-buffer-window "*Occur*"))
  6481. (select-window win))
  6482. (goto-char (point-min))
  6483. (when (re-search-forward "match[a-z]+" nil t)
  6484. (setq beg (match-end 0))
  6485. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6486. (setq end (1- (match-beginning 0)))))
  6487. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6488. (goto-char (point-min))
  6489. (select-window cwin))))
  6490. ;;; The mark ring for links jumps
  6491. (defvar org-mark-ring nil
  6492. "Mark ring for positions before jumps in Org-mode.")
  6493. (defvar org-mark-ring-last-goto nil
  6494. "Last position in the mark ring used to go back.")
  6495. ;; Fill and close the ring
  6496. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6497. (loop for i from 1 to org-mark-ring-length do
  6498. (push (make-marker) org-mark-ring))
  6499. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6500. org-mark-ring)
  6501. (defun org-mark-ring-push (&optional pos buffer)
  6502. "Put the current position or POS into the mark ring and rotate it."
  6503. (interactive)
  6504. (setq pos (or pos (point)))
  6505. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6506. (move-marker (car org-mark-ring)
  6507. (or pos (point))
  6508. (or buffer (current-buffer)))
  6509. (message "%s"
  6510. (substitute-command-keys
  6511. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6512. (defun org-mark-ring-goto (&optional n)
  6513. "Jump to the previous position in the mark ring.
  6514. With prefix arg N, jump back that many stored positions. When
  6515. called several times in succession, walk through the entire ring.
  6516. Org-mode commands jumping to a different position in the current file,
  6517. or to another Org-mode file, automatically push the old position
  6518. onto the ring."
  6519. (interactive "p")
  6520. (let (p m)
  6521. (if (eq last-command this-command)
  6522. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6523. (setq p org-mark-ring))
  6524. (setq org-mark-ring-last-goto p)
  6525. (setq m (car p))
  6526. (switch-to-buffer (marker-buffer m))
  6527. (goto-char m)
  6528. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6529. (defun org-remove-angle-brackets (s)
  6530. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6531. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6532. s)
  6533. (defun org-add-angle-brackets (s)
  6534. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6535. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6536. s)
  6537. (defun org-remove-double-quotes (s)
  6538. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6539. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6540. s)
  6541. ;;; Following specific links
  6542. (defun org-follow-timestamp-link ()
  6543. (cond
  6544. ((org-at-date-range-p t)
  6545. (let ((org-agenda-start-on-weekday)
  6546. (t1 (match-string 1))
  6547. (t2 (match-string 2)))
  6548. (setq t1 (time-to-days (org-time-string-to-time t1))
  6549. t2 (time-to-days (org-time-string-to-time t2)))
  6550. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6551. ((org-at-timestamp-p t)
  6552. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6553. (substring (match-string 1) 0 10)))
  6554. 1))
  6555. (t (error "This should not happen"))))
  6556. ;;; Following file links
  6557. (defvar org-wait nil)
  6558. (defun org-open-file (path &optional in-emacs line search)
  6559. "Open the file at PATH.
  6560. First, this expands any special file name abbreviations. Then the
  6561. configuration variable `org-file-apps' is checked if it contains an
  6562. entry for this file type, and if yes, the corresponding command is launched.
  6563. If no application is found, Emacs simply visits the file.
  6564. With optional argument IN-EMACS, Emacs will visit the file.
  6565. Optional LINE specifies a line to go to, optional SEARCH a string to
  6566. search for. If LINE or SEARCH is given, the file will always be
  6567. opened in Emacs.
  6568. If the file does not exist, an error is thrown."
  6569. (setq in-emacs (or in-emacs line search))
  6570. (let* ((file (if (equal path "")
  6571. buffer-file-name
  6572. (substitute-in-file-name (expand-file-name path))))
  6573. (apps (append org-file-apps (org-default-apps)))
  6574. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6575. (dirp (if remp nil (file-directory-p file)))
  6576. (dfile (downcase file))
  6577. (old-buffer (current-buffer))
  6578. (old-pos (point))
  6579. (old-mode major-mode)
  6580. ext cmd)
  6581. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6582. (setq ext (match-string 1 dfile))
  6583. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6584. (setq ext (match-string 1 dfile))))
  6585. (if in-emacs
  6586. (setq cmd 'emacs)
  6587. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6588. (and dirp (cdr (assoc 'directory apps)))
  6589. (cdr (assoc ext apps))
  6590. (cdr (assoc t apps)))))
  6591. (when (eq cmd 'mailcap)
  6592. (require 'mailcap)
  6593. (mailcap-parse-mailcaps)
  6594. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6595. (command (mailcap-mime-info mime-type)))
  6596. (if (stringp command)
  6597. (setq cmd command)
  6598. (setq cmd 'emacs))))
  6599. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6600. (not (file-exists-p file))
  6601. (not org-open-non-existing-files))
  6602. (error "No such file: %s" file))
  6603. (cond
  6604. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6605. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6606. (while (string-match "['\"]%s['\"]" cmd)
  6607. (setq cmd (replace-match "%s" t t cmd)))
  6608. (while (string-match "%s" cmd)
  6609. (setq cmd (replace-match
  6610. (save-match-data (shell-quote-argument file))
  6611. t t cmd)))
  6612. (save-window-excursion
  6613. (start-process-shell-command cmd nil cmd)
  6614. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6615. ))
  6616. ((or (stringp cmd)
  6617. (eq cmd 'emacs))
  6618. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6619. (widen)
  6620. (if line (goto-line line)
  6621. (if search (org-link-search search))))
  6622. ((consp cmd)
  6623. (eval cmd))
  6624. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6625. (and (org-mode-p) (eq old-mode 'org-mode)
  6626. (or (not (equal old-buffer (current-buffer)))
  6627. (not (equal old-pos (point))))
  6628. (org-mark-ring-push old-pos old-buffer))))
  6629. (defun org-default-apps ()
  6630. "Return the default applications for this operating system."
  6631. (cond
  6632. ((eq system-type 'darwin)
  6633. org-file-apps-defaults-macosx)
  6634. ((eq system-type 'windows-nt)
  6635. org-file-apps-defaults-windowsnt)
  6636. (t org-file-apps-defaults-gnu)))
  6637. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6638. (defun org-file-remote-p (file)
  6639. "Test whether FILE specifies a location on a remote system.
  6640. Return non-nil if the location is indeed remote.
  6641. For example, the filename \"/user@host:/foo\" specifies a location
  6642. on the system \"/user@host:\"."
  6643. (cond ((fboundp 'file-remote-p)
  6644. (file-remote-p file))
  6645. ((fboundp 'tramp-handle-file-remote-p)
  6646. (tramp-handle-file-remote-p file))
  6647. ((and (boundp 'ange-ftp-name-format)
  6648. (string-match (car ange-ftp-name-format) file))
  6649. t)
  6650. (t nil)))
  6651. ;;;; Refiling
  6652. (defun org-get-org-file ()
  6653. "Read a filename, with default directory `org-directory'."
  6654. (let ((default (or org-default-notes-file remember-data-file)))
  6655. (read-file-name (format "File name [%s]: " default)
  6656. (file-name-as-directory org-directory)
  6657. default)))
  6658. (defun org-notes-order-reversed-p ()
  6659. "Check if the current file should receive notes in reversed order."
  6660. (cond
  6661. ((not org-reverse-note-order) nil)
  6662. ((eq t org-reverse-note-order) t)
  6663. ((not (listp org-reverse-note-order)) nil)
  6664. (t (catch 'exit
  6665. (let ((all org-reverse-note-order)
  6666. entry)
  6667. (while (setq entry (pop all))
  6668. (if (string-match (car entry) buffer-file-name)
  6669. (throw 'exit (cdr entry))))
  6670. nil)))))
  6671. (defvar org-refile-target-table nil
  6672. "The list of refile targets, created by `org-refile'.")
  6673. (defvar org-agenda-new-buffers nil
  6674. "Buffers created to visit agenda files.")
  6675. (defun org-get-refile-targets (&optional default-buffer)
  6676. "Produce a table with refile targets."
  6677. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6678. targets txt re files f desc descre)
  6679. (with-current-buffer (or default-buffer (current-buffer))
  6680. (while (setq entry (pop entries))
  6681. (setq files (car entry) desc (cdr entry))
  6682. (cond
  6683. ((null files) (setq files (list (current-buffer))))
  6684. ((eq files 'org-agenda-files)
  6685. (setq files (org-agenda-files 'unrestricted)))
  6686. ((and (symbolp files) (fboundp files))
  6687. (setq files (funcall files)))
  6688. ((and (symbolp files) (boundp files))
  6689. (setq files (symbol-value files))))
  6690. (if (stringp files) (setq files (list files)))
  6691. (cond
  6692. ((eq (car desc) :tag)
  6693. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6694. ((eq (car desc) :todo)
  6695. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6696. ((eq (car desc) :regexp)
  6697. (setq descre (cdr desc)))
  6698. ((eq (car desc) :level)
  6699. (setq descre (concat "^\\*\\{" (number-to-string
  6700. (if org-odd-levels-only
  6701. (1- (* 2 (cdr desc)))
  6702. (cdr desc)))
  6703. "\\}[ \t]")))
  6704. ((eq (car desc) :maxlevel)
  6705. (setq descre (concat "^\\*\\{1," (number-to-string
  6706. (if org-odd-levels-only
  6707. (1- (* 2 (cdr desc)))
  6708. (cdr desc)))
  6709. "\\}[ \t]")))
  6710. (t (error "Bad refiling target description %s" desc)))
  6711. (while (setq f (pop files))
  6712. (save-excursion
  6713. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6714. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6715. (save-excursion
  6716. (save-restriction
  6717. (widen)
  6718. (goto-char (point-min))
  6719. (while (re-search-forward descre nil t)
  6720. (goto-char (point-at-bol))
  6721. (when (looking-at org-complex-heading-regexp)
  6722. (setq txt (match-string 4)
  6723. re (concat "^" (regexp-quote
  6724. (buffer-substring (match-beginning 1)
  6725. (match-end 4)))))
  6726. (if (match-end 5) (setq re (concat re "[ \t]+"
  6727. (regexp-quote
  6728. (match-string 5)))))
  6729. (setq re (concat re "[ \t]*$"))
  6730. (when org-refile-use-outline-path
  6731. (setq txt (mapconcat 'identity
  6732. (append
  6733. (if (eq org-refile-use-outline-path 'file)
  6734. (list (file-name-nondirectory
  6735. (buffer-file-name (buffer-base-buffer))))
  6736. (if (eq org-refile-use-outline-path 'full-file-path)
  6737. (list (buffer-file-name (buffer-base-buffer)))))
  6738. (org-get-outline-path)
  6739. (list txt))
  6740. "/")))
  6741. (push (list txt f re (point)) targets))
  6742. (goto-char (point-at-eol))))))))
  6743. (nreverse targets))))
  6744. (defun org-get-outline-path ()
  6745. "Return the outline path to the current entry, as a list."
  6746. (let (rtn)
  6747. (save-excursion
  6748. (while (org-up-heading-safe)
  6749. (when (looking-at org-complex-heading-regexp)
  6750. (push (org-match-string-no-properties 4) rtn)))
  6751. rtn)))
  6752. (defvar org-refile-history nil
  6753. "History for refiling operations.")
  6754. (defun org-refile (&optional goto default-buffer)
  6755. "Move the entry at point to another heading.
  6756. The list of target headings is compiled using the information in
  6757. `org-refile-targets', which see. This list is created before each use
  6758. and will therefore always be up-to-date.
  6759. At the target location, the entry is filed as a subitem of the target heading.
  6760. Depending on `org-reverse-note-order', the new subitem will either be the
  6761. first of the last subitem.
  6762. With prefix arg GOTO, the command will only visit the target location,
  6763. not actually move anything.
  6764. With a double prefix `C-c C-c', go to the location where the last refiling
  6765. operation has put the subtree."
  6766. (interactive "P")
  6767. (let* ((cbuf (current-buffer))
  6768. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6769. pos it nbuf file re level reversed)
  6770. (if (equal goto '(16))
  6771. (org-refile-goto-last-stored)
  6772. (when (setq it (org-refile-get-location
  6773. (if goto "Goto: " "Refile to: ") default-buffer))
  6774. (setq file (nth 1 it)
  6775. re (nth 2 it)
  6776. pos (nth 3 it))
  6777. (setq nbuf (or (find-buffer-visiting file)
  6778. (find-file-noselect file)))
  6779. (if goto
  6780. (progn
  6781. (switch-to-buffer nbuf)
  6782. (goto-char pos)
  6783. (org-show-context 'org-goto))
  6784. (org-copy-subtree 1 nil t)
  6785. (save-excursion
  6786. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6787. (find-file-noselect file))))
  6788. (setq reversed (org-notes-order-reversed-p))
  6789. (save-excursion
  6790. (save-restriction
  6791. (widen)
  6792. (goto-char pos)
  6793. (looking-at outline-regexp)
  6794. (setq level (org-get-valid-level (funcall outline-level) 1))
  6795. (goto-char
  6796. (if reversed
  6797. (outline-next-heading)
  6798. (or (save-excursion (outline-get-next-sibling))
  6799. (org-end-of-subtree t t)
  6800. (point-max))))
  6801. (bookmark-set "org-refile-last-stored")
  6802. (org-paste-subtree level))))
  6803. (org-cut-subtree)
  6804. (setq org-markers-to-move nil)
  6805. (message "Entry refiled to \"%s\"" (car it)))))))
  6806. (defun org-refile-goto-last-stored ()
  6807. "Go to the location where the last refile was stored."
  6808. (interactive)
  6809. (bookmark-jump "org-refile-last-stored")
  6810. (message "This is the location of the last refile"))
  6811. (defun org-refile-get-location (&optional prompt default-buffer)
  6812. "Prompt the user for a refile location, using PROMPT."
  6813. (let ((org-refile-targets org-refile-targets)
  6814. (org-refile-use-outline-path org-refile-use-outline-path))
  6815. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6816. (unless org-refile-target-table
  6817. (error "No refile targets"))
  6818. (let* ((cbuf (current-buffer))
  6819. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6820. (fname (and filename (file-truename filename)))
  6821. (tbl (mapcar
  6822. (lambda (x)
  6823. (if (not (equal fname (file-truename (nth 1 x))))
  6824. (cons (concat (car x) " (" (file-name-nondirectory
  6825. (nth 1 x)) ")")
  6826. (cdr x))
  6827. x))
  6828. org-refile-target-table))
  6829. (completion-ignore-case t))
  6830. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  6831. tbl)))
  6832. ;;;; Dynamic blocks
  6833. (defun org-find-dblock (name)
  6834. "Find the first dynamic block with name NAME in the buffer.
  6835. If not found, stay at current position and return nil."
  6836. (let (pos)
  6837. (save-excursion
  6838. (goto-char (point-min))
  6839. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6840. nil t)
  6841. (match-beginning 0))))
  6842. (if pos (goto-char pos))
  6843. pos))
  6844. (defconst org-dblock-start-re
  6845. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6846. "Matches the startline of a dynamic block, with parameters.")
  6847. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6848. "Matches the end of a dyhamic block.")
  6849. (defun org-create-dblock (plist)
  6850. "Create a dynamic block section, with parameters taken from PLIST.
  6851. PLIST must containe a :name entry which is used as name of the block."
  6852. (unless (bolp) (newline))
  6853. (let ((name (plist-get plist :name)))
  6854. (insert "#+BEGIN: " name)
  6855. (while plist
  6856. (if (eq (car plist) :name)
  6857. (setq plist (cddr plist))
  6858. (insert " " (prin1-to-string (pop plist)))))
  6859. (insert "\n\n#+END:\n")
  6860. (beginning-of-line -2)))
  6861. (defun org-prepare-dblock ()
  6862. "Prepare dynamic block for refresh.
  6863. This empties the block, puts the cursor at the insert position and returns
  6864. the property list including an extra property :name with the block name."
  6865. (unless (looking-at org-dblock-start-re)
  6866. (error "Not at a dynamic block"))
  6867. (let* ((begdel (1+ (match-end 0)))
  6868. (name (org-no-properties (match-string 1)))
  6869. (params (append (list :name name)
  6870. (read (concat "(" (match-string 3) ")")))))
  6871. (unless (re-search-forward org-dblock-end-re nil t)
  6872. (error "Dynamic block not terminated"))
  6873. (setq params
  6874. (append params
  6875. (list :content (buffer-substring
  6876. begdel (match-beginning 0)))))
  6877. (delete-region begdel (match-beginning 0))
  6878. (goto-char begdel)
  6879. (open-line 1)
  6880. params))
  6881. (defun org-map-dblocks (&optional command)
  6882. "Apply COMMAND to all dynamic blocks in the current buffer.
  6883. If COMMAND is not given, use `org-update-dblock'."
  6884. (let ((cmd (or command 'org-update-dblock))
  6885. pos)
  6886. (save-excursion
  6887. (goto-char (point-min))
  6888. (while (re-search-forward org-dblock-start-re nil t)
  6889. (goto-char (setq pos (match-beginning 0)))
  6890. (condition-case nil
  6891. (funcall cmd)
  6892. (error (message "Error during update of dynamic block")))
  6893. (goto-char pos)
  6894. (unless (re-search-forward org-dblock-end-re nil t)
  6895. (error "Dynamic block not terminated"))))))
  6896. (defun org-dblock-update (&optional arg)
  6897. "User command for updating dynamic blocks.
  6898. Update the dynamic block at point. With prefix ARG, update all dynamic
  6899. blocks in the buffer."
  6900. (interactive "P")
  6901. (if arg
  6902. (org-update-all-dblocks)
  6903. (or (looking-at org-dblock-start-re)
  6904. (org-beginning-of-dblock))
  6905. (org-update-dblock)))
  6906. (defun org-update-dblock ()
  6907. "Update the dynamic block at point
  6908. This means to empty the block, parse for parameters and then call
  6909. the correct writing function."
  6910. (save-window-excursion
  6911. (let* ((pos (point))
  6912. (line (org-current-line))
  6913. (params (org-prepare-dblock))
  6914. (name (plist-get params :name))
  6915. (cmd (intern (concat "org-dblock-write:" name))))
  6916. (message "Updating dynamic block `%s' at line %d..." name line)
  6917. (funcall cmd params)
  6918. (message "Updating dynamic block `%s' at line %d...done" name line)
  6919. (goto-char pos))))
  6920. (defun org-beginning-of-dblock ()
  6921. "Find the beginning of the dynamic block at point.
  6922. Error if there is no scuh block at point."
  6923. (let ((pos (point))
  6924. beg)
  6925. (end-of-line 1)
  6926. (if (and (re-search-backward org-dblock-start-re nil t)
  6927. (setq beg (match-beginning 0))
  6928. (re-search-forward org-dblock-end-re nil t)
  6929. (> (match-end 0) pos))
  6930. (goto-char beg)
  6931. (goto-char pos)
  6932. (error "Not in a dynamic block"))))
  6933. (defun org-update-all-dblocks ()
  6934. "Update all dynamic blocks in the buffer.
  6935. This function can be used in a hook."
  6936. (when (org-mode-p)
  6937. (org-map-dblocks 'org-update-dblock)))
  6938. ;;;; Completion
  6939. (defconst org-additional-option-like-keywords
  6940. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6941. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6942. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  6943. (defcustom org-structure-template-alist
  6944. '(
  6945. ("s" "#+begin_src ?\n\n#+end_src")
  6946. ("e" "#+begin_example\n?\n#+end_example")
  6947. ("q" "#+begin_quote\n?\n#+end_quote")
  6948. ("v" "#+begin_verse\n?\n#+end_verse")
  6949. ("l" "#+begin_latex\n?\n#+end_latex")
  6950. ("L" "#+latex: ")
  6951. ("h" "#+begin_html\n?\n#+end_html")
  6952. ("H" "#+html: ")
  6953. ("a" "#+begin_ascii\n?\n#+end_ascii")
  6954. ("A" "#+ascii: ")
  6955. ("i" "#+include: %file ?")
  6956. )
  6957. "Structure completion elements.
  6958. This is a list of abbreviation keys and values. The value gets inserted
  6959. it you type @samp{.} followed by the key and then the completion key,
  6960. usually `M-TAB'. %file will be replaced by a file name after prompting
  6961. for the file uning completion.
  6962. This is an experimental feature, it is undecided if it is going to stay in."
  6963. :group 'org-completion
  6964. :type '(repeat
  6965. (string :tag "Key") (string :tag "Template")))
  6966. (defun org-complete-expand-structure-template (start cell)
  6967. "Expand a structure template."
  6968. (let ((rpl (nth 1 cell)))
  6969. (delete-region start (point))
  6970. (when (string-match "\\`#\\+" rpl)
  6971. (cond
  6972. ((bolp))
  6973. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  6974. (delete-region (point-at-bol) (point)))
  6975. (t (newline))))
  6976. (setq start (point))
  6977. (if (string-match "%file" rpl)
  6978. (setq rpl (replace-match
  6979. (concat
  6980. "\""
  6981. (save-match-data
  6982. (abbreviate-file-name (read-file-name "Include file: ")))
  6983. "\"")
  6984. t t rpl)))
  6985. (insert rpl)
  6986. (if (re-search-backward "\\?" start t) (delete-char 1))))
  6987. (defun org-complete (&optional arg)
  6988. "Perform completion on word at point.
  6989. At the beginning of a headline, this completes TODO keywords as given in
  6990. `org-todo-keywords'.
  6991. If the current word is preceded by a backslash, completes the TeX symbols
  6992. that are supported for HTML support.
  6993. If the current word is preceded by \"#+\", completes special words for
  6994. setting file options.
  6995. In the line after \"#+STARTUP:, complete valid keywords.\"
  6996. At all other locations, this simply calls the value of
  6997. `org-completion-fallback-command'."
  6998. (interactive "P")
  6999. (org-without-partial-completion
  7000. (catch 'exit
  7001. (let* ((a nil)
  7002. (end (point))
  7003. (beg1 (save-excursion
  7004. (skip-chars-backward (org-re "[:alnum:]_@"))
  7005. (point)))
  7006. (beg (save-excursion
  7007. (skip-chars-backward "a-zA-Z0-9_:$")
  7008. (point)))
  7009. (confirm (lambda (x) (stringp (car x))))
  7010. (searchhead (equal (char-before beg) ?*))
  7011. (struct
  7012. (when (and (equal (char-before beg1) ?.)
  7013. (setq a (assoc (buffer-substring beg1 (point))
  7014. org-structure-template-alist)))
  7015. (org-complete-expand-structure-template (1- beg1) a)
  7016. (throw 'exit t)))
  7017. (tag (and (equal (char-before beg1) ?:)
  7018. (equal (char-after (point-at-bol)) ?*)))
  7019. (prop (and (equal (char-before beg1) ?:)
  7020. (not (equal (char-after (point-at-bol)) ?*))))
  7021. (texp (equal (char-before beg) ?\\))
  7022. (link (equal (char-before beg) ?\[))
  7023. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7024. beg)
  7025. "#+"))
  7026. (startup (string-match "^#\\+STARTUP:.*"
  7027. (buffer-substring (point-at-bol) (point))))
  7028. (completion-ignore-case opt)
  7029. (type nil)
  7030. (tbl nil)
  7031. (table (cond
  7032. (opt
  7033. (setq type :opt)
  7034. (require 'org-exp)
  7035. (append
  7036. (mapcar
  7037. (lambda (x)
  7038. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7039. (cons (match-string 2 x) (match-string 1 x)))
  7040. (org-split-string (org-get-current-options) "\n"))
  7041. (mapcar 'list org-additional-option-like-keywords)))
  7042. (startup
  7043. (setq type :startup)
  7044. org-startup-options)
  7045. (link (append org-link-abbrev-alist-local
  7046. org-link-abbrev-alist))
  7047. (texp
  7048. (setq type :tex)
  7049. org-html-entities)
  7050. ((string-match "\\`\\*+[ \t]+\\'"
  7051. (buffer-substring (point-at-bol) beg))
  7052. (setq type :todo)
  7053. (mapcar 'list org-todo-keywords-1))
  7054. (searchhead
  7055. (setq type :searchhead)
  7056. (save-excursion
  7057. (goto-char (point-min))
  7058. (while (re-search-forward org-todo-line-regexp nil t)
  7059. (push (list
  7060. (org-make-org-heading-search-string
  7061. (match-string 3) t))
  7062. tbl)))
  7063. tbl)
  7064. (tag (setq type :tag beg beg1)
  7065. (or org-tag-alist (org-get-buffer-tags)))
  7066. (prop (setq type :prop beg beg1)
  7067. (mapcar 'list (org-buffer-property-keys nil t t)))
  7068. (t (progn
  7069. (call-interactively org-completion-fallback-command)
  7070. (throw 'exit nil)))))
  7071. (pattern (buffer-substring-no-properties beg end))
  7072. (completion (try-completion pattern table confirm)))
  7073. (cond ((eq completion t)
  7074. (if (not (assoc (upcase pattern) table))
  7075. (message "Already complete")
  7076. (if (and (equal type :opt)
  7077. (not (member (car (assoc (upcase pattern) table))
  7078. org-additional-option-like-keywords)))
  7079. (insert (substring (cdr (assoc (upcase pattern) table))
  7080. (length pattern)))
  7081. (if (memq type '(:tag :prop)) (insert ":")))))
  7082. ((null completion)
  7083. (message "Can't find completion for \"%s\"" pattern)
  7084. (ding))
  7085. ((not (string= pattern completion))
  7086. (delete-region beg end)
  7087. (if (string-match " +$" completion)
  7088. (setq completion (replace-match "" t t completion)))
  7089. (insert completion)
  7090. (if (get-buffer-window "*Completions*")
  7091. (delete-window (get-buffer-window "*Completions*")))
  7092. (if (assoc completion table)
  7093. (if (eq type :todo) (insert " ")
  7094. (if (memq type '(:tag :prop)) (insert ":"))))
  7095. (if (and (equal type :opt) (assoc completion table))
  7096. (message "%s" (substitute-command-keys
  7097. "Press \\[org-complete] again to insert example settings"))))
  7098. (t
  7099. (message "Making completion list...")
  7100. (let ((list (sort (all-completions pattern table confirm)
  7101. 'string<)))
  7102. (with-output-to-temp-buffer "*Completions*"
  7103. (condition-case nil
  7104. ;; Protection needed for XEmacs and emacs 21
  7105. (display-completion-list list pattern)
  7106. (error (display-completion-list list)))))
  7107. (message "Making completion list...%s" "done")))))))
  7108. ;;;; TODO, DEADLINE, Comments
  7109. (defun org-toggle-comment ()
  7110. "Change the COMMENT state of an entry."
  7111. (interactive)
  7112. (save-excursion
  7113. (org-back-to-heading)
  7114. (let (case-fold-search)
  7115. (if (looking-at (concat outline-regexp
  7116. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7117. (replace-match "" t t nil 1)
  7118. (if (looking-at outline-regexp)
  7119. (progn
  7120. (goto-char (match-end 0))
  7121. (insert org-comment-string " ")))))))
  7122. (defvar org-last-todo-state-is-todo nil
  7123. "This is non-nil when the last TODO state change led to a TODO state.
  7124. If the last change removed the TODO tag or switched to DONE, then
  7125. this is nil.")
  7126. (defvar org-setting-tags nil) ; dynamically skiped
  7127. (defun org-parse-local-options (string var)
  7128. "Parse STRING for startup setting relevant for variable VAR."
  7129. (let ((rtn (symbol-value var))
  7130. e opts)
  7131. (save-match-data
  7132. (if (or (not string) (not (string-match "\\S-" string)))
  7133. rtn
  7134. (setq opts (delq nil (mapcar (lambda (x)
  7135. (setq e (assoc x org-startup-options))
  7136. (if (eq (nth 1 e) var) e nil))
  7137. (org-split-string string "[ \t]+"))))
  7138. (if (not opts)
  7139. rtn
  7140. (setq rtn nil)
  7141. (while (setq e (pop opts))
  7142. (if (not (nth 3 e))
  7143. (setq rtn (nth 2 e))
  7144. (if (not (listp rtn)) (setq rtn nil))
  7145. (push (nth 2 e) rtn)))
  7146. rtn)))))
  7147. (defvar org-blocker-hook nil
  7148. "Hook for functions that are allowed to block a state change.
  7149. Each function gets as its single argument a property list, see
  7150. `org-trigger-hook' for more information about this list.
  7151. If any of the functions in this hook returns nil, the state change
  7152. is blocked.")
  7153. (defvar org-trigger-hook nil
  7154. "Hook for functions that are triggered by a state change.
  7155. Each function gets as its single argument a property list with at least
  7156. the following elements:
  7157. (:type type-of-change :position pos-at-entry-start
  7158. :from old-state :to new-state)
  7159. Depending on the type, more properties may be present.
  7160. This mechanism is currently implemented for:
  7161. TODO state changes
  7162. ------------------
  7163. :type todo-state-change
  7164. :from previous state (keyword as a string), or nil
  7165. :to new state (keyword as a string), or nil")
  7166. (defun org-todo (&optional arg)
  7167. "Change the TODO state of an item.
  7168. The state of an item is given by a keyword at the start of the heading,
  7169. like
  7170. *** TODO Write paper
  7171. *** DONE Call mom
  7172. The different keywords are specified in the variable `org-todo-keywords'.
  7173. By default the available states are \"TODO\" and \"DONE\".
  7174. So for this example: when the item starts with TODO, it is changed to DONE.
  7175. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7176. DONE are present, add TODO at the beginning of the heading.
  7177. With C-u prefix arg, use completion to determine the new state.
  7178. With numeric prefix arg, switch to that state.
  7179. For calling through lisp, arg is also interpreted in the following way:
  7180. 'none -> empty state
  7181. \"\"(empty string) -> switch to empty state
  7182. 'done -> switch to DONE
  7183. 'nextset -> switch to the next set of keywords
  7184. 'previousset -> switch to the previous set of keywords
  7185. \"WAITING\" -> switch to the specified keyword, but only if it
  7186. really is a member of `org-todo-keywords'."
  7187. (interactive "P")
  7188. (save-excursion
  7189. (catch 'exit
  7190. (org-back-to-heading)
  7191. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7192. (or (looking-at (concat " +" org-todo-regexp " *"))
  7193. (looking-at " *"))
  7194. (let* ((match-data (match-data))
  7195. (startpos (point-at-bol))
  7196. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7197. (org-log-done org-log-done)
  7198. (org-log-repeat org-log-repeat)
  7199. (org-todo-log-states org-todo-log-states)
  7200. (this (match-string 1))
  7201. (hl-pos (match-beginning 0))
  7202. (head (org-get-todo-sequence-head this))
  7203. (ass (assoc head org-todo-kwd-alist))
  7204. (interpret (nth 1 ass))
  7205. (done-word (nth 3 ass))
  7206. (final-done-word (nth 4 ass))
  7207. (last-state (or this ""))
  7208. (completion-ignore-case t)
  7209. (member (member this org-todo-keywords-1))
  7210. (tail (cdr member))
  7211. (state (cond
  7212. ((and org-todo-key-trigger
  7213. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7214. (and (not arg) org-use-fast-todo-selection
  7215. (not (eq org-use-fast-todo-selection 'prefix)))))
  7216. ;; Use fast selection
  7217. (org-fast-todo-selection))
  7218. ((and (equal arg '(4))
  7219. (or (not org-use-fast-todo-selection)
  7220. (not org-todo-key-trigger)))
  7221. ;; Read a state with completion
  7222. (completing-read "State: " (mapcar (lambda(x) (list x))
  7223. org-todo-keywords-1)
  7224. nil t))
  7225. ((eq arg 'right)
  7226. (if this
  7227. (if tail (car tail) nil)
  7228. (car org-todo-keywords-1)))
  7229. ((eq arg 'left)
  7230. (if (equal member org-todo-keywords-1)
  7231. nil
  7232. (if this
  7233. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7234. org-todo-keywords-1)
  7235. (org-last org-todo-keywords-1))))
  7236. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7237. (setq arg nil))) ; hack to fall back to cycling
  7238. (arg
  7239. ;; user or caller requests a specific state
  7240. (cond
  7241. ((equal arg "") nil)
  7242. ((eq arg 'none) nil)
  7243. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7244. ((eq arg 'nextset)
  7245. (or (car (cdr (member head org-todo-heads)))
  7246. (car org-todo-heads)))
  7247. ((eq arg 'previousset)
  7248. (let ((org-todo-heads (reverse org-todo-heads)))
  7249. (or (car (cdr (member head org-todo-heads)))
  7250. (car org-todo-heads))))
  7251. ((car (member arg org-todo-keywords-1)))
  7252. ((nth (1- (prefix-numeric-value arg))
  7253. org-todo-keywords-1))))
  7254. ((null member) (or head (car org-todo-keywords-1)))
  7255. ((equal this final-done-word) nil) ;; -> make empty
  7256. ((null tail) nil) ;; -> first entry
  7257. ((eq interpret 'sequence)
  7258. (car tail))
  7259. ((memq interpret '(type priority))
  7260. (if (eq this-command last-command)
  7261. (car tail)
  7262. (if (> (length tail) 0)
  7263. (or done-word (car org-done-keywords))
  7264. nil)))
  7265. (t nil)))
  7266. (next (if state (concat " " state " ") " "))
  7267. (change-plist (list :type 'todo-state-change :from this :to state
  7268. :position startpos))
  7269. dolog now-done-p)
  7270. (when org-blocker-hook
  7271. (unless (save-excursion
  7272. (save-match-data
  7273. (run-hook-with-args-until-failure
  7274. 'org-blocker-hook change-plist)))
  7275. (if (interactive-p)
  7276. (error "TODO state change from %s to %s blocked" this state)
  7277. ;; fail silently
  7278. (message "TODO state change from %s to %s blocked" this state)
  7279. (throw 'exit nil))))
  7280. (store-match-data match-data)
  7281. (replace-match next t t)
  7282. (unless (pos-visible-in-window-p hl-pos)
  7283. (message "TODO state changed to %s" (org-trim next)))
  7284. (unless head
  7285. (setq head (org-get-todo-sequence-head state)
  7286. ass (assoc head org-todo-kwd-alist)
  7287. interpret (nth 1 ass)
  7288. done-word (nth 3 ass)
  7289. final-done-word (nth 4 ass)))
  7290. (when (memq arg '(nextset previousset))
  7291. (message "Keyword-Set %d/%d: %s"
  7292. (- (length org-todo-sets) -1
  7293. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7294. (length org-todo-sets)
  7295. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7296. (setq org-last-todo-state-is-todo
  7297. (not (member state org-done-keywords)))
  7298. (setq now-done-p (and (member state org-done-keywords)
  7299. (not (member this org-done-keywords))))
  7300. (and logging (org-local-logging logging))
  7301. (when (and (or org-todo-log-states org-log-done)
  7302. (not (memq arg '(nextset previousset))))
  7303. ;; we need to look at recording a time and note
  7304. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7305. (nth 2 (assoc this org-todo-log-states))))
  7306. (when (and state
  7307. (member state org-not-done-keywords)
  7308. (not (member this org-not-done-keywords)))
  7309. ;; This is now a todo state and was not one before
  7310. ;; If there was a CLOSED time stamp, get rid of it.
  7311. (org-add-planning-info nil nil 'closed))
  7312. (when (and now-done-p org-log-done)
  7313. ;; It is now done, and it was not done before
  7314. (org-add-planning-info 'closed (org-current-time))
  7315. (if (and (not dolog) (eq 'note org-log-done))
  7316. (org-add-log-setup 'done state 'findpos 'note)))
  7317. (when (and state dolog)
  7318. ;; This is a non-nil state, and we need to log it
  7319. (org-add-log-setup 'state state 'findpos dolog)))
  7320. ;; Fixup tag positioning
  7321. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7322. (run-hooks 'org-after-todo-state-change-hook)
  7323. (if (and arg (not (member state org-done-keywords)))
  7324. (setq head (org-get-todo-sequence-head state)))
  7325. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7326. ;; Do we need to trigger a repeat?
  7327. (when now-done-p (org-auto-repeat-maybe state))
  7328. ;; Fixup cursor location if close to the keyword
  7329. (if (and (outline-on-heading-p)
  7330. (not (bolp))
  7331. (save-excursion (beginning-of-line 1)
  7332. (looking-at org-todo-line-regexp))
  7333. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7334. (progn
  7335. (goto-char (or (match-end 2) (match-end 1)))
  7336. (just-one-space)))
  7337. (when org-trigger-hook
  7338. (save-excursion
  7339. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7340. (defun org-local-logging (value)
  7341. "Get logging settings from a property VALUE."
  7342. (let* (words w a)
  7343. ;; directly set the variables, they are already local.
  7344. (setq org-log-done nil
  7345. org-log-repeat nil
  7346. org-todo-log-states nil)
  7347. (setq words (org-split-string value))
  7348. (while (setq w (pop words))
  7349. (cond
  7350. ((setq a (assoc w org-startup-options))
  7351. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7352. (set (nth 1 a) (nth 2 a))))
  7353. ((setq a (org-extract-log-state-settings w))
  7354. (and (member (car a) org-todo-keywords-1)
  7355. (push a org-todo-log-states)))))))
  7356. (defun org-get-todo-sequence-head (kwd)
  7357. "Return the head of the TODO sequence to which KWD belongs.
  7358. If KWD is not set, check if there is a text property remembering the
  7359. right sequence."
  7360. (let (p)
  7361. (cond
  7362. ((not kwd)
  7363. (or (get-text-property (point-at-bol) 'org-todo-head)
  7364. (progn
  7365. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7366. nil (point-at-eol)))
  7367. (get-text-property p 'org-todo-head))))
  7368. ((not (member kwd org-todo-keywords-1))
  7369. (car org-todo-keywords-1))
  7370. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7371. (defun org-fast-todo-selection ()
  7372. "Fast TODO keyword selection with single keys.
  7373. Returns the new TODO keyword, or nil if no state change should occur."
  7374. (let* ((fulltable org-todo-key-alist)
  7375. (done-keywords org-done-keywords) ;; needed for the faces.
  7376. (maxlen (apply 'max (mapcar
  7377. (lambda (x)
  7378. (if (stringp (car x)) (string-width (car x)) 0))
  7379. fulltable)))
  7380. (expert nil)
  7381. (fwidth (+ maxlen 3 1 3))
  7382. (ncol (/ (- (window-width) 4) fwidth))
  7383. tg cnt e c tbl
  7384. groups ingroup)
  7385. (save-window-excursion
  7386. (if expert
  7387. (set-buffer (get-buffer-create " *Org todo*"))
  7388. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7389. (erase-buffer)
  7390. (org-set-local 'org-done-keywords done-keywords)
  7391. (setq tbl fulltable cnt 0)
  7392. (while (setq e (pop tbl))
  7393. (cond
  7394. ((equal e '(:startgroup))
  7395. (push '() groups) (setq ingroup t)
  7396. (when (not (= cnt 0))
  7397. (setq cnt 0)
  7398. (insert "\n"))
  7399. (insert "{ "))
  7400. ((equal e '(:endgroup))
  7401. (setq ingroup nil cnt 0)
  7402. (insert "}\n"))
  7403. (t
  7404. (setq tg (car e) c (cdr e))
  7405. (if ingroup (push tg (car groups)))
  7406. (setq tg (org-add-props tg nil 'face
  7407. (org-get-todo-face tg)))
  7408. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7409. (insert "[" c "] " tg (make-string
  7410. (- fwidth 4 (length tg)) ?\ ))
  7411. (when (= (setq cnt (1+ cnt)) ncol)
  7412. (insert "\n")
  7413. (if ingroup (insert " "))
  7414. (setq cnt 0)))))
  7415. (insert "\n")
  7416. (goto-char (point-min))
  7417. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7418. (fit-window-to-buffer))
  7419. (message "[a-z..]:Set [SPC]:clear")
  7420. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7421. (cond
  7422. ((or (= c ?\C-g)
  7423. (and (= c ?q) (not (rassoc c fulltable))))
  7424. (setq quit-flag t))
  7425. ((= c ?\ ) nil)
  7426. ((setq e (rassoc c fulltable) tg (car e))
  7427. tg)
  7428. (t (setq quit-flag t))))))
  7429. (defun org-entry-is-todo-p ()
  7430. (member (org-get-todo-state) org-not-done-keywords))
  7431. (defun org-entry-is-done-p ()
  7432. (member (org-get-todo-state) org-done-keywords))
  7433. (defun org-get-todo-state ()
  7434. (save-excursion
  7435. (org-back-to-heading t)
  7436. (and (looking-at org-todo-line-regexp)
  7437. (match-end 2)
  7438. (match-string 2))))
  7439. (defun org-at-date-range-p (&optional inactive-ok)
  7440. "Is the cursor inside a date range?"
  7441. (interactive)
  7442. (save-excursion
  7443. (catch 'exit
  7444. (let ((pos (point)))
  7445. (skip-chars-backward "^[<\r\n")
  7446. (skip-chars-backward "<[")
  7447. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7448. (>= (match-end 0) pos)
  7449. (throw 'exit t))
  7450. (skip-chars-backward "^<[\r\n")
  7451. (skip-chars-backward "<[")
  7452. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7453. (>= (match-end 0) pos)
  7454. (throw 'exit t)))
  7455. nil)))
  7456. (defun org-get-repeat ()
  7457. "Check if tere is a deadline/schedule with repeater in this entry."
  7458. (save-match-data
  7459. (save-excursion
  7460. (org-back-to-heading t)
  7461. (if (re-search-forward
  7462. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7463. (match-string 1)))))
  7464. (defvar org-last-changed-timestamp)
  7465. (defvar org-log-post-message)
  7466. (defvar org-log-note-purpose)
  7467. (defvar org-log-note-how)
  7468. (defun org-auto-repeat-maybe (done-word)
  7469. "Check if the current headline contains a repeated deadline/schedule.
  7470. If yes, set TODO state back to what it was and change the base date
  7471. of repeating deadline/scheduled time stamps to new date.
  7472. This function is run automatically after each state change to a DONE state."
  7473. ;; last-state is dynamically scoped into this function
  7474. (let* ((repeat (org-get-repeat))
  7475. (aa (assoc last-state org-todo-kwd-alist))
  7476. (interpret (nth 1 aa))
  7477. (head (nth 2 aa))
  7478. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7479. (msg "Entry repeats: ")
  7480. (org-log-done nil)
  7481. (org-todo-log-states nil)
  7482. (nshiftmax 10) (nshift 0)
  7483. re type n what ts mb0 time)
  7484. (when repeat
  7485. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7486. (org-todo (if (eq interpret 'type) last-state head))
  7487. (when org-log-repeat
  7488. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7489. (memq 'org-add-log-note post-command-hook))
  7490. ;; OK, we are already setup for some record
  7491. (if (eq org-log-repeat 'note)
  7492. ;; make sure we take a note, not only a time stamp
  7493. (setq org-log-note-how 'note))
  7494. ;; Set up for taking a record
  7495. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7496. 'findpos org-log-repeat)))
  7497. (org-back-to-heading t)
  7498. (org-add-planning-info nil nil 'closed)
  7499. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7500. org-deadline-time-regexp "\\)\\|\\("
  7501. org-ts-regexp "\\)"))
  7502. (while (re-search-forward
  7503. re (save-excursion (outline-next-heading) (point)) t)
  7504. (setq type (if (match-end 1) org-scheduled-string
  7505. (if (match-end 3) org-deadline-string "Plain:"))
  7506. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7507. mb0 (match-beginning 0))
  7508. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7509. (setq n (string-to-number (match-string 2 ts))
  7510. what (match-string 3 ts))
  7511. (if (equal what "w") (setq n (* n 7) what "d"))
  7512. ;; Preparation, see if we need to modify the start date for the change
  7513. (when (match-end 1)
  7514. (setq time (save-match-data (org-time-string-to-time ts)))
  7515. (cond
  7516. ((equal (match-string 1 ts) ".")
  7517. ;; Shift starting date to today
  7518. (org-timestamp-change
  7519. (- (time-to-days (current-time)) (time-to-days time))
  7520. 'day))
  7521. ((equal (match-string 1 ts) "+")
  7522. (while (or (= nshift 0)
  7523. (<= (time-to-days time) (time-to-days (current-time))))
  7524. (when (= (incf nshift) nshiftmax)
  7525. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7526. (error "Abort")))
  7527. (org-timestamp-change n (cdr (assoc what whata)))
  7528. (org-at-timestamp-p t)
  7529. (setq ts (match-string 1))
  7530. (setq time (save-match-data (org-time-string-to-time ts))))
  7531. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7532. ;; rematch, so that we have everything in place for the real shift
  7533. (org-at-timestamp-p t)
  7534. (setq ts (match-string 1))
  7535. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7536. (org-timestamp-change n (cdr (assoc what whata)))
  7537. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7538. (setq org-log-post-message msg)
  7539. (message "%s" msg))))
  7540. (defun org-show-todo-tree (arg)
  7541. "Make a compact tree which shows all headlines marked with TODO.
  7542. The tree will show the lines where the regexp matches, and all higher
  7543. headlines above the match.
  7544. With a \\[universal-argument] prefix, also show the DONE entries.
  7545. With a numeric prefix N, construct a sparse tree for the Nth element
  7546. of `org-todo-keywords-1'."
  7547. (interactive "P")
  7548. (let ((case-fold-search nil)
  7549. (kwd-re
  7550. (cond ((null arg) org-not-done-regexp)
  7551. ((equal arg '(4))
  7552. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7553. (mapcar 'list org-todo-keywords-1))))
  7554. (concat "\\("
  7555. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7556. "\\)\\>")))
  7557. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7558. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7559. org-todo-keywords-1)))
  7560. (t (error "Invalid prefix argument: %s" arg)))))
  7561. (message "%d TODO entries found"
  7562. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7563. (defun org-deadline (&optional remove)
  7564. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7565. With argument REMOVE, remove any deadline from the item."
  7566. (interactive "P")
  7567. (if remove
  7568. (progn
  7569. (org-remove-timestamp-with-keyword org-deadline-string)
  7570. (message "Item no longer has a deadline."))
  7571. (org-add-planning-info 'deadline nil 'closed)))
  7572. (defun org-schedule (&optional remove)
  7573. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7574. With argument REMOVE, remove any scheduling date from the item."
  7575. (interactive "P")
  7576. (if remove
  7577. (progn
  7578. (org-remove-timestamp-with-keyword org-scheduled-string)
  7579. (message "Item is no longer scheduled."))
  7580. (org-add-planning-info 'scheduled nil 'closed)))
  7581. (defun org-remove-timestamp-with-keyword (keyword)
  7582. "Remove all time stamps with KEYWORD in the current entry."
  7583. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7584. beg)
  7585. (save-excursion
  7586. (org-back-to-heading t)
  7587. (setq beg (point))
  7588. (org-end-of-subtree t t)
  7589. (while (re-search-backward re beg t)
  7590. (replace-match "")
  7591. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7592. (equal (char-before) ?\ ))
  7593. (backward-delete-char 1)
  7594. (if (string-match "^[ \t]*$" (buffer-substring
  7595. (point-at-bol) (point-at-eol)))
  7596. (delete-region (point-at-bol)
  7597. (min (point-max) (1+ (point-at-eol))))))))))
  7598. (defun org-add-planning-info (what &optional time &rest remove)
  7599. "Insert new timestamp with keyword in the line directly after the headline.
  7600. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7601. If non is given, the user is prompted for a date.
  7602. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7603. be removed."
  7604. (interactive)
  7605. (let (org-time-was-given org-end-time-was-given ts
  7606. end default-time default-input)
  7607. (when (and (not time) (memq what '(scheduled deadline)))
  7608. ;; Try to get a default date/time from existing timestamp
  7609. (save-excursion
  7610. (org-back-to-heading t)
  7611. (setq end (save-excursion (outline-next-heading) (point)))
  7612. (when (re-search-forward (if (eq what 'scheduled)
  7613. org-scheduled-time-regexp
  7614. org-deadline-time-regexp)
  7615. end t)
  7616. (setq ts (match-string 1)
  7617. default-time
  7618. (apply 'encode-time (org-parse-time-string ts))
  7619. default-input (and ts (org-get-compact-tod ts))))))
  7620. (when what
  7621. ;; If necessary, get the time from the user
  7622. (setq time (or time (org-read-date nil 'to-time nil nil
  7623. default-time default-input))))
  7624. (when (and org-insert-labeled-timestamps-at-point
  7625. (member what '(scheduled deadline)))
  7626. (insert
  7627. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7628. (org-insert-time-stamp time org-time-was-given
  7629. nil nil nil (list org-end-time-was-given))
  7630. (setq what nil))
  7631. (save-excursion
  7632. (save-restriction
  7633. (let (col list elt ts buffer-invisibility-spec)
  7634. (org-back-to-heading t)
  7635. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7636. (goto-char (match-end 1))
  7637. (setq col (current-column))
  7638. (goto-char (match-end 0))
  7639. (if (eobp) (insert "\n") (forward-char 1))
  7640. (if (and (not (looking-at outline-regexp))
  7641. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7642. "[^\r\n]*"))
  7643. (not (equal (match-string 1) org-clock-string)))
  7644. (narrow-to-region (match-beginning 0) (match-end 0))
  7645. (insert-before-markers "\n")
  7646. (backward-char 1)
  7647. (narrow-to-region (point) (point))
  7648. (org-indent-to-column col))
  7649. ;; Check if we have to remove something.
  7650. (setq list (cons what remove))
  7651. (while list
  7652. (setq elt (pop list))
  7653. (goto-char (point-min))
  7654. (when (or (and (eq elt 'scheduled)
  7655. (re-search-forward org-scheduled-time-regexp nil t))
  7656. (and (eq elt 'deadline)
  7657. (re-search-forward org-deadline-time-regexp nil t))
  7658. (and (eq elt 'closed)
  7659. (re-search-forward org-closed-time-regexp nil t)))
  7660. (replace-match "")
  7661. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7662. (if (looking-at " +") (replace-match ""))))
  7663. (goto-char (point-max))
  7664. (when what
  7665. (insert
  7666. (if (not (equal (char-before) ?\ )) " " "")
  7667. (cond ((eq what 'scheduled) org-scheduled-string)
  7668. ((eq what 'deadline) org-deadline-string)
  7669. ((eq what 'closed) org-closed-string))
  7670. " ")
  7671. (setq ts (org-insert-time-stamp
  7672. time
  7673. (or org-time-was-given
  7674. (and (eq what 'closed) org-log-done-with-time))
  7675. (eq what 'closed)
  7676. nil nil (list org-end-time-was-given)))
  7677. (end-of-line 1))
  7678. (goto-char (point-min))
  7679. (widen)
  7680. (if (and (looking-at "[ \t]+\n")
  7681. (equal (char-before) ?\n))
  7682. (delete-region (1- (point)) (point-at-eol)))
  7683. ts)))))
  7684. (defvar org-log-note-marker (make-marker))
  7685. (defvar org-log-note-purpose nil)
  7686. (defvar org-log-note-state nil)
  7687. (defvar org-log-note-how nil)
  7688. (defvar org-log-note-window-configuration nil)
  7689. (defvar org-log-note-return-to (make-marker))
  7690. (defvar org-log-post-message nil
  7691. "Message to be displayed after a log note has been stored.
  7692. The auto-repeater uses this.")
  7693. (defun org-add-note ()
  7694. "Add a note to the current entry.
  7695. This is done in the same way as adding a state change note."
  7696. (interactive)
  7697. (org-add-log-setup 'note nil t nil))
  7698. (defun org-add-log-setup (&optional purpose state findpos how)
  7699. "Set up the post command hook to take a note.
  7700. If this is about to TODO state change, the new state is expected in STATE.
  7701. When FINDPOS is non-nil, find the correct position for the note in
  7702. the current entry. If not, assume that it can be inserted at point."
  7703. (save-excursion
  7704. (when findpos
  7705. (org-back-to-heading t)
  7706. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7707. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7708. "[^\r\n]*\\)?"))
  7709. (goto-char (match-end 0))
  7710. (unless org-log-states-order-reversed
  7711. (and (= (char-after) ?\n) (forward-char 1))
  7712. (org-skip-over-state-notes)
  7713. (skip-chars-backward " \t\n\r")))
  7714. (move-marker org-log-note-marker (point))
  7715. (setq org-log-note-purpose purpose
  7716. org-log-note-state state
  7717. org-log-note-how how)
  7718. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  7719. (defun org-skip-over-state-notes ()
  7720. "Skip past the list of State notes in an entry."
  7721. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7722. (while (looking-at "[ \t]*- State")
  7723. (condition-case nil
  7724. (org-next-item)
  7725. (error (org-end-of-item)))))
  7726. (defun org-add-log-note (&optional purpose)
  7727. "Pop up a window for taking a note, and add this note later at point."
  7728. (remove-hook 'post-command-hook 'org-add-log-note)
  7729. (setq org-log-note-window-configuration (current-window-configuration))
  7730. (delete-other-windows)
  7731. (move-marker org-log-note-return-to (point))
  7732. (switch-to-buffer (marker-buffer org-log-note-marker))
  7733. (goto-char org-log-note-marker)
  7734. (org-switch-to-buffer-other-window "*Org Note*")
  7735. (erase-buffer)
  7736. (if (memq org-log-note-how '(time state))
  7737. (org-store-log-note)
  7738. (let ((org-inhibit-startup t)) (org-mode))
  7739. (insert (format "# Insert note for %s.
  7740. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7741. (cond
  7742. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7743. ((eq org-log-note-purpose 'done) "closed todo item")
  7744. ((eq org-log-note-purpose 'state)
  7745. (format "state change to \"%s\"" org-log-note-state))
  7746. ((eq org-log-note-purpose 'note)
  7747. "this entry")
  7748. (t (error "This should not happen")))))
  7749. (org-set-local 'org-finish-function 'org-store-log-note)))
  7750. (defvar org-note-abort nil) ; dynamically scoped
  7751. (defun org-store-log-note ()
  7752. "Finish taking a log note, and insert it to where it belongs."
  7753. (let ((txt (buffer-string))
  7754. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7755. lines ind)
  7756. (kill-buffer (current-buffer))
  7757. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7758. (setq txt (replace-match "" t t txt)))
  7759. (if (string-match "\\s-+\\'" txt)
  7760. (setq txt (replace-match "" t t txt)))
  7761. (setq lines (org-split-string txt "\n"))
  7762. (when (and note (string-match "\\S-" note))
  7763. (setq note
  7764. (org-replace-escapes
  7765. note
  7766. (list (cons "%u" (user-login-name))
  7767. (cons "%U" user-full-name)
  7768. (cons "%t" (format-time-string
  7769. (org-time-stamp-format 'long 'inactive)
  7770. (current-time)))
  7771. (cons "%s" (if org-log-note-state
  7772. (concat "\"" org-log-note-state "\"")
  7773. "")))))
  7774. (if lines (setq note (concat note " \\\\")))
  7775. (push note lines))
  7776. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7777. (when lines
  7778. (save-excursion
  7779. (set-buffer (marker-buffer org-log-note-marker))
  7780. (save-excursion
  7781. (goto-char org-log-note-marker)
  7782. (move-marker org-log-note-marker nil)
  7783. (end-of-line 1)
  7784. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7785. (indent-relative nil)
  7786. (insert "- " (pop lines))
  7787. (org-indent-line-function)
  7788. (beginning-of-line 1)
  7789. (looking-at "[ \t]*")
  7790. (setq ind (concat (match-string 0) " "))
  7791. (end-of-line 1)
  7792. (while lines (insert "\n" ind (pop lines)))))))
  7793. (set-window-configuration org-log-note-window-configuration)
  7794. (with-current-buffer (marker-buffer org-log-note-return-to)
  7795. (goto-char org-log-note-return-to))
  7796. (move-marker org-log-note-return-to nil)
  7797. (and org-log-post-message (message "%s" org-log-post-message)))
  7798. (defun org-sparse-tree (&optional arg)
  7799. "Create a sparse tree, prompt for the details.
  7800. This command can create sparse trees. You first need to select the type
  7801. of match used to create the tree:
  7802. t Show entries with a specific TODO keyword.
  7803. T Show entries selected by a tags match.
  7804. p Enter a property name and its value (both with completion on existing
  7805. names/values) and show entries with that property.
  7806. r Show entries matching a regular expression
  7807. d Show deadlines due within `org-deadline-warning-days'."
  7808. (interactive "P")
  7809. (let (ans kwd value)
  7810. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7811. (setq ans (read-char-exclusive))
  7812. (cond
  7813. ((equal ans ?d)
  7814. (call-interactively 'org-check-deadlines))
  7815. ((equal ans ?b)
  7816. (call-interactively 'org-check-before-date))
  7817. ((equal ans ?t)
  7818. (org-show-todo-tree '(4)))
  7819. ((equal ans ?T)
  7820. (call-interactively 'org-tags-sparse-tree))
  7821. ((member ans '(?p ?P))
  7822. (setq kwd (completing-read "Property: "
  7823. (mapcar 'list (org-buffer-property-keys))))
  7824. (setq value (completing-read "Value: "
  7825. (mapcar 'list (org-property-values kwd))))
  7826. (unless (string-match "\\`{.*}\\'" value)
  7827. (setq value (concat "\"" value "\"")))
  7828. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7829. ((member ans '(?r ?R ?/))
  7830. (call-interactively 'org-occur))
  7831. (t (error "No such sparse tree command \"%c\"" ans)))))
  7832. (defvar org-occur-highlights nil
  7833. "List of overlays used for occur matches.")
  7834. (make-variable-buffer-local 'org-occur-highlights)
  7835. (defvar org-occur-parameters nil
  7836. "Parameters of the active org-occur calls.
  7837. This is a list, each call to org-occur pushes as cons cell,
  7838. containing the regular expression and the callback, onto the list.
  7839. The list can contain several entries if `org-occur' has been called
  7840. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7841. will only contain one set of parameters. When the highlights are
  7842. removed (for example with `C-c C-c', or with the next edit (depending
  7843. on `org-remove-highlights-with-change'), this variable is emptied
  7844. as well.")
  7845. (make-variable-buffer-local 'org-occur-parameters)
  7846. (defun org-occur (regexp &optional keep-previous callback)
  7847. "Make a compact tree which shows all matches of REGEXP.
  7848. The tree will show the lines where the regexp matches, and all higher
  7849. headlines above the match. It will also show the heading after the match,
  7850. to make sure editing the matching entry is easy.
  7851. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7852. call to `org-occur' will be kept, to allow stacking of calls to this
  7853. command.
  7854. If CALLBACK is non-nil, it is a function which is called to confirm
  7855. that the match should indeed be shown."
  7856. (interactive "sRegexp: \nP")
  7857. (unless keep-previous
  7858. (org-remove-occur-highlights nil nil t))
  7859. (push (cons regexp callback) org-occur-parameters)
  7860. (let ((cnt 0))
  7861. (save-excursion
  7862. (goto-char (point-min))
  7863. (if (or (not keep-previous) ; do not want to keep
  7864. (not org-occur-highlights)) ; no previous matches
  7865. ;; hide everything
  7866. (org-overview))
  7867. (while (re-search-forward regexp nil t)
  7868. (when (or (not callback)
  7869. (save-match-data (funcall callback)))
  7870. (setq cnt (1+ cnt))
  7871. (when org-highlight-sparse-tree-matches
  7872. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7873. (org-show-context 'occur-tree))))
  7874. (when org-remove-highlights-with-change
  7875. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7876. nil 'local))
  7877. (unless org-sparse-tree-open-archived-trees
  7878. (org-hide-archived-subtrees (point-min) (point-max)))
  7879. (run-hooks 'org-occur-hook)
  7880. (if (interactive-p)
  7881. (message "%d match(es) for regexp %s" cnt regexp))
  7882. cnt))
  7883. (defun org-show-context (&optional key)
  7884. "Make sure point and context and visible.
  7885. How much context is shown depends upon the variables
  7886. `org-show-hierarchy-above', `org-show-following-heading'. and
  7887. `org-show-siblings'."
  7888. (let ((heading-p (org-on-heading-p t))
  7889. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7890. (following-p (org-get-alist-option org-show-following-heading key))
  7891. (entry-p (org-get-alist-option org-show-entry-below key))
  7892. (siblings-p (org-get-alist-option org-show-siblings key)))
  7893. (catch 'exit
  7894. ;; Show heading or entry text
  7895. (if (and heading-p (not entry-p))
  7896. (org-flag-heading nil) ; only show the heading
  7897. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7898. (org-show-hidden-entry))) ; show entire entry
  7899. (when following-p
  7900. ;; Show next sibling, or heading below text
  7901. (save-excursion
  7902. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  7903. (org-flag-heading nil))))
  7904. (when siblings-p (org-show-siblings))
  7905. (when hierarchy-p
  7906. ;; show all higher headings, possibly with siblings
  7907. (save-excursion
  7908. (while (and (condition-case nil
  7909. (progn (org-up-heading-all 1) t)
  7910. (error nil))
  7911. (not (bobp)))
  7912. (org-flag-heading nil)
  7913. (when siblings-p (org-show-siblings))))))))
  7914. (defun org-reveal (&optional siblings)
  7915. "Show current entry, hierarchy above it, and the following headline.
  7916. This can be used to show a consistent set of context around locations
  7917. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  7918. not t for the search context.
  7919. With optional argument SIBLINGS, on each level of the hierarchy all
  7920. siblings are shown. This repairs the tree structure to what it would
  7921. look like when opened with hierarchical calls to `org-cycle'."
  7922. (interactive "P")
  7923. (let ((org-show-hierarchy-above t)
  7924. (org-show-following-heading t)
  7925. (org-show-siblings (if siblings t org-show-siblings)))
  7926. (org-show-context nil)))
  7927. (defun org-highlight-new-match (beg end)
  7928. "Highlight from BEG to END and mark the highlight is an occur headline."
  7929. (let ((ov (org-make-overlay beg end)))
  7930. (org-overlay-put ov 'face 'secondary-selection)
  7931. (push ov org-occur-highlights)))
  7932. (defun org-remove-occur-highlights (&optional beg end noremove)
  7933. "Remove the occur highlights from the buffer.
  7934. BEG and END are ignored. If NOREMOVE is nil, remove this function
  7935. from the `before-change-functions' in the current buffer."
  7936. (interactive)
  7937. (unless org-inhibit-highlight-removal
  7938. (mapc 'org-delete-overlay org-occur-highlights)
  7939. (setq org-occur-highlights nil)
  7940. (setq org-occur-parameters nil)
  7941. (unless noremove
  7942. (remove-hook 'before-change-functions
  7943. 'org-remove-occur-highlights 'local))))
  7944. ;;;; Priorities
  7945. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  7946. "Regular expression matching the priority indicator.")
  7947. (defvar org-remove-priority-next-time nil)
  7948. (defun org-priority-up ()
  7949. "Increase the priority of the current item."
  7950. (interactive)
  7951. (org-priority 'up))
  7952. (defun org-priority-down ()
  7953. "Decrease the priority of the current item."
  7954. (interactive)
  7955. (org-priority 'down))
  7956. (defun org-priority (&optional action)
  7957. "Change the priority of an item by ARG.
  7958. ACTION can be `set', `up', `down', or a character."
  7959. (interactive)
  7960. (setq action (or action 'set))
  7961. (let (current new news have remove)
  7962. (save-excursion
  7963. (org-back-to-heading)
  7964. (if (looking-at org-priority-regexp)
  7965. (setq current (string-to-char (match-string 2))
  7966. have t)
  7967. (setq current org-default-priority))
  7968. (cond
  7969. ((or (eq action 'set)
  7970. (if (featurep 'xemacs) (characterp action) (integerp action)))
  7971. (if (not (eq action 'set))
  7972. (setq new action)
  7973. (message "Priority %c-%c, SPC to remove: "
  7974. org-highest-priority org-lowest-priority)
  7975. (setq new (read-char-exclusive)))
  7976. (if (and (= (upcase org-highest-priority) org-highest-priority)
  7977. (= (upcase org-lowest-priority) org-lowest-priority))
  7978. (setq new (upcase new)))
  7979. (cond ((equal new ?\ ) (setq remove t))
  7980. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  7981. (error "Priority must be between `%c' and `%c'"
  7982. org-highest-priority org-lowest-priority))))
  7983. ((eq action 'up)
  7984. (if (and (not have) (eq last-command this-command))
  7985. (setq new org-lowest-priority)
  7986. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7987. org-default-priority (1- current)))))
  7988. ((eq action 'down)
  7989. (if (and (not have) (eq last-command this-command))
  7990. (setq new org-highest-priority)
  7991. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7992. org-default-priority (1+ current)))))
  7993. (t (error "Invalid action")))
  7994. (if (or (< (upcase new) org-highest-priority)
  7995. (> (upcase new) org-lowest-priority))
  7996. (setq remove t))
  7997. (setq news (format "%c" new))
  7998. (if have
  7999. (if remove
  8000. (replace-match "" t t nil 1)
  8001. (replace-match news t t nil 2))
  8002. (if remove
  8003. (error "No priority cookie found in line")
  8004. (looking-at org-todo-line-regexp)
  8005. (if (match-end 2)
  8006. (progn
  8007. (goto-char (match-end 2))
  8008. (insert " [#" news "]"))
  8009. (goto-char (match-beginning 3))
  8010. (insert "[#" news "] ")))))
  8011. (org-preserve-lc (org-set-tags nil 'align))
  8012. (if remove
  8013. (message "Priority removed")
  8014. (message "Priority of current item set to %s" news))))
  8015. (defun org-get-priority (s)
  8016. "Find priority cookie and return priority."
  8017. (save-match-data
  8018. (if (not (string-match org-priority-regexp s))
  8019. (* 1000 (- org-lowest-priority org-default-priority))
  8020. (* 1000 (- org-lowest-priority
  8021. (string-to-char (match-string 2 s)))))))
  8022. ;;;; Tags
  8023. (defun org-scan-tags (action matcher &optional todo-only)
  8024. "Scan headline tags with inheritance and produce output ACTION.
  8025. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8026. evaluated, testing if a given set of tags qualifies a headline for
  8027. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8028. are included in the output."
  8029. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8030. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8031. (org-re
  8032. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8033. (props (list 'face nil
  8034. 'done-face 'org-done
  8035. 'undone-face nil
  8036. 'mouse-face 'highlight
  8037. 'org-not-done-regexp org-not-done-regexp
  8038. 'org-todo-regexp org-todo-regexp
  8039. 'keymap org-agenda-keymap
  8040. 'help-echo
  8041. (format "mouse-2 or RET jump to org file %s"
  8042. (abbreviate-file-name
  8043. (or (buffer-file-name (buffer-base-buffer))
  8044. (buffer-name (buffer-base-buffer)))))))
  8045. (case-fold-search nil)
  8046. lspos
  8047. tags tags-list tags-alist (llast 0) rtn level category i txt
  8048. todo marker entry priority)
  8049. (save-excursion
  8050. (goto-char (point-min))
  8051. (when (eq action 'sparse-tree)
  8052. (org-overview)
  8053. (org-remove-occur-highlights))
  8054. (while (re-search-forward re nil t)
  8055. (catch :skip
  8056. (setq todo (if (match-end 1) (match-string 2))
  8057. tags (if (match-end 4) (match-string 4)))
  8058. (goto-char (setq lspos (1+ (match-beginning 0))))
  8059. (setq level (org-reduced-level (funcall outline-level))
  8060. category (org-get-category))
  8061. (setq i llast llast level)
  8062. ;; remove tag lists from same and sublevels
  8063. (while (>= i level)
  8064. (when (setq entry (assoc i tags-alist))
  8065. (setq tags-alist (delete entry tags-alist)))
  8066. (setq i (1- i)))
  8067. ;; add the next tags
  8068. (when tags
  8069. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8070. tags-alist
  8071. (cons (cons level tags) tags-alist)))
  8072. ;; compile tags for current headline
  8073. (setq tags-list
  8074. (if org-use-tag-inheritance
  8075. (apply 'append (mapcar 'cdr tags-alist))
  8076. tags))
  8077. (when (and tags org-use-tag-inheritance
  8078. (not (eq t org-use-tag-inheritance)))
  8079. ;; selective inheritance, remove uninherited ones
  8080. (setcdr (car tags-alist)
  8081. (org-remove-uniherited-tags (cdar tags-alist))))
  8082. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8083. (eval matcher)
  8084. (or (not org-agenda-skip-archived-trees)
  8085. (not (member org-archive-tag tags-list))))
  8086. (and (eq action 'agenda) (org-agenda-skip))
  8087. ;; list this headline
  8088. (if (eq action 'sparse-tree)
  8089. (progn
  8090. (and org-highlight-sparse-tree-matches
  8091. (org-get-heading) (match-end 0)
  8092. (org-highlight-new-match
  8093. (match-beginning 0) (match-beginning 1)))
  8094. (org-show-context 'tags-tree))
  8095. (setq txt (org-format-agenda-item
  8096. ""
  8097. (concat
  8098. (if org-tags-match-list-sublevels
  8099. (make-string (1- level) ?.) "")
  8100. (org-get-heading))
  8101. category tags-list)
  8102. priority (org-get-priority txt))
  8103. (goto-char lspos)
  8104. (setq marker (org-agenda-new-marker))
  8105. (org-add-props txt props
  8106. 'org-marker marker 'org-hd-marker marker 'org-category category
  8107. 'priority priority 'type "tagsmatch")
  8108. (push txt rtn))
  8109. ;; if we are to skip sublevels, jump to end of subtree
  8110. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8111. (when (and (eq action 'sparse-tree)
  8112. (not org-sparse-tree-open-archived-trees))
  8113. (org-hide-archived-subtrees (point-min) (point-max)))
  8114. (nreverse rtn)))
  8115. (defun org-remove-uniherited-tags (tags)
  8116. "Remove all tags that are not inherited from the list TAGS."
  8117. (cond
  8118. ((eq org-use-tag-inheritance t) tags)
  8119. ((not org-use-tag-inheritance) nil)
  8120. ((stringp org-use-tag-inheritance)
  8121. (delq nil (mapcar
  8122. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8123. tags)))
  8124. ((listp org-use-tag-inheritance)
  8125. (org-delete-all org-use-tag-inheritance tags))))
  8126. (defvar todo-only) ;; dynamically scoped
  8127. (defun org-tags-sparse-tree (&optional todo-only match)
  8128. "Create a sparse tree according to tags string MATCH.
  8129. MATCH can contain positive and negative selection of tags, like
  8130. \"+WORK+URGENT-WITHBOSS\".
  8131. If optional argument TODO_ONLY is non-nil, only select lines that are
  8132. also TODO lines."
  8133. (interactive "P")
  8134. (org-prepare-agenda-buffers (list (current-buffer)))
  8135. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8136. (defvar org-cached-props nil)
  8137. (defun org-cached-entry-get (pom property)
  8138. (if (or (eq t org-use-property-inheritance)
  8139. (and (stringp org-use-property-inheritance)
  8140. (string-match org-use-property-inheritance property))
  8141. (and (listp org-use-property-inheritance)
  8142. (member property org-use-property-inheritance)))
  8143. ;; Caching is not possible, check it directly
  8144. (org-entry-get pom property 'inherit)
  8145. ;; Get all properties, so that we can do complicated checks easily
  8146. (cdr (assoc property (or org-cached-props
  8147. (setq org-cached-props
  8148. (org-entry-properties pom)))))))
  8149. (defun org-global-tags-completion-table (&optional files)
  8150. "Return the list of all tags in all agenda buffer/files."
  8151. (save-excursion
  8152. (org-uniquify
  8153. (delq nil
  8154. (apply 'append
  8155. (mapcar
  8156. (lambda (file)
  8157. (set-buffer (find-file-noselect file))
  8158. (append (org-get-buffer-tags)
  8159. (mapcar (lambda (x) (if (stringp (car-safe x))
  8160. (list (car-safe x)) nil))
  8161. org-tag-alist)))
  8162. (if (and files (car files))
  8163. files
  8164. (org-agenda-files))))))))
  8165. (defun org-make-tags-matcher (match)
  8166. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8167. ;; todo-only is scoped dynamically into this function, and the function
  8168. ;; may change it it the matcher asksk for it.
  8169. (unless match
  8170. ;; Get a new match request, with completion
  8171. (let ((org-last-tags-completion-table
  8172. (org-global-tags-completion-table)))
  8173. (setq match (completing-read
  8174. "Match: " 'org-tags-completion-function nil nil nil
  8175. 'org-tags-history))))
  8176. ;; Parse the string and create a lisp form
  8177. (let ((match0 match)
  8178. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8179. minus tag mm
  8180. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8181. orterms term orlist re-p str-p level-p level-op
  8182. prop-p pn pv po cat-p gv)
  8183. (if (string-match "/+" match)
  8184. ;; match contains also a todo-matching request
  8185. (progn
  8186. (setq tagsmatch (substring match 0 (match-beginning 0))
  8187. todomatch (substring match (match-end 0)))
  8188. (if (string-match "^!" todomatch)
  8189. (setq todo-only t todomatch (substring todomatch 1)))
  8190. (if (string-match "^\\s-*$" todomatch)
  8191. (setq todomatch nil)))
  8192. ;; only matching tags
  8193. (setq tagsmatch match todomatch nil))
  8194. ;; Make the tags matcher
  8195. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8196. (setq tagsmatcher t)
  8197. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8198. (while (setq term (pop orterms))
  8199. (while (and (equal (substring term -1) "\\") orterms)
  8200. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8201. (while (string-match re term)
  8202. (setq minus (and (match-end 1)
  8203. (equal (match-string 1 term) "-"))
  8204. tag (match-string 2 term)
  8205. re-p (equal (string-to-char tag) ?{)
  8206. level-p (match-end 4)
  8207. prop-p (match-end 5)
  8208. mm (cond
  8209. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8210. (level-p
  8211. (setq level-op (org-op-to-function (match-string 3 term)))
  8212. `(,level-op level ,(string-to-number
  8213. (match-string 4 term))))
  8214. (prop-p
  8215. (setq pn (match-string 5 term)
  8216. po (match-string 6 term)
  8217. pv (match-string 7 term)
  8218. cat-p (equal pn "CATEGORY")
  8219. re-p (equal (string-to-char pv) ?{)
  8220. str-p (equal (string-to-char pv) ?\")
  8221. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8222. (setq po (org-op-to-function po str-p))
  8223. (if (equal pn "CATEGORY")
  8224. (setq gv '(get-text-property (point) 'org-category))
  8225. (setq gv `(org-cached-entry-get nil ,pn)))
  8226. (if re-p
  8227. (if (eq po 'org<>)
  8228. `(not (string-match ,pv (or ,gv "")))
  8229. `(string-match ,pv (or ,gv "")))
  8230. (if str-p
  8231. `(,po (or ,gv "") ,pv)
  8232. `(,po (string-to-number (or ,gv ""))
  8233. ,(string-to-number pv) ))))
  8234. (t `(member ,(downcase tag) tags-list)))
  8235. mm (if minus (list 'not mm) mm)
  8236. term (substring term (match-end 0)))
  8237. (push mm tagsmatcher))
  8238. (push (if (> (length tagsmatcher) 1)
  8239. (cons 'and tagsmatcher)
  8240. (car tagsmatcher))
  8241. orlist)
  8242. (setq tagsmatcher nil))
  8243. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8244. (setq tagsmatcher
  8245. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8246. ;; Make the todo matcher
  8247. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8248. (setq todomatcher t)
  8249. (setq orterms (org-split-string todomatch "|") orlist nil)
  8250. (while (setq term (pop orterms))
  8251. (while (string-match re term)
  8252. (setq minus (and (match-end 1)
  8253. (equal (match-string 1 term) "-"))
  8254. kwd (match-string 2 term)
  8255. re-p (equal (string-to-char kwd) ?{)
  8256. term (substring term (match-end 0))
  8257. mm (if re-p
  8258. `(string-match ,(substring kwd 1 -1) todo)
  8259. (list 'equal 'todo kwd))
  8260. mm (if minus (list 'not mm) mm))
  8261. (push mm todomatcher))
  8262. (push (if (> (length todomatcher) 1)
  8263. (cons 'and todomatcher)
  8264. (car todomatcher))
  8265. orlist)
  8266. (setq todomatcher nil))
  8267. (setq todomatcher (if (> (length orlist) 1)
  8268. (cons 'or orlist) (car orlist))))
  8269. ;; Return the string and lisp forms of the matcher
  8270. (setq matcher (if todomatcher
  8271. (list 'and tagsmatcher todomatcher)
  8272. tagsmatcher))
  8273. (cons match0 matcher)))
  8274. (defun org-op-to-function (op &optional stringp)
  8275. (setq op
  8276. (cond
  8277. ((equal op "<" ) '(< string< ))
  8278. ((equal op ">" ) '(> org-string> ))
  8279. ((member op '("<=" "=<")) '(<= org-string<= ))
  8280. ((member op '(">=" "=>")) '(>= org-string>= ))
  8281. ((member op '("=" "==")) '(= string= ))
  8282. ((member op '("<>" "!=")) '(org<> org-string<> ))))
  8283. (nth (if stringp 1 0) op))
  8284. (defun org<> (a b) (not (= a b)))
  8285. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8286. (defun org-string>= (a b) (not (string< a b)))
  8287. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8288. (defun org-string<> (a b) (not (string= a b)))
  8289. (defun org-match-any-p (re list)
  8290. "Does re match any element of list?"
  8291. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8292. (delq nil list))
  8293. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8294. (defvar org-tags-overlay (org-make-overlay 1 1))
  8295. (org-detach-overlay org-tags-overlay)
  8296. (defun org-get-tags-at (&optional pos)
  8297. "Get a list of all headline tags applicable at POS.
  8298. POS defaults to point. If tags are inherited, the list contains
  8299. the targets in the same sequence as the headlines appear, i.e.
  8300. sthe tags of the current headline come last."
  8301. (interactive)
  8302. (let (tags ltags lastpos parent)
  8303. (save-excursion
  8304. (save-restriction
  8305. (widen)
  8306. (goto-char (or pos (point)))
  8307. (save-match-data
  8308. (condition-case nil
  8309. (progn
  8310. (org-back-to-heading t)
  8311. (while (not (equal lastpos (point)))
  8312. (setq lastpos (point))
  8313. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8314. (setq ltags (org-split-string
  8315. (org-match-string-no-properties 1) ":"))
  8316. (setq tags (append (org-remove-uniherited-tags ltags)
  8317. tags)))
  8318. (or org-use-tag-inheritance (error ""))
  8319. (org-up-heading-all 1)
  8320. (setq parent t)))
  8321. (error nil))))
  8322. tags)))
  8323. (defun org-toggle-tag (tag &optional onoff)
  8324. "Toggle the tag TAG for the current line.
  8325. If ONOFF is `on' or `off', don't toggle but set to this state."
  8326. (unless (org-on-heading-p t) (error "Not on headling"))
  8327. (let (res current)
  8328. (save-excursion
  8329. (beginning-of-line)
  8330. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8331. (point-at-eol) t)
  8332. (progn
  8333. (setq current (match-string 1))
  8334. (replace-match ""))
  8335. (setq current ""))
  8336. (setq current (nreverse (org-split-string current ":")))
  8337. (cond
  8338. ((eq onoff 'on)
  8339. (setq res t)
  8340. (or (member tag current) (push tag current)))
  8341. ((eq onoff 'off)
  8342. (or (not (member tag current)) (setq current (delete tag current))))
  8343. (t (if (member tag current)
  8344. (setq current (delete tag current))
  8345. (setq res t)
  8346. (push tag current))))
  8347. (end-of-line 1)
  8348. (if current
  8349. (progn
  8350. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8351. (org-set-tags nil t))
  8352. (delete-horizontal-space))
  8353. (run-hooks 'org-after-tags-change-hook))
  8354. res))
  8355. (defun org-align-tags-here (to-col)
  8356. ;; Assumes that this is a headline
  8357. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8358. (beginning-of-line 1)
  8359. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8360. (< pos (match-beginning 2)))
  8361. (progn
  8362. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8363. (goto-char (match-beginning 1))
  8364. (insert " ")
  8365. (delete-region (point) (1+ (match-beginning 2)))
  8366. (setq ncol (max (1+ (current-column))
  8367. (1+ col)
  8368. (if (> to-col 0)
  8369. to-col
  8370. (- (abs to-col) tags-l))))
  8371. (setq p (point))
  8372. (insert (make-string (- ncol (current-column)) ?\ ))
  8373. (setq ncol (current-column))
  8374. (tabify p (point-at-eol))
  8375. (org-move-to-column (min ncol col) t))
  8376. (goto-char pos))))
  8377. (defun org-set-tags (&optional arg just-align)
  8378. "Set the tags for the current headline.
  8379. With prefix ARG, realign all tags in headings in the current buffer."
  8380. (interactive "P")
  8381. (let* ((re (concat "^" outline-regexp))
  8382. (current (org-get-tags-string))
  8383. (col (current-column))
  8384. (org-setting-tags t)
  8385. table current-tags inherited-tags ; computed below when needed
  8386. tags p0 c0 c1 rpl)
  8387. (if arg
  8388. (save-excursion
  8389. (goto-char (point-min))
  8390. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8391. (while (re-search-forward re nil t)
  8392. (org-set-tags nil t)
  8393. (end-of-line 1)))
  8394. (message "All tags realigned to column %d" org-tags-column))
  8395. (if just-align
  8396. (setq tags current)
  8397. ;; Get a new set of tags from the user
  8398. (save-excursion
  8399. (setq table (or org-tag-alist (org-get-buffer-tags))
  8400. org-last-tags-completion-table table
  8401. current-tags (org-split-string current ":")
  8402. inherited-tags (nreverse
  8403. (nthcdr (length current-tags)
  8404. (nreverse (org-get-tags-at))))
  8405. tags
  8406. (if (or (eq t org-use-fast-tag-selection)
  8407. (and org-use-fast-tag-selection
  8408. (delq nil (mapcar 'cdr table))))
  8409. (org-fast-tag-selection
  8410. current-tags inherited-tags table
  8411. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8412. (let ((org-add-colon-after-tag-completion t))
  8413. (org-trim
  8414. (org-without-partial-completion
  8415. (completing-read "Tags: " 'org-tags-completion-function
  8416. nil nil current 'org-tags-history)))))))
  8417. (while (string-match "[-+&]+" tags)
  8418. ;; No boolean logic, just a list
  8419. (setq tags (replace-match ":" t t tags))))
  8420. (if (string-match "\\`[\t ]*\\'" tags)
  8421. (setq tags "")
  8422. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8423. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8424. ;; Insert new tags at the correct column
  8425. (beginning-of-line 1)
  8426. (cond
  8427. ((and (equal current "") (equal tags "")))
  8428. ((re-search-forward
  8429. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8430. (point-at-eol) t)
  8431. (if (equal tags "")
  8432. (setq rpl "")
  8433. (goto-char (match-beginning 0))
  8434. (setq c0 (current-column) p0 (point)
  8435. c1 (max (1+ c0) (if (> org-tags-column 0)
  8436. org-tags-column
  8437. (- (- org-tags-column) (length tags))))
  8438. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8439. (replace-match rpl t t)
  8440. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8441. tags)
  8442. (t (error "Tags alignment failed")))
  8443. (org-move-to-column col)
  8444. (unless just-align
  8445. (run-hooks 'org-after-tags-change-hook)))))
  8446. (defun org-change-tag-in-region (beg end tag off)
  8447. "Add or remove TAG for each entry in the region.
  8448. This works in the agenda, and also in an org-mode buffer."
  8449. (interactive
  8450. (list (region-beginning) (region-end)
  8451. (let ((org-last-tags-completion-table
  8452. (if (org-mode-p)
  8453. (org-get-buffer-tags)
  8454. (org-global-tags-completion-table))))
  8455. (completing-read
  8456. "Tag: " 'org-tags-completion-function nil nil nil
  8457. 'org-tags-history))
  8458. (progn
  8459. (message "[s]et or [r]emove? ")
  8460. (equal (read-char-exclusive) ?r))))
  8461. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8462. (let ((agendap (equal major-mode 'org-agenda-mode))
  8463. l1 l2 m buf pos newhead (cnt 0))
  8464. (goto-char end)
  8465. (setq l2 (1- (org-current-line)))
  8466. (goto-char beg)
  8467. (setq l1 (org-current-line))
  8468. (loop for l from l1 to l2 do
  8469. (goto-line l)
  8470. (setq m (get-text-property (point) 'org-hd-marker))
  8471. (when (or (and (org-mode-p) (org-on-heading-p))
  8472. (and agendap m))
  8473. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8474. pos (if agendap m (point)))
  8475. (with-current-buffer buf
  8476. (save-excursion
  8477. (save-restriction
  8478. (goto-char pos)
  8479. (setq cnt (1+ cnt))
  8480. (org-toggle-tag tag (if off 'off 'on))
  8481. (setq newhead (org-get-heading)))))
  8482. (and agendap (org-agenda-change-all-lines newhead m))))
  8483. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8484. (defun org-tags-completion-function (string predicate &optional flag)
  8485. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8486. (confirm (lambda (x) (stringp (car x)))))
  8487. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8488. (setq s1 (match-string 1 string)
  8489. s2 (match-string 2 string))
  8490. (setq s1 "" s2 string))
  8491. (cond
  8492. ((eq flag nil)
  8493. ;; try completion
  8494. (setq rtn (try-completion s2 ctable confirm))
  8495. (if (stringp rtn)
  8496. (setq rtn
  8497. (concat s1 s2 (substring rtn (length s2))
  8498. (if (and org-add-colon-after-tag-completion
  8499. (assoc rtn ctable))
  8500. ":" ""))))
  8501. rtn)
  8502. ((eq flag t)
  8503. ;; all-completions
  8504. (all-completions s2 ctable confirm)
  8505. )
  8506. ((eq flag 'lambda)
  8507. ;; exact match?
  8508. (assoc s2 ctable)))
  8509. ))
  8510. (defun org-fast-tag-insert (kwd tags face &optional end)
  8511. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8512. (insert (format "%-12s" (concat kwd ":"))
  8513. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8514. (or end "")))
  8515. (defun org-fast-tag-show-exit (flag)
  8516. (save-excursion
  8517. (goto-line 3)
  8518. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8519. (replace-match ""))
  8520. (when flag
  8521. (end-of-line 1)
  8522. (org-move-to-column (- (window-width) 19) t)
  8523. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8524. (defun org-set-current-tags-overlay (current prefix)
  8525. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8526. (if (featurep 'xemacs)
  8527. (org-overlay-display org-tags-overlay (concat prefix s)
  8528. 'secondary-selection)
  8529. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8530. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8531. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8532. "Fast tag selection with single keys.
  8533. CURRENT is the current list of tags in the headline, INHERITED is the
  8534. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8535. possibly with grouping information. TODO-TABLE is a similar table with
  8536. TODO keywords, should these have keys assigned to them.
  8537. If the keys are nil, a-z are automatically assigned.
  8538. Returns the new tags string, or nil to not change the current settings."
  8539. (let* ((fulltable (append table todo-table))
  8540. (maxlen (apply 'max (mapcar
  8541. (lambda (x)
  8542. (if (stringp (car x)) (string-width (car x)) 0))
  8543. fulltable)))
  8544. (buf (current-buffer))
  8545. (expert (eq org-fast-tag-selection-single-key 'expert))
  8546. (buffer-tags nil)
  8547. (fwidth (+ maxlen 3 1 3))
  8548. (ncol (/ (- (window-width) 4) fwidth))
  8549. (i-face 'org-done)
  8550. (c-face 'org-todo)
  8551. tg cnt e c char c1 c2 ntable tbl rtn
  8552. ov-start ov-end ov-prefix
  8553. (exit-after-next org-fast-tag-selection-single-key)
  8554. (done-keywords org-done-keywords)
  8555. groups ingroup)
  8556. (save-excursion
  8557. (beginning-of-line 1)
  8558. (if (looking-at
  8559. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8560. (setq ov-start (match-beginning 1)
  8561. ov-end (match-end 1)
  8562. ov-prefix "")
  8563. (setq ov-start (1- (point-at-eol))
  8564. ov-end (1+ ov-start))
  8565. (skip-chars-forward "^\n\r")
  8566. (setq ov-prefix
  8567. (concat
  8568. (buffer-substring (1- (point)) (point))
  8569. (if (> (current-column) org-tags-column)
  8570. " "
  8571. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8572. (org-move-overlay org-tags-overlay ov-start ov-end)
  8573. (save-window-excursion
  8574. (if expert
  8575. (set-buffer (get-buffer-create " *Org tags*"))
  8576. (delete-other-windows)
  8577. (split-window-vertically)
  8578. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8579. (erase-buffer)
  8580. (org-set-local 'org-done-keywords done-keywords)
  8581. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8582. (org-fast-tag-insert "Current" current c-face "\n\n")
  8583. (org-fast-tag-show-exit exit-after-next)
  8584. (org-set-current-tags-overlay current ov-prefix)
  8585. (setq tbl fulltable char ?a cnt 0)
  8586. (while (setq e (pop tbl))
  8587. (cond
  8588. ((equal e '(:startgroup))
  8589. (push '() groups) (setq ingroup t)
  8590. (when (not (= cnt 0))
  8591. (setq cnt 0)
  8592. (insert "\n"))
  8593. (insert "{ "))
  8594. ((equal e '(:endgroup))
  8595. (setq ingroup nil cnt 0)
  8596. (insert "}\n"))
  8597. (t
  8598. (setq tg (car e) c2 nil)
  8599. (if (cdr e)
  8600. (setq c (cdr e))
  8601. ;; automatically assign a character.
  8602. (setq c1 (string-to-char
  8603. (downcase (substring
  8604. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8605. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8606. (while (or (rassoc char ntable) (rassoc char table))
  8607. (setq char (1+ char)))
  8608. (setq c2 c1))
  8609. (setq c (or c2 char)))
  8610. (if ingroup (push tg (car groups)))
  8611. (setq tg (org-add-props tg nil 'face
  8612. (cond
  8613. ((not (assoc tg table))
  8614. (org-get-todo-face tg))
  8615. ((member tg current) c-face)
  8616. ((member tg inherited) i-face)
  8617. (t nil))))
  8618. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8619. (insert "[" c "] " tg (make-string
  8620. (- fwidth 4 (length tg)) ?\ ))
  8621. (push (cons tg c) ntable)
  8622. (when (= (setq cnt (1+ cnt)) ncol)
  8623. (insert "\n")
  8624. (if ingroup (insert " "))
  8625. (setq cnt 0)))))
  8626. (setq ntable (nreverse ntable))
  8627. (insert "\n")
  8628. (goto-char (point-min))
  8629. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8630. (fit-window-to-buffer))
  8631. (setq rtn
  8632. (catch 'exit
  8633. (while t
  8634. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8635. (if groups " [!] no groups" " [!]groups")
  8636. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8637. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8638. (cond
  8639. ((= c ?\r) (throw 'exit t))
  8640. ((= c ?!)
  8641. (setq groups (not groups))
  8642. (goto-char (point-min))
  8643. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8644. ((= c ?\C-c)
  8645. (if (not expert)
  8646. (org-fast-tag-show-exit
  8647. (setq exit-after-next (not exit-after-next)))
  8648. (setq expert nil)
  8649. (delete-other-windows)
  8650. (split-window-vertically)
  8651. (org-switch-to-buffer-other-window " *Org tags*")
  8652. (and (fboundp 'fit-window-to-buffer)
  8653. (fit-window-to-buffer))))
  8654. ((or (= c ?\C-g)
  8655. (and (= c ?q) (not (rassoc c ntable))))
  8656. (org-detach-overlay org-tags-overlay)
  8657. (setq quit-flag t))
  8658. ((= c ?\ )
  8659. (setq current nil)
  8660. (if exit-after-next (setq exit-after-next 'now)))
  8661. ((= c ?\t)
  8662. (condition-case nil
  8663. (setq tg (completing-read
  8664. "Tag: "
  8665. (or buffer-tags
  8666. (with-current-buffer buf
  8667. (org-get-buffer-tags)))))
  8668. (quit (setq tg "")))
  8669. (when (string-match "\\S-" tg)
  8670. (add-to-list 'buffer-tags (list tg))
  8671. (if (member tg current)
  8672. (setq current (delete tg current))
  8673. (push tg current)))
  8674. (if exit-after-next (setq exit-after-next 'now)))
  8675. ((setq e (rassoc c todo-table) tg (car e))
  8676. (with-current-buffer buf
  8677. (save-excursion (org-todo tg)))
  8678. (if exit-after-next (setq exit-after-next 'now)))
  8679. ((setq e (rassoc c ntable) tg (car e))
  8680. (if (member tg current)
  8681. (setq current (delete tg current))
  8682. (loop for g in groups do
  8683. (if (member tg g)
  8684. (mapc (lambda (x)
  8685. (setq current (delete x current)))
  8686. g)))
  8687. (push tg current))
  8688. (if exit-after-next (setq exit-after-next 'now))))
  8689. ;; Create a sorted list
  8690. (setq current
  8691. (sort current
  8692. (lambda (a b)
  8693. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8694. (if (eq exit-after-next 'now) (throw 'exit t))
  8695. (goto-char (point-min))
  8696. (beginning-of-line 2)
  8697. (delete-region (point) (point-at-eol))
  8698. (org-fast-tag-insert "Current" current c-face)
  8699. (org-set-current-tags-overlay current ov-prefix)
  8700. (while (re-search-forward
  8701. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8702. (setq tg (match-string 1))
  8703. (add-text-properties
  8704. (match-beginning 1) (match-end 1)
  8705. (list 'face
  8706. (cond
  8707. ((member tg current) c-face)
  8708. ((member tg inherited) i-face)
  8709. (t (get-text-property (match-beginning 1) 'face))))))
  8710. (goto-char (point-min)))))
  8711. (org-detach-overlay org-tags-overlay)
  8712. (if rtn
  8713. (mapconcat 'identity current ":")
  8714. nil))))
  8715. (defun org-get-tags-string ()
  8716. "Get the TAGS string in the current headline."
  8717. (unless (org-on-heading-p t)
  8718. (error "Not on a heading"))
  8719. (save-excursion
  8720. (beginning-of-line 1)
  8721. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8722. (org-match-string-no-properties 1)
  8723. "")))
  8724. (defun org-get-tags ()
  8725. "Get the list of tags specified in the current headline."
  8726. (org-split-string (org-get-tags-string) ":"))
  8727. (defun org-get-buffer-tags ()
  8728. "Get a table of all tags used in the buffer, for completion."
  8729. (let (tags)
  8730. (save-excursion
  8731. (goto-char (point-min))
  8732. (while (re-search-forward
  8733. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8734. (when (equal (char-after (point-at-bol 0)) ?*)
  8735. (mapc (lambda (x) (add-to-list 'tags x))
  8736. (org-split-string (org-match-string-no-properties 1) ":")))))
  8737. (mapcar 'list tags)))
  8738. ;;;; Properties
  8739. ;;; Setting and retrieving properties
  8740. (defconst org-special-properties
  8741. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8742. "TIMESTAMP" "TIMESTAMP_IA")
  8743. "The special properties valid in Org-mode.
  8744. These are properties that are not defined in the property drawer,
  8745. but in some other way.")
  8746. (defconst org-default-properties
  8747. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8748. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  8749. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE")
  8750. "Some properties that are used by Org-mode for various purposes.
  8751. Being in this list makes sure that they are offered for completion.")
  8752. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8753. "Regular expression matching the first line of a property drawer.")
  8754. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8755. "Regular expression matching the first line of a property drawer.")
  8756. (defun org-property-action ()
  8757. "Do an action on properties."
  8758. (interactive)
  8759. (let (c)
  8760. (org-at-property-p)
  8761. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8762. (setq c (read-char-exclusive))
  8763. (cond
  8764. ((equal c ?s)
  8765. (call-interactively 'org-set-property))
  8766. ((equal c ?d)
  8767. (call-interactively 'org-delete-property))
  8768. ((equal c ?D)
  8769. (call-interactively 'org-delete-property-globally))
  8770. ((equal c ?c)
  8771. (call-interactively 'org-compute-property-at-point))
  8772. (t (error "No such property action %c" c)))))
  8773. (defun org-at-property-p ()
  8774. "Is the cursor in a property line?"
  8775. ;; FIXME: Does not check if we are actually in the drawer.
  8776. ;; FIXME: also returns true on any drawers.....
  8777. ;; This is used by C-c C-c for property action.
  8778. (save-excursion
  8779. (beginning-of-line 1)
  8780. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8781. (defun org-get-property-block (&optional beg end force)
  8782. "Return the (beg . end) range of the body of the property drawer.
  8783. BEG and END can be beginning and end of subtree, if not given
  8784. they will be found.
  8785. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8786. (catch 'exit
  8787. (save-excursion
  8788. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8789. (end (or end (progn (outline-next-heading) (point)))))
  8790. (goto-char beg)
  8791. (if (re-search-forward org-property-start-re end t)
  8792. (setq beg (1+ (match-end 0)))
  8793. (if force
  8794. (save-excursion
  8795. (org-insert-property-drawer)
  8796. (setq end (progn (outline-next-heading) (point))))
  8797. (throw 'exit nil))
  8798. (goto-char beg)
  8799. (if (re-search-forward org-property-start-re end t)
  8800. (setq beg (1+ (match-end 0)))))
  8801. (if (re-search-forward org-property-end-re end t)
  8802. (setq end (match-beginning 0))
  8803. (or force (throw 'exit nil))
  8804. (goto-char beg)
  8805. (setq end beg)
  8806. (org-indent-line-function)
  8807. (insert ":END:\n"))
  8808. (cons beg end)))))
  8809. (defun org-entry-properties (&optional pom which)
  8810. "Get all properties of the entry at point-or-marker POM.
  8811. This includes the TODO keyword, the tags, time strings for deadline,
  8812. scheduled, and clocking, and any additional properties defined in the
  8813. entry. The return value is an alist, keys may occur multiple times
  8814. if the property key was used several times.
  8815. POM may also be nil, in which case the current entry is used.
  8816. If WHICH is nil or `all', get all properties. If WHICH is
  8817. `special' or `standard', only get that subclass."
  8818. (setq which (or which 'all))
  8819. (org-with-point-at pom
  8820. (let ((clockstr (substring org-clock-string 0 -1))
  8821. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8822. beg end range props sum-props key value string clocksum)
  8823. (save-excursion
  8824. (when (condition-case nil (org-back-to-heading t) (error nil))
  8825. (setq beg (point))
  8826. (setq sum-props (get-text-property (point) 'org-summaries))
  8827. (setq clocksum (get-text-property (point) :org-clock-minutes))
  8828. (outline-next-heading)
  8829. (setq end (point))
  8830. (when (memq which '(all special))
  8831. ;; Get the special properties, like TODO and tags
  8832. (goto-char beg)
  8833. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  8834. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  8835. (when (looking-at org-priority-regexp)
  8836. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  8837. (when (and (setq value (org-get-tags-string))
  8838. (string-match "\\S-" value))
  8839. (push (cons "TAGS" value) props))
  8840. (when (setq value (org-get-tags-at))
  8841. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  8842. props))
  8843. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  8844. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  8845. string (if (equal key clockstr)
  8846. (org-no-properties
  8847. (org-trim
  8848. (buffer-substring
  8849. (match-beginning 3) (goto-char (point-at-eol)))))
  8850. (substring (org-match-string-no-properties 3) 1 -1)))
  8851. (unless key
  8852. (if (= (char-after (match-beginning 3)) ?\[)
  8853. (setq key "TIMESTAMP_IA")
  8854. (setq key "TIMESTAMP")))
  8855. (when (or (equal key clockstr) (not (assoc key props)))
  8856. (push (cons key string) props)))
  8857. )
  8858. (when (memq which '(all standard))
  8859. ;; Get the standard properties, like :PORP: ...
  8860. (setq range (org-get-property-block beg end))
  8861. (when range
  8862. (goto-char (car range))
  8863. (while (re-search-forward
  8864. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  8865. (cdr range) t)
  8866. (setq key (org-match-string-no-properties 1)
  8867. value (org-trim (or (org-match-string-no-properties 2) "")))
  8868. (unless (member key excluded)
  8869. (push (cons key (or value "")) props)))))
  8870. (if clocksum
  8871. (push (cons "CLOCKSUM"
  8872. (org-columns-number-to-string (/ (float clocksum) 60.)
  8873. 'add_times))
  8874. props))
  8875. (append sum-props (nreverse props)))))))
  8876. (defun org-entry-get (pom property &optional inherit)
  8877. "Get value of PROPERTY for entry at point-or-marker POM.
  8878. If INHERIT is non-nil and the entry does not have the property,
  8879. then also check higher levels of the hierarchy.
  8880. If INHERIT is the symbol `selective', use inheritance only if the setting
  8881. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  8882. If the property is present but empty, the return value is the empty string.
  8883. If the property is not present at all, nil is returned."
  8884. (org-with-point-at pom
  8885. (if (and inherit (if (eq inherit 'selective)
  8886. (org-property-inherit-p property)
  8887. t))
  8888. (org-entry-get-with-inheritance property)
  8889. (if (member property org-special-properties)
  8890. ;; We need a special property. Use brute force, get all properties.
  8891. (cdr (assoc property (org-entry-properties nil 'special)))
  8892. (let ((range (org-get-property-block)))
  8893. (if (and range
  8894. (goto-char (car range))
  8895. (re-search-forward
  8896. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  8897. (cdr range) t))
  8898. ;; Found the property, return it.
  8899. (if (match-end 1)
  8900. (org-match-string-no-properties 1)
  8901. "")))))))
  8902. (defun org-property-or-variable-value (var &optional inherit)
  8903. "Check if there is a property fixing the value of VAR.
  8904. If yes, return this value. If not, return the current value of the variable."
  8905. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  8906. (if (and prop (stringp prop) (string-match "\\S-" prop))
  8907. (read prop)
  8908. (symbol-value var))))
  8909. (defun org-entry-delete (pom property)
  8910. "Delete the property PROPERTY from entry at point-or-marker POM."
  8911. (org-with-point-at pom
  8912. (if (member property org-special-properties)
  8913. nil ; cannot delete these properties.
  8914. (let ((range (org-get-property-block)))
  8915. (if (and range
  8916. (goto-char (car range))
  8917. (re-search-forward
  8918. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  8919. (cdr range) t))
  8920. (progn
  8921. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  8922. t)
  8923. nil)))))
  8924. ;; Multi-values properties are properties that contain multiple values
  8925. ;; These values are assumed to be single words, separated by whitespace.
  8926. (defun org-entry-add-to-multivalued-property (pom property value)
  8927. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  8928. (let* ((old (org-entry-get pom property))
  8929. (values (and old (org-split-string old "[ \t]"))))
  8930. (unless (member value values)
  8931. (setq values (cons value values))
  8932. (org-entry-put pom property
  8933. (mapconcat 'identity values " ")))))
  8934. (defun org-entry-remove-from-multivalued-property (pom property value)
  8935. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  8936. (let* ((old (org-entry-get pom property))
  8937. (values (and old (org-split-string old "[ \t]"))))
  8938. (when (member value values)
  8939. (setq values (delete value values))
  8940. (org-entry-put pom property
  8941. (mapconcat 'identity values " ")))))
  8942. (defun org-entry-member-in-multivalued-property (pom property value)
  8943. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  8944. (let* ((old (org-entry-get pom property))
  8945. (values (and old (org-split-string old "[ \t]"))))
  8946. (member value values)))
  8947. (defvar org-entry-property-inherited-from (make-marker))
  8948. (defun org-entry-get-with-inheritance (property)
  8949. "Get entry property, and search higher levels if not present."
  8950. (let (tmp)
  8951. (save-excursion
  8952. (save-restriction
  8953. (widen)
  8954. (catch 'ex
  8955. (while t
  8956. (when (setq tmp (org-entry-get nil property))
  8957. (org-back-to-heading t)
  8958. (move-marker org-entry-property-inherited-from (point))
  8959. (throw 'ex tmp))
  8960. (or (org-up-heading-safe) (throw 'ex nil)))))
  8961. (or tmp
  8962. (cdr (assoc property org-local-properties))
  8963. (cdr (assoc property org-global-properties))
  8964. (cdr (assoc property org-global-properties-fixed))))))
  8965. (defun org-entry-put (pom property value)
  8966. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  8967. (org-with-point-at pom
  8968. (org-back-to-heading t)
  8969. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  8970. range)
  8971. (cond
  8972. ((equal property "TODO")
  8973. (when (and (stringp value) (string-match "\\S-" value)
  8974. (not (member value org-todo-keywords-1)))
  8975. (error "\"%s\" is not a valid TODO state" value))
  8976. (if (or (not value)
  8977. (not (string-match "\\S-" value)))
  8978. (setq value 'none))
  8979. (org-todo value)
  8980. (org-set-tags nil 'align))
  8981. ((equal property "PRIORITY")
  8982. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  8983. (string-to-char value) ?\ ))
  8984. (org-set-tags nil 'align))
  8985. ((equal property "SCHEDULED")
  8986. (if (re-search-forward org-scheduled-time-regexp end t)
  8987. (cond
  8988. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8989. ((eq value 'later) (org-timestamp-change 1 'day))
  8990. (t (call-interactively 'org-schedule)))
  8991. (call-interactively 'org-schedule)))
  8992. ((equal property "DEADLINE")
  8993. (if (re-search-forward org-deadline-time-regexp end t)
  8994. (cond
  8995. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8996. ((eq value 'later) (org-timestamp-change 1 'day))
  8997. (t (call-interactively 'org-deadline)))
  8998. (call-interactively 'org-deadline)))
  8999. ((member property org-special-properties)
  9000. (error "The %s property can not yet be set with `org-entry-put'"
  9001. property))
  9002. (t ; a non-special property
  9003. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9004. (setq range (org-get-property-block beg end 'force))
  9005. (goto-char (car range))
  9006. (if (re-search-forward
  9007. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9008. (progn
  9009. (delete-region (match-beginning 1) (match-end 1))
  9010. (goto-char (match-beginning 1)))
  9011. (goto-char (cdr range))
  9012. (insert "\n")
  9013. (backward-char 1)
  9014. (org-indent-line-function)
  9015. (insert ":" property ":"))
  9016. (and value (insert " " value))
  9017. (org-indent-line-function)))))))
  9018. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9019. "Get all property keys in the current buffer.
  9020. With INCLUDE-SPECIALS, also list the special properties that relect things
  9021. like tags and TODO state.
  9022. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9023. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9024. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9025. formats in the current buffer."
  9026. (let (rtn range cfmt cols s p)
  9027. (save-excursion
  9028. (save-restriction
  9029. (widen)
  9030. (goto-char (point-min))
  9031. (while (re-search-forward org-property-start-re nil t)
  9032. (setq range (org-get-property-block))
  9033. (goto-char (car range))
  9034. (while (re-search-forward
  9035. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9036. (cdr range) t)
  9037. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9038. (outline-next-heading))))
  9039. (when include-specials
  9040. (setq rtn (append org-special-properties rtn)))
  9041. (when include-defaults
  9042. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9043. (when include-columns
  9044. (save-excursion
  9045. (save-restriction
  9046. (widen)
  9047. (goto-char (point-min))
  9048. (while (re-search-forward
  9049. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9050. nil t)
  9051. (setq cfmt (match-string 2) s 0)
  9052. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9053. cfmt s)
  9054. (setq s (match-end 0)
  9055. p (match-string 1 cfmt))
  9056. (unless (or (equal p "ITEM")
  9057. (member p org-special-properties))
  9058. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9059. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9060. (defun org-property-values (key)
  9061. "Return a list of all values of property KEY."
  9062. (save-excursion
  9063. (save-restriction
  9064. (widen)
  9065. (goto-char (point-min))
  9066. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9067. values)
  9068. (while (re-search-forward re nil t)
  9069. (add-to-list 'values (org-trim (match-string 1))))
  9070. (delete "" values)))))
  9071. (defun org-insert-property-drawer ()
  9072. "Insert a property drawer into the current entry."
  9073. (interactive)
  9074. (org-back-to-heading t)
  9075. (looking-at outline-regexp)
  9076. (let ((indent (- (match-end 0)(match-beginning 0)))
  9077. (beg (point))
  9078. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9079. end hiddenp)
  9080. (outline-next-heading)
  9081. (setq end (point))
  9082. (goto-char beg)
  9083. (while (re-search-forward re end t))
  9084. (setq hiddenp (org-invisible-p))
  9085. (end-of-line 1)
  9086. (and (equal (char-after) ?\n) (forward-char 1))
  9087. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9088. (beginning-of-line 2))
  9089. (org-skip-over-state-notes)
  9090. (skip-chars-backward " \t\n\r")
  9091. (if (eq (char-before) ?*) (forward-char 1))
  9092. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9093. (beginning-of-line 0)
  9094. (org-indent-to-column indent)
  9095. (beginning-of-line 2)
  9096. (org-indent-to-column indent)
  9097. (beginning-of-line 0)
  9098. (if hiddenp
  9099. (save-excursion
  9100. (org-back-to-heading t)
  9101. (hide-entry))
  9102. (org-flag-drawer t))))
  9103. (defun org-set-property (property value)
  9104. "In the current entry, set PROPERTY to VALUE.
  9105. When called interactively, this will prompt for a property name, offering
  9106. completion on existing and default properties. And then it will prompt
  9107. for a value, offering competion either on allowed values (via an inherited
  9108. xxx_ALL property) or on existing values in other instances of this property
  9109. in the current file."
  9110. (interactive
  9111. (let* ((completion-ignore-case t)
  9112. (keys (org-buffer-property-keys nil t t))
  9113. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9114. (prop (if (member prop0 keys)
  9115. prop0
  9116. (or (cdr (assoc (downcase prop0)
  9117. (mapcar (lambda (x) (cons (downcase x) x))
  9118. keys)))
  9119. prop0)))
  9120. (cur (org-entry-get nil prop))
  9121. (allowed (org-property-get-allowed-values nil prop 'table))
  9122. (existing (mapcar 'list (org-property-values prop)))
  9123. (val (if allowed
  9124. (org-completing-read "Value: " allowed nil 'req-match)
  9125. (org-completing-read
  9126. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9127. (concat "[" cur "]") "")
  9128. ": ")
  9129. existing nil nil "" nil cur))))
  9130. (list prop (if (equal val "") cur val))))
  9131. (unless (equal (org-entry-get nil property) value)
  9132. (org-entry-put nil property value)))
  9133. (defun org-delete-property (property)
  9134. "In the current entry, delete PROPERTY."
  9135. (interactive
  9136. (let* ((completion-ignore-case t)
  9137. (prop (completing-read
  9138. "Property: " (org-entry-properties nil 'standard))))
  9139. (list prop)))
  9140. (message "Property %s %s" property
  9141. (if (org-entry-delete nil property)
  9142. "deleted"
  9143. "was not present in the entry")))
  9144. (defun org-delete-property-globally (property)
  9145. "Remove PROPERTY globally, from all entries."
  9146. (interactive
  9147. (let* ((completion-ignore-case t)
  9148. (prop (completing-read
  9149. "Globally remove property: "
  9150. (mapcar 'list (org-buffer-property-keys)))))
  9151. (list prop)))
  9152. (save-excursion
  9153. (save-restriction
  9154. (widen)
  9155. (goto-char (point-min))
  9156. (let ((cnt 0))
  9157. (while (re-search-forward
  9158. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9159. nil t)
  9160. (setq cnt (1+ cnt))
  9161. (replace-match ""))
  9162. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9163. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9164. (defun org-compute-property-at-point ()
  9165. "Compute the property at point.
  9166. This looks for an enclosing column format, extracts the operator and
  9167. then applies it to the proerty in the column format's scope."
  9168. (interactive)
  9169. (unless (org-at-property-p)
  9170. (error "Not at a property"))
  9171. (let ((prop (org-match-string-no-properties 2)))
  9172. (org-columns-get-format-and-top-level)
  9173. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9174. (error "No operator defined for property %s" prop))
  9175. (org-columns-compute prop)))
  9176. (defun org-property-get-allowed-values (pom property &optional table)
  9177. "Get allowed values for the property PROPERTY.
  9178. When TABLE is non-nil, return an alist that can directly be used for
  9179. completion."
  9180. (let (vals)
  9181. (cond
  9182. ((equal property "TODO")
  9183. (setq vals (org-with-point-at pom
  9184. (append org-todo-keywords-1 '("")))))
  9185. ((equal property "PRIORITY")
  9186. (let ((n org-lowest-priority))
  9187. (while (>= n org-highest-priority)
  9188. (push (char-to-string n) vals)
  9189. (setq n (1- n)))))
  9190. ((member property org-special-properties))
  9191. (t
  9192. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9193. (when (and vals (string-match "\\S-" vals))
  9194. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9195. (setq vals (mapcar (lambda (x)
  9196. (cond ((stringp x) x)
  9197. ((numberp x) (number-to-string x))
  9198. ((symbolp x) (symbol-name x))
  9199. (t "???")))
  9200. vals)))))
  9201. (if table (mapcar 'list vals) vals)))
  9202. (defun org-property-previous-allowed-value (&optional previous)
  9203. "Switch to the next allowed value for this property."
  9204. (interactive)
  9205. (org-property-next-allowed-value t))
  9206. (defun org-property-next-allowed-value (&optional previous)
  9207. "Switch to the next allowed value for this property."
  9208. (interactive)
  9209. (unless (org-at-property-p)
  9210. (error "Not at a property"))
  9211. (let* ((key (match-string 2))
  9212. (value (match-string 3))
  9213. (allowed (or (org-property-get-allowed-values (point) key)
  9214. (and (member value '("[ ]" "[-]" "[X]"))
  9215. '("[ ]" "[X]"))))
  9216. nval)
  9217. (unless allowed
  9218. (error "Allowed values for this property have not been defined"))
  9219. (if previous (setq allowed (reverse allowed)))
  9220. (if (member value allowed)
  9221. (setq nval (car (cdr (member value allowed)))))
  9222. (setq nval (or nval (car allowed)))
  9223. (if (equal nval value)
  9224. (error "Only one allowed value for this property"))
  9225. (org-at-property-p)
  9226. (replace-match (concat " :" key ": " nval) t t)
  9227. (org-indent-line-function)
  9228. (beginning-of-line 1)
  9229. (skip-chars-forward " \t")))
  9230. (defun org-find-entry-with-id (ident)
  9231. "Locate the entry that contains the ID property with exact value IDENT.
  9232. IDENT can be a string, a symbol or a number, this function will search for
  9233. the string representation of it.
  9234. Return the position where this entry starts, or nil if there is no such entry."
  9235. (let ((id (cond
  9236. ((stringp ident) ident)
  9237. ((symbol-name ident) (symbol-name ident))
  9238. ((numberp ident) (number-to-string ident))
  9239. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9240. (case-fold-search nil))
  9241. (save-excursion
  9242. (save-restriction
  9243. (widen)
  9244. (goto-char (point-min))
  9245. (when (re-search-forward
  9246. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9247. nil t)
  9248. (org-back-to-heading)
  9249. (point))))))
  9250. ;;;; Timestamps
  9251. (defvar org-last-changed-timestamp nil)
  9252. (defvar org-time-was-given) ; dynamically scoped parameter
  9253. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9254. (defvar org-ts-what) ; dynamically scoped parameter
  9255. (defun org-time-stamp (arg)
  9256. "Prompt for a date/time and insert a time stamp.
  9257. If the user specifies a time like HH:MM, or if this command is called
  9258. with a prefix argument, the time stamp will contain date and time.
  9259. Otherwise, only the date will be included. All parts of a date not
  9260. specified by the user will be filled in from the current date/time.
  9261. So if you press just return without typing anything, the time stamp
  9262. will represent the current date/time. If there is already a timestamp
  9263. at the cursor, it will be modified."
  9264. (interactive "P")
  9265. (let* ((ts nil)
  9266. (default-time
  9267. ;; Default time is either today, or, when entering a range,
  9268. ;; the range start.
  9269. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9270. (save-excursion
  9271. (re-search-backward
  9272. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9273. (- (point) 20) t)))
  9274. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9275. (current-time)))
  9276. (default-input (and ts (org-get-compact-tod ts)))
  9277. org-time-was-given org-end-time-was-given time)
  9278. (cond
  9279. ((and (org-at-timestamp-p)
  9280. (eq last-command 'org-time-stamp)
  9281. (eq this-command 'org-time-stamp))
  9282. (insert "--")
  9283. (setq time (let ((this-command this-command))
  9284. (org-read-date arg 'totime nil nil default-time default-input)))
  9285. (org-insert-time-stamp time (or org-time-was-given arg)))
  9286. ((org-at-timestamp-p)
  9287. (setq time (let ((this-command this-command))
  9288. (org-read-date arg 'totime nil nil default-time default-input)))
  9289. (when (org-at-timestamp-p) ; just to get the match data
  9290. (replace-match "")
  9291. (setq org-last-changed-timestamp
  9292. (org-insert-time-stamp
  9293. time (or org-time-was-given arg)
  9294. nil nil nil (list org-end-time-was-given))))
  9295. (message "Timestamp updated"))
  9296. (t
  9297. (setq time (let ((this-command this-command))
  9298. (org-read-date arg 'totime nil nil default-time default-input)))
  9299. (org-insert-time-stamp time (or org-time-was-given arg)
  9300. nil nil nil (list org-end-time-was-given))))))
  9301. ;; FIXME: can we use this for something else, like computing time differences?
  9302. (defun org-get-compact-tod (s)
  9303. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9304. (let* ((t1 (match-string 1 s))
  9305. (h1 (string-to-number (match-string 2 s)))
  9306. (m1 (string-to-number (match-string 3 s)))
  9307. (t2 (and (match-end 4) (match-string 5 s)))
  9308. (h2 (and t2 (string-to-number (match-string 6 s))))
  9309. (m2 (and t2 (string-to-number (match-string 7 s))))
  9310. dh dm)
  9311. (if (not t2)
  9312. t1
  9313. (setq dh (- h2 h1) dm (- m2 m1))
  9314. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9315. (concat t1 "+" (number-to-string dh)
  9316. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9317. (defun org-time-stamp-inactive (&optional arg)
  9318. "Insert an inactive time stamp.
  9319. An inactive time stamp is enclosed in square brackets instead of angle
  9320. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9321. does not link to the calendar and cannot be changed with the S-cursor keys.
  9322. So these are more for recording a certain time/date."
  9323. (interactive "P")
  9324. (let (org-time-was-given org-end-time-was-given time)
  9325. (setq time (org-read-date arg 'totime))
  9326. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9327. nil nil (list org-end-time-was-given))))
  9328. (defvar org-date-ovl (org-make-overlay 1 1))
  9329. (org-overlay-put org-date-ovl 'face 'org-warning)
  9330. (org-detach-overlay org-date-ovl)
  9331. (defvar org-ans1) ; dynamically scoped parameter
  9332. (defvar org-ans2) ; dynamically scoped parameter
  9333. (defvar org-plain-time-of-day-regexp) ; defined below
  9334. (defvar org-read-date-overlay nil)
  9335. (defvar org-dcst nil) ; dynamically scoped
  9336. (defun org-read-date (&optional with-time to-time from-string prompt
  9337. default-time default-input)
  9338. "Read a date, possibly a time, and make things smooth for the user.
  9339. The prompt will suggest to enter an ISO date, but you can also enter anything
  9340. which will at least partially be understood by `parse-time-string'.
  9341. Unrecognized parts of the date will default to the current day, month, year,
  9342. hour and minute. If this command is called to replace a timestamp at point,
  9343. of to enter the second timestamp of a range, the default time is taken from the
  9344. existing stamp. For example,
  9345. 3-2-5 --> 2003-02-05
  9346. feb 15 --> currentyear-02-15
  9347. sep 12 9 --> 2009-09-12
  9348. 12:45 --> today 12:45
  9349. 22 sept 0:34 --> currentyear-09-22 0:34
  9350. 12 --> currentyear-currentmonth-12
  9351. Fri --> nearest Friday (today or later)
  9352. etc.
  9353. Furthermore you can specify a relative date by giving, as the *first* thing
  9354. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9355. change in days weeks, months, years.
  9356. With a single plus or minus, the date is relative to today. With a double
  9357. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9358. +4d --> four days from today
  9359. +4 --> same as above
  9360. +2w --> two weeks from today
  9361. ++5 --> five days from default date
  9362. The function understands only English month and weekday abbreviations,
  9363. but this can be configured with the variables `parse-time-months' and
  9364. `parse-time-weekdays'.
  9365. While prompting, a calendar is popped up - you can also select the
  9366. date with the mouse (button 1). The calendar shows a period of three
  9367. months. To scroll it to other months, use the keys `>' and `<'.
  9368. If you don't like the calendar, turn it off with
  9369. \(setq org-read-date-popup-calendar nil)
  9370. With optional argument TO-TIME, the date will immediately be converted
  9371. to an internal time.
  9372. With an optional argument WITH-TIME, the prompt will suggest to also
  9373. insert a time. Note that when WITH-TIME is not set, you can still
  9374. enter a time, and this function will inform the calling routine about
  9375. this change. The calling routine may then choose to change the format
  9376. used to insert the time stamp into the buffer to include the time.
  9377. With optional argument FROM-STRING, read from this string instead from
  9378. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9379. the time/date that is used for everything that is not specified by the
  9380. user."
  9381. (require 'parse-time)
  9382. (let* ((org-time-stamp-rounding-minutes
  9383. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9384. (org-dcst org-display-custom-times)
  9385. (ct (org-current-time))
  9386. (def (or default-time ct))
  9387. (defdecode (decode-time def))
  9388. (dummy (progn
  9389. (when (< (nth 2 defdecode) org-extend-today-until)
  9390. (setcar (nthcdr 2 defdecode) -1)
  9391. (setcar (nthcdr 1 defdecode) 59)
  9392. (setq def (apply 'encode-time defdecode)
  9393. defdecode (decode-time def)))))
  9394. (calendar-move-hook nil)
  9395. (calendar-view-diary-initially-flag nil)
  9396. (view-diary-entries-initially nil)
  9397. (calendar-view-holidays-initially-flag nil)
  9398. (view-calendar-holidays-initially nil)
  9399. (timestr (format-time-string
  9400. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9401. (prompt (concat (if prompt (concat prompt " ") "")
  9402. (format "Date+time [%s]: " timestr)))
  9403. ans (org-ans0 "") org-ans1 org-ans2 final)
  9404. (cond
  9405. (from-string (setq ans from-string))
  9406. (org-read-date-popup-calendar
  9407. (save-excursion
  9408. (save-window-excursion
  9409. (calendar)
  9410. (calendar-forward-day (- (time-to-days def)
  9411. (calendar-absolute-from-gregorian
  9412. (calendar-current-date))))
  9413. (org-eval-in-calendar nil t)
  9414. (let* ((old-map (current-local-map))
  9415. (map (copy-keymap calendar-mode-map))
  9416. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9417. (org-defkey map (kbd "RET") 'org-calendar-select)
  9418. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9419. 'org-calendar-select-mouse)
  9420. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9421. 'org-calendar-select-mouse)
  9422. (org-defkey minibuffer-local-map [(meta shift left)]
  9423. (lambda () (interactive)
  9424. (org-eval-in-calendar '(calendar-backward-month 1))))
  9425. (org-defkey minibuffer-local-map [(meta shift right)]
  9426. (lambda () (interactive)
  9427. (org-eval-in-calendar '(calendar-forward-month 1))))
  9428. (org-defkey minibuffer-local-map [(meta shift up)]
  9429. (lambda () (interactive)
  9430. (org-eval-in-calendar '(calendar-backward-year 1))))
  9431. (org-defkey minibuffer-local-map [(meta shift down)]
  9432. (lambda () (interactive)
  9433. (org-eval-in-calendar '(calendar-forward-year 1))))
  9434. (org-defkey minibuffer-local-map [(shift up)]
  9435. (lambda () (interactive)
  9436. (org-eval-in-calendar '(calendar-backward-week 1))))
  9437. (org-defkey minibuffer-local-map [(shift down)]
  9438. (lambda () (interactive)
  9439. (org-eval-in-calendar '(calendar-forward-week 1))))
  9440. (org-defkey minibuffer-local-map [(shift left)]
  9441. (lambda () (interactive)
  9442. (org-eval-in-calendar '(calendar-backward-day 1))))
  9443. (org-defkey minibuffer-local-map [(shift right)]
  9444. (lambda () (interactive)
  9445. (org-eval-in-calendar '(calendar-forward-day 1))))
  9446. (org-defkey minibuffer-local-map ">"
  9447. (lambda () (interactive)
  9448. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9449. (org-defkey minibuffer-local-map "<"
  9450. (lambda () (interactive)
  9451. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9452. (unwind-protect
  9453. (progn
  9454. (use-local-map map)
  9455. (add-hook 'post-command-hook 'org-read-date-display)
  9456. (setq org-ans0 (read-string prompt default-input nil nil))
  9457. ;; org-ans0: from prompt
  9458. ;; org-ans1: from mouse click
  9459. ;; org-ans2: from calendar motion
  9460. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9461. (remove-hook 'post-command-hook 'org-read-date-display)
  9462. (use-local-map old-map)
  9463. (when org-read-date-overlay
  9464. (org-delete-overlay org-read-date-overlay)
  9465. (setq org-read-date-overlay nil)))))))
  9466. (t ; Naked prompt only
  9467. (unwind-protect
  9468. (setq ans (read-string prompt default-input nil timestr))
  9469. (when org-read-date-overlay
  9470. (org-delete-overlay org-read-date-overlay)
  9471. (setq org-read-date-overlay nil)))))
  9472. (setq final (org-read-date-analyze ans def defdecode))
  9473. (if to-time
  9474. (apply 'encode-time final)
  9475. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9476. (format "%04d-%02d-%02d %02d:%02d"
  9477. (nth 5 final) (nth 4 final) (nth 3 final)
  9478. (nth 2 final) (nth 1 final))
  9479. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9480. (defvar def)
  9481. (defvar defdecode)
  9482. (defvar with-time)
  9483. (defun org-read-date-display ()
  9484. "Display the currrent date prompt interpretation in the minibuffer."
  9485. (when org-read-date-display-live
  9486. (when org-read-date-overlay
  9487. (org-delete-overlay org-read-date-overlay))
  9488. (let ((p (point)))
  9489. (end-of-line 1)
  9490. (while (not (equal (buffer-substring
  9491. (max (point-min) (- (point) 4)) (point))
  9492. " "))
  9493. (insert " "))
  9494. (goto-char p))
  9495. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9496. " " (or org-ans1 org-ans2)))
  9497. (org-end-time-was-given nil)
  9498. (f (org-read-date-analyze ans def defdecode))
  9499. (fmts (if org-dcst
  9500. org-time-stamp-custom-formats
  9501. org-time-stamp-formats))
  9502. (fmt (if (or with-time
  9503. (and (boundp 'org-time-was-given) org-time-was-given))
  9504. (cdr fmts)
  9505. (car fmts)))
  9506. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9507. (when (and org-end-time-was-given
  9508. (string-match org-plain-time-of-day-regexp txt))
  9509. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9510. org-end-time-was-given
  9511. (substring txt (match-end 0)))))
  9512. (setq org-read-date-overlay
  9513. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  9514. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9515. (defun org-read-date-analyze (ans def defdecode)
  9516. "Analyze the combined answer of the date prompt."
  9517. ;; FIXME: cleanup and comment
  9518. (let (delta deltan deltaw deltadef year month day
  9519. hour minute second wday pm h2 m2 tl wday1
  9520. iso-year iso-weekday iso-week iso-year iso-date)
  9521. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9522. (setq ans "+0"))
  9523. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9524. (setq ans (replace-match "" t t ans)
  9525. deltan (car delta)
  9526. deltaw (nth 1 delta)
  9527. deltadef (nth 2 delta)))
  9528. ;; Check if there is an iso week date in there
  9529. ;; If yes, sore the info and ostpone interpreting it until the rest
  9530. ;; of the parsing is done
  9531. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9532. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9533. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9534. iso-week (string-to-number (match-string 2 ans)))
  9535. (setq ans (replace-match "" t t ans)))
  9536. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9537. (when (string-match
  9538. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9539. (setq year (if (match-end 2)
  9540. (string-to-number (match-string 2 ans))
  9541. (string-to-number (format-time-string "%Y")))
  9542. month (string-to-number (match-string 3 ans))
  9543. day (string-to-number (match-string 4 ans)))
  9544. (if (< year 100) (setq year (+ 2000 year)))
  9545. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9546. t nil ans)))
  9547. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9548. ;; If there is a time with am/pm, and *no* time without it, we convert
  9549. ;; so that matching will be successful.
  9550. (loop for i from 1 to 2 do ; twice, for end time as well
  9551. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9552. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9553. (setq hour (string-to-number (match-string 1 ans))
  9554. minute (if (match-end 3)
  9555. (string-to-number (match-string 3 ans))
  9556. 0)
  9557. pm (equal ?p
  9558. (string-to-char (downcase (match-string 4 ans)))))
  9559. (if (and (= hour 12) (not pm))
  9560. (setq hour 0)
  9561. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9562. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9563. t t ans))))
  9564. ;; Check if a time range is given as a duration
  9565. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9566. (setq hour (string-to-number (match-string 1 ans))
  9567. h2 (+ hour (string-to-number (match-string 3 ans)))
  9568. minute (string-to-number (match-string 2 ans))
  9569. m2 (+ minute (if (match-end 5) (string-to-number
  9570. (match-string 5 ans))0)))
  9571. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9572. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9573. t t ans)))
  9574. ;; Check if there is a time range
  9575. (when (boundp 'org-end-time-was-given)
  9576. (setq org-time-was-given nil)
  9577. (when (and (string-match org-plain-time-of-day-regexp ans)
  9578. (match-end 8))
  9579. (setq org-end-time-was-given (match-string 8 ans))
  9580. (setq ans (concat (substring ans 0 (match-beginning 7))
  9581. (substring ans (match-end 7))))))
  9582. (setq tl (parse-time-string ans)
  9583. day (or (nth 3 tl) (nth 3 defdecode))
  9584. month (or (nth 4 tl)
  9585. (if (and org-read-date-prefer-future
  9586. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9587. (1+ (nth 4 defdecode))
  9588. (nth 4 defdecode)))
  9589. year (or (nth 5 tl)
  9590. (if (and org-read-date-prefer-future
  9591. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9592. (1+ (nth 5 defdecode))
  9593. (nth 5 defdecode)))
  9594. hour (or (nth 2 tl) (nth 2 defdecode))
  9595. minute (or (nth 1 tl) (nth 1 defdecode))
  9596. second (or (nth 0 tl) 0)
  9597. wday (nth 6 tl))
  9598. ;; Special date definitions below
  9599. (cond
  9600. (iso-week
  9601. ;; There was an iso week
  9602. (setq year (or iso-year year)
  9603. day (or iso-weekday wday 1)
  9604. wday nil ; to make sure that the trigger below does not match
  9605. iso-date (calendar-gregorian-from-absolute
  9606. (calendar-absolute-from-iso
  9607. (list iso-week day year))))
  9608. ; FIXME: Should we also push ISO weeks into the future?
  9609. ; (when (and org-read-date-prefer-future
  9610. ; (not iso-year)
  9611. ; (< (calendar-absolute-from-gregorian iso-date)
  9612. ; (time-to-days (current-time))))
  9613. ; (setq year (1+ year)
  9614. ; iso-date (calendar-gregorian-from-absolute
  9615. ; (calendar-absolute-from-iso
  9616. ; (list iso-week day year)))))
  9617. (setq month (car iso-date)
  9618. year (nth 2 iso-date)
  9619. day (nth 1 iso-date)))
  9620. (deltan
  9621. (unless deltadef
  9622. (let ((now (decode-time (current-time))))
  9623. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9624. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9625. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9626. ((equal deltaw "m") (setq month (+ month deltan)))
  9627. ((equal deltaw "y") (setq year (+ year deltan)))))
  9628. ((and wday (not (nth 3 tl)))
  9629. ;; Weekday was given, but no day, so pick that day in the week
  9630. ;; on or after the derived date.
  9631. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9632. (unless (equal wday wday1)
  9633. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9634. (if (and (boundp 'org-time-was-given)
  9635. (nth 2 tl))
  9636. (setq org-time-was-given t))
  9637. (if (< year 100) (setq year (+ 2000 year)))
  9638. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9639. (list second minute hour day month year)))
  9640. (defvar parse-time-weekdays)
  9641. (defun org-read-date-get-relative (s today default)
  9642. "Check string S for special relative date string.
  9643. TODAY and DEFAULT are internal times, for today and for a default.
  9644. Return shift list (N what def-flag)
  9645. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9646. N is the number of WHATs to shift.
  9647. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9648. the DEFAULT date rather than TODAY."
  9649. (when (and
  9650. (string-match
  9651. (concat
  9652. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9653. "\\([0-9]+\\)?"
  9654. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9655. "\\([ \t]\\|$\\)") s)
  9656. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  9657. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  9658. (string-to-char (substring (match-string 1 s) -1))
  9659. ?+))
  9660. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9661. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9662. (what (if (match-end 3) (match-string 3 s) "d"))
  9663. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9664. (date (if rel default today))
  9665. (wday (nth 6 (decode-time date)))
  9666. delta)
  9667. (if wday1
  9668. (progn
  9669. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9670. (if (= dir ?-) (setq delta (- delta 7)))
  9671. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9672. (list delta "d" rel))
  9673. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9674. (defun org-eval-in-calendar (form &optional keepdate)
  9675. "Eval FORM in the calendar window and return to current window.
  9676. Also, store the cursor date in variable org-ans2."
  9677. (let ((sw (selected-window)))
  9678. (select-window (get-buffer-window "*Calendar*"))
  9679. (eval form)
  9680. (when (and (not keepdate) (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-ans2 (format-time-string "%Y-%m-%d" time))))
  9684. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9685. (select-window sw)))
  9686. ; ;; Update the prompt to show new default date
  9687. ; (save-excursion
  9688. ; (goto-char (point-min))
  9689. ; (when (and org-ans2
  9690. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  9691. ; (get-text-property (match-end 0) 'field))
  9692. ; (let ((inhibit-read-only t))
  9693. ; (replace-match (concat "[" org-ans2 "]") t t)
  9694. ; (add-text-properties (point-min) (1+ (match-end 0))
  9695. ; (text-properties-at (1+ (point-min)))))))))
  9696. (defun org-calendar-select ()
  9697. "Return to `org-read-date' with the date currently selected.
  9698. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9699. (interactive)
  9700. (when (calendar-cursor-to-date)
  9701. (let* ((date (calendar-cursor-to-date))
  9702. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9703. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9704. (if (active-minibuffer-window) (exit-minibuffer))))
  9705. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9706. "Insert a date stamp for the date given by the internal TIME.
  9707. WITH-HM means, use the stamp format that includes the time of the day.
  9708. INACTIVE means use square brackets instead of angular ones, so that the
  9709. stamp will not contribute to the agenda.
  9710. PRE and POST are optional strings to be inserted before and after the
  9711. stamp.
  9712. The command returns the inserted time stamp."
  9713. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9714. stamp)
  9715. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9716. (insert-before-markers (or pre ""))
  9717. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9718. (when (listp extra)
  9719. (setq extra (car extra))
  9720. (if (and (stringp extra)
  9721. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9722. (setq extra (format "-%02d:%02d"
  9723. (string-to-number (match-string 1 extra))
  9724. (string-to-number (match-string 2 extra))))
  9725. (setq extra nil)))
  9726. (when extra
  9727. (backward-char 1)
  9728. (insert-before-markers extra)
  9729. (forward-char 1))
  9730. (insert-before-markers (or post ""))
  9731. stamp))
  9732. (defun org-toggle-time-stamp-overlays ()
  9733. "Toggle the use of custom time stamp formats."
  9734. (interactive)
  9735. (setq org-display-custom-times (not org-display-custom-times))
  9736. (unless org-display-custom-times
  9737. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9738. (while (setq p (next-single-property-change p 'display))
  9739. (if (and (get-text-property p 'display)
  9740. (eq (get-text-property p 'face) 'org-date))
  9741. (remove-text-properties
  9742. p (setq p (next-single-property-change p 'display))
  9743. '(display t))))
  9744. (set-buffer-modified-p bmp)))
  9745. (if (featurep 'xemacs)
  9746. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9747. (org-restart-font-lock)
  9748. (setq org-table-may-need-update t)
  9749. (if org-display-custom-times
  9750. (message "Time stamps are overlayed with custom format")
  9751. (message "Time stamp overlays removed")))
  9752. (defun org-display-custom-time (beg end)
  9753. "Overlay modified time stamp format over timestamp between BEG and END."
  9754. (let* ((ts (buffer-substring beg end))
  9755. t1 w1 with-hm tf time str w2 (off 0))
  9756. (save-match-data
  9757. (setq t1 (org-parse-time-string ts t))
  9758. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9759. (setq off (- (match-end 0) (match-beginning 0)))))
  9760. (setq end (- end off))
  9761. (setq w1 (- end beg)
  9762. with-hm (and (nth 1 t1) (nth 2 t1))
  9763. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9764. time (org-fix-decoded-time t1)
  9765. str (org-add-props
  9766. (format-time-string
  9767. (substring tf 1 -1) (apply 'encode-time time))
  9768. nil 'mouse-face 'highlight)
  9769. w2 (length str))
  9770. (if (not (= w2 w1))
  9771. (add-text-properties (1+ beg) (+ 2 beg)
  9772. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9773. (if (featurep 'xemacs)
  9774. (progn
  9775. (put-text-property beg end 'invisible t)
  9776. (put-text-property beg end 'end-glyph (make-glyph str)))
  9777. (put-text-property beg end 'display str))))
  9778. (defun org-translate-time (string)
  9779. "Translate all timestamps in STRING to custom format.
  9780. But do this only if the variable `org-display-custom-times' is set."
  9781. (when org-display-custom-times
  9782. (save-match-data
  9783. (let* ((start 0)
  9784. (re org-ts-regexp-both)
  9785. t1 with-hm inactive tf time str beg end)
  9786. (while (setq start (string-match re string start))
  9787. (setq beg (match-beginning 0)
  9788. end (match-end 0)
  9789. t1 (save-match-data
  9790. (org-parse-time-string (substring string beg end) t))
  9791. with-hm (and (nth 1 t1) (nth 2 t1))
  9792. inactive (equal (substring string beg (1+ beg)) "[")
  9793. tf (funcall (if with-hm 'cdr 'car)
  9794. org-time-stamp-custom-formats)
  9795. time (org-fix-decoded-time t1)
  9796. str (format-time-string
  9797. (concat
  9798. (if inactive "[" "<") (substring tf 1 -1)
  9799. (if inactive "]" ">"))
  9800. (apply 'encode-time time))
  9801. string (replace-match str t t string)
  9802. start (+ start (length str)))))))
  9803. string)
  9804. (defun org-fix-decoded-time (time)
  9805. "Set 0 instead of nil for the first 6 elements of time.
  9806. Don't touch the rest."
  9807. (let ((n 0))
  9808. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9809. (defun org-days-to-time (timestamp-string)
  9810. "Difference between TIMESTAMP-STRING and now in days."
  9811. (- (time-to-days (org-time-string-to-time timestamp-string))
  9812. (time-to-days (current-time))))
  9813. (defun org-deadline-close (timestamp-string &optional ndays)
  9814. "Is the time in TIMESTAMP-STRING close to the current date?"
  9815. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9816. (and (< (org-days-to-time timestamp-string) ndays)
  9817. (not (org-entry-is-done-p))))
  9818. (defun org-get-wdays (ts)
  9819. "Get the deadline lead time appropriate for timestring TS."
  9820. (cond
  9821. ((<= org-deadline-warning-days 0)
  9822. ;; 0 or negative, enforce this value no matter what
  9823. (- org-deadline-warning-days))
  9824. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9825. ;; lead time is specified.
  9826. (floor (* (string-to-number (match-string 1 ts))
  9827. (cdr (assoc (match-string 2 ts)
  9828. '(("d" . 1) ("w" . 7)
  9829. ("m" . 30.4) ("y" . 365.25)))))))
  9830. ;; go for the default.
  9831. (t org-deadline-warning-days)))
  9832. (defun org-calendar-select-mouse (ev)
  9833. "Return to `org-read-date' with the date currently selected.
  9834. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9835. (interactive "e")
  9836. (mouse-set-point ev)
  9837. (when (calendar-cursor-to-date)
  9838. (let* ((date (calendar-cursor-to-date))
  9839. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9840. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9841. (if (active-minibuffer-window) (exit-minibuffer))))
  9842. (defun org-check-deadlines (ndays)
  9843. "Check if there are any deadlines due or past due.
  9844. A deadline is considered due if it happens within `org-deadline-warning-days'
  9845. days from today's date. If the deadline appears in an entry marked DONE,
  9846. it is not shown. The prefix arg NDAYS can be used to test that many
  9847. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  9848. (interactive "P")
  9849. (let* ((org-warn-days
  9850. (cond
  9851. ((equal ndays '(4)) 100000)
  9852. (ndays (prefix-numeric-value ndays))
  9853. (t (abs org-deadline-warning-days))))
  9854. (case-fold-search nil)
  9855. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  9856. (callback
  9857. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  9858. (message "%d deadlines past-due or due within %d days"
  9859. (org-occur regexp nil callback)
  9860. org-warn-days)))
  9861. (defun org-check-before-date (date)
  9862. "Check if there are deadlines or scheduled entries before DATE."
  9863. (interactive (list (org-read-date)))
  9864. (let ((case-fold-search nil)
  9865. (regexp (concat "\\<\\(" org-deadline-string
  9866. "\\|" org-scheduled-string
  9867. "\\) *<\\([^>]+\\)>"))
  9868. (callback
  9869. (lambda () (time-less-p
  9870. (org-time-string-to-time (match-string 2))
  9871. (org-time-string-to-time date)))))
  9872. (message "%d entries before %s"
  9873. (org-occur regexp nil callback) date)))
  9874. (defun org-evaluate-time-range (&optional to-buffer)
  9875. "Evaluate a time range by computing the difference between start and end.
  9876. Normally the result is just printed in the echo area, but with prefix arg
  9877. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  9878. If the time range is actually in a table, the result is inserted into the
  9879. next column.
  9880. For time difference computation, a year is assumed to be exactly 365
  9881. days in order to avoid rounding problems."
  9882. (interactive "P")
  9883. (or
  9884. (org-clock-update-time-maybe)
  9885. (save-excursion
  9886. (unless (org-at-date-range-p t)
  9887. (goto-char (point-at-bol))
  9888. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  9889. (if (not (org-at-date-range-p t))
  9890. (error "Not at a time-stamp range, and none found in current line")))
  9891. (let* ((ts1 (match-string 1))
  9892. (ts2 (match-string 2))
  9893. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  9894. (match-end (match-end 0))
  9895. (time1 (org-time-string-to-time ts1))
  9896. (time2 (org-time-string-to-time ts2))
  9897. (t1 (time-to-seconds time1))
  9898. (t2 (time-to-seconds time2))
  9899. (diff (abs (- t2 t1)))
  9900. (negative (< (- t2 t1) 0))
  9901. ;; (ys (floor (* 365 24 60 60)))
  9902. (ds (* 24 60 60))
  9903. (hs (* 60 60))
  9904. (fy "%dy %dd %02d:%02d")
  9905. (fy1 "%dy %dd")
  9906. (fd "%dd %02d:%02d")
  9907. (fd1 "%dd")
  9908. (fh "%02d:%02d")
  9909. y d h m align)
  9910. (if havetime
  9911. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9912. y 0
  9913. d (floor (/ diff ds)) diff (mod diff ds)
  9914. h (floor (/ diff hs)) diff (mod diff hs)
  9915. m (floor (/ diff 60)))
  9916. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9917. y 0
  9918. d (floor (+ (/ diff ds) 0.5))
  9919. h 0 m 0))
  9920. (if (not to-buffer)
  9921. (message "%s" (org-make-tdiff-string y d h m))
  9922. (if (org-at-table-p)
  9923. (progn
  9924. (goto-char match-end)
  9925. (setq align t)
  9926. (and (looking-at " *|") (goto-char (match-end 0))))
  9927. (goto-char match-end))
  9928. (if (looking-at
  9929. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  9930. (replace-match ""))
  9931. (if negative (insert " -"))
  9932. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  9933. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  9934. (insert " " (format fh h m))))
  9935. (if align (org-table-align))
  9936. (message "Time difference inserted")))))
  9937. (defun org-make-tdiff-string (y d h m)
  9938. (let ((fmt "")
  9939. (l nil))
  9940. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  9941. l (push y l)))
  9942. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  9943. l (push d l)))
  9944. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  9945. l (push h l)))
  9946. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  9947. l (push m l)))
  9948. (apply 'format fmt (nreverse l))))
  9949. (defun org-time-string-to-time (s)
  9950. (apply 'encode-time (org-parse-time-string s)))
  9951. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  9952. "Convert a time stamp to an absolute day number.
  9953. If there is a specifyer for a cyclic time stamp, get the closest date to
  9954. DAYNR.
  9955. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  9956. (cond
  9957. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  9958. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  9959. daynr
  9960. (+ daynr 1000)))
  9961. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  9962. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  9963. (time-to-days (current-time))) (match-string 0 s)
  9964. prefer show-all))
  9965. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  9966. (defun org-days-to-iso-week (days)
  9967. "Return the iso week number."
  9968. (require 'cal-iso)
  9969. (car (calendar-iso-from-absolute days)))
  9970. (defun org-small-year-to-year (year)
  9971. "Convert 2-digit years into 4-digit years.
  9972. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  9973. The year 2000 cannot be abbreviated. Any year lager than 99
  9974. is retrned unchanged."
  9975. (if (< year 38)
  9976. (setq year (+ 2000 year))
  9977. (if (< year 100)
  9978. (setq year (+ 1900 year))))
  9979. year)
  9980. (defun org-time-from-absolute (d)
  9981. "Return the time corresponding to date D.
  9982. D may be an absolute day number, or a calendar-type list (month day year)."
  9983. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  9984. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  9985. (defun org-calendar-holiday ()
  9986. "List of holidays, for Diary display in Org-mode."
  9987. (require 'holidays)
  9988. (let ((hl (funcall
  9989. (if (fboundp 'calendar-check-holidays)
  9990. 'calendar-check-holidays 'check-calendar-holidays) date)))
  9991. (if hl (mapconcat 'identity hl "; "))))
  9992. (defun org-diary-sexp-entry (sexp entry date)
  9993. "Process a SEXP diary ENTRY for DATE."
  9994. (require 'diary-lib)
  9995. (let ((result (if calendar-debug-sexp
  9996. (let ((stack-trace-on-error t))
  9997. (eval (car (read-from-string sexp))))
  9998. (condition-case nil
  9999. (eval (car (read-from-string sexp)))
  10000. (error
  10001. (beep)
  10002. (message "Bad sexp at line %d in %s: %s"
  10003. (org-current-line)
  10004. (buffer-file-name) sexp)
  10005. (sleep-for 2))))))
  10006. (cond ((stringp result) result)
  10007. ((and (consp result)
  10008. (stringp (cdr result))) (cdr result))
  10009. (result entry)
  10010. (t nil))))
  10011. (defun org-diary-to-ical-string (frombuf)
  10012. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10013. This uses the icalendar.el library."
  10014. (let* ((tmpdir (if (featurep 'xemacs)
  10015. (temp-directory)
  10016. temporary-file-directory))
  10017. (tmpfile (make-temp-name
  10018. (expand-file-name "orgics" tmpdir)))
  10019. buf rtn b e)
  10020. (save-excursion
  10021. (set-buffer frombuf)
  10022. (icalendar-export-region (point-min) (point-max) tmpfile)
  10023. (setq buf (find-buffer-visiting tmpfile))
  10024. (set-buffer buf)
  10025. (goto-char (point-min))
  10026. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10027. (setq b (match-beginning 0)))
  10028. (goto-char (point-max))
  10029. (if (re-search-backward "^END:VEVENT" nil t)
  10030. (setq e (match-end 0)))
  10031. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10032. (kill-buffer buf)
  10033. (kill-buffer frombuf)
  10034. (delete-file tmpfile)
  10035. rtn))
  10036. (defun org-closest-date (start current change prefer show-all)
  10037. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10038. When PREFER is `past' return a date that is either CURRENT or past.
  10039. When PREFER is `future', return a date that is either CURRENT or future.
  10040. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10041. ;; Make the proper lists from the dates
  10042. (catch 'exit
  10043. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10044. dn dw sday cday n1 n2
  10045. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10046. (setq start (org-date-to-gregorian start)
  10047. current (org-date-to-gregorian
  10048. (if show-all
  10049. current
  10050. (time-to-days (current-time))))
  10051. sday (calendar-absolute-from-gregorian start)
  10052. cday (calendar-absolute-from-gregorian current))
  10053. (if (<= cday sday) (throw 'exit sday))
  10054. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10055. (setq dn (string-to-number (match-string 1 change))
  10056. dw (cdr (assoc (match-string 2 change) a1)))
  10057. (error "Invalid change specifyer: %s" change))
  10058. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10059. (cond
  10060. ((eq dw 'day)
  10061. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10062. n2 (+ n1 dn)))
  10063. ((eq dw 'year)
  10064. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10065. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10066. (setq date1 (list m d y1)
  10067. n1 (calendar-absolute-from-gregorian date1)
  10068. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10069. n2 (calendar-absolute-from-gregorian date2)))
  10070. ((eq dw 'month)
  10071. ;; approx number of month between the tow dates
  10072. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10073. ;; How often does dn fit in there?
  10074. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10075. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10076. m (+ m nm)
  10077. ny (floor (/ m 12))
  10078. y (+ y ny)
  10079. m (- m (* ny 12)))
  10080. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10081. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10082. (setq m2 (+ m dn) y2 y)
  10083. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10084. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10085. (while (< n2 cday)
  10086. (setq n1 n2 m m2 y y2)
  10087. (setq m2 (+ m dn) y2 y)
  10088. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10089. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10090. (if show-all
  10091. (cond
  10092. ((eq prefer 'past) n1)
  10093. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10094. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10095. (cond
  10096. ((eq prefer 'past) n1)
  10097. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10098. (t (if (= cday n1) n1 n2)))))))
  10099. (defun org-date-to-gregorian (date)
  10100. "Turn any specification of DATE into a gregorian date for the calendar."
  10101. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10102. ((and (listp date) (= (length date) 3)) date)
  10103. ((stringp date)
  10104. (setq date (org-parse-time-string date))
  10105. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10106. ((listp date)
  10107. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10108. (defun org-parse-time-string (s &optional nodefault)
  10109. "Parse the standard Org-mode time string.
  10110. This should be a lot faster than the normal `parse-time-string'.
  10111. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10112. hour and minute fields will be nil if not given."
  10113. (if (string-match org-ts-regexp0 s)
  10114. (list 0
  10115. (if (or (match-beginning 8) (not nodefault))
  10116. (string-to-number (or (match-string 8 s) "0")))
  10117. (if (or (match-beginning 7) (not nodefault))
  10118. (string-to-number (or (match-string 7 s) "0")))
  10119. (string-to-number (match-string 4 s))
  10120. (string-to-number (match-string 3 s))
  10121. (string-to-number (match-string 2 s))
  10122. nil nil nil)
  10123. (make-list 9 0)))
  10124. (defun org-timestamp-up (&optional arg)
  10125. "Increase the date item at the cursor by one.
  10126. If the cursor is on the year, change the year. If it is on the month or
  10127. the day, change that.
  10128. With prefix ARG, change by that many units."
  10129. (interactive "p")
  10130. (org-timestamp-change (prefix-numeric-value arg)))
  10131. (defun org-timestamp-down (&optional arg)
  10132. "Decrease the date item at the cursor by one.
  10133. If the cursor is on the year, change the year. If it is on the month or
  10134. the day, change that.
  10135. With prefix ARG, change by that many units."
  10136. (interactive "p")
  10137. (org-timestamp-change (- (prefix-numeric-value arg))))
  10138. (defun org-timestamp-up-day (&optional arg)
  10139. "Increase the date in the time stamp by one day.
  10140. With prefix ARG, change that many days."
  10141. (interactive "p")
  10142. (if (and (not (org-at-timestamp-p t))
  10143. (org-on-heading-p))
  10144. (org-todo 'up)
  10145. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10146. (defun org-timestamp-down-day (&optional arg)
  10147. "Decrease the date in the time stamp by one day.
  10148. With prefix ARG, change that many days."
  10149. (interactive "p")
  10150. (if (and (not (org-at-timestamp-p t))
  10151. (org-on-heading-p))
  10152. (org-todo 'down)
  10153. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10154. (defun org-at-timestamp-p (&optional inactive-ok)
  10155. "Determine if the cursor is in or at a timestamp."
  10156. (interactive)
  10157. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10158. (pos (point))
  10159. (ans (or (looking-at tsr)
  10160. (save-excursion
  10161. (skip-chars-backward "^[<\n\r\t")
  10162. (if (> (point) (point-min)) (backward-char 1))
  10163. (and (looking-at tsr)
  10164. (> (- (match-end 0) pos) -1))))))
  10165. (and ans
  10166. (boundp 'org-ts-what)
  10167. (setq org-ts-what
  10168. (cond
  10169. ((= pos (match-beginning 0)) 'bracket)
  10170. ((= pos (1- (match-end 0))) 'bracket)
  10171. ((org-pos-in-match-range pos 2) 'year)
  10172. ((org-pos-in-match-range pos 3) 'month)
  10173. ((org-pos-in-match-range pos 7) 'hour)
  10174. ((org-pos-in-match-range pos 8) 'minute)
  10175. ((or (org-pos-in-match-range pos 4)
  10176. (org-pos-in-match-range pos 5)) 'day)
  10177. ((and (> pos (or (match-end 8) (match-end 5)))
  10178. (< pos (match-end 0)))
  10179. (- pos (or (match-end 8) (match-end 5))))
  10180. (t 'day))))
  10181. ans))
  10182. (defun org-toggle-timestamp-type ()
  10183. "Toggle the type (<active> or [inactive]) of a time stamp."
  10184. (interactive)
  10185. (when (org-at-timestamp-p t)
  10186. (save-excursion
  10187. (goto-char (match-beginning 0))
  10188. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10189. (goto-char (1- (match-end 0)))
  10190. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10191. (message "Timestamp is now %sactive"
  10192. (if (equal (char-before) ?>) "in" ""))))
  10193. (defun org-timestamp-change (n &optional what)
  10194. "Change the date in the time stamp at point.
  10195. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10196. `year', `minute', `second'. If WHAT is not given, the cursor position
  10197. in the timestamp determines what will be changed."
  10198. (let ((pos (point))
  10199. with-hm inactive
  10200. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10201. org-ts-what
  10202. extra rem
  10203. ts time time0)
  10204. (if (not (org-at-timestamp-p t))
  10205. (error "Not at a timestamp"))
  10206. (if (and (not what) (eq org-ts-what 'bracket))
  10207. (org-toggle-timestamp-type)
  10208. (if (and (not what) (not (eq org-ts-what 'day))
  10209. org-display-custom-times
  10210. (get-text-property (point) 'display)
  10211. (not (get-text-property (1- (point)) 'display)))
  10212. (setq org-ts-what 'day))
  10213. (setq org-ts-what (or what org-ts-what)
  10214. inactive (= (char-after (match-beginning 0)) ?\[)
  10215. ts (match-string 0))
  10216. (replace-match "")
  10217. (if (string-match
  10218. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10219. ts)
  10220. (setq extra (match-string 1 ts)))
  10221. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10222. (setq with-hm t))
  10223. (setq time0 (org-parse-time-string ts))
  10224. (when (and (eq org-ts-what 'minute)
  10225. (eq current-prefix-arg nil))
  10226. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10227. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10228. (setcar (cdr time0) (+ (nth 1 time0)
  10229. (if (> n 0) (- rem) (- dm rem))))))
  10230. (setq time
  10231. (encode-time (or (car time0) 0)
  10232. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10233. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10234. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10235. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10236. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10237. (nthcdr 6 time0)))
  10238. (when (integerp org-ts-what)
  10239. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10240. (if (eq what 'calendar)
  10241. (let ((cal-date (org-get-date-from-calendar)))
  10242. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10243. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10244. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10245. (setcar time0 (or (car time0) 0))
  10246. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10247. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10248. (setq time (apply 'encode-time time0))))
  10249. (setq org-last-changed-timestamp
  10250. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10251. (org-clock-update-time-maybe)
  10252. (goto-char pos)
  10253. ;; Try to recenter the calendar window, if any
  10254. (if (and org-calendar-follow-timestamp-change
  10255. (get-buffer-window "*Calendar*" t)
  10256. (memq org-ts-what '(day month year)))
  10257. (org-recenter-calendar (time-to-days time))))))
  10258. (defun org-modify-ts-extra (s pos n dm)
  10259. "Change the different parts of the lead-time and repeat fields in timestamp."
  10260. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10261. ng h m new rem)
  10262. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10263. (cond
  10264. ((or (org-pos-in-match-range pos 2)
  10265. (org-pos-in-match-range pos 3))
  10266. (setq m (string-to-number (match-string 3 s))
  10267. h (string-to-number (match-string 2 s)))
  10268. (if (org-pos-in-match-range pos 2)
  10269. (setq h (+ h n))
  10270. (setq n (* dm (org-no-warnings (signum n))))
  10271. (when (not (= 0 (setq rem (% m dm))))
  10272. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10273. (setq m (+ m n)))
  10274. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10275. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10276. (setq h (min 24 (max 0 h)))
  10277. (setq ng 1 new (format "-%02d:%02d" h m)))
  10278. ((org-pos-in-match-range pos 6)
  10279. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10280. ((org-pos-in-match-range pos 5)
  10281. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10282. ((org-pos-in-match-range pos 9)
  10283. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10284. ((org-pos-in-match-range pos 8)
  10285. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10286. (when ng
  10287. (setq s (concat
  10288. (substring s 0 (match-beginning ng))
  10289. new
  10290. (substring s (match-end ng))))))
  10291. s))
  10292. (defun org-recenter-calendar (date)
  10293. "If the calendar is visible, recenter it to DATE."
  10294. (let* ((win (selected-window))
  10295. (cwin (get-buffer-window "*Calendar*" t))
  10296. (calendar-move-hook nil))
  10297. (when cwin
  10298. (select-window cwin)
  10299. (calendar-goto-date (if (listp date) date
  10300. (calendar-gregorian-from-absolute date)))
  10301. (select-window win))))
  10302. (defun org-goto-calendar (&optional arg)
  10303. "Go to the Emacs calendar at the current date.
  10304. If there is a time stamp in the current line, go to that date.
  10305. A prefix ARG can be used to force the current date."
  10306. (interactive "P")
  10307. (let ((tsr org-ts-regexp) diff
  10308. (calendar-move-hook nil)
  10309. (calendar-view-holidays-initially-flag nil)
  10310. (view-calendar-holidays-initially nil)
  10311. (calendar-view-diary-initially-flag nil)
  10312. (view-diary-entries-initially nil))
  10313. (if (or (org-at-timestamp-p)
  10314. (save-excursion
  10315. (beginning-of-line 1)
  10316. (looking-at (concat ".*" tsr))))
  10317. (let ((d1 (time-to-days (current-time)))
  10318. (d2 (time-to-days
  10319. (org-time-string-to-time (match-string 1)))))
  10320. (setq diff (- d2 d1))))
  10321. (calendar)
  10322. (calendar-goto-today)
  10323. (if (and diff (not arg)) (calendar-forward-day diff))))
  10324. (defun org-get-date-from-calendar ()
  10325. "Return a list (month day year) of date at point in calendar."
  10326. (with-current-buffer "*Calendar*"
  10327. (save-match-data
  10328. (calendar-cursor-to-date))))
  10329. (defun org-date-from-calendar ()
  10330. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10331. If there is already a time stamp at the cursor position, update it."
  10332. (interactive)
  10333. (if (org-at-timestamp-p t)
  10334. (org-timestamp-change 0 'calendar)
  10335. (let ((cal-date (org-get-date-from-calendar)))
  10336. (org-insert-time-stamp
  10337. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10338. (defun org-minutes-to-hh:mm-string (m)
  10339. "Compute H:MM from a number of minutes."
  10340. (let ((h (/ m 60)))
  10341. (setq m (- m (* 60 h)))
  10342. (format "%d:%02d" h m)))
  10343. (defun org-hh:mm-string-to-minutes (s)
  10344. "Convert a string H:MM to a number of minutes."
  10345. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10346. (+ (* (string-to-number (match-string 1 s)) 60)
  10347. (string-to-number (match-string 2 s)))
  10348. 0))
  10349. ;;;; Agenda files
  10350. ;;;###autoload
  10351. (defun org-iswitchb (&optional arg)
  10352. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10353. With a prefix argument, restrict available to files.
  10354. With two prefix arguments, restrict available buffers to agenda files.
  10355. Due to some yet unresolved reason, global function
  10356. `iswitchb-mode' needs to be active for this function to work."
  10357. (interactive "P")
  10358. (require 'iswitchb)
  10359. (let ((enabled iswitchb-mode) blist)
  10360. (or enabled (iswitchb-mode 1))
  10361. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10362. ((equal arg '(16)) (org-buffer-list 'agenda))
  10363. (t (org-buffer-list))))
  10364. (unwind-protect
  10365. (let ((iswitchb-make-buflist-hook
  10366. (lambda ()
  10367. (setq iswitchb-temp-buflist
  10368. (mapcar 'buffer-name blist)))))
  10369. (switch-to-buffer
  10370. (iswitchb-read-buffer
  10371. "Switch-to: " nil t))
  10372. (or enabled (iswitchb-mode -1))))))
  10373. (defun org-buffer-list (&optional predicate tmp)
  10374. "Return a list of Org buffers.
  10375. PREDICATE can be either 'export, 'files or 'agenda.
  10376. 'export restrict the list to Export buffers.
  10377. 'files restrict the list to buffers visiting Org files.
  10378. 'agenda restrict the list to buffers visiting agenda files.
  10379. If TMP is non-nil, don't include temporary buffers."
  10380. (let (filter blist)
  10381. (setq filter
  10382. (cond ((eq predicate 'files) "\.org$")
  10383. ((eq predicate 'export) "\*Org .*Export")
  10384. (t "\*Org \\|\.org$")))
  10385. (setq blist
  10386. (mapcar
  10387. (lambda(b)
  10388. (let ((bname (buffer-name b))
  10389. (bfile (buffer-file-name b)))
  10390. (if (and (string-match filter bname)
  10391. (if (eq predicate 'agenda)
  10392. (member bfile
  10393. (mapcar (lambda(f) (file-truename f))
  10394. org-agenda-files)) t)
  10395. (if tmp (not (string-match "tmp" bname)) t)) b)))
  10396. (buffer-list)))
  10397. (delete nil blist)))
  10398. (defun org-agenda-files (&optional unrestricted ext)
  10399. "Get the list of agenda files.
  10400. Optional UNRESTRICTED means return the full list even if a restriction
  10401. is currently in place.
  10402. When EXT is non-nil, try to add all files that are created by adding EXT
  10403. to the file nemes. Basically, this is a way to add the archive files
  10404. to the list, by setting EXT to \"_archive\" If EXT is non-nil, but not
  10405. a string, \"_archive\" will be used."
  10406. (let ((files
  10407. (cond
  10408. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10409. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10410. ((listp org-agenda-files) org-agenda-files)
  10411. (t (error "Invalid value of `org-agenda-files'")))))
  10412. (setq files (apply 'append
  10413. (mapcar (lambda (f)
  10414. (if (file-directory-p f)
  10415. (directory-files
  10416. f t org-agenda-file-regexp)
  10417. (list f)))
  10418. files)))
  10419. (when org-agenda-skip-unavailable-files
  10420. (setq files (delq nil
  10421. (mapcar (function
  10422. (lambda (file)
  10423. (and (file-readable-p file) file)))
  10424. files))))
  10425. (when ext
  10426. (setq ext (if (and (stringp ext) (string-match "\\S-" ext))
  10427. ext "_archive"))
  10428. (setq files (apply 'append
  10429. (mapcar
  10430. (lambda (f)
  10431. (if (file-exists-p (concat f ext))
  10432. (list f (concat f ext))
  10433. (list f)))
  10434. files))))
  10435. files))
  10436. (defun org-edit-agenda-file-list ()
  10437. "Edit the list of agenda files.
  10438. Depending on setup, this either uses customize to edit the variable
  10439. `org-agenda-files', or it visits the file that is holding the list. In the
  10440. latter case, the buffer is set up in a way that saving it automatically kills
  10441. the buffer and restores the previous window configuration."
  10442. (interactive)
  10443. (if (stringp org-agenda-files)
  10444. (let ((cw (current-window-configuration)))
  10445. (find-file org-agenda-files)
  10446. (org-set-local 'org-window-configuration cw)
  10447. (org-add-hook 'after-save-hook
  10448. (lambda ()
  10449. (set-window-configuration
  10450. (prog1 org-window-configuration
  10451. (kill-buffer (current-buffer))))
  10452. (org-install-agenda-files-menu)
  10453. (message "New agenda file list installed"))
  10454. nil 'local)
  10455. (message "%s" (substitute-command-keys
  10456. "Edit list and finish with \\[save-buffer]")))
  10457. (customize-variable 'org-agenda-files)))
  10458. (defun org-store-new-agenda-file-list (list)
  10459. "Set new value for the agenda file list and save it correcly."
  10460. (if (stringp org-agenda-files)
  10461. (let ((f org-agenda-files) b)
  10462. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10463. (with-temp-file f
  10464. (insert (mapconcat 'identity list "\n") "\n")))
  10465. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10466. (setq org-agenda-files list)
  10467. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10468. (defun org-read-agenda-file-list ()
  10469. "Read the list of agenda files from a file."
  10470. (when (file-directory-p org-agenda-files)
  10471. (error "`org-agenda-files' cannot be a single directory"))
  10472. (when (stringp org-agenda-files)
  10473. (with-temp-buffer
  10474. (insert-file-contents org-agenda-files)
  10475. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10476. ;;;###autoload
  10477. (defun org-cycle-agenda-files ()
  10478. "Cycle through the files in `org-agenda-files'.
  10479. If the current buffer visits an agenda file, find the next one in the list.
  10480. If the current buffer does not, find the first agenda file."
  10481. (interactive)
  10482. (let* ((fs (org-agenda-files t))
  10483. (files (append fs (list (car fs))))
  10484. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10485. file)
  10486. (unless files (error "No agenda files"))
  10487. (catch 'exit
  10488. (while (setq file (pop files))
  10489. (if (equal (file-truename file) tcf)
  10490. (when (car files)
  10491. (find-file (car files))
  10492. (throw 'exit t))))
  10493. (find-file (car fs)))
  10494. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10495. (defun org-agenda-file-to-front (&optional to-end)
  10496. "Move/add the current file to the top of the agenda file list.
  10497. If the file is not present in the list, it is added to the front. If it is
  10498. present, it is moved there. With optional argument TO-END, add/move to the
  10499. end of the list."
  10500. (interactive "P")
  10501. (let ((org-agenda-skip-unavailable-files nil)
  10502. (file-alist (mapcar (lambda (x)
  10503. (cons (file-truename x) x))
  10504. (org-agenda-files t)))
  10505. (ctf (file-truename buffer-file-name))
  10506. x had)
  10507. (setq x (assoc ctf file-alist) had x)
  10508. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10509. (if to-end
  10510. (setq file-alist (append (delq x file-alist) (list x)))
  10511. (setq file-alist (cons x (delq x file-alist))))
  10512. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10513. (org-install-agenda-files-menu)
  10514. (message "File %s to %s of agenda file list"
  10515. (if had "moved" "added") (if to-end "end" "front"))))
  10516. (defun org-remove-file (&optional file)
  10517. "Remove current file from the list of files in variable `org-agenda-files'.
  10518. These are the files which are being checked for agenda entries.
  10519. Optional argument FILE means, use this file instead of the current."
  10520. (interactive)
  10521. (let* ((org-agenda-skip-unavailable-files nil)
  10522. (file (or file buffer-file-name))
  10523. (true-file (file-truename file))
  10524. (afile (abbreviate-file-name file))
  10525. (files (delq nil (mapcar
  10526. (lambda (x)
  10527. (if (equal true-file
  10528. (file-truename x))
  10529. nil x))
  10530. (org-agenda-files t)))))
  10531. (if (not (= (length files) (length (org-agenda-files t))))
  10532. (progn
  10533. (org-store-new-agenda-file-list files)
  10534. (org-install-agenda-files-menu)
  10535. (message "Removed file: %s" afile))
  10536. (message "File was not in list: %s (not removed)" afile))))
  10537. (defun org-file-menu-entry (file)
  10538. (vector file (list 'find-file file) t))
  10539. (defun org-check-agenda-file (file)
  10540. "Make sure FILE exists. If not, ask user what to do."
  10541. (when (not (file-exists-p file))
  10542. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10543. (abbreviate-file-name file))
  10544. (let ((r (downcase (read-char-exclusive))))
  10545. (cond
  10546. ((equal r ?r)
  10547. (org-remove-file file)
  10548. (throw 'nextfile t))
  10549. (t (error "Abort"))))))
  10550. (defun org-get-agenda-file-buffer (file)
  10551. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10552. it to the list of buffers which might be released later."
  10553. (let ((buf (org-find-base-buffer-visiting file)))
  10554. (if buf
  10555. buf ; just return it
  10556. ;; Make a new buffer and remember it
  10557. (setq buf (find-file-noselect file))
  10558. (if buf (push buf org-agenda-new-buffers))
  10559. buf)))
  10560. (defun org-release-buffers (blist)
  10561. "Release all buffers in list, asking the user for confirmation when needed.
  10562. When a buffer is unmodified, it is just killed. When modified, it is saved
  10563. \(if the user agrees) and then killed."
  10564. (let (buf file)
  10565. (while (setq buf (pop blist))
  10566. (setq file (buffer-file-name buf))
  10567. (when (and (buffer-modified-p buf)
  10568. file
  10569. (y-or-n-p (format "Save file %s? " file)))
  10570. (with-current-buffer buf (save-buffer)))
  10571. (kill-buffer buf))))
  10572. (defun org-prepare-agenda-buffers (files)
  10573. "Create buffers for all agenda files, protect archived trees and comments."
  10574. (interactive)
  10575. (let ((pa '(:org-archived t))
  10576. (pc '(:org-comment t))
  10577. (pall '(:org-archived t :org-comment t))
  10578. (inhibit-read-only t)
  10579. (rea (concat ":" org-archive-tag ":"))
  10580. bmp file re)
  10581. (save-excursion
  10582. (save-restriction
  10583. (while (setq file (pop files))
  10584. (if (bufferp file)
  10585. (set-buffer file)
  10586. (org-check-agenda-file file)
  10587. (set-buffer (org-get-agenda-file-buffer file)))
  10588. (widen)
  10589. (setq bmp (buffer-modified-p))
  10590. (org-refresh-category-properties)
  10591. (setq org-todo-keywords-for-agenda
  10592. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10593. (setq org-done-keywords-for-agenda
  10594. (append org-done-keywords-for-agenda org-done-keywords))
  10595. (save-excursion
  10596. (remove-text-properties (point-min) (point-max) pall)
  10597. (when org-agenda-skip-archived-trees
  10598. (goto-char (point-min))
  10599. (while (re-search-forward rea nil t)
  10600. (if (org-on-heading-p t)
  10601. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10602. (goto-char (point-min))
  10603. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10604. (while (re-search-forward re nil t)
  10605. (add-text-properties
  10606. (match-beginning 0) (org-end-of-subtree t) pc)))
  10607. (set-buffer-modified-p bmp))))))
  10608. ;;;; Embedded LaTeX
  10609. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10610. "Keymap for the minor `org-cdlatex-mode'.")
  10611. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10612. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10613. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10614. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10615. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10616. (defvar org-cdlatex-texmathp-advice-is-done nil
  10617. "Flag remembering if we have applied the advice to texmathp already.")
  10618. (define-minor-mode org-cdlatex-mode
  10619. "Toggle the minor `org-cdlatex-mode'.
  10620. This mode supports entering LaTeX environment and math in LaTeX fragments
  10621. in Org-mode.
  10622. \\{org-cdlatex-mode-map}"
  10623. nil " OCDL" nil
  10624. (when org-cdlatex-mode (require 'cdlatex))
  10625. (unless org-cdlatex-texmathp-advice-is-done
  10626. (setq org-cdlatex-texmathp-advice-is-done t)
  10627. (defadvice texmathp (around org-math-always-on activate)
  10628. "Always return t in org-mode buffers.
  10629. This is because we want to insert math symbols without dollars even outside
  10630. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10631. en embedded LaTeX fragement, let texmathp do its job.
  10632. \\[org-cdlatex-mode-map]"
  10633. (interactive)
  10634. (let (p)
  10635. (cond
  10636. ((not (org-mode-p)) ad-do-it)
  10637. ((eq this-command 'cdlatex-math-symbol)
  10638. (setq ad-return-value t
  10639. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10640. (t
  10641. (let ((p (org-inside-LaTeX-fragment-p)))
  10642. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10643. (setq ad-return-value t
  10644. texmathp-why '("Org-mode embedded math" . 0))
  10645. (if p ad-do-it)))))))))
  10646. (defun turn-on-org-cdlatex ()
  10647. "Unconditionally turn on `org-cdlatex-mode'."
  10648. (org-cdlatex-mode 1))
  10649. (defun org-inside-LaTeX-fragment-p ()
  10650. "Test if point is inside a LaTeX fragment.
  10651. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10652. sequence appearing also before point.
  10653. Even though the matchers for math are configurable, this function assumes
  10654. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10655. delimiters are skipped when they have been removed by customization.
  10656. The return value is nil, or a cons cell with the delimiter and
  10657. and the position of this delimiter.
  10658. This function does a reasonably good job, but can locally be fooled by
  10659. for example currency specifications. For example it will assume being in
  10660. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10661. fragments that are properly closed, but during editing, we have to live
  10662. with the uncertainty caused by missing closing delimiters. This function
  10663. looks only before point, not after."
  10664. (catch 'exit
  10665. (let ((pos (point))
  10666. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10667. (lim (progn
  10668. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10669. (point)))
  10670. dd-on str (start 0) m re)
  10671. (goto-char pos)
  10672. (when dodollar
  10673. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10674. re (nth 1 (assoc "$" org-latex-regexps)))
  10675. (while (string-match re str start)
  10676. (cond
  10677. ((= (match-end 0) (length str))
  10678. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10679. ((= (match-end 0) (- (length str) 5))
  10680. (throw 'exit nil))
  10681. (t (setq start (match-end 0))))))
  10682. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10683. (goto-char pos)
  10684. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10685. (and (match-beginning 2) (throw 'exit nil))
  10686. ;; count $$
  10687. (while (re-search-backward "\\$\\$" lim t)
  10688. (setq dd-on (not dd-on)))
  10689. (goto-char pos)
  10690. (if dd-on (cons "$$" m))))))
  10691. (defun org-try-cdlatex-tab ()
  10692. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10693. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10694. - inside a LaTeX fragment, or
  10695. - after the first word in a line, where an abbreviation expansion could
  10696. insert a LaTeX environment."
  10697. (when org-cdlatex-mode
  10698. (cond
  10699. ((save-excursion
  10700. (skip-chars-backward "a-zA-Z0-9*")
  10701. (skip-chars-backward " \t")
  10702. (bolp))
  10703. (cdlatex-tab) t)
  10704. ((org-inside-LaTeX-fragment-p)
  10705. (cdlatex-tab) t)
  10706. (t nil))))
  10707. (defun org-cdlatex-underscore-caret (&optional arg)
  10708. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10709. Revert to the normal definition outside of these fragments."
  10710. (interactive "P")
  10711. (if (org-inside-LaTeX-fragment-p)
  10712. (call-interactively 'cdlatex-sub-superscript)
  10713. (let (org-cdlatex-mode)
  10714. (call-interactively (key-binding (vector last-input-event))))))
  10715. (defun org-cdlatex-math-modify (&optional arg)
  10716. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10717. Revert to the normal definition outside of these fragments."
  10718. (interactive "P")
  10719. (if (org-inside-LaTeX-fragment-p)
  10720. (call-interactively 'cdlatex-math-modify)
  10721. (let (org-cdlatex-mode)
  10722. (call-interactively (key-binding (vector last-input-event))))))
  10723. (defvar org-latex-fragment-image-overlays nil
  10724. "List of overlays carrying the images of latex fragments.")
  10725. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10726. (defun org-remove-latex-fragment-image-overlays ()
  10727. "Remove all overlays with LaTeX fragment images in current buffer."
  10728. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10729. (setq org-latex-fragment-image-overlays nil))
  10730. (defun org-preview-latex-fragment (&optional subtree)
  10731. "Preview the LaTeX fragment at point, or all locally or globally.
  10732. If the cursor is in a LaTeX fragment, create the image and overlay
  10733. it over the source code. If there is no fragment at point, display
  10734. all fragments in the current text, from one headline to the next. With
  10735. prefix SUBTREE, display all fragments in the current subtree. With a
  10736. double prefix `C-u C-u', or when the cursor is before the first headline,
  10737. display all fragments in the buffer.
  10738. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10739. (interactive "P")
  10740. (org-remove-latex-fragment-image-overlays)
  10741. (save-excursion
  10742. (save-restriction
  10743. (let (beg end at msg)
  10744. (cond
  10745. ((or (equal subtree '(16))
  10746. (not (save-excursion
  10747. (re-search-backward (concat "^" outline-regexp) nil t))))
  10748. (setq beg (point-min) end (point-max)
  10749. msg "Creating images for buffer...%s"))
  10750. ((equal subtree '(4))
  10751. (org-back-to-heading)
  10752. (setq beg (point) end (org-end-of-subtree t)
  10753. msg "Creating images for subtree...%s"))
  10754. (t
  10755. (if (setq at (org-inside-LaTeX-fragment-p))
  10756. (goto-char (max (point-min) (- (cdr at) 2)))
  10757. (org-back-to-heading))
  10758. (setq beg (point) end (progn (outline-next-heading) (point))
  10759. msg (if at "Creating image...%s"
  10760. "Creating images for entry...%s"))))
  10761. (message msg "")
  10762. (narrow-to-region beg end)
  10763. (goto-char beg)
  10764. (org-format-latex
  10765. (concat "ltxpng/" (file-name-sans-extension
  10766. (file-name-nondirectory
  10767. buffer-file-name)))
  10768. default-directory 'overlays msg at 'forbuffer)
  10769. (message msg "done. Use `C-c C-c' to remove images.")))))
  10770. (defvar org-latex-regexps
  10771. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10772. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10773. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10774. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10775. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10776. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10777. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10778. "Regular expressions for matching embedded LaTeX.")
  10779. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10780. "Replace LaTeX fragments with links to an image, and produce images."
  10781. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10782. (let* ((prefixnodir (file-name-nondirectory prefix))
  10783. (absprefix (expand-file-name prefix dir))
  10784. (todir (file-name-directory absprefix))
  10785. (opt org-format-latex-options)
  10786. (matchers (plist-get opt :matchers))
  10787. (re-list org-latex-regexps)
  10788. (cnt 0) txt link beg end re e checkdir
  10789. m n block linkfile movefile ov)
  10790. ;; Check if there are old images files with this prefix, and remove them
  10791. (when (file-directory-p todir)
  10792. (mapc 'delete-file
  10793. (directory-files
  10794. todir 'full
  10795. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10796. ;; Check the different regular expressions
  10797. (while (setq e (pop re-list))
  10798. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10799. block (if (nth 3 e) "\n\n" ""))
  10800. (when (member m matchers)
  10801. (goto-char (point-min))
  10802. (while (re-search-forward re nil t)
  10803. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10804. (setq txt (match-string n)
  10805. beg (match-beginning n) end (match-end n)
  10806. cnt (1+ cnt)
  10807. linkfile (format "%s_%04d.png" prefix cnt)
  10808. movefile (format "%s_%04d.png" absprefix cnt)
  10809. link (concat block "[[file:" linkfile "]]" block))
  10810. (if msg (message msg cnt))
  10811. (goto-char beg)
  10812. (unless checkdir ; make sure the directory exists
  10813. (setq checkdir t)
  10814. (or (file-directory-p todir) (make-directory todir)))
  10815. (org-create-formula-image
  10816. txt movefile opt forbuffer)
  10817. (if overlays
  10818. (progn
  10819. (setq ov (org-make-overlay beg end))
  10820. (if (featurep 'xemacs)
  10821. (progn
  10822. (org-overlay-put ov 'invisible t)
  10823. (org-overlay-put
  10824. ov 'end-glyph
  10825. (make-glyph (vector 'png :file movefile))))
  10826. (org-overlay-put
  10827. ov 'display
  10828. (list 'image :type 'png :file movefile :ascent 'center)))
  10829. (push ov org-latex-fragment-image-overlays)
  10830. (goto-char end))
  10831. (delete-region beg end)
  10832. (insert link))))))))
  10833. ;; This function borrows from Ganesh Swami's latex2png.el
  10834. (defun org-create-formula-image (string tofile options buffer)
  10835. (let* ((tmpdir (if (featurep 'xemacs)
  10836. (temp-directory)
  10837. temporary-file-directory))
  10838. (texfilebase (make-temp-name
  10839. (expand-file-name "orgtex" tmpdir)))
  10840. (texfile (concat texfilebase ".tex"))
  10841. (dvifile (concat texfilebase ".dvi"))
  10842. (pngfile (concat texfilebase ".png"))
  10843. (fnh (if (featurep 'xemacs)
  10844. (font-height (get-face-font 'default))
  10845. (face-attribute 'default :height nil)))
  10846. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  10847. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  10848. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  10849. "Black"))
  10850. (bg (or (plist-get options (if buffer :background :html-background))
  10851. "Transparent")))
  10852. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  10853. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  10854. (with-temp-file texfile
  10855. (insert org-format-latex-header
  10856. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  10857. (let ((dir default-directory))
  10858. (condition-case nil
  10859. (progn
  10860. (cd tmpdir)
  10861. (call-process "latex" nil nil nil texfile))
  10862. (error nil))
  10863. (cd dir))
  10864. (if (not (file-exists-p dvifile))
  10865. (progn (message "Failed to create dvi file from %s" texfile) nil)
  10866. (call-process "dvipng" nil nil nil
  10867. "-E" "-fg" fg "-bg" bg
  10868. "-D" dpi
  10869. ;;"-x" scale "-y" scale
  10870. "-T" "tight"
  10871. "-o" pngfile
  10872. dvifile)
  10873. (if (not (file-exists-p pngfile))
  10874. (progn (message "Failed to create png file from %s" texfile) nil)
  10875. ;; Use the requested file name and clean up
  10876. (copy-file pngfile tofile 'replace)
  10877. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  10878. (delete-file (concat texfilebase e)))
  10879. pngfile))))
  10880. (defun org-dvipng-color (attr)
  10881. "Return an rgb color specification for dvipng."
  10882. (apply 'format "rgb %s %s %s"
  10883. (mapcar 'org-normalize-color
  10884. (color-values (face-attribute 'default attr nil)))))
  10885. (defun org-normalize-color (value)
  10886. "Return string to be used as color value for an RGB component."
  10887. (format "%g" (/ value 65535.0)))
  10888. ;;;; Key bindings
  10889. ;; Make `C-c C-x' a prefix key
  10890. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  10891. ;; TAB key with modifiers
  10892. (org-defkey org-mode-map "\C-i" 'org-cycle)
  10893. (org-defkey org-mode-map [(tab)] 'org-cycle)
  10894. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  10895. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  10896. (org-defkey org-mode-map "\M-\t" 'org-complete)
  10897. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  10898. ;; The following line is necessary under Suse GNU/Linux
  10899. (unless (featurep 'xemacs)
  10900. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  10901. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  10902. (define-key org-mode-map [backtab] 'org-shifttab)
  10903. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  10904. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  10905. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  10906. ;; Cursor keys with modifiers
  10907. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  10908. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  10909. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  10910. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  10911. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  10912. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  10913. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  10914. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  10915. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  10916. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  10917. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  10918. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  10919. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  10920. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  10921. ;;; Extra keys for tty access.
  10922. ;; We only set them when really needed because otherwise the
  10923. ;; menus don't show the simple keys
  10924. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  10925. (not window-system))
  10926. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  10927. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  10928. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  10929. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  10930. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  10931. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  10932. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  10933. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  10934. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  10935. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  10936. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  10937. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  10938. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  10939. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  10940. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  10941. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  10942. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  10943. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  10944. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  10945. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  10946. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  10947. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  10948. ;; All the other keys
  10949. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  10950. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  10951. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  10952. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  10953. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  10954. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  10955. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  10956. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  10957. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  10958. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  10959. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  10960. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  10961. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  10962. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  10963. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  10964. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  10965. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  10966. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  10967. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  10968. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  10969. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  10970. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  10971. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  10972. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  10973. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  10974. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  10975. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  10976. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  10977. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  10978. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  10979. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  10980. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  10981. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  10982. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  10983. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  10984. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  10985. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  10986. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  10987. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  10988. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  10989. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  10990. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  10991. (org-defkey org-mode-map "\C-c^" 'org-sort)
  10992. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  10993. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  10994. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  10995. (org-defkey org-mode-map "\C-m" 'org-return)
  10996. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  10997. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  10998. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  10999. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11000. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11001. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  11002. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11003. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11004. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11005. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11006. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  11007. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11008. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11009. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11010. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11011. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11012. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  11013. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11014. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11015. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11016. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11017. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11018. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11019. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11020. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11021. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11022. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11023. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11024. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11025. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11026. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11027. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  11028. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11029. (when (featurep 'xemacs)
  11030. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11031. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11032. (defun org-self-insert-command (N)
  11033. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11034. If the cursor is in a table looking at whitespace, the whitespace is
  11035. overwritten, and the table is not marked as requiring realignment."
  11036. (interactive "p")
  11037. (if (and (org-table-p)
  11038. (progn
  11039. ;; check if we blank the field, and if that triggers align
  11040. (and (featurep 'org-table) org-table-auto-blank-field
  11041. (member last-command
  11042. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11043. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11044. ;; got extra space, this field does not determine column width
  11045. (let (org-table-may-need-update) (org-table-blank-field))
  11046. ;; no extra space, this field may determine column width
  11047. (org-table-blank-field)))
  11048. t)
  11049. (eq N 1)
  11050. (looking-at "[^|\n]* |"))
  11051. (let (org-table-may-need-update)
  11052. (goto-char (1- (match-end 0)))
  11053. (delete-backward-char 1)
  11054. (goto-char (match-beginning 0))
  11055. (self-insert-command N))
  11056. (setq org-table-may-need-update t)
  11057. (self-insert-command N)
  11058. (org-fix-tags-on-the-fly)))
  11059. (defun org-fix-tags-on-the-fly ()
  11060. (when (and (equal (char-after (point-at-bol)) ?*)
  11061. (org-on-heading-p))
  11062. (org-align-tags-here org-tags-column)))
  11063. (defun org-delete-backward-char (N)
  11064. "Like `delete-backward-char', insert whitespace at field end in tables.
  11065. When deleting backwards, in tables this function will insert whitespace in
  11066. front of the next \"|\" separator, to keep the table aligned. The table will
  11067. still be marked for re-alignment if the field did fill the entire column,
  11068. because, in this case the deletion might narrow the column."
  11069. (interactive "p")
  11070. (if (and (org-table-p)
  11071. (eq N 1)
  11072. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11073. (looking-at ".*?|"))
  11074. (let ((pos (point))
  11075. (noalign (looking-at "[^|\n\r]* |"))
  11076. (c org-table-may-need-update))
  11077. (backward-delete-char N)
  11078. (skip-chars-forward "^|")
  11079. (insert " ")
  11080. (goto-char (1- pos))
  11081. ;; noalign: if there were two spaces at the end, this field
  11082. ;; does not determine the width of the column.
  11083. (if noalign (setq org-table-may-need-update c)))
  11084. (backward-delete-char N)
  11085. (org-fix-tags-on-the-fly)))
  11086. (defun org-delete-char (N)
  11087. "Like `delete-char', but insert whitespace at field end in tables.
  11088. When deleting characters, in tables this function will insert whitespace in
  11089. front of the next \"|\" separator, to keep the table aligned. The table will
  11090. still be marked for re-alignment if the field did fill the entire column,
  11091. because, in this case the deletion might narrow the column."
  11092. (interactive "p")
  11093. (if (and (org-table-p)
  11094. (not (bolp))
  11095. (not (= (char-after) ?|))
  11096. (eq N 1))
  11097. (if (looking-at ".*?|")
  11098. (let ((pos (point))
  11099. (noalign (looking-at "[^|\n\r]* |"))
  11100. (c org-table-may-need-update))
  11101. (replace-match (concat
  11102. (substring (match-string 0) 1 -1)
  11103. " |"))
  11104. (goto-char pos)
  11105. ;; noalign: if there were two spaces at the end, this field
  11106. ;; does not determine the width of the column.
  11107. (if noalign (setq org-table-may-need-update c)))
  11108. (delete-char N))
  11109. (delete-char N)
  11110. (org-fix-tags-on-the-fly)))
  11111. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11112. (put 'org-self-insert-command 'delete-selection t)
  11113. (put 'orgtbl-self-insert-command 'delete-selection t)
  11114. (put 'org-delete-char 'delete-selection 'supersede)
  11115. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11116. ;; Make `flyspell-mode' delay after some commands
  11117. (put 'org-self-insert-command 'flyspell-delayed t)
  11118. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11119. (put 'org-delete-char 'flyspell-delayed t)
  11120. (put 'org-delete-backward-char 'flyspell-delayed t)
  11121. ;; Make pabbrev-mode expand after org-mode commands
  11122. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11123. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11124. ;; How to do this: Measure non-white length of current string
  11125. ;; If equal to column width, we should realign.
  11126. (defun org-remap (map &rest commands)
  11127. "In MAP, remap the functions given in COMMANDS.
  11128. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11129. (let (new old)
  11130. (while commands
  11131. (setq old (pop commands) new (pop commands))
  11132. (if (fboundp 'command-remapping)
  11133. (org-defkey map (vector 'remap old) new)
  11134. (substitute-key-definition old new map global-map)))))
  11135. (when (eq org-enable-table-editor 'optimized)
  11136. ;; If the user wants maximum table support, we need to hijack
  11137. ;; some standard editing functions
  11138. (org-remap org-mode-map
  11139. 'self-insert-command 'org-self-insert-command
  11140. 'delete-char 'org-delete-char
  11141. 'delete-backward-char 'org-delete-backward-char)
  11142. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11143. (defun org-shiftcursor-error ()
  11144. "Throw an error because Shift-Cursor command was applied in wrong context."
  11145. (error "This command is active in special context like tables, headlines or timestamps"))
  11146. (defun org-shifttab (&optional arg)
  11147. "Global visibility cycling or move to previous table field.
  11148. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11149. on context.
  11150. See the individual commands for more information."
  11151. (interactive "P")
  11152. (cond
  11153. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11154. ((integerp arg)
  11155. (message "Content view to level: %d" arg)
  11156. (org-content (prefix-numeric-value arg))
  11157. (setq org-cycle-global-status 'overview))
  11158. (t (call-interactively 'org-global-cycle))))
  11159. (defun org-shiftmetaleft ()
  11160. "Promote subtree or delete table column.
  11161. Calls `org-promote-subtree', `org-outdent-item',
  11162. or `org-table-delete-column', depending on context.
  11163. See the individual commands for more information."
  11164. (interactive)
  11165. (cond
  11166. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11167. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11168. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11169. (t (org-shiftcursor-error))))
  11170. (defun org-shiftmetaright ()
  11171. "Demote subtree or insert table column.
  11172. Calls `org-demote-subtree', `org-indent-item',
  11173. or `org-table-insert-column', depending on context.
  11174. See the individual commands for more information."
  11175. (interactive)
  11176. (cond
  11177. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11178. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11179. ((org-at-item-p) (call-interactively 'org-indent-item))
  11180. (t (org-shiftcursor-error))))
  11181. (defun org-shiftmetaup (&optional arg)
  11182. "Move subtree up or kill table row.
  11183. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11184. `org-move-item-up' depending on context. See the individual commands
  11185. for more information."
  11186. (interactive "P")
  11187. (cond
  11188. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11189. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11190. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11191. (t (org-shiftcursor-error))))
  11192. (defun org-shiftmetadown (&optional arg)
  11193. "Move subtree down or insert table row.
  11194. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11195. `org-move-item-down', depending on context. See the individual
  11196. commands for more information."
  11197. (interactive "P")
  11198. (cond
  11199. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11200. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11201. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11202. (t (org-shiftcursor-error))))
  11203. (defun org-metaleft (&optional arg)
  11204. "Promote heading or move table column to left.
  11205. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11206. With no specific context, calls the Emacs default `backward-word'.
  11207. See the individual commands for more information."
  11208. (interactive "P")
  11209. (cond
  11210. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11211. ((or (org-on-heading-p) (org-region-active-p))
  11212. (call-interactively 'org-do-promote))
  11213. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11214. (t (call-interactively 'backward-word))))
  11215. (defun org-metaright (&optional arg)
  11216. "Demote subtree or move table column to right.
  11217. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11218. With no specific context, calls the Emacs default `forward-word'.
  11219. See the individual commands for more information."
  11220. (interactive "P")
  11221. (cond
  11222. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11223. ((or (org-on-heading-p) (org-region-active-p))
  11224. (call-interactively 'org-do-demote))
  11225. ((org-at-item-p) (call-interactively 'org-indent-item))
  11226. (t (call-interactively 'forward-word))))
  11227. (defun org-metaup (&optional arg)
  11228. "Move subtree up or move table row up.
  11229. Calls `org-move-subtree-up' or `org-table-move-row' or
  11230. `org-move-item-up', depending on context. See the individual commands
  11231. for more information."
  11232. (interactive "P")
  11233. (cond
  11234. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11235. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11236. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11237. (t (transpose-lines 1) (beginning-of-line -1))))
  11238. (defun org-metadown (&optional arg)
  11239. "Move subtree down or move table row down.
  11240. Calls `org-move-subtree-down' or `org-table-move-row' or
  11241. `org-move-item-down', depending on context. See the individual
  11242. commands for more information."
  11243. (interactive "P")
  11244. (cond
  11245. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11246. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11247. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11248. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11249. (defun org-shiftup (&optional arg)
  11250. "Increase item in timestamp or increase priority of current headline.
  11251. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11252. depending on context. See the individual commands for more information."
  11253. (interactive "P")
  11254. (cond
  11255. ((org-at-timestamp-p t)
  11256. (call-interactively (if org-edit-timestamp-down-means-later
  11257. 'org-timestamp-down 'org-timestamp-up)))
  11258. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11259. ((org-at-item-p) (call-interactively 'org-previous-item))
  11260. ((org-clocktable-try-shift 'up arg))
  11261. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11262. (defun org-shiftdown (&optional arg)
  11263. "Decrease item in timestamp or decrease priority of current headline.
  11264. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11265. depending on context. See the individual commands for more information."
  11266. (interactive "P")
  11267. (cond
  11268. ((org-at-timestamp-p t)
  11269. (call-interactively (if org-edit-timestamp-down-means-later
  11270. 'org-timestamp-up 'org-timestamp-down)))
  11271. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11272. ((org-clocktable-try-shift 'down arg))
  11273. (t (call-interactively 'org-next-item))))
  11274. (defun org-shiftright (&optional arg)
  11275. "Next TODO keyword or timestamp one day later, depending on context."
  11276. (interactive "P")
  11277. (cond
  11278. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11279. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11280. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11281. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11282. ((org-clocktable-try-shift 'right arg))
  11283. (t (org-shiftcursor-error))))
  11284. (defun org-shiftleft (&optional arg)
  11285. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11286. (interactive "P")
  11287. (cond
  11288. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11289. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11290. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11291. ((org-at-property-p)
  11292. (call-interactively 'org-property-previous-allowed-value))
  11293. ((org-clocktable-try-shift 'left arg))
  11294. (t (org-shiftcursor-error))))
  11295. (defun org-shiftcontrolright ()
  11296. "Switch to next TODO set."
  11297. (interactive)
  11298. (cond
  11299. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11300. (t (org-shiftcursor-error))))
  11301. (defun org-shiftcontrolleft ()
  11302. "Switch to previous TODO set."
  11303. (interactive)
  11304. (cond
  11305. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11306. (t (org-shiftcursor-error))))
  11307. (defun org-ctrl-c-ret ()
  11308. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11309. (interactive)
  11310. (cond
  11311. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11312. (t (call-interactively 'org-insert-heading))))
  11313. (defun org-copy-special ()
  11314. "Copy region in table or copy current subtree.
  11315. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11316. See the individual commands for more information."
  11317. (interactive)
  11318. (call-interactively
  11319. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11320. (defun org-cut-special ()
  11321. "Cut region in table or cut current subtree.
  11322. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11323. See the individual commands for more information."
  11324. (interactive)
  11325. (call-interactively
  11326. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11327. (defun org-paste-special (arg)
  11328. "Paste rectangular region into table, or past subtree relative to level.
  11329. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11330. See the individual commands for more information."
  11331. (interactive "P")
  11332. (if (org-at-table-p)
  11333. (org-table-paste-rectangle)
  11334. (org-paste-subtree arg)))
  11335. (defun org-ctrl-c-ctrl-c (&optional arg)
  11336. "Set tags in headline, or update according to changed information at point.
  11337. This command does many different things, depending on context:
  11338. - If the cursor is in a headline, prompt for tags and insert them
  11339. into the current line, aligned to `org-tags-column'. When called
  11340. with prefix arg, realign all tags in the current buffer.
  11341. - If the cursor is in one of the special #+KEYWORD lines, this
  11342. triggers scanning the buffer for these lines and updating the
  11343. information.
  11344. - If the cursor is inside a table, realign the table. This command
  11345. works even if the automatic table editor has been turned off.
  11346. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11347. the entire table.
  11348. - If the cursor is a the beginning of a dynamic block, update it.
  11349. - If the cursor is inside a table created by the table.el package,
  11350. activate that table.
  11351. - If the current buffer is a remember buffer, close note and file it.
  11352. with a prefix argument, file it without further interaction to the default
  11353. location.
  11354. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11355. links in this buffer.
  11356. - If the cursor is on a numbered item in a plain list, renumber the
  11357. ordered list.
  11358. - If the cursor is on a checkbox, toggle it."
  11359. (interactive "P")
  11360. (let ((org-enable-table-editor t))
  11361. (cond
  11362. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11363. org-occur-highlights
  11364. org-latex-fragment-image-overlays)
  11365. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11366. (org-remove-occur-highlights)
  11367. (org-remove-latex-fragment-image-overlays)
  11368. (message "Temporary highlights/overlays removed from current buffer"))
  11369. ((and (local-variable-p 'org-finish-function (current-buffer))
  11370. (fboundp org-finish-function))
  11371. (funcall org-finish-function))
  11372. ((org-at-property-p)
  11373. (call-interactively 'org-property-action))
  11374. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11375. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11376. ((org-at-table.el-p)
  11377. (require 'table)
  11378. (beginning-of-line 1)
  11379. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11380. (call-interactively 'table-recognize-table))
  11381. ((org-at-table-p)
  11382. (org-table-maybe-eval-formula)
  11383. (if arg
  11384. (call-interactively 'org-table-recalculate)
  11385. (org-table-maybe-recalculate-line))
  11386. (call-interactively 'org-table-align))
  11387. ((org-at-item-checkbox-p)
  11388. (call-interactively 'org-toggle-checkbox))
  11389. ((org-at-item-p)
  11390. (call-interactively 'org-maybe-renumber-ordered-list))
  11391. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11392. ;; Dynamic block
  11393. (beginning-of-line 1)
  11394. (org-update-dblock))
  11395. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11396. (cond
  11397. ((equal (match-string 1) "TBLFM")
  11398. ;; Recalculate the table before this line
  11399. (save-excursion
  11400. (beginning-of-line 1)
  11401. (skip-chars-backward " \r\n\t")
  11402. (if (org-at-table-p)
  11403. (org-call-with-arg 'org-table-recalculate t))))
  11404. (t
  11405. ; (org-set-regexps-and-options)
  11406. ; (org-restart-font-lock)
  11407. (let ((org-inhibit-startup t)) (org-mode-restart))
  11408. (message "Local setup has been refreshed"))))
  11409. (t (error "C-c C-c can do nothing useful at this location.")))))
  11410. (defun org-mode-restart ()
  11411. "Restart Org-mode, to scan again for special lines.
  11412. Also updates the keyword regular expressions."
  11413. (interactive)
  11414. (org-mode)
  11415. (message "Org-mode restarted"))
  11416. (defun org-kill-note-or-show-branches ()
  11417. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11418. (interactive)
  11419. (if (not org-finish-function)
  11420. (call-interactively 'show-branches)
  11421. (let ((org-note-abort t))
  11422. (funcall org-finish-function))))
  11423. (defun org-return (&optional indent)
  11424. "Goto next table row or insert a newline.
  11425. Calls `org-table-next-row' or `newline', depending on context.
  11426. See the individual commands for more information."
  11427. (interactive)
  11428. (cond
  11429. ((bobp) (if indent (newline-and-indent) (newline)))
  11430. ((and (org-at-heading-p)
  11431. (looking-at
  11432. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11433. (org-show-entry)
  11434. (end-of-line 1)
  11435. (newline))
  11436. ((org-at-table-p)
  11437. (org-table-justify-field-maybe)
  11438. (call-interactively 'org-table-next-row))
  11439. (t (if indent (newline-and-indent) (newline)))))
  11440. (defun org-return-indent ()
  11441. "Goto next table row or insert a newline and indent.
  11442. Calls `org-table-next-row' or `newline-and-indent', depending on
  11443. context. See the individual commands for more information."
  11444. (interactive)
  11445. (org-return t))
  11446. (defun org-ctrl-c-star ()
  11447. "Compute table, or change heading status of lines.
  11448. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  11449. depending on context. This will also turn a plain list item or a normal
  11450. line into a subheading."
  11451. (interactive)
  11452. (cond
  11453. ((org-at-table-p)
  11454. (call-interactively 'org-table-recalculate))
  11455. ((org-region-active-p)
  11456. ;; Convert all lines in region to list items
  11457. (call-interactively 'org-toggle-region-headings))
  11458. ((org-on-heading-p)
  11459. (org-toggle-region-headings (point-at-bol)
  11460. (min (1+ (point-at-eol)) (point-max))))
  11461. ((org-at-item-p)
  11462. ;; Convert to heading
  11463. (let ((level (save-match-data
  11464. (save-excursion
  11465. (condition-case nil
  11466. (progn
  11467. (org-back-to-heading t)
  11468. (funcall outline-level))
  11469. (error 0))))))
  11470. (replace-match
  11471. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11472. (t (org-toggle-region-headings (point-at-bol)
  11473. (min (1+ (point-at-eol)) (point-max))))))
  11474. (defun org-ctrl-c-minus ()
  11475. "Insert separator line in table or modify bullet status of line.
  11476. Also turns a plain line or a region of lines into list items.
  11477. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11478. `org-cycle-list-bullet', depending on context."
  11479. (interactive)
  11480. (cond
  11481. ((org-at-table-p)
  11482. (call-interactively 'org-table-insert-hline))
  11483. ((org-on-heading-p)
  11484. ;; Convert to item
  11485. (save-excursion
  11486. (beginning-of-line 1)
  11487. (if (looking-at "\\*+ ")
  11488. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11489. ((org-region-active-p)
  11490. ;; Convert all lines in region to list items
  11491. (call-interactively 'org-toggle-region-items))
  11492. ((org-in-item-p)
  11493. (call-interactively 'org-cycle-list-bullet))
  11494. (t (org-toggle-region-items (point-at-bol)
  11495. (min (1+ (point-at-eol)) (point-max))))))
  11496. (defun org-toggle-region-items (beg end)
  11497. "Convert all lines in region to list items.
  11498. If the first line is already an item, convert all list items in the region
  11499. to normal lines."
  11500. (interactive "r")
  11501. (let (l2 l)
  11502. (save-excursion
  11503. (goto-char end)
  11504. (setq l2 (org-current-line))
  11505. (goto-char beg)
  11506. (beginning-of-line 1)
  11507. (setq l (1- (org-current-line)))
  11508. (if (org-at-item-p)
  11509. ;; We already have items, de-itemize
  11510. (while (< (setq l (1+ l)) l2)
  11511. (when (org-at-item-p)
  11512. (goto-char (match-beginning 2))
  11513. (delete-region (match-beginning 2) (match-end 2))
  11514. (and (looking-at "[ \t]+") (replace-match "")))
  11515. (beginning-of-line 2))
  11516. (while (< (setq l (1+ l)) l2)
  11517. (unless (org-at-item-p)
  11518. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11519. (replace-match "\\1- \\2")))
  11520. (beginning-of-line 2))))))
  11521. (defun org-toggle-region-headings (beg end)
  11522. "Convert all lines in region to list items.
  11523. If the first line is already an item, convert all list items in the region
  11524. to normal lines."
  11525. (interactive "r")
  11526. (let (l2 l)
  11527. (save-excursion
  11528. (goto-char end)
  11529. (setq l2 (org-current-line))
  11530. (goto-char beg)
  11531. (beginning-of-line 1)
  11532. (setq l (1- (org-current-line)))
  11533. (if (org-on-heading-p)
  11534. ;; We already have headlines, de-star them
  11535. (while (< (setq l (1+ l)) l2)
  11536. (when (org-on-heading-p t)
  11537. (and (looking-at outline-regexp) (replace-match "")))
  11538. (beginning-of-line 2))
  11539. (let* ((stars (save-excursion
  11540. (re-search-backward org-complex-heading-regexp nil t)
  11541. (or (match-string 1) "*")))
  11542. (add-stars (if org-odd-levels-only "**" "*"))
  11543. (rpl (concat stars add-stars " \\2")))
  11544. (while (< (setq l (1+ l)) l2)
  11545. (unless (org-on-heading-p)
  11546. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11547. (replace-match rpl)))
  11548. (beginning-of-line 2)))))))
  11549. (defun org-meta-return (&optional arg)
  11550. "Insert a new heading or wrap a region in a table.
  11551. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11552. See the individual commands for more information."
  11553. (interactive "P")
  11554. (cond
  11555. ((org-at-table-p)
  11556. (call-interactively 'org-table-wrap-region))
  11557. (t (call-interactively 'org-insert-heading))))
  11558. ;;; Menu entries
  11559. ;; Define the Org-mode menus
  11560. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11561. '("Tbl"
  11562. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11563. ["Next Field" org-cycle (org-at-table-p)]
  11564. ["Previous Field" org-shifttab (org-at-table-p)]
  11565. ["Next Row" org-return (org-at-table-p)]
  11566. "--"
  11567. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11568. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11569. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11570. "--"
  11571. ("Column"
  11572. ["Move Column Left" org-metaleft (org-at-table-p)]
  11573. ["Move Column Right" org-metaright (org-at-table-p)]
  11574. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11575. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11576. ("Row"
  11577. ["Move Row Up" org-metaup (org-at-table-p)]
  11578. ["Move Row Down" org-metadown (org-at-table-p)]
  11579. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11580. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11581. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11582. "--"
  11583. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11584. ("Rectangle"
  11585. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11586. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11587. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11588. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11589. "--"
  11590. ("Calculate"
  11591. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11592. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11593. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  11594. "--"
  11595. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11596. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11597. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11598. "--"
  11599. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11600. "--"
  11601. ["Sum Column/Rectangle" org-table-sum
  11602. (or (org-at-table-p) (org-region-active-p))]
  11603. ["Which Column?" org-table-current-column (org-at-table-p)])
  11604. ["Debug Formulas"
  11605. org-table-toggle-formula-debugger
  11606. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11607. ["Show Col/Row Numbers"
  11608. org-table-toggle-coordinate-overlays
  11609. :style toggle
  11610. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11611. "--"
  11612. ["Create" org-table-create (and (not (org-at-table-p))
  11613. org-enable-table-editor)]
  11614. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11615. ["Import from File" org-table-import (not (org-at-table-p))]
  11616. ["Export to File" org-table-export (org-at-table-p)]
  11617. "--"
  11618. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11619. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11620. '("Org"
  11621. ("Show/Hide"
  11622. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11623. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11624. ["Sparse Tree..." org-sparse-tree t]
  11625. ["Reveal Context" org-reveal t]
  11626. ["Show All" show-all t]
  11627. "--"
  11628. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11629. "--"
  11630. ["New Heading" org-insert-heading t]
  11631. ("Navigate Headings"
  11632. ["Up" outline-up-heading t]
  11633. ["Next" outline-next-visible-heading t]
  11634. ["Previous" outline-previous-visible-heading t]
  11635. ["Next Same Level" outline-forward-same-level t]
  11636. ["Previous Same Level" outline-backward-same-level t]
  11637. "--"
  11638. ["Jump" org-goto t])
  11639. ("Edit Structure"
  11640. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11641. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11642. "--"
  11643. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11644. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11645. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11646. "--"
  11647. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11648. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11649. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11650. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11651. "--"
  11652. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11653. "--"
  11654. ["Convert to odd levels" org-convert-to-odd-levels t]
  11655. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11656. ("Editing"
  11657. ["Emphasis..." org-emphasize t])
  11658. ("Archive"
  11659. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11660. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11661. ; :active t :keys "C-u C-c C-x C-a"]
  11662. ["Sparse trees open ARCHIVE trees"
  11663. (setq org-sparse-tree-open-archived-trees
  11664. (not org-sparse-tree-open-archived-trees))
  11665. :style toggle :selected org-sparse-tree-open-archived-trees]
  11666. ["Cycling opens ARCHIVE trees"
  11667. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11668. :style toggle :selected org-cycle-open-archived-trees]
  11669. ["Agenda includes ARCHIVE trees"
  11670. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  11671. :style toggle :selected (not org-agenda-skip-archived-trees)]
  11672. "--"
  11673. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11674. ; ["Check and Move Children" (org-archive-subtree '(4))
  11675. ; :active t :keys "C-u C-c C-x C-s"]
  11676. )
  11677. "--"
  11678. ("TODO Lists"
  11679. ["TODO/DONE/-" org-todo t]
  11680. ("Select keyword"
  11681. ["Next keyword" org-shiftright (org-on-heading-p)]
  11682. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11683. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11684. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11685. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11686. ["Show TODO Tree" org-show-todo-tree t]
  11687. ["Global TODO list" org-todo-list t]
  11688. "--"
  11689. ["Set Priority" org-priority t]
  11690. ["Priority Up" org-shiftup t]
  11691. ["Priority Down" org-shiftdown t])
  11692. ("TAGS and Properties"
  11693. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  11694. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11695. "--"
  11696. ["Set property" 'org-set-property t]
  11697. ["Column view of properties" org-columns t]
  11698. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11699. ("Dates and Scheduling"
  11700. ["Timestamp" org-time-stamp t]
  11701. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11702. ("Change Date"
  11703. ["1 Day Later" org-shiftright t]
  11704. ["1 Day Earlier" org-shiftleft t]
  11705. ["1 ... Later" org-shiftup t]
  11706. ["1 ... Earlier" org-shiftdown t])
  11707. ["Compute Time Range" org-evaluate-time-range t]
  11708. ["Schedule Item" org-schedule t]
  11709. ["Deadline" org-deadline t]
  11710. "--"
  11711. ["Custom time format" org-toggle-time-stamp-overlays
  11712. :style radio :selected org-display-custom-times]
  11713. "--"
  11714. ["Goto Calendar" org-goto-calendar t]
  11715. ["Date from Calendar" org-date-from-calendar t])
  11716. ("Logging work"
  11717. ["Clock in" org-clock-in t]
  11718. ["Clock out" org-clock-out t]
  11719. ["Clock cancel" org-clock-cancel t]
  11720. ["Goto running clock" org-clock-goto t]
  11721. ["Display times" org-clock-display t]
  11722. ["Create clock table" org-clock-report t]
  11723. "--"
  11724. ["Record DONE time"
  11725. (progn (setq org-log-done (not org-log-done))
  11726. (message "Switching to %s will %s record a timestamp"
  11727. (car org-done-keywords)
  11728. (if org-log-done "automatically" "not")))
  11729. :style toggle :selected org-log-done])
  11730. "--"
  11731. ["Agenda Command..." org-agenda t]
  11732. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11733. ("File List for Agenda")
  11734. ("Special views current file"
  11735. ["TODO Tree" org-show-todo-tree t]
  11736. ["Check Deadlines" org-check-deadlines t]
  11737. ["Timeline" org-timeline t]
  11738. ["Tags Tree" org-tags-sparse-tree t])
  11739. "--"
  11740. ("Hyperlinks"
  11741. ["Store Link (Global)" org-store-link t]
  11742. ["Insert Link" org-insert-link t]
  11743. ["Follow Link" org-open-at-point t]
  11744. "--"
  11745. ["Next link" org-next-link t]
  11746. ["Previous link" org-previous-link t]
  11747. "--"
  11748. ["Descriptive Links"
  11749. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11750. :style radio
  11751. :selected (member '(org-link) buffer-invisibility-spec)]
  11752. ["Literal Links"
  11753. (progn
  11754. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11755. :style radio
  11756. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11757. "--"
  11758. ["Export/Publish..." org-export t]
  11759. ("LaTeX"
  11760. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11761. :selected org-cdlatex-mode]
  11762. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11763. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11764. ["Modify math symbol" org-cdlatex-math-modify
  11765. (org-inside-LaTeX-fragment-p)]
  11766. ["Export LaTeX fragments as images"
  11767. (if (featurep 'org-exp)
  11768. (setq org-export-with-LaTeX-fragments
  11769. (not org-export-with-LaTeX-fragments))
  11770. (require 'org-exp))
  11771. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11772. org-export-with-LaTeX-fragments)])
  11773. "--"
  11774. ("Documentation"
  11775. ["Show Version" org-version t]
  11776. ["Info Documentation" org-info t])
  11777. ("Customize"
  11778. ["Browse Org Group" org-customize t]
  11779. "--"
  11780. ["Expand This Menu" org-create-customize-menu
  11781. (fboundp 'customize-menu-create)])
  11782. "--"
  11783. ["Refresh setup" org-mode-restart t]
  11784. ))
  11785. (defun org-info (&optional node)
  11786. "Read documentation for Org-mode in the info system.
  11787. With optional NODE, go directly to that node."
  11788. (interactive)
  11789. (info (format "(org)%s" (or node ""))))
  11790. (defun org-install-agenda-files-menu ()
  11791. (let ((bl (buffer-list)))
  11792. (save-excursion
  11793. (while bl
  11794. (set-buffer (pop bl))
  11795. (if (org-mode-p) (setq bl nil)))
  11796. (when (org-mode-p)
  11797. (easy-menu-change
  11798. '("Org") "File List for Agenda"
  11799. (append
  11800. (list
  11801. ["Edit File List" (org-edit-agenda-file-list) t]
  11802. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11803. ["Remove Current File from List" org-remove-file t]
  11804. ["Cycle through agenda files" org-cycle-agenda-files t]
  11805. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11806. "--")
  11807. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11808. ;;;; Documentation
  11809. (defun org-require-autoloaded-modules ()
  11810. (interactive)
  11811. (mapc 'require
  11812. '(org-agenda org-archive org-clock org-colview
  11813. org-exp org-export-latex org-publish
  11814. org-remember org-table)))
  11815. (defun org-customize ()
  11816. "Call the customize function with org as argument."
  11817. (interactive)
  11818. (org-load-modules-maybe)
  11819. (org-require-autoloaded-modules)
  11820. (customize-browse 'org))
  11821. (defun org-create-customize-menu ()
  11822. "Create a full customization menu for Org-mode, insert it into the menu."
  11823. (interactive)
  11824. (org-load-modules-maybe)
  11825. (org-require-autoloaded-modules)
  11826. (if (fboundp 'customize-menu-create)
  11827. (progn
  11828. (easy-menu-change
  11829. '("Org") "Customize"
  11830. `(["Browse Org group" org-customize t]
  11831. "--"
  11832. ,(customize-menu-create 'org)
  11833. ["Set" Custom-set t]
  11834. ["Save" Custom-save t]
  11835. ["Reset to Current" Custom-reset-current t]
  11836. ["Reset to Saved" Custom-reset-saved t]
  11837. ["Reset to Standard Settings" Custom-reset-standard t]))
  11838. (message "\"Org\"-menu now contains full customization menu"))
  11839. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  11840. ;;;; Miscellaneous stuff
  11841. ;;; Generally useful functions
  11842. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  11843. "Display the given MESSAGE as a warning."
  11844. (if (fboundp 'display-warning)
  11845. (display-warning 'org message
  11846. (if (featurep 'xemacs)
  11847. 'warning
  11848. :warning))
  11849. (let ((buf (get-buffer-create "*Org warnings*")))
  11850. (with-current-buffer buf
  11851. (goto-char (point-max))
  11852. (insert "Warning (Org): " message)
  11853. (unless (bolp)
  11854. (newline)))
  11855. (display-buffer buf)
  11856. (sit-for 0))))
  11857. (defun org-quote-csv-field (s)
  11858. "Quote field for inclusion in CSV material."
  11859. (if (string-match "[\",]" s)
  11860. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  11861. s))
  11862. (defun org-plist-delete (plist property)
  11863. "Delete PROPERTY from PLIST.
  11864. This is in contrast to merely setting it to 0."
  11865. (let (p)
  11866. (while plist
  11867. (if (not (eq property (car plist)))
  11868. (setq p (plist-put p (car plist) (nth 1 plist))))
  11869. (setq plist (cddr plist)))
  11870. p))
  11871. (defun org-force-self-insert (N)
  11872. "Needed to enforce self-insert under remapping."
  11873. (interactive "p")
  11874. (self-insert-command N))
  11875. (defun org-string-width (s)
  11876. "Compute width of string, ignoring invisible characters.
  11877. This ignores character with invisibility property `org-link', and also
  11878. characters with property `org-cwidth', because these will become invisible
  11879. upon the next fontification round."
  11880. (let (b l)
  11881. (when (or (eq t buffer-invisibility-spec)
  11882. (assq 'org-link buffer-invisibility-spec))
  11883. (while (setq b (text-property-any 0 (length s)
  11884. 'invisible 'org-link s))
  11885. (setq s (concat (substring s 0 b)
  11886. (substring s (or (next-single-property-change
  11887. b 'invisible s) (length s)))))))
  11888. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  11889. (setq s (concat (substring s 0 b)
  11890. (substring s (or (next-single-property-change
  11891. b 'org-cwidth s) (length s))))))
  11892. (setq l (string-width s) b -1)
  11893. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  11894. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  11895. l))
  11896. (defun org-base-buffer (buffer)
  11897. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  11898. (if (not buffer)
  11899. buffer
  11900. (or (buffer-base-buffer buffer)
  11901. buffer)))
  11902. (defun org-trim (s)
  11903. "Remove whitespace at beginning and end of string."
  11904. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  11905. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  11906. s)
  11907. (defun org-wrap (string &optional width lines)
  11908. "Wrap string to either a number of lines, or a width in characters.
  11909. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  11910. that costs. If there is a word longer than WIDTH, the text is actually
  11911. wrapped to the length of that word.
  11912. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  11913. many lines, whatever width that takes.
  11914. The return value is a list of lines, without newlines at the end."
  11915. (let* ((words (org-split-string string "[ \t\n]+"))
  11916. (maxword (apply 'max (mapcar 'org-string-width words)))
  11917. w ll)
  11918. (cond (width
  11919. (org-do-wrap words (max maxword width)))
  11920. (lines
  11921. (setq w maxword)
  11922. (setq ll (org-do-wrap words maxword))
  11923. (if (<= (length ll) lines)
  11924. ll
  11925. (setq ll words)
  11926. (while (> (length ll) lines)
  11927. (setq w (1+ w))
  11928. (setq ll (org-do-wrap words w)))
  11929. ll))
  11930. (t (error "Cannot wrap this")))))
  11931. (defun org-do-wrap (words width)
  11932. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  11933. (let (lines line)
  11934. (while words
  11935. (setq line (pop words))
  11936. (while (and words (< (+ (length line) (length (car words))) width))
  11937. (setq line (concat line " " (pop words))))
  11938. (setq lines (push line lines)))
  11939. (nreverse lines)))
  11940. (defun org-split-string (string &optional separators)
  11941. "Splits STRING into substrings at SEPARATORS.
  11942. No empty strings are returned if there are matches at the beginning
  11943. and end of string."
  11944. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  11945. (start 0)
  11946. notfirst
  11947. (list nil))
  11948. (while (and (string-match rexp string
  11949. (if (and notfirst
  11950. (= start (match-beginning 0))
  11951. (< start (length string)))
  11952. (1+ start) start))
  11953. (< (match-beginning 0) (length string)))
  11954. (setq notfirst t)
  11955. (or (eq (match-beginning 0) 0)
  11956. (and (eq (match-beginning 0) (match-end 0))
  11957. (eq (match-beginning 0) start))
  11958. (setq list
  11959. (cons (substring string start (match-beginning 0))
  11960. list)))
  11961. (setq start (match-end 0)))
  11962. (or (eq start (length string))
  11963. (setq list
  11964. (cons (substring string start)
  11965. list)))
  11966. (nreverse list)))
  11967. (defun org-context ()
  11968. "Return a list of contexts of the current cursor position.
  11969. If several contexts apply, all are returned.
  11970. Each context entry is a list with a symbol naming the context, and
  11971. two positions indicating start and end of the context. Possible
  11972. contexts are:
  11973. :headline anywhere in a headline
  11974. :headline-stars on the leading stars in a headline
  11975. :todo-keyword on a TODO keyword (including DONE) in a headline
  11976. :tags on the TAGS in a headline
  11977. :priority on the priority cookie in a headline
  11978. :item on the first line of a plain list item
  11979. :item-bullet on the bullet/number of a plain list item
  11980. :checkbox on the checkbox in a plain list item
  11981. :table in an org-mode table
  11982. :table-special on a special filed in a table
  11983. :table-table in a table.el table
  11984. :link on a hyperlink
  11985. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  11986. :target on a <<target>>
  11987. :radio-target on a <<<radio-target>>>
  11988. :latex-fragment on a LaTeX fragment
  11989. :latex-preview on a LaTeX fragment with overlayed preview image
  11990. This function expects the position to be visible because it uses font-lock
  11991. faces as a help to recognize the following contexts: :table-special, :link,
  11992. and :keyword."
  11993. (let* ((f (get-text-property (point) 'face))
  11994. (faces (if (listp f) f (list f)))
  11995. (p (point)) clist o)
  11996. ;; First the large context
  11997. (cond
  11998. ((org-on-heading-p t)
  11999. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12000. (when (progn
  12001. (beginning-of-line 1)
  12002. (looking-at org-todo-line-tags-regexp))
  12003. (push (org-point-in-group p 1 :headline-stars) clist)
  12004. (push (org-point-in-group p 2 :todo-keyword) clist)
  12005. (push (org-point-in-group p 4 :tags) clist))
  12006. (goto-char p)
  12007. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12008. (if (looking-at "\\[#[A-Z0-9]\\]")
  12009. (push (org-point-in-group p 0 :priority) clist)))
  12010. ((org-at-item-p)
  12011. (push (org-point-in-group p 2 :item-bullet) clist)
  12012. (push (list :item (point-at-bol)
  12013. (save-excursion (org-end-of-item) (point)))
  12014. clist)
  12015. (and (org-at-item-checkbox-p)
  12016. (push (org-point-in-group p 0 :checkbox) clist)))
  12017. ((org-at-table-p)
  12018. (push (list :table (org-table-begin) (org-table-end)) clist)
  12019. (if (memq 'org-formula faces)
  12020. (push (list :table-special
  12021. (previous-single-property-change p 'face)
  12022. (next-single-property-change p 'face)) clist)))
  12023. ((org-at-table-p 'any)
  12024. (push (list :table-table) clist)))
  12025. (goto-char p)
  12026. ;; Now the small context
  12027. (cond
  12028. ((org-at-timestamp-p)
  12029. (push (org-point-in-group p 0 :timestamp) clist))
  12030. ((memq 'org-link faces)
  12031. (push (list :link
  12032. (previous-single-property-change p 'face)
  12033. (next-single-property-change p 'face)) clist))
  12034. ((memq 'org-special-keyword faces)
  12035. (push (list :keyword
  12036. (previous-single-property-change p 'face)
  12037. (next-single-property-change p 'face)) clist))
  12038. ((org-on-target-p)
  12039. (push (org-point-in-group p 0 :target) clist)
  12040. (goto-char (1- (match-beginning 0)))
  12041. (if (looking-at org-radio-target-regexp)
  12042. (push (org-point-in-group p 0 :radio-target) clist))
  12043. (goto-char p))
  12044. ((setq o (car (delq nil
  12045. (mapcar
  12046. (lambda (x)
  12047. (if (memq x org-latex-fragment-image-overlays) x))
  12048. (org-overlays-at (point))))))
  12049. (push (list :latex-fragment
  12050. (org-overlay-start o) (org-overlay-end o)) clist)
  12051. (push (list :latex-preview
  12052. (org-overlay-start o) (org-overlay-end o)) clist))
  12053. ((org-inside-LaTeX-fragment-p)
  12054. ;; FIXME: positions wrong.
  12055. (push (list :latex-fragment (point) (point)) clist)))
  12056. (setq clist (nreverse (delq nil clist)))
  12057. clist))
  12058. ;; FIXME: Compare with at-regexp-p Do we need both?
  12059. (defun org-in-regexp (re &optional nlines visually)
  12060. "Check if point is inside a match of regexp.
  12061. Normally only the current line is checked, but you can include NLINES extra
  12062. lines both before and after point into the search.
  12063. If VISUALLY is set, require that the cursor is not after the match but
  12064. really on, so that the block visually is on the match."
  12065. (catch 'exit
  12066. (let ((pos (point))
  12067. (eol (point-at-eol (+ 1 (or nlines 0))))
  12068. (inc (if visually 1 0)))
  12069. (save-excursion
  12070. (beginning-of-line (- 1 (or nlines 0)))
  12071. (while (re-search-forward re eol t)
  12072. (if (and (<= (match-beginning 0) pos)
  12073. (>= (+ inc (match-end 0)) pos))
  12074. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12075. (defun org-at-regexp-p (regexp)
  12076. "Is point inside a match of REGEXP in the current line?"
  12077. (catch 'exit
  12078. (save-excursion
  12079. (let ((pos (point)) (end (point-at-eol)))
  12080. (beginning-of-line 1)
  12081. (while (re-search-forward regexp end t)
  12082. (if (and (<= (match-beginning 0) pos)
  12083. (>= (match-end 0) pos))
  12084. (throw 'exit t)))
  12085. nil))))
  12086. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12087. "Call `multi-occur' with buffers for all agenda files."
  12088. (interactive "sOrg-files matching: \np")
  12089. (let* ((files (org-agenda-files))
  12090. (tnames (mapcar 'file-truename files))
  12091. (extra org-agenda-text-search-extra-files)
  12092. f)
  12093. (when (eq (car extra) 'agenda-archives)
  12094. (setq extra (cdr extra))
  12095. (setq files (org-add-archive-files files)))
  12096. (while (setq f (pop extra))
  12097. (unless (member (file-truename f) tnames)
  12098. (add-to-list 'files f 'append)
  12099. (add-to-list 'tnames (file-truename f) 'append)))
  12100. (multi-occur
  12101. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12102. regexp)))
  12103. (if (boundp 'occur-mode-find-occurrence-hook)
  12104. ;; Emacs 23
  12105. (add-hook 'occur-mode-find-occurrence-hook
  12106. (lambda ()
  12107. (when (org-mode-p)
  12108. (org-reveal))))
  12109. ;; Emacs 22
  12110. (defadvice occur-mode-goto-occurrence
  12111. (after org-occur-reveal activate)
  12112. (and (org-mode-p) (org-reveal)))
  12113. (defadvice occur-mode-goto-occurrence-other-window
  12114. (after org-occur-reveal activate)
  12115. (and (org-mode-p) (org-reveal)))
  12116. (defadvice occur-mode-display-occurrence
  12117. (after org-occur-reveal activate)
  12118. (when (org-mode-p)
  12119. (let ((pos (occur-mode-find-occurrence)))
  12120. (with-current-buffer (marker-buffer pos)
  12121. (save-excursion
  12122. (goto-char pos)
  12123. (org-reveal)))))))
  12124. (defun org-uniquify (list)
  12125. "Remove duplicate elements from LIST."
  12126. (let (res)
  12127. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12128. res))
  12129. (defun org-delete-all (elts list)
  12130. "Remove all elements in ELTS from LIST."
  12131. (while elts
  12132. (setq list (delete (pop elts) list)))
  12133. list)
  12134. (defun org-back-over-empty-lines ()
  12135. "Move backwards over witespace, to the beginning of the first empty line.
  12136. Returns the number of empty lines passed."
  12137. (let ((pos (point)))
  12138. (skip-chars-backward " \t\n\r")
  12139. (beginning-of-line 2)
  12140. (goto-char (min (point) pos))
  12141. (count-lines (point) pos)))
  12142. (defun org-skip-whitespace ()
  12143. (skip-chars-forward " \t\n\r"))
  12144. (defun org-point-in-group (point group &optional context)
  12145. "Check if POINT is in match-group GROUP.
  12146. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12147. match. If the match group does ot exist or point is not inside it,
  12148. return nil."
  12149. (and (match-beginning group)
  12150. (>= point (match-beginning group))
  12151. (<= point (match-end group))
  12152. (if context
  12153. (list context (match-beginning group) (match-end group))
  12154. t)))
  12155. (defun org-switch-to-buffer-other-window (&rest args)
  12156. "Switch to buffer in a second window on the current frame.
  12157. In particular, do not allow pop-up frames."
  12158. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12159. special-display-function)
  12160. (apply 'switch-to-buffer-other-window args)))
  12161. (defun org-combine-plists (&rest plists)
  12162. "Create a single property list from all plists in PLISTS.
  12163. The process starts by copying the first list, and then setting properties
  12164. from the other lists. Settings in the last list are the most significant
  12165. ones and overrule settings in the other lists."
  12166. (let ((rtn (copy-sequence (pop plists)))
  12167. p v ls)
  12168. (while plists
  12169. (setq ls (pop plists))
  12170. (while ls
  12171. (setq p (pop ls) v (pop ls))
  12172. (setq rtn (plist-put rtn p v))))
  12173. rtn))
  12174. (defun org-move-line-down (arg)
  12175. "Move the current line down. With prefix argument, move it past ARG lines."
  12176. (interactive "p")
  12177. (let ((col (current-column))
  12178. beg end pos)
  12179. (beginning-of-line 1) (setq beg (point))
  12180. (beginning-of-line 2) (setq end (point))
  12181. (beginning-of-line (+ 1 arg))
  12182. (setq pos (move-marker (make-marker) (point)))
  12183. (insert (delete-and-extract-region beg end))
  12184. (goto-char pos)
  12185. (org-move-to-column col)))
  12186. (defun org-move-line-up (arg)
  12187. "Move the current line up. With prefix argument, move it past ARG lines."
  12188. (interactive "p")
  12189. (let ((col (current-column))
  12190. beg end pos)
  12191. (beginning-of-line 1) (setq beg (point))
  12192. (beginning-of-line 2) (setq end (point))
  12193. (beginning-of-line (- arg))
  12194. (setq pos (move-marker (make-marker) (point)))
  12195. (insert (delete-and-extract-region beg end))
  12196. (goto-char pos)
  12197. (org-move-to-column col)))
  12198. (defun org-replace-escapes (string table)
  12199. "Replace %-escapes in STRING with values in TABLE.
  12200. TABLE is an association list with keys like \"%a\" and string values.
  12201. The sequences in STRING may contain normal field width and padding information,
  12202. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12203. so values can contain further %-escapes if they are define later in TABLE."
  12204. (let ((case-fold-search nil)
  12205. e re rpl)
  12206. (while (setq e (pop table))
  12207. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12208. (while (string-match re string)
  12209. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12210. (cdr e)))
  12211. (setq string (replace-match rpl t t string))))
  12212. string))
  12213. (defun org-sublist (list start end)
  12214. "Return a section of LIST, from START to END.
  12215. Counting starts at 1."
  12216. (let (rtn (c start))
  12217. (setq list (nthcdr (1- start) list))
  12218. (while (and list (<= c end))
  12219. (push (pop list) rtn)
  12220. (setq c (1+ c)))
  12221. (nreverse rtn)))
  12222. (defun org-find-base-buffer-visiting (file)
  12223. "Like `find-buffer-visiting' but alway return the base buffer and
  12224. not an indirect buffer."
  12225. (let ((buf (find-buffer-visiting file)))
  12226. (if buf
  12227. (or (buffer-base-buffer buf) buf)
  12228. nil)))
  12229. (defun org-image-file-name-regexp ()
  12230. "Return regexp matching the file names of images."
  12231. (if (fboundp 'image-file-name-regexp)
  12232. (image-file-name-regexp)
  12233. (let ((image-file-name-extensions
  12234. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12235. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12236. (concat "\\."
  12237. (regexp-opt (nconc (mapcar 'upcase
  12238. image-file-name-extensions)
  12239. image-file-name-extensions)
  12240. t)
  12241. "\\'"))))
  12242. (defun org-file-image-p (file)
  12243. "Return non-nil if FILE is an image."
  12244. (save-match-data
  12245. (string-match (org-image-file-name-regexp) file)))
  12246. ;;; Paragraph filling stuff.
  12247. ;; We want this to be just right, so use the full arsenal.
  12248. (defun org-indent-line-function ()
  12249. "Indent line like previous, but further if previous was headline or item."
  12250. (interactive)
  12251. (let* ((pos (point))
  12252. (itemp (org-at-item-p))
  12253. column bpos bcol tpos tcol bullet btype bullet-type)
  12254. ;; Find the previous relevant line
  12255. (beginning-of-line 1)
  12256. (cond
  12257. ((looking-at "#") (setq column 0))
  12258. ((looking-at "\\*+ ") (setq column 0))
  12259. (t
  12260. (beginning-of-line 0)
  12261. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12262. (beginning-of-line 0))
  12263. (cond
  12264. ((looking-at "\\*+[ \t]+")
  12265. (goto-char (match-end 0))
  12266. (setq column (current-column)))
  12267. ((org-in-item-p)
  12268. (org-beginning-of-item)
  12269. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12270. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  12271. (setq bpos (match-beginning 1) tpos (match-end 0)
  12272. bcol (progn (goto-char bpos) (current-column))
  12273. tcol (progn (goto-char tpos) (current-column))
  12274. bullet (match-string 1)
  12275. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12276. (if (not itemp)
  12277. (setq column tcol)
  12278. (goto-char pos)
  12279. (beginning-of-line 1)
  12280. (if (looking-at "\\S-")
  12281. (progn
  12282. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12283. (setq bullet (match-string 1)
  12284. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12285. (setq column (if (equal btype bullet-type) bcol tcol)))
  12286. (setq column (org-get-indentation)))))
  12287. (t (setq column (org-get-indentation))))))
  12288. (goto-char pos)
  12289. (if (<= (current-column) (current-indentation))
  12290. (org-indent-line-to column)
  12291. (save-excursion (org-indent-line-to column)))
  12292. (setq column (current-column))
  12293. (beginning-of-line 1)
  12294. (if (looking-at
  12295. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12296. (replace-match (concat "\\1" (format org-property-format
  12297. (match-string 2) (match-string 3)))
  12298. t nil))
  12299. (org-move-to-column column)))
  12300. (defun org-set-autofill-regexps ()
  12301. (interactive)
  12302. ;; In the paragraph separator we include headlines, because filling
  12303. ;; text in a line directly attached to a headline would otherwise
  12304. ;; fill the headline as well.
  12305. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12306. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12307. ;; The paragraph starter includes hand-formatted lists.
  12308. (org-set-local 'paragraph-start
  12309. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12310. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12311. ;; But only if the user has not turned off tables or fixed-width regions
  12312. (org-set-local
  12313. 'auto-fill-inhibit-regexp
  12314. (concat "\\*+ \\|#\\+"
  12315. "\\|[ \t]*" org-keyword-time-regexp
  12316. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12317. (concat
  12318. "\\|[ \t]*["
  12319. (if org-enable-table-editor "|" "")
  12320. (if org-enable-fixed-width-editor ":" "")
  12321. "]"))))
  12322. ;; We use our own fill-paragraph function, to make sure that tables
  12323. ;; and fixed-width regions are not wrapped. That function will pass
  12324. ;; through to `fill-paragraph' when appropriate.
  12325. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12326. ; Adaptive filling: To get full control, first make sure that
  12327. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12328. (org-set-local 'adaptive-fill-regexp "\000")
  12329. (org-set-local 'adaptive-fill-function
  12330. 'org-adaptive-fill-function)
  12331. (org-set-local
  12332. 'align-mode-rules-list
  12333. '((org-in-buffer-settings
  12334. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12335. (modes . '(org-mode))))))
  12336. (defun org-fill-paragraph (&optional justify)
  12337. "Re-align a table, pass through to fill-paragraph if no table."
  12338. (let ((table-p (org-at-table-p))
  12339. (table.el-p (org-at-table.el-p)))
  12340. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12341. (save-excursion (goto-char (point-at-bol))
  12342. (looking-at outline-regexp)))
  12343. t) ; skip headlines
  12344. (table.el-p t) ; skip table.el tables
  12345. (table-p (org-table-align) t) ; align org-mode tables
  12346. (t nil)))) ; call paragraph-fill
  12347. ;; For reference, this is the default value of adaptive-fill-regexp
  12348. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12349. (defun org-adaptive-fill-function ()
  12350. "Return a fill prefix for org-mode files.
  12351. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12352. work correctly."
  12353. (cond ((looking-at "#[ \t]+")
  12354. (match-string 0))
  12355. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12356. (save-excursion
  12357. (goto-char (match-end 0))
  12358. (make-string (current-column) ?\ )))
  12359. (t nil)))
  12360. ;;; Other stuff.
  12361. (defun org-toggle-fixed-width-section (arg)
  12362. "Toggle the fixed-width export.
  12363. If there is no active region, the QUOTE keyword at the current headline is
  12364. inserted or removed. When present, it causes the text between this headline
  12365. and the next to be exported as fixed-width text, and unmodified.
  12366. If there is an active region, this command adds or removes a colon as the
  12367. first character of this line. If the first character of a line is a colon,
  12368. this line is also exported in fixed-width font."
  12369. (interactive "P")
  12370. (let* ((cc 0)
  12371. (regionp (org-region-active-p))
  12372. (beg (if regionp (region-beginning) (point)))
  12373. (end (if regionp (region-end)))
  12374. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12375. (case-fold-search nil)
  12376. (re "[ \t]*\\(:\\)")
  12377. off)
  12378. (if regionp
  12379. (save-excursion
  12380. (goto-char beg)
  12381. (setq cc (current-column))
  12382. (beginning-of-line 1)
  12383. (setq off (looking-at re))
  12384. (while (> nlines 0)
  12385. (setq nlines (1- nlines))
  12386. (beginning-of-line 1)
  12387. (cond
  12388. (arg
  12389. (org-move-to-column cc t)
  12390. (insert ":\n")
  12391. (forward-line -1))
  12392. ((and off (looking-at re))
  12393. (replace-match "" t t nil 1))
  12394. ((not off) (org-move-to-column cc t) (insert ":")))
  12395. (forward-line 1)))
  12396. (save-excursion
  12397. (org-back-to-heading)
  12398. (if (looking-at (concat outline-regexp
  12399. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12400. (replace-match "" t t nil 1)
  12401. (if (looking-at outline-regexp)
  12402. (progn
  12403. (goto-char (match-end 0))
  12404. (insert org-quote-string " "))))))))
  12405. ;;;; Functions extending outline functionality
  12406. (defun org-beginning-of-line (&optional arg)
  12407. "Go to the beginning of the current line. If that is invisible, continue
  12408. to a visible line beginning. This makes the function of C-a more intuitive.
  12409. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12410. first attempt, and only move to after the tags when the cursor is already
  12411. beyond the end of the headline."
  12412. (interactive "P")
  12413. (let ((pos (point)))
  12414. (beginning-of-line 1)
  12415. (if (bobp)
  12416. nil
  12417. (backward-char 1)
  12418. (if (org-invisible-p)
  12419. (while (and (not (bobp)) (org-invisible-p))
  12420. (backward-char 1)
  12421. (beginning-of-line 1))
  12422. (forward-char 1)))
  12423. (when org-special-ctrl-a/e
  12424. (cond
  12425. ((and (looking-at org-todo-line-regexp)
  12426. (= (char-after (match-end 1)) ?\ ))
  12427. (goto-char
  12428. (if (eq org-special-ctrl-a/e t)
  12429. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  12430. ((= pos (point)) (match-beginning 3))
  12431. (t (point)))
  12432. (cond ((> pos (point)) (point))
  12433. ((not (eq last-command this-command)) (point))
  12434. (t (match-beginning 3))))))
  12435. ((org-at-item-p)
  12436. (goto-char
  12437. (if (eq org-special-ctrl-a/e t)
  12438. (cond ((> pos (match-end 4)) (match-end 4))
  12439. ((= pos (point)) (match-end 4))
  12440. (t (point)))
  12441. (cond ((> pos (point)) (point))
  12442. ((not (eq last-command this-command)) (point))
  12443. (t (match-end 4))))))))))
  12444. (defun org-end-of-line (&optional arg)
  12445. "Go to the end of the line.
  12446. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12447. first attempt, and only move to after the tags when the cursor is already
  12448. beyond the end of the headline."
  12449. (interactive "P")
  12450. (if (or (not org-special-ctrl-a/e)
  12451. (not (org-on-heading-p)))
  12452. (end-of-line arg)
  12453. (let ((pos (point)))
  12454. (beginning-of-line 1)
  12455. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12456. (if (eq org-special-ctrl-a/e t)
  12457. (if (or (< pos (match-beginning 1))
  12458. (= pos (match-end 0)))
  12459. (goto-char (match-beginning 1))
  12460. (goto-char (match-end 0)))
  12461. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12462. (goto-char (match-end 0))
  12463. (goto-char (match-beginning 1))))
  12464. (end-of-line arg)))))
  12465. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12466. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12467. (defun org-kill-line (&optional arg)
  12468. "Kill line, to tags or end of line."
  12469. (interactive "P")
  12470. (cond
  12471. ((or (not org-special-ctrl-k)
  12472. (bolp)
  12473. (not (org-on-heading-p)))
  12474. (call-interactively 'kill-line))
  12475. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12476. (kill-region (point) (match-beginning 1))
  12477. (org-set-tags nil t))
  12478. (t (kill-region (point) (point-at-eol)))))
  12479. (define-key org-mode-map "\C-k" 'org-kill-line)
  12480. (defun org-invisible-p ()
  12481. "Check if point is at a character currently not visible."
  12482. ;; Early versions of noutline don't have `outline-invisible-p'.
  12483. (if (fboundp 'outline-invisible-p)
  12484. (outline-invisible-p)
  12485. (get-char-property (point) 'invisible)))
  12486. (defun org-invisible-p2 ()
  12487. "Check if point is at a character currently not visible."
  12488. (save-excursion
  12489. (if (and (eolp) (not (bobp))) (backward-char 1))
  12490. ;; Early versions of noutline don't have `outline-invisible-p'.
  12491. (if (fboundp 'outline-invisible-p)
  12492. (outline-invisible-p)
  12493. (get-char-property (point) 'invisible))))
  12494. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12495. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12496. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12497. (defun org-at-heading-or-item-p ()
  12498. (or (org-on-heading-p) (org-at-item-p)))
  12499. (defun org-on-target-p ()
  12500. (or (org-in-regexp org-radio-target-regexp)
  12501. (org-in-regexp org-target-regexp)))
  12502. (defun org-up-heading-all (arg)
  12503. "Move to the heading line of which the present line is a subheading.
  12504. This function considers both visible and invisible heading lines.
  12505. With argument, move up ARG levels."
  12506. (if (fboundp 'outline-up-heading-all)
  12507. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12508. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12509. (defun org-up-heading-safe ()
  12510. "Move to the heading line of which the present line is a subheading.
  12511. This version will not throw an error. It will return the level of the
  12512. headline found, or nil if no higher level is found."
  12513. (let ((pos (point)) start-level level
  12514. (re (concat "^" outline-regexp)))
  12515. (catch 'exit
  12516. (outline-back-to-heading t)
  12517. (setq start-level (funcall outline-level))
  12518. (if (equal start-level 1) (throw 'exit nil))
  12519. (while (re-search-backward re nil t)
  12520. (setq level (funcall outline-level))
  12521. (if (< level start-level) (throw 'exit level)))
  12522. nil)))
  12523. (defun org-first-sibling-p ()
  12524. "Is this heading the first child of its parents?"
  12525. (interactive)
  12526. (let ((re (concat "^" outline-regexp))
  12527. level l)
  12528. (unless (org-at-heading-p t)
  12529. (error "Not at a heading"))
  12530. (setq level (funcall outline-level))
  12531. (save-excursion
  12532. (if (not (re-search-backward re nil t))
  12533. t
  12534. (setq l (funcall outline-level))
  12535. (< l level)))))
  12536. (defun org-goto-sibling (&optional previous)
  12537. "Goto the next sibling, even if it is invisible.
  12538. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12539. when a sibling was found. When none is found, return nil and don't
  12540. move point."
  12541. (let ((fun (if previous 're-search-backward 're-search-forward))
  12542. (pos (point))
  12543. (re (concat "^" outline-regexp))
  12544. level l)
  12545. (when (condition-case nil (org-back-to-heading t) (error nil))
  12546. (setq level (funcall outline-level))
  12547. (catch 'exit
  12548. (or previous (forward-char 1))
  12549. (while (funcall fun re nil t)
  12550. (setq l (funcall outline-level))
  12551. (when (< l level) (goto-char pos) (throw 'exit nil))
  12552. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12553. (goto-char pos)
  12554. nil))))
  12555. (defun org-show-siblings ()
  12556. "Show all siblings of the current headline."
  12557. (save-excursion
  12558. (while (org-goto-sibling) (org-flag-heading nil)))
  12559. (save-excursion
  12560. (while (org-goto-sibling 'previous)
  12561. (org-flag-heading nil))))
  12562. (defun org-show-hidden-entry ()
  12563. "Show an entry where even the heading is hidden."
  12564. (save-excursion
  12565. (org-show-entry)))
  12566. (defun org-flag-heading (flag &optional entry)
  12567. "Flag the current heading. FLAG non-nil means make invisible.
  12568. When ENTRY is non-nil, show the entire entry."
  12569. (save-excursion
  12570. (org-back-to-heading t)
  12571. ;; Check if we should show the entire entry
  12572. (if entry
  12573. (progn
  12574. (org-show-entry)
  12575. (save-excursion
  12576. (and (outline-next-heading)
  12577. (org-flag-heading nil))))
  12578. (outline-flag-region (max (point-min) (1- (point)))
  12579. (save-excursion (outline-end-of-heading) (point))
  12580. flag))))
  12581. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12582. ;; This is an exact copy of the original function, but it uses
  12583. ;; `org-back-to-heading', to make it work also in invisible
  12584. ;; trees. And is uses an invisible-OK argument.
  12585. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12586. (org-back-to-heading invisible-OK)
  12587. (let ((first t)
  12588. (level (funcall outline-level)))
  12589. (while (and (not (eobp))
  12590. (or first (> (funcall outline-level) level)))
  12591. (setq first nil)
  12592. (outline-next-heading))
  12593. (unless to-heading
  12594. (if (memq (preceding-char) '(?\n ?\^M))
  12595. (progn
  12596. ;; Go to end of line before heading
  12597. (forward-char -1)
  12598. (if (memq (preceding-char) '(?\n ?\^M))
  12599. ;; leave blank line before heading
  12600. (forward-char -1))))))
  12601. (point))
  12602. (defun org-show-subtree ()
  12603. "Show everything after this heading at deeper levels."
  12604. (outline-flag-region
  12605. (point)
  12606. (save-excursion
  12607. (outline-end-of-subtree) (outline-next-heading) (point))
  12608. nil))
  12609. (defun org-show-entry ()
  12610. "Show the body directly following this heading.
  12611. Show the heading too, if it is currently invisible."
  12612. (interactive)
  12613. (save-excursion
  12614. (condition-case nil
  12615. (progn
  12616. (org-back-to-heading t)
  12617. (outline-flag-region
  12618. (max (point-min) (1- (point)))
  12619. (save-excursion
  12620. (re-search-forward
  12621. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12622. (or (match-beginning 1) (point-max)))
  12623. nil))
  12624. (error nil))))
  12625. (defun org-make-options-regexp (kwds)
  12626. "Make a regular expression for keyword lines."
  12627. (concat
  12628. "^"
  12629. "#?[ \t]*\\+\\("
  12630. (mapconcat 'regexp-quote kwds "\\|")
  12631. "\\):[ \t]*"
  12632. "\\(.+\\)"))
  12633. ;; Make isearch reveal the necessary context
  12634. (defun org-isearch-end ()
  12635. "Reveal context after isearch exits."
  12636. (when isearch-success ; only if search was successful
  12637. (if (featurep 'xemacs)
  12638. ;; Under XEmacs, the hook is run in the correct place,
  12639. ;; we directly show the context.
  12640. (org-show-context 'isearch)
  12641. ;; In Emacs the hook runs *before* restoring the overlays.
  12642. ;; So we have to use a one-time post-command-hook to do this.
  12643. ;; (Emacs 22 has a special variable, see function `org-mode')
  12644. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12645. isearch-mode-end-hook-quit)
  12646. ;; Only when the isearch was not quitted.
  12647. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12648. 'append 'local)))))
  12649. (defun org-isearch-post-command ()
  12650. "Remove self from hook, and show context."
  12651. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12652. (org-show-context 'isearch))
  12653. ;;;; Integration with and fixes for other packages
  12654. ;;; Imenu support
  12655. (defvar org-imenu-markers nil
  12656. "All markers currently used by Imenu.")
  12657. (make-variable-buffer-local 'org-imenu-markers)
  12658. (defun org-imenu-new-marker (&optional pos)
  12659. "Return a new marker for use by Imenu, and remember the marker."
  12660. (let ((m (make-marker)))
  12661. (move-marker m (or pos (point)))
  12662. (push m org-imenu-markers)
  12663. m))
  12664. (defun org-imenu-get-tree ()
  12665. "Produce the index for Imenu."
  12666. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12667. (setq org-imenu-markers nil)
  12668. (let* ((n org-imenu-depth)
  12669. (re (concat "^" outline-regexp))
  12670. (subs (make-vector (1+ n) nil))
  12671. (last-level 0)
  12672. m tree level head)
  12673. (save-excursion
  12674. (save-restriction
  12675. (widen)
  12676. (goto-char (point-max))
  12677. (while (re-search-backward re nil t)
  12678. (setq level (org-reduced-level (funcall outline-level)))
  12679. (when (<= level n)
  12680. (looking-at org-complex-heading-regexp)
  12681. (setq head (org-match-string-no-properties 4)
  12682. m (org-imenu-new-marker))
  12683. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12684. (if (>= level last-level)
  12685. (push (cons head m) (aref subs level))
  12686. (push (cons head (aref subs (1+ level))) (aref subs level))
  12687. (loop for i from (1+ level) to n do (aset subs i nil)))
  12688. (setq last-level level)))))
  12689. (aref subs 1)))
  12690. (eval-after-load "imenu"
  12691. '(progn
  12692. (add-hook 'imenu-after-jump-hook
  12693. (lambda () (org-show-context 'org-goto)))))
  12694. ;; Speedbar support
  12695. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12696. "Overlay marking the agenda restriction line in speedbar.")
  12697. (org-overlay-put org-speedbar-restriction-lock-overlay
  12698. 'face 'org-agenda-restriction-lock)
  12699. (org-overlay-put org-speedbar-restriction-lock-overlay
  12700. 'help-echo "Agendas are currently limited to this item.")
  12701. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  12702. (defun org-speedbar-set-agenda-restriction ()
  12703. "Restrict future agenda commands to the location at point in speedbar.
  12704. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  12705. (interactive)
  12706. (require 'org-agenda)
  12707. (let (p m tp np dir txt w)
  12708. (cond
  12709. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12710. 'org-imenu t))
  12711. (setq m (get-text-property p 'org-imenu-marker))
  12712. (save-excursion
  12713. (save-restriction
  12714. (set-buffer (marker-buffer m))
  12715. (goto-char m)
  12716. (org-agenda-set-restriction-lock 'subtree))))
  12717. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12718. 'speedbar-function 'speedbar-find-file))
  12719. (setq tp (previous-single-property-change
  12720. (1+ p) 'speedbar-function)
  12721. np (next-single-property-change
  12722. tp 'speedbar-function)
  12723. dir (speedbar-line-directory)
  12724. txt (buffer-substring-no-properties (or tp (point-min))
  12725. (or np (point-max))))
  12726. (save-excursion
  12727. (save-restriction
  12728. (set-buffer (find-file-noselect
  12729. (let ((default-directory dir))
  12730. (expand-file-name txt))))
  12731. (unless (org-mode-p)
  12732. (error "Cannot restrict to non-Org-mode file"))
  12733. (org-agenda-set-restriction-lock 'file))))
  12734. (t (error "Don't know how to restrict Org-mode's agenda")))
  12735. (org-move-overlay org-speedbar-restriction-lock-overlay
  12736. (point-at-bol) (point-at-eol))
  12737. (setq current-prefix-arg nil)
  12738. (org-agenda-maybe-redo)))
  12739. (eval-after-load "speedbar"
  12740. '(progn
  12741. (speedbar-add-supported-extension ".org")
  12742. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  12743. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  12744. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  12745. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12746. (add-hook 'speedbar-visiting-tag-hook
  12747. (lambda () (org-show-context 'org-goto)))))
  12748. ;;; Fixes and Hacks for problems with other packages
  12749. ;; Make flyspell not check words in links, to not mess up our keymap
  12750. (defun org-mode-flyspell-verify ()
  12751. "Don't let flyspell put overlays at active buttons."
  12752. (not (get-text-property (point) 'keymap)))
  12753. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12754. (eval-after-load "bookmark"
  12755. '(if (boundp 'bookmark-after-jump-hook)
  12756. ;; We can use the hook
  12757. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12758. ;; Hook not available, use advice
  12759. (defadvice bookmark-jump (after org-make-visible activate)
  12760. "Make the position visible."
  12761. (org-bookmark-jump-unhide))))
  12762. (defun org-bookmark-jump-unhide ()
  12763. "Unhide the current position, to show the bookmark location."
  12764. (and (org-mode-p)
  12765. (or (org-invisible-p)
  12766. (save-excursion (goto-char (max (point-min) (1- (point))))
  12767. (org-invisible-p)))
  12768. (org-show-context 'bookmark-jump)))
  12769. ;; Make session.el ignore our circular variable
  12770. (eval-after-load "session"
  12771. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  12772. ;;;; Experimental code
  12773. (defun org-closed-in-range ()
  12774. "Sparse tree of items closed in a certain time range.
  12775. Still experimental, may disappear in the future."
  12776. (interactive)
  12777. ;; Get the time interval from the user.
  12778. (let* ((time1 (time-to-seconds
  12779. (org-read-date nil 'to-time nil "Starting date: ")))
  12780. (time2 (time-to-seconds
  12781. (org-read-date nil 'to-time nil "End date:")))
  12782. ;; callback function
  12783. (callback (lambda ()
  12784. (let ((time
  12785. (time-to-seconds
  12786. (apply 'encode-time
  12787. (org-parse-time-string
  12788. (match-string 1))))))
  12789. ;; check if time in interval
  12790. (and (>= time time1) (<= time time2))))))
  12791. ;; make tree, check each match with the callback
  12792. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  12793. ;;;; Finish up
  12794. (provide 'org)
  12795. (run-hooks 'org-load-hook)
  12796. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  12797. ;;; org.el ends here